def write_outputs(): """ Defines a module to write on files the outputs of the CO2MPAS model. .. dispatcher:: d >>> d = write_outputs() :return: The write outputs module. :rtype: SubDispatchFunction """ d = dsp.Dispatcher( name='write_outputs', description='Writes on files the outputs of the CO2MPAS model.') d.add_function(function=convert2df, inputs=['output_data', 'start_time', 'main_flags'], outputs=['dfs']) d.add_function(function=excel.write_to_excel, inputs=['dfs', 'output_file_name', 'template_file_name']) inp = [ 'output_file_name', 'template_file_name', 'output_data', 'start_time', 'main_flags' ] return dsp_utl.SubDispatchFunction(d, d.name, inp)
def write_outputs(): """ Defines a module to write on files the outputs of the CO2MPAS model. .. dispatcher:: dsp >>> dsp = write_outputs() :return: The write outputs module. :rtype: SubDispatchFunction """ dsp = Dispatcher( name='write_outputs', description='Writes on files the outputs of the CO2MPAS model.' ) dsp.add_function( function=partial(convert2df, data_descriptions=get_doc_description(), write_schema=define_data_schema(read=False)), inputs=['output_data', 'start_time', 'main_flags'], outputs=['dfs'] ) dsp.add_function( function=write_to_excel, inputs=['dfs', 'output_file_name', 'template_file_name'] ) inp = ['output_file_name', 'template_file_name', 'output_data', 'start_time', 'main_flags'] return dsp_utl.SubDispatchFunction(dsp, dsp.name, inp)
def report(): """ Defines and returns a function that produces a vehicle report from CO2MPAS outputs. .. dispatcher:: dsp >>> dsp = report() :return: The reporting model. :rtype: SubDispatchFunction """ # Initialize a dispatcher. dsp = Dispatcher( name='make_report', description='Produces a vehicle report from CO2MPAS outputs.' ) dsp.add_function( function=get_report_output_data, inputs=['output_data'], outputs=['report'] ) dsp.add_function( function=extract_summary, inputs=['report', 'vehicle_name'], outputs=['summary'] ) inputs = ['output_data', 'vehicle_name'] outputs = ['report', 'summary'] return dsp_utl.SubDispatchFunction(dsp, dsp.name, inputs, outputs)
def _errors(name, data_id, data_out, setting): name = ''.join(k[0].upper() for k in name.split('_')) d = dsp.Dispatcher( name='%s-%s errors' % (name, data_id), description='Calculates the error of calibrated model.', ) setting = setting.copy() d.add_data( data_id='models', default_value=setting.pop('models', []) ) select_data = functools.partial(dsp_utl.selector, allow_miss=True) d.add_function( function_id='select_models', function=setting.pop('select_models', select_data), inputs=['models', data_id], outputs=['calibrated_models'] ) d.add_data( data_id='data_in', default_value=data_id ) d.add_data( data_id='error_settings', default_value={} ) for o in data_out: d.add_function( function=functools.partial( dsp_utl.map_list, ['calibrated_models', 'data'] ), inputs=['calibrated_models', o], outputs=['input/%s' % o] ) d.add_function( function=_error(name, data_id, o, setting), inputs=['input/%s' % o, 'error_settings'], outputs=['error/%s' % o] ) i = ['error_settings', data_id] + [k for k in data_out if k != data_id] func = dsp_utl.SubDispatchFunction( dsp=d, function_id=d.name, inputs=i ) return func
def selector(*data): """ Defines the models' selector model. .. dispatcher:: dsp >>> dsp = selector() :return: The models' selector model. :rtype: SubDispatchFunction """ data = data or ('wltp_h', 'wltp_l') dsp = Dispatcher( name='Models selector', description='Select the calibrated models.', ) dsp.add_function(function=partial(dsp_utl.map_list, data), inputs=data, outputs=['CO2MPAS_results']) dsp.add_data(data_id='models', function=combine_outputs, wait_inputs=True) dsp.add_data(data_id='scores', function=combine_scores, wait_inputs=True) setting = sub_models() dsp.add_data(data_id='error_settings', default_value={}) m = list(setting) dsp.add_function(function=partial(split_error_settings, m), inputs=['error_settings'], outputs=['error_settings/%s' % k for k in m]) for k, v in setting.items(): v['dsp'] = v.pop('define_sub_model', define_sub_model)(**v) v['metrics'] = dsp_utl.map_list(v['targets'], *v['metrics']) dsp.add_function(function=v.pop('model_selector', _selector)(k, data, data, v), function_id='%s selector' % k, inputs=['CO2MPAS_results', 'error_settings/%s' % k], outputs=['models', 'scores']) func = dsp_utl.SubDispatchFunction(dsp=dsp, function_id='models_selector', inputs=('error_settings', ) + data, outputs=['models', 'scores']) return func
def calculate_wltp_velocities(): """ Defines the wltp cycle model. .. dispatcher:: dsp >>> dsp = calculate_wltp_velocities() :return: The wltp cycle model. :rtype: Dispatcher """ dsp = Dispatcher( name='WLTP velocities model', description='Returns the theoretical velocities of WLTP.' ) dsp.add_function( function=get_dfl, inputs=['base_model'], outputs=['driver_mass', 'resistance_coeffs_regression_curves', 'wltc_data'] ) dsp.add_function( function=calculate_unladen_mass, inputs=['vehicle_mass', 'driver_mass'], outputs=['unladen_mass'] ) dsp.add_function( function=calc_default_resistance_coeffs, inputs=['vehicle_mass', 'resistance_coeffs_regression_curves'], outputs=['road_loads'], weight=15 ) dsp.add_function( function=calculate_max_speed_velocity_ratio, inputs=['speed_velocity_ratios'], outputs=['max_speed_velocity_ratio'] ) dsp.add_function( function=calculate_max_velocity, inputs=['engine_max_speed_at_max_power', 'max_speed_velocity_ratio'], outputs=['max_velocity'] ) dsp.add_function( function=calculate_wltp_class, inputs=['wltc_data', 'engine_max_power', 'unladen_mass', 'max_velocity'], outputs=['wltp_class'] ) dsp.add_function( function=get_class_data, inputs=['wltc_data', 'wltp_class'], outputs=['class_data'] ) dsp.add_function( function=get_class_velocities, inputs=['class_data', 'times'], outputs=['class_velocities'], weight=25 ) from ..vehicle import vehicle func = dsp_utl.SubDispatchFunction( dsp=vehicle(), function_id='calculate_class_powers', inputs=['vehicle_mass', 'velocities', 'climbing_force', 'road_loads', 'inertial_factor', 'times'], outputs=['motive_powers'] ) dsp.add_function( function=func, inputs=['vehicle_mass', 'class_velocities', 'climbing_force', 'road_loads', 'inertial_factor', 'times'], outputs=['class_powers'] ) dsp.add_data( data_id='downscale_factor_threshold', default_value=dfl.values.downscale_factor_threshold ) dsp.add_function( function=calculate_downscale_factor, inputs=['class_data', 'downscale_factor_threshold', 'max_velocity', 'engine_max_power', 'class_powers', 'times'], outputs=['downscale_factor'] ) dsp.add_function( function=get_downscale_phases, inputs=['class_data'], outputs=['downscale_phases'] ) dsp.add_function( function=wltp_velocities, inputs=['downscale_factor', 'class_velocities', 'downscale_phases', 'times'], outputs=['velocities'] ) return dsp