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.
Files changed (64) hide show
  1. RATapi/__init__.py +8 -0
  2. RATapi/classlist.py +387 -0
  3. RATapi/controls.py +143 -0
  4. RATapi/eventManager.dll +0 -0
  5. RATapi/events.py +70 -0
  6. RATapi/examples/__init__.py +0 -0
  7. RATapi/examples/absorption/__init__.py +0 -0
  8. RATapi/examples/absorption/absorption.py +148 -0
  9. RATapi/examples/absorption/volume_thiol_bilayer.py +138 -0
  10. RATapi/examples/data/D2O_spin_down.dat +100 -0
  11. RATapi/examples/data/D2O_spin_up.dat +101 -0
  12. RATapi/examples/data/DSPC_D2O.dat +82 -0
  13. RATapi/examples/data/DSPC_SMW.dat +82 -0
  14. RATapi/examples/data/H2O_spin_down.dat +102 -0
  15. RATapi/examples/data/H2O_spin_up.dat +102 -0
  16. RATapi/examples/data/__init__.py +0 -0
  17. RATapi/examples/data/c_PLP0016596.dat +146 -0
  18. RATapi/examples/data/c_PLP0016601.dat +97 -0
  19. RATapi/examples/data/c_PLP0016607.dat +104 -0
  20. RATapi/examples/domains/__init__.py +0 -0
  21. RATapi/examples/domains/alloy_domains.py +26 -0
  22. RATapi/examples/domains/domains_XY_model.py +70 -0
  23. RATapi/examples/domains/domains_custom_XY.py +69 -0
  24. RATapi/examples/domains/domains_custom_layers.py +46 -0
  25. RATapi/examples/domains/domains_standard_layers.py +75 -0
  26. RATapi/examples/languages/__init__.py +0 -0
  27. RATapi/examples/languages/custom_bilayer.py +66 -0
  28. RATapi/examples/languages/run_custom_file_languages.py +41 -0
  29. RATapi/examples/languages/setup_problem.py +130 -0
  30. RATapi/examples/non_polarised/DSPC_custom_XY.py +140 -0
  31. RATapi/examples/non_polarised/DSPC_custom_layers.py +121 -0
  32. RATapi/examples/non_polarised/DSPC_standard_layers.py +358 -0
  33. RATapi/examples/non_polarised/__init__.py +0 -0
  34. RATapi/examples/non_polarised/custom_XY_DSPC.py +137 -0
  35. RATapi/examples/non_polarised/custom_bilayer_DSPC.py +85 -0
  36. RATapi/inputs.py +471 -0
  37. RATapi/models.py +288 -0
  38. RATapi/outputs.py +218 -0
  39. RATapi/project.py +659 -0
  40. RATapi/rat_core.cp39-win_amd64.pyd +0 -0
  41. RATapi/run.py +35 -0
  42. RATapi/utils/__init__.py +0 -0
  43. RATapi/utils/custom_errors.py +42 -0
  44. RATapi/utils/enums.py +101 -0
  45. RATapi/utils/plotting.py +211 -0
  46. RATapi/wrappers.py +115 -0
  47. RATapi-0.0.0.dev0.dist-info/METADATA +57 -0
  48. RATapi-0.0.0.dev0.dist-info/RECORD +64 -0
  49. RATapi-0.0.0.dev0.dist-info/WHEEL +5 -0
  50. RATapi-0.0.0.dev0.dist-info/top_level.txt +2 -0
  51. tests/__init__.py +0 -0
  52. tests/conftest.py +4728 -0
  53. tests/test_classlist.py +882 -0
  54. tests/test_controls.py +863 -0
  55. tests/test_custom_errors.py +49 -0
  56. tests/test_events.py +61 -0
  57. tests/test_inputs.py +908 -0
  58. tests/test_models.py +308 -0
  59. tests/test_outputs.py +31 -0
  60. tests/test_plotting.py +167 -0
  61. tests/test_project.py +1387 -0
  62. tests/test_run.py +570 -0
  63. tests/test_wrappers.py +57 -0
  64. tests/utils.py +171 -0
