Source code for imednet.models.study_structure
"""Models for representing the hierarchical structure of a study."""
from __future__ import annotations
from datetime import datetime
from pydantic import Field
from imednet.models.base import ImednetBaseModel
# Import existing models needed for type hints and potentially reuse
from .forms import Form
from .intervals import Interval
from .variables import Variable
# Define a structure for Forms within the context of an Interval, including variables
# Define a structure for Intervals, containing FormStructures
[docs]class IntervalStructure(ImednetBaseModel):
"""Hierarchical representation of an interval including its forms."""
# Key identifying fields
interval_id: int = Field(..., alias="intervalId")
interval_name: str = Field(..., alias="intervalName")
# Additional relevant fields from Interval
interval_sequence: int = Field(..., alias="intervalSequence")
interval_description: str = Field(..., alias="intervalDescription")
interval_group_name: str = Field(..., alias="intervalGroupName")
disabled: bool | None = Field(default=None, alias="disabled")
date_created: datetime = Field(..., alias="dateCreated")
date_modified: datetime = Field(..., alias="dateModified")
# Nested forms
forms: list[FormStructure] = Field(default_factory=list)
[docs] @classmethod
def from_interval(cls, interval: Interval, forms: list[FormStructure]) -> IntervalStructure:
"""Creates IntervalStructure from an Interval model and its associated FormStructures."""
interval_data = interval.model_dump(by_alias=True)
# Remove the 'forms' key to avoid multiple values for keyword argument 'forms'
interval_data.pop("forms", None)
return cls(**interval_data, forms=forms)
# Define the root StudyStructure model
[docs]class StudyStructure(ImednetBaseModel):
"""Hierarchical representation of a full study including intervals and forms."""
study_key: str = Field(..., alias="studyKey")
intervals: list[IntervalStructure] = Field(default_factory=list)