def metric_after_treatment_speed_model(y_true, y_pred, after_treatment_warm_up_phases): """ Metric for the `after_treatment_speed_model`. :param y_true: Reference engine speed vector [RPM]. :type y_true: numpy.array :param y_pred: Predicted engine speed vector [RPM]. :type y_pred: numpy.array :param after_treatment_warm_up_phases: Phases when engine speed is affected by the after treatment warm up [-]. :type after_treatment_warm_up_phases: numpy.array :return: Error. :rtype: float """ b = after_treatment_warm_up_phases if b.any(): from co2mpas.utils import mae return float(mae(y_true[b], y_pred[b])) else: return 0
def metric_engine_speed_model(y_true, y_pred, times, velocities, gear_shifts, on_engine, stop_velocity, hybrid_modes, after_treatment_warm_up_phases=False): """ Metric for the `engine_speed_model`. :param y_true: Reference engine speed vector [RPM]. :type y_true: numpy.array :param y_pred: Predicted engine speed vector [RPM]. :type y_pred: numpy.array :param times: Time vector [s]. :type times: numpy.array :param velocities: Velocity vector [km/h]. :type velocities: numpy.array :param gear_shifts: When there is a gear shifting [-]. :type gear_shifts: numpy.array :param on_engine: If the engine is on [-]. :type on_engine: numpy.array :param stop_velocity: Maximum velocity to consider the vehicle stopped [km/h]. :type stop_velocity: float :param hybrid_modes: Hybrid mode status (0: EV, 1: Parallel, 2: Serial). :type hybrid_modes: numpy.array :param after_treatment_warm_up_phases: Phases when engine speed is affected by the after treatment warm up [-]. :type after_treatment_warm_up_phases: numpy.array :return: Error. :rtype: float """ from co2mpas.utils import mae b = ~calculate_clutch_phases(times, 1, 1, gear_shifts, 0, (-4.0, 4.0)) b &= (velocities > stop_velocity) & ~after_treatment_warm_up_phases b &= on_engine & (hybrid_modes == 1) return b.any() and float(mae(y_true[b], y_pred[b])) or .0
def _error_fun(vel_limits): _update_gvs(vel_limits) g_pre = predict(times, velocities, accelerations, motive_powers, correct_gear=correct_gear) speed_pred = calculate_gear_box_speeds_in( g_pre, velocities, velocity_speed_ratios, stop_velocity) return np.float32(mae(speed_pred, engine_speeds_out))
def calculate_error_coefficients(identified_gears, gears, engine_speeds, predicted_engine_speeds, velocities, stop_velocity): """ Calculates the prediction's error coefficients. :param identified_gears: Identified gear vector [-]. :type identified_gears: numpy.array :param gears: Gear vector [-]. :type gears: numpy.array :param engine_speeds: Engine speed vector [RPM]. :type engine_speeds: numpy.array :param predicted_engine_speeds: Predicted Engine speed vector [RPM]. :type predicted_engine_speeds: numpy.array :param velocities: Vehicle velocity [km/h]. :type velocities: numpy.array :param stop_velocity: Maximum velocity to consider the vehicle stopped [km/h]. :type stop_velocity: float :return: Correlation coefficient and mean absolute error. :rtype: dict """ b = velocities > stop_velocity x = engine_speeds[b] y = predicted_engine_speeds[b] res = { 'mean_absolute_error': co2_utl.mae(x, y), 'correlation_coefficient': _correlation_coefficient(x, y), 'accuracy_score': _accuracy_score(identified_gears, gears) } return res
def metric_clutch_torque_converter_model(y_true, y_pred, on_engine): """ Metric for the `clutch_torque_converter_model`. :param y_true: Reference engine speed vector [RPM]. :type y_true: numpy.array :param y_pred: Predicted engine speed vector [RPM]. :type y_pred: numpy.array :param on_engine: If the engine is on [-]. :type on_engine: numpy.array :return: Error. :rtype: float """ from co2mpas.utils import mae return float(mae(y_true[on_engine], y_pred[on_engine]))
def calibrate_clutch_speed_model(clutch_phases, accelerations, clutch_tc_speeds_delta, velocities, gear_box_speeds_in, gears): """ Calibrate clutch speed model. :param clutch_phases: When the clutch is active [-]. :type clutch_phases: numpy.array :param accelerations: Acceleration vector [m/s2]. :type accelerations: numpy.array :param clutch_tc_speeds_delta: Engine speed delta due to the clutch [RPM]. :type clutch_tc_speeds_delta: numpy.array :param velocities: Velocity vector [km/h]. :type velocities: numpy.array :param gear_box_speeds_in: Gear box speed vector [RPM]. :type gear_box_speeds_in: numpy.array :param gears: Gear vector [-]. :type gears: numpy.array :return: Clutch speed model. :rtype: callable """ model = _no_model if clutch_phases.sum() > 10: from co2mpas.utils import mae from sklearn.pipeline import Pipeline # noinspection PyProtectedMember from ..engine._thermal import _SelectFromModel, _XGBRegressor X = np.column_stack((accelerations, velocities, gear_box_speeds_in, gears))[clutch_phases] y = clutch_tc_speeds_delta[clutch_phases] # noinspection PyArgumentEqualDefault mdl = _XGBRegressor(max_depth=2, n_estimators=int(min(300., 0.25 * (len(y) - 1))), random_state=0, objective='reg:squarederror') mdl = Pipeline([('feature_selection', _SelectFromModel(mdl, '0.8*median')), ('classification', mdl)]) mdl.fit(X, y) if mae(mdl.predict(X), y) < mae(0, y): keys = 'accelerations', 'velocities', 'gear_box_speeds_in', 'gears' # noinspection PyUnusedLocal,PyMissingOrEmptyDocstring def model(times, **kwargs): return mdl.predict(np.column_stack(sh.selector(keys, kwargs))) return model
def _err(factor): e = mae(ds, np.nan_to_num(predict((gbs, gbt / factor)) - es)) return np.float32(e)
def _err(v, r): v = int(v) or 1 return np.float32(co2_utl.mae(ratios[slice(v - 1, v + 1, 1)], r))
def error(params): return co2_utl.mae(state_of_charges, func(params))