Beispiel #1
0
    def test_files(self):
        mydir = osp.dirname(__file__)
        if SEATBELT_FILE and osp.isfile(SEATBELT_FILE):
            res_file = SEATBELT_FILE
        else:
            tmpdir = tempfile.gettempdir()
            res_file = osp.join(tmpdir, 'co2mpas_seatbelt_demos.dill')

        log.info(
            "\n  OVERWRITE_SEATBELT: %s \n"
            "  RUN_INPUT_FOLDER: %s \n"
            "  RUN_ALL_FILES: %s \n"
            "  SEATBELT_FILE: %s", OVERWRITE_SEATBELT, RUN_INPUT_FOLDER,
            RUN_ALL_FILES, res_file)

        if not OVERWRITE_SEATBELT and osp.isfile(res_file):
            old_results = dsp_utl.load_dispatcher(res_file)
            log.info("Old results loaded!")
        else:
            old_results = None

        path = RUN_INPUT_FOLDER or osp.join(mydir, '..', 'co2mpas', 'demos')
        file = (path if (RUN_ALL_FILES or RUN_INPUT_FOLDER) else osp.join(
            path, 'co2mpas_demo-0.xlsx'))

        model = vehicle_processing_model()

        results = []

        inp_files = file_finder([file])
        if not inp_files:
            raise AssertionError("DataCheck found no input-files in %r!" %
                                 file)

        for fpath in inp_files:
            fname = osp.splitext(osp.basename(fpath))[0]
            log.info('Processing: %s', fname)

            inputs = {
                'input_file_name': fpath,
                'variation': {
                    'flag.only_summary': True
                }
            }
            r = model.dispatch(inputs=inputs)
            r = dsp_utl.selector(['report', 'summary'], r['solution'])
            r.get('report', {}).pop('pipe', None)
            results.append(sorted(dsp_utl.stack_nested_keys(r)))

        if not OVERWRITE_SEATBELT and osp.isfile(res_file):
            log.info('Comparing...')
            self._check_results(results, old_results)
        else:
            os.environ["OVERWRITE_SEATBELT"] = '0'
            dsp_utl.save_dispatcher(results, res_file)
            log.info('Overwritten seat belt %r.', res_file)
Beispiel #2
0
def make_simulation_plan(plan, timestamp, variation, flag, model=None):
    model, summary = model or batch.vehicle_processing_model(), {}
    run_base = model.get_node('run_base')[0].dsp
    run_modes = tuple(
        run_base.get_sub_dsp_from_workflow(
            ('data', 'vehicle_name'), check_inputs=False,
            graph=run_base.dmap).data_nodes) + ('start_time', 'vehicle_name')

    var = json.dumps(variation, sort_keys=True)
    o_cache, o_folder = flag['overwrite_cache'], flag['output_folder']
    modelconf = flag.get('modelconf', None)
    kw, bases = sh.combine_dicts(flag, {'run_base': True}), set()
    for (i, base_fpath, run), p in tqdm.tqdm(plan, disable=False):
        try:
            base = get_results(model, o_cache, base_fpath, timestamp, run, var,
                               o_folder, modelconf)
        except KeyError:
            log.warning('Base model "%s" of variation "%s" cannot be parsed!',
                        base_fpath, i)
            continue

        name = base['vehicle_name']
        if 'summary' in base and name not in bases:
            batch._add2summary(summary, base['summary'])
            bases.add(name)

        name = '{}-{}'.format(name, i)

        new_base, o = define_new_inputs(p, base)
        inputs = batch.prepare_data(new_base, {}, base_fpath, o_cache,
                                    o_folder, timestamp, False, modelconf)[0]
        inputs.update(sh.selector(set(base).difference(run_modes), base))
        inputs['vehicle_name'] = name
        inputs.update(kw)
        res = run_base.dispatch(inputs)
        batch.notify_result_listener(plan_listener, {'solution': res})

        s = filter_summary(p, o, res.get('summary', {}))
        base_keys = {
            'vehicle_name': (base_fpath, name, run),
        }
        batch._add2summary(summary, s, base_keys)

    return summary