Skip to content

Commit 938a366

Browse files
Update units, remove unused tests
1 parent f9b7fa1 commit 938a366

File tree

8 files changed

+59
-96
lines changed

8 files changed

+59
-96
lines changed

src/easydynamics/sample_model/components/damped_harmonic_oscillator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from easyscience.variable import Parameter
88

9-
from easydynamics.sample_model.components.model_component import ModelComponent
9+
from .model_component import ModelComponent
1010

1111
import scipp as sc
1212

@@ -66,7 +66,7 @@ def __init__(
6666
center = float(center)
6767

6868
super().__init__(name=name)
69-
self.unit = unit # Set the unit for the component
69+
self._unit = unit # Set the unit for the component
7070
# Create Parameters from floats, or set Parameters if already provided
7171
if isinstance(center, Numeric):
7272
self.center = Parameter(name=name + " center", value=center, unit=unit)
@@ -100,9 +100,9 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
100100
# Handle units
101101
if isinstance(x, sc.Variable):
102102
x_in = x.values
103-
if self.unit is not None and x.unit != self.unit:
103+
if self._unit is not None and x.unit != self._unit:
104104
warnings.warn(
105-
f"Input x has unit {x.unit}, but DHO component has unit {self.unit}. Converting DHO to {x.unit}."
105+
f"Input x has unit {x.unit}, but DHO component has unit {self._unit}. Converting DHO to {x.unit}."
106106
)
107107
self.convert_unit(x.unit.name)
108108
else:
@@ -138,7 +138,7 @@ def convert_unit(self, unit: str):
138138
self.area.convert_unit(unit)
139139
self.center.convert_unit(unit)
140140
self.width.convert_unit(unit)
141-
self.unit = unit
141+
self._unit = unit
142142

143143
def copy(self) -> DampedHarmonicOscillator:
144144
"""
@@ -150,12 +150,12 @@ def copy(self) -> DampedHarmonicOscillator:
150150
area=self.area.value,
151151
center=self.center.value,
152152
width=self.width.value,
153-
unit=self.unit,
153+
unit=self._unit,
154154
)
155155
model_copy.area.fixed = self.area.fixed
156156
model_copy.center.fixed = self.center.fixed
157157
model_copy.width.fixed = self.width.fixed
158158
return model_copy
159159

160160
def __repr__(self):
161-
return f"DampedHarmonicOscillator(name = {self.name}, unit = {self.unit},\n area = {self.area},\n center = {self.center},\n width = {self.width})"
161+
return f"DampedHarmonicOscillator(name = {self.name}, unit = {self._unit},\n area = {self.area},\n center = {self.center},\n width = {self.width})"

src/easydynamics/sample_model/components/delta_function.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from easyscience.variable import Parameter
66

7-
from easydynamics.sample_model.components.model_component import ModelComponent
7+
from .model_component import ModelComponent
88

99

1010
import warnings
@@ -51,7 +51,7 @@ def __init__(
5151
center = float(center)
5252

5353
super().__init__(name=name)
54-
self.unit = unit
54+
self._unit = unit
5555
# Create Parameters from floats, or set Parameters if already provided
5656
if center is None:
5757
self.center = Parameter(
@@ -94,7 +94,7 @@ def convert_unit(self, unit):
9494
"""
9595
self.area.convert_unit(unit)
9696
self.center.convert_unit(unit)
97-
self.unit = unit
97+
self._unit = unit
9898

9999
def copy(self) -> DeltaFunction:
100100
"""
@@ -104,11 +104,11 @@ def copy(self) -> DeltaFunction:
104104
name=self.name,
105105
area=self.area.value,
106106
center=self.center.value,
107-
unit=self.unit,
107+
unit=self._unit,
108108
)
109109
model_copy.area.fixed = self.area.fixed
110110
model_copy.center.fixed = self.center.fixed
111111
return model_copy
112112

113113
def __repr__(self):
114-
return f"DeltaFunction(name = {self.name}, unit = {self.unit},\n area = {self.area},\n center = {self.center}"
114+
return f"DeltaFunction(name = {self.name}, unit = {self._unit},\n area = {self.area},\n center = {self.center}"

src/easydynamics/sample_model/components/gaussian.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from easyscience.variable import Parameter
88

9-
from easydynamics.sample_model.components.model_component import ModelComponent
9+
from .model_component import ModelComponent
1010

1111
import scipp as sc
1212

@@ -61,7 +61,7 @@ def __init__(
6161
area = float(area)
6262

6363
super().__init__(name=name)
64-
self.unit = unit # Set the unit for the component
64+
self._unit = unit # Set the unit for the component
6565

6666
# Create Parameters from floats, or set Parameters if already provided
6767
if center is None:
@@ -98,9 +98,9 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
9898
# Handle units
9999
if isinstance(x, sc.Variable):
100100
x_in = x.values
101-
if self.unit is not None and x.unit != self.unit:
101+
if self._unit is not None and x.unit != self._unit:
102102
warnings.warn(
103-
f"Input x has unit {x.unit}, but Gaussian component has unit {self.unit}. Converting Gaussian to {x.unit}."
103+
f"Input x has unit {x.unit}, but Gaussian component has unit {self._unit}. Converting Gaussian to {x.unit}."
104104
)
105105
self.convert_unit(x.unit.name)
106106
else:
@@ -131,7 +131,7 @@ def convert_unit(self, unit: str):
131131
self.area.convert_unit(unit)
132132
self.center.convert_unit(unit)
133133
self.width.convert_unit(unit)
134-
self.unit = unit
134+
self._unit = unit
135135

136136
def copy(self) -> Gaussian:
137137
"""
@@ -143,7 +143,7 @@ def copy(self) -> Gaussian:
143143
area=self.area.value,
144144
center=self.center.value,
145145
width=self.width.value,
146-
unit=self.unit,
146+
unit=self._unit,
147147
)
148148

