Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/RAT
Submodule RAT updated 1 files
+1 −1 applyHydration.cpp
4 changes: 4 additions & 0 deletions tests/test_data/ORSO/test0.layers
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.000000000000000000e+00 2.069999999999999840e+00 0.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+02 3.450000000000000178e+00 1.000000000000000056e-01 3.000000000000000000e+00
2.000000000000000000e+02 5.000000000000000000e+00 1.000000000000000021e-02 1.000000000000000000e+00
0.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00 5.000000000000000000e+00
22 changes: 22 additions & 0 deletions tests/test_data/ORSO/test1.layers
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
0.0000 0.0000 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
30.0000 -1.9493 0.0000 0.0000
70.0000 9.4245 0.0000 0.0000
0.0000 2.0704 0.0000 0.0000
2 changes: 2 additions & 0 deletions tests/test_data/ORSO/test2.layers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0.0000 0.0000 0.0000 0.0000
0.0000 6.3600 0.0000 3.0000
2,001 changes: 2,001 additions & 0 deletions tests/test_data/ORSO/test3.layers

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions tests/test_data/ORSO/test6.layers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.00e+00 2.07e+00 0.00e+00 0
1.20e+03 4.66e+00 1.60e-02 1.00e+01
0.00e+00 6.36e+00 0.00e+00 3.00e+00
3 changes: 3 additions & 0 deletions tests/test_data/ORSO/test7.layers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.00e+00 0.0e+00 0.00e+00 0
1.20e+03 4.66e+00 1.60e-02 1.00e+01
0.00e+00 6.36e+00 0.00e+00 3.00e+00
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Comment thread
StephenNneji marked this conversation as resolved.
Outdated
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion tests/test_data/orso_validation.json

This file was deleted.

96 changes: 67 additions & 29 deletions tests/test_orso_validation.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,84 @@
import os
import pathlib
import tempfile

import numpy as np
import pytest

import ratapi as RAT
from ratapi.utils.plotting import plot_ref_sld

PROJECT_PATH = pathlib.Path(__file__).parents[0] / "test_data"
TEST_FUNC = """import pathlib
import numpy as np


@pytest.fixture(scope="module")
def orso_project():
"""Load a project with all the ORSO validation data included."""
return RAT.Project.load(pathlib.Path(PROJECT_PATH, "orso_validation.json"))
def run_func(params, bulk_in, bulk_out, contrast):
layers = np.loadtxt("{layers_file_path}")

# Change the units to Å
layers[:, 1:3] = layers[:, 1:3] * 1e-6

# Returns layers only, bulk in and bulk out added to project
return layers[1:-1, :], params[0]
"""


@pytest.mark.parametrize(
["test_index", "substrate_roughness", "layer_model"],
[
(0, 5.0, ["Test 0 Layer 1", "Test 0 Layer 2"]),
(1, 0.0, ["Test 1 Layer 1", "Test 1 Layer 2"] * 10),
(2, 3.0, ["Test 2 Layer 1"]),
(3, 0.0, [f"Test 3 Layer {i}" for i in range(2001)]),
(6, 3.0, ["Test 6 Layer 1"]),
(7, 3.0, ["Test 7 Layer 1"]),
],
"layer_index",
[0, 1, 2, 3, 6, 7],
)
def test_orso_validation(orso_project, substrate_roughness, test_index, layer_model):
orso_project.parameters.set_fields("Substrate Roughness", value=substrate_roughness)
def test_orso_validation(layer_index):
data_path = pathlib.Path(__file__).parent / "test_data" / "ORSO"

problem = RAT.Project(name="test", model="custom layers", absorption=True)
problem.scalefactors.set_fields(0, min=1, value=1, max=1)
problem.background_parameters.set_fields(0, min=0, value=0, max=0)
problem.resolution_parameters.set_fields(0, min=0, value=0, max=0)

# Write a custom file that reads the ORSO layers.
filename = data_path / f"test{layer_index}.layers"
with tempfile.NamedTemporaryFile("w+", suffix=".py", delete=False) as f:
f.write(TEST_FUNC.format(layers_file_path=filename.as_posix()))
f.flush()

layers = np.loadtxt(filename)
sub_rough = layers[-1, -1]

# Change the units to Å
bulk_in = layers[0, 1] * 1e-6
bulk_out = layers[-1, 1] * 1e-6

problem.parameters.set_fields(0, min=sub_rough, value=sub_rough, max=sub_rough)
problem.bulk_in.set_fields(0, name="Bulk In", min=bulk_in, value=bulk_in, max=bulk_in, fit=False)
problem.bulk_out.set_fields(0, name="Bulk Out", min=bulk_out, value=bulk_out, max=bulk_out, fit=False)

orso_project.contrasts.set_fields(
"ORSO Contrast",
data=f"Data {test_index}",
bulk_in=f"Bulk In {test_index}",
bulk_out=f"Bulk Out {test_index}",
model=layer_model,
)
data = np.loadtxt(data_path / f"test_{layer_index}.dat")
problem.data.append(name="Data", data=data)

controls = RAT.Controls(procedure="calculate")
output_project, results = RAT.run(orso_project, controls)
problem.custom_files.append(
name="Model",
filename=pathlib.Path(f.name).name,
language="python",
path=pathlib.Path(f.name).parent,
function_name="run_func",
)

plot_ref_sld(output_project, results)
problem.contrasts.append(
name="ORSO Contrast",
background="Background 1",
background_action="add",
resolution="Resolution 1",
scalefactor="Scalefactor 1",
bulk_out="Bulk Out",
bulk_in="Bulk In",
data="Data",
resample=False,
model=["Model"],
)

total_error = sum((results.reflectivity[0][:, 1] - results.shiftedData[0][:, 1]) ** 2)
controls = RAT.Controls()
problem, results = RAT.run(problem, controls)
total_error = sum((results.reflectivity[0][:, 1] - results.shiftedData[0][:, 1]) ** 2)

assert total_error < 1e-10
assert total_error < 1e-10
f.close()
os.remove(f.name)