Example #1
0
    def testGoodVehicle(self, plot_results=False):
        logging.getLogger().setLevel(logging.DEBUG)

        mdl = goodVehicle()

        experiment = Experiment(mdl)
        mdl = experiment.run()
        self.assertTrue("cycle" in mdl, 'No result "cycle" in Model: %s' % mdl)

        print("DRIVEABILITY: \n%s" % experiment.driveability_report())
        cycle = mdl["cycle"]
        gears = cycle["gears"]
        print(
            "G1: %s, G2: %s"
            % (np.count_nonzero(gears == 1), np.count_nonzero(gears == 2))
        )

        self._compare_exp_results(cycle, "goodveh", self.run_comparison)

        if plot_results:
            print(mdl["cycle"])
            # print([get_wltc_data()['classes']['class3b']['cycle'][k] for k in mdl['cycle']['driveability_issues'].keys()])
            self._plotResults(mdl)

            np.set_printoptions(edgeitems=16)
            # print(driveability_issues)
            # print(v_max)
            # results['target'] = []; print(results)
            plt.show()
Example #2
0
    def test_two_ramps_smoke_test(self):
        mdl = goodVehicle()

        V = np.hstack((np.r_[0:100:2], np.r_[100:0:-2]))
        mdl['cycle_run'] = {'v_target': V}

        experiment = Experiment(mdl)
        mdl = experiment.run()
Example #3
0
    def test_badCycle(self):
        mdl = goodVehicle()
        mdl["cycle"] = 1

        with self.assertRaisesRegex(
                PandasError, "DataFrame constructor not properly called"):
            experiment = Experiment(mdl)
            mdl = experiment.run()
Example #4
0
    def test_two_ramps_smoke_test(self):
        mdl = goodVehicle()

        V = np.hstack((np.r_[0:100:2], np.r_[100:0:-2]))
        mdl['cycle_run'] = {'v_target': V}

        experiment = Experiment(mdl)
        mdl = experiment.run()
Example #5
0
    def test_badCycle(self):
        mdl = goodVehicle()

        mdl['params'] = params = {}
        mdl['cycle_run'] = 1

        with assertRaisesRegex(self, PandasError, 'DataFrame constructor not properly called'):
            experiment = Experiment(mdl)
            mdl = experiment.run()
Example #6
0
    def test_two_ramps_smoke_test(self):
        mdl = goodVehicle()
        mdl = datamodel.upd_resistance_coeffs_regression_curves(mdl)

        V = np.hstack((np.r_[0:100:2], np.r_[98:0:-2]))
        mdl["cycle"] = {"v_target": V}

        experiment = Experiment(mdl)
        mdl = experiment.run()
Example #7
0
    def test_badCycle(self):
        mdl = goodVehicle()

        mdl['params'] = params = {}
        mdl['cycle_run'] = 1

        with assertRaisesRegex(self, PandasError,
                               'DataFrame constructor not properly called'):
            experiment = Experiment(mdl)
            mdl = experiment.run()
