Skip to content

Commit 72f61b9

Browse files
committed
added docstrings to models
1 parent 2a32ff3 commit 72f61b9

File tree

1 file changed

+228
-44
lines changed

1 file changed

+228
-44
lines changed

RATapi/models.py

Lines changed: 228 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""The models module. Contains the pydantic models used by RAT to store project parameters."""
22

33
import pathlib
4+
from itertools import count
45
from typing import Any
56

67
import numpy as np
@@ -15,23 +16,15 @@
1516
from strenum import StrEnum
1617

1718

18-
def int_sequence():
19-
"""Iterate through integers for use as model counters."""
20-
num = 1
21-
while True:
22-
yield str(num)
23-
num += 1
24-
25-
2619
# Create a counter for each model
27-
background_number = int_sequence()
28-
contrast_number = int_sequence()
29-
custom_file_number = int_sequence()
30-
data_number = int_sequence()
31-
domain_contrast_number = int_sequence()
32-
layer_number = int_sequence()
33-
parameter_number = int_sequence()
34-
resolution_number = int_sequence()
20+
background_number = count(1)
21+
contrast_number = count(1)
22+
custom_file_number = count(1)
23+
data_number = count(1)
24+
domain_contrast_number = count(1)
25+
layer_number = count(1)
26+
parameter_number = count(1)
27+
resolution_number = count(1)
3528

3629

3730
class RATModel(BaseModel, validate_assignment=True, extra="forbid"):
@@ -52,10 +45,30 @@ def __str__(self):
5245

5346

5447
class Background(RATModel):
55-
"""Defines the Backgrounds in RAT."""
56-
57-
name: str = Field(default_factory=lambda: "New Background " + next(background_number), min_length=1)
48+
"""A background signal.
49+
50+
Parameters
51+
----------
52+
name : str
53+
The name of the background.
54+
type : TypeOptions
55+
The type of background (constant, function or data)
56+
source : str
57+
The source data for the background;
58+
- if type is 'constant', this should be the name of a background parameter.
59+
- if type is 'data', this should be the name of a dataset defined in `Project.data`.
60+
- if type is 'function', this should be the name of a custom function defined in `Project.custom_files`.
61+
value_1, value_2, ..., value_5 : str
62+
Values required by the background.
63+
- if type is 'constant', this should be blank.
64+
- if type is 'data', value_1 may be the parameter name for an optional offset. Other values are ignored.
65+
- if type is 'function', these values may be the names of up to 5 parameters which are passed to the function.
66+
67+
"""
68+
69+
name: str = Field(default_factory=lambda: f"New Background {next(background_number)}", min_length=1)
5870
type: TypeOptions = TypeOptions.Constant
71+
source: str = ""
5972
value_1: str = ""
6073
value_2: str = ""
6174
value_3: str = ""
@@ -64,9 +77,38 @@ class Background(RATModel):
6477

6578

6679
class Contrast(RATModel):
67-
"""Groups together all of the components of the model."""
68-
69-
name: str = Field(default_factory=lambda: "New Contrast " + next(contrast_number), min_length=1)
80+
"""A group of all of the components of a model.
81+
82+
Parameters
83+
----------
84+
name : str
85+
The name of the contrast.
86+
data : str
87+
The name of the dataset used by the contrast.
88+
background : str
89+
The name of the background for the contrast.
90+
background_action : BackgroundActions
91+
Whether the background should be added ('add') or subtracted ('subtract') from the data.
92+
bulk_in : str
93+
The name of the bulk-in parameter which defines the SLD of the interface between the
94+
first layer and the environment.
95+
bulk_out : str
96+
The name of the bulk-out parameter which defines the SLD of the interface between the last
97+
layer and the environment.
98+
scalefactor : str
99+
resolution : str
100+
The name of the instrument resolution for this contrast.
101+
resample : bool
102+
Whether adaptive resampling should be used for interface microslicing.
103+
model : list[str]
104+
If this is a standard layers model, this should be a list of layer names
105+
that make up the slab model for this contrast.
106+
For custom models, this should be a list of custom file names of the custom
107+
model functions.
108+
109+
"""
110+
111+
name: str = Field(default_factory=lambda: f"New Contrast {next(contrast_number)}", min_length=1)
70112
data: str = ""
71113
background: str = ""
72114
background_action: BackgroundActions = BackgroundActions.Add
@@ -111,9 +153,39 @@ def __str__(self):
111153

112154

113155
class ContrastWithRatio(RATModel):
114-
"""Groups together all of the components of the model including domain terms."""
115-
116-
name: str = Field(default_factory=lambda: "New Contrast " + next(contrast_number), min_length=1)
156+
"""A group of all of the components of a model, including domain terms.
157+
158+
Parameters
159+
----------
160+
name : str
161+
The name of the contrast.
162+
data : str
163+
The name of the dataset used by the contrast.
164+
background : str
165+
The name of the background for the contrast.
166+
background_action : BackgroundActions
167+
Whether the background should be added ('add') or subtracted ('subtract') from the data.
168+
bulk_in : str
169+
The name of the bulk-in parameter which defines the SLD of the interface between the
170+
first layer and the environment.
171+
bulk_out : str
172+
The name of the bulk-out parameter which defines the SLD of the interface between the last
173+
layer and the environment.
174+
scalefactor : str
175+
resolution : str
176+
The name of the instrument resolution for this contrast.
177+
resample : bool
178+
Whether adaptive resampling should be used for interface microslicing.
179+
domain_ratio : str
180+
model : list[str]
181+
If this is a standard layers model, this should be a list of layer names
182+
that make up the slab model for this contrast.
183+
For custom models, this should be a list of custom file names of the custom
184+
model functions.
185+
186+
"""
187+
188+
name: str = Field(default_factory=lambda: f"New Contrast {next(contrast_number)}", min_length=1)
117189
data: str = ""
118190
background: str = ""
119191
background_action: BackgroundActions = BackgroundActions.Add
@@ -147,9 +219,24 @@ def __str__(self):
147219

148220

149221
class CustomFile(RATModel):
150-
"""Defines the files containing functions to run when using custom models."""
151-
152-
name: str = Field(default_factory=lambda: "New Custom File " + next(custom_file_number), min_length=1)
222+
"""A file containing functions to use for a custom model, function background or function resolution.
223+
224+
Parameters
225+
----------
226+
name : str
227+
The name of this custom file object.
228+
filename : str
229+
The name of the file containing the custom function.
230+
function_name : str
231+
The name of the custom function within the file.
232+
language : Languages
233+
What language the custom function is written in: 'matlab', 'python', or 'cpp' (C++)
234+
path : pathlib.Path
235+
The path to the custom file.
236+
237+
"""
238+
239+
name: str = Field(default_factory=lambda: f"New Custom File {next(custom_file_number)}", min_length=1)
153240
filename: str = ""
154241
function_name: str = ""
155242
language: Languages = Languages.Python
@@ -174,9 +261,18 @@ def set_matlab_function_name(self):
174261

175262

176263
class Data(RATModel, arbitrary_types_allowed=True):
177-
"""Defines the dataset required for each contrast."""
264+
"""A dataset required for a contrast.
265+
266+
name : str
267+
The name of this dataset.
268+
data : np.ndarray[np.float64]
269+
The (x,y,z) data for this dataset, given as a Numpy array of three columns.
270+
data_range : list[float]
271+
simulation_range : list[float]
178272
179-
name: str = Field(default_factory=lambda: "New Data " + next(data_number), min_length=1)
273+
"""
274+
275+
name: str = Field(default_factory=lambda: f"New Data {next(data_number)}", min_length=1)
180276
data: np.ndarray[np.float64] = np.empty([0, 3])
181277
data_range: list[float] = Field(default=[], min_length=2, max_length=2)
182278
simulation_range: list[float] = Field(default=[], min_length=2, max_length=2)
@@ -266,9 +362,21 @@ def __str__(self):
266362

267363

268364
class DomainContrast(RATModel):
269-
"""Groups together the layers required for each domain."""
365+
"""A group of layers required for a domain.
366+
367+
Parameters
368+
----------
369+
name : str
370+
The name of this domain contrast.
371+
model : list[str]
372+
If this is a standard layers model, this should be a list of layer names
373+
that make up the slab model for this contrast.
374+
For custom models, this should be a list of custom file names of the custom
375+
model functions.
270376
271-
name: str = Field(default_factory=lambda: "New Domain Contrast " + next(domain_contrast_number), min_length=1)
377+
"""
378+
379+
name: str = Field(default_factory=lambda: f"New Domain Contrast {next(domain_contrast_number)}", min_length=1)
272380
model: list[str] = []
273381

274382
def __str__(self):
@@ -280,9 +388,25 @@ def __str__(self):
280388

281389

282390
class Layer(RATModel, populate_by_name=True):
283-
"""Combines parameters into defined layers."""
284-
285-
name: str = Field(default_factory=lambda: "New Layer " + next(layer_number), min_length=1)
391+
"""A slab model layer with given physical properties.
392+
393+
Parameters
394+
----------
395+
name : str
396+
The name of this layer.
397+
thickness : str
398+
The name of the parameter describing the thickness of this layer.
399+
SLD : str
400+
The name of the parameter describing the scattering length density
401+
of this layer.
402+
roughness : str
403+
The name of the parameter describing the roughness of this layer.
404+
hydration : str
405+
hydrate_with : str
406+
407+
"""
408+
409+
name: str = Field(default_factory=lambda: f"New Layer {next(layer_number)}", min_length=1)
286410
thickness: str
287411
SLD: str = Field(validation_alias="SLD_real")
288412
roughness: str
@@ -301,9 +425,28 @@ def sld_imaginary_error(cls, data: Any):
301425

302426

303427
class AbsorptionLayer(RATModel, populate_by_name=True):
304-
"""Combines parameters into defined layers including absorption terms."""
305-
306-
name: str = Field(default_factory=lambda: "New Layer " + next(layer_number), min_length=1)
428+
"""A slab model layer with a non-negligible absorption term.
429+
430+
Parameters
431+
----------
432+
name : str
433+
The name of this layer.
434+
thickness : str
435+
The name of the parameter describing the thickness of this layer.
436+
SLD_real : str
437+
The name of the parameter describing the real (scattering) term
438+
for the scattering length density of this layer.
439+
SLD_imaginary : str
440+
The name of the parameter describing the imaginary (absorption) term
441+
for the scattering length density of this layer.
442+
roughness : str
443+
The name of the parameter describing the roughness of this layer.
444+
hydration : str
445+
hydrate_with : str
446+
447+
"""
448+
449+
name: str = Field(default_factory=lambda: f"New Layer {next(layer_number)}", min_length=1)
307450
thickness: str
308451
SLD_real: str = Field(validation_alias="SLD")
309452
SLD_imaginary: str = ""
@@ -313,9 +456,30 @@ class AbsorptionLayer(RATModel, populate_by_name=True):
313456

314457

315458
class Parameter(RATModel):
316-
"""Defines parameters needed to specify the model."""
317-
318-
name: str = Field(default_factory=lambda: "New Parameter " + next(parameter_number), min_length=1)
459+
"""A parameter needed to specify the model.
460+
461+
Parameters
462+
----------
463+
name : str
464+
The name of this parameter.
465+
min : float
466+
The minimum value that this parameter could take when fitted.
467+
value : float
468+
The value of this parameter.
469+
max : float
470+
The maximum value that this parameter could take when fitted.
471+
fit : bool
472+
Whether this parameter should be fitted in a calculation.
473+
prior_type : Priors
474+
For Bayesian calculations, whether the prior likelihood
475+
is assumed to be 'uniform' or 'gaussian'.
476+
mu, sigma : float
477+
If the prior type is Gaussian, the mu and sigma values describing
478+
the Gaussian function for the prior likelihood.
479+
480+
"""
481+
482+
name: str = Field(default_factory=lambda: f"New Parameter {next(parameter_number)}", min_length=1)
319483
min: float = 0.0
320484
value: float = 0.0
321485
max: float = 0.0
@@ -346,10 +510,30 @@ class ProtectedParameter(Parameter):
346510

347511

348512
class Resolution(RATModel):
349-
"""Defines Resolutions in RAT."""
350-
351-
name: str = Field(default_factory=lambda: "New Resolution " + next(resolution_number), min_length=1)
513+
"""An instrument resolution.
514+
515+
Parameters
516+
----------
517+
name : str
518+
The name of the background.
519+
type : TypeOptions
520+
The type of background (constant, function or data)
521+
source : str
522+
The source data for the background;
523+
- if type is 'constant', this should be the name of a background parameter.
524+
- if type is 'data', this should be the name of a dataset defined in `Project.data`.
525+
- if type is 'function', this should be the name of a custom function defined in `Project.custom_files`.
526+
value_1, value_2, ..., value_5 : str
527+
Values required by the background.
528+
- if type is 'constant', this should be blank.
529+
- if type is 'data', value_1 may be the parameter name for an optional offset. Other values are ignored.
530+
- if type is 'function', these values may be the names of up to 5 parameters which are passed to the function.
531+
532+
"""
533+
534+
name: str = Field(default_factory=lambda: f"New Resolution {next(resolution_number)}", min_length=1)
352535
type: TypeOptions = TypeOptions.Constant
536+
source: str = ""
353537
value_1: str = ""
354538
value_2: str = ""
355539
value_3: str = ""

0 commit comments

Comments
 (0)