Main loop

This commit is contained in:
Florent Guiotte 2018-07-26 11:55:44 +02:00
parent 578249f644
commit 81d96823e8

View File

@ -19,6 +19,7 @@ import os
import datetime
from sklearn import metrics
from pathlib import Path
from operator import itemgetter
from sklearn.ensemble import RandomForestClassifier
@ -37,6 +38,23 @@ def setup_yaml():
yaml.add_representer(OrderedDict, represent_dict_order)
setup_yaml()
test_dir = Path('./Tests')
def update_queue():
tmp_queue = list()
for child in test_dir.iterdir():
if child.is_file() and child.suffix == '.yml':
tmp_queue.append({'expe_file': child, 'priority': get_priority(child)})
queue = sorted(tmp_queue, key=itemgetter('priority'))
return queue
def get_priority(yml_file):
with open(yml_file) as f:
expe = OrderedDict(yaml.safe_load(f)['expe'])
return expe['priority']
def run(expe_file):
with open(expe_file) as f:
@ -47,7 +65,6 @@ def run(expe_file):
### Keep track of time
kronos = Kronos()
start_time = time.time()
### Compute descriptors
descriptors = compute_descriptors(expe)
@ -61,18 +78,23 @@ def run(expe_file):
metrics = run_metrics(expe, classification)
kronos.time('metrics')
### Create report WIP WIP WIP WIP WIP WIP WIP
expe_report = OrderedDict()
### Create report
expe_report = create_report(kronos)
expe_report['supervisor'] = os.uname()[1]
### Name and write prediction
oname = '{}_{}'.format(Path(expe_file).stem, expe_hashes['global'][:6])
oname_tif = oname + '.tif'
triskele.write(oname_tif, classification)
for timev, datek in zip((start_time, end_time), ('start_date', 'end_date')):
expe_report[datek] = datetime.datetime.fromtimestamp(timev).strftime('Le %d/%m/%Y à %H:%M:%S')
ressources = kronos.get_times()
ressources['ram'] = None
expe_report['ressources'] = ressources
### Write report and results
oname_yml = oname + '.yml'
with open(oname_yml, 'w') as of:
yaml.dump(OrderedDict({'expe': expe,
'expe_hashes': expe_hashes,
'expe_report': expe_report,
'expe_classification': oname_tif,
'expe_results': metrics}),
of, default_flow_style=False, encoding=None, allow_unicode=True)
def compute_hashes(expe):
@ -121,7 +143,7 @@ def compute_classification(expe, descriptors):
return prediction
def compute_metrics(ground_truth, classication):
def compute_metrics(ground_truth, classification):
"""Return dict of metrics for ground_truth and classification prediction in parameters"""
f = np.nonzero(classification)
pred = classification[f].ravel()
@ -142,11 +164,26 @@ def run_metrics(expe, classification):
return compute_metrics(gt, classification)
def create_report(kronos):
expe_report = OrderedDict()
expe_report['supervisor'] = os.uname()[1]
for timev, datek in zip((kronos.get_start_date(), kronos.get_end_date()), ('start_date', 'end_date')):
expe_report[datek] = datetime.datetime.fromtimestamp(timev).strftime('Le %d/%m/%Y à %H:%M:%S')
ressources = kronos.get_times()
ressources['ram'] = None
expe_report['ressources'] = ressources
return expe_report
class Kronos(object):
def __init__(self):
self._pt = time.process_time()
self._times = OrderedDict()
self._stime = time.time()
def time(self, name):
self._times[name + '_process_time'] = time.process_time() - self._pt
@ -154,3 +191,25 @@ class Kronos(object):
def get_times(self):
return self._times
def get_start_date(self):
return self._stime
def get_end_date(self):
return time.time()
def watch_folder():
time.sleep(10)
def main():
while(True):
queue = update_queue()
if not queue:
watch_folder()
continue
run(queue.pop()['expe_file'])
if __name__ == '__main__':
main()