Example #8
0
def _run_the_experiments(
    transplant_original_gears=False,
    plot_results=False,
    compare_results=False,
    encoding="ISO-8859-1",
):
    # rated_power,kerb_mass,rated_speed,idling_speed,test_mass,no_of_gears,ndv_1,ndv_2,ndv_3,ndv_4,ndv_5,ndv_6,ndv_7,ID_cat,user_def_driv_res_coeff,user_def_power_curve,f0,f1,f2,Comment
    # 0                                                            5                                10                                                    15                        19
    df = read_vehicle_data()

    for (ix, row) in df.iterrows():
        veh_num = ix

        model = goodVehicle()
        veh = model["vehicle"]

        veh["test_mass"] = row["test_mass"]
        veh["unladen_mass"] = veh["test_mass"] - driver_weight
        veh["f0"] = row["f0"]
        veh["f1"] = row["f1"]
        veh["f2"] = row["f2"]
        veh["p_rated"] = row["rated_power"]
        veh["n_rated"] = row["rated_speed"]
        veh["n_idle"] = int(row["idling_speed"])
        ngears = int(row["no_of_gears"])
        veh["gear_ratios"] = list(row[6:6 + ngears])  #'ndv_1'

        if transplant_original_gears:
            log.warning(">>> Transplanting gears from Heinz's!")
            df_h = read_heinz_file(veh_num)

            forced_cycle = df_h["g_max"]
            forced_cycle.columns = ["gears_orig"]

            model["forced_cycle"] = forced_cycle

        experiment = Experiment(model)
        model = experiment.run()

        params = model["params"]

        f_downscale = params["f_downscale"]
        if f_downscale > 0:
            log.warning(">> DOWNSCALE %s", f_downscale)

        # ankostis_mdb:  't', "v in km/h","v_orig","a in m/s²","gear","g_min","g_max","gear_modification","error_description"
        # heinz:         't', 'km_h', 'stg', 'gear'

        (root, ext) = os.path.splitext(vehs_data_inp_fname)
        outfname = os.path.join(mydir, samples_dir,
                                "{}-{:05}{}".format(root, veh_num, ext))
        df = pd.DataFrame(model["cycle"])

        _compare_exp_results(df, outfname, compare_results)
        df.to_csv(outfname, index_label="time")
Example #9
0
    def testUnderPowered(self, plot_results=False):
        mdl = goodVehicle()
        mdl["vehicle"]["p_rated"] = 50

        experiment = Experiment(mdl)
        mdl = experiment.run()
        print("DRIVEABILITY: \n%s" % experiment.driveability_report())
        self._compare_exp_results(mdl["cycle"], "unpower1", self.run_comparison)

        mdl = goodVehicle()
        veh = mdl["vehicle"]
        veh["test_mass"] = 1000
        veh["unladen_mass"] = veh["test_mass"] - driver_weight
        veh["p_rated"] = 80
        veh["v_max"] = 120
        veh["gear_ratios"] = [120.5, 95, 72, 52]

        experiment = Experiment(mdl)
        mdl = experiment.run()
        print("DRIVEABILITY: \n%s" % experiment.driveability_report())
        self._compare_exp_results(mdl["cycle"], "unpower2", self.run_comparison)

        if plot_results:
            self._plotResults(mdl)
            plt.show()
Example #10
0
def run_pyalgo_on_accdb_vehicle(
    h5,
    vehnum,
    additional_properties=False,
    props_group_suffix="prop",
    pwot_group_suffix="wot",
) -> Tuple[dict, pd.DataFrame, pd.DataFrame]:
    """
    Quick 'n dirty way to invoke python-algo (bc model will change).

    :param h5:
        the `WltpGs-msaccess.h5` file (path or h5db) to read input from
    :return:
        the *out-props* key-values, the *cycle* data-frame,
        and the grid-wots constructed to solve v_max.
    """
    from wltp import io as wio, engine, utils
    from wltp.experiment import Experiment

    props, wot, n2vs = load_vehicle_accdb(h5, vehnum)

    mdl = mdl_from_accdb(props, wot, n2vs)
    datamodel.validate_model(mdl, additional_properties=additional_properties)
    exp = Experiment(mdl, skip_model_validation=True)
    mdl = exp.run()

    ## Keep only *output* key-values, not to burden HDF data-model
    #  (excluding `driveability`, which is a list, and f0,f1,f2, addume were input).
    #
    # oprops = {k: v for k, v in veh if np.isscalar(v)}
    out_mdl = {
        "pmr": mdl["pmr"],
        "n95_low": mdl["n95_low"],
        "n95_high": mdl["n95_high"],
        "v_max": mdl["v_max"],
        "n_vmax": mdl["n_vmax"],
        "g_vmax": mdl["g_vmax"],
        "is_n_lim_vmax": mdl["is_n_lim_vmax"],
        "n_max1": mdl["n_max1"],
        "n_max2": mdl["n_max2"],
        "n_max3": mdl["n_max3"],
        "n_max": mdl["n_max"],
        "wltc_class": mdl["wltc_class"],
        "f_dsc_raw": mdl["f_dsc_raw"],
        "f_dsc": mdl["f_dsc"],
    }

    cycle = mdl["cycle"]

    return out_mdl, cycle, mdl["wots_vmax"]
