def _determine_output_signal_unit( func: MathematicalExpression, input_unit: Union[str, Union] ) -> pint.Unit: """Determine the unit of a transformations' output signal. Parameters ---------- func : The function describing the transformation input_unit : The unit of the input signal Returns ------- pint.Unit: Unit of the transformations' output signal """ input_unit = U_(input_unit) if func is not None: variables = func.get_variable_names() if len(variables) != 1: raise ValueError("The provided function must have exactly 1 parameter") try: test_output = func.evaluate(**{variables[0]: Q_(1, input_unit)}) except Exception as e: raise ValueError( "The provided function is incompatible with the input signals unit." f" \nThe test raised the following exception:\n{e}" ) return test_output.data.units return input_unit
def test_generic_measurement(): """Test basic measurement creation and ASDF read/write.""" data_01 = msm.Data( name="Welding current", data=xr.DataArray([1, 2, 3, 4], dims=["time"]) ) data_02 = msm.Data( name="Welding voltage", data=xr.DataArray([10, 20, 30, 40], dims=["time"]) ) src_01 = msm.Source( name="Current Sensor", output_signal=msm.Signal("analog", "V", data=None), error=msm.Error(1337.42), ) src_02 = msm.Source( name="Voltage Sensor", output_signal=msm.Signal("analog", "V", data=None), error=msm.Error(1), ) dp_01 = msm.DataTransformation( name="AD conversion current measurement", input_signal=src_01.output_signal, output_signal=msm.Signal("digital", "V", data=None), error=msm.Error(999.0), ) dp_02 = msm.DataTransformation( name="Calibration current measurement", input_signal=dp_01.output_signal, output_signal=msm.Signal("digital", "A", data=data_01), error=msm.Error(43.0), ) dp_03 = msm.DataTransformation( name="AD conversion voltage measurement", input_signal=dp_02.output_signal, output_signal=msm.Signal("digital", "V", data=None), error=msm.Error(2.0), ) dp_04 = msm.DataTransformation( name="Calibration voltage measurement", input_signal=dp_03.output_signal, output_signal=msm.Signal("digital", "V", data=data_02), error=msm.Error(Q_(3.0, "percent")), ) chn_01 = msm.MeasurementChain( name="Current measurement", data_source=src_01, data_processors=[dp_01, dp_02] ) chn_02 = msm.MeasurementChain( name="Voltage measurement", data_source=src_02, data_processors=[dp_03, dp_04] ) eqp_01 = msm.GenericEquipment( "Current Sensor", sources=[src_01], data_transformations=[dp_02] ) eqp_02 = msm.GenericEquipment( "AD Converter", sources=None, data_transformations=[dp_01, dp_03] ) eqp_03 = msm.GenericEquipment( "Voltage Sensor", sources=None, data_transformations=[dp_04] ) measurement_01 = msm.Measurement( name="Current measurement", data=[data_01], measurement_chain=chn_01 ) measurement_02 = msm.Measurement( name="Voltage measurement", data=[data_02], measurement_chain=chn_02 ) equipment = [eqp_01, eqp_02, eqp_03] measurement_data = [data_01, data_02] measurement_chains = [chn_01] measurements = [measurement_01, measurement_02] sources = [src_01] processors = [dp_01, dp_02] [a, x, b] = sympy.symbols("a x b") expr_01 = MathematicalExpression(a * x + b) expr_01.set_parameter("a", 2) expr_01.set_parameter("b", 3) print(expr_01.parameters) print(expr_01.get_variable_names()) print(expr_01.evaluate(x=3)) tree = { "equipment": equipment, "data": measurement_data, "measurements": measurements, # "expression": expr_01, "measurement_chains": measurement_chains, "data_sources": sources, "data_processors": processors, } _write_read_buffer(tree)
def test_evaluation(expression, parameters, variables, exp_result): """Test the evaluation of the mathematical function.""" expr = MathematicalExpression(expression=expression, parameters=parameters) assert np.all(expr.evaluate(**variables) == exp_result)