149149
model_copy.area.fixed = self.area.fixed
@@ -152,4 +152,4 @@ def copy(self) -> Gaussian:
152152
return model_copy
153153

154154
def __repr__(self):
155-
return f"Gaussian(name = {self.name}, unit = {self.unit},\n area = {self.area},\n center = {self.center},\n width = {self.width})"
155+
return f"Gaussian(name = {self.name}, unit = {self._unit},\n area = {self.area},\n center = {self.center},\n width = {self.width})"

src/easydynamics/sample_model/components/lorentzian.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from easyscience.variable import Parameter
88

9-
from easydynamics.sample_model.components.model_component import ModelComponent
9+
from .model_component import ModelComponent
1010

1111
import scipp as sc
1212

@@ -64,7 +64,7 @@ def __init__(
6464
center = float(center)
6565

6666
super().__init__(name=name)
67-
self.unit = unit # Set the unit for the component
67+
self._unit = unit # Set the unit for the component
6868

6969
# Create Parameters from floats, or set Parameters if already provided
7070
if center is None:
@@ -101,9 +101,9 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
101101
# Handle units
102102
if isinstance(x, sc.Variable):
103103
x_in = x.values
104-
if self.unit is not None and x.unit != self.unit:
104+
if self._unit is not None and x.unit != self._unit:
105105
warnings.warn(
106-
f"Input x has unit {x.unit}, but Lorentzian component has unit {self.unit}. Converting Lorentzian to {x.unit}."
106+
f"Input x has unit {x.unit}, but Lorentzian component has unit {self._unit}. Converting Lorentzian to {x.unit}."
107107
)
108108
self.convert_unit(x.unit.name)
109109
else:
@@ -133,20 +133,20 @@ def convert_unit(self, unit: str):
133133
self.area.convert_unit(unit)
134134
self.center.convert_unit(unit)
135135
self.width.convert_unit(unit)
136-
self.unit = unit
136+
self._unit = unit
137137

138138
def copy(self) -> Lorentzian:
139139
model_copy = Lorentzian(
140140
name=self.name,
141141
area=self.area.value,
142142
center=self.center.value,
143143
width=self.width.value,
144-
unit=self.unit,
144+
unit=self._unit,
145145
)
146146
model_copy.area.fixed = self.area.fixed
147147
model_copy.center.fixed = self.center.fixed
148148
model_copy.width.fixed = self.width.fixed
149149
return model_copy
150150

151151
def __repr__(self):
152-
return f"Lorentzian(name = {self.name}, unit = {self.unit},\n area = {self.area},\n center = {self.center},\n width = {self.width})"
152+
return f"Lorentzian(name = {self.name}, unit = {self._unit},\n area = {self.area},\n center = {self.center},\n width = {self.width})"

src/easydynamics/sample_model/components/model_component.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from abc import abstractmethod
44

5-
from typing import Union, List
5+
from typing import Union, List, Optional
66

77
import numpy as np
88

@@ -19,9 +19,27 @@ class ModelComponent(ObjBase):
1919
Abstract base class for all model components.
2020
"""
2121

22-
def __init__(self, name="ModelComponent"):
22+
def __init__(self, name="ModelComponent", unit: Optional[str] = None):
2323
super().__init__(name=name)
24-
self.unit = None
24+
self._unit = unit
25+
26+
@property
27+
def unit(self) -> str:
28+
"""
29+
Get the unit.
30+
31+
:return: Unit as a string.
32+
"""
33+
return str(self._unit)
34+
35+
@unit.setter
36+
def unit(self, unit_str: str) -> None:
37+
raise AttributeError(
38+
(
39+
f"Unit is read-only. Use convert_unit to change the unit between allowed types "
40+
f"or create a new {self.__class__.__name__} with the desired unit."
41+
)
42+
) # noqa: E501
2543

2644
def fix_all_parameters(self):
2745
"""Fix all parameters in the model component."""

src/easydynamics/sample_model/components/polynomial.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from easyscience.variable import Parameter
88

9-
from easydynamics.sample_model.components.model_component import ModelComponent
9+
from .model_component import ModelComponent
1010

1111
import scipp as sc
1212

@@ -50,14 +50,14 @@ def __init__(
5050
)
5151
for i, coef in enumerate(coefficients)
5252
]
53-
self.unit = unit
53+
self._unit = unit
5454

5555
def evaluate(self, x: Union[Numeric, sc.Variable]) -> np.ndarray:
5656
if isinstance(x, sc.Variable):
5757
x_in = x.values
58-
if self.unit is not None and x.unit != self.unit:
58+
if self._unit is not None and x.unit != self._unit:
5959
raise ValueError(
60-
f"Input x has unit {x.unit}, but Polynomial component has unit {self.unit}. Change the unit of the Polynomial and try again. "
60+
f"Input x has unit {x.unit}, but Polynomial component has unit {self._unit}. Change the unit of the Polynomial and try again. "
6161
)
6262
else:
6363
x_in = x
@@ -100,7 +100,7 @@ def __repr__(self):
100100
coeffs_str = ", ".join(
101101
f"{param.name}={param.value}" for param in self.coefficients
102102
)
103-
return f"Polynomial(name = {self.name}, unit = {self.unit},\n coefficients = [{coeffs_str}])"
103+
return f"Polynomial(name = {self.name}, unit = {self._unit},\n coefficients = [{coeffs_str}])"
104104

105105
def convert_unit(self, unit):
106106
raise NotImplementedError(

src/easydynamics/sample_model/components/voigt.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from easyscience.variable import Parameter
1010

11-
from easydynamics.sample_model.components.model_component import ModelComponent
11+
from .model_component import ModelComponent
1212

1313
import scipp as sc
1414

@@ -78,7 +78,7 @@ def __init__(
7878

7979
super().__init__(name=name)
8080

81-
self.unit = unit # Set the unit for the component
81+
self._unit = unit # Set the unit for the component
8282
# Create Parameters from floats, or set Parameters if already provided
8383
if center is None:
8484
self.center = Parameter(
@@ -128,9 +128,9 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
128128
# Handle units
129129
if isinstance(x, sc.Variable):
130130
x_in = x.values
131-
if self.unit is not None and x.unit != self.unit:
131+
if self._unit is not None and x.unit != self._unit:
132132
warnings.warn(
133-
f"Input x has unit {x.unit}, but Voigt component has unit {self.unit}. Converting Voigt to {x.unit}."
133+
f"Input x has unit {x.unit}, but Voigt component has unit {self._unit}. Converting Voigt to {x.unit}."
134134
)
135135
self.convert_unit(x.unit.name)
136136
else:
@@ -152,7 +152,7 @@ def convert_unit(self, unit: str):
152152
self.center.convert_unit(unit)
153153
self.gaussian_width.convert_unit(unit)
154154
self.lorentzian_width.convert_unit(unit)
155-
self.unit = unit
155+
self._unit = unit
156156

157157
def get_parameters(self):
158158
"""
@@ -169,7 +169,7 @@ def copy(self) -> Voigt:
169169
center=self.center.value,
170170
gaussian_width=self.gaussian_width.value,
171171
lorentzian_width=self.lorentzian_width.value,
172-
unit=self.unit,
172+
unit=self._unit,
173173
)
174174
model_copy.area.fixed = self.area.fixed
175175
model_copy.center.fixed = self.center.fixed
@@ -179,4 +179,4 @@ def copy(self) -> Voigt:
179179
return model_copy
180180