Example #11
0
    def testPerf(self):
        logging.getLogger().setLevel(logging.WARNING)

        nexp = 100
        start = time.time()
        for _ in range(nexp):
            mdl = goodVehicle()

            experiment = Experiment(mdl)

            experiment.run()

        elapsed = time.time() - start
        print(">> ELAPSED: %.2fsec, RUN/EXP: %.4fsec" %
              (elapsed, elapsed / nexp))
Example #12
0
    def testOverlayOnInit(self):
        mdl = goodVehicle()
        nval = 6000
        mdl2 = {"n_rated": nval}

        exp = Experiment(mdl, mdl2)
        mdl = exp._model
        self.assertEqual(mdl["n_rated"], nval)
Example #13
0
def testGoodVehicle():
    mdl = goodVehicle()

    exp = Experiment(mdl)
    mdl = exp._model
    defwot = datamodel.upd_default_load_curve({})["wot"]
    assert pd.DataFrame(mdl["wot"][["n_norm",
                                    "p_norm"]]).equals(pd.DataFrame(defwot))
Example #14
0
def testOverlayOnInit():
    mdl = goodVehicle()
    n_rated = 6000
    mdl2 = {"n_rated": n_rated}

    exp = Experiment(datamodel.merge(mdl, mdl2))
    mdl = exp._model
    assert mdl["n_rated"] == n_rated
Example #15
0
    def test_two_ramps_with_map(self):
        V = np.array([0, 2, 5, 10, 20, 30, 40, 50, 30, 20, 10, 5, 1, 0])
        v_columns = ('v_class', 'v_target')
        slope = [0, 0, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0, 0, 0]
        for col in v_columns:
            results = []
            for s in (slope, None):
                mdl = {
                    "vehicle": {
                        "unladen_mass": 1430,
                        "test_mass": 1500,
                        "v_max": None,
                        "p_rated": 100,
                        "n_rated": 5450,
                        "n_idle": 950,
                        "gear_ratios": [120.5, 75, 50, 43, 37, 32],
                        "resistance_coeffs": [100, 0.5, 0.04],
                    },
                    'cycle_run': {
                        col: V,
                    }
                }

                if s:
                    mdl['cycle_run']['slope'] = s

                proc = Experiment(mdl)
                mdl = proc.run()

                #print(mdl)

                cycle_run = mdl['cycle_run']
                if s:
                    self.assertIn('slope', cycle_run)
                self.assertIn('gears', cycle_run)
                for vcol in v_columns:
                    npt.assert_array_equal(
                        V, cycle_run[vcol],
                        "V_Column(%s) not overriden!" % vcol)
                results.append(cycle_run['gears'])
            npt.assert_array_equal(V, cycle_run[vcol],
                                   "V_Column(%s) not overriden!" % vcol)
Example #16
0
def build_models(vehs_df, **locals_kws):
    """
    Builds all input-dataframes as Experiment classes and returns them in a list of (veh_id, exp) pairs.

    :param vehs_df:     A dataframe indexed by veh_id, and with columns *json-pointer* paths into the model
    :return: a list of (veh_id, :class:`wltp.experiment.Experiment`) tuples
    """
    experiment_pairs = []
    for veh_id, row in vehs_df.iterrows():
        try:
            mdl_in = {}
            for colname, colval in row.items():
                log.debug("veh_id(%s): Column(%s): %s", veh_id, colname,
                          colval)
                if not colval or (isinstance(colval, str)
                                  and not colval.strip()):
                    continue
                if isinstance(colval, str):

                    ## Is it an excel-ref like:
                    #        @<sheet_name>!A1[:R10:c1].table
                    #
                    try:
                        colval = resolve_excel_ref(colval)
                    except ValueError:
                        ## Try to parse value as python-code.
                        #
                        try:
                            old_v = colval
                            colval = eval(
                                colval, globals(), locals_kws
                            )  ## NOTE: Total insecure, but we're scientists, aren't we?
                        except Exception:
                            log.info(
                                "Failed parsing value(%s) as python code due to: %s\n  Assuming plain string.",
                                old_v,
                                traceback.format_exc(),
                            )
                        else:
                            log.info(
                                "Parsed value(%s) as python code into: %s",
                                old_v,
                                colval,
                            )
                pdl.set_jsonpointer(mdl_in, colname, colval)

            exp = Experiment(mdl_in)

            experiment_pairs.append((veh_id, exp))
        except Exception as ex:
            raise Exception("Invalid model for vehicle(%s): %s" %
                            (veh_id, ex)) from ex

    return experiment_pairs
