Created
December 4, 2015 19:13
-
-
Save Realio/363f6cf4a06292774e91 to your computer and use it in GitHub Desktop.
Creates a file called OP.csv with one line in it - Cycle Push Load Pull Load Push Average Pull Average
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
#!/usr/bin/env python | |
import os | |
import csv | |
#source_dir = "\\\gameshare2\IEB\Reliability\BRB-2015\\98-Non-FTE\\01-DataAndTestResults\\11-Headband\Data_Processor" | |
source_dir = "C:\cygwin64\home\Test Data" | |
#change source directory for testing | |
input_file_path = source_dir + "\IP.csv" | |
#can this be any .csv files? | |
output_file_path = source_dir + "\OP.csv" | |
#all should write to this directory file when completed | |
if os.path.isfile(output_file_path): | |
print(os.remove(output_file_path)) | |
#deletes the OS file if there is currently one in there taking up room | |
wfile = open(output_file_path, "at") | |
wfile.write("Cycle, Push Load, Pull Load, Push Average, Pull Average\n") | |
#open output file and create headers - creates OP if none present | |
class Averager: | |
def __init__(self): | |
self.count = 0 | |
self.sum = 0 | |
self.max = 0 | |
#class to store data | |
def add_value(self, value): | |
self.count = self.count + 1 | |
self.sum = self.sum + value | |
if abs(value) > abs(self.max): | |
self.max = abs(value) | |
#class to do calculations | |
def reset(self, value): | |
self.max = 0 | |
@property | |
def max(self): | |
return self.max | |
@property | |
def average(self): | |
return self.total / self.count | |
@property | |
def has_data(self): | |
return self.max != 0 | |
with open(output_file_path) as ipfile: | |
file_lines = csv.reader(ipfile, delimiter=',') | |
#read .csvfiles | |
cycle_num = 1 | |
pull_averager = Averager() | |
push_averager = Averager() | |
for row in file_lines: | |
if row[2] == '': | |
# not a valid row format | |
continue | |
current_load = float(row[1]) | |
if current_load * previous_load > 0: | |
# this implies the direction of force did not change | |
averager = push_averager if current_load > 0 else pull_averager | |
averager.add_value(current_load) | |
elif push_averager.has_data and pull_averager.has_data: | |
wfile.write("%d,%d,%d,%d,%d\n" % (cycle_num, push_averager.max, pull_averager.max, \ | |
push_averager.average, pull_averager.average)) | |
cycle_num = cycle_num + 1 | |
push_averager.reset() | |
pull_averager.reset() | |
previous_load = current_load |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment