11"""The models module. Contains the pydantic models used by RAT to store project parameters."""
22
33import pathlib
4+ import warnings
45from itertools import count
56from 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,24 @@ 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
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+
7897
7998class Contrast (RATModel ):
8099 """A group of all of the components of a model.
@@ -515,19 +534,20 @@ class Resolution(RATModel):
515534 Parameters
516535 ----------
517536 name : str
518- The name of the background .
537+ The name of the resolution .
519538 type : TypeOptions
520- The type of background ( constant, function or data)
539+ The type of resolution: ' constant', 'data', or (NOT YET IMPLEMENTED) 'function'.
521540 source : str
522- The source data for the background ;
541+ The source data for the resolution ;
523542 - 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`.
543+ - if type is 'data', this should be empty (resolution data is in the contrast data).
544+ - if type is 'function' (NOT YET IMPLEMENTED),
545+ this should be the name of a custom function defined in `Project.custom_files`.
526546 value_1, value_2, ..., value_5 : str
527547 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.
548+ - if type is 'constant' or 'data', all values will be ignored .
549+ - if type is 'function' (NOT YET IMPLEMENTED),
550+ these values may be the names of up to 5 parameters which are passed to the function.
531551
532552 """
533553
@@ -539,3 +559,29 @@ class Resolution(RATModel):
539559 value_3 : str = ""
540560 value_4 : str = ""
541561 value_5 : str = ""
562+
563+ @field_validator ("type" )
564+ @classmethod
565+ def validate_unimplemented_resolutions (cls , type : TypeOptions ):
566+ """Raise an error if currently unsupported function resolutions are used."""
567+ if type == TypeOptions .Function :
568+ raise NotImplementedError ("Function resolutions are not yet supported." )
569+ return type
570+
571+ @model_validator (mode = "after" )
572+ def warn_parameters (self ):
573+ """Raise a warning if the parameters given are not expected for the given type."""
574+ if self .type == TypeOptions .Constant :
575+ expected_empty_fields = ["value_1" , "value_2" , "value_3" , "value_4" , "value_5" ]
576+ elif self .type == TypeOptions .Data :
577+ expected_empty_fields = ["source" , "value_1" , "value_2" , "value_3" , "value_4" , "value_5" ]
578+ else :
579+ return
580+
581+ non_empty_fields = [v for v in expected_empty_fields if getattr (self , v ) != "" ]
582+ if non_empty_fields :
583+ warnings .warn (
584+ "The following values are not recognised by this resolution type and will be ignored: "
585+ f"{ ', ' .join (non_empty_fields )} " ,
586+ stacklevel = 2 ,
587+ )
0 commit comments