fakecbed 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fakecbed/tds.py ADDED
@@ -0,0 +1,268 @@
1
+ """For modelling thermal diffuse scattering.
2
+
3
+ """
4
+
5
+
6
+
7
+ #####################################
8
+ ## Load libraries/packages/modules ##
9
+ #####################################
10
+
11
+ # For accessing attributes of functions.
12
+ import inspect
13
+
14
+ # For randomly selecting items in dictionaries.
15
+ import random
16
+
17
+ # For performing deep copies.
18
+ import copy
19
+
20
+
21
+
22
+ # For general array handling.
23
+ import numpy as np
24
+ import torch
25
+
26
+ # For validating and converting objects.
27
+ import czekitout.check
28
+ import czekitout.convert
29
+
30
+ # For defining classes that support enforced validation, updatability,
31
+ # pre-serialization, and de-serialization.
32
+ import fancytypes
33
+
34
+
35
+
36
+ # For validating, pre-serializing, and de-pre-serializing instances of the class
37
+ # :class:`fakecbed.shapes.Peak`. Also for defining subclasses of the
38
+ # :class:`fakecbed.shapes.BaseShape` class.
39
+ import fakecbed.shapes
40
+
41
+
42
+
43
+ ##################################
44
+ ## Define classes and functions ##
45
+ ##################################
46
+
47
+ # List of public objects in module.
48
+ __all__ = ["Model"]
49
+
50
+
51
+
52
+ def _check_and_convert_peaks(params):
53
+ current_func_name = inspect.stack()[0][3]
54
+ char_idx = 19
55
+ obj_name = current_func_name[char_idx:]
56
+ obj = params[obj_name]
57
+
58
+ accepted_types = (fakecbed.shapes.Peak,)
59
+
60
+ try:
61
+ for peak in obj:
62
+ kwargs = {"obj": peak,
63
+ "obj_name": "peak",
64
+ "accepted_types": accepted_types}
65
+ czekitout.check.if_instance_of_any_accepted_types(**kwargs)
66
+ except:
67
+ err_msg = globals()[current_func_name+"_err_msg_1"]
68
+ raise TypeError(err_msg)
69
+
70
+ peaks = copy.deepcopy(obj)
71
+
72
+ return peaks
73
+
74
+
75
+
76
+ def _pre_serialize_peaks(peaks):
77
+ obj_to_pre_serialize = random.choice(list(locals().values()))
78
+ serializable_rep = tuple()
79
+ for elem in obj_to_pre_serialize:
80
+ serializable_rep += (elem.pre_serialize(),)
81
+
82
+ return serializable_rep
83
+
84
+
85
+
86
+ def _de_pre_serialize_peaks(serializable_rep):
87
+ peaks = tuple()
88
+ for pre_serialized_peak in serializable_rep:
89
+ peak = fakecbed.shapes.Peak.de_pre_serialize(pre_serialized_peak)
90
+ peaks += (peak,)
91
+
92
+ return peaks
93
+
94
+
95
+
96
+ def _check_and_convert_constant_bg(params):
97
+ current_func_name = inspect.stack()[0][3]
98
+ obj_name = current_func_name[19:]
99
+ kwargs = {"obj": params[obj_name], "obj_name": obj_name}
100
+ constant_bg = czekitout.convert.to_nonnegative_float(**kwargs)
101
+
102
+ return constant_bg
103
+
104
+
105
+
106
+ def _pre_serialize_constant_bg(constant_bg):
107
+ obj_to_pre_serialize = random.choice(list(locals().values()))
108
+ serializable_rep = obj_to_pre_serialize
109
+
110
+ return serializable_rep
111
+
112
+
113
+
114
+ def _de_pre_serialize_constant_bg(serializable_rep):
115
+ constant_bg = serializable_rep
116
+
117
+ return constant_bg
118
+
119
+
120
+
121
+ _default_peaks = \
122
+ tuple()
123
+ _default_constant_bg = \
124
+ 0
125
+ _default_skip_validation_and_conversion = \
126
+ fakecbed.shapes._default_skip_validation_and_conversion
127
+
128
+
129
+
130
+ class Model(fakecbed.shapes.BaseShape):
131
+ r"""The intensity pattern of a thermal diffuse scattering (TDS) model.
132
+
133
+ Let :math:`N_{\text{TDS}}`, and :math:`\left\{
134
+ \mathcal{I}_{k;\text{TDS}}\left(u_{x},u_{y}\right)\right\}
135
+ _{k=0}^{N_{\text{TDS}}-1}` be the number of peaks in the intensity pattern
136
+ of the TDS model, and the intensity patterns of said peaks
137
+ respectively. Furthermore, let :math:`C_{\text{TDS}}` be the value of the
138
+ intensity pattern of the TDS model at any point that is an arbitrarily large
139
+ distance away from the origin of the image coordinate axes. The undistorted
140
+ intensity pattern of the TDS model is given by:
141
+
142
+ .. math ::
143
+ \mathcal{I}_{\text{TDS}}\left(u_{x},u_{y}\right)=
144
+ C_{\text{TDS}}+\left|\sum_{k=0}^{N_{\text{TDS}}-1}
145
+ \mathcal{I}_{k;\text{TDS}}\left(u_{x},u_{y}\right)\right|,
146
+ :label: intensity_pattern_of_tds_model__1
147
+
148
+ where :math:`u_{x}` and :math:`u_{y}` are fractional horizontal and vertical
149
+ coordinates of the undistorted intensity pattern of the TDS model
150
+ respectively.
151
+
152
+ Parameters
153
+ ----------
154
+ peaks : `array_like` (`fakecbed.shapes.Peak`, ndim=1), optional
155
+ The intensity patterns of the peaks, :math:`\left\{
156
+ \mathcal{I}_{k;\text{TDS}}\left(u_{x},u_{y}\right)\right\}
157
+ _{k=0}^{N_{\text{TDS}}-1}`, inside the intensity pattern of the TDS
158
+ model.
159
+ constant_bg : `float`, optional
160
+ The value of the intensity pattern of the TDS model,
161
+ :math:`C_{\text{TDS}}`, at any point that is an arbitrarily large
162
+ distance away from the origin of the image coordinate axes. Must be
163
+ nonnegative.
164
+ skip_validation_and_conversion : `bool`, optional
165
+ Let ``validation_and_conversion_funcs`` and ``core_attrs`` denote the
166
+ attributes :attr:`~fancytypes.Checkable.validation_and_conversion_funcs`
167
+ and :attr:`~fancytypes.Checkable.core_attrs` respectively, both of which
168
+ being `dict` objects.
169
+
170
+ Let ``params_to_be_mapped_to_core_attrs`` denote the `dict`
171
+ representation of the constructor parameters excluding the parameter
172
+ ``skip_validation_and_conversion``, where each `dict` key ``key`` is a
173
+ different constructor parameter name, excluding the name
174
+ ``"skip_validation_and_conversion"``, and
175
+ ``params_to_be_mapped_to_core_attrs[key]`` would yield the value of the
176
+ constructor parameter with the name given by ``key``.
177
+
178
+ If ``skip_validation_and_conversion`` is set to ``False``, then for each
179
+ key ``key`` in ``params_to_be_mapped_to_core_attrs``,
180
+ ``core_attrs[key]`` is set to ``validation_and_conversion_funcs[key]
181
+ (params_to_be_mapped_to_core_attrs)``.
182
+
183
+ Otherwise, if ``skip_validation_and_conversion`` is set to ``True``,
184
+ then ``core_attrs`` is set to
185
+ ``params_to_be_mapped_to_core_attrs.copy()``. This option is desired
186
+ primarily when the user wants to avoid potentially expensive deep copies
187
+ and/or conversions of the `dict` values of
188
+ ``params_to_be_mapped_to_core_attrs``, as it is guaranteed that no
189
+ copies or conversions are made in this case.
190
+
191
+ """
192
+ ctor_param_names = ("peaks",
193
+ "constant_bg")
194
+ kwargs = {"namespace_as_dict": globals(),
195
+ "ctor_param_names": ctor_param_names}
196
+
197
+ _validation_and_conversion_funcs_ = \
198
+ fancytypes.return_validation_and_conversion_funcs(**kwargs)
199
+ _pre_serialization_funcs_ = \
200
+ fancytypes.return_pre_serialization_funcs(**kwargs)
201
+ _de_pre_serialization_funcs_ = \
202
+ fancytypes.return_de_pre_serialization_funcs(**kwargs)
203
+
204
+ del ctor_param_names, kwargs
205
+
206
+
207
+
208
+ def __init__(self,
209
+ peaks=\
210
+ _default_peaks,
211
+ constant_bg=\
212
+ _default_constant_bg,
213
+ skip_validation_and_conversion=\
214
+ _default_skip_validation_and_conversion):
215
+ ctor_params = {key: val
216
+ for key, val in locals().items()
217
+ if (key not in ("self", "__class__"))}
218
+ fakecbed.shapes.BaseShape.__init__(self, ctor_params)
219
+
220
+ self.execute_post_core_attrs_update_actions()
221
+
222
+ return None
223
+
224
+
225
+
226
+ def execute_post_core_attrs_update_actions(self):
227
+ self_core_attrs = self.get_core_attrs(deep_copy=False)
228
+ for self_core_attr_name in self_core_attrs:
229
+ attr_name = "_"+self_core_attr_name
230
+ attr = self_core_attrs[self_core_attr_name]
231
+ setattr(self, attr_name, attr)
232
+
233
+ return None
234
+
235
+
236
+
237
+ def update(self,
238
+ new_core_attr_subset_candidate,
239
+ skip_validation_and_conversion=\
240
+ _default_skip_validation_and_conversion):
241
+ super().update(new_core_attr_subset_candidate,
242
+ skip_validation_and_conversion)
243
+ self.execute_post_core_attrs_update_actions()
244
+
245
+ return None
246
+
247
+
248
+
249
+ def _eval(self, u_x, u_y):
250
+ peaks = self._peaks
251
+ C = self._constant_bg
252
+
253
+ result = torch.zeros_like(u_x)
254
+ for peak in peaks:
255
+ result += peak._eval(u_x, u_y)
256
+ result = C+torch.abs(result)
257
+
258
+ return result
259
+
260
+
261
+
262
+ ###########################
263
+ ## Define error messages ##
264
+ ###########################
265
+
266
+ _check_and_convert_peaks_err_msg_1 = \
267
+ ("The object ``peaks`` must be a sequence of `fakecbed.shapes.Peak` "
268
+ "objects.")
fakecbed/version.py ADDED
@@ -0,0 +1,16 @@
1
+ # file generated by setuptools_scm
2
+ # don't change, don't track in version control
3
+ TYPE_CHECKING = False
4
+ if TYPE_CHECKING:
5
+ from typing import Tuple, Union
6
+ VERSION_TUPLE = Tuple[Union[int, str], ...]
7
+ else:
8
+ VERSION_TUPLE = object
9
+
10
+ version: str
11
+ __version__: str
12
+ __version_tuple__: VERSION_TUPLE
13
+ version_tuple: VERSION_TUPLE
14
+
15
+ __version__ = version = '0.2.0'
16
+ __version_tuple__ = version_tuple = (0, 2, 0)