Example #17
0
    def test_two_ramps_with_slope(self):
        V = np.hstack((np.r_[0:100:2], np.r_[100:0:-2]))
        SLOPE = np.random.rand(V.shape[0])
        v_columns = ('v_class', 'v_target')
        for col in v_columns:
            mdl = goodVehicle()

            mdl['cycle_run'] = pd.DataFrame({
                col: V,
                'slope': SLOPE,
            })
            proc = Experiment(mdl)
            mdl = proc.run()

            #print(pd.DataFrame(mdl['cycle_run']))

            cycle_run = mdl['cycle_run']
            self.assertIn('slope', cycle_run)
            self.assertIn('gears', cycle_run)
            for vcol in v_columns:
                npt.assert_array_equal(V, cycle_run[vcol], "V_Column(%s) not overriden!"%vcol)
Example #18
0
    def test_two_ramps_with_slope(self):
        V = np.hstack((np.r_[0:100:2], np.r_[100:0:-2]))
        SLOPE = np.random.rand(V.shape[0])
        v_columns = ('v_class', 'v_target')
        for col in v_columns:
            mdl = goodVehicle()

            mdl['cycle_run'] = pd.DataFrame({
                col: V,
                'slope': SLOPE,
            })
            proc = Experiment(mdl)
            mdl = proc.run()

            #print(pd.DataFrame(mdl['cycle_run']))

            cycle_run = mdl['cycle_run']
            self.assertIn('slope', cycle_run)
            self.assertIn('gears', cycle_run)
            for vcol in v_columns:
                npt.assert_array_equal(V, cycle_run[vcol],
                                       "V_Column(%s) not overriden!" % vcol)
Example #19
0
    def test_two_ramps_with_map(self):
        V = np.array([0,2,5,10,20,30,40,50,30,20,10,5,1,0])
        v_columns = ('v_class', 'v_target')
        slope = [0, 0, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1, 0.1,0,0,0]
        for col in v_columns:
            results = []
            for s in (slope, None):
                mdl = {
                    "vehicle": {
                        "unladen_mass": 1430,
                        "test_mass":    1500,
                        "v_max":    None,
                        "p_rated":  100,
                        "n_rated":  5450,
                        "n_idle":   950,
                        "gear_ratios":      [120.5, 75, 50, 43, 37, 32],
                        "resistance_coeffs":[100, 0.5, 0.04],
                    }, 'cycle_run': {
                        col: V,
                    }
                }

                if s:
                    mdl['cycle_run']['slope'] = s

                proc = Experiment(mdl)
                mdl = proc.run()

                #print(mdl)

                cycle_run = mdl['cycle_run']
                if s:
                    self.assertIn('slope', cycle_run)
                self.assertIn('gears', cycle_run)
                for vcol in v_columns:
                    npt.assert_array_equal(V, cycle_run[vcol], "V_Column(%s) not overriden!"%vcol)
                results.append(cycle_run['gears'])
            npt.assert_array_equal(V, cycle_run[vcol], "V_Column(%s) not overriden!"%vcol)
