def test_parameter_numpy_operations(): """Operators work like a float""" parm1 = Parameter(value=1) parm1_neg = Parameter(value=-1) parm2 = Parameter(value=2) parm3 = Parameter(value=3) parm3_5 = Parameter(value=3.5) assert parm1 == 1 assert parm1 != 2 assert -parm1 == -1 assert abs(parm1_neg) == 1 assert int(parm1) == 1 assert np.allclose(float(parm1), 1) assert np.allclose(parm1 + parm2, 3) assert np.allclose(parm3 - parm2, 1) assert np.allclose(parm3 * parm2, 6) assert np.allclose(parm3 / parm2, 1.5) assert parm3 // parm2 == 1 assert np.trunc(parm3_5) == 3 assert parm2 % 2 == 0 assert 2 % parm2 == 0 assert divmod(parm3, 2) == (1, 1) assert divmod(3, parm2) == (1, 1) assert np.allclose(parm3**parm2, 9) assert parm3 > parm2 assert parm3 >= parm2 assert parm1 < parm2 assert parm1 <= parm2
def setter(that_self, value: set_type): if value is None and not self._allow_none: raise Exception( f"Property '{name}' of '{cls.__name__}' is not allowed to set to None.") if self._is_parameter and value is not None: if self._is_parameter_value and isinstance(value, str): value = Parameter(full_label=str(value)) elif self._is_parameter_list and all([isinstance(v, str) for v in value]): value = [Parameter(full_label=str(v)) for v in value] elif self._is_parameter_dict and all([isinstance(v, str) for v in value.values()]): for k, v in value.items(): value[k] = Parameter(full_label=v) setattr(that_self, f'_{self._name}', value)
def test_parameter_set_from_group(): """Parameter extracted from group has correct values""" group = load_parameters( "foo:\n - [\"1\", 123,{non-negative: true, min: 10, max: 8e2, vary: true, expr:'2'}]", format_name="yml_str", ) parameter = Parameter(full_label="foo.1") parameter.set_from_group(group=group) assert parameter.value == 123 assert parameter.non_negative is True assert np.allclose(parameter.minimum, 10) assert np.allclose(parameter.maximum, 800) assert parameter.vary is True # Set to None since value and expr were provided? assert parameter.expression is None
def setter(self, value: model_property.glotaran_property_type): # type: ignore[name-defined] if value is None and not model_property._allow_none: raise ValueError( f"Property '{model_property._name}' of '{cls.__name__}' " "is not allowed to set to None." ) if value is not None and model_property.glotaran_is_parameter_property: if model_property.glotaran_is_scalar_property and not isinstance(value, Parameter): value = Parameter(full_label=str(value)) elif model_property.glotaran_is_sequence_property and all( map(lambda v: not isinstance(v, Parameter), value) ): value = [Parameter(full_label=str(v)) for v in value] elif model_property.glotaran_is_mapping_property and all( map(lambda v: not isinstance(v, Parameter), value.values()) ): value = {k: Parameter(full_label=str(v)) for k, v in value.items()} setattr(self, f"_{model_property._name}", value)
def test_parameter_pickle(tmpdir): parameter = Parameter("testlabel", "testlabelfull", "testexpression", 1, 2, True, 42, False) with open(tmpdir.join("test_param_pickle"), "wb") as f: pickle.dump(parameter, f) with open(tmpdir.join("test_param_pickle"), "rb") as f: pickled_parameter = pickle.load(f) assert parameter == pickled_parameter
def test_parameter_dict_roundtrip(): param = Parameter( label="foo", full_label="bar.foo", expression="1", maximum=2, minimum=1, non_negative=True, value=42, vary=False, ) param_dict = param.as_dict() param_from_dict = Parameter.from_dict(param_dict) assert param.label == param_from_dict.label assert param.full_label == param_from_dict.full_label assert param.expression == param_from_dict.expression assert param.maximum == param_from_dict.maximum assert param.minimum == param_from_dict.minimum assert param.non_negative == param_from_dict.non_negative assert param.value == param_from_dict.value assert param.vary == param_from_dict.vary
def test_parameter_repr(): """Repr creates code to recreate the object.""" result = Parameter(label="foo", value=1.0, expression="$foo.bar", vary=False) result_short = Parameter(label="foo", value=1, expression="$foo.bar") expected = "Parameter(label='foo', value=1.0, expression='$foo.bar', vary=False)" assert result == result_short assert result.vary == result_short.vary assert result.__repr__() == expected assert result_short.__repr__() == expected
def test_transform_expression(case): expression, wanted_parameters = case parameter = Parameter(expression=expression) assert parameter.transformed_expression == wanted_parameters
def test_non_negative(): notnonneg = Parameter(value=1, non_negative=False) valuenotnoneg, _, _ = notnonneg.get_value_and_bounds_for_optimization() assert np.allclose(1, valuenotnoneg) notnonneg.set_value_from_optimization(valuenotnoneg) assert np.allclose(1, notnonneg.value) nonneg1 = Parameter(value=1, non_negative=True) value1, _, _ = nonneg1.get_value_and_bounds_for_optimization() assert not np.allclose(1, value1) nonneg1.set_value_from_optimization(value1) assert np.allclose(1, nonneg1.value) nonneg2 = Parameter(value=2, non_negative=True) value2, _, _ = nonneg2.get_value_and_bounds_for_optimization() assert not np.allclose(2, value2) nonneg2.set_value_from_optimization(value2) assert np.allclose(2, nonneg2.value) nonnegminmax = Parameter(value=5, minimum=3, maximum=6, non_negative=True) value5, minimum, maximum = nonnegminmax.get_value_and_bounds_for_optimization( ) assert not np.allclose(5, value5) assert not np.allclose(3, minimum) assert not np.allclose(6, maximum)
def test_parameter_label_error_wrong_label_pattern(label: str | int | float): """Error if label can't be casted to a valid label str""" with pytest.raises(ValueError, match=f"'{label}' is not a valid group label."): Parameter(label=label) # type:ignore[arg-type]
def test_transform_expression(case): expression, wanted_parameters = case parameter = Parameter(expression=expression) assert parameter.transformed_expression == wanted_parameters # just for the test coverage so the if condition is wrong assert parameter.transformed_expression == wanted_parameters
def test_parameter_minimum_not_numeric_error(): """Error if minimum isn't numeric.""" with pytest.raises(TypeError, match="Parameter minimum must be numeric"): Parameter(minimum="foo") # type:ignore[arg-type]
def test_parameter_value_not_numeric_error(): """Error if value isn't numeric.""" with pytest.raises(TypeError, match="Parameter value must be numeric"): Parameter(value="foo") # type:ignore[arg-type]
def test_parameter_label_always_str_or_None(label: str | int | None, expected: str | None): """Parameter.label is always a string or None""" parameter = Parameter(label=label) # type:ignore[arg-type] assert parameter.label == expected