Skip to content

Commit f5458c5

Browse files
committed
Adds "repeat_layers" option to contrasts
1 parent ceacffd commit f5458c5

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

ratapi/inputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def make_problem(project: ratapi.Project) -> ProblemDefinition:
302302
problem.numberOfContrasts = len(project.contrasts)
303303
problem.geometry = project.geometry
304304
problem.useImaginary = project.absorption
305-
problem.repeatLayers = [1] * len(project.contrasts)
305+
problem.repeatLayers = [contrast.repeat_layers for contrast in project.contrasts]
306306
problem.contrastBackgroundParams = contrast_background_params
307307
problem.contrastBackgroundTypes = contrast_background_types
308308
problem.contrastBackgroundActions = [contrast.background_action for contrast in project.contrasts]

ratapi/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class Contrast(RATModel):
163163
The name of the instrument resolution for this contrast.
164164
resample : bool
165165
Whether adaptive resampling should be used for interface microslicing.
166+
repeat_layers : int
167+
For standard layers, the number of times the set of layers defined in the model should be repeated.
166168
model : list[str]
167169
If this is a standard layers model, this should be a list of layer names
168170
that make up the slab model for this contrast.
@@ -180,6 +182,7 @@ class Contrast(RATModel):
180182
scalefactor: str = ""
181183
resolution: str = ""
182184
resample: bool = False
185+
repeat_layers: int = Field(default=1, gt=0)
183186
model: list[str] = []
184187

185188
@model_validator(mode="before")
@@ -208,6 +211,7 @@ def __str__(self):
208211
self.scalefactor,
209212
self.resolution,
210213
self.resample,
214+
self.repeat_layers,
211215
model_entry,
212216
]
213217
)
@@ -238,6 +242,8 @@ class ContrastWithRatio(RATModel):
238242
The name of the instrument resolution for this contrast.
239243
resample : bool
240244
Whether adaptive resampling should be used for interface microslicing.
245+
repeat_layers : int
246+
For standard layers, the number of times the set of layers defined in the model should be repeated.
241247
domain_ratio : str
242248
The name of the domain ratio parameter describing how the first domain should be weighted
243249
relative to the second.
@@ -258,6 +264,7 @@ class ContrastWithRatio(RATModel):
258264
scalefactor: str = ""
259265
resolution: str = ""
260266
resample: bool = False
267+
repeat_layers: int = Field(default=1, gt=0)
261268
domain_ratio: str = ""
262269
model: list[str] = []
263270

@@ -276,6 +283,8 @@ def __str__(self):
276283
self.scalefactor,
277284
self.resolution,
278285
self.resample,
286+
self.repeat_layers,
287+
self.domain_ratio,
279288
model_entry,
280289
]
281290
)

ratapi/project.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,20 @@ def set_layers(self) -> "Project":
454454
self.layers.data = []
455455
return self
456456

457+
@model_validator(mode="after")
458+
def set_repeat_layers(self) -> "Project":
459+
"""If we are not using a standard layers model, warn that the repeat layers setting is not valid."""
460+
if self.model != LayerModels.StandardLayers:
461+
for contrast in self.contrasts:
462+
if "repeat_layers" in contrast.model_fields_set and contrast.repeat_layers != 1:
463+
warnings.warn(
464+
'For a custom layers or custom XY calculation, the "repeat_layers" setting for each '
465+
"contrast is not valid - resetting to 1.",
466+
stacklevel=2,
467+
)
468+
contrast.repeat_layers = 1
469+
return self
470+
457471
@model_validator(mode="after")
458472
def set_resample(self) -> "Project":
459473
"""If we are using a custom XY model, warn that the resample setting for each contrast must always be True."""

tests/test_project.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,29 @@ def test_set_layers(project_parameters: dict) -> None:
443443
assert project.layers == []
444444

445445

446+
@pytest.mark.parametrize(
447+
"project_parameters",
448+
[
449+
(
450+
{
451+
"model": LayerModels.CustomLayers,
452+
"contrasts": [ratapi.models.Contrast(name="Test Contrast", repeat_layers=2)],
453+
}
454+
),
455+
({"model": LayerModels.CustomXY, "contrasts": [ratapi.models.Contrast(name="Test Contrast", repeat_layers=2)]}),
456+
],
457+
)
458+
def test_set_repeat_layers(project_parameters: dict) -> None:
459+
"""If we are using a custom layers of custom XY model, the "resample" field of all the contrasts should always
460+
be 1."""
461+
with pytest.warns(
462+
match='For a custom layers or custom XY calculation, the "repeat_layers" setting for each '
463+
"contrast is not valid - resetting to 1."
464+
):
465+
project = ratapi.Project(**project_parameters)
466+
assert all(contrast.repeat_layers == 1 for contrast in project.contrasts)
467+
468+
446469
@pytest.mark.parametrize(
447470
"project_parameters",
448471
[

0 commit comments

Comments
 (0)