From 81d96823e851f7a3d44ca8e0359a1031bb474406 Mon Sep 17 00:00:00 2001 From: Karamaz0V1 Date: Thu, 26 Jul 2018 11:55:44 +0200 Subject: [PATCH] Main loop --- supervisor.py | 87 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 14 deletions(-) diff --git a/supervisor.py b/supervisor.py index 0a5b7ab..9514634 100644 --- a/supervisor.py +++ b/supervisor.py @@ -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,19 +78,24 @@ def run(expe_file): metrics = run_metrics(expe, classification) kronos.time('metrics') - ### Create report WIP WIP WIP WIP WIP WIP WIP -expe_report = OrderedDict() - -expe_report['supervisor'] = os.uname()[1] - -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 + ### Create report + expe_report = create_report(kronos) + + ### Name and write prediction + oname = '{}_{}'.format(Path(expe_file).stem, expe_hashes['global'][:6]) + oname_tif = oname + '.tif' + triskele.write(oname_tif, classification) + ### 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): glob = hashlib.sha1() @@ -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() \ No newline at end of file