Created
June 3, 2013 15:12
-
-
Save crmne/5698891 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Script to process background data (.ibs files) generated by the Optotrak 3020 system | |
| # Carmine Paolino <[email protected]> | |
| require 'csv' | |
| num_pattern = /[+-]?\d*\.?\d+/ | |
| allowed_frenquencies = [ 1, 2.5, 4 ] | |
| trials = [] | |
| class ListableAccessors | |
| def self.attr_accessor(*vars) | |
| @attributes ||= [] | |
| @attributes.concat vars | |
| super(*vars) | |
| end | |
| def self.attributes | |
| @attributes | |
| end | |
| end | |
| class Trial < ListableAccessors | |
| attr_accessor :subject | |
| attr_accessor :experiment | |
| attr_accessor :trial | |
| attr_accessor :background_motion_frequency | |
| attr_accessor :background_motion | |
| attr_accessor :direction | |
| attr_accessor :performance | |
| attr_accessor :background_phase | |
| attr_accessor :reaction_time | |
| attr_accessor :horizontal_error | |
| attr_accessor :vertical_error | |
| end | |
| data_folders = Dir["data_*_*_background"].delete_if { |x| x.include?("1") or x.include?("demo") } # exclude practice trials | |
| data_folders.each do |folder| | |
| Dir.chdir folder | |
| match_data = folder.match(/data_(\w+)_(\d+)_background/) | |
| subject = match_data[1] | |
| experiment = match_data[2].to_i | |
| ibs = Dir["*.ibs"].sort { |x,y| x.match(num_pattern)[0].to_i <=> y.match(num_pattern)[0].to_i } | |
| ibs.each_index do |i| | |
| print "." | |
| file = File.new(ibs[i]) | |
| file.seek(-1000, IO::SEEK_END) # optimizations! | |
| lines = IO.readlines(file) | |
| trial = Trial.new | |
| trial.subject = subject | |
| trial.experiment = experiment - 1 # first experiment is just practice | |
| trial.trial = i + 1 # don't start with 0 | |
| begin | |
| trial.background_motion_frequency = lines[-8].match(num_pattern)[0].to_f | |
| unless allowed_frenquencies.include? trial.background_motion_frequency | |
| raise "background_motion_frequency has invalid value #{trial.background_motion_frequency}. Valid values: #{allowed_frenquencies.inspect}" | |
| end | |
| trial.background_motion = lines[-7].include?("counter") ? "counter-clockwise" : "clockwise" | |
| trial.direction = lines[-17].include?("90") ? "upwards" : "rightwards" | |
| trial.performance = lines[-15].include?("hit") ? "hit" : "miss" | |
| trial.background_phase = lines[-6].match(num_pattern)[0].to_f | |
| trial.reaction_time = lines[-5].match(num_pattern)[0].to_f | |
| errors = lines[-1].scan(num_pattern) | |
| trial.horizontal_error = errors[0].to_f | |
| trial.vertical_error = errors[1].to_f | |
| trials << trial | |
| rescue => e | |
| $stderr.puts | |
| $stderr.puts "#{folder}/#{ibs[i]} skipped, reason: #{e.message}" | |
| end | |
| end | |
| Dir.chdir ".." | |
| end | |
| puts | |
| print "Saving CSV... " | |
| CSV.open("background_summary.csv", "wb") do |csv| | |
| csv << Trial.attributes # header | |
| trials.each do |trial| | |
| csv << Trial.attributes.map { |method| trial.send(method) } | |
| end | |
| end | |
| puts "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment