-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmeta_to_meta.py
74 lines (62 loc) · 2.43 KB
/
meta_to_meta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
import argparse
from lib.io_utils import *
from lib.math_utils import *
from lib.processing_utils import *
import os
from pprint import pprint
import re
import sys
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="tmp/ia_globallives_all.csv", help="Input file")
parser.add_argument('-key', dest="COLUMN_KEY", default="filename", help="Column key to match against")
parser.add_argument('-pattern', dest="PATTERN", default=".*([0-2]\d):?([0-5]\d):?([0-5]\d)[ \-_]+([0-2]\d):?([0-5]\d):?([0-5]\d).*", help="File pattern")
parser.add_argument('-features', dest="PATTERN_FEATURES", default="hh0,mm0,ss0,hh1,mm1,ss1", help="Features that the pattern maps to")
parser.add_argument('-out', dest="OUTPUT_FILE", default="", help="CSV output file; leave blank if update the same file")
parser.add_argument('-probe', dest="PROBE", action="store_true", help="Show plot?")
parser.add_argument('-overwrite', dest="OVERWRITE", action="store_true", help="Overwrite value if it already exists?")
a = parser.parse_args()
# Parse arguments
INPUT_FILE = a.INPUT_FILE
COLUMN_KEY = a.COLUMN_KEY
OUTPUT_FILE = a.OUTPUT_FILE if len(a.OUTPUT_FILE) > 0 else INPUT_FILE
PATTERN = a.PATTERN.strip()
PATTERN_FEATURES = a.PATTERN_FEATURES.strip().split(",")
PROBE = a.PROBE
# Read data
headings, rows = readCsv(INPUT_FILE)
for feature in PATTERN_FEATURES:
if feature not in headings:
headings.append(feature)
# Make sure output dirs exist
makeDirectories(OUTPUT_FILE)
pattern = re.compile(PATTERN)
rowCount = len(rows)
noMatchCount = 0
noMatches = []
for i, row in enumerate(rows):
matches = pattern.match(str(row[COLUMN_KEY]))
if not matches:
# if a.PROBE:
# print("Did not match: %s" % row[COLUMN_KEY])
noMatchCount += 1
noMatches.append(row[COLUMN_KEY])
elif a.PROBE:
print("Matched %s" % row[COLUMN_KEY])
for j, feature in enumerate(PATTERN_FEATURES):
# check to see if we can overwrite
if feature in row and not a.OVERWRITE and len(str(row[feature])) > 0:
continue
if matches:
rows[i][feature] = matches.group(j+1)
else:
rows[i][feature] = ""
printProgress(i+1, rowCount)
print("Matched %s files" % (rowCount-noMatchCount))
print("Did not match %s files" % noMatchCount)
if a.PROBE:
if len(noMatches) > 0:
pprint(noMatches)
sys.exit()
writeCsv(OUTPUT_FILE, rows, headings)