Skip to content

Commit 3d77799

Browse files
committed
removed to-project and updated notebook
1 parent a7eeac2 commit 3d77799

File tree

5 files changed

+67
-126
lines changed

5 files changed

+67
-126
lines changed

RATapi/examples/orso_integration/orso_integration.ipynb

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 8,
19+
"execution_count": null,
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
@@ -94,53 +94,37 @@
9494
"cell_type": "markdown",
9595
"metadata": {},
9696
"source": [
97-
"## Reading in data and models from .ort files\n",
98-
"\n",
99-
"It is possible to read data from .ort format files at two different levels of depth."
97+
"## Reading in data and models from .ort files"
10098
]
10199
},
102100
{
103101
"cell_type": "markdown",
104102
"metadata": {},
105103
"source": [
106-
"### Reading data\n",
104+
"RAT can also load both data and model information from an .ort file. This is done through the `ORSOProject` object, which takes a file path and can also optionally account for absorption.\n",
107105
"\n",
108-
"Firstly, if you just want to read the data from a file, you can do so via the function `RATapi.utils.orso.load_ort_data`.\n",
109-
"\n",
110-
"This will read the .ort data in as a RAT `Data` object. If there are multiple datasets in your file, you will get a list of datasets back."
106+
"The example data file we use here [is example data by Artur Glavic, taken on the Amor reflectometer at PSI.](https://github.com/reflectivity/orsopy/blob/main/examples/prist5_10K_m_025.Rqz.ort)"
111107
]
112108
},
113109
{
114110
"cell_type": "code",
115-
"execution_count": null,
111+
"execution_count": 5,
116112
"metadata": {},
117113
"outputs": [],
118114
"source": [
119115
"import pathlib\n",
120116
"data_path = pathlib.Path(\"../data\")\n",
121117
"\n",
122-
"data = RATapi.utils.orso.load_ort_data(data_path / \"IvsQ_82043_82044.ort\")\n",
123-
"print(data)"
118+
"orso_data = RATapi.utils.orso.ORSOProject(data_path / \"prist5_10K_m_025.Rqz.ort\")"
124119
]
125120
},
126121
{
127122
"cell_type": "markdown",
128123
"metadata": {},
129124
"source": [
130-
"### Reading an entire project\n",
131-
"If your .ort file contains model data, you can also use the more elaborate `RATapi.utils.orso.ort_to_project`, which creates an almost-complete RAT `Project` with model and data from the file. If your file contains multiple datasets with models, a contrast will be created in the project for each model.\n",
125+
"The `ORSOProject` object contains two lists: `ORSOProject.data` and `ORSOProject.samples`. The former is a list of Data objects with each dataset defined in the file, and the latter is a list of `ORSOSample` objects (like above) with model information. Note that if the .ort file does not define a model for a dataset, that index of `ORSOProject.samples` will be None.\n",
132126
"\n",
133-
"Note that none of the parameters will have ranges or be fittable (you will have to manually change the minimum and maximum range if you want to fit the parameters!). Furthermore, the background and scalefactor are not part of the format and will just be empty placeholders in this project. "
134-
]
135-
},
136-
{
137-
"cell_type": "code",
138-
"execution_count": null,
139-
"metadata": {},
140-
"outputs": [],
141-
"source": [
142-
"project = RATapi.utils.orso.ort_to_project(data_path / \"prist5_10K_m_025.Rqz.ort\")\n",
143-
"print(project)"
127+
"It's then easy to access this data to create a RAT `Project` that represents our data."
144128
]
145129
},
146130
{
@@ -149,6 +133,34 @@
149133
"metadata": {},
150134
"outputs": [],
151135
"source": [
136+
"from RATapi.models import Background, Contrast, Parameter, Resolution\n",
137+
"\n",
138+
"dataset = orso_data.data[0]\n",
139+
"sample = orso_data.samples[0]\n",
140+
"\n",
141+
"project = RATapi.Project(\n",
142+
" name = \"Example Project\",\n",
143+
" parameters = sample.parameters,\n",
144+
" bulk_in = [sample.bulk_in],\n",
145+
" bulk_out = [sample.bulk_out],\n",
146+
" scalefactors = [Parameter(name=\"Scalefactor\", min=0, value=1, max=1.5, fit=True)],\n",
147+
" background_parameters = [Parameter(name=\"Background Parameter\", min=0, value=0.025, max=1, fit=True)],\n",
148+
" backgrounds = [Background(name=\"Background\", type=\"constant\", source=\"Background Parameter\")],\n",
149+
" resolutions = [Resolution(name=\"Data Resolution\", type=\"data\")],\n",
150+
" data = [dataset],\n",
151+
" layers = sample.layers,\n",
152+
" contrasts = [Contrast(\n",
153+
" name = \"prist4\",\n",
154+
" data = dataset.name,\n",
155+
" background = \"Background\",\n",
156+
" bulk_in = sample.bulk_in.name,\n",
157+
" bulk_out = sample.bulk_out.name,\n",
158+
" scalefactor = \"Scalefactor\",\n",
159+
" resolution = \"Data Resolution\",\n",
160+
" model = sample.model,\n",
161+
" )]\n",
162+
")\n",
163+
"\n",
152164
"controls = RATapi.Controls()\n",
153165
"project, results = RATapi.run(project, controls)\n",
154166
"RATapi.plotting.plot_ref_sld(project, results)"

RATapi/utils/orso.py

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,32 @@
11
"""Readers from file formats."""
22

33
from dataclasses import dataclass
4+
from pathlib import Path
45
from typing import Union
56

67
import orsopy
78
from orsopy.fileio import load_orso
89

9-
from RATapi import ClassList, Project
10-
from RATapi.models import AbsorptionLayer, Contrast, Data, Layer, Parameter, Resolution
10+
from RATapi import ClassList
11+
from RATapi.models import AbsorptionLayer, Data, Layer, Parameter
1112

1213

13-
def load_ort_data(filepath: str) -> Union[Data, list[Data]]:
14-
"""Read data from an .ort file.
14+
class ORSOProject:
15+
"""A class to encapsulate model information and data from an .ort file.
1516
1617
Parameters
1718
----------
18-
filepath : str
19+
filepath : str or Path
1920
The path to the .ort file.
20-
21-
Returns
22-
-------
23-
Data or list[Data]
24-
If the .ort file contains one dataset, just the data model.
25-
If it contains multiple datasets, returns a list of data models.
21+
absorption : bool, default None
22+
Whether to account for absorption in the model data.
2623
2724
"""
28-
ort_data = load_orso(filepath)
29-
datasets = [Data(name=dataset.info.data_source.sample.name, data=dataset.data) for dataset in ort_data]
30-
if len(datasets) == 1:
31-
return datasets[0]
32-
return datasets
3325

34-
35-
def ort_to_project(filepath: str) -> Project:
36-
"""Create a project from an .ort file.
37-
38-
Parameters
39-
----------
40-
filepath : str
41-
The path to the .ort file.
42-
43-
Returns
44-
-------
45-
Project
46-
The project data from the .ort file.
47-
"""
48-
project = Project()
49-
50-
ort_data = load_orso(filepath)
51-
52-
for dataset in ort_data:
53-
sample = dataset.info.data_source.sample
54-
55-
project.data.append(Data(name=sample.name, data=dataset.data))
56-
57-
if sample.model is not None:
58-
model_info = orso_model_to_rat(sample.model)
59-
60-
# Add all parameters that aren't already defined
61-
project.parameters.union(model_info.parameters)
62-
project.layers.extend(model_info.layers)
63-
project.bulk_in.append(model_info.bulk_in)
64-
project.bulk_out.append(model_info.bulk_out)
65-
66-
if dataset.data.shape[1] == 4:
67-
project.resolutions.append(
68-
Resolution(
69-
name=f"{sample.name} Resolution",
70-
type="data",
71-
)
72-
)
73-
else:
74-
project.resolutions.append(
75-
Resolution(
76-
name=f"{sample.name} Resolution",
77-
type="constant",
78-
source="Resolution Param 1",
79-
)
80-
)
81-
82-
project.contrasts.append(
83-
Contrast(
84-
name=dataset.info.data_source.experiment.title,
85-
data=sample.name,
86-
background="Background 1",
87-
bulk_in=model_info.bulk_in.name,
88-
bulk_out=model_info.bulk_out.name,
89-
scalefactor="Scalefactor 1",
90-
resolution=f"{sample.name} Resolution",
91-
model=model_info.model,
92-
)
93-
)
94-
95-
return project
26+
def __init__(self, filepath: Union[str, Path], absorption: bool = False):
27+
ort_data = load_orso(filepath)
28+
self.data = [Data(name=dataset.info.data_source.sample.name, data=dataset.data) for dataset in ort_data]
29+
self.samples = [orso_model_to_rat(dataset.info.data_source.sample.model) for dataset in ort_data]
9630

9731

9832
@dataclass
@@ -108,7 +42,7 @@ class ORSOSample:
10842

10943
def orso_model_to_rat(
11044
model: Union[orsopy.fileio.model_language.SampleModel, str], absorption: bool = False
111-
) -> ORSOSample:
45+
) -> Union[ORSOSample, None]:
11246
"""Get information from an ORSO SampleModel object.
11347
11448
Parameters
@@ -122,21 +56,21 @@ def orso_model_to_rat(
12256
Returns
12357
-------
12458
ORSOSample
125-
A dataclass containing the sample data.
59+
A dataclass containing the sample data, or None if the model is None.
12660
12761
"""
62+
if model is None:
63+
return None
64+
12865
if isinstance(model, str):
12966
model = orsopy.fileio.model_language.SampleModel(stack=model)
67+
13068
stack = model.resolve_to_layers()
13169
# if bulk in or out is air, it has SLD predefined
13270
# else we need to grab it from SLDDB
13371
if bulk_in_sld := stack[0].material.sld is None:
13472
bulk_in_sld = stack[0].material.get_sld()
13573

136-
# orsopy SLDs are in 10^-6 inverse square Angstroms
137-
# but RAT uses just inverse square Angstroms
138-
bulk_in_sld *= 1e6
139-
14074
# resolve_to_layers loses the name of bulk in and out
14175
bulk_in_name = model.stack.split("|")[0].strip()
14276
bulk_in = Parameter(
@@ -150,8 +84,6 @@ def orso_model_to_rat(
15084
if bulk_out_sld := stack[-1].material.sld is None:
15185
bulk_out_sld = stack[-1].material.get_sld()
15286

153-
bulk_out_sld *= 1e6
154-
15587
bulk_out_name = model.stack.split("|")[-1].strip()
15688
bulk_out = Parameter(
15789
name=f"{bulk_out_name} SLD",
@@ -199,10 +131,7 @@ def orso_layer_to_rat_layer(
199131
name = layer.material.formula
200132
thickness = layer.thickness.as_unit("angstrom")
201133
roughness = layer.roughness.as_unit("angstrom")
202-
203-
# orsopy SLDs are in 10^-6 inverse square Angstroms
204-
# but RAT uses just inverse square Angstroms
205-
sld = layer.material.get_sld() * 1e6
134+
sld = layer.material.get_sld()
206135

207136
params = ClassList(
208137
[
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name": "", "calculation": "normal", "model": "standard layers", "geometry": "air/substrate", "absorption": false, "parameters": [{"name": "Substrate Roughness", "min": 1.0, "value": 3.0, "max": 5.0, "fit": true, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_in": [{"name": "SLD Air", "min": 0.0, "value": 0.0, "max": 0.0, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "air SLD", "min": 0.00442927130586151, "value": 0.00442927130586151, "max": 0.00442927130586151, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_out": [{"name": "SLD D2O", "min": 6.2e-06, "value": 6.35e-06, "max": 6.35e-06, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}, {"name": "D2O SLD", "min": 6.360408603667384, "value": 6.360408603667384, "max": 6.360408603667384, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "scalefactors": [{"name": "Scalefactor 1", "min": 0.02, "value": 0.23, "max": 0.25, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "domain_ratios": [], "background_parameters": [{"name": "Background Param 1", "min": 1e-07, "value": 1e-06, "max": 1e-05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "backgrounds": [{"name": "Background 1", "type": "constant", "source": "Background Param 1", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "resolution_parameters": [{"name": "Resolution Param 1", "min": 0.01, "value": 0.03, "max": 0.05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "resolutions": [{"name": "Resolution 1", "type": "constant", "source": "Resolution Param 1", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}, {"name": "D2O substrate Resolution", "type": "data", "source": "", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "custom_files": [], "data": [{"name": "Simulation", "data": [], "data_range": [], "simulation_range": [0.005, 0.7]}, {"name": "D2O substrate", "data": [[0.048866, 0.00012343, 1.3213e-06, 0.03], [0.051309, 0.00010063, 1.0803e-06, 0.03], [0.053874, 8.2165e-05, 8.8779e-07, 0.03], [0.056568, 6.4993e-05, 7.2018e-07, 0.03], [0.059396, 5.3958e-05, 6.0015e-07, 0.03], [0.062366, 4.359e-05, 5.0129e-07, 0.03], [0.065485, 3.578e-05, 4.1957e-07, 0.03], [0.068759, 2.913e-05, 3.5171e-07, 0.03], [0.072197, 2.3481e-05, 3.0586e-07, 0.03], [0.075807, 1.8906e-05, 2.6344e-07, 0.03], [0.079597, 1.4642e-05, 2.2314e-07, 0.03], [0.083577, 1.1589e-05, 1.8938e-07, 0.03], [0.087756, 9.5418e-06, 1.622e-07, 0.03], [0.092143, 7.5694e-06, 1.3809e-07, 0.03], [0.096751, 6.3831e-06, 1.2097e-07, 0.03], [0.10159, 5.0708e-06, 1.0333e-07, 0.03], [0.10667, 4.1041e-06, 8.9548e-08, 0.03], [0.112, 3.4253e-06, 7.983e-08, 0.03], [0.1176, 2.8116e-06, 7.1554e-08, 0.03], [0.12348, 2.3767e-06, 6.3738e-08, 0.03], [0.12966, 1.9241e-06, 5.6586e-08, 0.03], [0.13614, 1.5642e-06, 5.2778e-08, 0.03], [0.14294, 1.2922e-06, 4.973e-08, 0.03], [0.15009, 1.1694e-06, 5.1175e-08, 0.03], [0.1576, 9.7837e-07, 5.0755e-08, 0.03], [0.16548, 8.9138e-07, 5.3542e-08, 0.03], [0.17375, 7.942e-07, 5.4857e-08, 0.03], [0.18244, 7.9131e-07, 5.8067e-08, 0.03], [0.19156, 6.5358e-07, 5.7717e-08, 0.03], [0.20114, 6.297e-07, 5.7951e-08, 0.03], [0.21119, 5.013e-07, 5.5262e-08, 0.03], [0.22175, 5.0218e-07, 5.6461e-08, 0.03], [0.23284, 3.9299e-07, 5.0685e-08, 0.03], [0.24448, 3.5324e-07, 5.0194e-08, 0.03], [0.25671, 4.4475e-07, 5.6485e-08, 0.03], [0.26954, 5.1338e-07, 6.2247e-08, 0.03], [0.28302, 3.4918e-07, 4.9745e-08, 0.03], [0.29717, 4.3037e-07, 5.5488e-08, 0.03], [0.31203, 4.0099e-07, 5.3591e-08, 0.03], [0.32763, 3.8397e-07, 5.1303e-08, 0.03], [0.34401, 3.0995e-07, 4.5965e-08, 0.03], [0.36121, 3.9357e-07, 5.0135e-08, 0.03], [0.37927, 3.0997e-07, 4.368e-08, 0.03], [0.39824, 2.9656e-07, 4.2432e-08, 0.03], [0.41815, 2.1909e-07, 3.6117e-08, 0.03], [0.43906, 2.3153e-07, 3.6307e-08, 0.03], [0.46101, 3.3428e-07, 4.3874e-08, 0.03], [0.48406, 2.3441e-07, 3.7488e-08, 0.03], [0.50826, 1.5496e-07, 3.0585e-08, 0.03], [0.53368, 2.4708e-07, 3.9376e-08, 0.03], [0.56036, 2.2157e-07, 3.8258e-08, 0.03], [0.58838, 2.2798e-07, 4.6976e-08, 0.03], [0.61169, 6.0272e-07, 2.3239e-07, 0.03]], "data_range": [0.048866, 0.61169], "simulation_range": [0.048866, 0.61169]}], "layers": [], "domain_contrasts": [], "contrasts": [{"name": "Bare D2O substrate", "data": "D2O substrate", "background": "Background 1", "background_action": "add", "bulk_in": "air SLD", "bulk_out": "D2O SLD", "scalefactor": "Scalefactor 1", "resolution": "D2O substrate Resolution", "resample": false, "model": []}]}
1+
{"name": "substrate", "calculation": "normal", "model": "standard layers", "geometry": "air/substrate", "absorption": false, "parameters": [{"name": "Substrate Roughness", "min": 1.0, "value": 3.0, "max": 5.0, "fit": true, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_in": [{"name": "air SLD", "min": 4.42927130586151e-09, "value": 4.42927130586151e-09, "max": 4.42927130586151e-09, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "bulk_out": [{"name": "D2O SLD", "min": 6.360408603667384e-06, "value": 6.360408603667384e-06, "max": 6.360408603667384e-06, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "scalefactors": [{"name": "Scalefactor 1", "min": 0.02, "value": 0.23, "max": 0.25, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "domain_ratios": [], "background_parameters": [{"name": "Background Param 1", "min": 1e-07, "value": 1e-06, "max": 1e-05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "backgrounds": [{"name": "Background 1", "type": "constant", "source": "Background Param 1", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "resolution_parameters": [{"name": "Resolution Param 1", "min": 0.01, "value": 0.03, "max": 0.05, "fit": false, "prior_type": "uniform", "mu": 0.0, "sigma": Infinity, "show_priors": false}], "resolutions": [{"name": "Resolution 1", "type": "constant", "source": "Resolution Param 1", "value_1": "", "value_2": "", "value_3": "", "value_4": "", "value_5": ""}], "custom_files": [], "data": [{"name": "Simulation", "data": [], "data_range": [], "simulation_range": [0.005, 0.7]}, {"name": "D2O substrate", "data": [[0.048866, 0.00012343, 1.3213e-06, 0.03], [0.051309, 0.00010063, 1.0803e-06, 0.03], [0.053874, 8.2165e-05, 8.8779e-07, 0.03], [0.056568, 6.4993e-05, 7.2018e-07, 0.03], [0.059396, 5.3958e-05, 6.0015e-07, 0.03], [0.062366, 4.359e-05, 5.0129e-07, 0.03], [0.065485, 3.578e-05, 4.1957e-07, 0.03], [0.068759, 2.913e-05, 3.5171e-07, 0.03], [0.072197, 2.3481e-05, 3.0586e-07, 0.03], [0.075807, 1.8906e-05, 2.6344e-07, 0.03], [0.079597, 1.4642e-05, 2.2314e-07, 0.03], [0.083577, 1.1589e-05, 1.8938e-07, 0.03], [0.087756, 9.5418e-06, 1.622e-07, 0.03], [0.092143, 7.5694e-06, 1.3809e-07, 0.03], [0.096751, 6.3831e-06, 1.2097e-07, 0.03], [0.10159, 5.0708e-06, 1.0333e-07, 0.03], [0.10667, 4.1041e-06, 8.9548e-08, 0.03], [0.112, 3.4253e-06, 7.983e-08, 0.03], [0.1176, 2.8116e-06, 7.1554e-08, 0.03], [0.12348, 2.3767e-06, 6.3738e-08, 0.03], [0.12966, 1.9241e-06, 5.6586e-08, 0.03], [0.13614, 1.5642e-06, 5.2778e-08, 0.03], [0.14294, 1.2922e-06, 4.973e-08, 0.03], [0.15009, 1.1694e-06, 5.1175e-08, 0.03], [0.1576, 9.7837e-07, 5.0755e-08, 0.03], [0.16548, 8.9138e-07, 5.3542e-08, 0.03], [0.17375, 7.942e-07, 5.4857e-08, 0.03], [0.18244, 7.9131e-07, 5.8067e-08, 0.03], [0.19156, 6.5358e-07, 5.7717e-08, 0.03], [0.20114, 6.297e-07, 5.7951e-08, 0.03], [0.21119, 5.013e-07, 5.5262e-08, 0.03], [0.22175, 5.0218e-07, 5.6461e-08, 0.03], [0.23284, 3.9299e-07, 5.0685e-08, 0.03], [0.24448, 3.5324e-07, 5.0194e-08, 0.03], [0.25671, 4.4475e-07, 5.6485e-08, 0.03], [0.26954, 5.1338e-07, 6.2247e-08, 0.03], [0.28302, 3.4918e-07, 4.9745e-08, 0.03], [0.29717, 4.3037e-07, 5.5488e-08, 0.03], [0.31203, 4.0099e-07, 5.3591e-08, 0.03], [0.32763, 3.8397e-07, 5.1303e-08, 0.03], [0.34401, 3.0995e-07, 4.5965e-08, 0.03], [0.36121, 3.9357e-07, 5.0135e-08, 0.03], [0.37927, 3.0997e-07, 4.368e-08, 0.03], [0.39824, 2.9656e-07, 4.2432e-08, 0.03], [0.41815, 2.1909e-07, 3.6117e-08, 0.03], [0.43906, 2.3153e-07, 3.6307e-08, 0.03], [0.46101, 3.3428e-07, 4.3874e-08, 0.03], [0.48406, 2.3441e-07, 3.7488e-08, 0.03], [0.50826, 1.5496e-07, 3.0585e-08, 0.03], [0.53368, 2.4708e-07, 3.9376e-08, 0.03], [0.56036, 2.2157e-07, 3.8258e-08, 0.03], [0.58838, 2.2798e-07, 4.6976e-08, 0.03], [0.61169, 6.0272e-07, 2.3239e-07, 0.03]], "data_range": [0.048866, 0.61169], "simulation_range": [0.048866, 0.61169]}], "layers": [], "domain_contrasts": [], "contrasts": []}

0 commit comments

Comments
 (0)