Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions care/utils/evaluators/evaluation_metric/patient_age.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import date
from enum import Enum

from dateutil.relativedelta import relativedelta
from django.utils import timezone
from pydantic import BaseModel, StrictInt, model_validator

from care.utils.evaluators.evaluation_metric.base import EvaluationMetricBase
from care.utils.registries.evaluation_metric import (
Expand All @@ -10,6 +12,29 @@
)


class ValueType(str, Enum):
years = "years"
months = "months"
days = "days"


class ValueSpec(BaseModel):
value: StrictInt
value_type: ValueType = ValueType.years


Comment thread
nandkishorr marked this conversation as resolved.
class RangeSpec(BaseModel):
min: StrictInt
max: StrictInt
value_type: ValueType = ValueType.years

@model_validator(mode="after")
def validate_range(self):
if self.min > self.max:
raise ValueError("min value cannot be greater than max value")
return self


Comment thread
nandkishorr marked this conversation as resolved.
class PatientAgeMetric(EvaluationMetricBase):
context = "patient"
name = "patient_age"
Expand All @@ -19,6 +44,14 @@ class PatientAgeMetric(EvaluationMetricBase):
AllowedOperations.equality.value,
]

@classmethod
def validate_rule(cls, operation, value):
super().validate_rule(operation, value)
if operation == AllowedOperations.equality.value:
ValueSpec.model_validate(value)
elif operation == AllowedOperations.in_range.value:
RangeSpec.model_validate(value)
Comment on lines +47 to +53

def get_value(self):
start = self.context_object.date_of_birth or date(
self.context_object.year_of_birth, 1, 1
Expand All @@ -27,11 +60,11 @@ def get_value(self):
return relativedelta(end, start).normalized()

def convert_value_to_units(self, value, value_type):
if value_type == "years":
if value_type == ValueType.years:
return value.years
if value_type == "months":
if value_type == ValueType.months:
return value.years * 12 + value.months
if value_type == "days":
if value_type == ValueType.days:
return value.years * 365 + value.months * 30 + value.days
raise ValueError("Invalid value type")

Expand Down
Loading