Skip to content

Commit 58d1141

Browse files
authored
Adds docstrings to the Results struct (#132)
* docstrings for outputs and fixed plot bayes docstring * review fixes
1 parent 6f47925 commit 58d1141

File tree

2 files changed

+244
-1
lines changed

2 files changed

+244
-1
lines changed

RATapi/outputs.py

Lines changed: 243 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,40 @@ def __str__(self):
6262

6363
@dataclass
6464
class CalculationResults(RATResult):
65+
"""The goodness of fit from the Abeles calculation.
66+
67+
Attributes
68+
----------
69+
chiValues : np.ndarray
70+
The chi-squared value for each contrast.
71+
sumChi : float
72+
The sum of the chiValues array.
73+
74+
"""
75+
6576
chiValues: np.ndarray
6677
sumChi: float
6778

6879

6980
@dataclass
7081
class ContrastParams(RATResult):
82+
"""The experimental parameters for each contrast.
83+
84+
Attributes
85+
----------
86+
scalefactors : np.ndarray
87+
The scalefactor values for each contrast.
88+
bulkIn : np.ndarray
89+
The bulk in values for each contrast.
90+
bulkOut : np.ndarray
91+
The bulk out values for each contrast.
92+
subRoughs : np.ndarray
93+
The substrate roughness values for each contrast.
94+
resample : np.ndarray
95+
An array containing whether each contrast was resampled.
96+
97+
"""
98+
7199
scalefactors: np.ndarray
72100
bulkIn: np.ndarray
73101
bulkOut: np.ndarray
@@ -77,6 +105,42 @@ class ContrastParams(RATResult):
77105

78106
@dataclass
79107
class Results:
108+
"""The results of a RAT calculation.
109+
110+
Attributes
111+
----------
112+
reflectivity : list
113+
The reflectivity curves for each contrast,
114+
with the same range as the data
115+
(``data_range`` in the contrast's ``Data`` object)
116+
simulation : list
117+
The reflectivity curves for each contrast,
118+
which can be a wider range to allow extrapolation
119+
(``simulation_range`` in the contrast's ``Data`` object).
120+
shiftedData : list
121+
The data with scalefactors and background corrections applied.
122+
backgrounds : list
123+
The background for each contrast defined over the simulation range.
124+
resolutions : list
125+
The resolution for each contrast defined over the simulation range.
126+
layerSlds : list
127+
The array of layer parameter values for each contrast.
128+
sldProfiles : list
129+
The SLD profiles for each contrast.
130+
resampledLayers : list
131+
If resampling is used, the SLD for each contrast after resampling has been performed.
132+
calculationResults : CalculationResults
133+
The chi-squared fit results from the final calculation and fit.
134+
contrastParams : ContrastParams
135+
The experimental parameters for the contrasts.
136+
fitParams : np.ndarray
137+
The best fit value of the parameter with name ``fitNames[i]``.
138+
fitNames : list[str]
139+
The names of the fit parameters, where ``fitNames[i]`` is the name
140+
of the parameter with value given in ``fitParams[i]``.
141+
142+
"""
143+
80144
reflectivity: list
81145
simulation: list
82146
shiftedData: list
@@ -99,20 +163,102 @@ def __str__(self):
99163

100164
@dataclass
101165
class PredictionIntervals(RATResult):
166+
"""The Bayesian prediction intervals for 95% and 65% confidence.
167+
168+
For ``reflectivity`` and ``sld``, each list item is an array
169+
with five rows. The rows represent:
170+
171+
- 0: the 5th percentile;
172+
- 1: the 35th percentile;
173+
- 2: the mean value of the interval;
174+
- 3: the 65th percentile;
175+
- 4: the 95th percentile.
176+
177+
Attributes
178+
----------
179+
reflectivity : list
180+
The prediction interval data for reflectivity of each contrast.
181+
SLD : list
182+
The prediction interval data for SLD of each contrast.
183+
sampleChi : np.ndarray
184+
The value of sumChi at each point of the Markov chain.
185+
"""
186+
102187
reflectivity: list
103188
sld: list
104189
sampleChi: np.ndarray
105190

106191

107192
@dataclass
108193
class ConfidenceIntervals(RATResult):
194+
"""
195+
The 65% and 95% confidence intervals for the best fit results.
196+
197+
Attributes
198+
----------
199+
percentile95 : np.ndarray
200+
The 95% confidence intervals for each fit parameter.
201+
percentile65 : np.ndarray
202+
The 65% confidence intervals for each fit parameter.
203+
mean : np.ndarray
204+
The mean values for each fit parameter.
205+
"""
206+
109207
percentile95: np.ndarray
110208
percentile65: np.ndarray
111209
mean: np.ndarray
112210

113211

114212
@dataclass
115213
class DreamParams(RATResult):
214+
"""The parameters used by the inner DREAM algorithm.
215+
216+
Attributes
217+
----------
218+
nParams : float
219+
The number of parameters used by the algorithm.
220+
nChains : float
221+
The number of MCMC chains used by the algorithm.
222+
nGenerations : float
223+
The number of DE generations calculated per iteration.
224+
parallel : bool
225+
Whether the algorithm should run chains in parallel.
226+
CPU : float
227+
The number of processor cores used for parallel chains.
228+
jumpProbability : float
229+
A probability range for the size of jumps when performing subspace sampling.
230+
pUnitGamma : float
231+
The probability that the scaling-down factor of jumps will be ignored
232+
and a larger jump will be taken for one iteration.
233+
nCR : float
234+
The number of crossovers performed each iteration.
235+
delta : float
236+
The number of chain mutation pairs proposed each iteration.
237+
steps : float
238+
The number of MCMC steps to perform between conversion checks.
239+
zeta : float
240+
The ergodicity of the algorithm.
241+
outlier : str
242+
What test should be used to detect outliers.
243+
adaptPCR : bool
244+
Whether the crossover probability for differential evolution should be
245+
adapted by the algorithm as it runs.
246+
thinning : float
247+
The thinning rate of each Markov chain (to reduce memory intensity)
248+
epsilon : float
249+
The cutoff threshold for Approximate Bayesian Computation (if used)
250+
ABC : bool
251+
Whether Approximate Bayesian Computation is used.
252+
IO : bool
253+
Whether the algorithm should perform IO writes of the model in parallel.
254+
storeOutput : bool
255+
Whether output model simulations are performed.
256+
R : np.ndarray
257+
An array where row ``i`` is the list of chains
258+
with which chain ``i`` can mutate.
259+
260+
"""
261+
116262
nParams: float
117263
nChains: float
118264
nGenerations: float
@@ -136,6 +282,45 @@ class DreamParams(RATResult):
136282

137283
@dataclass
138284
class DreamOutput(RATResult):
285+
"""The diagnostic output information from DREAM.
286+
287+
Attributes
288+
----------
289+
allChains : np.ndarray
290+
An ``nGenerations`` x ``nParams + 2`` x ``nChains`` size array,
291+
where ``chain_k = DreamOutput.allChains[:, :, k]``
292+
is the data of chain ``k`` in the final iteration;
293+
for generation i of the final iteration, ``chain_k[i, j]`` represents:
294+
295+
- the sampled value of parameter ``j`` for ``j in 0:nParams``;
296+
- the associated log-prior for those sampled values for ``j = nParams + 1``;
297+
- the associated log-likelihood for those sampled values for ``j = nParams + 2``.
298+
299+
outlierChains : np.ndarray
300+
A two-column array where ``DreamOutput.AR[i, 1]`` is the index of a chain
301+
and ``DreamOutput.AR[i, 0]`` is the length of that chain when it was removed
302+
for being an outlier.
303+
runtime : float
304+
The runtime of the DREAM algorithm in seconds.
305+
iteration : float
306+
The number of iterations performed.
307+
modelOutput : float
308+
Unused. Will always be 0.
309+
AR : np.ndarray
310+
A two-column array where ``DreamOutput.AR[i, 0]`` is an iteration number
311+
and ``DreamOutput.AR[i, 1]`` is the average acceptance rate of chain step
312+
proposals for that iteration.
313+
R_stat : np.ndarray
314+
An array where ``DreamOutput.R_stat[i, 0]`` is an iteration number and
315+
``DreamOutput.R_stat[i, j]`` is the convergence statistic for parameter ``j``
316+
at that iteration (where chains are indexed 1 to ``nParams`` inclusive).
317+
CR : np.ndarray
318+
A four-column array where ``DreamOutput.CR[i, 0]`` is an iteration number,
319+
``and DreamOutput.CR[i, j]`` is the selection probability of the ``j``'th crossover
320+
value for that iteration.
321+
322+
"""
323+
139324
allChains: np.ndarray
140325
outlierChains: np.ndarray
141326
runtime: float
@@ -148,6 +333,26 @@ class DreamOutput(RATResult):
148333

149334
@dataclass
150335
class NestedSamplerOutput(RATResult):
336+
"""The output information from the Nested Sampler (ns).
337+
338+
Attributes
339+
----------
340+
logZ : float
341+
The natural logarithm of the evidence Z for the parameter values.
342+
logZErr : float
343+
The estimated uncertainty in the final value of logZ.
344+
nestSamples : np.ndarray
345+
``NestedSamplerOutput.nestSamples[i, j]`` represents the values
346+
sampled at iteration ``i``, where this value is:
347+
348+
- the value sampled for parameter ``j``, for ``j`` in ``0:nParams``,
349+
- the minimum log-likelihood for ``j = nParams + 1``.
350+
351+
postSamples : np.ndarray
352+
The posterior values at the points sampled in ``NestedSamplerOutput.nestSamples``.
353+
354+
"""
355+
151356
logZ: float
152357
logZErr: float
153358
nestSamples: np.ndarray
@@ -156,6 +361,26 @@ class NestedSamplerOutput(RATResult):
156361

157362
@dataclass
158363
class BayesResults(Results):
364+
"""The results of a Bayesian RAT calculation.
365+
366+
Attributes
367+
----------
368+
predictionIntervals : PredictionIntervals
369+
The prediction intervals.
370+
confidenceIntervals : ConfidenceIntervals
371+
The 65% and 95% confidence intervals for the best fit results.
372+
dreamParams : DreamParams
373+
The parameters used by DREAM, if relevant.
374+
dreamOutput : DreamOutput
375+
The output from DREAM if DREAM was used.
376+
nestedSamplerOutput : NestedSamplerOutput
377+
The output from nested sampling if ns was used.
378+
chain : np.ndarray
379+
The MCMC chains for each parameter.
380+
The ``i``'th column of the array contains the chain for parameter ``fitNames[i]``.
381+
382+
"""
383+
159384
predictionIntervals: PredictionIntervals
160385
confidenceIntervals: ConfidenceIntervals
161386
dreamParams: DreamParams
@@ -169,7 +394,24 @@ def make_results(
169394
output_results: RATapi.rat_core.OutputResult,
170395
bayes_results: Optional[RATapi.rat_core.BayesResults] = None,
171396
) -> Union[Results, BayesResults]:
172-
"""Initialise a python Results or BayesResults object using the outputs from a RAT calculation."""
397+
"""Initialise a python Results or BayesResults object using the outputs from a RAT calculation.
398+
399+
Parameters
400+
----------
401+
procedure : Procedures
402+
The procedure used by the calculation.
403+
output_results : RATapi.rat_core.OutputResult
404+
The C++ output results from the calculation.
405+
bayes_results : Optional[RATapi.rat_core.BayesResults]
406+
The optional extra C++ Bayesian output results from a Bayesian calculation.
407+
408+
Returns
409+
-------
410+
Results or BayesResults
411+
A result object containing the results of the calculation, of type
412+
Results for non-Bayesian procedures and BayesResults for Bayesian procedures.
413+
414+
"""
173415
calculation_results = CalculationResults(
174416
chiValues=output_results.calculationResults.chiValues,
175417
sumChi=output_results.calculationResults.sumChi,

RATapi/utils/plotting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ def plot_bayes(project: RATapi.Project, results: RATapi.outputs.BayesResults):
865865
all parameters.
866866
867867
Parameters
868+
----------
868869
project : Project
869870
An instance of the Project class
870871
results : Union[Results, BayesResults]

0 commit comments

Comments
 (0)