tests/test_controls.py ADDED
@@ -0,0 +1,863 @@
1
+ """Test the controls module."""
2
+
3
+ from typing import Any, Union
4
+
5
+ import pydantic
6
+ import pytest
7
+
8
+ from RATapi.controls import Controls, fields
9
+ from RATapi.utils.enums import BoundHandling, Display, Parallel, Procedures, Strategies
10
+
11
+
12
+ def test_initialise_procedure_error() -> None:
13
+ """Tests for a ValidationError if the procedure property of the Controls class is initialised with an invalid
14
+ value.
15
+ """
16
+ with pytest.raises(pydantic.ValidationError, match="Input should be 'calculate', 'simplex', 'de', 'ns' or 'dream'"):
17
+ Controls(procedure="test")
18
+
19
+
20
+ def test_set_procedure_error() -> None:
21
+ """Tests for a ValidationError if the procedure property of the Controls class is set to an invalid value."""
22
+ controls = Controls()
23
+ with pytest.raises(pydantic.ValidationError, match="Input should be 'calculate', 'simplex', 'de', 'ns' or 'dream'"):
24
+ controls.procedure = "test"
25
+
26
+
27
+ def test_extra_property_error() -> None:
28
+ """Tests the extra property setter in the Controls class."""
29
+ controls = Controls()
30
+ with pytest.raises(pydantic.ValidationError, match="Object has no attribute 'test'"):
31
+ controls.test = 1
32
+
33
+
34
+ class TestCalculate:
35
+ """Tests the Calculate class."""
36
+
37
+ @pytest.fixture(autouse=True)
38
+ def setup_class(self):
39
+ self.calculate = Controls()
40
+
41
+ @pytest.fixture
42
+ def table_str(self):
43
+ table_str = (
44
+ "+------------------+-----------+\n"
45
+ "| Property | Value |\n"
46
+ "+------------------+-----------+\n"
47
+ "| procedure | calculate |\n"
48
+ "| parallel | single |\n"
49
+ "| calcSldDuringFit | False |\n"
50
+ "| resampleParams | [0.9, 50] |\n"
51
+ "| display | iter |\n"
52
+ "+------------------+-----------+"
53
+ )
54
+
55
+ return table_str
56
+
57
+ @pytest.mark.parametrize(
58
+ "control_property, value",
59
+ [
60
+ ("parallel", Parallel.Single),
61
+ ("calcSldDuringFit", False),
62
+ ("resampleParams", [0.9, 50]),
63
+ ("display", Display.Iter),
64
+ ("procedure", Procedures.Calculate),
65
+ ],
66
+ )
67
+ def test_calculate_property_values(self, control_property: str, value: Any) -> None:
68
+ """Tests the default values of Calculate class."""
69
+ assert getattr(self.calculate, control_property) == value
70
+
71
+ @pytest.mark.parametrize(
72
+ "control_property, value",
73
+ [
74
+ ("parallel", Parallel.Points),
75
+ ("calcSldDuringFit", True),
76
+ ("resampleParams", [0.2, 1]),
77
+ ("display", Display.Notify),
78
+ ],
79
+ )
80
+ def test_calculate_property_setters(self, control_property: str, value: Any) -> None:
81
+ """Tests the setters of Calculate class."""
82
+ setattr(self.calculate, control_property, value)
83
+ assert getattr(self.calculate, control_property) == value
84
+
85
+ @pytest.mark.parametrize(
86
+ "wrong_property, value",
87
+ [
88
+ ("xTolerance", 4e-6),
89
+ ("funcTolerance", 3e-4),
90
+ ("maxFuncEvals", 100),
91
+ ("maxIterations", 50),
92
+ ("updateFreq", 4),
93
+ ("updatePlotFreq", 3),
94
+ ("populationSize", 200),
95
+ ("fWeight", 0.3),
96
+ ("crossoverProbability", 0.4),
97
+ ("strategy", Strategies.BestWithJitter),
98
+ ("targetValue", 2.0),
99
+ ("numGenerations", 50),
100
+ ("nLive", 1500),
101
+ ("nMCMC", 1),
102
+ ("propScale", 0.5),
103
+ ("nsTolerance", 0.8),
104
+ ("nSamples", 500),
105
+ ("nChains", 1000),
106
+ ("jumpProbability", 0.7),
107
+ ("pUnitGamma", 0.3),
108
+ ("boundHandling", BoundHandling.Fold),
109
+ ("adaptPCR", False),
110
+ ],
111
+ )
112
+ def test_initialise_non_calculate_properties(self, wrong_property: str, value: Any) -> None:
113
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
114
+ with pytest.warns(
115
+ UserWarning,
116
+ match=f'\nThe current controls procedure is "calculate", but the property'
117
+ f' "{wrong_property}" applies instead to the'
118
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
119
+ f' The fields for the "calculate" controls procedure are:\n'
120
+ f' {", ".join(fields["calculate"])}\n',
121
+ ):
122
+ Controls(procedure="calculate", **{wrong_property: value})
123
+
124
+ @pytest.mark.parametrize(
125
+ "wrong_property, value",
126
+ [
127
+ ("xTolerance", 4e-6),
128
+ ("funcTolerance", 3e-4),
129
+ ("maxFuncEvals", 100),
130
+ ("maxIterations", 50),
131
+ ("updateFreq", 4),
132
+ ("updatePlotFreq", 3),
133
+ ("populationSize", 200),
134
+ ("fWeight", 0.3),
135
+ ("crossoverProbability", 0.4),
136
+ ("strategy", Strategies.BestWithJitter),
137
+ ("targetValue", 2.0),
138
+ ("numGenerations", 50),
139
+ ("nLive", 1500),
140
+ ("nMCMC", 1),
141
+ ("propScale", 0.5),
142
+ ("nsTolerance", 0.8),
143
+ ("nSamples", 500),
144
+ ("nChains", 1000),
145
+ ("jumpProbability", 0.7),
146
+ ("pUnitGamma", 0.3),
147
+ ("boundHandling", BoundHandling.Fold),
148
+ ("adaptPCR", False),
149
+ ],
150
+ )
151
+ def test_set_non_calculate_properties(self, wrong_property: str, value: Any) -> None:
152
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
153
+ with pytest.warns(
154
+ UserWarning,
155
+ match=f'\nThe current controls procedure is "calculate", but the property'
156
+ f' "{wrong_property}" applies instead to the'
157
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
158
+ f' The fields for the "calculate" controls procedure are:\n'
159
+ f' {", ".join(fields["calculate"])}\n',
160
+ ):
161
+ setattr(self.calculate, wrong_property, value)
162
+
163
+ @pytest.mark.parametrize("value", ["test", "ALL", "Contrast", True, 1, 3.0])
164
+ def test_calculate_parallel_validation(self, value: Any) -> None:
165
+ """Tests the parallel setter validation in Calculate class."""
166
+ with pytest.raises(pydantic.ValidationError, match="Input should be 'single', 'points' or 'contrasts'"):
167
+ self.calculate.parallel = value
168
+
169
+ @pytest.mark.parametrize("value", [5.0, 12])
170
+ def test_calculate_calcSldDuringFit_validation(self, value: Union[int, float]) -> None:
171
+ """Tests the calcSldDuringFit setter validation in Calculate class."""
172
+ with pytest.raises(
173
+ pydantic.ValidationError, match="Input should be a valid boolean, unable to interpret input"
174
+ ):
175
+ self.calculate.calcSldDuringFit = value
176
+
177
+ @pytest.mark.parametrize("value", ["test", "iterate", "FINAL", True, 1, 3.0])
178
+ def test_calculate_display_validation(self, value: Any) -> None:
179
+ """Tests the display setter validation in Calculate class."""
180
+ with pytest.raises(pydantic.ValidationError, match="Input should be 'off', 'iter', 'notify' or 'final'"):
181
+ self.calculate.display = value
182
+
183
+ @pytest.mark.parametrize(
184
+ "value, msg",
185
+ [
186
+ ([5.0], "List should have at least 2 items after validation, not 1"),
187
+ ([12, 13, 14], "List should have at most 2 items after validation, not 3"),
188
+ ],
189
+ )
190
+ def test_calculate_resampleParams_length_validation(self, value: list, msg: str) -> None:
191
+ """Tests the resampleParams setter length validation in Calculate class."""
192
+ with pytest.raises(pydantic.ValidationError, match=msg):
193
+ self.calculate.resampleParams = value
194
+
195
+ @pytest.mark.parametrize(
196
+ "value, msg",
197
+ [
198
+ ([1.0, 2], "Value error, resampleParams[0] must be between 0 and 1"),
199
+ ([0.5, -0.1], "Value error, resampleParams[1] must be greater than or equal to 0"),
200
+ ],
201
+ )
202
+ def test_calculate_resampleParams_value_validation(self, value: list, msg: str) -> None:
203
+ """Tests the resampleParams setter value validation in Calculate class."""
204
+ with pytest.raises(pydantic.ValidationError) as exp:
205
+ self.calculate.resampleParams = value
206
+ assert exp.value.errors()[0]["msg"] == msg
207
+
208
+ def test_str(self, table_str) -> None:
209
+ """Tests the Calculate model __str__."""
210
+ assert self.calculate.__str__() == table_str
211
+
212
+
213
+ class TestSimplex:
214
+ """Tests the Simplex class."""
215
+
216
+ @pytest.fixture(autouse=True)
217
+ def setup_class(self):
218
+ self.simplex = Controls(procedure=Procedures.Simplex)
219
+
220
+ @pytest.fixture
221
+ def table_str(self):
222
+ table_str = (
223
+ "+------------------+-----------+\n"
224
+ "| Property | Value |\n"
225
+ "+------------------+-----------+\n"
226
+ "| procedure | simplex |\n"
227
+ "| parallel | single |\n"
228
+ "| calcSldDuringFit | False |\n"
229
+ "| resampleParams | [0.9, 50] |\n"
230
+ "| display | iter |\n"
231
+ "| xTolerance | 1e-06 |\n"
232
+ "| funcTolerance | 1e-06 |\n"
233
+ "| maxFuncEvals | 10000 |\n"
234
+ "| maxIterations | 1000 |\n"
235
+ "| updateFreq | 1 |\n"
236
+ "| updatePlotFreq | 20 |\n"
237
+ "+------------------+-----------+"
238
+ )
239
+
240
+ return table_str
241
+
242
+ @pytest.mark.parametrize(
243
+ "control_property, value",
244
+ [
245
+ ("parallel", Parallel.Single),
246
+ ("calcSldDuringFit", False),
247
+ ("resampleParams", [0.9, 50]),
248
+ ("display", Display.Iter),
249
+ ("procedure", Procedures.Simplex),
250
+ ("xTolerance", 1e-6),
251
+ ("funcTolerance", 1e-6),
252
+ ("maxFuncEvals", 10000),
253
+ ("maxIterations", 1000),
254
+ ("updateFreq", 1),
255
+ ("updatePlotFreq", 20),
256
+ ],
257
+ )
258
+ def test_simplex_property_values(self, control_property: str, value: Any) -> None:
259
+ """Tests the default values of Simplex class."""
260
+ assert getattr(self.simplex, control_property) == value
261
+
262
+ @pytest.mark.parametrize(
263
+ "control_property, value",
264
+ [
265
+ ("parallel", Parallel.Points),
266
+ ("calcSldDuringFit", True),
267
+ ("resampleParams", [0.2, 1]),
268
+ ("display", Display.Notify),
269
+ ("xTolerance", 4e-6),
270
+ ("funcTolerance", 3e-4),
271
+ ("maxFuncEvals", 100),
272
+ ("maxIterations", 50),
273
+ ("updateFreq", 4),
274
+ ("updatePlotFreq", 3),
275
+ ],
276
+ )
277
+ def test_simplex_property_setters(self, control_property: str, value: Any) -> None:
278
+ """Tests the setters of Simplex class."""
279
+ setattr(self.simplex, control_property, value)
280
+ assert getattr(self.simplex, control_property) == value
281
+
282
+ @pytest.mark.parametrize(
283
+ "wrong_property, value",
284
+ [
285
+ ("populationSize", 200),
286
+ ("fWeight", 0.3),
287
+ ("crossoverProbability", 0.4),
288
+ ("strategy", Strategies.BestWithJitter),
289
+ ("targetValue", 2.0),
290
+ ("numGenerations", 50),
291
+ ("nLive", 1500),
292
+ ("nMCMC", 1),
293
+ ("propScale", 0.5),
294
+ ("nsTolerance", 0.8),
295
+ ("nSamples", 500),
296
+ ("nChains", 1000),
297
+ ("jumpProbability", 0.7),
298
+ ("pUnitGamma", 0.3),
299
+ ("boundHandling", BoundHandling.Fold),
300
+ ("adaptPCR", False),
301
+ ],
302
+ )
303
+ def test_initialise_non_simplex_properties(self, wrong_property: str, value: Any) -> None:
304
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
305
+ with pytest.warns(
306
+ UserWarning,
307
+ match=f'\nThe current controls procedure is "simplex", but the property'
308
+ f' "{wrong_property}" applies instead to the'
309
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
310
+ f' The fields for the "simplex" controls procedure are:\n'
311
+ f' {", ".join(fields["simplex"])}\n',
312
+ ):
313
+ Controls(procedure="simplex", **{wrong_property: value})
314
+
315
+ @pytest.mark.parametrize(
316
+ "wrong_property, value",
317
+ [
318
+ ("populationSize", 200),
319
+ ("fWeight", 0.3),
320
+ ("crossoverProbability", 0.4),
321
+ ("strategy", Strategies.BestWithJitter),
322
+ ("targetValue", 2.0),
323
+ ("numGenerations", 50),
324
+ ("nLive", 1500),
325
+ ("nMCMC", 1),
326
+ ("propScale", 0.5),
327
+ ("nsTolerance", 0.8),
328
+ ("nSamples", 500),
329
+ ("nChains", 1000),
330
+ ("jumpProbability", 0.7),
331
+ ("pUnitGamma", 0.3),
332
+ ("boundHandling", BoundHandling.Fold),
333
+ ("adaptPCR", False),
334
+ ],
335
+ )
336
+ def test_set_non_simplex_properties(self, wrong_property: str, value: Any) -> None:
337
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
338
+ with pytest.warns(
339
+ UserWarning,
340
+ match=f'\nThe current controls procedure is "simplex", but the property'
341
+ f' "{wrong_property}" applies instead to the'
342
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
343
+ f' The fields for the "simplex" controls procedure are:\n'
344
+ f' {", ".join(fields["simplex"])}\n',
345
+ ):
346
+ setattr(self.simplex, wrong_property, value)
347
+
348
+ @pytest.mark.parametrize(
349
+ "control_property, value",
350
+ [
351
+ ("xTolerance", -4e-6),
352
+ ("funcTolerance", -3e-4),
353
+ ("maxFuncEvals", -100),
354
+ ("maxIterations", -50),
355
+ ],
356
+ )
357
+ def test_simplex_property_errors(self, control_property: str, value: Union[float, int]) -> None:
358
+ """Tests the property errors of Simplex class."""
359
+ with pytest.raises(pydantic.ValidationError, match="Input should be greater than 0"):
360
+ setattr(self.simplex, control_property, value)
361
+
362
+ def test_str(self, table_str) -> None:
363
+ """Tests the Simplex model __str__."""
364
+ assert self.simplex.__str__() == table_str
365
+
366
+
367
+ class TestDE:
368
+ """Tests the DE class."""
369
+
370
+ @pytest.fixture(autouse=True)
371
+ def setup_class(self):
372
+ self.de = Controls(procedure=Procedures.DE)
373
+
374
+ @pytest.fixture
375
+ def table_str(self):
376
+ table_str = (
377
+ "+----------------------+--------------------------------------+\n"
378
+ "| Property | Value |\n"
379
+ "+----------------------+--------------------------------------+\n"
380
+ "| procedure | de |\n"
381
+ "| parallel | single |\n"
382
+ "| calcSldDuringFit | False |\n"
383
+ "| resampleParams | [0.9, 50] |\n"
384
+ "| display | iter |\n"
385
+ "| populationSize | 20 |\n"
386
+ "| fWeight | 0.5 |\n"
387
+ "| crossoverProbability | 0.8 |\n"
388
+ "| strategy | Strategies.RandomWithPerVectorDither |\n"
389
+ "| targetValue | 1.0 |\n"
390
+ "| numGenerations | 500 |\n"
391
+ "| updateFreq | 1 |\n"
392
+ "| updatePlotFreq | 20 |\n"
393
+ "+----------------------+--------------------------------------+"
394
+ )
395
+
396
+ return table_str
397
+
398
+ @pytest.mark.parametrize(
399
+ "control_property, value",
400
+ [
401
+ ("parallel", Parallel.Single),
402
+ ("calcSldDuringFit", False),
403
+ ("resampleParams", [0.9, 50]),
404
+ ("display", Display.Iter),
405
+ ("procedure", Procedures.DE),
406
+ ("populationSize", 20),
407
+ ("fWeight", 0.5),
408
+ ("crossoverProbability", 0.8),
409
+ ("strategy", Strategies.RandomWithPerVectorDither),
410
+ ("targetValue", 1),
411
+ ("numGenerations", 500),
412
+ ],
413
+ )
414
+ def test_de_property_values(self, control_property: str, value: Any) -> None:
415
+ """Tests the default values of DE class."""
416
+ assert getattr(self.de, control_property) == value
417
+
418
+ @pytest.mark.parametrize(
419
+ "control_property, value",
420
+ [
421
+ ("parallel", Parallel.Points),
422
+ ("calcSldDuringFit", True),
423
+ ("resampleParams", [0.2, 1]),
424
+ ("display", Display.Notify),
425
+ ("populationSize", 20),
426
+ ("fWeight", 0.3),
427
+ ("crossoverProbability", 0.4),
428
+ ("strategy", Strategies.BestWithJitter),
429
+ ("targetValue", 2.0),
430
+ ("numGenerations", 50),
431
+ ],
432
+ )
433
+ def test_de_property_setters(self, control_property: str, value: Any) -> None:
434
+ """Tests the setters of DE class."""
435
+ setattr(self.de, control_property, value)
436
+ assert getattr(self.de, control_property) == value
437
+
438
+ @pytest.mark.parametrize(
439
+ "wrong_property, value",
440
+ [
441
+ ("xTolerance", 4e-6),
442
+ ("funcTolerance", 3e-4),
443
+ ("maxFuncEvals", 100),
444
+ ("maxIterations", 50),
445
+ ("nLive", 1500),
446
+ ("nMCMC", 1),
447
+ ("propScale", 0.5),
448
+ ("nsTolerance", 0.8),
449
+ ("nSamples", 500),
450
+ ("nChains", 1000),
451
+ ("jumpProbability", 0.7),
452
+ ("pUnitGamma", 0.3),
453
+ ("boundHandling", BoundHandling.Fold),
454
+ ("adaptPCR", False),
455
+ ],
456
+ )
457
+ def test_initialise_non_de_properties(self, wrong_property: str, value: Any) -> None:
458
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
459
+ with pytest.warns(
460
+ UserWarning,
461
+ match=f'\nThe current controls procedure is "de", but the property'
462
+ f' "{wrong_property}" applies instead to the'
463
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
464
+ f' The fields for the "de" controls procedure are:\n'
465
+ f' {", ".join(fields["de"])}\n',
466
+ ):
467
+ Controls(procedure="de", **{wrong_property: value})
468
+
469
+ @pytest.mark.parametrize(
470
+ "wrong_property, value",
471
+ [
472
+ ("xTolerance", 4e-6),
473
+ ("funcTolerance", 3e-4),
474
+ ("maxFuncEvals", 100),
475
+ ("maxIterations", 50),
476
+ ("nLive", 1500),
477
+ ("nMCMC", 1),
478
+ ("propScale", 0.5),
479
+ ("nsTolerance", 0.8),
480
+ ("nSamples", 500),
481
+ ("nChains", 1000),
482
+ ("jumpProbability", 0.7),
483
+ ("pUnitGamma", 0.3),
484
+ ("boundHandling", BoundHandling.Fold),
485
+ ("adaptPCR", False),
486
+ ],
487
+ )
488
+ def test_set_non_de_properties(self, wrong_property: str, value: Any) -> None:
489
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
490
+ with pytest.warns(
491
+ UserWarning,
492
+ match=f'\nThe current controls procedure is "de", but the property'
493
+ f' "{wrong_property}" applies instead to the'
494
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
495
+ f' The fields for the "de" controls procedure are:\n'
496
+ f' {", ".join(fields["de"])}\n',
497
+ ):
498
+ setattr(self.de, wrong_property, value)
499
+
500
+ @pytest.mark.parametrize(
501
+ "value, msg",
502
+ [
503
+ (0, "Input should be greater than 0"),
504
+ (2, "Input should be less than 1"),
505
+ ],
506
+ )
507
+ def test_de_crossoverProbability_error(self, value: int, msg: str) -> None:
508
+ """Tests the crossoverProbability setter error in DE class."""
509
+ with pytest.raises(pydantic.ValidationError, match=msg):
510
+ self.de.crossoverProbability = value
511
+
512
+ @pytest.mark.parametrize(
513
+ "control_property, value",
514
+ [
515
+ ("targetValue", 0),
516
+ ("targetValue", 0.999),
517
+ ("numGenerations", -500),
518
+ ("numGenerations", 0),
519
+ ("populationSize", 0),
520
+ ("populationSize", -1),
521
+ ],
522
+ )
523
+ def test_de_targetValue_numGenerations_populationSize_error(
524
+ self,
525
+ control_property: str,
526
+ value: Union[int, float],
527
+ ) -> None:
528
+ """Tests the targetValue, numGenerations, populationSize setter error in DE class."""
529
+ with pytest.raises(pydantic.ValidationError, match="Input should be greater than or equal to 1"):
530
+ setattr(self.de, control_property, value)
531
+
532
+ def test_str(self, table_str) -> None:
533
+ """Tests the DE model __str__."""
534
+ assert self.de.__str__() == table_str
535
+
536
+
537
+ class TestNS:
538
+ """Tests the NS class."""
539
+
540
+ @pytest.fixture(autouse=True)
541
+ def setup_class(self):
542
+ self.ns = Controls(procedure=Procedures.NS)
543
+
544
+ @pytest.fixture
545
+ def table_str(self):
546
+ table_str = (
547
+ "+------------------+-----------+\n"
548
+ "| Property | Value |\n"
549
+ "+------------------+-----------+\n"
550
+ "| procedure | ns |\n"
551
+ "| parallel | single |\n"
552
+ "| calcSldDuringFit | False |\n"
553
+ "| resampleParams | [0.9, 50] |\n"
554
+ "| display | iter |\n"
555
+ "| nLive | 150 |\n"
556
+ "| nMCMC | 0.0 |\n"
557
+ "| propScale | 0.1 |\n"
558
+ "| nsTolerance | 0.1 |\n"
559
+ "+------------------+-----------+"
560
+ )
561
+
562
+ return table_str
563
+
564
+ @pytest.mark.parametrize(
565
+ "control_property, value",
566
+ [
567
+ ("parallel", Parallel.Single),
568
+ ("calcSldDuringFit", False),
569
+ ("resampleParams", [0.9, 50]),
570
+ ("display", Display.Iter),
571
+ ("procedure", Procedures.NS),
572
+ ("nLive", 150),
573
+ ("nMCMC", 0),
574
+ ("propScale", 0.1),
575
+ ("nsTolerance", 0.1),
576
+ ],
577
+ )
578
+ def test_ns_property_values(self, control_property: str, value: Any) -> None:
579
+ """Tests the default values of NS class."""
580
+ assert getattr(self.ns, control_property) == value
581
+
582
+ @pytest.mark.parametrize(
583
+ "control_property, value",
584
+ [
585
+ ("parallel", Parallel.Points),
586
+ ("calcSldDuringFit", True),
587
+ ("resampleParams", [0.2, 1]),
588
+ ("display", Display.Notify),
589
+ ("nLive", 1500),
590
+ ("nMCMC", 1),
591
+ ("propScale", 0.5),
592
+ ("nsTolerance", 0.8),
593
+ ],
594
+ )
595
+ def test_ns_property_setters(self, control_property: str, value: Any) -> None:
596
+ """Tests the setters of NS class."""
597
+ setattr(self.ns, control_property, value)
598
+ assert getattr(self.ns, control_property) == value
599
+
600
+ @pytest.mark.parametrize(
601
+ "wrong_property, value",
602
+ [
603
+ ("xTolerance", 4e-6),
604
+ ("funcTolerance", 3e-4),
605
+ ("maxFuncEvals", 100),
606
+ ("maxIterations", 50),
607
+ ("updateFreq", 4),
608
+ ("updatePlotFreq", 3),
609
+ ("populationSize", 200),
610
+ ("fWeight", 0.3),
611
+ ("crossoverProbability", 0.4),
612
+ ("strategy", Strategies.BestWithJitter),
613
+ ("targetValue", 2.0),
614
+ ("numGenerations", 50),
615
+ ("nSamples", 500),
616
+ ("nChains", 1000),
617
+ ("jumpProbability", 0.7),
618
+ ("pUnitGamma", 0.3),
619
+ ("boundHandling", BoundHandling.Fold),
620
+ ("adaptPCR", False),
621
+ ],
622
+ )
623
+ def test_initialise_non_ns_properties(self, wrong_property: str, value: Any) -> None:
624
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
625
+ with pytest.warns(
626
+ UserWarning,
627
+ match=f'\nThe current controls procedure is "ns", but the property'
628
+ f' "{wrong_property}" applies instead to the'
629
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
630
+ f' The fields for the "ns" controls procedure are:\n'
631
+ f' {", ".join(fields["ns"])}\n',
632
+ ):
633
+ Controls(procedure="ns", **{wrong_property: value})
634
+
635
+ @pytest.mark.parametrize(
636
+ "wrong_property, value",
637
+ [
638
+ ("xTolerance", 4e-6),
639
+ ("funcTolerance", 3e-4),
640
+ ("maxFuncEvals", 100),
641
+ ("maxIterations", 50),
642
+ ("updateFreq", 4),
643
+ ("updatePlotFreq", 3),
644
+ ("populationSize", 200),
645
+ ("fWeight", 0.3),
646
+ ("crossoverProbability", 0.4),
647
+ ("strategy", Strategies.BestWithJitter),
648
+ ("targetValue", 2.0),
649
+ ("numGenerations", 50),
650
+ ("nSamples", 500),
651
+ ("nChains", 1000),
652
+ ("jumpProbability", 0.7),
653
+ ("pUnitGamma", 0.3),
654
+ ("boundHandling", BoundHandling.Fold),
655
+ ("adaptPCR", False),
656
+ ],
657
+ )
658
+ def test_set_non_ns_properties(self, wrong_property: str, value: Any) -> None:
659
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
660
+ with pytest.warns(
661
+ UserWarning,
662
+ match=f'\nThe current controls procedure is "ns", but the property'
663
+ f' "{wrong_property}" applies instead to the'
664
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
665
+ f' The fields for the "ns" controls procedure are:\n'
666
+ f' {", ".join(fields["ns"])}\n',
667
+ ):
668
+ setattr(self.ns, wrong_property, value)
669
+
670
+ @pytest.mark.parametrize(
671
+ "control_property, value, bound",
672
+ [
673
+ ("nMCMC", -0.6, 0),
674
+ ("nsTolerance", -500, 0),
675
+ ("nLive", -500, 1),
676
+ ],
677
+ )
678
+ def test_ns_setter_error(self, control_property: str, value: Union[int, float], bound: int) -> None:
679
+ """Tests the nMCMC, nsTolerance, nLive setter error in NS class."""
680
+ with pytest.raises(pydantic.ValidationError, match=f"Input should be greater than or equal to {bound}"):
681
+ setattr(self.ns, control_property, value)
682
+
683
+ @pytest.mark.parametrize(
684
+ "value, msg",
685
+ [
686
+ (0, "Input should be greater than 0"),
687
+ (2, "Input should be less than 1"),
688
+ ],
689
+ )
690
+ def test_ns_propScale_error(self, value: int, msg: str) -> None:
691
+ """Tests the propScale error in NS class."""
692
+ with pytest.raises(pydantic.ValidationError, match=msg):
693
+ self.ns.propScale = value
694
+
695
+ def test_control_class_ns_str(self, table_str) -> None:
696
+ """Tests the NS model __str__."""
697
+ assert self.ns.__str__() == table_str
698
+
699
+
700
+ class TestDream:
701
+ """Tests the Dream class."""
702
+
703
+ @pytest.fixture(autouse=True)
704
+ def setup_class(self):
705
+ self.dream = Controls(procedure=Procedures.Dream)
706
+
707
+ @pytest.fixture
708
+ def table_str(self):
709
+ table_str = (
710
+ "+------------------+-----------+\n"
711
+ "| Property | Value |\n"
712
+ "+------------------+-----------+\n"
713
+ "| procedure | dream |\n"
714
+ "| parallel | single |\n"
715
+ "| calcSldDuringFit | False |\n"
716
+ "| resampleParams | [0.9, 50] |\n"
717
+ "| display | iter |\n"
718
+ "| nSamples | 20000 |\n"
719
+ "| nChains | 10 |\n"
720
+ "| jumpProbability | 0.5 |\n"
721
+ "| pUnitGamma | 0.2 |\n"
722
+ "| boundHandling | reflect |\n"
723
+ "| adaptPCR | True |\n"
724
+ "+------------------+-----------+"
725
+ )
726
+
727
+ return table_str
728
+
729
+ @pytest.mark.parametrize(
730
+ "control_property, value",
731
+ [
732
+ ("parallel", Parallel.Single),
733
+ ("calcSldDuringFit", False),
734
+ ("resampleParams", [0.9, 50]),
735
+ ("display", Display.Iter),
736
+ ("procedure", Procedures.Dream),
737
+ ("nSamples", 20000),
738
+ ("nChains", 10),
739
+ ("jumpProbability", 0.5),
740
+ ("pUnitGamma", 0.2),
741
+ ("boundHandling", BoundHandling.Reflect),
742
+ ("adaptPCR", True),
743
+ ],
744
+ )
745
+ def test_dream_property_values(self, control_property: str, value: Any) -> None:
746
+ """Tests the default values of Dream class."""
747
+ assert getattr(self.dream, control_property) == value
748
+
749
+ @pytest.mark.parametrize(
750
+ "control_property, value",
751
+ [
752
+ ("parallel", Parallel.Points),
753
+ ("calcSldDuringFit", True),
754
+ ("resampleParams", [0.2, 1]),
755
+ ("display", Display.Notify),
756
+ ("nSamples", 500),
757
+ ("nChains", 1000),
758
+ ("jumpProbability", 0.7),
759
+ ("pUnitGamma", 0.3),
760
+ ("boundHandling", BoundHandling.Fold),
761
+ ("adaptPCR", False),
762
+ ],
763
+ )
764
+ def test_dream_property_setters(self, control_property: str, value: Any) -> None:
765
+ """Tests the setters in Dream class."""
766
+ setattr(self.dream, control_property, value)
767
+ assert getattr(self.dream, control_property) == value
768
+
769
+ @pytest.mark.parametrize(
770
+ "wrong_property, value",
771
+ [
772
+ ("xTolerance", 4e-6),
773
+ ("funcTolerance", 3e-4),
774
+ ("maxFuncEvals", 100),
775
+ ("maxIterations", 50),
776
+ ("updateFreq", 4),
777
+ ("updatePlotFreq", 3),
778
+ ("populationSize", 200),
779
+ ("fWeight", 0.3),
780
+ ("crossoverProbability", 0.4),
781
+ ("strategy", Strategies.BestWithJitter),
782
+ ("targetValue", 2.0),
783
+ ("numGenerations", 50),
784
+ ("nLive", 1500),
785
+ ("nMCMC", 1),
786
+ ("propScale", 0.5),
787
+ ("nsTolerance", 0.8),
788
+ ],
789
+ )
790
+ def test_initialise_non_dream_properties(self, wrong_property: str, value: Any) -> None:
791
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
792
+ with pytest.warns(
793
+ UserWarning,
794
+ match=f'\nThe current controls procedure is "dream", but the property'
795
+ f' "{wrong_property}" applies instead to the'
796
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
797
+ f' The fields for the "dream" controls procedure are:\n'
798
+ f' {", ".join(fields["dream"])}\n',
799
+ ):
800
+ Controls(procedure="dream", **{wrong_property: value})
801
+
802
+ @pytest.mark.parametrize(
803
+ "wrong_property, value",
804
+ [
805
+ ("xTolerance", 4e-6),
806
+ ("funcTolerance", 3e-4),
807
+ ("maxFuncEvals", 100),
808
+ ("maxIterations", 50),
809
+ ("updateFreq", 4),
810
+ ("updatePlotFreq", 3),
811
+ ("populationSize", 200),
812
+ ("fWeight", 0.3),
813
+ ("crossoverProbability", 0.4),
814
+ ("strategy", Strategies.BestWithJitter),
815
+ ("targetValue", 2.0),
816
+ ("numGenerations", 50),
817
+ ("nLive", 1500),
818
+ ("nMCMC", 1),
819
+ ("propScale", 0.5),
820
+ ("nsTolerance", 0.8),
821
+ ],
822
+ )
823
+ def test_set_non_dream_properties(self, wrong_property: str, value: Any) -> None:
824
+ incorrect_procedures = [key for (key, value) in fields.items() if wrong_property in value]
825
+ with pytest.warns(
826
+ UserWarning,
827
+ match=f'\nThe current controls procedure is "dream", but the property'
828
+ f' "{wrong_property}" applies instead to the'
829
+ f' {", ".join(incorrect_procedures)} procedure.\n\n'
830
+ f' The fields for the "dream" controls procedure are:\n'
831
+ f' {", ".join(fields["dream"])}\n',
832
+ ):
833
+ setattr(self.dream, wrong_property, value)
834
+
835
+ @pytest.mark.parametrize(
836
+ "control_property, value, msg",
837
+ [
838
+ ("jumpProbability", 0, "Input should be greater than 0"),
839
+ ("jumpProbability", 2, "Input should be less than 1"),
840
+ ("pUnitGamma", -5, "Input should be greater than 0"),
841
+ ("pUnitGamma", 20, "Input should be less than 1"),
842
+ ],
843
+ )
844
+ def test_dream_jumpProbability_pUnitGamma_error(self, control_property: str, value: int, msg: str) -> None:
845
+ """Tests the jumpProbability and pUnitGamma setter errors in Dream class."""
846
+ with pytest.raises(pydantic.ValidationError, match=msg):
847
+ setattr(self.dream, control_property, value)
848
+
849
+ @pytest.mark.parametrize("value", [-80, -2])
850
+ def test_dream_nSamples_error(self, value: int) -> None:
851
+ """Tests the nSamples setter error in Dream class."""
852
+ with pytest.raises(pydantic.ValidationError, match="Input should be greater than or equal to 0"):
853
+ self.dream.nSamples = value
854
+
855
+ @pytest.mark.parametrize("value", [-5, 0])
856
+ def test_dream_nChains_error(self, value: int) -> None:
857
+ """Tests the nChains setter error in Dream class."""
858
+ with pytest.raises(pydantic.ValidationError, match="Input should be greater than 0"):
859
+ self.dream.nChains = value
860
+
861
+ def test_control_class_dream_str(self, table_str) -> None:
862
+ """Tests the Dream model __str__."""
863
+ assert self.dream.__str__() == table_str