181181
def __repr__(self):
182-
return f"Voigt(name = {self.name}, unit = {self.unit},\n area = {self.area},\n center = {self.center},\n gaussian_width = {self.gaussian_width},\n lorentzian_width = {self.lorentzian_width})"
182+
return f"Voigt(name = {self.name}, unit = {self._unit},\n area = {self.area},\n center = {self.center},\n gaussian_width = {self.gaussian_width},\n lorentzian_width = {self.lorentzian_width})"

tests/unit_tests/sample_model/test_components.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -80,61 +80,6 @@ def test_get_parameter_ambiguous_match_raises(self, dummy):
8080
with pytest.raises(ValueError, match="Ambiguous parameter name"):
8181
dummy.get_parameter("are")
8282

83-
def test_set_parameter_value(self, dummy):
84-
# WHEN
85-
dummy.set_parameter_value("width", 10.0)
86-
87-
# THEN EXPECT
88-
assert dummy.width.value == 10.0
89-
90-
def test_set_parameter_bounds_min(self, dummy):
91-
# WHEN
92-
dummy.set_parameter_bounds("width", min=1.0)
93-
94-
# THEN EXPECT
95-
assert dummy.width.min == 1.0
96-
assert dummy.width.max == np.inf
97-
98-
def test_set_parameter_bounds_max(self, dummy):
99-
# WHEN
100-
dummy.set_parameter_bounds("width", max=5.0)
101-
102-
# THEN EXPECT
103-
assert dummy.width.min == -np.inf
104-
assert dummy.width.max == 5.0
105-
106-
def test_set_parameter_bounds_min_max(self, dummy):
107-
# WHEN
108-
dummy.set_parameter_bounds("width", min=1.0, max=5.0)
109-
110-
# THEN EXPECT
111-
assert dummy.width.min == 1.0
112-
assert dummy.width.max == 5.0
113-
114-
def test_set_parameter_bounds_with_unit_conversion(self, dummy):
115-
# WHEN
116-
dummy.set_parameter_bounds("width", min=1000.0, max=5000.0, unit="microeV")
117-
118-
# THEN EXPECT
119-
assert dummy.width.min == 1000
120-
assert dummy.width.max == 5000
121-
assert dummy.width.unit == "µeV"
122-
123-
def test_fix_parameter(self, dummy):
124-
# WHEN
125-
dummy.fix_parameter("width")
126-
127-
# THEN EXPECT
128-
assert dummy.width.fixed is True
129-
130-
def test_free_parameter(self, dummy):
131-
# WHEN
132-
dummy.fix_parameter("width")
133-
# THEN
134-
dummy.free_parameter("width")
135-
# EXPECT
136-
assert dummy.width.fixed is False
137-
13883
def test_repr(self, dummy):
13984
repr_str = repr(dummy)
14085
assert "DummyComponent" in repr_str

0 commit comments

Comments
 (0)