dags: reading nodes for a cohort build, its members and their timeline
The timeline is cut at the build's clinical and load cut-offs, so the read can be repeated. Adds apache-hamilton. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Hamilton modules for the model DAGs: each function is a node, each parameter an edge."""
|
||||
@@ -0,0 +1,44 @@
|
||||
"""The nodes every model DAG reads through: a cohort build, its members and their timeline.
|
||||
|
||||
Each node's first parameter is the asset it reads, given as an input (`Asset`), so the graph shows
|
||||
which tables a model depends on (`persistance.assets.validate` with them as sources). The repositories
|
||||
are inputs too: whoever builds the driver passes a domain's `cohorte_repository` and a
|
||||
`timeline_repository` already bound to its query.
|
||||
|
||||
The timeline is cut at the build's clinical date (`datos_as_of`) and at what had been loaded when the
|
||||
build was made (`conocida_hasta`), so reading it again gives the build's input.
|
||||
"""
|
||||
|
||||
from dataclasses import fields
|
||||
|
||||
import pandas as pd
|
||||
from persistance.aristas.oro import CohortBuild, CohortMember, CohorteRepository
|
||||
from persistance.aristas.plata import TIMELINE_COLUMNS, TimelineRepository
|
||||
from persistance.assets import Asset
|
||||
|
||||
MEMBER_COLUMNS = tuple(f.name for f in fields(CohortMember))
|
||||
|
||||
|
||||
class UnknownBuild(LookupError):
|
||||
"""The requested cohort build doesn't exist."""
|
||||
|
||||
|
||||
def cohort_build(cohorte_armado: Asset, cohort_repository: CohorteRepository, build_id: int) -> CohortBuild:
|
||||
"""The build's lineage: cohort, version, fingerprints and cut-offs."""
|
||||
build = cohort_repository.build(build_id)
|
||||
if build is None:
|
||||
raise UnknownBuild(f"cohort build {build_id} doesn't exist")
|
||||
return build
|
||||
|
||||
|
||||
def members(cohorte_miembro: Asset, cohort_repository: CohorteRepository, cohort_build: CohortBuild) -> pd.DataFrame:
|
||||
"""One row per member (`MEMBER_COLUMNS`), ordered by `pac_id`."""
|
||||
return pd.DataFrame(cohort_repository.members(cohort_build.armado_id), columns=list(MEMBER_COLUMNS))
|
||||
|
||||
|
||||
def timeline(linea_de_tiempo: Asset, members: pd.DataFrame, cohort_build: CohortBuild,
|
||||
timeline_repository: TimelineRepository) -> pd.DataFrame:
|
||||
"""The members' events (`TIMELINE_COLUMNS`), cut at the build's `datos_as_of` and `conocida_hasta`."""
|
||||
rows = timeline_repository.read(patients=members["pac_id"].tolist(), until=cohort_build.datos_as_of,
|
||||
known_until=cohort_build.conocida_hasta)
|
||||
return pd.DataFrame(rows, columns=list(TIMELINE_COLUMNS))
|
||||
Reference in New Issue
Block a user