Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class DampedHarmonicOscillator(CreateParametersMixin, ModelComponent):

def __init__(
self,
area: Numeric | Parameter = 1.0,
center: Numeric | Parameter = 1.0,
width: Numeric | Parameter = 1.0,
area: Numeric = 1.0,
center: Numeric = 1.0,
width: Numeric = 1.0,
unit: str | sc.Unit = 'meV',
name: str = 'DampedHarmonicOscillator',
display_name: str | None = None,
Expand All @@ -39,11 +39,11 @@ def __init__(

Parameters
----------
area : Numeric | Parameter, default=1.0
area : Numeric, default=1.0
Area under the curve.
center : Numeric | Parameter, default=1.0
center : Numeric, default=1.0
Resonance frequency, approximately the peak position.
width : Numeric | Parameter, default=1.0
width : Numeric, default=1.0
Damping constant, approximately the half width at half max (HWHM) of the peaks. By
default, 1.0.
unit : str | sc.Unit, default='meV'
Expand Down
8 changes: 4 additions & 4 deletions src/easydynamics/sample_model/components/delta_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class DeltaFunction(CreateParametersMixin, ModelComponent):

def __init__(
self,
center: Numeric | Parameter | None = None,
area: Numeric | Parameter = 1.0,
center: Numeric | None = None,
area: Numeric = 1.0,
unit: str | sc.Unit = 'meV',
name: str = 'DeltaFunction',
display_name: str | None = None,
Expand All @@ -42,9 +42,9 @@ def __init__(

Parameters
----------
center : Numeric | Parameter | None, default=None
center : Numeric | None, default=None
Center of the delta function. If None, it will be centered at 0 and fixed.
area : Numeric | Parameter, default=1.0
area : Numeric, default=1.0
Total area under the curve.
unit : str | sc.Unit, default='meV'
Unit of the parameters.
Expand Down
12 changes: 6 additions & 6 deletions src/easydynamics/sample_model/components/exponential.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Exponential(CreateParametersMixin, ModelComponent):

def __init__(
self,
amplitude: Numeric | Parameter = 1.0,
center: Numeric | Parameter | None = None,
rate: Numeric | Parameter = 1.0,
amplitude: Numeric = 1.0,
center: Numeric | None = None,
rate: Numeric = 1.0,
unit: str | sc.Unit = 'meV',
name: str = 'Exponential',
display_name: str | None = None,
Expand All @@ -38,11 +38,11 @@ def __init__(

Parameters
----------
amplitude : Numeric | Parameter, default=1.0
amplitude : Numeric, default=1.0
Amplitude of the Exponential.
center : Numeric | Parameter | None, default=None
center : Numeric | None, default=None
Center of the Exponential. If None, the center is fixed at 0.
rate : Numeric | Parameter, default=1.0
rate : Numeric, default=1.0
Decay or growth constant of the Exponential.
unit : str | sc.Unit, default='meV'
Unit of the parameters.
Expand Down
12 changes: 6 additions & 6 deletions src/easydynamics/sample_model/components/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class Gaussian(CreateParametersMixin, ModelComponent):

def __init__(
self,
area: Numeric | Parameter = 1.0,
center: Numeric | Parameter | None = None,
width: Numeric | Parameter = 1.0,
area: Numeric = 1.0,
center: Numeric | None = None,
width: Numeric = 1.0,
unit: str | sc.Unit = 'meV',
name: str = 'Gaussian',
display_name: str | None = None,
Expand All @@ -46,11 +46,11 @@ def __init__(

Parameters
----------
area : Numeric | Parameter, default=1.0
area : Numeric, default=1.0
Area of the Gaussian.
center : Numeric | Parameter | None, default=None
center : Numeric | None, default=None
Center of the Gaussian. If None.
width : Numeric | Parameter, default=1.0
width : Numeric, default=1.0
Standard deviation.
unit : str | sc.Unit, default='meV'
Unit of the parameters.
Expand Down
12 changes: 6 additions & 6 deletions src/easydynamics/sample_model/components/lorentzian.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class Lorentzian(CreateParametersMixin, ModelComponent):

def __init__(
self,
area: Numeric | Parameter = 1.0,
center: Numeric | Parameter | None = None,
width: Numeric | Parameter = 1.0,
area: Numeric = 1.0,
center: Numeric | None = None,
width: Numeric = 1.0,
unit: str | sc.Unit = 'meV',
name: str = 'Lorentzian',
display_name: str | None = None,
Expand All @@ -43,11 +43,11 @@ def __init__(

Parameters
----------
area : Numeric | Parameter, default=1.0
area : Numeric, default=1.0
Area of the Lorentzian.
center : Numeric | Parameter | None, default=None
center : Numeric | None, default=None
Center of the Lorentzian. If None, defaults to 0 and is fixed.
width : Numeric | Parameter, default=1.0
width : Numeric, default=1.0
Half width at half maximum (HWHM).
unit : str | sc.Unit, default='meV'
Unit of the parameters.
Expand Down
114 changes: 50 additions & 64 deletions src/easydynamics/sample_model/components/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CreateParametersMixin:

def _create_area_parameter(
self,
area: Numeric | Parameter,
area: Numeric,
name: str,
unit: str | sc.Unit = 'meV',
minimum_area: float = MINIMUM_AREA,
Expand All @@ -37,51 +37,48 @@ def _create_area_parameter(

Parameters
----------
area : Numeric | Parameter
The area value or Parameter.
area : Numeric
The area value.
name : str
The name of the model component.
unit : str | sc.Unit, default='meV'
unit : str | sc.Unit, default="meV"
The unit of the area Parameter.
minimum_area : float, default=MINIMUM_AREA
The minimum allowed area.

Raises
------
TypeError
If area is not a number or a Parameter.
If area is not a number.
ValueError
If area is not a finite number or if the area Parameter has a non-finite value.
If area is not a finite number.

Returns
-------
Parameter
The validated area Parameter.
"""
if not isinstance(area, (Parameter, Numeric)):
raise TypeError('area must be a number or a Parameter.')
if not isinstance(area, Numeric):
raise TypeError('area must be a number.')

if isinstance(area, Numeric):
if not np.isfinite(area):
raise ValueError('area must be a finite number or a Parameter')
if not np.isfinite(area):
raise ValueError('area must be a finite number.')
area_param = Parameter(name=name + ' area', value=float(area), unit=unit)

area = Parameter(name=name + ' area', value=float(area), unit=unit)

if area.value < 0:
if area_param.value < 0:
warnings.warn(
f'The area of {name} is negative, which may not be physically meaningful.',
UserWarning,
stacklevel=3,
)
else:
if area.min < minimum_area:
area.min = minimum_area
area_param.min = minimum_area

return area
return area_param

def _create_center_parameter(
self,
center: Numeric | Parameter | None,
center: Numeric | None,
name: str,
fix_if_none: bool,
unit: str | sc.Unit = 'meV',
Expand All @@ -92,52 +89,51 @@ def _create_center_parameter(

Parameters
----------
center : Numeric | Parameter | None
The center value or Parameter.
center : Numeric | None
The center value.
name : str
The name of the model component.
fix_if_none : bool
Whether to fix the center Parameter if center is None.
unit : str | sc.Unit, default='meV'
unit : str | sc.Unit, default="meV"
The unit of the center Parameter.
enforce_minimum_center : bool, default=False
Whether to enforce a minimum center value to avoid zero center in DHO.

Raises
------
TypeError
If center is not None, a number, or a Parameter.
If center is not None or a number.
ValueError
If center is a number but not finite, or if center is a Parameter but has a non-finite
value.
If center is a number but not finite.

Returns
-------
Parameter
The validated center Parameter.
"""
if center is not None and not isinstance(center, (Numeric, Parameter)):
raise TypeError('center must be None, a number, or a Parameter.')
if center is not None and not isinstance(center, Numeric):
raise TypeError('center must be None or a number.')

if center is None:
center = Parameter(
center_param = Parameter(
name=name + ' center',
value=0.0,
unit=unit,
fixed=fix_if_none,
)
elif isinstance(center, Numeric):
else:
if not np.isfinite(center):
raise ValueError('center must be None, a finite number or a Parameter')
raise ValueError('center must be None or a finite number.')

center = Parameter(name=name + ' center', value=float(center), unit=unit)
if enforce_minimum_center and center.min < DHO_MINIMUM_CENTER:
center.min = DHO_MINIMUM_CENTER
return center
center_param = Parameter(name=name + ' center', value=float(center), unit=unit)
if enforce_minimum_center and center_param.min < DHO_MINIMUM_CENTER:
center_param.min = DHO_MINIMUM_CENTER
return center_param

def _create_width_parameter(
self,
width: Numeric | Parameter,
width: Numeric,
name: str,
param_name: str = 'width',
unit: str | sc.Unit = 'meV',
Expand All @@ -148,21 +144,21 @@ def _create_width_parameter(

Parameters
----------
width : Numeric | Parameter
The width value or Parameter.
width : Numeric
The width value.
name : str
The name of the model component.
param_name : str, default='width'
param_name : str, default="width"
The name of the width parameter.
unit : str | sc.Unit, default='meV'
unit : str | sc.Unit, default="meV"
The unit of the width Parameter.
minimum_width : float, default=MINIMUM_WIDTH
The minimum allowed width.

Raises
------
TypeError
If width is not a number or a Parameter.
If width is not a number.
ValueError
If width is non-positive.

Expand All @@ -171,29 +167,19 @@ def _create_width_parameter(
Parameter
The validated width Parameter.
"""
if not isinstance(width, (Numeric, Parameter)):
raise TypeError(f'{param_name} must be a number or a Parameter.')

if isinstance(width, Numeric):
if not np.isfinite(width):
raise ValueError(f'{param_name} must be a finite number or a Parameter')

if float(width) < minimum_width:
raise ValueError(
f'The {param_name} of a {self.__class__.__name__} must be greater than zero.'
)
width = Parameter(
name=name + ' ' + param_name,
value=float(width),
unit=unit,
min=minimum_width,
if not isinstance(width, Numeric):
raise TypeError(f'{param_name} must be a number.')

if not np.isfinite(width):
raise ValueError(f'{param_name} must be a finite number')

if float(width) < minimum_width:
raise ValueError(
f'The {param_name} of a {self.__class__.__name__} must be greater than zero.'
)
else:
if width.value <= 0:
raise ValueError(
f'The {param_name} of a {self.__class__.__name__} must be greater than zero.'
)
if width.min < minimum_width:
width.min = minimum_width

return width
return Parameter(
name=name + ' ' + param_name,
value=float(width),
unit=unit,
min=minimum_width,
)
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,13 @@ def create_component_collections(
# ------------------------------#
# Create Lorentzian
# ------------------------------#
if self._allow_Q_variation['lorentzian_width'] is True:
width = self._lorentzian_width_list[i]
else:
width = 1.0
lorentzian_component = Lorentzian(
name=self.lorentzian_name,
display_name=self.lorentzian_display_name,
unit=self.unit,
width=width,
)
if self._allow_Q_variation['lorentzian_width'] is True:
lorentzian_component._width = self._lorentzian_width_list[i] # noqa: SLF001

# If the width is allowed to vary with Q it is independent.
# If the width is not allowed to vary with Q it must be made
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,6 @@ def test_initialization(self, dho: DampedHarmonicOscillator):
assert dho.width.value == pytest.approx(0.3)
assert dho.unit == 'meV'

def test_init_with_parameters(self):
# WHEN
area_param = Parameter(name='area_param', value=3.0, unit='meV')
center_param = Parameter(name='center_param', value=1.0, unit='meV')
width_param = Parameter(name='width_param', value=0.8, unit='meV')

# THEN
dho = DampedHarmonicOscillator(
display_name='Paramdho',
area=area_param,
center=center_param,
width=width_param,
unit='meV',
)

# EXPECT
assert dho.display_name == 'Paramdho'
assert dho.area is area_param
assert dho.center is center_param
assert dho.width is width_param
assert dho.unit == 'meV'

@pytest.mark.parametrize(
'kwargs, expected_message',
[
Expand Down
Loading
Loading