Skip to content

Commit c025aeb

Browse files
authored
Adds controls option "numSimulationPoints" (#170)
1 parent cc8fdf7 commit c025aeb

File tree

7 files changed

+108
-72
lines changed

7 files changed

+108
-72
lines changed

cpp/includes/defines.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ procedure : str
590590
Which procedure RAT should execute. Can be 'calculate', 'simplex', 'de', 'ns', or 'dream'.
591591
calcSldDuringFit : bool
592592
Whether SLD will be calculated during fit (for live plotting etc.)
593+
numSimulationPoints : int
594+
The number of points used for a reflectivity simulation where no data is present.
593595
resampleMinAngle : float
594596
The upper threshold on the angle between three sampled points for resampling, in units of radians over pi.
595597
resampleNPoints : int
@@ -663,6 +665,7 @@ struct Control {
663665
real_T propScale {};
664666
real_T nsTolerance {};
665667
boolean_T calcSldDuringFit {};
668+
real_T numSimulationPoints {};
666669
real_T resampleMinAngle {};
667670
real_T resampleNPoints {};
668671
real_T updateFreq {};

cpp/rat.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ RAT::Controls createControlsStruct(const Control& control)
318318
control_struct.propScale = control.propScale;
319319
control_struct.nsTolerance = control.nsTolerance;
320320
control_struct.calcSldDuringFit = control.calcSldDuringFit;
321+
control_struct.numSimulationPoints = control.numSimulationPoints;
321322
control_struct.updateFreq = control.updateFreq;
322323
control_struct.updatePlotFreq = control.updatePlotFreq;
323324
control_struct.nSamples = control.nSamples;
@@ -910,6 +911,7 @@ PYBIND11_MODULE(rat_core, m) {
910911
.def_readwrite("propScale", &Control::propScale)
911912
.def_readwrite("nsTolerance", &Control::nsTolerance)
912913
.def_readwrite("calcSldDuringFit", &Control::calcSldDuringFit)
914+
.def_readwrite("numSimulationPoints", &Control::numSimulationPoints)
913915
.def_readwrite("resampleMinAngle", &Control::resampleMinAngle)
914916
.def_readwrite("resampleNPoints", &Control::resampleNPoints)
915917
.def_readwrite("updateFreq", &Control::updateFreq)
@@ -927,12 +929,12 @@ PYBIND11_MODULE(rat_core, m) {
927929
return py::make_tuple(ctrl.parallel, ctrl.procedure, ctrl.display, ctrl.xTolerance, ctrl.funcTolerance,
928930
ctrl.maxFuncEvals, ctrl.maxIterations, ctrl.populationSize, ctrl.fWeight, ctrl.crossoverProbability,
929931
ctrl.targetValue, ctrl.numGenerations, ctrl.strategy, ctrl.nLive, ctrl.nMCMC, ctrl.propScale,
930-
ctrl.nsTolerance, ctrl.calcSldDuringFit, ctrl.resampleMinAngle, ctrl.resampleNPoints,
932+
ctrl.nsTolerance, ctrl.calcSldDuringFit, ctrl.numSimulationPoints, ctrl.resampleMinAngle, ctrl.resampleNPoints,
931933
ctrl.updateFreq, ctrl.updatePlotFreq, ctrl.nSamples, ctrl.nChains, ctrl.jumpProbability, ctrl.pUnitGamma,
932934
ctrl.boundHandling, ctrl.adaptPCR, ctrl.IPCFilePath);
933935
},
934936
[](py::tuple t) { // __setstate__
935-
if (t.size() != 29)
937+
if (t.size() != 30)
936938
throw std::runtime_error("Encountered invalid state unpickling ProblemDefinition object!");
937939

938940
/* Create a new C++ instance */
@@ -956,17 +958,18 @@ PYBIND11_MODULE(rat_core, m) {
956958
ctrl.propScale = t[15].cast<real_T>();
957959
ctrl.nsTolerance = t[16].cast<real_T>();
958960
ctrl.calcSldDuringFit = t[17].cast<boolean_T>();
959-
ctrl.resampleMinAngle = t[18].cast<real_T>();
960-
ctrl.resampleNPoints = t[19].cast<real_T>();
961-
ctrl.updateFreq = t[20].cast<real_T>();
962-
ctrl.updatePlotFreq = t[21].cast<real_T>();
963-
ctrl.nSamples = t[22].cast<real_T>();
964-
ctrl.nChains = t[23].cast<real_T>();
965-
ctrl.jumpProbability = t[24].cast<real_T>();
966-
ctrl.pUnitGamma = t[25].cast<real_T>();
967-
ctrl.boundHandling = t[26].cast<std::string>();
968-
ctrl.adaptPCR = t[27].cast<boolean_T>();
969-
ctrl.IPCFilePath = t[28].cast<std::string>();
961+
ctrl.numSimulationPoints = t[18].cast<real_T>();
962+
ctrl.resampleMinAngle = t[19].cast<real_T>();
963+
ctrl.resampleNPoints = t[20].cast<real_T>();
964+
ctrl.updateFreq = t[21].cast<real_T>();
965+
ctrl.updatePlotFreq = t[22].cast<real_T>();
966+
ctrl.nSamples = t[23].cast<real_T>();
967+
ctrl.nChains = t[24].cast<real_T>();
968+
ctrl.jumpProbability = t[25].cast<real_T>();
969+
ctrl.pUnitGamma = t[26].cast<real_T>();
970+
ctrl.boundHandling = t[27].cast<std::string>();
971+
ctrl.adaptPCR = t[28].cast<boolean_T>();
972+
ctrl.IPCFilePath = t[29].cast<std::string>();
970973

971974
return ctrl;
972975
}));

ratapi/controls.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@
2020
from ratapi.utils.custom_errors import custom_pydantic_validation_error
2121
from ratapi.utils.enums import BoundHandling, Display, Parallel, Procedures, Strategies
2222

23-
common_fields = ["procedure", "parallel", "calcSldDuringFit", "resampleMinAngle", "resampleNPoints", "display"]
23+
common_fields = [
24+
"procedure",
25+
"parallel",
26+
"calcSldDuringFit",
27+
"numSimulationPoints",
28+
"resampleMinAngle",
29+
"resampleNPoints",
30+
"display",
31+
]
2432
update_fields = ["updateFreq", "updatePlotFreq"]
2533
fields = {
2634
"calculate": common_fields,
@@ -53,6 +61,9 @@ class Controls(BaseModel, validate_assignment=True, extra="forbid", use_attribut
5361
calcSldDuringFit: bool = False
5462
"""Whether SLD will be calculated during fit (for live plotting etc.)"""
5563

64+
numSimulationPoints: int = Field(500, ge=2)
65+
"""The number of points used for reflectivity simulations where no data is supplied."""
66+
5667
resampleMinAngle: float = Field(0.9, le=1, gt=0)
5768
"""The upper threshold on the angle between three sampled points for resampling, in units of radians over pi."""
5869

ratapi/inputs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ def make_controls(input_controls: ratapi.Controls) -> Control:
553553
controls.procedure = input_controls.procedure
554554
controls.parallel = input_controls.parallel
555555
controls.calcSldDuringFit = input_controls.calcSldDuringFit
556+
controls.numSimulationPoints = input_controls.numSimulationPoints
556557
controls.resampleMinAngle = input_controls.resampleMinAngle
557558
controls.resampleNPoints = input_controls.resampleNPoints
558559
controls.display = input_controls.display

tests/test_controls.py

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ def setup_class(self):
5555
@pytest.fixture
5656
def table_str(self):
5757
table_str = (
58-
"+------------------+-----------+\n"
59-
"| Property | Value |\n"
60-
"+------------------+-----------+\n"
61-
"| procedure | calculate |\n"
62-
"| parallel | single |\n"
63-
"| calcSldDuringFit | False |\n"
64-
"| resampleMinAngle | 0.9 |\n"
65-
"| resampleNPoints | 50 |\n"
66-
"| display | iter |\n"
67-
"+------------------+-----------+"
58+
"+---------------------+-----------+\n"
59+
"| Property | Value |\n"
60+
"+---------------------+-----------+\n"
61+
"| procedure | calculate |\n"
62+
"| parallel | single |\n"
63+
"| calcSldDuringFit | False |\n"
64+
"| numSimulationPoints | 500 |\n"
65+
"| resampleMinAngle | 0.9 |\n"
66+
"| resampleNPoints | 50 |\n"
67+
"| display | iter |\n"
68+
"+---------------------+-----------+"
6869
)
6970

7071
return table_str
@@ -74,6 +75,7 @@ def table_str(self):
7475
[
7576
("parallel", Parallel.Single),
7677
("calcSldDuringFit", False),
78+
("numSimulationPoints", 500),
7779
("resampleMinAngle", 0.9),
7880
("resampleNPoints", 50),
7981
("display", Display.Iter),
@@ -89,6 +91,7 @@ def test_calculate_property_values(self, control_property: str, value: Any) -> N
8991
[
9092
("parallel", Parallel.Points),
9193
("calcSldDuringFit", True),
94+
("numSimulationPoints", 10),
9295
("resampleMinAngle", 0.2),
9396
("resampleNPoints", 1),
9497
("display", Display.Notify),
@@ -212,22 +215,23 @@ def setup_class(self):
212215
@pytest.fixture
213216
def table_str(self):
214217
table_str = (
215-
"+------------------+---------+\n"
216-
"| Property | Value |\n"
217-
"+------------------+---------+\n"
218-
"| procedure | simplex |\n"
219-
"| parallel | single |\n"
220-
"| calcSldDuringFit | False |\n"
221-
"| resampleMinAngle | 0.9 |\n"
222-
"| resampleNPoints | 50 |\n"
223-
"| display | iter |\n"
224-
"| xTolerance | 1e-06 |\n"
225-
"| funcTolerance | 1e-06 |\n"
226-
"| maxFuncEvals | 10000 |\n"
227-
"| maxIterations | 1000 |\n"
228-
"| updateFreq | 1 |\n"
229-
"| updatePlotFreq | 20 |\n"
230-
"+------------------+---------+"
218+
"+---------------------+---------+\n"
219+
"| Property | Value |\n"
220+
"+---------------------+---------+\n"
221+
"| procedure | simplex |\n"
222+
"| parallel | single |\n"
223+
"| calcSldDuringFit | False |\n"
224+
"| numSimulationPoints | 500 |\n"
225+
"| resampleMinAngle | 0.9 |\n"
226+
"| resampleNPoints | 50 |\n"
227+
"| display | iter |\n"
228+
"| xTolerance | 1e-06 |\n"
229+
"| funcTolerance | 1e-06 |\n"
230+
"| maxFuncEvals | 10000 |\n"
231+
"| maxIterations | 1000 |\n"
232+
"| updateFreq | 1 |\n"
233+
"| updatePlotFreq | 20 |\n"
234+
"+---------------------+---------+"
231235
)
232236

233237
return table_str
@@ -237,6 +241,7 @@ def table_str(self):
237241
[
238242
("parallel", Parallel.Single),
239243
("calcSldDuringFit", False),
244+
("numSimulationPoints", 500),
240245
("resampleMinAngle", 0.9),
241246
("resampleNPoints", 50),
242247
("display", Display.Iter),
@@ -258,6 +263,7 @@ def test_simplex_property_values(self, control_property: str, value: Any) -> Non
258263
[
259264
("parallel", Parallel.Points),
260265
("calcSldDuringFit", True),
266+
("numSimulationPoints", 10),
261267
("resampleMinAngle", 0.2),
262268
("resampleNPoints", 1),
263269
("display", Display.Notify),
@@ -375,6 +381,7 @@ def table_str(self):
375381
"| procedure | de |\n"
376382
"| parallel | single |\n"
377383
"| calcSldDuringFit | False |\n"
384+
"| numSimulationPoints | 500 |\n"
378385
"| resampleMinAngle | 0.9 |\n"
379386
"| resampleNPoints | 50 |\n"
380387
"| display | iter |\n"
@@ -396,6 +403,7 @@ def table_str(self):
396403
[
397404
("parallel", Parallel.Single),
398405
("calcSldDuringFit", False),
406+
("numSimulationPoints", 500),
399407
("resampleMinAngle", 0.9),
400408
("resampleNPoints", 50),
401409
("display", Display.Iter),
@@ -417,6 +425,7 @@ def test_de_property_values(self, control_property: str, value: Any) -> None:
417425
[
418426
("parallel", Parallel.Points),
419427
("calcSldDuringFit", True),
428+
("numSimulationPoints", 10),
420429
("resampleMinAngle", 0.2),
421430
("resampleNPoints", 1),
422431
("display", Display.Notify),
@@ -542,20 +551,21 @@ def setup_class(self):
542551
@pytest.fixture
543552
def table_str(self):
544553
table_str = (
545-
"+------------------+--------+\n"
546-
"| Property | Value |\n"
547-
"+------------------+--------+\n"
548-
"| procedure | ns |\n"
549-
"| parallel | single |\n"
550-
"| calcSldDuringFit | False |\n"
551-
"| resampleMinAngle | 0.9 |\n"
552-
"| resampleNPoints | 50 |\n"
553-
"| display | iter |\n"
554-
"| nLive | 150 |\n"
555-
"| nMCMC | 0 |\n"
556-
"| propScale | 0.1 |\n"
557-
"| nsTolerance | 0.1 |\n"
558-
"+------------------+--------+"
554+
"+---------------------+--------+\n"
555+
"| Property | Value |\n"
556+
"+---------------------+--------+\n"
557+
"| procedure | ns |\n"
558+
"| parallel | single |\n"
559+
"| calcSldDuringFit | False |\n"
560+
"| numSimulationPoints | 500 |\n"
561+
"| resampleMinAngle | 0.9 |\n"
562+
"| resampleNPoints | 50 |\n"
563+
"| display | iter |\n"
564+
"| nLive | 150 |\n"
565+
"| nMCMC | 0 |\n"
566+
"| propScale | 0.1 |\n"
567+
"| nsTolerance | 0.1 |\n"
568+
"+---------------------+--------+"
559569
)
560570

561571
return table_str
@@ -565,6 +575,7 @@ def table_str(self):
565575
[
566576
("parallel", Parallel.Single),
567577
("calcSldDuringFit", False),
578+
("numSimulationPoints", 500),
568579
("resampleMinAngle", 0.9),
569580
("resampleNPoints", 50),
570581
("display", Display.Iter),
@@ -584,6 +595,7 @@ def test_ns_property_values(self, control_property: str, value: Any) -> None:
584595
[
585596
("parallel", Parallel.Points),
586597
("calcSldDuringFit", True),
598+
("numSimulationPoints", 10),
587599
("resampleMinAngle", 0.2),
588600
("resampleNPoints", 1),
589601
("display", Display.Notify),
@@ -708,22 +720,23 @@ def setup_class(self):
708720
@pytest.fixture
709721
def table_str(self):
710722
table_str = (
711-
"+------------------+---------+\n"
712-
"| Property | Value |\n"
713-
"+------------------+---------+\n"
714-
"| procedure | dream |\n"
715-
"| parallel | single |\n"
716-
"| calcSldDuringFit | False |\n"
717-
"| resampleMinAngle | 0.9 |\n"
718-
"| resampleNPoints | 50 |\n"
719-
"| display | iter |\n"
720-
"| nSamples | 20000 |\n"
721-
"| nChains | 10 |\n"
722-
"| jumpProbability | 0.5 |\n"
723-
"| pUnitGamma | 0.2 |\n"
724-
"| boundHandling | reflect |\n"
725-
"| adaptPCR | True |\n"
726-
"+------------------+---------+"
723+
"+---------------------+---------+\n"
724+
"| Property | Value |\n"
725+
"+---------------------+---------+\n"
726+
"| procedure | dream |\n"
727+
"| parallel | single |\n"
728+
"| calcSldDuringFit | False |\n"
729+
"| numSimulationPoints | 500 |\n"
730+
"| resampleMinAngle | 0.9 |\n"
731+
"| resampleNPoints | 50 |\n"
732+
"| display | iter |\n"
733+
"| nSamples | 20000 |\n"
734+
"| nChains | 10 |\n"
735+
"| jumpProbability | 0.5 |\n"
736+
"| pUnitGamma | 0.2 |\n"
737+
"| boundHandling | reflect |\n"
738+
"| adaptPCR | True |\n"
739+
"+---------------------+---------+"
727740
)
728741

729742
return table_str
@@ -733,6 +746,7 @@ def table_str(self):
733746
[
734747
("parallel", Parallel.Single),
735748
("calcSldDuringFit", False),
749+
("numSimulationPoints", 500),
736750
("resampleMinAngle", 0.9),
737751
("resampleNPoints", 50),
738752
("display", Display.Iter),
@@ -754,6 +768,7 @@ def test_dream_property_values(self, control_property: str, value: Any) -> None:
754768
[
755769
("parallel", Parallel.Points),
756770
("calcSldDuringFit", True),
771+
("numSimulationPoints", 10),
757772
("resampleMinAngle", 0.2),
758773
("resampleNPoints", 1),
759774
("display", Display.Notify),

tests/test_inputs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ def standard_layers_controls():
360360
controls.procedure = Procedures.Calculate
361361
controls.parallel = Parallel.Single
362362
controls.calcSldDuringFit = False
363+
controls.numSimulationPoints = 500
363364
controls.resampleMinAngle = 0.9
364-
controls.resampleNPoints = 50.0
365+
controls.resampleNPoints = 50
365366
controls.display = Display.Iter
366367
controls.xTolerance = 1.0e-6
367368
controls.funcTolerance = 1.0e-6
@@ -398,6 +399,7 @@ def custom_xy_controls():
398399
controls.procedure = Procedures.Calculate
399400
controls.parallel = Parallel.Single
400401
controls.calcSldDuringFit = False
402+
controls.numSimulationPoints = 500
401403
controls.resampleMinAngle = 0.9
402404
controls.resampleNPoints = 50.0
403405
controls.display = Display.Iter
@@ -756,6 +758,7 @@ def check_controls_equal(actual_controls, expected_controls) -> None:
756758
"procedure",
757759
"parallel",
758760
"calcSldDuringFit",
761+
"numSimulationPoints",
759762
"resampleMinAngle",
760763
"resampleNPoints",
761764
"display",

0 commit comments

Comments
 (0)