Skip to content

Commit cda4220

Browse files
committed
docstrings for outputs and fixed plot bayes docstring
1 parent 6f47925 commit cda4220

File tree

2 files changed

+234
-1
lines changed

2 files changed

+234
-1
lines changed

RATapi/outputs.py

Lines changed: 233 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,38 @@ 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+
"""
6575
chiValues: np.ndarray
6676
sumChi: float
6777

6878

6979
@dataclass
7080
class ContrastParams(RATResult):
81+
"""The experimental parameters for each contrast.
82+
83+
Attributes
84+
----------
85+
scalefactors : np.ndarray
86+
An array containing the scalefactor values for each contrast.
87+
bulkIn : np.ndarray
88+
An array containing the bulk in values for each contrast.
89+
bulkOut : np.ndarray
90+
An array containing the bulk out values for each contrast.
91+
subRoughs : np.ndarray
92+
An array containing the substrate roughness values for each contrast.
93+
resample : np.ndarray
94+
An array containing whether each contrast was resampled.
95+
96+
"""
7197
scalefactors: np.ndarray
7298
bulkIn: np.ndarray
7399
bulkOut: np.ndarray
@@ -77,6 +103,42 @@ class ContrastParams(RATResult):
77103

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

100162
@dataclass
101163
class PredictionIntervals(RATResult):
164+
"""The Bayesian prediction intervals for 95% and 65% confidence.
165+
166+
For ``reflectivity`` and ``sld``, each list item is an array
167+
with five rows. The 0th and 4th index are the minimum and maximum
168+
range with 95% confidence, the 1st and 3rd are the minimum and maximum
169+
with 65% confidence, and the 2nd is the mean value of the interval.
170+
Column ``i`` is this data for the i'th point of the reflectivity or
171+
SLD result.
172+
173+
Attributes
174+
----------
175+
reflectivity : list
176+
The prediction interval data for reflectivity of each contrast.
177+
SLD : list
178+
The prediction interval data for SLD of each contrast.
179+
sampleChi : np.ndarray
180+
181+
"""
102182
reflectivity: list
103183
sld: list
104184
sampleChi: np.ndarray
105185

106186

107187
@dataclass
108188
class ConfidenceIntervals(RATResult):
189+
"""
190+
The 65% and 95% confidence intervals for the best fit results.
191+
192+
Attributes
193+
----------
194+
percentile95 : np.ndarray
195+
The 95% confidence intervals.
196+
percentile65 : np.ndarray
197+
The 65% confidence intervals
198+
mean : np.ndarray
199+
200+
"""
109201
percentile95: np.ndarray
110202
percentile65: np.ndarray
111203
mean: np.ndarray
112204

113205

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

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

149326
@dataclass
150327
class NestedSamplerOutput(RATResult):
328+
"""The output information from the Nested Sampler (ns).
329+
330+
Attributes
331+
----------
332+
logZ : float
333+
The natural logarithm of the evidence Z for the parameter values.
334+
logZErr : float
335+
The estimated uncertainty in the final value of logZ.
336+
nestSamples : np.ndarray
337+
``NestedSamplerOutput.nestSamples[i, j]`` represents the values
338+
sampled at iteration ``i``, where this value is:
339+
340+
- the value sampled for parameter ``j``, for ``j`` in ``0:nParams``,
341+
- the minimum log-likelihood for ``j = nParams + 1``.
342+
343+
postSamples : np.ndarray
344+
The posterior values at the points sampled in ``NestedSamplerOutput.nestSamples``.
345+
346+
"""
151347
logZ: float
152348
logZErr: float
153349
nestSamples: np.ndarray
@@ -156,6 +352,25 @@ class NestedSamplerOutput(RATResult):
156352

157353
@dataclass
158354
class BayesResults(Results):
355+
"""The results of a Bayesian RAT calculation.
356+
357+
Attributes
358+
----------
359+
predictionIntervals : PredictionIntervals
360+
The prediction intervals.
361+
confidenceIntervals : ConfidenceIntervals
362+
The 65% and 95% confidence intervals for the best fit results.
363+
dreamParams : DreamParams
364+
The parameters used by DREAM, if relevant.
365+
dreamOutput : DreamOutput
366+
The output from DREAM if DREAM was used.
367+
nestedSamplerOutput : NestedSamplerOutput
368+
The output from nested sampling if ns was used.
369+
chain : np.ndarray
370+
The MCMC chains for each parameter.
371+
The ``i``'th column of the array contains the chain for parameter ``fitNames[i]``.
372+
373+
"""
159374
predictionIntervals: PredictionIntervals
160375
confidenceIntervals: ConfidenceIntervals
161376
dreamParams: DreamParams
@@ -169,7 +384,24 @@ def make_results(
169384
output_results: RATapi.rat_core.OutputResult,
170385
bayes_results: Optional[RATapi.rat_core.BayesResults] = None,
171386
) -> Union[Results, BayesResults]:
172-
"""Initialise a python Results or BayesResults object using the outputs from a RAT calculation."""
387+
"""Initialise a python Results or BayesResults object using the outputs from a RAT calculation.
388+
389+
Parameters
390+
----------
391+
procedure : Procedures
392+
The procedure used by the calculation.
393+
output_results : RATapi.rat_core.OutputResult
394+
The C++ output results from the calculation.
395+
bayes_results : Optional[RATapi.rat_core.BayesResults]
396+
The optional extra C++ Bayesian output results from a Bayesian calculation.
397+
398+
Returns
399+
-------
400+
Results or BayesResults
401+
A result object containing the results of the calculation, of type
402+
Results for non-Bayesian procedures and BayesResults for Bayesian procedures.
403+
404+
"""
173405
calculation_results = CalculationResults(
174406
chiValues=output_results.calculationResults.chiValues,
175407
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)