Skip to content

Commit 265668a

Browse files
authored
Adds docstrings to controls and project fields (#117)
* added docstrings to controls and project fields * added newlines * review fixes * fixed docstrings for doc build
1 parent 47093d4 commit 265668a

File tree

4 files changed

+98
-7
lines changed

4 files changed

+98
-7
lines changed

RATapi/controls.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,99 @@
3636
}
3737

3838

39-
class Controls(BaseModel, validate_assignment=True, extra="forbid"):
39+
class Controls(BaseModel, validate_assignment=True, extra="forbid", use_attribute_docstrings=True):
4040
"""The full set of controls parameters for all five procedures that are required for the compiled RAT code."""
4141

4242
# All Procedures
4343
procedure: Procedures = Procedures.Calculate
44+
"""Which procedure RAT should execute. Can be 'calculate', 'simplex', 'de', 'ns', or 'dream'."""
45+
4446
parallel: Parallel = Parallel.Single
47+
"""How the calculation should be parallelised. Can be 'single', 'contrasts' or 'points'."""
48+
4549
calcSldDuringFit: bool = False
50+
"""Whether SLD will be calculated during fit (for live plotting etc.)"""
51+
4652
resampleMinAngle: float = Field(0.9, le=1, gt=0)
53+
"""The upper threshold on the angle between three sampled points for resampling, in units of radians over pi."""
54+
4755
resampleNPoints: int = Field(50, gt=0)
56+
"""The number of initial points to use for resampling."""
57+
4858
display: Display = Display.Iter
59+
"""How much RAT should print to the terminal. Can be 'off', 'iter', 'notify', or 'final'."""
60+
4961
# Simplex
5062
xTolerance: float = Field(1.0e-6, gt=0.0)
63+
"""[SIMPLEX] The termination tolerance for step size."""
64+
5165
funcTolerance: float = Field(1.0e-6, gt=0.0)
66+
"""[SIMPLEX] The termination tolerance for change in chi-squared."""
67+
5268
maxFuncEvals: int = Field(10000, gt=0)
69+
"""[SIMPLEX] The maximum number of function evaluations before the algorithm terminates."""
70+
5371
maxIterations: int = Field(1000, gt=0)
72+
"""[SIMPLEX] The maximum number of iterations before the algorithm terminates."""
73+
5474
# Simplex and DE
5575
updateFreq: int = 1
76+
"""[SIMPLEX, DE] Number of iterations between printing progress updates to the terminal."""
77+
5678
updatePlotFreq: int = 20
79+
"""[SIMPLEX, DE] Number of iterations between updates to live plots."""
80+
5781
# DE
5882
populationSize: int = Field(20, ge=1)
83+
"""[DE] The number of candidate solutions that exist at any time."""
84+
5985
fWeight: float = Field(0.5, gt=0.0)
86+
"""[DE] The step size for how different mutations are to their parents."""
87+
6088
crossoverProbability: float = Field(0.8, gt=0.0, lt=1.0)
89+
"""[DE] The probability of exchange of parameters between individuals at any iteration."""
90+
6191
strategy: Strategies = Strategies.RandomWithPerVectorDither
92+
"""[DE] The algorithm used to generate new candidates."""
93+
6294
targetValue: float = Field(1.0, ge=1.0)
95+
"""[DE] The value of chi-squared at which the algorithm will terminate."""
96+
6397
numGenerations: int = Field(500, ge=1)
98+
"""[DE] The maximum number of iterations before the algorithm terminates."""
99+
64100
# NS
65101
nLive: int = Field(150, ge=1)
102+
"""[NS] The number of points to sample."""
103+
66104
nMCMC: int = Field(0, ge=0)
105+
"""[NS] If non-zero, an MCMC process with ``nMCMC`` chains will be used instead of MultiNest."""
106+
67107
propScale: float = Field(0.1, gt=0.0, lt=1.0)
108+
"""[NS] A scaling factor for the ellipsoid generated by MultiNest."""
109+
68110
nsTolerance: float = Field(0.1, ge=0.0)
111+
"""[NS] The tolerance threshold for when the algorithm should terminate."""
112+
69113
# Dream
70114
nSamples: int = Field(20000, ge=0)
115+
"""[DREAM] The number of samples in the initial population for each chain."""
116+
71117
nChains: int = Field(10, gt=0)
118+
"""[DREAM] The number of Markov chains to use in the algorithm."""
119+
72120
jumpProbability: float = Field(0.5, gt=0.0, lt=1.0)
121+
"""[DREAM] The probability range for the size of jumps in sampling. Larger values mean more variable jumps."""
122+
73123
pUnitGamma: float = Field(0.2, gt=0.0, lt=1.0)
124+
"""[DREAM] The probability that the scaling-down factor of jumps will be ignored and a larger jump will be taken."""
125+
74126
boundHandling: BoundHandling = BoundHandling.Reflect
127+
"""[DREAM] How steps past the space boundaries should be handled. Can be 'off', 'reflect', 'bound', or 'fold'."""
128+
75129
adaptPCR: bool = True
130+
"""[DREAM] Whether the crossover probability for differential evolution should be adapted during the run."""
131+
76132
# Private field for IPC file
77133
_IPCFilePath: str = ""
78134

