tvaLib
mot-saving-results.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # tvaLib Copyright (c) 2012-2016 Paul G. St-Aubin
3 # Ecole Polytechnique de Montreal, McGill University
4 # Python 2.7; (dt) Spyder Windows 10 64-bit; ipython Ubuntu 15.04 64-bit
5 version = 'R2.3.0 u. 2017-03-22'
6 
7 
8 
9 
10 
13 def main():
14  try:
15 
16 
20  import os, logging, sys, time
21  from shutil import copy as shutil_copy
22  from site import addsitedir
23  #Add parent library
24  if(os.path.isfile(os.path.join(os.getcwd(), os.pardir, 'main.py'))):
25  addsitedir(os.path.join(os.getcwd(), os.pardir))
26  os.chdir(os.path.join(os.getcwd(), os.pardir))
27 
29  import include.config as tvaConfig
30  import include.local as tvaLocal
31 
32  import lib.scene as tvaScene
33  import lib.tools as tvaLib
34  import include.runtime as tvaRuntime
35 
36 
39  commands = tvaConfig.commands()
40  config = tvaConfig.Config(readonly=False) #Set this to true if the script is running independantly of the main program
41  if(config.debug): logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s')
42  if(commands.dir): config.dir = commands.dir
43  if(commands.dbn): config.dbn = commands.dbn
44  if(commands.language): config.language = commands.language
45  local = tvaLocal.Local(config.language)
46  sites = tvaScene.Sites(config)
47  site_analyses = tvaScene.SiteAnalyses(config, sites=sites)
48 
49 
52  print('Running batch results creator, version '+version+'.')
53 
54 
55 
59  saIxs = site_analyses.interpret(commands.s_analysis)
60  IxIx = 0
61  for saIx in saIxs:
62  siteIxs = tvaRuntime.targetSiteIxs(commands, sites, site_analyses[saIx])
63  for siteIx in siteIxs:
64 
65 
66  siteList = tvaLib.Parse.list1D(commands.site)
67  cameraList = tvaLib.Parse.list1D(commands.camera)
68  if(len(siteList)>=2 and len(cameraList)==len(siteList)): camIxs = sites[siteIx].interpret(cameraList[IxIx])
69  else: camIxs = sites[siteIx].interpret()
70  for camIx in camIxs:
71  if(commands.verbose >= 2): print ' '+sites[siteIx][camIx].name
72 
73 
74 
75  fileIxs = sites[siteIx][camIx].interpret()
76  for fileIx in fileIxs:
77  filenumber = 0
78  if os.path.exists(os.path.join(site_analyses[saIx].getFullResultsFolder(), 'MOT-calibration.txt')):
79  with open(os.path.join(site_analyses[saIx].getFullResultsFolder(), 'MOT-calibration.txt'), 'r') as file: mot_saving = file.readlines()
80  numfiles=len(mot_saving)
81  upairsfile = 'cvl_'+sites[siteIx][camIx].name.replace('\\','').replace('/','')+'_'+sites[siteIx][camIx][fileIx].name+'.upairs'
82  while filenumber<numfiles:
83  print('Running Line ' + str(filenumber+1) + '/' + str(numfiles))
84  if(os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'results.csv'))):
85  os.remove(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'results.csv'))
86  if(os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'TTC_cvl.csv'))):
87  os.remove(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'TTC_cvl.csv'))
88  if(os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(),'roundaboutHLI','analysis.pva'))):
89  os.remove(os.path.join(site_analyses[siteIx].getFullResultsFolder(),'roundaboutHLI','analysis.pva'))
90  if(os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(),upairsfile))):
91  os.remove(os.path.join(site_analyses[siteIx].getFullResultsFolder(),upairsfile))
92  trialdata=mot_saving[filenumber]
93  trialnum=int(trialdata[7:trialdata.find(",")])
94  temploc=trialdata[trialdata.find("Generation #")+12:]
95  gennum=int(temploc[:temploc.find(",")])
96  temploc=trialdata[trialdata.find("File ")+5:]
97  filename=temploc[:31]
98  shutil_copy(os.path.join(config.dir, sites[siteIx].name, sites[siteIx][camIx].name, config.MOT_save_all_dir_name, 'Trial'+str(trialnum), 'Gen'+str(gennum), str(filename)), os.path.join(config.dir, sites[siteIx].name, sites[siteIx][camIx].name, str(commands.file)+'.sqlite'))
99  os.system('main.py -s ' + str(commands.site) + ' -c ' + str(commands.camera) + ' -f ' + str(commands.file) + ' -b ' + str(commands.s_analysis) + ' -a -u -i 0 -y cg')
100  if os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'results.csv')):
101  shutil_copy(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'results.csv'), os.path.join(site_analyses[siteIx].getFullResultsFolder(config.output_folder, customSubPath= os.path.join('MOT_Saving_'+str(commands.file), 'Trial'+str(trialnum), 'Gen'+str(gennum))), str(filename[:20]+'_results.csv')))
102  if os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'TTC_cvl.csv')):
103  shutil_copy(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'TTC_cvl.csv'), os.path.join(site_analyses[siteIx].getFullResultsFolder(config.output_folder, customSubPath= os.path.join('MOT_Saving_'+str(commands.file), 'Trial'+str(trialnum), 'Gen'+str(gennum))), str(filename[:20]+'_TTC_cvl.csv')))
104  if os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'roundaboutHLI', 'analysis.pva')):
105  shutil_copy(os.path.join(site_analyses[siteIx].getFullResultsFolder(), 'roundaboutHLI', 'analysis.pva'), os.path.join(site_analyses[siteIx].getFullResultsFolder(config.output_folder, customSubPath= os.path.join('MOT_Saving_'+str(commands.file), 'Trial'+str(trialnum), 'Gen'+str(gennum))), str(filename[:20]+'_HLI_analysis.pva')))
106  if os.path.exists(os.path.join(site_analyses[siteIx].getFullResultsFolder(), upairsfile)):
107  shutil_copy(os.path.join(site_analyses[siteIx].getFullResultsFolder(), upairsfile), os.path.join(site_analyses[siteIx].getFullResultsFolder(config.output_folder, customSubPath= os.path.join('MOT_Saving_'+str(commands.file), 'Trial'+str(trialnum), 'Gen'+str(gennum))), str(filename[:20]+'_cvl.upairs')))
108 
109  with open(os.path.join(site_analyses[saIx].getFullResultsFolder(), 'MOT-calibration.txt'), 'a') as f:
110  f.write(str(mot_saving[filenumber]))
111  filenumber+=1
112 
113  else:
114  print "MOT-calibration.txt file not found."
115 
116 
117 
118 
119  raise Exception, [0003, 'MOTA processing complete.']
120  sys.exit()
121 
122 
126  except KeyboardInterrupt:
127  if(commands.verbose): print('==User exited== [0000]\nRuntime: {0}'.format(round(time.clock())))
128 
129  except SystemExit, e:
130  try:
131  if(commands.verbose): print('==Execution finished== [0001]\nRuntime: {0}'.format(round(time.clock())))
132  except UnboundLocalError: pass
133 
134  except Exception, e:
135  if('commands' in dir()): verbose = commands.verbose
136  else: verbose = 1
137  if('config' in dir()): debug = config.debug
138  else: debug = True
139  from include.runtime import debug as tvaRuntime_debug
140  tvaRuntime_debug(e, time, logging=logging, verbose=verbose, force=debug)
141 
142 
145 if __name__ == '__main__':
146  main()