-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffusion_model_base.py
More file actions
96 lines (78 loc) · 3.01 KB
/
diffusion_model_base.py
File metadata and controls
96 lines (78 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# SPDX-FileCopyrightText: 2025-2026 EasyDynamics contributors <https://github.com/easyscience>
# SPDX-License-Identifier: BSD-3-Clause
import scipp as sc
from easyscience.base_classes.model_base import ModelBase
from easyscience.variable import DescriptorNumber
from easyscience.variable import Parameter
from scipp import UnitError
from easydynamics.utils.utils import Numeric
class DiffusionModelBase(ModelBase):
"""Base class for constructing diffusion models."""
def __init__(
self,
display_name='MyDiffusionModel',
unique_name: str | None = None,
scale: Numeric = 1.0,
unit: str | sc.Unit = 'meV',
):
"""Initialize a new DiffusionModel.
Parameters
----------
display_name : str
Display name of the diffusion model.
unit : str or sc.Unit, optional
Unit of the diffusion model. Defaults to "meV".
"""
if not isinstance(scale, Numeric):
raise TypeError('scale must be a number.')
scale = Parameter(name='scale', value=float(scale), fixed=False, min=0.0)
try:
test = DescriptorNumber(name='test', value=1, unit=unit)
test.convert_unit('meV')
except Exception as e:
raise UnitError(
f'Invalid unit: {unit}. Unit must be a string or scipp Unit and convertible to meV.' # noqa: E501
) from e
super().__init__(display_name=display_name, unique_name=unique_name)
self._unit = unit
self._scale = scale
# ------------------------------------------------------------------
# Properties
# ------------------------------------------------------------------
@property
def unit(self) -> str:
"""Get the unit of the DiffusionModel.
Returns
-------
str or sc.Unit or None
"""
return str(self._unit)
@unit.setter
def unit(self, unit_str: str) -> None:
raise AttributeError(
(
f'Unit is read-only. Use convert_unit to change the unit between allowed types '
f'or create a new {self.__class__.__name__} with the desired unit.'
)
) # noqa: E501
@property
def scale(self) -> Parameter:
"""Get the scale parameter of the diffusion model.
Returns
-------
Parameter
Scale parameter.
"""
return self._scale
@scale.setter
def scale(self, scale: Numeric) -> None:
"""Set the scale parameter of the diffusion model."""
if not isinstance(scale, Numeric):
raise TypeError('scale must be a number.')
self._scale.value = scale
# ------------------------------------------------------------------
# dunder methods
# ------------------------------------------------------------------
def __repr__(self):
"""String representation of the Diffusion model."""
return f'{self.__class__.__name__}(display_name={self.display_name}, unit={self.unit})'