RATapi/examples/non_polarised/DSPC_custom_XY.py

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

77

88
def DSPC_custom_XY():
9-
"""Custom XY Example for Supported DSPC layer.
9+
r"""Custom XY Example for Supported DSPC layer.
1010
1111
In this example, we model the same data (DSPC supported bilayer) as the Custom Layers example, but this time we will
1212
use continuous distributions of the volume fractions of each component to build up the SLD profiles (as described in
@@ -19,15 +19,15 @@ def DSPC_custom_XY():
1919
We can define our lipid in terms of an Area per Molecule, almost in its entirety, if we recognise that where the
2020
volume is known, the thickness of the layer is simply given by the layer volume / APM:
2121
22-
$d_{\textrm{layer}} =\frac{V_{\textrm{layer}} }{{\textrm{APM}}_{\textrm{layer}}}$.
22+
.. math:: d_{\textrm{layer}} =\frac{V_{\textrm{layer}} }{{\textrm{APM}}_{\textrm{layer}}}.
2323
2424
We can then define the Volume Fraction of this layer with a roughened Heaviside of length dlayer and a height of 1.
2525
Then, the total volume occupied will be given by the sum of the volume fractions across the interface. Of course,
2626
this does not permit any hydration, so to deal with this, we can simply scale the (full occupation) Heaviside
2727
functions by relevant coverage parameters. When this is correctly done, we can obtain the remaining water
2828
distribution as:
2929
30-
$${\textrm{VF}}_{\textrm{wat}} =1-\\sum_n {\textrm{VF}}_n$$
30+
.. math:: {\textrm{VF}}_{\textrm{wat}} =1-\\sum_n {\textrm{VF}}_n
3131
3232
where VFn is the Volume Fraction of the n'th layer.
3333
"""

RATapi/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ class Background(Signal):
9494
The type of background (constant, function or data)
9595
source : str
9696
The source of the background;
97+
9798
- if type is 'constant', this should be the name of a background parameter.
9899
- if type is 'data', this should be the name of a dataset defined in `Project.data`.
99100
- if type is 'function', this should be the name of a custom function defined in `Project.custom_files`.
100-
value_1, value_2, ..., value_5 : str
101+
102+
value_1, value_2, value_3, value_4, value_5 : str
101103
Values required by the background.
104+
102105
- if type is 'constant', all values will be ignored.
103106
- if type is 'data', value_1 may be the parameter name for an optional offset. Other values are ignored.
104107
- if type is 'function', these values may be the names of up to 5 parameters which are passed to the function.
@@ -318,6 +321,8 @@ def set_matlab_function_name(self):
318321
class Data(RATModel, arbitrary_types_allowed=True):
319322
"""A dataset required for a contrast.
320323
324+
Parameters
325+
----------
321326
name : str
322327
The name of this dataset.
323328
data : np.ndarray[np.float64]
@@ -584,12 +589,15 @@ class Resolution(Signal):
584589
The type of resolution: 'constant', 'data', or (NOT YET IMPLEMENTED) 'function'.
585590
source : str
586591
The source data for the resolution;
592+
587593
- if type is 'constant', this should be the name of a background parameter.
588594
- if type is 'data', this should be empty (resolution data is in the contrast data).
589595
- if type is 'function' (NOT YET IMPLEMENTED),
590596
this should be the name of a custom function defined in `Project.custom_files`.
591-
value_1, value_2, ..., value_5 : str
597+
598+
value_1, value_2, value_3, value_4, value_5 : str
592599
Values required by the background.
600+
593601
- if type is 'constant' or 'data', all values will be ignored.
594602
- if type is 'function' (NOT YET IMPLEMENTED),
595603
these values may be the names of up to 5 parameters which are passed to the function.

RATapi/project.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,30 @@ def discriminate_contrasts(contrast_input):
123123
]
124124

125125

126-
class Project(BaseModel, validate_assignment=True, extra="forbid"):
126+
class Project(BaseModel, validate_assignment=True, extra="forbid", use_attribute_docstrings=True):
127127
"""Defines the input data for a reflectivity calculation in RAT.
128128
129129
This class combines the data defined in each of the pydantic models included in "models.py" into the full set of
130130
inputs required for a reflectivity calculation.
131131
"""
132132

