annotate micropita_prepare.py @ 1:cd71e90abfab draft

Updated version that suppresses the future warnings option that was causing a problem
author george-weingart
date Sun, 08 Feb 2015 18:27:54 -0500
parents 2f4f6f08c8c4
children b4cf8c75305b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
1 #!/usr/bin/env python
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
2
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
3 """
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
4 Author: George Weingart
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
5 Description: Prepare parameters to call micropita
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
6 """
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
7
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
8 #####################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
9 #Copyright (C) <2012>
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
10 #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
11 #Permission is hereby granted, free of charge, to any person obtaining a copy of
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
12 #this software and associated documentation files (the "Software"), to deal in the
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
13 #Software without restriction, including without limitation the rights to use, copy,
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
14 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
15 #and to permit persons to whom the Software is furnished to do so, subject to
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
16 #the following conditions:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
17 #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
18 #The above copyright notice and this permission notice shall be included in all copies
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
19 #or substantial portions of the Software.
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
20 #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
21 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
22 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
23 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
24 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
25 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
26 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
27 #####################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
28
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
29 __author__ = "George Weingart"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
30 __copyright__ = "Copyright 2012"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
31 __credits__ = ["George Weingart"]
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
32 __license__ = "MIT"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
33 __maintainer__ = "George Weingart"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
34 __email__ = "george.weingart@gmail.com"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
35 __status__ = "Development"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
36
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
37 import argparse
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
38 from cStringIO import StringIO
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
39 import sys,string,time
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
40 import os
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
41 from time import gmtime, strftime
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
42 from pprint import pprint
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
43 import subprocess
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
44 import blist
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
45 import shlex
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
46 import tempfile
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
47
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
48 ##################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
49 # Modification by George Weingart 5/6/2014 #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
50 # Using subprocess to invoke the calls to Micropita #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
51 # and allocating the temporary file using trmpfile #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
52 ##################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
53
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
54
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
55
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
56
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
57 ##################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
58 # Decode Parms #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
59 ##################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
60 def read_params(x):
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
61 parser = argparse.ArgumentParser(description='Micropita Annotate Argparser')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
62 parser.add_argument('--input', action="store",dest='inputname')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
63 parser.add_argument('--output', action="store",dest='outputname')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
64 parser.add_argument('-m', action="store",dest='MParameter')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
65 parser.add_argument('-n', action="store",dest='NSamples')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
66 parser.add_argument('--lastmeta', action="store",dest='lastmeta')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
67 parser.add_argument('--stratify_value', action="store",dest='stratify_value')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
68
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
69
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
70 try:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
71 parser.add_argument('--feature_method', action="store",dest='feature_method')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
72 except:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
73 pass
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
74 try:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
75 parser.add_argument('--targets', action="store",dest='targets')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
76 except:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
77 pass
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
78 try:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
79 parser.add_argument('--label_value', action="store",dest='label_value')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
80 except:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
81 pass
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
82 return parser
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
83
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
84
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
85 ##################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
86 # Main Program #
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
87 ##################################################################################
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
88 parser = read_params( sys.argv )
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
89 results = parser.parse_args()
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
90 root_dir = os.environ.get('micropita_SCRIPT_PATH')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
91
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
92
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
93 fname = results.inputname
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
94 input_file = open(fname,'rU')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
95 input_lines = input_file.readlines()
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
96 input_file.close()
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
97 table_lines = []
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
98 for x in input_lines:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
99 first_column = x.split('\t')[0]
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
100 table_lines.append(first_column)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
101
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
102
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
103
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
104 FileTimeStamp = strftime("%Y%m%d%H%M%S", gmtime())
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
105 LastMetaInt = 0
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
106 if results.lastmeta and not results.lastmeta == "None":
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
107 LastMetaInt = int(results.lastmeta) - 1
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
108
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
109 StratifyValueInt = 0
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
110 if results.stratify_value and not results.stratify_value == "None":
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
111 StratifyValueInt = int(results.stratify_value) - 2
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
112
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
113 LabelValueInt = 0
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
114 if results.label_value and not results.label_value == "None":
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
115 LabelValueInt = int(results.label_value) - 1
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
116
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
117 stratify_string = ""
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
118 q = '"'
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
119 if not results.stratify_value == '1':
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
120 stratify_string = " --stratify " + q + table_lines[StratifyValueInt] + q + " "
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
121
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
122 if results.MParameter == "features":
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
123 TBTargets = list()
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
124 TableTargets = results.targets.split(',')
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
125 for t in TableTargets:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
126 tb_entry = int(t) + LastMetaInt
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
127 TBTargets.append(int(tb_entry))
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
128
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
129
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
130 OutTargetsFile = tempfile.NamedTemporaryFile('w', delete=False )
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
131 TempTargetsFileName = OutTargetsFile.name
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
132 indx = -1
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
133 for c in table_lines:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
134 indx+=1
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
135 if indx in TBTargets:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
136 OutputString = table_lines[indx] + "\n"
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
137 OutTargetsFile.write(OutputString)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
138 OutTargetsFile.close()
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
139 os_command = "python " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
140 root_dir + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
141 "/MicroPITA.py "+\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
142 "--lastmeta " + table_lines[LastMetaInt]+ " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
143 "--feature_method " + results.feature_method + " " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
144 "--target " + TempTargetsFileName + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
145 "-m " + results.MParameter + " " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
146 "-n " + results.NSamples + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
147 stratify_string + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
148 results.inputname + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
149 results.outputname
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
150 #print os_command
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
151 os.system(os_command)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
152 argsx = shlex.split(os_command) #Split the command
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
153 try:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
154 subprocess.check_call(argsx , shell=False)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
155 except:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
156 print "The call to micropita failed============="
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
157 sys.exit(0)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
158
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
159
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
160
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
161 if results.MParameter == "representative"\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
162 or results.MParameter == "diverse"\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
163 or results.MParameter == "extreme":
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
164 os_command = "python " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
165 root_dir + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
166 "/MicroPITA.py "+\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
167 "--lastmeta " + table_lines[LastMetaInt]+ " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
168 "-m " + results.MParameter + " " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
169 "-n " + results.NSamples + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
170 stratify_string + " " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
171 results.inputname + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
172 results.outputname
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
173 argsx = shlex.split(os_command) #Split the command
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
174 try:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
175 ###os.system(os_command)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
176 subprocess.check_call(argsx , shell=False)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
177 except:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
178 print "The call to micropita failed============="
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
179 sys.exit(0)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
180
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
181
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
182
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
183
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
184 if results.MParameter == "distinct"\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
185 or results.MParameter == "discriminant":
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
186 os_command = "python " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
187 root_dir + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
188 "/MicroPITA.py "+\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
189 "--lastmeta " + table_lines[LastMetaInt]+ " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
190 "--label " + table_lines[LastMetaInt]+ " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
191 "-m " + results.MParameter + " " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
192 "-n " + results.NSamples + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
193 stratify_string + " " + \
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
194 results.inputname + " " +\
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
195 results.outputname
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
196 #print os_command
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
197 argsx = shlex.split(os_command) #Split the command
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
198 try:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
199 subprocess.check_call(argsx , shell=False)
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
200 except:
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
201 print "The call to micropita failed============="
2f4f6f08c8c4 Uploaded
george-weingart
parents:
diff changeset
202 sys.exit(0)