Skip to content

Commit f50aa6a

Browse files
committed
test: add tests from ess.reflectometry
1 parent 0904f26 commit f50aa6a

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

tests/amor/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)

tests/amor/tools_test.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
# @author Andrew R. McCluskey (arm61)
4+
import pytest
5+
import scipp as sc
6+
7+
from essreflectometry.amor import tools
8+
9+
10+
def test_linlogspace_linear():
11+
q_lin = tools.linlogspace(
12+
dim='qz', edges=[0.008, 0.08], scale='linear', num=50, unit='1/angstrom'
13+
)
14+
expected = sc.linspace(dim='qz', start=0.008, stop=0.08, num=50, unit='1/angstrom')
15+
assert sc.allclose(q_lin, expected)
16+
17+
18+
def test_linlogspace_linear_list_input():
19+
q_lin = tools.linlogspace(
20+
dim='qz', edges=[0.008, 0.08], unit='1/angstrom', scale=['linear'], num=[50]
21+
)
22+
expected = sc.linspace(dim='qz', start=0.008, stop=0.08, num=50, unit='1/angstrom')
23+
assert sc.allclose(q_lin, expected)
24+
25+
26+
def test_linlogspace_log():
27+
q_log = tools.linlogspace(
28+
dim='qz', edges=[0.008, 0.08], unit='1/angstrom', scale='log', num=50
29+
)
30+
expected = sc.geomspace(dim='qz', start=0.008, stop=0.08, num=50, unit='1/angstrom')
31+
assert sc.allclose(q_log, expected)
32+
33+
34+
def test_linlogspace_linear_log():
35+
q_linlog = tools.linlogspace(
36+
dim='qz',
37+
edges=[0.008, 0.03, 0.08],
38+
unit='1/angstrom',
39+
scale=['linear', 'log'],
40+
num=[16, 20],
41+
)
42+
exp_lin = sc.linspace(dim='qz', start=0.008, stop=0.03, num=16, unit='1/angstrom')
43+
exp_log = sc.geomspace(dim='qz', start=0.03, stop=0.08, num=21, unit='1/angstrom')
44+
expected = sc.concat([exp_lin, exp_log['qz', 1:]], 'qz')
45+
assert sc.allclose(q_linlog, expected)
46+
47+
48+
def test_linlogspace_log_linear():
49+
q_loglin = tools.linlogspace(
50+
dim='qz',
51+
edges=[0.008, 0.03, 0.08],
52+
unit='1/angstrom',
53+
scale=['log', 'linear'],
54+
num=[16, 20],
55+
)
56+
exp_log = sc.geomspace(dim='qz', start=0.008, stop=0.03, num=16, unit='1/angstrom')
57+
exp_lin = sc.linspace(dim='qz', start=0.03, stop=0.08, num=21, unit='1/angstrom')
58+
expected = sc.concat([exp_log, exp_lin['qz', 1:]], 'qz')
59+
assert sc.allclose(q_loglin, expected)
60+
61+
62+
def test_linlogspace_linear_log_linear():
63+
q_linloglin = tools.linlogspace(
64+
dim='qz',
65+
edges=[0.008, 0.03, 0.08, 0.12],
66+
unit='1/angstrom',
67+
scale=['linear', 'log', 'linear'],
68+
num=[16, 20, 10],
69+
)
70+
exp_lin = sc.linspace(dim='qz', start=0.008, stop=0.03, num=16, unit='1/angstrom')
71+
exp_log = sc.geomspace(dim='qz', start=0.03, stop=0.08, num=21, unit='1/angstrom')
72+
exp_lin2 = sc.linspace(dim='qz', start=0.08, stop=0.12, num=11, unit='1/angstrom')
73+
expected = sc.concat([exp_lin, exp_log['qz', 1:], exp_lin2['qz', 1:]], 'qz')
74+
assert sc.allclose(q_linloglin, expected)
75+
76+
77+
def test_linlogspace_bad_input():
78+
with pytest.raises(ValueError):
79+
_ = tools.linlogspace(
80+
dim='qz',
81+
edges=[0.008, 0.03, 0.08, 0.12],
82+
unit='1/angstrom',
83+
scale=['linear', 'log'],
84+
num=[16, 20],
85+
)

