Skip to content

Commit fe848ac

Browse files
committed
added validators for value warnings and unimplemented behaviour
1 parent 427e289 commit fe848ac

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

RATapi/models.py

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""The models module. Contains the pydantic models used by RAT to store project parameters."""
22

33
import pathlib
4+
import warnings
45
from itertools import count
56
from typing import Any
67

@@ -60,7 +61,7 @@ class Background(RATModel):
6061
- if type is 'function', this should be the name of a custom function defined in `Project.custom_files`.
6162
value_1, value_2, ..., value_5 : str
6263
Values required by the background.
63-
- if type is 'constant', this should be blank.
64+
- if type is 'constant', all values will be ignored.
6465
- if type is 'data', value_1 may be the parameter name for an optional offset. Other values are ignored.
6566
- if type is 'function', these values may be the names of up to 5 parameters which are passed to the function.
6667
@@ -75,6 +76,26 @@ class Background(RATModel):
7576
value_4: str = ""
7677
value_5: str = ""
7778

79+
@model_validator(mode="after")
80+
def warn_parameters(self):
81+
"""Raise a warning if the parameters given are not expected for the given type."""
82+
if self.type == TypeOptions.Constant:
83+
expected_empty_fields = ["value_1", "value_2", "value_3", "value_4", "value_5"]
84+
elif self.type == TypeOptions.Data:
85+
expected_empty_fields = ["value_2", "value_3", "value_4", "value_5"]
86+
else:
87+
return self
88+
89+
non_empty_fields = [v for v in expected_empty_fields if getattr(self, v) != ""]
90+
if non_empty_fields:
91+
warnings.warn(
92+
"The following values are not recognised by this background type and will be ignored: "
93+
f"{', '.join(non_empty_fields)}",
94+
stacklevel=2,
95+
)
96+
97+
return self
98+
7899

79100
class Contrast(RATModel):
80101
"""A group of all of the components of a model.
@@ -515,19 +536,20 @@ class Resolution(RATModel):
515536
Parameters
516537
----------
517538
name : str
518-
The name of the background.
539+
The name of the resolution.
519540
type : TypeOptions
520-
The type of background (constant, function or data)
541+
The type of resolution: 'constant', 'data', or (NOT YET IMPLEMENTED) 'function'.
521542
source : str
522-
The source data for the background;
543+
The source data for the resolution;
523544
- if type is 'constant', this should be the name of a background parameter.
524-
- if type is 'data', this should be the name of a dataset defined in `Project.data`.
525-
- if type is 'function', this should be the name of a custom function defined in `Project.custom_files`.
545+
- if type is 'data', this should be empty (resolution data is in the contrast data).
546+
- if type is 'function' (NOT YET IMPLEMENTED),
547+
this should be the name of a custom function defined in `Project.custom_files`.
526548
value_1, value_2, ..., value_5 : str
527549
Values required by the background.
528-
- if type is 'constant', this should be blank.
529-
- if type is 'data', value_1 may be the parameter name for an optional offset. Other values are ignored.
530-
- if type is 'function', these values may be the names of up to 5 parameters which are passed to the function.
550+
- if type is 'constant' or 'data', all values will be ignored.
551+
- if type is 'function' (NOT YET IMPLEMENTED),
552+
these values may be the names of up to 5 parameters which are passed to the function.
531553
532554
"""
533555

@@ -539,3 +561,31 @@ class Resolution(RATModel):
539561
value_3: str = ""
540562
value_4: str = ""
541563
value_5: str = ""
564+
565+
@field_validator("type")
566+
@classmethod
567+
def validate_unimplemented_resolutions(cls, type: TypeOptions):
568+
"""Raise an error if currently unsupported function resolutions are used."""
569+
if type == TypeOptions.Function:
570+
raise NotImplementedError("Function resolutions are not yet supported.")
571+
return type
572+
573+
@model_validator(mode="after")
574+
def warn_parameters(self):
575+
"""Raise a warning if the parameters given are not expected for the given type."""
576+
if self.type == TypeOptions.Constant:
577+
expected_empty_fields = ["value_1", "value_2", "value_3", "value_4", "value_5"]
578+
elif self.type == TypeOptions.Data:
579+
expected_empty_fields = ["source", "value_1", "value_2", "value_3", "value_4", "value_5"]
580+
else:
581+
return self
582+
583+
non_empty_fields = [v for v in expected_empty_fields if getattr(self, v) != ""]
584+
if non_empty_fields:
585+
warnings.warn(
586+
"The following values are not recognised by this resolution type and will be ignored: "
587+
f"{', '.join(non_empty_fields)}",
588+
stacklevel=2,
589+
)
590+
591+
return self

0 commit comments

Comments
 (0)