|
| 1 | +"""Converts outputs from the compiled RAT code to python dataclasses""" |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +import numpy as np |
| 5 | +from typing import Optional, Union |
| 6 | +from RAT.utils.enums import Procedures |
| 7 | +import RAT.rat_core |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class CalculationResults: |
| 12 | + chiValues: np.ndarray |
| 13 | + sumChi: float |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class ContrastParams: |
| 18 | + backgroundParams: np.ndarray |
| 19 | + scalefactors: np.ndarray |
| 20 | + bulkIn: np.ndarray |
| 21 | + bulkOut: np.ndarray |
| 22 | + resolutionParams: np.ndarray |
| 23 | + subRoughs: np.ndarray |
| 24 | + resample: np.ndarray |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class Results: |
| 29 | + reflectivity: list |
| 30 | + simulation: list |
| 31 | + shiftedData: list |
| 32 | + layerSlds: list |
| 33 | + sldProfiles: list |
| 34 | + resampledLayers: list |
| 35 | + calculationResults: CalculationResults |
| 36 | + contrastParams: ContrastParams |
| 37 | + fitParams: np.ndarray |
| 38 | + fitNames: list[str] |
| 39 | + |
| 40 | + |
| 41 | +@dataclass |
| 42 | +class PredictionIntervals: |
| 43 | + reflectivity: list |
| 44 | + sld: list |
| 45 | + reflectivityXData: list |
| 46 | + sldXData: list |
| 47 | + sampleChi: np.ndarray |
| 48 | + |
| 49 | + |
| 50 | +@dataclass |
| 51 | +class ConfidenceIntervals: |
| 52 | + percentile95: np.ndarray |
| 53 | + percentile65: np.ndarray |
| 54 | + mean: np.ndarray |
| 55 | + |
| 56 | + |
| 57 | +@dataclass |
| 58 | +class DreamParams: |
| 59 | + nParams: float |
| 60 | + nChains: float |
| 61 | + nGenerations: float |
| 62 | + parallel: bool |
| 63 | + CPU: float |
| 64 | + jumpProbability: float |
| 65 | + pUnitGamma: float |
| 66 | + nCR: float |
| 67 | + delta: float |
| 68 | + steps: float |
| 69 | + zeta: float |
| 70 | + outlier: str |
| 71 | + adaptPCR: bool |
| 72 | + thinning: float |
| 73 | + epsilon: float |
| 74 | + ABC: bool |
| 75 | + IO: bool |
| 76 | + storeOutput: bool |
| 77 | + R: np.ndarray |
| 78 | + |
| 79 | + |
| 80 | +@dataclass |
| 81 | +class DreamOutput: |
| 82 | + allChains: np.ndarray |
| 83 | + outlierChains: np.ndarray |
| 84 | + runtime: float |
| 85 | + iteration: float |
| 86 | + modelOutput: float |
| 87 | + AR: np.ndarray |
| 88 | + R_stat: np.ndarray |
| 89 | + CR: np.ndarray |
| 90 | + |
| 91 | + |
| 92 | +@dataclass |
| 93 | +class NestedSamplerOutput: |
| 94 | + logZ: float |
| 95 | + nestSamples: np.ndarray |
| 96 | + postSamples: np.ndarray |
| 97 | + |
| 98 | + |
| 99 | +@dataclass |
| 100 | +class BayesResults(Results): |
| 101 | + predictionIntervals: PredictionIntervals |
| 102 | + confidenceIntervals: ConfidenceIntervals |
| 103 | + dreamParams: DreamParams |
| 104 | + dreamOutput: DreamOutput |
| 105 | + nestedSamplerOutput: NestedSamplerOutput |
| 106 | + chain: np.ndarray |
| 107 | + |
| 108 | + |
| 109 | +def make_results(procedure: Procedures, output_results: RAT.rat_core.OutputResult, |
| 110 | + bayes_results: Optional[RAT.rat_core.BayesResults] = None) -> Union[Results, BayesResults]: |
| 111 | + """Initialise a python Results or BayesResults object using the outputs from a RAT calculation.""" |
| 112 | + |
| 113 | + calculation_results = CalculationResults(chiValues=output_results.calculationResults.chiValues, |
| 114 | + sumChi=output_results.calculationResults.sumChi |
| 115 | + ) |
| 116 | + contrast_params = ContrastParams( |
| 117 | + backgroundParams=output_results.contrastParams.backgroundParams, |
| 118 | + scalefactors=output_results.contrastParams.scalefactors, |
| 119 | + bulkIn=output_results.contrastParams.bulkIn, |
| 120 | + bulkOut=output_results.contrastParams.bulkOut, |
| 121 | + resolutionParams=output_results.contrastParams.resolutionParams, |
| 122 | + subRoughs=output_results.contrastParams.subRoughs, |
| 123 | + resample=output_results.contrastParams.resample |
| 124 | + ) |
| 125 | + |
| 126 | + if procedure in [Procedures.NS, Procedures.Dream]: |
| 127 | + |
| 128 | + prediction_intervals = PredictionIntervals( |
| 129 | + reflectivity=bayes_results.predictionIntervals.reflectivity, |
| 130 | + sld=bayes_results.predictionIntervals.sld, |
| 131 | + reflectivityXData=bayes_results.predictionIntervals.reflectivityXData, |
| 132 | + sldXData=bayes_results.predictionIntervals.sldXData, |
| 133 | + sampleChi=bayes_results.predictionIntervals.sampleChi |
| 134 | + ) |
| 135 | + |
| 136 | + confidence_intervals = ConfidenceIntervals( |
| 137 | + percentile95=bayes_results.confidenceIntervals.percentile95, |
| 138 | + percentile65=bayes_results.confidenceIntervals.percentile65, |
| 139 | + mean=bayes_results.confidenceIntervals.mean |
| 140 | + ) |
| 141 | + |
| 142 | + dream_params = DreamParams( |
| 143 | + nParams=bayes_results.dreamParams.nParams, |
| 144 | + nChains=bayes_results.dreamParams.nChains, |
| 145 | + nGenerations=bayes_results.dreamParams.nGenerations, |
| 146 | + parallel=bool(bayes_results.dreamParams.parallel), |
| 147 | + CPU=bayes_results.dreamParams.CPU, |
| 148 | + jumpProbability=bayes_results.dreamParams.jumpProbability, |
| 149 | + pUnitGamma=bayes_results.dreamParams.pUnitGamma, |
| 150 | + nCR=bayes_results.dreamParams.nCR, |
| 151 | + delta=bayes_results.dreamParams.delta, |
| 152 | + steps=bayes_results.dreamParams.steps, |
| 153 | + zeta=bayes_results.dreamParams.zeta, |
| 154 | + outlier=bayes_results.dreamParams.outlier, |
| 155 | + adaptPCR=bool(bayes_results.dreamParams.adaptPCR), |
| 156 | + thinning=bayes_results.dreamParams.thinning, |
| 157 | + epsilon=bayes_results.dreamParams.epsilon, |
| 158 | + ABC=bool(bayes_results.dreamParams.ABC), |
| 159 | + IO=bool(bayes_results.dreamParams.IO), |
| 160 | + storeOutput=bool(bayes_results.dreamParams.storeOutput), |
| 161 | + R=bayes_results.dreamParams.R |
| 162 | + ) |
| 163 | + |
| 164 | + dream_output = DreamOutput( |
| 165 | + allChains=bayes_results.dreamOutput.allChains, |
| 166 | + outlierChains=bayes_results.dreamOutput.outlierChains, |
| 167 | + runtime=bayes_results.dreamOutput.runtime, |
| 168 | + iteration=bayes_results.dreamOutput.iteration, |
| 169 | + modelOutput=bayes_results.dreamOutput.modelOutput, |
| 170 | + AR=bayes_results.dreamOutput.AR, |
| 171 | + R_stat=bayes_results.dreamOutput.R_stat, |
| 172 | + CR=bayes_results.dreamOutput.CR |
| 173 | + ) |
| 174 | + |
| 175 | + nested_sampler_output = NestedSamplerOutput( |
| 176 | + logZ=bayes_results.nestedSamplerOutput.logZ, |
| 177 | + nestSamples=bayes_results.nestedSamplerOutput.nestSamples, |
| 178 | + postSamples=bayes_results.nestedSamplerOutput.postSamples |
| 179 | + ) |
| 180 | + |
| 181 | + results = BayesResults( |
| 182 | + reflectivity=output_results.reflectivity, |
| 183 | + simulation=output_results.simulation, |
| 184 | + shiftedData=output_results.shiftedData, |
| 185 | + layerSlds=output_results.layerSlds, |
| 186 | + sldProfiles=output_results.sldProfiles, |
| 187 | + resampledLayers=output_results.resampledLayers, |
| 188 | + calculationResults=calculation_results, |
| 189 | + contrastParams=contrast_params, |
| 190 | + fitParams=output_results.fitParams, |
| 191 | + fitNames=output_results.fitNames, |
| 192 | + predictionIntervals=prediction_intervals, |
| 193 | + confidenceIntervals=confidence_intervals, |
| 194 | + dreamParams=dream_params, |
| 195 | + dreamOutput=dream_output, |
| 196 | + nestedSamplerOutput=nested_sampler_output, |
| 197 | + chain=bayes_results.chain |
| 198 | + ) |
| 199 | + |
| 200 | + else: |
| 201 | + |
| 202 | + results = Results( |
| 203 | + reflectivity=output_results.reflectivity, |
| 204 | + simulation=output_results.simulation, |
| 205 | + shiftedData=output_results.shiftedData, |
| 206 | + layerSlds=output_results.layerSlds, |
| 207 | + sldProfiles=output_results.sldProfiles, |
| 208 | + resampledLayers=output_results.resampledLayers, |
| 209 | + calculationResults=calculation_results, |
| 210 | + contrastParams=contrast_params, |
| 211 | + fitParams=output_results.fitParams, |
| 212 | + fitNames=output_results.fitNames |
| 213 | + ) |
| 214 | + |
| 215 | + return results |
0 commit comments