RATapi 0.0.0.dev0__cp39-cp39-win_amd64.whl
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/__init__.py +8 -0
- RATapi/classlist.py +387 -0
- RATapi/controls.py +143 -0
- RATapi/eventManager.dll +0 -0
- RATapi/events.py +70 -0
- RATapi/examples/__init__.py +0 -0
- RATapi/examples/absorption/__init__.py +0 -0
- RATapi/examples/absorption/absorption.py +148 -0
- RATapi/examples/absorption/volume_thiol_bilayer.py +138 -0
- RATapi/examples/data/D2O_spin_down.dat +100 -0
- RATapi/examples/data/D2O_spin_up.dat +101 -0
- RATapi/examples/data/DSPC_D2O.dat +82 -0
- RATapi/examples/data/DSPC_SMW.dat +82 -0
- RATapi/examples/data/H2O_spin_down.dat +102 -0
- RATapi/examples/data/H2O_spin_up.dat +102 -0
- RATapi/examples/data/__init__.py +0 -0
- RATapi/examples/data/c_PLP0016596.dat +146 -0
- RATapi/examples/data/c_PLP0016601.dat +97 -0
- RATapi/examples/data/c_PLP0016607.dat +104 -0
- RATapi/examples/domains/__init__.py +0 -0
- RATapi/examples/domains/alloy_domains.py +26 -0
- RATapi/examples/domains/domains_XY_model.py +70 -0
- RATapi/examples/domains/domains_custom_XY.py +69 -0
- RATapi/examples/domains/domains_custom_layers.py +46 -0
- RATapi/examples/domains/domains_standard_layers.py +75 -0
- RATapi/examples/languages/__init__.py +0 -0
- RATapi/examples/languages/custom_bilayer.py +66 -0
- RATapi/examples/languages/run_custom_file_languages.py +41 -0
- RATapi/examples/languages/setup_problem.py +130 -0
- RATapi/examples/non_polarised/DSPC_custom_XY.py +140 -0
- RATapi/examples/non_polarised/DSPC_custom_layers.py +121 -0
- RATapi/examples/non_polarised/DSPC_standard_layers.py +358 -0
- RATapi/examples/non_polarised/__init__.py +0 -0
- RATapi/examples/non_polarised/custom_XY_DSPC.py +137 -0
- RATapi/examples/non_polarised/custom_bilayer_DSPC.py +85 -0
- RATapi/inputs.py +471 -0
- RATapi/models.py +288 -0
- RATapi/outputs.py +218 -0
- RATapi/project.py +659 -0
- RATapi/rat_core.cp39-win_amd64.pyd +0 -0
- RATapi/run.py +35 -0
- RATapi/utils/__init__.py +0 -0
- RATapi/utils/custom_errors.py +42 -0
- RATapi/utils/enums.py +101 -0
- RATapi/utils/plotting.py +211 -0
- RATapi/wrappers.py +115 -0
- RATapi-0.0.0.dev0.dist-info/METADATA +57 -0
- RATapi-0.0.0.dev0.dist-info/RECORD +64 -0
- RATapi-0.0.0.dev0.dist-info/WHEEL +5 -0
- RATapi-0.0.0.dev0.dist-info/top_level.txt +2 -0
- tests/__init__.py +0 -0
- tests/conftest.py +4728 -0
- tests/test_classlist.py +882 -0
- tests/test_controls.py +863 -0
- tests/test_custom_errors.py +49 -0
- tests/test_events.py +61 -0
- tests/test_inputs.py +908 -0
- tests/test_models.py +308 -0
- tests/test_outputs.py +31 -0
- tests/test_plotting.py +167 -0
- tests/test_project.py +1387 -0
- tests/test_run.py +570 -0
- tests/test_wrappers.py +57 -0
- tests/utils.py +171 -0
tests/test_models.py
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Test the pydantic models."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Callable
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import pydantic
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
import RATapi.models
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.mark.parametrize(
|
|
14
|
+
["model", "model_name", "model_params"],
|
|
15
|
+
[
|
|
16
|
+
(RATapi.models.Background, "Background", {}),
|
|
17
|
+
(RATapi.models.Contrast, "Contrast", {}),
|
|
18
|
+
(RATapi.models.CustomFile, "Custom File", {}),
|
|
19
|
+
(RATapi.models.Data, "Data", {}),
|
|
20
|
+
(RATapi.models.DomainContrast, "Domain Contrast", {}),
|
|
21
|
+
(
|
|
22
|
+
RATapi.models.Layer,
|
|
23
|
+
"Layer",
|
|
24
|
+
{"thickness": "Test Thickness", "SLD": "Test SLD", "roughness": "Test Roughness"},
|
|
25
|
+
),
|
|
26
|
+
(RATapi.models.Parameter, "Parameter", {}),
|
|
27
|
+
(RATapi.models.Resolution, "Resolution", {}),
|
|
28
|
+
],
|
|
29
|
+
)
|
|
30
|
+
def test_default_names(model: Callable, model_name: str, model_params: dict) -> None:
|
|
31
|
+
"""When initialising multiple models without specifying a name, they should be given a default name with the
|
|
32
|
+
format: "New <model name> <integer>".
|
|
33
|
+
"""
|
|
34
|
+
model_1 = model(**model_params)
|
|
35
|
+
model_2 = model(**model_params)
|
|
36
|
+
model_3 = model(name="Given Name", **model_params)
|
|
37
|
+
model_4 = model(**model_params)
|
|
38
|
+
|
|
39
|
+
assert model_1.name == f"New {model_name} 1"
|
|
40
|
+
assert model_2.name == f"New {model_name} 2"
|
|
41
|
+
assert model_3.name == "Given Name"
|
|
42
|
+
assert model_4.name == f"New {model_name} 3"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@pytest.mark.parametrize(
|
|
46
|
+
["model", "model_params"],
|
|
47
|
+
[
|
|
48
|
+
(RATapi.models.Background, {}),
|
|
49
|
+
(RATapi.models.Contrast, {}),
|
|
50
|
+
(RATapi.models.ContrastWithRatio, {}),
|
|
51
|
+
(RATapi.models.CustomFile, {}),
|
|
52
|
+
(RATapi.models.Data, {}),
|
|
53
|
+
(RATapi.models.DomainContrast, {}),
|
|
54
|
+
(RATapi.models.Layer, {"thickness": "Test Thickness", "SLD": "Test SLD", "roughness": "Test Roughness"}),
|
|
55
|
+
(
|
|
56
|
+
RATapi.models.AbsorptionLayer,
|
|
57
|
+
{
|
|
58
|
+
"thickness": "Test Thickness",
|
|
59
|
+
"SLD_real": "Test SLD",
|
|
60
|
+
"SLD_imaginary": "Test SLD",
|
|
61
|
+
"roughness": "Test Roughness",
|
|
62
|
+
},
|
|
63
|
+
),
|
|
64
|
+
(RATapi.models.Parameter, {}),
|
|
65
|
+
(RATapi.models.Resolution, {}),
|
|
66
|
+
],
|
|
67
|
+
)
|
|
68
|
+
class TestModels:
|
|
69
|
+
def test_initialise_with_wrong_type(self, model: Callable, model_params: dict) -> None:
|
|
70
|
+
"""When initialising a model with the wrong type for the "name" field, we should raise a ValidationError."""
|
|
71
|
+
with pytest.raises(
|
|
72
|
+
pydantic.ValidationError,
|
|
73
|
+
match=f"1 validation error for {model.__name__}\nname\n " f"Input should be a valid string",
|
|
74
|
+
):
|
|
75
|
+
model(name=1, **model_params)
|
|
76
|
+
|
|
77
|
+
def test_assignment_with_wrong_type(self, model: Callable, model_params: dict) -> None:
|
|
78
|
+
"""When assigning the "name" field of a model with the wrong type, we should raise a ValidationError."""
|
|
79
|
+
test_model = model(**model_params)
|
|
80
|
+
with pytest.raises(
|
|
81
|
+
pydantic.ValidationError,
|
|
82
|
+
match=f"1 validation error for {model.__name__}\nname\n " f"Input should be a valid string",
|
|
83
|
+
):
|
|
84
|
+
test_model.name = 1
|
|
85
|
+
|
|
86
|
+
def test_initialise_with_zero_length_name(self, model: Callable, model_params: dict) -> None:
|
|
87
|
+
"""When initialising a model with a zero length name, we should raise a ValidationError."""
|
|
88
|
+
with pytest.raises(
|
|
89
|
+
pydantic.ValidationError,
|
|
90
|
+
match=f"1 validation error for {model.__name__}\nname\n " f"String should have at least 1 character",
|
|
91
|
+
):
|
|
92
|
+
model(name="", **model_params)
|
|
93
|
+
|
|
94
|
+
def test_initialise_with_extra_fields(self, model: Callable, model_params: dict) -> None:
|
|
95
|
+
"""When initialising a model with unspecified fields, we should raise a ValidationError."""
|
|
96
|
+
with pytest.raises(
|
|
97
|
+
pydantic.ValidationError,
|
|
98
|
+
match=f"1 validation error for {model.__name__}\nnew_field\n " f"Extra inputs are not permitted",
|
|
99
|
+
):
|
|
100
|
+
model(new_field=1, **model_params)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def test_data_eq() -> None:
|
|
104
|
+
"""If we use the Data.__eq__ method with an object that is not a pydantic BaseModel, we should return
|
|
105
|
+
"NotImplemented".
|
|
106
|
+
"""
|
|
107
|
+
assert RATapi.models.Data().__eq__("data") == NotImplemented
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@pytest.mark.parametrize(
|
|
111
|
+
"input_data",
|
|
112
|
+
[
|
|
113
|
+
(np.array([[1.0, 1.0, 1.0]])),
|
|
114
|
+
],
|
|
115
|
+
)
|
|
116
|
+
def test_data_dimension(input_data: np.ndarray[float]) -> None:
|
|
117
|
+
"""The "data" field of the "Data" model should be a two-dimensional numpy array with at least three values in the
|
|
118
|
+
second dimension.
|
|
119
|
+
"""
|
|
120
|
+
test_data = RATapi.models.Data(data=input_data)
|
|
121
|
+
assert (test_data.data == input_data).all()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@pytest.mark.parametrize(
|
|
125
|
+
"input_data",
|
|
126
|
+
[
|
|
127
|
+
(np.array([])),
|
|
128
|
+
(np.array([1.0, 1.0])),
|
|
129
|
+
],
|
|
130
|
+
)
|
|
131
|
+
def test_data_too_few_dimensions(input_data: np.ndarray[float]) -> None:
|
|
132
|
+
"""If the "data" field of the "Data" model is not a two-dimensional numpy array we should raise a
|
|
133
|
+
ValidationError.
|
|
134
|
+
"""
|
|
135
|
+
with pytest.raises(
|
|
136
|
+
pydantic.ValidationError,
|
|
137
|
+
match='1 validation error for Data\ndata\n Value error, "data" must ' "have at least two dimensions",
|
|
138
|
+
):
|
|
139
|
+
RATapi.models.Data(data=input_data)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@pytest.mark.parametrize(
|
|
143
|
+
"input_data",
|
|
144
|
+
[
|
|
145
|
+
(np.array([[]])),
|
|
146
|
+
(np.array([[1.0]])),
|
|
147
|
+
(np.array([[1.0, 1.0]])),
|
|
148
|
+
],
|
|
149
|
+
)
|
|
150
|
+
def test_data_too_few_values(input_data: np.ndarray[float]) -> None:
|
|
151
|
+
"""If the second dimension of the array in the "data" field of the "Data" model has fewer than three values we
|
|
152
|
+
should raise a ValidationError.
|
|
153
|
+
"""
|
|
154
|
+
with pytest.raises(
|
|
155
|
+
pydantic.ValidationError,
|
|
156
|
+
match='1 validation error for Data\ndata\n Value error, "data" must ' "have at least three columns",
|
|
157
|
+
):
|
|
158
|
+
RATapi.models.Data(data=input_data)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@pytest.mark.parametrize(
|
|
162
|
+
"input_range",
|
|
163
|
+
[
|
|
164
|
+
([1.0, 2.0]),
|
|
165
|
+
],
|
|
166
|
+
)
|
|
167
|
+
def test_data_ranges(input_range: list[float]) -> None:
|
|
168
|
+
"""The "data_range" and "simulation_range" fields of the "Data" model should contain exactly two values."""
|
|
169
|
+
assert RATapi.models.Data(data_range=input_range).data_range == input_range
|
|
170
|
+
assert RATapi.models.Data(simulation_range=input_range).simulation_range == input_range
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@pytest.mark.parametrize(
|
|
174
|
+
"input_range",
|
|
175
|
+
[
|
|
176
|
+
([]),
|
|
177
|
+
([1.0]),
|
|
178
|
+
([1.0, 2.0, 3.0]),
|
|
179
|
+
],
|
|
180
|
+
)
|
|
181
|
+
def test_two_values_in_data_range(input_range: list[float]) -> None:
|
|
182
|
+
"""If the "data_range" field of the "Data" model contains more or less than two values, we should raise a
|
|
183
|
+
ValidationError.
|
|
184
|
+
"""
|
|
185
|
+
with pytest.raises(
|
|
186
|
+
pydantic.ValidationError,
|
|
187
|
+
match=f'1 validation error for Data\ndata_range\n List should have '
|
|
188
|
+
f'at {"least" if len(input_range) < 2 else "most"} 2 items '
|
|
189
|
+
f'after validation, not {len(input_range)}',
|
|
190
|
+
):
|
|
191
|
+
RATapi.models.Data(data_range=input_range)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
@pytest.mark.parametrize(
|
|
195
|
+
"input_range",
|
|
196
|
+
[
|
|
197
|
+
([]),
|
|
198
|
+
([1.0]),
|
|
199
|
+
([1.0, 2.0, 3.0]),
|
|
200
|
+
],
|
|
201
|
+
)
|
|
202
|
+
def test_two_values_in_simulation_range(input_range: list[float]) -> None:
|
|
203
|
+
"""If the "simulation_range" field of the "Data" model contains more or less than two values, we should raise a
|
|
204
|
+
ValidationError.
|
|
205
|
+
"""
|
|
206
|
+
with pytest.raises(
|
|
207
|
+
pydantic.ValidationError,
|
|
208
|
+
match=f'1 validation error for Data\nsimulation_range\n List should '
|
|
209
|
+
f'have at {"least" if len(input_range) < 2 else "most"} 2 items '
|
|
210
|
+
f'after validation, not {len(input_range)}',
|
|
211
|
+
):
|
|
212
|
+
RATapi.models.Data(simulation_range=input_range)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@pytest.mark.parametrize(
|
|
216
|
+
"field",
|
|
217
|
+
[
|
|
218
|
+
"data_range",
|
|
219
|
+
"simulation_range",
|
|
220
|
+
],
|
|
221
|
+
)
|
|
222
|
+
def test_min_max_in_range(field: str) -> None:
|
|
223
|
+
"""If the maximum value of the "data_range" or "simulation_range" fields of the "Data" model is greater than the
|
|
224
|
+
minimum value, we should raise a ValidationError.
|
|
225
|
+
"""
|
|
226
|
+
with pytest.raises(
|
|
227
|
+
pydantic.ValidationError,
|
|
228
|
+
match=f"1 validation error for Data\n{field}\n Value error, {field} "
|
|
229
|
+
f'"min" value is greater than the "max" value',
|
|
230
|
+
):
|
|
231
|
+
RATapi.models.Data(**{field: [1.0, 0.0]})
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_default_ranges() -> None:
|
|
235
|
+
"""If "data" is specified but either the "data_range" or "simulation_range" fields are not, we set the ranges to
|
|
236
|
+
the minimum and maximum values of the first column of the data.
|
|
237
|
+
"""
|
|
238
|
+
test_data = RATapi.models.Data(data=np.array([[1.0, 0.0, 0.0], [3.0, 0.0, 0.0]]))
|
|
239
|
+
assert test_data.data_range == [1.0, 3.0]
|
|
240
|
+
assert test_data.simulation_range == [1.0, 3.0]
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
@pytest.mark.parametrize(
|
|
244
|
+
"test_range",
|
|
245
|
+
[
|
|
246
|
+
[0.0, 2.0],
|
|
247
|
+
[2.0, 4.0],
|
|
248
|
+
[0.0, 4.0],
|
|
249
|
+
],
|
|
250
|
+
)
|
|
251
|
+
def test_data_range(test_range) -> None:
|
|
252
|
+
"""If "data" is specified but the "data_range" lies outside of the limits of the data we should raise a
|
|
253
|
+
ValidationError.
|
|
254
|
+
"""
|
|
255
|
+
with pytest.raises(
|
|
256
|
+
pydantic.ValidationError,
|
|
257
|
+
match=re.escape(
|
|
258
|
+
f"1 validation error for Data\n Value error, The "
|
|
259
|
+
f"data_range value of: {test_range} must lie within "
|
|
260
|
+
f"the min/max values of the data: [1.0, 3.0]",
|
|
261
|
+
),
|
|
262
|
+
):
|
|
263
|
+
RATapi.models.Data(data=np.array([[1.0, 0.0, 0.0], [3.0, 0.0, 0.0]]), data_range=test_range)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
@pytest.mark.parametrize(
|
|
267
|
+
"test_range",
|
|
268
|
+
[
|
|
269
|
+
[0.0, 2.0],
|
|
270
|
+
[2.0, 4.0],
|
|
271
|
+
[1.5, 2.5],
|
|
272
|
+
],
|
|
273
|
+
)
|
|
274
|
+
def test_simulation_range(test_range) -> None:
|
|
275
|
+
"""If "data" is specified but the "simulation_range" lies within the limits of the data we should raise a
|
|
276
|
+
ValidationError.
|
|
277
|
+
"""
|
|
278
|
+
with pytest.raises(
|
|
279
|
+
pydantic.ValidationError,
|
|
280
|
+
match=re.escape(
|
|
281
|
+
f"1 validation error for Data\n Value error, The "
|
|
282
|
+
f"simulation_range value of: {test_range} must lie "
|
|
283
|
+
f"outside of the min/max values of the data: "
|
|
284
|
+
f"[1.0, 3.0]",
|
|
285
|
+
),
|
|
286
|
+
):
|
|
287
|
+
RATapi.models.Data(data=np.array([[1.0, 0.0, 0.0], [3.0, 0.0, 0.0]]), simulation_range=test_range)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@pytest.mark.parametrize(
|
|
291
|
+
["minimum", "value", "maximum"],
|
|
292
|
+
[
|
|
293
|
+
(0.0, 2.0, 1.0),
|
|
294
|
+
(0, 1, 0),
|
|
295
|
+
(1, -1, 1),
|
|
296
|
+
],
|
|
297
|
+
)
|
|
298
|
+
def test_parameter_range(minimum: float, value: float, maximum: float) -> None:
|
|
299
|
+
"""For the "Parameter" model, if the value of the "value" field does not lie with the values given in the "min" and
|
|
300
|
+
"max" fields, we should raise a ValidationError.
|
|
301
|
+
"""
|
|
302
|
+
with pytest.raises(
|
|
303
|
+
pydantic.ValidationError,
|
|
304
|
+
match=f"1 validation error for Parameter\n Value error, value "
|
|
305
|
+
f"{float(value)} is not within the defined range: "
|
|
306
|
+
f"{float(minimum)} <= value <= {float(maximum)}",
|
|
307
|
+
):
|
|
308
|
+
RATapi.models.Parameter(min=minimum, value=value, max=maximum)
|
tests/test_outputs.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Test the outputs module using the example calculation from "DSPC_standard_layers.py".
|
|
2
|
+
|
|
3
|
+
We use the example for both a reflectivity calculation, and Bayesian analysis using the Dream algorithm.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
import RATapi
|
|
9
|
+
import RATapi.outputs
|
|
10
|
+
import RATapi.rat_core
|
|
11
|
+
from RATapi.utils.enums import Procedures
|
|
12
|
+
from tests.utils import check_results_equal
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.mark.parametrize(
|
|
16
|
+
["test_procedure", "test_output_results", "test_bayes", "test_results"],
|
|
17
|
+
[
|
|
18
|
+
(Procedures.Calculate, "reflectivity_calculation_output_results", None, "reflectivity_calculation_results"),
|
|
19
|
+
(Procedures.Dream, "dream_output_results", "dream_bayes", "dream_results"),
|
|
20
|
+
],
|
|
21
|
+
)
|
|
22
|
+
def test_make_results(test_procedure, test_output_results, test_bayes, test_results, request) -> None:
|
|
23
|
+
"""The python results object should contain the relevant parameters defined in the C++ results and bayes objects."""
|
|
24
|
+
test_output_results = request.getfixturevalue(test_output_results)
|
|
25
|
+
if test_bayes:
|
|
26
|
+
test_bayes = request.getfixturevalue(test_bayes)
|
|
27
|
+
|
|
28
|
+
test_results = request.getfixturevalue(test_results)
|
|
29
|
+
results = RATapi.outputs.make_results(test_procedure, test_output_results, test_bayes)
|
|
30
|
+
|
|
31
|
+
check_results_equal(test_results, results)
|
tests/test_plotting.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pickle
|
|
3
|
+
import re
|
|
4
|
+
from unittest.mock import MagicMock, patch
|
|
5
|
+
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from RATapi.rat_core import PlotEventData
|
|
10
|
+
from RATapi.utils.plotting import Figure, plot_ref_sld_helper
|
|
11
|
+
|
|
12
|
+
TEST_DIR_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def data() -> PlotEventData:
|
|
16
|
+
"""Creates the fixture for the tests."""
|
|
17
|
+
data_path = os.path.join(TEST_DIR_PATH, "plotting_data.pickle")
|
|
18
|
+
with open(data_path, "rb") as f:
|
|
19
|
+
loaded_data = pickle.load(f)
|
|
20
|
+
|
|
21
|
+
data = PlotEventData()
|
|
22
|
+
data.modelType = loaded_data["modelType"]
|
|
23
|
+
data.dataPresent = loaded_data["dataPresent"]
|
|
24
|
+
data.subRoughs = loaded_data["subRoughs"]
|
|
25
|
+
data.resample = loaded_data["resample"]
|
|
26
|
+
data.resampledLayers = loaded_data["resampledLayers"]
|
|
27
|
+
data.reflectivity = loaded_data["reflectivity"]
|
|
28
|
+
data.shiftedData = loaded_data["shiftedData"]
|
|
29
|
+
data.sldProfiles = loaded_data["sldProfiles"]
|
|
30
|
+
return data
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest.fixture
|
|
34
|
+
def fig() -> Figure:
|
|
35
|
+
"""Creates the fixture for the tests."""
|
|
36
|
+
plt.close("all")
|
|
37
|
+
figure = Figure(1, 3)
|
|
38
|
+
fig = plot_ref_sld_helper(fig=figure, data=data())
|
|
39
|
+
return fig
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_figure_axis_formating(fig: Figure) -> None:
|
|
43
|
+
"""Tests the axis formating of the figure."""
|
|
44
|
+
ref_plot = fig._ax[0]
|
|
45
|
+
sld_plot = fig._ax[1]
|
|
46
|
+
|
|
47
|
+
assert fig._fig.axes[0].get_subplotspec().get_gridspec().get_geometry() == (1, 2)
|
|
48
|
+
assert fig._ax.shape == (2,)
|
|
49
|
+
|
|
50
|
+
assert ref_plot.get_xlabel() == "Qz"
|
|
51
|
+
assert ref_plot.get_xscale() == "log"
|
|
52
|
+
assert ref_plot.get_ylabel() == "Ref"
|
|
53
|
+
assert ref_plot.get_yscale() == "log"
|
|
54
|
+
assert [label._text for label in ref_plot.get_legend().texts] == ["ref 1", "ref 2", "ref 3"]
|
|
55
|
+
|
|
56
|
+
assert sld_plot.get_xlabel() == "Z"
|
|
57
|
+
assert sld_plot.get_xscale() == "linear"
|
|
58
|
+
assert sld_plot.get_ylabel() == "SLD"
|
|
59
|
+
assert sld_plot.get_yscale() == "linear"
|
|
60
|
+
assert [label._text for label in sld_plot.get_legend().texts] == ["sld 1", "sld 2", "sld 3"]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_figure_color_formating(fig: Figure) -> None:
|
|
64
|
+
"""Tests the color formating of the figure."""
|
|
65
|
+
ref_plot = fig._ax[0]
|
|
66
|
+
sld_plot = fig._ax[1]
|
|
67
|
+
|
|
68
|
+
assert len(ref_plot.get_lines()) == 3
|
|
69
|
+
assert len(sld_plot.get_lines()) == 6
|
|
70
|
+
|
|
71
|
+
for axis_ix in range(len(ref_plot.get_lines())):
|
|
72
|
+
ax1 = axis_ix * 2
|
|
73
|
+
ax2 = ax1 + 1
|
|
74
|
+
|
|
75
|
+
# Tests whether the color of the line and the errorbars match on the ref_plot
|
|
76
|
+
assert (
|
|
77
|
+
ref_plot.containers[ax1][2][0]._original_edgecolor
|
|
78
|
+
== ref_plot.containers[ax2][2][0]._original_edgecolor
|
|
79
|
+
== ref_plot.get_lines()[axis_ix].get_color()
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
# Tests whether the color of the sld and resampled_sld match on the sld_plot
|
|
83
|
+
assert sld_plot.get_lines()[ax1].get_color() == sld_plot.get_lines()[ax2].get_color()
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_eventhandlers_linked_to_figure(fig: Figure) -> None:
|
|
87
|
+
"""Tests whether the eventhandlers for close_event
|
|
88
|
+
and key_press_event in the figure are linked to the
|
|
89
|
+
class methods.
|
|
90
|
+
"""
|
|
91
|
+
pattern = r"\(([^\]]+)\)"
|
|
92
|
+
index = 0
|
|
93
|
+
|
|
94
|
+
for ix, val in fig._fig.canvas.callbacks.callbacks["close_event"].items():
|
|
95
|
+
if str(type(val)) == "<class 'weakref.WeakMethod'>":
|
|
96
|
+
index = ix
|
|
97
|
+
break
|
|
98
|
+
canvas_close_event_callback = fig._fig.canvas.callbacks.callbacks["close_event"][index]._func_ref.__repr__()
|
|
99
|
+
close_event_callback = re.findall(pattern, canvas_close_event_callback)[0]
|
|
100
|
+
assert close_event_callback == "_close"
|
|
101
|
+
assert hasattr(Figure, "_close")
|
|
102
|
+
|
|
103
|
+
for ix, val in fig._fig.canvas.callbacks.callbacks["key_press_event"].items():
|
|
104
|
+
if str(type(val)) == "<class 'weakref.WeakMethod'>":
|
|
105
|
+
index = ix
|
|
106
|
+
break
|
|
107
|
+
canvas_key_press_event_callback = fig._fig.canvas.callbacks.callbacks["key_press_event"][index]._func_ref.__repr__()
|
|
108
|
+
key_press_event_callback = re.findall(pattern, canvas_key_press_event_callback)[0]
|
|
109
|
+
assert key_press_event_callback == "_process_button_press"
|
|
110
|
+
assert hasattr(Figure, "_process_button_press")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_eventhandler_variable_update(fig: Figure) -> None:
|
|
114
|
+
"""Tests whether the eventhandlers for close_event
|
|
115
|
+
and key_press_event update variables that stop
|
|
116
|
+
while loop in wait_for_close.
|
|
117
|
+
"""
|
|
118
|
+
assert not fig._esc_pressed
|
|
119
|
+
on_key_mock_event = type("MockEvent", (object,), {"key": "escape"})
|
|
120
|
+
fig._process_button_press(on_key_mock_event)
|
|
121
|
+
assert fig._esc_pressed
|
|
122
|
+
|
|
123
|
+
assert not fig._close_clicked
|
|
124
|
+
fig._close("test")
|
|
125
|
+
assert fig._close_clicked
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@patch("RATapi.utils.plotting.plt.waitforbuttonpress")
|
|
129
|
+
def test_wait_for_close(mock: MagicMock, fig: Figure) -> None:
|
|
130
|
+
"""Tests the _wait_for_close method stops the
|
|
131
|
+
while loop when _esc_pressed is True.
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def mock_wait_for_button_press(timeout):
|
|
135
|
+
fig._esc_pressed = True
|
|
136
|
+
|
|
137
|
+
mock.side_effect = mock_wait_for_button_press
|
|
138
|
+
assert not fig._esc_pressed
|
|
139
|
+
fig.wait_for_close()
|
|
140
|
+
assert fig._esc_pressed
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@patch("RATapi.utils.plotting.makeSLDProfileXY")
|
|
144
|
+
def test_sld_profile_function_call(mock: MagicMock) -> None:
|
|
145
|
+
"""Tests the makeSLDProfileXY function called with
|
|
146
|
+
correct args.
|
|
147
|
+
"""
|
|
148
|
+
plot_ref_sld_helper(data())
|
|
149
|
+
|
|
150
|
+
assert mock.call_count == 3
|
|
151
|
+
assert mock.call_args_list[0].args[0] == 2.07e-06
|
|
152
|
+
assert mock.call_args_list[0].args[1] == 6.28e-06
|
|
153
|
+
assert mock.call_args_list[0].args[2] == 0.0
|
|
154
|
+
assert mock.call_args_list[0].args[4] == 82
|
|
155
|
+
assert mock.call_args_list[0].args[5] == 1.0
|
|
156
|
+
|
|
157
|
+
assert mock.call_args_list[1].args[0] == 2.07e-06
|
|
158
|
+
assert mock.call_args_list[1].args[1] == 1.83e-06
|
|
159
|
+
assert mock.call_args_list[1].args[2] == 0.0
|
|
160
|
+
assert mock.call_args_list[1].args[4] == 128
|
|
161
|
+
assert mock.call_args_list[1].args[5] == 1.0
|
|
162
|
+
|
|
163
|
+
assert mock.call_args_list[2].args[0] == 2.07e-06
|
|
164
|
+
assert mock.call_args_list[2].args[1] == -5.87e-07
|
|
165
|
+
assert mock.call_args_list[2].args[2] == 0.0
|
|
166
|
+
assert mock.call_args_list[2].args[4] == 153
|
|
167
|
+
assert mock.call_args_list[2].args[5] == 1.0
|