Skip to content

Commit

Permalink
pr response
Browse files Browse the repository at this point in the history
  • Loading branch information
andped10 committed Apr 12, 2024
1 parent fbfcecf commit b0629c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion EasyReflectometry/parameter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
from easyCore.Objects.ObjectClasses import Parameter


def get_as_parameter(value: Union[Parameter, Number, None], name, default_dict: dict[str, str]) -> Parameter:
def get_as_parameter(value: Union[Parameter, Number, None], name: str, default_dict: dict[str, str]) -> Parameter:
"""
This function creates a parameter for the variable `name`. A parameter has a value and metadata.
If the value already is a parameter, it is returned.
If the value is a number, a parameter is created with this value and metadata from the dictionary.
If the value is None, a parameter is created with the default value and metadata from the dictionary.
param value: The value to use for the parameter. If None, the default value in the dictionary is used.
param name: The name of the parameter
param default_dict: Dictionary with entry for `name` containing the default value and metadata for the parameter
"""
# Should leave the passed dictionary unchanged
local_dict = deepcopy(default_dict)
if value is None:
# Create a default parameter using both value and metadata from dictionary
return Parameter(name, **local_dict[name])
elif isinstance(value, Number):
# Create a parameter using provided value and metadata from dictionary
del local_dict[name]['value']
return Parameter(name, value, **local_dict[name])
elif not isinstance(value, Parameter):
Expand Down
8 changes: 7 additions & 1 deletion tests/experiment/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ def test_from_pars(self):
o2 = RepeatingMultilayer.from_pars(ls2, 1.0, 'oneLayerItem2')
d = Sample.from_pars(o1, o2, name='myModel')
resolution_function = constant_resolution_function(2.0)
mod = Model(d, 2, 1e-5, resolution_function, 'newModel')
mod = Model(
sample=d,
scale=2,
background=1e-5,
resolution_function=resolution_function,
name='newModel',
)
assert_equal(mod.name, 'newModel')
assert_equal(mod.interface, None)
assert_equal(mod.sample.name, 'myModel')
Expand Down

0 comments on commit b0629c2

Please sign in to comment.