def setUp(self): ss_dsp = sh.Dispatcher() fun = lambda a: (a + 1, 5, a - 1) dom = lambda kw: True c = '|!"£$%&/()=?^*+éè[]#¶ù§çò@:;-_.,<>' ss_dsp.add_function(function=fun, inputs=['a'], outputs=['b', sh.SINK, c], input_domain=dom, weight=1) def raise_fun(a): raise ValueError('Error') ss_dsp.add_function(function=raise_fun, inputs=['a'], outputs=['b']) sdspfunc = sh.SubDispatchFunction(ss_dsp, 'SubDispatchFunction', ['a'], ['b', c]) sdsppipe = sh.SubDispatchPipe(ss_dsp, 'SubDispatchPipe', ['a'], ['b', c]) spipe = sh.DispatchPipe(ss_dsp, 'DispatchPipe', ['a'], [c]) sdsp = sh.SubDispatch(ss_dsp, ['b', c], output_type='list') s_dsp = sh.Dispatcher() s_dsp.add_function(None, sdspfunc, ['a'], ['b', 'c'], weight=2) s_dsp.add_function(None, sdsppipe, ['a'], ['b', 'c'], out_weight={'c': 5}) s_dsp.add_function(None, spipe, ['a'], ['b'], weight=2) s_dsp.add_function('SubDispatch', sdsp, ['d'], ['e', 'f']) dsp = sh.Dispatcher() dsp.add_data('A', default_value=[0] * 1000) dsp.add_data('D', default_value={'a': 3}) dsp.add_dispatcher(dsp=s_dsp, inputs={ 'A': 'a', 'D': 'd' }, outputs={ 'b': 'B', 'c': 'C', 'e': 'E', 'f': 'F' }, inp_weight={'A': 3}) self.sol = dsp.dispatch() self.dsp = dsp dsp = sh.Dispatcher() dsp.add_function(function=sh.bypass, inputs=['a'], outputs=[sh.PLOT]) self.dsp_plot = dsp
def compile(self, inputs, outputs): dsp = self.dsp.shrink_dsp(outputs=outputs) dsp.default_values = sh.selector( set(dsp.default_values) - set(inputs), dsp.default_values) res = dsp() dsp = dsp.get_sub_dsp_from_workflow(outputs, graph=dsp.dmap, reverse=True, blockers=res, wildcard=False) for k, v in sh.selector(dsp.data_nodes, res, allow_miss=True).items(): dsp.set_default_value(k, v.value) func = sh.SubDispatchPipe(dsp=dsp, function_id=self.dsp.name, inputs=inputs, outputs=outputs) return func
def electrics_prediction(): """ Defines the electric sub model to predict the alternator loads. .. dispatcher:: d >>> d = electrics_prediction() :return: The electric sub model. :rtype: SubDispatchPipe """ d = sh.Dispatcher( name='Electric sub model', description='Electric sub model to predict the alternator loads') d.add_function(function=calculate_battery_current, inputs=[ 'electric_load', 'alternator_current', 'alternator_nominal_voltage', 'on_engine', 'max_battery_charging_current' ], outputs=['battery_current']) d.add_function(function=calculate_alternator_current, inputs=[ 'alternator_status', 'on_engine', 'gear_box_power_in', 'max_alternator_current', 'alternator_current_model', 'engine_start_current', 'prev_battery_current', 'acceleration' ], outputs=['alternator_current']) d.add_function(function=calculate_battery_state_of_charge, inputs=[ 'battery_state_of_charge', 'battery_capacity', 'delta_time', 'battery_current', 'prev_battery_current' ], outputs=['battery_state_of_charge']) d.add_function(function=predict_alternator_status, inputs=[ 'alternator_status_model', 'prev_alternator_status', 'battery_state_of_charge', 'gear_box_power_in' ], outputs=['alternator_status']) d.add_function(function=calculate_engine_start_current, inputs=[ 'engine_start', 'start_demand', 'alternator_nominal_voltage', 'delta_time' ], outputs=['engine_start_current']) func = sh.SubDispatchPipe( dsp=d, function_id='electric_sub_model', inputs=[ 'battery_capacity', 'alternator_status_model', 'max_alternator_current', 'alternator_current_model', 'max_battery_charging_current', 'alternator_nominal_voltage', 'start_demand', 'electric_load', 'delta_time', 'gear_box_power_in', 'acceleration', 'on_engine', 'engine_start', 'prev_alternator_status', 'prev_battery_current', 'battery_state_of_charge' ], outputs=[ 'alternator_current', 'alternator_status', 'battery_current', 'battery_state_of_charge' ]) return func
def setUp(self): ss_dsp = sh.Dispatcher(name='ss_dsp') fun = lambda a: (a + 1, 5, a - 1) dom = lambda kw: True ss_dsp.add_function(function=fun, inputs=['a'], outputs=['b', 'd', 'c'], input_domain=dom, weight=1) sdspfunc = sh.SubDispatchFunction(ss_dsp, 'SubDispatchFunction', ['a'], ['b', 'c']) sdsppipe = sh.SubDispatchPipe(ss_dsp, 'SubDispatchPipe', ['a'], ['b', 'c']) sdsp = sh.SubDispatch(ss_dsp, ['b', 'c'], output_type='list') s_dsp = sh.Dispatcher(name='s_dsp') s_dsp.add_function(None, sdspfunc, ['a'], ['b', 'c']) s_dsp.add_function(None, sdsppipe, ['a'], ['g']) s_dsp.add_function('SubDispatch', sdsp, ['d'], ['e', 'f']) dsp = sh.Dispatcher(name='model') dsp.add_data('A', default_value=0) dsp.add_data('D', default_value={'a': 3}) dsp.add_dispatcher(dsp=s_dsp, inputs={ 'A': 'a', 'D': 'd' }, outputs={ 'b': 'B', 'c': 'C', 'e': 'E', 'f': 'F', 'g': 'G' }, inp_weight={'A': 3}) self.dsp = dsp self.sol = sol = dsp.dispatch() sites = set() webmap = dsp.web(node_data=('+set_value', ), run=True, sites=sites) self.site = sites.pop() self.url = '%s/' % self.site.url rules = webmap.rules() self.io = io = [] for rule in rules.values(): n = rule.split('/')[1:] if not n: continue s, k = sol.get_node(*n, node_attr='sol') k = k[-1] try: v = s.workflow.node[k] except KeyError: continue if 'results' not in v: continue inputs = s._wf_pred[k] # List of the function's arguments. inputs = sh.bypass( *[inputs[k]['value'] for k in s.nodes[k]['inputs']]) io.append((rule, inputs, v['results'])) self.sol1 = sol = dsp.dispatch({'A': 1}) self.io1 = io = [] for rule in rules.values(): n = rule.split('/')[1:] if not n: continue s, k = sol.get_node(*n, node_attr='sol') k = k[-1] try: v = s.workflow.node[k] except KeyError: continue if 'results' not in v: continue inputs = s._wf_pred[k] # List of the function's arguments. inputs = sh.bypass( *[inputs[k]['value'] for k in s.nodes[k]['inputs']]) io.append((rule, inputs, v['results']))
def test_function(self): fun = sh.SubDispatchPipe(self.dsp_1, 'F', ['a', 'b'], ['a']) self.assertEqual(fun.__name__, 'F') # noinspection PyCallingNonCallable self.assertEqual(fun(2, 1), 1) self.assertRaises(sh.DispatcherError, fun, 3, -1) self.assertRaises(sh.DispatcherError, fun, 3, None) fun = sh.SubDispatchPipe(self.dsp_2, 'F', ['b', 'a'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(1, 2), [3, 2]) self.assertRaises(ValueError, sh.SubDispatchPipe, self.dsp_2, 'F', ['a', 'c'], ['d']) fun = sh.SubDispatchPipe(self.dsp_3, 'F', ['b', 'a'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(5, 20), [25, 20]) fun = sh.SubDispatchPipe(self.dsp_4, 'F', ['b', 'a'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(5, 20), [25, 20]) fun = sh.SubDispatchPipe(self.dsp_5, 'F', ['b', 'a', 'e', 'h'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(1, 2, 0, 0), [3, 2]) self.assertEqual(fun(b=1, a=2, e=0, h=0), [3, 2]) self.assertEqual(fun(1, 2, 0), [4, 3]) self.assertEqual(fun(1, 2, e=0), [4, 3]) self.assertRaises(ValueError, sh.SubDispatchPipe, self.dsp_5, 'F', ['a', 'c'], ['d']) self.assertRaises(TypeError, fun, 2, 1, 2, 5, 6) self.assertRaises(TypeError, fun, 2, 1, a=2, b=2) self.assertRaises(TypeError, fun, 2, 1, g=0) self.assertRaises(TypeError, fun, 1, 2, 0, c=3, aa=0) self.assertRaises(TypeError, fun, 1, 2, 0, i=3) self.assertRaises(TypeError, fun) fun = sh.SubDispatchPipe(self.dsp_6, outputs=['d']) self.assertEqual(fun(), 0) self.assertRaises(TypeError, fun, a=4) self.assertRaises(TypeError, fun, d=5) self.assertRaises(TypeError, fun, a=3, d=5) self.assertRaises(TypeError, fun, 2) self.assertRaises(TypeError, fun, c=2) fun = sh.SubDispatchPipe(self.dsp_6, outputs=['c']) self.assertEqual(fun(), 0) self.assertRaises(TypeError, fun, 2) self.assertRaises(TypeError, fun, a=2) self.assertRaises(TypeError, fun, c=5) self.assertRaises(TypeError, fun, a=2, c=7) self.assertRaises(TypeError, fun, d=2) fun = sh.SubDispatchPipe(self.dsp_6, inputs=['y', 'x'], outputs=['z']) self.assertEqual(fun(x=3), 3) self.assertRaises(TypeError, fun, 2) self.assertRaises(TypeError, fun, y=4) self.assertRaises(TypeError, fun, a=2)
:param class_data: WLTP class data. :type class_data: dict :return: Class time and velocity vectors [s, km/h]. :rtype: tuple[numpy.array] """ vel = np.asarray(class_data['cycle'], dtype=float) return np.arange(vel.shape[0], dtype=float), vel i = ['vehicle_mass', 'road_loads', 'inertial_factor'] calculate_class_powers = sh.SubDispatchPipe( _vehicle, function_id='calculate_class_powers', inputs=['times', 'velocities'] + i, outputs=['motive_powers']) dsp.add_function(function=calculate_class_powers, inputs=['class_times', 'class_velocities'] + i, outputs=['class_powers']) dsp.add_data('downscale_factor_threshold', dfl.values.downscale_factor_threshold) @sh.add_function(dsp, outputs=['downscale_factor']) def calculate_downscale_factor(class_data, downscale_factor_threshold, max_velocity, engine_max_power, class_powers): """ Calculates velocity downscale factor [-].