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