-
Notifications
You must be signed in to change notification settings - Fork 2
/
updateFormants.py
133 lines (112 loc) · 4.75 KB
/
updateFormants.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import argparse, os, glob, subprocess, sys
pj,bn = os.path.join, os.path.basename
parser = argparse.ArgumentParser(description = 'Correct formant.txt files based on plotmish logs')
parser.add_argument('formant_files', help = 'directory containing formant.txt files or a single formant.txt file to be corrected')
parser.add_argument('-l','-logs', metavar = 'plotmish logs',default = 'log' , help = 'change folder containing plotmish correction logs. Can also be a single csv file. Default is log/')
parser.add_argument('-c','-corrected', metavar = 'corrected csvs', default = 'corrected', help = 'change folder to write corrected formant.txt files to, default is corrected/')
args = parser.parse_args()
if os.path.isdir('plotmish'):
os.chdir('plotmish')
## get log files
if os.path.isdir(args.l):
logs = glob.glob(pj(args.l, '*'))
else:
logs = [args.l]
## get formant.txt files to be corrected
if os.path.isdir(args.formant_files):
oldF = glob.glob(pj(args.formant_files,'*'))
else:
oldF = [args.formant_files]
## make directory to write out files if it doesn't exist
if not os.path.isdir(args.c):
subprocess.call(['mkdir', args.c])
## read config file
configF = open('config.txt','rU')
configList = [c.split('#')[0].strip().split(':') if '#' in c else c.strip() for c in configF.readlines() if c.split('#')[0]]
configF.close()
## define dictionaries of headings that will be changed in the corrected formant.txt file
headings = ['F1', 'F2', 'TIME', 'MAX FORMANTS']
configDict = {c[0].strip(): c[1].strip() for c in configList if c[0].strip() in headings}
revConfigDict = {v:k for k,v in configDict.items()}
newHeadings = ['plotmish - changed', 'plotmish - removed' , 'plotmish - note', 'plotmish - annotator']
## iterate over all log files
for l in logs:
name = bn(l).rsplit('-',1)
## check that file is named correctly
if name[1] != 'corrLog.csv':
print >> sys.stderr, 'Log file %r does not end in -corrLog.csv. File may have been renamed \nskipping...' %bn(l)
continue
oldForms = []
for o in oldF:
formName = bn(o).rsplit('-',1)
try:
oldForms += [o] if formName[0] == name[0] and formName[1] == 'formant.txt' else []
except: pass
##check that file corresponds to a formant.txt file
if not oldForms:
print >> sys.stderr, 'Log file %r does not correspond to a formant.txt file in %r \nskipping...' %(bn(l),bn(args.formant_files))
continue
## extract old formant files info to a list
oldFile = open(oldForms[0],'rU')
formList = [o.replace('\n','').split('\t') for o in oldFile.readlines()]
oldFile.close()
## find indexes to write the changes to
indexes = {i: None for i in headings}
badFile = True
for i,line in enumerate(formList):
headCount = 0
for head in configDict:
if configDict[head] in line: headCount += 1
if headCount == len(headings):
for j,li in enumerate(line):
try: indexes[revConfigDict[li]] = j
except: pass
badFile = False
topping = formList[:i]
formList = formList[i:]
updateFormlist = []
for n in newHeadings:
if n in line:
indexes.update({n.split('-')[1].strip().upper() : line.index(n)})
else:
indexes.update({n.split('-')[1].strip().upper() : len(formList[0])})
formList[0] += [n]
updateFormlist += [n]
if updateFormlist:
formList = [f+['']*len(updateFormlist) for f in formList]
break
if badFile:
print >> sys.stderr, 'Mandatory Headings not found','for file: '+ basename(oldForms[0])+'\nCannot write to file, continuing...'
continue
## extract logFile info to a list
logFile = open(l,'rU')
logList = [o.replace('\n','').split(',') for o in logFile.readlines()]
logFile.close()
## correct formant files
for ll in logList[1:]:
number = int(ll[1])
time = ll[5]
maxForms = ll[8]
F1 = ll[10]
F2 = ll[12]
annotator = ll[0]
try:
comment = ll[13]
except:
comment = 'corrected'
## change the values where appropriate
formList[number][indexes['F1']] = F1 if F1 != 'NA' else formList[number][indexes['F1']]
formList[number][indexes['F2']] = F2 if F2 != 'NA' else formList[number][indexes['F2']]
formList[number][indexes['TIME']] = time if time != 'NA' else formList[number][indexes['TIME']]
formList[number][indexes['MAX FORMANTS']] = maxForms if maxForms != 'NA' else formList[number][indexes['MAX FORMANTS']]
formList[number][indexes['CHANGED']] = 'Yes' if comment == 'corrected' else formList[number][indexes['CHANGED']]
formList[number][indexes['REMOVED']] = 'Yes' if comment != 'corrected' else formList[number][indexes['REMOVED']]
formList[number][indexes['ANNOTATOR']] = annotator
formList[number][indexes['NOTE']] = comment
## write to new formant.txt file
newFile = open(pj(args.c,name[0]+'-formant.txt'),'wb')
for t in topping:
newFile.write(' '.join(t)+'\n')
for fl in formList:
newFile.write('\t'.join(fl)+'\n')
newFile.close()