annotate micropita_prepare.py @ 1:caffff6e32e2

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