This repository has been archived by the owner on Jul 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward.rb
executable file
·53 lines (40 loc) · 2.06 KB
/
forward.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
#!/usr/bin/env ruby
#print ARGV[0]
# loop across this to append to csv to get all the forward files, but for now, one file
workingfile = ARGV[0]
trial = 1
# only if this is a real file, do the needful
if File.file?(workingfile)
# Search the files for 'subject or session' then grab the first result, then split that result down the last word (the number)
subject = File.open(workingfile).grep(/Subject:/).first.split.last
session = File.open(workingfile).grep(/Session:/).first.split.last
# set the subject/session info at the beginning of the row
#datarecord = "#{subject},#{session},"
datarecord = ''
# search through the file for any possible values we may want and create an array of all of it
bigpull = File.open(workingfile).grep(/Procedure|First|Second|Third|Fourth|DigitSpan|Fifth|Sixth|Seventh|Eighth|KeyPress1RT|KeyPress|KeyPress1RT|KeyPress1RESP|KeyPress2RT|KeyPress2RESP|KeyPress3RT|KeyPress3RESP|KeyPress4RT|KeyPress4RESP|KeyPress5RT|KeyPress5RESP|KeyPress6RT|KeyPress6RESP|KeyPress7RT|KeyPress7RESP|KeyPress8RT|KeyPress8RESP/)
# loop to clean whitespace from ALL the strings in the array (even strings we don't want)
bigpull.each do |x|
x = x.strip! # the ! permanently writes back to the array instead of the temporary values
end
# grab the first instance of the TrialProcMain line, which is the start of the REAL data
first_idx = bigpull.index('Procedure: Length4')
#puts bigpull
# remove the first chunk of the array before the pieces of info that matter -- this drops all the test data
bigpull.shift(first_idx)
bigpull.each do |x|
# skip the entries that say "Procedure" etc, because they don't actually contain data
if x =~ /Procedure/
datarecord += "\n"
datarecord += "#{subject},"
datarecord += "#{session},"
datarecord += "#{trial},"
trial = trial + 1
else
# if it's the sample number we insert a newline to start a new row
datarecord += "#{x.split.last},"
end
end
#puts datarecord
File.open('forward_ruby_out.csv', 'a') {|f| f.write(datarecord) }
end