tests/reflectometry/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
import pytest
4+
import scipp as sc
5+
from orsopy import fileio
6+
7+
from essreflectometry.reflectometry import corrections
8+
9+
10+
def test_normalize_by_counts():
11+
"""
12+
Tests the corrections.normalize_by_counts function without
13+
a orsopy object present.
14+
"""
15+
N = 50
16+
values = [1.0] * N
17+
data = sc.Variable(
18+
dims=['position'], unit=sc.units.counts, values=values, variances=values
19+
)
20+
array = sc.DataArray(data=data)
21+
array_normalized = corrections.normalize_by_counts(array)
22+
result = sc.DataArray(
23+
data=sc.Variable(
24+
dims=['position'],
25+
unit=sc.units.dimensionless,
26+
values=[1 / N] * N,
27+
variances=[1 / (N * N)] * N,
28+
)
29+
)
30+
assert sc.allclose(array_normalized.data, result.data)
31+
32+
33+
def test_normalize_by_counts_fails_when_ncounts_is_too_small():
34+
N = 10
35+
values = [1.0] * N
36+
values[3] = 20.0
37+
data = sc.Variable(
38+
dims=['position'], unit=sc.units.counts, values=values, variances=values
39+
)
40+
array = sc.DataArray(data=data)
41+
with pytest.raises(
42+
ValueError,
43+
match='One or more bins contain a number of counts of the same '
44+
'order as the total number of counts',
45+
):
46+
_ = corrections.normalize_by_counts(array)
47+
48+
49+
def test_normalize_by_counts_orso():
50+
"""
51+
Tests the corrections.normalize_by_counts function
52+
with a orsopy object present.
53+
"""
54+
N = 50
55+
values = [1.0] * N
56+
data = sc.Variable(
57+
dims=['position'], unit=sc.units.counts, values=values, variances=values
58+
)
59+
array = sc.DataArray(data=data, attrs={'orso': sc.scalar(fileio.orso.Orso.empty())})
60+
array.attrs['orso'].value.reduction.corrections = []
61+
array_normalized = corrections.normalize_by_counts(array)
62+
result = sc.DataArray(
63+
data=sc.Variable(
64+
dims=['position'],
65+
unit=sc.units.dimensionless,
66+
values=[1 / N] * N,
67+
variances=[1 / (N * N)] * N,
68+
)
69+
)
70+
assert sc.allclose(array_normalized.data, result.data)
71+
assert 'total counts' in array.attrs['orso'].value.reduction.corrections
72+
73+
74+
def test_beam_on_sample():
75+
"""
76+
Tests the corrections.beam_on_sample function.
77+
"""
78+
beam_size = sc.scalar(1.0, unit=sc.units.mm)
79+
theta = sc.scalar(0.1, unit=sc.units.rad)
80+
expected_result = sc.scalar(10.01668613, unit=sc.units.mm)
81+
assert sc.allclose(expected_result, corrections.beam_on_sample(beam_size, theta))
82+
83+
84+
def test_beam_on_sample_array():
85+
"""
86+
Tests the corrections.beam_on_sample function with an array of theta.
87+
"""
88+
beam_size = sc.scalar(1.0, unit=sc.units.mm)
89+
theta = sc.array(dims=['x'], values=[0.1, 0.5], unit=sc.units.rad)
90+
expected_result = sc.array(
91+
dims=['x'], values=[10.01668613, 2.085829643], unit=sc.units.mm
92+
)
93+
assert sc.allclose(expected_result, corrections.beam_on_sample(beam_size, theta))

0 commit comments

Comments
 (0)