-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggersNEU.rb
237 lines (192 loc) · 5.39 KB
/
triggersNEU.rb
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
######################
#Program to reorganize ERP-Trigger #
######################
require 'fileutils' #to allow for copying files
# some regular expressions used later on
reg = /^[^Mk]/ #everything that's not starting with Mk, so the header!
regM = /^Mk([0-9]+)=[^Stimulus]/ #Markers that are not of type "stimulus"
reg1 = /^Mk([0-9]+)=Stimulus,(S 1),([0-9]+),0,0/ #Marker of standards
reg2 = /^Mk([0-9]+)=Stimulus,(S 2|S 3|S 4),([0-9]+),0,0/ #markers of deviants
reg3 = /^Mk([0-9]+)=Stimulus,(S[^,]*),([0-9]+),0,0/ #all stimulus markers
# some variables used later on
markerID = 0
problems = []
# addMarker method to add markers based on existing markers
def addMarker f, m, mrk, t
out = File.open(f,"a")
out.puts "Mk#{m}=Stimulus,#{mrk},#{t},0,0"
out.close
end
##### get files to process ######
files = Dir["*.vmrk"]
#### check files on data structure #####
puts "Checking files on data structure!"
x = files.length.to_f/10
y = files.length.to_f/10
files.each {|file|
if files.length <= 10
puts "#{((files.index(file)+1)/files.length.to_f*100).to_i} %"
elsif file == files.last
puts "100%"
else
if file == files[x]
puts "#{((files.index(file)+1)/files.length.to_f*100).to_i} %"
x = x+y
end
end
array = []
buffer=[]
output=[]
finalarray=[]
array = IO.readlines(file.to_s)
array.delete_if {|x| x =~ /^[^Mk]/ || x =~ /^Mk([0-9]+)=[^Stimulus]/}
while array.length > 0 do
buffer[0] = array[0]
buffer[1] = array[1]
buffer[2] = array[2]
finalarray.push buffer
buffer = []
array.delete_at(0)
array.delete_at(0)
array.delete_at(0)
end
unless finalarray.last[0] =~ /^[Mk]/ && finalarray.last[1] =~ /^[Mk]/ && finalarray.last[2] =~ /^[Mk]/
problems.push file
puts "#{file} has a problem"
out = File.open("#{file}.txt","w")
out.puts finalarray.inspect
out.close
end
}
puts ""
if problems.length > 0
puts "You have problems to fix, see filenames above!"
Process.exit
else puts "Done! Processing Files:"
end
x = files.length.to_f/10
y = files.length.to_f/10
### Process files ##########
files.each {|file|
if files.length <= 10
puts "#{((files.index(file)+1)/files.length.to_f*100).to_i} %"
elsif file == files.last
puts "100%"
else
if file == files[x]
puts "#{((files.index(file)+1)/files.length.to_f*100).to_i} %"
x = x+y
end
end
array = []
buffer=[]
output=[]
finalarray=[]
#copy marker files in new directory and switch to it
unless File.directory?("NewMarkers")
Dir.mkdir("NewMarkers")
end
FileUtils.copy(file.to_s, "NewMarkers/#{file.to_s}")
Dir.chdir("NewMarkers")
#edit marker files in new directory
array = IO.readlines(file.to_s)
# header stuff to output and delete from array
array.each {|entry|
if entry =~ reg || entry =~ regM
output.push entry
end
}
array.delete_if {|x| x =~ reg || x =~ regM}
# make triplets of triggers in big array
while array.length > 0 do
buffer[0] = array[0]
buffer[1] = array[1]
buffer[2] = array[2]
finalarray.push buffer
buffer = []
array.delete_at(0)
array.delete_at(0)
array.delete_at(0)
end
# reverse array
finalarray.reverse!
#keep only triplets of triggers S2,S3 and S4 as well as S1 if preceded by deviant!
finalarray.each {|a|
if a[0] =~ reg2
output.push a
elsif a[0] =~ reg1 && buffer[0]=~reg2
output.push a
end
buffer[0] = a[0]
}
# use sort-functie to sort array properly so last marker ID can be determined
output.flatten!
output = output.sort {|a,b|
markerA = a.split('=')[0].gsub('Mk', '')
markerB = b.split('=')[0].gsub('Mk', '')
markerA.to_i <=> markerB.to_i
}
# write reduced amount of triggers (sorted) to file
out = File.open(file.to_s,"w")
out.puts output
out.close
#####
#get last MarkerID to add up to...
#####
if output.last =~ reg3
info = reg3.match(output.last)
markerID = info[1].to_i + 1
end
if file =~ /(AV|VO)/
output.each {|line|
if line =~ reg3
info = reg3.match(line)
time = info[3].to_i + 110
timeVis = info[3].to_i + 60
if info[2] == "S 1"
addMarker file.to_s, markerID, "Std_vis", timeVis.to_s
markerID = markerID +1
addMarker file.to_s, markerID, "Std_aud", time.to_s
markerID = markerID +1
elsif info[2] == "S 2"
addMarker file.to_s, markerID, "Dev1_vis", timeVis.to_s
markerID = markerID +1
addMarker file.to_s, markerID, "Dev1_aud", time.to_s
markerID = markerID +1
elsif info[2] == "S 3"
addMarker file.to_s, markerID, "Dev2_vis", timeVis.to_s
markerID = markerID +1
addMarker file.to_s, markerID, "Dev2_aud", time.to_s
markerID = markerID +1
elsif info[2] == "S 4"
addMarker file.to_s, markerID, "McG_vis", timeVis.to_s
markerID = markerID +1
addMarker file.to_s, markerID, "McG_aud", time.to_s
markerID = markerID +1
end
end
}
else output.each {|line|
if line =~ reg3
info = reg3.match(line)
time = info[3].to_i + 110
if info[2] == "S 1"
addMarker file.to_s, markerID, "Std_aud", time.to_s
markerID = markerID +1
elsif info[2] == "S 2"
addMarker file.to_s, markerID, "Dev1_aud", time.to_s
markerID = markerID +1
elsif info[2] == "S 3"
addMarker file.to_s, markerID, "Dev2_aud", time.to_s
markerID = markerID +1
elsif info[2] == "S 4"
addMarker file.to_s, markerID, "McG_aud", time.to_s
markerID = markerID +1
end
end
}
end
Dir.chdir("..")
}
puts ""
puts "Done!"