def test_age_evaluator(self): """Test the age evaluator which is a child class of the basic evaluator. """ report_datetime = utc.localize(datetime(2017, 12, 7)) dob = report_datetime - relativedelta(years=25) rdelta = age(dob, report_datetime) self.assertEqual(age(dob, report_datetime).years, 25) self.assertTrue(24 < getattr(rdelta, "years") < 26) self.assertFalse(25 < getattr(rdelta, "years") < 26) self.assertFalse(24 < getattr(rdelta, "years") < 25) age_eval = AgeEvaluator(age_lower=24, age_upper=26) self.assertTrue(repr(age_eval)) self.assertTrue(age_eval.in_bounds_or_raise(dob, report_datetime)) self.assertEqual(age_eval.description(), "24<AGE<26 years") age_eval = AgeEvaluator(age_lower=18) self.assertTrue(repr(age_eval)) self.assertTrue(age_eval.in_bounds_or_raise(dob, report_datetime)) self.assertEqual(age_eval.description(), "18<AGE years") age_eval = AgeEvaluator(age_lower=25, age_upper=26) self.assertRaises(ValueBoundryError, age_eval.in_bounds_or_raise, dob, report_datetime) self.assertEqual(age_eval.description(), "25<AGE<26 years") age_eval = AgeEvaluator(age_lower=24, age_upper=25) self.assertRaises(ValueBoundryError, age_eval.in_bounds_or_raise, dob, report_datetime) self.assertEqual(age_eval.description(), "24<AGE<25 years")
def age(self): consent_datetime = self.cleaned_data.get( "consent_datetime", self.instance.consent_datetime) dob = self.cleaned_data.get("dob") if consent_datetime and dob: return age(dob, consent_datetime) return None
def age_in_years(context, born): reference_datetime = context.get("reference_datetime") or get_utcnow() try: age_in_years_ = age(born, reference_datetime).years except AgeValueError: age_in_years_ = None return age_in_years_ or born
def screening_age_in_years(self) -> int: """Returns age in years calculated from dob relative to screening datetime""" try: rdelta = age(self.dob, self.subject_screening.report_datetime.date()) except AgeValueError as e: raise forms.ValidationError(str(e)) return rdelta.years
def age_at_consent(self): """Returns a relativedelta.""" try: return age(self.get_dob(), self.consent_datetime) except AttributeError as e: if "dob" in str(e): raise NotImplementedError( f"Missing model mixin. Got `{str(e)}`.") raise
def age(self) -> Optional[relativedelta]: consent_datetime = self.cleaned_data.get( "consent_datetime", self.instance.consent_datetime) dob = self.cleaned_data.get("dob") if consent_datetime and dob: try: return age(dob, consent_datetime) except AgeValueError as e: raise forms.ValidationError(str(e)) return None
def __init__( self, weight_kg=None, height_cm=None, lower_bmi_value=None, upper_bmi_value=None, dob=None, report_datetime=None, **kwargs, ): if not weight_kg or not height_cm: raise CalculatorError( f"Unable to calculate BMI. Got {weight_kg}kg, {height_cm}cm") if age(dob, report_datetime).years < 18: raise CalculatorError("Unable to calculate BMI. Got age<18") self.lower = float(lower_bmi_value or 5.0) self.upper = float(upper_bmi_value or 60.0) self.weight = float(weight_kg) self.height = float(height_cm) / 100.0 self.raw_bmi_value = self.weight / (self.height**2) if not (self.lower <= self.raw_bmi_value <= self.upper): raise CalculatorError( f"BMI value is absurd. Using {self.weight}kg, {self.height}m. Got {self.value}." )
def age_in_years(self): return age(self.subject_consent.dob, self.report_datetime).years
def get_age(self: MoccaRegisterFormValidatorStub): return age( born=self.cleaned_data.get("dob"), reference_dt=self.instance.report_datetime, ).years
def age_at_consent(self): """Returns a relativedelta. """ return age(self.dob, self.consent_datetime)
def age_in_years(self) -> int: return age(self.subject_consent.dob, to_utc(self.report_datetime)).years