RATapi 0.0.0.dev5__tar.gz → 0.0.0.dev6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/PKG-INFO +1 -1
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/__init__.py +14 -1
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/inputs.py +65 -17
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/models.py +17 -3
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/outputs.py +275 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/project.py +7 -6
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/utils/enums.py +0 -1
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/utils/plotting.py +356 -86
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi.egg-info/PKG-INFO +1 -1
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/setup.py +1 -1
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/MANIFEST.in +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/classlist.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/controls.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/events.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/absorption/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/absorption/absorption.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/absorption/volume_thiol_bilayer.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/convert_rascal_project/Model_IIb.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/convert_rascal_project/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/convert_rascal_project/convert_rascal.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/D2O_spin_down.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/D2O_spin_up.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/DSPC_D2O.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/DSPC_SMW.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/H2O_spin_down.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/H2O_spin_up.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/c_PLP0016596.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/c_PLP0016601.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/c_PLP0016607.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/data/d2o_background_data.dat +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/domains/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/domains/alloy_domains.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/domains/domains_XY_model.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/domains/domains_custom_XY.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/domains/domains_custom_layers.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/domains/domains_standard_layers.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/languages/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/languages/custom_bilayer.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/languages/run_custom_file_languages.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/languages/setup_problem.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/DSPC_custom_XY.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/DSPC_custom_layers.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/DSPC_data_background.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/DSPC_function_background.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/DSPC_standard_layers.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/background_function.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/custom_XY_DSPC.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/examples/normal_reflectivity/custom_bilayer_DSPC.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/run.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/utils/__init__.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/utils/convert.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/utils/custom_errors.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/utils/orso.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi/wrappers.py +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi.egg-info/SOURCES.txt +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi.egg-info/dependency_links.txt +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi.egg-info/not-zip-safe +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi.egg-info/requires.txt +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/RATapi.egg-info/top_level.txt +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/README.md +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/cpp/includes/defines.h +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/cpp/includes/functions.h +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/cpp/rat.cpp +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/pyproject.toml +0 -0
- {RATapi-0.0.0.dev5 → RATapi-0.0.0.dev6}/setup.cfg +0 -0
|
@@ -6,6 +6,7 @@ import RATapi.examples as examples
|
|
|
6
6
|
from RATapi import events, models
|
|
7
7
|
from RATapi.classlist import ClassList
|
|
8
8
|
from RATapi.controls import Controls
|
|
9
|
+
from RATapi.outputs import BayesResults, Results
|
|
9
10
|
from RATapi.project import Project
|
|
10
11
|
from RATapi.run import run
|
|
11
12
|
from RATapi.utils import convert, plotting
|
|
@@ -13,4 +14,16 @@ from RATapi.utils import convert, plotting
|
|
|
13
14
|
with suppress(ImportError): # orsopy is an optional dependency
|
|
14
15
|
from RATapi.utils import orso as orso
|
|
15
16
|
|
|
16
|
-
__all__ = [
|
|
17
|
+
__all__ = [
|
|
18
|
+
"examples",
|
|
19
|
+
"models",
|
|
20
|
+
"events",
|
|
21
|
+
"ClassList",
|
|
22
|
+
"Controls",
|
|
23
|
+
"BayesResults",
|
|
24
|
+
"Results",
|
|
25
|
+
"Project",
|
|
26
|
+
"run",
|
|
27
|
+
"plotting",
|
|
28
|
+
"convert",
|
|
29
|
+
]
|
|
@@ -151,9 +151,37 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
151
151
|
The problem input used in the compiled RAT code.
|
|
152
152
|
|
|
153
153
|
"""
|
|
154
|
-
hydrate_id = {"bulk in": 1, "bulk out": 2}
|
|
155
154
|
prior_id = {"uniform": 1, "gaussian": 2, "jeffreys": 3}
|
|
156
155
|
|
|
156
|
+
# Ensure all contrast fields are properly defined
|
|
157
|
+
for contrast in project.contrasts:
|
|
158
|
+
contrast_fields = ["data", "background", "bulk_in", "bulk_out", "scalefactor", "resolution"]
|
|
159
|
+
|
|
160
|
+
if project.calculation == Calculations.Domains:
|
|
161
|
+
contrast_fields.append("domain_ratio")
|
|
162
|
+
|
|
163
|
+
for field in contrast_fields:
|
|
164
|
+
if getattr(contrast, field) == "":
|
|
165
|
+
raise ValueError(
|
|
166
|
+
f'In the input project, the "{field}" field of contrast "{contrast.name}" does not have a '
|
|
167
|
+
f"value defined. A value must be supplied before running the project."
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
# Ensure backgrounds and resolutions have a source defined
|
|
171
|
+
background = project.backgrounds[contrast.background]
|
|
172
|
+
resolution = project.resolutions[contrast.resolution]
|
|
173
|
+
if background.source == "":
|
|
174
|
+
raise ValueError(
|
|
175
|
+
f"All backgrounds must have a source defined. For a {background.type} type background, "
|
|
176
|
+
f"the source must be defined in "
|
|
177
|
+
f'"{RATapi.project.values_defined_in[f"backgrounds.{background.type}.source"]}"'
|
|
178
|
+
)
|
|
179
|
+
if resolution.source == "" and resolution.type != TypeOptions.Data:
|
|
180
|
+
raise ValueError(
|
|
181
|
+
f"Constant resolutions must have a source defined. The source must be defined in "
|
|
182
|
+
f'"{RATapi.project.values_defined_in[f"resolutions.{resolution.type}.source"]}"'
|
|
183
|
+
)
|
|
184
|
+
|
|
157
185
|
# Set contrast parameters according to model type
|
|
158
186
|
if project.model == LayerModels.StandardLayers:
|
|
159
187
|
if project.calculation == Calculations.Domains:
|
|
@@ -175,16 +203,7 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
175
203
|
contrast_custom_files = [project.custom_files.index(contrast.model[0], True) for contrast in project.contrasts]
|
|
176
204
|
|
|
177
205
|
# Get details of defined layers
|
|
178
|
-
layer_details =
|
|
179
|
-
for layer in project.layers:
|
|
180
|
-
layer_params = [
|
|
181
|
-
project.parameters.index(getattr(layer, attribute), True)
|
|
182
|
-
for attribute in list(layer.model_fields.keys())[1:-2]
|
|
183
|
-
]
|
|
184
|
-
layer_params.append(project.parameters.index(layer.hydration, True) if layer.hydration else float("NaN"))
|
|
185
|
-
layer_params.append(hydrate_id[layer.hydrate_with])
|
|
186
|
-
|
|
187
|
-
layer_details.append(layer_params)
|
|
206
|
+
layer_details = get_layer_details(project)
|
|
188
207
|
|
|
189
208
|
contrast_background_params = []
|
|
190
209
|
contrast_background_types = []
|
|
@@ -194,9 +213,9 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
194
213
|
contrast_resolution_params = []
|
|
195
214
|
contrast_resolution_types = []
|
|
196
215
|
|
|
197
|
-
#
|
|
216
|
+
# Set data, background and resolution for each contrast
|
|
198
217
|
for contrast in project.contrasts:
|
|
199
|
-
#
|
|
218
|
+
# Set data
|
|
200
219
|
data_index = project.data.index(contrast.data)
|
|
201
220
|
data = project.data[data_index].data
|
|
202
221
|
data_range = project.data[data_index].data_range
|
|
@@ -212,7 +231,7 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
212
231
|
else:
|
|
213
232
|
simulation_limits.append([0.0, 0.0])
|
|
214
233
|
|
|
215
|
-
#
|
|
234
|
+
# Set background parameters
|
|
216
235
|
background = project.backgrounds[contrast.background]
|
|
217
236
|
contrast_background_types.append(background.type)
|
|
218
237
|
contrast_background_param = []
|
|
@@ -221,7 +240,7 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
221
240
|
contrast_background_param.append(project.data.index(background.source, True))
|
|
222
241
|
if background.value_1 != "":
|
|
223
242
|
contrast_background_param.append(project.background_parameters.index(background.value_1))
|
|
224
|
-
#
|
|
243
|
+
# If we are using a data background, we add the background data to the contrast data
|
|
225
244
|
data = append_data_background(data, project.data[background.source].data)
|
|
226
245
|
|
|
227
246
|
elif background.type == TypeOptions.Function:
|
|
@@ -245,7 +264,7 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
245
264
|
|
|
246
265
|
contrast_background_params.append(contrast_background_param)
|
|
247
266
|
|
|
248
|
-
#
|
|
267
|
+
# Set resolution parameters
|
|
249
268
|
resolution = project.resolutions[contrast.resolution]
|
|
250
269
|
contrast_resolution_types.append(resolution.type)
|
|
251
270
|
contrast_resolution_param = []
|
|
@@ -270,7 +289,7 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
270
289
|
|
|
271
290
|
contrast_resolution_params.append(contrast_resolution_param)
|
|
272
291
|
|
|
273
|
-
#
|
|
292
|
+
# Contrast data has exactly six columns to include background data if relevant
|
|
274
293
|
all_data.append(np.column_stack((data, np.zeros((data.shape[0], 6 - data.shape[1])))))
|
|
275
294
|
|
|
276
295
|
problem = ProblemDefinition()
|
|
@@ -365,6 +384,35 @@ def make_problem(project: RATapi.Project) -> ProblemDefinition:
|
|
|
365
384
|
return problem
|
|
366
385
|
|
|
367
386
|
|
|
387
|
+
def get_layer_details(project: RATapi.Project) -> list[int]:
|
|
388
|
+
"""Get parameter indices for all layers defined in the project."""
|
|
389
|
+
hydrate_id = {"bulk in": 1, "bulk out": 2}
|
|
390
|
+
layer_details = []
|
|
391
|
+
|
|
392
|
+
# Get the thickness, SLD, roughness fields from the appropriate model
|
|
393
|
+
if project.absorption:
|
|
394
|
+
layer_fields = list(RATapi.models.AbsorptionLayer.model_fields.keys())[1:-2]
|
|
395
|
+
else:
|
|
396
|
+
layer_fields = list(RATapi.models.Layer.model_fields.keys())[1:-2]
|
|
397
|
+
|
|
398
|
+
for layer in project.layers:
|
|
399
|
+
for field in layer_fields:
|
|
400
|
+
if getattr(layer, field) == "":
|
|
401
|
+
raise ValueError(
|
|
402
|
+
f'In the input project, the "{field}" field of layer {layer.name} does not have a value '
|
|
403
|
+
f"defined. A value must be supplied before running the project."
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
layer_params = [project.parameters.index(getattr(layer, attribute), True) for attribute in list(layer_fields)]
|
|
407
|
+
|
|
408
|
+
layer_params.append(project.parameters.index(layer.hydration, True) if layer.hydration else float("NaN"))
|
|
409
|
+
layer_params.append(hydrate_id[layer.hydrate_with])
|
|
410
|
+
|
|
411
|
+
layer_details.append(layer_params)
|
|
412
|
+
|
|
413
|
+
return layer_details
|
|
414
|
+
|
|
415
|
+
|
|
368
416
|
def make_resample(project: RATapi.Project) -> list[int]:
|
|
369
417
|
"""Construct the "resample" field of the problem input required for the compiled RAT code.
|
|
370
418
|
|
|
@@ -340,13 +340,13 @@ class Data(RATModel, arbitrary_types_allowed=True):
|
|
|
340
340
|
"""
|
|
341
341
|
|
|
342
342
|
name: str = Field(default_factory=lambda: f"New Data {next(data_number)}", min_length=1)
|
|
343
|
-
data: np.ndarray
|
|
343
|
+
data: np.ndarray = np.empty([0, 3])
|
|
344
344
|
data_range: list[float] = Field(default=[], min_length=2, max_length=2)
|
|
345
345
|
simulation_range: list[float] = Field(default=[], min_length=2, max_length=2)
|
|
346
346
|
|
|
347
347
|
@field_validator("data")
|
|
348
348
|
@classmethod
|
|
349
|
-
def check_data_dimension(cls, data: np.ndarray
|
|
349
|
+
def check_data_dimension(cls, data: np.ndarray) -> np.ndarray:
|
|
350
350
|
"""Ensure the data is be a two-dimensional array containing at least three columns."""
|
|
351
351
|
try:
|
|
352
352
|
data.shape[1]
|
|
@@ -516,7 +516,7 @@ class AbsorptionLayer(RATModel, populate_by_name=True):
|
|
|
516
516
|
name: str = Field(default_factory=lambda: f"New Layer {next(layer_number)}", min_length=1)
|
|
517
517
|
thickness: str
|
|
518
518
|
SLD_real: str = Field(validation_alias="SLD")
|
|
519
|
-
SLD_imaginary: str
|
|
519
|
+
SLD_imaginary: str
|
|
520
520
|
roughness: str
|
|
521
521
|
hydration: str = ""
|
|
522
522
|
hydrate_with: Hydration = Hydration.BulkOut
|
|
@@ -557,6 +557,20 @@ class Parameter(RATModel):
|
|
|
557
557
|
|
|
558
558
|
show_priors: bool = False
|
|
559
559
|
|
|
560
|
+
def model_post_init(self, __context: Any) -> None:
|
|
561
|
+
"""Apply parameter value to limits if they are not set."""
|
|
562
|
+
if "value" in self.model_fields_set:
|
|
563
|
+
if self.value > 0.0:
|
|
564
|
+
if "max" not in self.model_fields_set:
|
|
565
|
+
self.max = self.value
|
|
566
|
+
if "min" not in self.model_fields_set:
|
|
567
|
+
self.min = self.value
|
|
568
|
+
elif self.value < 0.0:
|
|
569
|
+
if "min" not in self.model_fields_set:
|
|
570
|
+
self.min = self.value
|
|
571
|
+
if "max" not in self.model_fields_set:
|
|
572
|
+
self.max = self.value
|
|
573
|
+
|
|
560
574
|
@model_validator(mode="after")
|
|
561
575
|
def check_min_max(self) -> "Parameter":
|
|
562
576
|
"""Ensure the maximum value of a parameter is greater than the minimum."""
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""Converts results from the compiled RAT code to python dataclasses."""
|
|
2
2
|
|
|
3
|
+
import json
|
|
3
4
|
from dataclasses import dataclass
|
|
5
|
+
from pathlib import Path
|
|
4
6
|
from typing import Any, Optional, Union
|
|
5
7
|
|
|
6
8
|
import numpy as np
|
|
@@ -8,6 +10,69 @@ import numpy as np
|
|
|
8
10
|
import RATapi.rat_core
|
|
9
11
|
from RATapi.utils.enums import Procedures
|
|
10
12
|
|
|
13
|
+
bayes_results_subclasses = [
|
|
14
|
+
"predictionIntervals",
|
|
15
|
+
"confidenceIntervals",
|
|
16
|
+
"dreamParams",
|
|
17
|
+
"dreamOutput",
|
|
18
|
+
"nestedSamplerOutput",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
bayes_results_fields = {
|
|
22
|
+
"param_fields": {
|
|
23
|
+
"predictionIntervals": [],
|
|
24
|
+
"confidenceIntervals": [],
|
|
25
|
+
"dreamParams": [
|
|
26
|
+
"nParams",
|
|
27
|
+
"nChains",
|
|
28
|
+
"nGenerations",
|
|
29
|
+
"parallel",
|
|
30
|
+
"CPU",
|
|
31
|
+
"jumpProbability",
|
|
32
|
+
"pUnitGamma",
|
|
33
|
+
"nCR",
|
|
34
|
+
"delta",
|
|
35
|
+
"steps",
|
|
36
|
+
"zeta",
|
|
37
|
+
"outlier",
|
|
38
|
+
"adaptPCR",
|
|
39
|
+
"thinning",
|
|
40
|
+
"epsilon",
|
|
41
|
+
"ABC",
|
|
42
|
+
"IO",
|
|
43
|
+
"storeOutput",
|
|
44
|
+
],
|
|
45
|
+
"dreamOutput": ["runtime", "iteration"],
|
|
46
|
+
"nestedSamplerOutput": ["logZ", "logZErr"],
|
|
47
|
+
},
|
|
48
|
+
"list_fields": {
|
|
49
|
+
"predictionIntervals": ["reflectivity"],
|
|
50
|
+
"confidenceIntervals": [],
|
|
51
|
+
"dreamParams": [],
|
|
52
|
+
"dreamOutput": [],
|
|
53
|
+
"nestedSamplerOutput": [],
|
|
54
|
+
},
|
|
55
|
+
"double_list_fields": {
|
|
56
|
+
"predictionIntervals": ["sld"],
|
|
57
|
+
"confidenceIntervals": [],
|
|
58
|
+
"dreamParams": [],
|
|
59
|
+
"dreamOutput": [],
|
|
60
|
+
"nestedSamplerOutput": [],
|
|
61
|
+
},
|
|
62
|
+
"array_fields": {
|
|
63
|
+
"predictionIntervals": ["sampleChi"],
|
|
64
|
+
"confidenceIntervals": ["percentile65", "percentile95", "mean"],
|
|
65
|
+
"dreamParams": ["R"],
|
|
66
|
+
"dreamOutput": ["allChains", "outlierChains", "AR", "R_stat", "CR"],
|
|
67
|
+
"nestedSamplerOutput": ["nestSamples", "postSamples"],
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
results_fields = {
|
|
72
|
+
"list_fields": ["reflectivity", "simulation", "shiftedData", "backgrounds", "resolutions"],
|
|
73
|
+
"double_list_fields": ["sldProfiles", "layers", "resampledLayers"],
|
|
74
|
+
}
|
|
75
|
+
|
|
11
76
|
|
|
12
77
|
def get_field_string(field: str, value: Any, array_limit: int):
|
|
13
78
|
"""Return a string representation of class fields where large arrays are represented by their shape.
|
|
@@ -179,6 +244,74 @@ class Results:
|
|
|
179
244
|
output += get_field_string(key, value, 100)
|
|
180
245
|
return output
|
|
181
246
|
|
|
247
|
+
def save(self, filepath: Union[str, Path] = "./results.json"):
|
|
248
|
+
"""Save the Results object to a JSON file.
|
|
249
|
+
|
|
250
|
+
Parameters
|
|
251
|
+
----------
|
|
252
|
+
filepath : str or Path
|
|
253
|
+
The path to where the results file will be written.
|
|
254
|
+
"""
|
|
255
|
+
filepath = Path(filepath).with_suffix(".json")
|
|
256
|
+
json_dict = write_core_results_fields(self)
|
|
257
|
+
|
|
258
|
+
filepath.write_text(json.dumps(json_dict))
|
|
259
|
+
|
|
260
|
+
@classmethod
|
|
261
|
+
def load(cls, path: Union[str, Path]) -> Union["Results", "BayesResults"]:
|
|
262
|
+
"""Load a Results object from file.
|
|
263
|
+
|
|
264
|
+
Parameters
|
|
265
|
+
----------
|
|
266
|
+
path : str or Path
|
|
267
|
+
The path to the results json file.
|
|
268
|
+
"""
|
|
269
|
+
path = Path(path)
|
|
270
|
+
input_data = path.read_text()
|
|
271
|
+
results_dict = json.loads(input_data)
|
|
272
|
+
|
|
273
|
+
results_dict = read_core_results_fields(results_dict)
|
|
274
|
+
|
|
275
|
+
if all(key in results_dict for key in bayes_results_subclasses):
|
|
276
|
+
results_dict = read_bayes_results_fields(results_dict)
|
|
277
|
+
|
|
278
|
+
return BayesResults(
|
|
279
|
+
reflectivity=results_dict["reflectivity"],
|
|
280
|
+
simulation=results_dict["simulation"],
|
|
281
|
+
shiftedData=results_dict["shiftedData"],
|
|
282
|
+
backgrounds=results_dict["backgrounds"],
|
|
283
|
+
resolutions=results_dict["resolutions"],
|
|
284
|
+
sldProfiles=results_dict["sldProfiles"],
|
|
285
|
+
layers=results_dict["layers"],
|
|
286
|
+
resampledLayers=results_dict["resampledLayers"],
|
|
287
|
+
calculationResults=CalculationResults(**results_dict["calculationResults"]),
|
|
288
|
+
contrastParams=ContrastParams(**results_dict["contrastParams"]),
|
|
289
|
+
fitParams=np.array(results_dict["fitParams"]),
|
|
290
|
+
fitNames=results_dict["fitNames"],
|
|
291
|
+
predictionIntervals=PredictionIntervals(**results_dict["predictionIntervals"]),
|
|
292
|
+
confidenceIntervals=ConfidenceIntervals(**results_dict["confidenceIntervals"]),
|
|
293
|
+
dreamParams=DreamParams(**results_dict["dreamParams"]),
|
|
294
|
+
dreamOutput=DreamOutput(**results_dict["dreamOutput"]),
|
|
295
|
+
nestedSamplerOutput=NestedSamplerOutput(**results_dict["nestedSamplerOutput"]),
|
|
296
|
+
chain=np.array(results_dict["chain"]),
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
else:
|
|
300
|
+
return Results(
|
|
301
|
+
reflectivity=results_dict["reflectivity"],
|
|
302
|
+
simulation=results_dict["simulation"],
|
|
303
|
+
shiftedData=results_dict["shiftedData"],
|
|
304
|
+
backgrounds=results_dict["backgrounds"],
|
|
305
|
+
resolutions=results_dict["resolutions"],
|
|
306
|
+
sldProfiles=results_dict["sldProfiles"],
|
|
307
|
+
layers=results_dict["layers"],
|
|
308
|
+
resampledLayers=results_dict["resampledLayers"],
|
|
309
|
+
calculationResults=CalculationResults(**results_dict["calculationResults"]),
|
|
310
|
+
contrastParams=ContrastParams(**results_dict["contrastParams"]),
|
|
311
|
+
fitParams=np.array(results_dict["fitParams"]),
|
|
312
|
+
fitNames=results_dict["fitNames"],
|
|
313
|
+
)
|
|
314
|
+
|
|
182
315
|
|
|
183
316
|
@dataclass
|
|
184
317
|
class PredictionIntervals(RATResult):
|
|
@@ -405,6 +538,148 @@ class BayesResults(Results):
|
|
|
405
538
|
nestedSamplerOutput: NestedSamplerOutput
|
|
406
539
|
chain: np.ndarray
|
|
407
540
|
|
|
541
|
+
def save(self, filepath: Union[str, Path] = "./results.json"):
|
|
542
|
+
"""Save the BayesResults object to a JSON file.
|
|
543
|
+
|
|
544
|
+
Parameters
|
|
545
|
+
----------
|
|
546
|
+
filepath : str or Path
|
|
547
|
+
The path to where the results file will be written.
|
|
548
|
+
"""
|
|
549
|
+
filepath = Path(filepath).with_suffix(".json")
|
|
550
|
+
json_dict = write_core_results_fields(self)
|
|
551
|
+
|
|
552
|
+
# Take each of the subclasses in a BayesResults instance and switch the numpy arrays to lists
|
|
553
|
+
for subclass_name in bayes_results_subclasses:
|
|
554
|
+
subclass = getattr(self, subclass_name)
|
|
555
|
+
subclass_dict = {}
|
|
556
|
+
|
|
557
|
+
for field in bayes_results_fields["param_fields"][subclass_name]:
|
|
558
|
+
subclass_dict[field] = getattr(subclass, field)
|
|
559
|
+
|
|
560
|
+
for field in bayes_results_fields["list_fields"][subclass_name]:
|
|
561
|
+
subclass_dict[field] = [result_array.tolist() for result_array in getattr(subclass, field)]
|
|
562
|
+
|
|
563
|
+
for field in bayes_results_fields["double_list_fields"][subclass_name]:
|
|
564
|
+
subclass_dict[field] = [
|
|
565
|
+
[result_array.tolist() for result_array in inner_list] for inner_list in getattr(subclass, field)
|
|
566
|
+
]
|
|
567
|
+
|
|
568
|
+
for field in bayes_results_fields["array_fields"][subclass_name]:
|
|
569
|
+
subclass_dict[field] = getattr(subclass, field).tolist()
|
|
570
|
+
|
|
571
|
+
json_dict[subclass_name] = subclass_dict
|
|
572
|
+
|
|
573
|
+
json_dict["chain"] = self.chain.tolist()
|
|
574
|
+
filepath.write_text(json.dumps(json_dict))
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
def write_core_results_fields(results: Union[Results, BayesResults], json_dict: Optional[dict] = None) -> dict:
|
|
578
|
+
"""Modify the values of the fields that appear in both Results and BayesResults when saving to a json file.
|
|
579
|
+
|
|
580
|
+
Parameters
|
|
581
|
+
----------
|
|
582
|
+
results: Union[Results, BayesResults]
|
|
583
|
+
The results or BayesResults object we are writing to json.
|
|
584
|
+
json_dict: Optional[dict]
|
|
585
|
+
The dictionary containing the json output.
|
|
586
|
+
|
|
587
|
+
Returns
|
|
588
|
+
-------
|
|
589
|
+
json_dict: dict
|
|
590
|
+
The output json dict updated with the fields that appear in both Results and BayesResults.
|
|
591
|
+
"""
|
|
592
|
+
if json_dict is None:
|
|
593
|
+
json_dict = {}
|
|
594
|
+
|
|
595
|
+
for field in results_fields["list_fields"]:
|
|
596
|
+
json_dict[field] = [result_array.tolist() for result_array in getattr(results, field)]
|
|
597
|
+
|
|
598
|
+
for field in results_fields["double_list_fields"]:
|
|
599
|
+
json_dict[field] = [
|
|
600
|
+
[result_array.tolist() for result_array in inner_list] for inner_list in getattr(results, field)
|
|
601
|
+
]
|
|
602
|
+
|
|
603
|
+
json_dict["calculationResults"] = {}
|
|
604
|
+
json_dict["calculationResults"]["chiValues"] = results.calculationResults.chiValues.tolist()
|
|
605
|
+
json_dict["calculationResults"]["sumChi"] = results.calculationResults.sumChi
|
|
606
|
+
|
|
607
|
+
json_dict["contrastParams"] = {}
|
|
608
|
+
for field in results.contrastParams.__dict__:
|
|
609
|
+
json_dict["contrastParams"][field] = getattr(results.contrastParams, field).tolist()
|
|
610
|
+
|
|
611
|
+
json_dict["fitParams"] = results.fitParams.tolist()
|
|
612
|
+
json_dict["fitNames"] = results.fitNames
|
|
613
|
+
|
|
614
|
+
return json_dict
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
def read_core_results_fields(results_dict: dict) -> dict:
|
|
618
|
+
"""Modify the values of the fields that appear in both Results and BayesResults when loading a json file.
|
|
619
|
+
|
|
620
|
+
Parameters
|
|
621
|
+
----------
|
|
622
|
+
results_dict: Optional[dict]
|
|
623
|
+
The dictionary containing the json input.
|
|
624
|
+
|
|
625
|
+
Returns
|
|
626
|
+
-------
|
|
627
|
+
results_dict: dict
|
|
628
|
+
The input json dict with the fields that appear in both Results and BayesResults converted to numpy arrays
|
|
629
|
+
where necessary.
|
|
630
|
+
"""
|
|
631
|
+
for field in results_fields["list_fields"]:
|
|
632
|
+
results_dict[field] = [np.array(result_array) for result_array in results_dict[field]]
|
|
633
|
+
|
|
634
|
+
for field in results_fields["double_list_fields"]:
|
|
635
|
+
results_dict[field] = [
|
|
636
|
+
[np.array(result_array) for result_array in inner_list] for inner_list in results_dict[field]
|
|
637
|
+
]
|
|
638
|
+
|
|
639
|
+
results_dict["calculationResults"]["chiValues"] = np.array(results_dict["calculationResults"]["chiValues"])
|
|
640
|
+
|
|
641
|
+
for field in results_dict["contrastParams"]:
|
|
642
|
+
results_dict["contrastParams"][field] = np.array(results_dict["contrastParams"][field])
|
|
643
|
+
|
|
644
|
+
return results_dict
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
def read_bayes_results_fields(results_dict: dict) -> dict:
|
|
648
|
+
"""Modify the values of the fields that appear only in BayesResults when loading a json file.
|
|
649
|
+
|
|
650
|
+
Parameters
|
|
651
|
+
----------
|
|
652
|
+
results_dict: Optional[dict]
|
|
653
|
+
The dictionary containing the json input.
|
|
654
|
+
|
|
655
|
+
Returns
|
|
656
|
+
-------
|
|
657
|
+
results_dict: dict
|
|
658
|
+
The input json dict with the fields that appear in both Results and BayesResults converted to numpy arrays
|
|
659
|
+
where necessary.
|
|
660
|
+
"""
|
|
661
|
+
for subclass_name in bayes_results_subclasses:
|
|
662
|
+
subclass_dict = {}
|
|
663
|
+
|
|
664
|
+
for field in bayes_results_fields["param_fields"][subclass_name]:
|
|
665
|
+
subclass_dict[field] = results_dict[subclass_name][field]
|
|
666
|
+
|
|
667
|
+
for field in bayes_results_fields["list_fields"][subclass_name]:
|
|
668
|
+
subclass_dict[field] = [np.array(result_array) for result_array in results_dict[subclass_name][field]]
|
|
669
|
+
|
|
670
|
+
for field in bayes_results_fields["double_list_fields"][subclass_name]:
|
|
671
|
+
subclass_dict[field] = [
|
|
672
|
+
[np.array(result_array) for result_array in inner_list]
|
|
673
|
+
for inner_list in results_dict[subclass_name][field]
|
|
674
|
+
]
|
|
675
|
+
|
|
676
|
+
for field in bayes_results_fields["array_fields"][subclass_name]:
|
|
677
|
+
subclass_dict[field] = np.array(results_dict[subclass_name][field])
|
|
678
|
+
|
|
679
|
+
results_dict[subclass_name] = subclass_dict
|
|
680
|
+
|
|
681
|
+
return results_dict
|
|
682
|
+
|
|
408
683
|
|
|
409
684
|
def make_results(
|
|
410
685
|
procedure: Procedures,
|
|
@@ -365,12 +365,12 @@ class Project(BaseModel, validate_assignment=True, extra="forbid", use_attribute
|
|
|
365
365
|
and wrap ClassList routines to control revalidation.
|
|
366
366
|
"""
|
|
367
367
|
# Ensure all ClassLists have the correct _class_handle defined
|
|
368
|
-
for field in (fields :=
|
|
369
|
-
|
|
370
|
-
if get_origin(
|
|
368
|
+
for field in (fields := Project.model_fields):
|
|
369
|
+
annotation = fields[field].annotation
|
|
370
|
+
if get_origin(annotation) == ClassList:
|
|
371
371
|
classlist = getattr(self, field)
|
|
372
372
|
if not hasattr(field, "_class_handle"):
|
|
373
|
-
classlist._class_handle = get_args(
|
|
373
|
+
classlist._class_handle = get_args(annotation)[0]
|
|
374
374
|
|
|
375
375
|
layers_field = self.layers
|
|
376
376
|
if not hasattr(layers_field, "_class_handle"):
|
|
@@ -529,7 +529,9 @@ class Project(BaseModel, validate_assignment=True, extra="forbid", use_attribute
|
|
|
529
529
|
handle = self.layers._class_handle.__name__
|
|
530
530
|
if self.absorption and handle == "Layer":
|
|
531
531
|
for layer in self.layers:
|
|
532
|
-
|
|
532
|
+
layer_params = layer.model_dump()
|
|
533
|
+
layer_params["SLD_imaginary"] = ""
|
|
534
|
+
layer_list.append(RATapi.models.AbsorptionLayer(**layer_params))
|
|
533
535
|
self.layers.data = layer_list
|
|
534
536
|
self.layers._class_handle = RATapi.models.AbsorptionLayer
|
|
535
537
|
elif not self.absorption and handle == "AbsorptionLayer":
|
|
@@ -904,7 +906,6 @@ from numpy import array, empty, inf
|
|
|
904
906
|
----------
|
|
905
907
|
filepath : str or Path
|
|
906
908
|
The path to where the project file will be written.
|
|
907
|
-
|
|
908
909
|
"""
|
|
909
910
|
filepath = Path(filepath).with_suffix(".json")
|
|
910
911
|
|