"""Functions to verify the validity of parameters:author: Michel Bierlaire:date: Thu Dec 1 16:22:34 2022"""from__future__importannotationsimportnumbersfromtypingimportTYPE_CHECKINGimportbiogeme.optimizationasoptfrombiogeme.second_derivativesimportSecondDerivativesModeifTYPE_CHECKING:frombiogeme.default_parametersimportParameterValue
[docs]defis_number(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is a number :param x: value of the parameter to check """ifisinstance(x,numbers.Number):returnTrue,NonereturnFalse,'Value must be a number'
[docs]defzero_one(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is between zero and one :param x: value of the parameter to check """check,msg=is_number(x)ifnotcheck:returncheck,msgif0<=x<=1:returnTrue,NonereturnFalse,'Value must be between zero and one'
[docs]defis_positive(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is positive :param x: value of the parameter to check """check,msg=is_number(x)ifnotcheck:returncheck,msgifx>0:returnTrue,NonereturnFalse,'Value must be positive'
[docs]defis_non_negative(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is non_negative :param x: value of the parameter to check """ifx>=0:returnTrue,NonereturnFalse,'Value must be non negative'
[docs]defis_integer(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is integer :param x: value of the parameter to check """ifisinstance(x,numbers.Integral):returnTrue,NonereturnFalse,'Value must be an integer'
[docs]defcheck_algo_name(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is a valid algorithm name :param x: value of the parameter to check """ifnotisinstance(x,str):returnFalse,f'Parameter must be a string: {x}'possibilities=['automatic']+list(opt.algorithms.keys())ifxinpossibilities:returnTrue,NonereturnFalse,f'Value must be in: {possibilities}'
[docs]defcheck_calculating_second_derivatives(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is a valid way to calculate the second derivatives :param x: value of the parameter to check """ifnotisinstance(x,str):returnFalse,f'Parameter must be a string: {x}'possibilities=[value.valueforvalueinSecondDerivativesMode]ifxinpossibilities:returnTrue,NonereturnFalse,f'Value must be in: {possibilities}'
[docs]defis_boolean(x:ParameterValue)->tuple[bool,str|None]:"""Return true if x is a boolean :param x: value of the parameter to check :type x: float """ifisinstance(x,bool):returnTrue,NonereturnFalse,'Value must be boolean'