133133
name: str = ""
134+
"""The name of the project."""
135+
134136
calculation: Calculations = Calculations.Normal
137+
"""What calculation type should be used. Can be 'normal' or 'domains'."""
138+
135139
model: LayerModels = LayerModels.StandardLayers
140+
"""What layer model should be used. Can be 'standard layers', 'custom layers', or 'custom xy'."""
141+
136142
geometry: Geometries = Geometries.AirSubstrate
143+
"""What geometry should be used. Can be 'air/substrate' or 'substrate/liquid'"""
144+
137145
absorption: bool = False
146+
"""Whether imaginary SLD (absorption) should be accounted for."""
138147

139148
parameters: ClassList[RATapi.models.Parameter] = ClassList()
149+
"""The list of parameters used in the layers of a model."""
140150

141151
bulk_in: ClassList[RATapi.models.Parameter] = ClassList(
142152
RATapi.models.Parameter(
@@ -150,6 +160,7 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
150160
sigma=np.inf,
151161
),
152162
)
163+
"""The list of parameters for SLD of the entry interfaces of a model."""
153164

154165
bulk_out: ClassList[RATapi.models.Parameter] = ClassList(
155166
RATapi.models.Parameter(
@@ -163,6 +174,7 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
163174
sigma=np.inf,
164175
),
165176
)
177+
"""The list of parameters for SLD of the exit interfaces of a model."""
166178

167179
scalefactors: ClassList[RATapi.models.Parameter] = ClassList(
168180
RATapi.models.Parameter(
@@ -176,6 +188,7 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
176188
sigma=np.inf,
177189
),
178190
)
191+
"""The list of parameters for scale factors to handle systematic error in model data."""
179192

180193
domain_ratios: ClassList[RATapi.models.Parameter] = ClassList(
181194
RATapi.models.Parameter(
@@ -189,6 +202,7 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
189202
sigma=np.inf,
190203
),
191204
)
205+
"""The list of parameters for weighting between domains of a domains model."""
192206

193207
background_parameters: ClassList[RATapi.models.Parameter] = ClassList(
194208
RATapi.models.Parameter(
@@ -202,10 +216,12 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
202216
sigma=np.inf,
203217
),
204218
)
219+
"""The list of parameters for models of backgrounds."""
205220

206221
backgrounds: ClassList[RATapi.models.Background] = ClassList(
207222
RATapi.models.Background(name="Background 1", type=TypeOptions.Constant, source="Background Param 1"),
208223
)
224+
"""The list of models for background noise in the project."""
209225

210226
resolution_parameters: ClassList[RATapi.models.Parameter] = ClassList(
211227
RATapi.models.Parameter(
@@ -219,13 +235,19 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
219235
sigma=np.inf,
220236
),
221237
)
238+
"""The list of parameters for models of resolutions."""
222239

223240
resolutions: ClassList[RATapi.models.Resolution] = ClassList(
224241
RATapi.models.Resolution(name="Resolution 1", type=TypeOptions.Constant, source="Resolution Param 1"),
225242
)
243+
"""The list of models for instrument resolution in the project."""
226244

227245
custom_files: ClassList[RATapi.models.CustomFile] = ClassList()
246+
"""Handles for custom files used by the project."""
247+
228248
data: ClassList[RATapi.models.Data] = ClassList()
249+
"""Experimental data for a model."""
250+
229251
layers: Union[
230252
Annotated[ClassList[RATapi.models.Layer], Tag("no_abs")],
231253
Annotated[ClassList[RATapi.models.AbsorptionLayer], Tag("abs")],
@@ -238,7 +260,11 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
238260
custom_error_context={"discriminator": "absorption_or_no"},
239261
),
240262
)
263+
"""The layers of a standard layer model."""
264+
241265
domain_contrasts: ClassList[RATapi.models.DomainContrast] = ClassList()
266+
"""The groups of layers required by each domain in a domains model."""
267+
242268
contrasts: Union[
243269
Annotated[ClassList[RATapi.models.Contrast], Tag("no_ratio")],
244270
Annotated[ClassList[RATapi.models.ContrastWithRatio], Tag("ratio")],
@@ -251,6 +277,7 @@ class Project(BaseModel, validate_assignment=True, extra="forbid"):
251277
custom_error_context={"discriminator": "ratio_or_no_ratio"},
252278
),
253279
)
280+
"""All groups of components used to define each model in the project."""
254281

255282
_all_names: dict
256283
_contrast_model_field: str

0 commit comments

Comments
 (0)