Example #20
0
def _run_the_experiments(
    transplant_original_gears=False,
    plot_results=False,
    compare_results=False,
    encoding="UTF-8",
):

    ## If file existent, it contains also calculated fields
    #    from the previous experiment run.
    #
    out_df = _read_vehicles_out()

    inp_df = _read_vehicles_inp()
    ## Reconstruct the columns only presetn in the out_df.
    #
    inp_df["pmr"] = np.NAN
    inp_df["wltc_class"] = ""
    inp_df["f_downscale"] = np.NAN

    wots = _read_wots()

    failed_vehicles = 0
    for (ix, row) in inp_df.iterrows():
        veh_num = ix
        heinz_fname = _make_heinz_fname(veh_num)
        outfname = _make_gened_fname(transplant_original_gears, veh_num)

        if not out_df is None and _is_file_up_to_date(outfname, [heinz_fname]):
            inp_df.loc[ix] = out_df.loc[ix]
            continue

        mdl = goodVehicle()
        veh = mdl["vehicle"]

        veh["test_mass"] = row["test_mass"]
        veh["unladen_mass"] = row["kerb_mass"]
        veh["f0"] = row["f0_real"]
        veh["f1"] = row["f1_real"]
        veh["f2"] = row["f2_real"]
        veh["p_rated"] = row["rated_power"]
        veh["n_rated"] = row["rated_speed"]
        veh["n_idle"] = int(row["idling_speed"])
        veh["v_max"] = row["v_max"]
        ngears = int(row["no_of_gears"])
        veh["gear_ratios"] = list(row["ndv_1" : "ndv_%s" % ngears])  #'ndv_1'
        veh["wot"] = _select_wot(wots, row["IDcat"] == 2)

        if transplant_original_gears:
            log.warning(">>> Transplanting gears from Heinz's!")
            df_h = _read_heinz_file(veh_num)

            mdl["cycle"] = {"gears_orig": df_h["g_max"].values}

        try:
            experiment = Experiment(mdl)
            mdl = experiment.run()
        except Exception as ex:
            log.warning("VEHICLE_FAILED(%s): %s", veh_num, str(ex))
            failed_vehicles += 1
            continue
        else:
            params = mdl["params"]
            veh = mdl["vehicle"]

            inp_df.loc[ix, "pmr"] = veh["pmr"]
            inp_df.loc[ix, "wltc_class"] = veh["wltc_class"]
            inp_df.loc[ix, "f_downscale"] = params["f_downscale"]

            # ankostis_mdb:  't', "v in km/h","v_orig","a in m/s²","gear","g_min","g_max","gear_modification","error_description"
            # heinz:         't', 'km_h', 'stg', 'gear'
            cycle_df = pd.DataFrame(mdl["cycle"])

            _compare_exp_results(cycle_df, outfname, compare_results)

            cycle_df.to_csv(outfname, index_label="time")
    fail_limit_prcnt = 0.1
    assert failed_vehicles < fail_limit_prcnt * inp_df.shape[0], (
        "TOO MANY(>%f) vehicles have Failed(%i out of %i)!"
        % (fail_limit_prcnt, failed_vehicles, inp_df.shape[0])
    )

    if not transplant_original_gears:
        _write_vehicle_data(inp_df)

    return inp_df
Example #21
0
3000	93.00
3250	94.15
3500	95.30
3750	95.00
4000	94.70
4250	92.15
4500	89.60
4750	86.95
5000	84.30
"""),
    sep="\t",
    header=0,
)

datamodel.validate_model(mdl, additional_properties=True)
exp = Experiment(mdl, skip_model_validation=True)

mdl = exp.run()

oprops = {
    "pmr": mdl["pmr"],
    "n95_low": mdl["n95_low"],
    "n95_high": mdl["n95_high"],
    "v_max": mdl["v_max"],
    "n_vmax": mdl["n_vmax"],
    "g_vmax": mdl["g_vmax"],
    "n_max1": mdl["n_max1"],
    "n_max2": mdl["n_max2"],
    "n_max3": mdl["n_max3"],
    "n_max": mdl["n_max"],
    "wltc_class": mdl["wltc_class"],