-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
163 lines (148 loc) · 6.41 KB
/
script.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#
# Copyright © 2016 Andrea Bianco
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# It is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public
# License along with eprimeConvert. If not, see <http://www.gnu.org/licenses/>.
#
import codecs
import csv
import os
import sys
import glob
import re
import itertools
totPatients = int(sys.argv[1])
walk_dir = sys.argv[2]
outFile_name = sys.argv[3]
print('walk_dir = ' + walk_dir)
# print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
regexp = ['null']
regexpN = ['null']
regexp[0] = 'FRU[01][0-9]_Nback.txt'
regexpN[0] = re.compile('.*\/FRU(.+?)_Nback.txt')
regexp.append('GLU[01][0-9]_Nback.txt')
regexpN.append(re.compile('.*\/GLU(.+?)_Nback.txt'))
regexp.append('H2O[01][0-9]_Nback.txt')
regexpN.append(re.compile('.*\/H2O(.+?)_Nback.txt'))
regexR = re.compile('.*\/(.+?)_Nback.txt')
patientList = ['null']
lastPatient = 0
patient = 1
patientFileList = [['null'] * totPatients, ['null'] * totPatients, ['null'] *
totPatients]
for s in range(len(regexp)):
for filename in glob.iglob(os.path.abspath(walk_dir) + '/' + regexp[s]):
try:
patientN = regexpN[s].search(filename).group(1)
patientFileList[s][int(patientN) - 1] = filename
except AttributeError:
print('Error file: '+filename)
print(patientFileList)
fout = open(outFile_name, 'w')
writer = csv.writer(fout, delimiter=',')
writer.writerow([' ', '0-back_corr', '0-back_err', '1-back_corr', '1-back_err',
'1-back_corr', '1-back_err'])
for i, j in itertools.product(range(3), range(totPatients)):
print(i, ' ', j)
print(patientFileList[i][j])
if(os.path.isfile(patientFileList[i][j])):
with codecs.open(patientFileList[i][j], encoding='utf-16') as f:
flag = 'wait'
procedure = False
stimulus = False
stimulusMix = False
trialMix = False
correctAns = False
running = False
acc = False
resp = False
cresp = False
nFrame = 0
nBunch = 0
zero_back_corr = 0
zero_back_err = 0
one_back_corr = 0
one_back_err = 0
two_back_corr = 0
two_back_err = 0
for line in f:
if 'LogFrame Start' in line:
flag = 'start'
stopRelease = True
elif 'LogFrame End' in line:
flag = 'stop'
if flag == 'start':
if ':' in line:
valueList = line.split()
if len(valueList) > 1:
value = line.split()[1]
else:
value = False
if 'Procedure:' in line:
procedure = value if value else False
elif 'Trial' in line:
trialMix = value if value else False
elif 'Running' in line:
running = value if value else False
elif 'Stimulus:' in line:
stimulus = value if value else False
elif 'CorrectAnswer:' in line:
correctAns = value if value else False
elif 'Stimulus.RESP:' in line:
resp = value if value else False
elif 'Stimulus.ACC:' in line:
acc = value if value else False
elif 'Stimulus.CRESP:' in line:
cresp = value if value else False
elif 'Stimulus.' in line:
stimulusMix = value if value else False
else:
stimulus = False
correctAns = False
resp = False
acc = False
elif flag == 'stop':
if(stopRelease):
stopRelease = False
if(stimulus is not False):
if(nFrame < 14):
nFrame = nFrame + 1
else:
nFrame = 1
nBunch = nBunch + 1
# 0 nback
if ((nBunch == 0) or (nBunch == 2) or (nBunch == 4)
or (nBunch == 6) or (nBunch == 8)):
if(acc == '1'):
zero_back_corr = zero_back_corr + 1
elif(acc == '0'):
zero_back_err = zero_back_err + 1
# 1 nback
if ((nBunch == 1) or (nBunch == 7)):
if(acc == '1'):
one_back_corr = one_back_corr + 1
elif(acc == '0'):
one_back_err = one_back_err + 1
# 2 nback
if ((nBunch == 3) or (nBunch == 5) or
(nBunch == 9)):
if(acc == '1'):
two_back_corr = two_back_corr + 1
elif(acc == '0'):
two_back_err = two_back_err + 1
# print(nBunch, ' ', nFrame)
# print('c: ', zero_back_corr, ' ', zero_back_err)
# print('c: ', one_back_corr, ' ', one_back_err)
# print('c: ', two_back_corr, ' ', two_back_err)
# print(stis, ' ', correctAns, ' ', resp, ' ', acc)
# end for
writer.writerow([regexR.search(patientFileList[i][j]).group(1),
zero_back_corr, zero_back_err, one_back_corr,
one_back_err, two_back_corr, two_back_err])