-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcurvefix.py
executable file
·257 lines (196 loc) · 6.25 KB
/
curvefix.py
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
'''
Small script to fix raw data issues
'''
# Copyright (C) 2016 by Jacob Alexander
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from collections import namedtuple
import argparse
import os
import sys
### Decorators ###
## Print Decorator Variables
ERROR = '\033[5;1;31mERROR\033[0m:'
WARNING = '\033[5;1;33mWARNING\033[0m:'
## Python Text Formatting Fixer...
## Because the creators of Python are averse to proper capitalization.
textFormatter_lookup = {
"usage: " : "\033[1mUsage\033[0m: ",
"optional arguments" : "\033[1mOptional Arguments\033[0m",
}
def textFormatter_gettext( s ):
return textFormatter_lookup.get( s, s )
argparse._ = textFormatter_gettext
### Argument Parsing ###
def checkFileExists( filename ):
'''
Validate that file exists
@param filename: Path to file
'''
if not os.path.isfile( filename ):
print ( "{0} {1} does not exist...".format( ERROR, filename ) )
sys.exit( 1 )
def command_line_args():
'''
Initialize argparse and process all command line arguments
'''
# Setup argument processor
parser = argparse.ArgumentParser(
usage="%(prog)s [options..] <file>",
description="Raw data fixing script, takes raw.gz files.",
epilog="Example: {0} olivetti.raw.gz".format( os.path.basename( sys.argv[0] ) ),
formatter_class=argparse.RawTextHelpFormatter,
add_help=False,
)
parser.add_argument( 'input_file', help=argparse.SUPPRESS ) # Suppressed help output
# Optional Arguments
parser.add_argument(
'-h', '--help',
action="help",
help="This message."
)
# Process Arguments
args = parser.parse_args()
return args.input_file
### Processing ###
ForceDataPoint = namedtuple(
'ForceDataPoint',
'time distance force_adc force_serial continuity direction'
)
def data_point( line ):
'''
Data point processing
'''
from force import ForceCurveDataPoint # noqa
# TODO Do something better than this -HaaTa
point = eval( line )
# Don't bother if the distance is 0 (Invalid reading)
if point.distance == 0:
return
# TODO Make options for different point "accumulation" methods
# - All
# - Average per distance tick
# - Increase distance precision using speed, time stamps and force ticks
data_point = ForceDataPoint(
point.time,
point.distance,
point.force_adc,
point.force_serial,
point.continuity,
point.direction,
)
return data_point
def next_test_starting_fix( input_file ):
'''
Iterates over the file looking for missing "Next Test Starting" tags
When the direction changes from 1 (up) to 2 (down), the next tag should be "Next Test Starting"
Changes are saved to a new file, append with .new
@param input_file: File to process
'''
import gzip
rawfile = gzip.open( input_file, 'rt' )
outfile_path = "{0}.new.gz".format( input_file )
outfile = gzip.open( outfile_path, 'wt' )
# Event tags
events = {
"Starting Test/Calibration" : "start",
"Next Test Starting" : "next_test",
"Test Complete" : "finish",
}
# Data points
data_points = {
"ForceCurveCalibrationPoint" : "calibration_point",
"ForceCurveDataPoint" : "data_point",
}
last_direction = 0
looking_for_next_test = False
test_count = 0
fixes = 0
linecount = 0
last_line_found = False
# Read file line-by-line
for line in rawfile:
linecount += 1
if line is None:
continue
# Check to see if we found test finished
if 'Test Complete' in line:
last_line_found = True
# Make sure we have a start
if linecount == 1 and 'Starting Test/Calibration' not in line:
# Insert Test Start
outfile.write('Starting Test/Calibration\n')
# Update stats
fixes += 1
# Check for event, call the associated function
event = [ event for event in events.keys() if event in line ]
# Reset check if we found the tag
if 'Next Test Starting' in event:
looking_for_next_test = False
test_count += 1
outfile.write( line )
continue
# Check to see if we were looking for Next Test Starting
if looking_for_next_test:
# Insert Next Test Starting
outfile.write("Next Test Starting\n")
# Update stats
fixes += 1
looking_for_next_test = False
outfile.write( line )
# Ignore other events
if any( event ):
continue
# Check for data point, call the associated function
point = [ point for point in data_points.keys() if point in line ]
# Check ForceCurveDataPoint to see the current direction
if 'ForceCurveDataPoint' in point:
direction = data_point( line ).direction
# Check for condition
if direction == 2 and last_direction == 1:
looking_for_next_test = True
# Store for next check
last_direction = direction
continue
# Ignore other points
if any( point ):
continue
# If we haven't found the last line, add it
if not last_line_found:
outfile.write('Test Complete')
# Update stats
fixes += 1
rawfile.close()
outfile.close()
# Move files
os.rename( input_file, "{0}.old.gz".format( input_file ) )
os.rename( outfile_path, input_file )
# Stats
print("-- Next Test Fix Stats --")
print( "Found: {0}".format( test_count ) )
print( "Fixed: {0}".format( fixes ) )
### Main Entry Point ###
if __name__ == '__main__':
# Process Command-Line Args
input_file = command_line_args()
# Check for missing "Next Test Starting"
next_test_starting_fix( input_file )
# Successful Execution
sys.exit( 0 )