-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessSamples.py
More file actions
executable file
·134 lines (112 loc) · 5.06 KB
/
Copy pathpreprocessSamples.py
File metadata and controls
executable file
·134 lines (112 loc) · 5.06 KB
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
#!/usr/bin/env python3
#
# Takes one or more input files of samples and applies various preprocessors
# to the samples, and writes preprocessed samples to stdout
#
# Sample Class in sampleDataLib.py is responsible for the sample
# handling and preprocessors and is intended to encapsulate all the
# sample details.
#
# Assumes all input files have the same column structure. see Sample.
#
# Note if no preprocessor steps are specified, this will intelligently cat
# the sample files, collapsing down to 1 header line at the start of the output
#
# This script is intended to be independent of specific ML projects.
#
import sys
import string
import os
import time
import argparse
import sklearnHelperLib as skHelper
import utilsLib
DEFAULT_SAMPLEDATALIB = "sampleDataLib"
DEFAULT_SAMPLE_TYPE = "BaseSample"
#-----------------------------------
def parseCmdLine():
parser = argparse.ArgumentParser( \
description='Apply preprocessor steps to files of samples. Write to stdout')
parser.add_argument('inputFiles', nargs='+',
help='files of samples, "-" for stdin')
parser.add_argument('-p', '--preprocessor', metavar='PREPROCESSOR',
dest='preprocessors', action='append', required=False, default=[],
help='preprocessor, multiples are applied in order. Default is none.' )
parser.add_argument('--omitrejects', dest='omitRejects',
action='store_true',
help="don't write reject samples, default is write")
parser.add_argument('--sampledatalib', dest='sampleDataLib',
default=DEFAULT_SAMPLEDATALIB,
help="Module to import that defines python sample class. " +
"Default: %s" % DEFAULT_SAMPLEDATALIB)
parser.add_argument('--sampletype', dest='sampleObjTypeName',
default=DEFAULT_SAMPLE_TYPE,
help="Sample class name to use if not specified in sample file. " +
"Default: %s" % DEFAULT_SAMPLE_TYPE)
parser.add_argument('--report', dest='preprocessorReport',
default=None,
help="Write a preprocessor report to the specified file. " +
"Default: no report")
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
default=True, help="include helpful messages to stderr, default")
parser.add_argument('-q', '--quiet', dest='verbose', action='store_false',
required=False, help="skip helpful messages to stderr")
return parser.parse_args()
#----------------------
args = parseCmdLine()
sampleDataLib = utilsLib.importPyFile(args.sampleDataLib)
#----------------------
def main():
#----------------------
# get default sampleObjType
if not hasattr(sampleDataLib, args.sampleObjTypeName):
sys.stderr.write("invalid sample class name '%s'\n" \
% args.sampleObjTypeName)
exit(5)
sampleObjType = getattr(sampleDataLib, args.sampleObjTypeName)
verbose("Preprocessing steps: %s\n" % ' '.join(args.preprocessors))
totNumSamples = 0
totNumRejects = 0
firstFile = True
startTime = time.time()
for fn in args.inputFiles:
verbose("Preprocessing '%s'\n" % fn)
if fn == '-': fn = sys.stdin
sampleSet = sampleDataLib.SampleSet(sampleObjType).read(fn)
if firstFile:
sampleObjType = sampleSet.getSampleObjType()
verbose("Sample type: %s\n" % sampleObjType.__name__)
else:
if sampleObjType != sampleSet.getSampleObjType():
sys.stderr.write( \
"Input files have inconsistent sample types: %s & %s\n" % \
(sampleObjType.__name__,
sampleSet.getSampleObjType().__name__) )
exit(5)
rejected = sampleSet.preprocess(args.preprocessors)
sampleSet.write(sys.stdout, writeHeader=firstFile,
writeMeta=firstFile, omitRejects=args.omitRejects)
firstFile = False
numSamples = sampleSet.getNumSamples()
numRejects = len(rejected)
totNumSamples += numSamples
totNumRejects += numRejects
verbose('...done. %d samples, %d marked as reject\n' % \
(numSamples, numRejects))
if args.omitRejects: numWritten = totNumSamples - totNumRejects
else: numWritten = totNumSamples
if args.preprocessorReport \
and hasattr(sampleObjType, 'getPreprocessorReport'):
with open(args.preprocessorReport, 'w') as fp:
fp.write(sampleObjType.getPreprocessorReport())
verbose("Wrote preprocessor report to '%s'\n" % args.preprocessorReport)
verbose("Samples read: %d \t Samples written: %d\n" % \
(totNumSamples, numWritten))
verbose( "Total time: %8.3f seconds\n\n" % (time.time()-startTime))
# ---------------------
def verbose(text):
if args.verbose:
sys.stderr.write(text)
sys.stderr.flush()
# ---------------------
if __name__ == "__main__": main()