symphony_dt.io¶
symphony_dt.io ¶
Input and output: tables, datasets, posteriors and adapters from clinical data layouts.
DatasetMapping ¶
Bases: _Strict
A full mapping from source tables to a TwinDataset.
Validation: TESTED.
from_fhir ¶
from_fhir(
bundles: Iterable[Mapping[str, Any] | str | Path],
mapping: DatasetMapping | str | Path = "fhir",
) -> TwinDataset
Build a dataset from HL7 FHIR R4 Bundles of Patient and Observation resources.
Bundles may be dicts, JSON strings or paths. An Observation's code is matched as
system#code against the mapping's endpoint_values; the shipped codes are placeholders to set.
Age is taken at the first observation from Patient.birthDate; sex from Patient.gender.
Validation: TESTED.
from_mapping ¶
from_mapping(
tables: Mapping[str, Any],
mapping: DatasetMapping | str | Path,
) -> TwinDataset
Build a dataset from named tables (pandas or polars) with any mapping.
Validation: TESTED.
from_omop ¶
from_omop(
tables: Mapping[str, Any],
mapping: DatasetMapping | str | Path = "omop",
) -> TwinDataset
Build a dataset from OMOP CDM tables: PERSON, MEASUREMENT, OBSERVATION and DRUG_EXPOSURE.
Keys of tables are table names in lower case. The shipped mapping's concept identifiers are
placeholders: copy it, set the concept ids of your vocabulary and pass the edited file. Age is
taken at the first record from year_of_birth.
Validation: TESTED.
from_ppmi_export ¶
from_ppmi_export(
directory: str | Path,
mapping: DatasetMapping | str | Path = "ppmi",
) -> TwinDataset
Build a dataset from a PPMI study-data download that the user obtained from PPMI.
Reads the files the mapping names (by default the curated MDS-UPDRS Part III, MoCA, SCOPA-AUT,
participant status and demographics exports) from directory. The package ships no PPMI data
and no derived values. Check file and column names against your download; PPMI layouts change.
Validation: TESTED. Tested on synthetic files with the documented layout only.
from_sdtm ¶
from_sdtm(
qs: Any,
dm: Any,
mapping: DatasetMapping | str | Path = "sdtm",
ex: Any | None = None,
) -> TwinDataset
Build a dataset from CDISC SDTM domains: QS (questionnaires) and DM (demographics).
Time is the study day QSDY. Test codes are matched on QSTESTCD; the shipped values are examples
to adapt to your study's controlled terminology. An optional exposure domain can carry LEDD.
Validation: TESTED.
from_wide ¶
from_wide(
frame: Any,
mapping: DatasetMapping
| str
| Path
| Mapping[str, Any] = "wide",
) -> TwinDataset
Build a dataset from one wide table: one row per visit, one column per score.
The shipped wide mapping expects participant_id, cohort, age, sex, days, updrs3,
moca, scopa_aut and ledd. Pass another mapping for other column names.
Validation: TESTED.
fit_to_inference_data ¶
fit_to_inference_data(
result: Any,
data: Any = None,
*,
globals_only: bool = True,
posterior_predictive: bool = True,
log_likelihood: bool = True,
seed: int = 0,
) -> Any
Return a fit as ArviZ InferenceData with posterior and sample statistics groups.
result is a FitResult. With globals_only, the per-participant latent blocks are left out
of the posterior. When data, the StanData the model was fitted to, is given, the result also
holds observed_data and, unless switched off, posterior_predictive and log_likelihood.
Those two groups rebuild each draw's latent path, so they need the per-participant blocks in
the chain files whatever globals_only says. Every chain must hold the same number of draws.
Validation: TESTED.
forecast_to_inference_data ¶
forecast_to_inference_data(forecast: Any) -> Any
Return a forecast as ArviZ InferenceData with a posterior predictive group over targets.
forecast is a Forecast. Its draws are pooled across chains, so the chain dimension has
length one. Targets with an observed value y give observed_data; a missing value stays
NaN. Each target's participant, endpoint, time and horizon go to constant_data.
Validation: TESTED.
apply_mapping ¶
apply_mapping(
tables: Mapping[str, DataFrame],
mapping: DatasetMapping | str | Path,
*,
metadata: dict[str, Any] | None = None,
) -> TwinDataset
Build a TwinDataset from named source tables with a mapping.
Time is re-expressed as whole days since each participant's first record across all sources
(origin: first_visit), or kept as given (origin: zero). Rows with a missing value or time are
dropped and counted in metadata["mapping_dropped"].
Validation: TESTED.
list_mappings ¶
list_mappings() -> list[str]
Return the names of the mappings shipped with the package.
Validation: TESTED.
load_mapping ¶
load_mapping(
name_or_path: str | Path | Mapping[str, Any],
) -> DatasetMapping
Load a shipped mapping by name, a YAML file by path, or a dict.
Raises MappingError with the validation detail when the mapping is malformed.
Validation: TESTED.
from_meds ¶
from_meds(
events: Any,
endpoint_codes: Mapping[str, str],
*,
sex_codes: Mapping[str, int] | None = None,
birth_code: str = "MEDS_BIRTH",
cohort_codes: Mapping[str, str] | None = None,
exposure_codes: Mapping[str, str] | None = None,
) -> TwinDataset
Build a dataset from MEDS events.
endpoint_codes maps a MEDS code to an endpoint, for example {"LOINC//UPDRS3": "updrs3"}.
Static events (no time) supply sex, cohort and birth; age at baseline comes from the birth event.
exposure_codes maps codes to visit-level exposures such as LEDD. Time becomes whole days since each
subject's first scored event.
Validation: TESTED.
to_meds ¶
to_meds(
dataset: TwinDataset,
*,
reference_date: str = "2000-01-01",
endpoint_codes: Mapping[str, str] | None = None,
) -> pd.DataFrame
Return the dataset as MEDS events, sorted by subject and time.
Each baseline sits at reference_date, so absolute dates carry no real calendar information.
Endpoint codes default to SYMPHONY//<endpoint>.
Validation: TESTED.
read_posterior ¶
read_posterior(path: str | Path) -> Any
Read an ArviZ InferenceData from netCDF.
Validation: TESTED.
write_posterior ¶
write_posterior(idata: Any, path: str | Path) -> Path
Write an ArviZ InferenceData to netCDF and return the path.
Validation: TESTED.
read_dataset ¶
read_dataset(directory: str | Path) -> TwinDataset
Read a dataset written by write_dataset and check its fingerprint.
Validation: TESTED.
read_query ¶
read_query(query: str, connection: Any) -> pd.DataFrame
Run a SQL query and return a DataFrame.
connection is a path to a DuckDB or SQLite file, or an open DB-API or SQLAlchemy connection.
Use it to pull cohort tables straight from a warehouse, for example an OMOP database.
Validation: TESTED.
read_table ¶
read_table(
path: str | Path,
format: str | None = None,
**kwargs: Any,
) -> pd.DataFrame
Read a table from CSV, TSV, Parquet, Excel or Arrow, by suffix unless format is given.
Validation: TESTED.
to_pandas ¶
to_pandas(frame: Any) -> pd.DataFrame
Return a pandas DataFrame from a pandas, polars or pyarrow table.
Validation: TESTED.
write_dataset ¶
write_dataset(
dataset: TwinDataset,
directory: str | Path,
format: str = "parquet",
) -> Path
Write a dataset as one table per frame plus dataset.json with instruments and metadata.
Validation: TESTED.
write_table ¶
write_table(
frame: Any, path: str | Path, format: str | None = None
) -> Path
Write a pandas or polars table to CSV, TSV, Parquet, Excel or Arrow.
Validation: TESTED.