Advertisement
Azdle

Ruby Data Processing Script

Dec 1st, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.26 KB | None | 0 0
  1. require 'csv'
  2. require 'ruport'
  3. #require 'ruport/util'
  4.  
  5. files = Array.new
  6.  
  7. folders = Array.new
  8.  
  9. def numeric?(object)
  10.   true if Float(object) rescue false
  11. end
  12.  
  13. def readcsvdata(file)
  14.     data = Hash.new()
  15.  
  16.     CSV.foreach(file, :quote_char => '"', :col_sep =>',', :row_sep =>:auto) do |row|
  17.         if not numeric?(row[0])
  18.             next
  19.         end
  20.    
  21.         data[row[0].to_f] = row[1].to_f
  22.  
  23.     end
  24.    
  25.     return data
  26. end
  27.  
  28.  
  29. #sampleId = RegEx.new(/AD\d\d\d\d/)
  30. #runNumber = RegEx.new(/\d[\d]/)
  31.  
  32. ARGV.each do|a|
  33.   folders.push a
  34. end
  35.  
  36. failWarningNeeded = false
  37.  
  38. folders.sort.each do |folder|
  39.     sampleId = folder.match(/AD\d\d\d\d/).to_s.match(/\d\d\d\d/)
  40.    
  41.     puts "Sample AD#{sampleId}"
  42.     dataTable = Ruport::Data::Table.new( :column_names => ["Sample", "Peel Extension (mm)", "Force Per Sample Width (N)", "Pass?"] ).sort_rows_by{["Sample"]}
  43.     if File.directory?(folder)
  44.         #puts "#{folder} is a Directory!"
  45.         Dir.foreach(folder) do |file|
  46.             next if file == '.' or file == '..' or file.match(/Specimen_RawData_\d+\.csv/) == nil
  47.  
  48.             #Parse File Name
  49.             #runNumber = "%02d" % file.match(/\d+\.csv/).to_s.match(/\d+/).to_s.to_i
  50.             runNumber = file.match(/\d+\.csv/).to_s.match(/\d+/).to_s.to_i
  51.            
  52.             data = readcsvdata(folder+"\\"+file) #returns a hash of {distance => force}
  53.  
  54.             firstDistPoint = data.keys.first #get starting distance
  55.  
  56.             minForce = data.select{ |k,v| k > (data.keys.first + 5)}.values.min #new hash of data after first 5 mm
  57.            
  58.             if minForce < 10
  59.                 pass = "-"
  60.                 failWarningNeeded = true
  61.             else
  62.                 pass = "+"
  63.             end
  64.            
  65.             dataTable << [runNumber, data.key(minForce)+firstDistPoint, minForce, pass ]
  66.            
  67.             #puts "Weakest Point for #{runNumber}\n Peel Extension #{minHash[0]}\n Force Per Area: #{minHash[1]}" #find lowest value of
  68.  
  69.             #Note: This does not check for a negative spike that the standard ignores.
  70.            
  71.  
  72.            
  73.         end
  74.     else
  75.         puts "Error: #{folder} is not a Directory!"
  76.     end
  77.     puts dataTable.sort_rows_by { |r| r.Sample }.to_html
  78.     puts "\n\n"
  79.    
  80.    
  81. end
  82.  
  83. puts "Possible Filure!\nOne or more of the samples failed, however this program does not check for a negative spike less than 1 mm in width that the standard ignores nor does it make sure that the sample is taught. You should double check any failure results." if failWarningNeeded == true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement