Spec-Driven Pipeline with Code Extensions¶
Interactive Playground — Click the badge above to launch this notebook in Google Colab and experiment with Fleet live, no local setup needed.
This notebook demonstrates the standard workflow for Fleet in a production environment: Spec-First.
- You define as much as possible in
define.yaml. - Fleet automatically executes it.
- You write a thin layer of Python code for complex custom rules.
In [ ]:
Copied!
try:
import fleet # noqa: F401 — registers .sdtm / .adam namespaces
except ModuleNotFoundError:
import importlib
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "fleet-cdisc"])
importlib.invalidate_caches()
import polars as pl
# Normally, we just build context:
# ctx = load_study_context('examples/cdiscpilot01/adam/define.yaml')
# adsl = ctx.build('ADSL')
print("Loaded Spec context successfully.")
try:
import fleet # noqa: F401 — registers .sdtm / .adam namespaces
except ModuleNotFoundError:
import importlib
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "fleet-cdisc"])
importlib.invalidate_caches()
import polars as pl
# Normally, we just build context:
# ctx = load_study_context('examples/cdiscpilot01/adam/define.yaml')
# adsl = ctx.build('ADSL')
print("Loaded Spec context successfully.")
Loaded Spec context successfully.
Extending the Built Data¶
Suppose define.yaml generated our base ADSL pipeline, but we have a completely proprietary calculation that the specification does not natively handle via its algorithms. We simply extend the resulting dataframe!
In [2]:
Copied!
# Mocking the dataframe that would have been returned by ctx.build('ADSL')
base_adsl = pl.from_repr("""\
┌─────────┬───────────────┬──────────┬───────────────┐
│ USUBJID │ ARM │ AGE │ RFSTDTC │
│ --- │ --- │ --- │ --- │
│ str │ str │ i64 │ str │
╞═════════╪═══════════════╪══════════╪═══════════════╡
│ S001 │ Drug A │ 35 │ 2023-01-01 │
│ S002 │ Placebo │ 42 │ 2023-01-02 │
└─────────┴───────────────┴──────────┴───────────────┘
""")
# Adding our "Thin layer of code":
final_adsl = (
base_adsl.adam.adsl.source("USUBJID")
.duration("TRTDUR", start="TRTSDT", end="TRTEDT")
.derivation(
"COMPLEX_FLAG",
expression=pl.when(pl.col("TRTDUR") > 10).then(pl.lit("Y")).otherwise(pl.lit(None)),
)
.to_frame()
)
print(final_adsl)
# Mocking the dataframe that would have been returned by ctx.build('ADSL')
base_adsl = pl.from_repr("""\
┌─────────┬───────────────┬──────────┬───────────────┐
│ USUBJID │ ARM │ AGE │ RFSTDTC │
│ --- │ --- │ --- │ --- │
│ str │ str │ i64 │ str │
╞═════════╪═══════════════╪══════════╪═══════════════╡
│ S001 │ Drug A │ 35 │ 2023-01-01 │
│ S002 │ Placebo │ 42 │ 2023-01-02 │
└─────────┴───────────────┴──────────┴───────────────┘
""")
# Adding our "Thin layer of code":
final_adsl = (
base_adsl.adam.adsl.source("USUBJID")
.duration("TRTDUR", start="TRTSDT", end="TRTEDT")
.derivation(
"COMPLEX_FLAG",
expression=pl.when(pl.col("TRTDUR") > 10).then(pl.lit("Y")).otherwise(pl.lit(None)),
)
.to_frame()
)
print(final_adsl)
shape: (2, 6) ┌─────────┬─────────┬────────────┬────────────┬────────┬──────────────┐ │ STUDYID ┆ USUBJID ┆ TRTSDT ┆ TRTEDT ┆ TRTDUR ┆ COMPLEX_FLAG │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str ┆ str ┆ i64 ┆ str │ ╞═════════╪═════════╪════════════╪════════════╪════════╪══════════════╡ │ MYSTUDY ┆ S001 ┆ 2023-01-01 ┆ 2023-01-10 ┆ 10 ┆ null │ │ MYSTUDY ┆ S002 ┆ 2023-01-05 ┆ 2023-01-20 ┆ 16 ┆ Y │ └─────────┴─────────┴────────────┴────────────┴────────┴──────────────┘