Skip to content

Commit e3bb2c1

Browse files
committed
refactor: move test code to tests, add docstrings
1 parent d22df0f commit e3bb2c1

File tree

2 files changed

+88
-56
lines changed

2 files changed

+88
-56
lines changed

src/ess/estia/calibration.py

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010

1111
def solve_for_calibration_parameters(Io, Is):
12+
"""
13+
Solves for the calibration parameters given the reference
14+
measurements.
15+
16+
See https://doi.org/10.1016/S0921-4526(00)00823-1.
17+
"""
1218
Iopp, Iopa, Ioap, Ioaa = Io
1319
Ipp, Ipa, Iap, Iaa = Is
1420

@@ -53,51 +59,11 @@ def solve_for_calibration_parameters(Io, Is):
5359
return I0 / 4, Pp, Pa, Ap, Aa, Rspp, Rsaa
5460

5561

56-
def generate_valid_calibration_parameters():
57-
I0 = np.random.random()
58-
Pp = np.random.random()
59-
Pa = -np.random.random()
60-
Ap = np.random.random()
61-
Aa = -np.random.random()
62-
Rspp = np.random.random()
63-
Rsaa = Rspp * np.random.random()
64-
return tuple(map(sc.scalar, (I0, Pp, Pa, Ap, Aa, Rspp, Rsaa)))
65-
66-
67-
def intensity_from_parameters(I0, Pp, Pa, Ap, Aa, Rpp, Rpa, Rap, Raa):
68-
return (
69-
I0
70-
* (
71-
Rpp * (1 + Ap) * (1 + Pp)
72-
+ Rpa * (1 - Ap) * (1 + Pp)
73-
+ Rap * (1 + Ap) * (1 - Pp)
74-
+ Raa * (1 - Ap) * (1 - Pp)
75-
),
76-
I0
77-
* (
78-
Rpp * (1 + Aa) * (1 + Pp)
79-
+ Rpa * (1 - Aa) * (1 + Pp)
80-
+ Rap * (1 + Aa) * (1 - Pp)
81-
+ Raa * (1 - Aa) * (1 - Pp)
82-
),
83-
I0
84-
* (
85-
Rpp * (1 + Ap) * (1 + Pa)
86-
+ Rpa * (1 - Ap) * (1 + Pa)
87-
+ Rap * (1 + Ap) * (1 - Pa)
88-
+ Raa * (1 - Ap) * (1 - Pa)
89-
),
90-
I0
91-
* (
92-
Rpp * (1 + Aa) * (1 + Pa)
93-
+ Rpa * (1 - Aa) * (1 + Pa)
94-
+ Rap * (1 + Aa) * (1 - Pa)
95-
+ Raa * (1 - Aa) * (1 - Pa)
96-
),
97-
)
98-
99-
10062
def correction_matrix(Pp, Pa, Ap, Aa):
63+
"""
64+
Defines the linear relationship between measured intensity
65+
and reflectivity.
66+
"""
10167
return [
10268
[
10369
(1 + Pp) * (1 + Ap),
@@ -126,48 +92,68 @@ def correction_matrix(Pp, Pa, Ap, Aa):
12692
]
12793

12894

129-
def compute_calibration_factors(Io, Is):
95+
def calibration_factors_from_reference_measurements(Io, Is):
96+
"""
97+
Computes the polarization instrument parameters from
98+
the calibration measurements on the non-magnetic reference
99+
and the calibration measurements on the magnetic reference.
100+
"""
130101
I0, Pp, Pa, Ap, Aa, _, _ = solve_for_calibration_parameters(Io, Is)
131102
return I0, correction_matrix(Pp, Pa, Ap, Aa)
132103

133104

105+
def _linsolve(A, b):
106+
x = np.linalg.solve(
107+
np.stack([np.stack(row, -1) for row in A], -2),
108+
np.stack(b, -1)[..., None],
109+
)[..., 0]
110+
return np.moveaxis(x, -1, 0)
111+
112+
134113
def linsolve(A, b):
135-
return np.linalg.solve(
136-
np.stack([[a.values for a in row] for row in A]),
137-
np.stack([bi.values for bi in b], axis=-1),
114+
x = _linsolve(
115+
[[a.values for a in row] for row in A],
116+
[bi.values for bi in b],
138117
)
118+
return [sc.array(dims=b[0].dims, values=xi) for xi in x]
139119

140120

141-
def computer_reflectivity_calibrate_on_q(
121+
def compute_reflectivity_calibrate_on_q(
142122
reference_supermirror,
143123
reference_polarized_supermirror,
144124
sample,
145125
qbins,
146126
):
127+
"""Reduces reference and sample to Q before applying
128+
the polarization correction and normalization."""
147129
reference_supermirror = [
148130
reduce_from_lz_to_q(i, qbins) for i in reference_supermirror
149131
]
150132
reference_polarized_supermirror = [
151133
reduce_from_lz_to_q(i, qbins) for i in reference_polarized_supermirror
152134
]
153-
sample = [reduce_from_events_to_q(i, qbins) for i in sample]
154-
I0, C = compute_calibration_factors(
135+
I0, C = calibration_factors_from_reference_measurements(
155136
reference_supermirror, reference_polarized_supermirror
156137
)
157-
return [i / I0 for i in linsolve(C, sample)]
138+
sample = [reduce_from_events_to_q(i, qbins) for i in sample]
139+
sample = linsolve(C, sample)
140+
return [i / I0 for i in sample]
158141

159142

160-
def computer_reflectivity_calibrate_on_lz(
143+
def compute_reflectivity_calibrate_on_lz(
161144
reference_supermirror,
162145
reference_polarized_supermirror,
163146
sample,
164147
wbins,
165148
qbins,
166149
):
167-
sample = reduce_from_events_to_lz(sample, wbins)
168-
I0, C = compute_calibration_factors(
150+
"""Applied the polarization correction on the wavelength-z grid
151+
then reduces to Q to apply the normalization."""
152+
sample = [reduce_from_events_to_lz(s, wbins) for s in sample]
153+
I0, C = calibration_factors_from_reference_measurements(
169154
reference_supermirror, reference_polarized_supermirror
170155
)
171156
sample = linsolve(C, sample)
157+
sample = [reduce_from_lz_to_q(s, qbins) for s in sample]
172158
I0 = reduce_from_lz_to_q(I0, qbins)
173-
return [i / I0 for i in reduce_from_lz_to_q(sample, qbins)]
159+
return [i / I0 for i in sample]

tests/estia/calibration_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
import scipp as sc
3+
4+
5+
def generate_valid_calibration_parameters():
6+
I0 = np.random.random()
7+
Pp = np.random.random()
8+
Pa = -np.random.random()
9+
Ap = np.random.random()
10+
Aa = -np.random.random()
11+
Rspp = np.random.random()
12+
Rsaa = Rspp * np.random.random()
13+
return tuple(map(sc.scalar, (I0, Pp, Pa, Ap, Aa, Rspp, Rsaa)))
14+
15+
16+
def intensity_from_parameters(I0, Pp, Pa, Ap, Aa, Rpp, Rpa, Rap, Raa):
17+
return (
18+
I0
19+
* (
20+
Rpp * (1 + Ap) * (1 + Pp)
21+
+ Rpa * (1 - Ap) * (1 + Pp)
22+
+ Rap * (1 + Ap) * (1 - Pp)
23+
+ Raa * (1 - Ap) * (1 - Pp)
24+
),
25+
I0
26+
* (
27+
Rpp * (1 + Aa) * (1 + Pp)
28+
+ Rpa * (1 - Aa) * (1 + Pp)
29+
+ Rap * (1 + Aa) * (1 - Pp)
30+
+ Raa * (1 - Aa) * (1 - Pp)
31+
),
32+
I0
33+
* (
34+
Rpp * (1 + Ap) * (1 + Pa)
35+
+ Rpa * (1 - Ap) * (1 + Pa)
36+
+ Rap * (1 + Ap) * (1 - Pa)
37+
+ Raa * (1 - Ap) * (1 - Pa)
38+
),
39+
I0
40+
* (
41+
Rpp * (1 + Aa) * (1 + Pa)
42+
+ Rpa * (1 - Aa) * (1 + Pa)
43+
+ Rap * (1 + Aa) * (1 - Pa)
44+
+ Raa * (1 - Aa) * (1 - Pa)
45+
),
46+
)

0 commit comments

Comments
 (0)