Skip to content

ADSL Namespace

The df.adam.adsl namespace builds one-row-per-subject ADSL datasets.

fleet.adam.ADSLNamespace

Bases: SharedADaMVerbsMixin

Immutable pipeline builder for ADSL (one-row-per-subject) derivations.

Also registered as the adsl LazyFrame namespace, so pl.scan_parquet(...).adsl starts a fully lazy ADSL pipeline.

Inherits shared verbs from SharedADaMVerbsMixin (assign, case_when, merge, source, derivation, whereclauses, ...) and adds ADSL-specific verbs that pull summary statistics from event-level datasets (AE, CM, MH, EX…). Discover the full set with .verbs().

Source code in src/fleet/adam/adsl_namespace.py
@pl.api.register_lazyframe_namespace("adsl")
class ADSLNamespace(SharedADaMVerbsMixin):
    """
    Immutable pipeline builder for ADSL (one-row-per-subject) derivations.

    Also registered as the ``adsl`` LazyFrame namespace, so
    ``pl.scan_parquet(...).adsl`` starts a fully lazy ADSL pipeline.

    Inherits shared verbs from ``SharedADaMVerbsMixin``
    (assign, case_when, merge, source, derivation, whereclauses, ...)
    and adds ADSL-specific verbs that pull summary statistics from
    event-level datasets (AE, CM, MH, EX…).  Discover the full set with
    ``.verbs()``.
    """

    def __init__(self, df: pl.DataFrame | pl.LazyFrame) -> None:
        super().__init__(df)
        self._domain = "ADSL"

    # ------------------------------------------------------------------ #
    # ADSL-specific verbs.                                                 #
    # first_date / last_date / count_events moved to                       #
    # SharedADaMVerbsMixin so the spec path (from_spec) can dispatch them. #
    # ------------------------------------------------------------------ #

    # ------------------------------------------------------------------ #
    # ADSL: age_years — unit-aware age conversion                         #
    # ------------------------------------------------------------------ #

    def age_years(
        self,
        target: str = "AAGE",
        *,
        age_col: str = "AGE",
        age_unit_col: str = "AGEU",
        round_digits: int | None = None,
    ) -> Self:
        """Convert age to years, handling common unit strings.

        Recognises *AGEU* values: ``YEARS``, ``MONTHS``, ``WEEKS``, ``DAYS``
        (case-insensitive).  Unknown units produce null.  The value keeps full
        precision (AGE 7 MONTHS gives 0.5833...).

        Args:
            target: Name of the output column (default ``"AAGE"``).
            age_col: Source age column.
            age_unit_col: Column containing the age unit string.
            round_digits: Decimal places, rounded as SAS ``ROUND`` (half away
                from zero).  Default ``None``: no rounding.

        Returns:
            Self: The namespace with *target* appended.

        References:
            admiral ``compute_age_years`` (pharmaverse): full precision; one year is
            12 months, 52.1775 weeks or 365.25 days.
            ADR-0025: a derived value is rounded only when the call says so.

        Examples:
            Derive age in years from DM variables::

                adsl = (
                    dm_raw.adam.adsl
                    .age_years("AAGE", age_col="AGE", age_unit_col="AGEU")
                    .to_frame()
                )
        """
        from fleet.adam.utils import age_in_years as _age_in_years_expr

        return self._chain(
            _age_in_years_expr(
                age_col=age_col,
                age_unit_col=age_unit_col,
                target=target,
                round_digits=round_digits,
            )
        )

    # ------------------------------------------------------------------ #
    # ADSL: age_from_dates — date-based age calculation                   #
    # ------------------------------------------------------------------ #

    def age_from_dates(
        self,
        target: str = "AAGE",
        *,
        birth_date: str = "BRTHDT",
        reference_date: str = "TRTSDT",
        unit: Literal["years", "decimal"] = "years",
        round_digits: int | None = None,
    ) -> Self:
        """Compute age from two date columns, in completed years by default.

        ``unit="years"`` gives whole completed years (Int64): the year
        difference, minus 1 when the birthday of the reference year is not
        yet reached.  A subject born 1956-06-01 is 64 on 2021-05-31.  A 29
        February birthday is reached on 1 March in a non-leap year.  A
        reference date before birth gives a negative age.
        ``unit="decimal"`` gives days / 365.25 at full precision.  Null when
        either date is missing.

        Args:
            target: Name of the output column (default ``"AAGE"``).
            birth_date: Date-of-birth column (``pl.Date``).
            reference_date: Reference date column (``pl.Date``).
            unit: ``"years"`` (default) or ``"decimal"``.
            round_digits: Decimal places for ``unit="decimal"``, rounded as
                SAS ``ROUND``.  Default ``None``: no rounding.

        Returns:
            Self: The namespace with *target* appended.

        Raises:
            VerbUsageError: When *round_digits* is given with
                ``unit="years"``, or *unit* is unknown.

        References:
            admiral ``derive_vars_aage`` (pharmaverse) and SAS
            ``FLOOR(YRDIF(BRTHDT, TRTSDT, 'AGE'))``: completed years. ICH E7 and
            E11 age bands use completed years. ADR-0025.

        Examples:
            Derive age from birth and randomisation dates::

                adsl = (
                    dm_raw.adam.adsl
                    .age_from_dates("AAGE", birth_date="BRTHDT",
                                    reference_date="RANDDT")
                    .to_frame()
                )
        """
        from fleet.adam.utils import age_between

        schema = self._lazy.collect_schema()
        reference, birth = (
            to_day(pl.col(c), schema.get(c), verb="age_from_dates").cast(pl.Date)
            for c in (reference_date, birth_date)
        )
        expr = age_between(
            birth, reference, unit=unit, round_digits=round_digits, verb="age_from_dates"
        )
        return self._chain(expr.alias(target))

    # ------------------------------------------------------------------ #
    # ADSL: derive_period — Subperiod / Phase variables                   #
    # ------------------------------------------------------------------ #

    def derive_period(
        self,
        *,
        periods: list[dict[str, Any]],
    ) -> Self:
        """Create standard Subperiod / Phase variables.

        Generates ``AP{nn}SDT``, ``AP{nn}EDT``, and optionally
        ``TRT{nn:02d}P`` columns for each period.

        Args:
            periods: List of dicts each with ``"number"``, ``"start"``,
                ``"end"``, and optionally ``"treatment"``.

        Returns:
            Self: The namespace with period columns appended.

        Examples:
            Define two treatment periods::

                adsl = (
                    dm_raw.adam.adsl
                    .derive_period(
                        periods=[
                            {"number": 1, "start": "AP01SDT",
                             "end": "AP01EDT", "treatment": "TRT01P"},
                            {"number": 2, "start": "AP02SDT",
                             "end": "AP02EDT", "treatment": "TRT02P"},
                        ],
                    )
                    .to_frame()
                )
        """
        exprs: list[pl.Expr] = []
        for period in periods:
            n = period["number"]
            prefix = f"AP{n:02d}"
            sdt_col = period["start"]
            edt_col = period["end"]

            exprs.append(pl.col(sdt_col).alias(f"{prefix}SDT"))
            exprs.append(pl.col(edt_col).alias(f"{prefix}EDT"))

            trt = period.get("treatment")
            if trt is not None:
                trt_target = f"TRT{n:02d}P"
                if trt in self._lazy.collect_schema().names():
                    exprs.append(pl.col(trt).alias(trt_target))
                else:
                    exprs.append(pl.lit(trt).alias(trt_target))

        return self._chain(*exprs)

age_years

age_years(
    target="AAGE",
    *,
    age_col="AGE",
    age_unit_col="AGEU",
    round_digits=None
)

Convert age to years, handling common unit strings.

Recognises AGEU values: YEARS, MONTHS, WEEKS, DAYS (case-insensitive). Unknown units produce null. The value keeps full precision (AGE 7 MONTHS gives 0.5833...).

Parameters:

Name Type Description Default
target str

Name of the output column (default "AAGE").

'AAGE'
age_col str

Source age column.

'AGE'
age_unit_col str

Column containing the age unit string.

'AGEU'
round_digits int | None

Decimal places, rounded as SAS ROUND (half away from zero). Default None: no rounding.

None

Returns:

Name Type Description
Self Self

The namespace with target appended.

References

admiral compute_age_years (pharmaverse): full precision; one year is 12 months, 52.1775 weeks or 365.25 days. ADR-0025: a derived value is rounded only when the call says so.

Examples:

Derive age in years from DM variables::

adsl = (
    dm_raw.adam.adsl
    .age_years("AAGE", age_col="AGE", age_unit_col="AGEU")
    .to_frame()
)
Source code in src/fleet/adam/adsl_namespace.py
def age_years(
    self,
    target: str = "AAGE",
    *,
    age_col: str = "AGE",
    age_unit_col: str = "AGEU",
    round_digits: int | None = None,
) -> Self:
    """Convert age to years, handling common unit strings.

    Recognises *AGEU* values: ``YEARS``, ``MONTHS``, ``WEEKS``, ``DAYS``
    (case-insensitive).  Unknown units produce null.  The value keeps full
    precision (AGE 7 MONTHS gives 0.5833...).

    Args:
        target: Name of the output column (default ``"AAGE"``).
        age_col: Source age column.
        age_unit_col: Column containing the age unit string.
        round_digits: Decimal places, rounded as SAS ``ROUND`` (half away
            from zero).  Default ``None``: no rounding.

    Returns:
        Self: The namespace with *target* appended.

    References:
        admiral ``compute_age_years`` (pharmaverse): full precision; one year is
        12 months, 52.1775 weeks or 365.25 days.
        ADR-0025: a derived value is rounded only when the call says so.

    Examples:
        Derive age in years from DM variables::

            adsl = (
                dm_raw.adam.adsl
                .age_years("AAGE", age_col="AGE", age_unit_col="AGEU")
                .to_frame()
            )
    """
    from fleet.adam.utils import age_in_years as _age_in_years_expr

    return self._chain(
        _age_in_years_expr(
            age_col=age_col,
            age_unit_col=age_unit_col,
            target=target,
            round_digits=round_digits,
        )
    )

age_from_dates

age_from_dates(
    target="AAGE",
    *,
    birth_date="BRTHDT",
    reference_date="TRTSDT",
    unit="years",
    round_digits=None
)

Compute age from two date columns, in completed years by default.

unit="years" gives whole completed years (Int64): the year difference, minus 1 when the birthday of the reference year is not yet reached. A subject born 1956-06-01 is 64 on 2021-05-31. A 29 February birthday is reached on 1 March in a non-leap year. A reference date before birth gives a negative age. unit="decimal" gives days / 365.25 at full precision. Null when either date is missing.

Parameters:

Name Type Description Default
target str

Name of the output column (default "AAGE").

'AAGE'
birth_date str

Date-of-birth column (pl.Date).

'BRTHDT'
reference_date str

Reference date column (pl.Date).

'TRTSDT'
unit Literal['years', 'decimal']

"years" (default) or "decimal".

'years'
round_digits int | None

Decimal places for unit="decimal", rounded as SAS ROUND. Default None: no rounding.

None

Returns:

Name Type Description
Self Self

The namespace with target appended.

Raises:

Type Description
VerbUsageError

When round_digits is given with unit="years", or unit is unknown.

References

admiral derive_vars_aage (pharmaverse) and SAS FLOOR(YRDIF(BRTHDT, TRTSDT, 'AGE')): completed years. ICH E7 and E11 age bands use completed years. ADR-0025.

Examples:

Derive age from birth and randomisation dates::

adsl = (
    dm_raw.adam.adsl
    .age_from_dates("AAGE", birth_date="BRTHDT",
                    reference_date="RANDDT")
    .to_frame()
)
Source code in src/fleet/adam/adsl_namespace.py
def age_from_dates(
    self,
    target: str = "AAGE",
    *,
    birth_date: str = "BRTHDT",
    reference_date: str = "TRTSDT",
    unit: Literal["years", "decimal"] = "years",
    round_digits: int | None = None,
) -> Self:
    """Compute age from two date columns, in completed years by default.

    ``unit="years"`` gives whole completed years (Int64): the year
    difference, minus 1 when the birthday of the reference year is not
    yet reached.  A subject born 1956-06-01 is 64 on 2021-05-31.  A 29
    February birthday is reached on 1 March in a non-leap year.  A
    reference date before birth gives a negative age.
    ``unit="decimal"`` gives days / 365.25 at full precision.  Null when
    either date is missing.

    Args:
        target: Name of the output column (default ``"AAGE"``).
        birth_date: Date-of-birth column (``pl.Date``).
        reference_date: Reference date column (``pl.Date``).
        unit: ``"years"`` (default) or ``"decimal"``.
        round_digits: Decimal places for ``unit="decimal"``, rounded as
            SAS ``ROUND``.  Default ``None``: no rounding.

    Returns:
        Self: The namespace with *target* appended.

    Raises:
        VerbUsageError: When *round_digits* is given with
            ``unit="years"``, or *unit* is unknown.

    References:
        admiral ``derive_vars_aage`` (pharmaverse) and SAS
        ``FLOOR(YRDIF(BRTHDT, TRTSDT, 'AGE'))``: completed years. ICH E7 and
        E11 age bands use completed years. ADR-0025.

    Examples:
        Derive age from birth and randomisation dates::

            adsl = (
                dm_raw.adam.adsl
                .age_from_dates("AAGE", birth_date="BRTHDT",
                                reference_date="RANDDT")
                .to_frame()
            )
    """
    from fleet.adam.utils import age_between

    schema = self._lazy.collect_schema()
    reference, birth = (
        to_day(pl.col(c), schema.get(c), verb="age_from_dates").cast(pl.Date)
        for c in (reference_date, birth_date)
    )
    expr = age_between(
        birth, reference, unit=unit, round_digits=round_digits, verb="age_from_dates"
    )
    return self._chain(expr.alias(target))

derive_period

derive_period(*, periods)

Create standard Subperiod / Phase variables.

Generates AP{nn}SDT, AP{nn}EDT, and optionally TRT{nn:02d}P columns for each period.

Parameters:

Name Type Description Default
periods list[dict[str, Any]]

List of dicts each with "number", "start", "end", and optionally "treatment".

required

Returns:

Name Type Description
Self Self

The namespace with period columns appended.

Examples:

Define two treatment periods::

adsl = (
    dm_raw.adam.adsl
    .derive_period(
        periods=[
            {"number": 1, "start": "AP01SDT",
             "end": "AP01EDT", "treatment": "TRT01P"},
            {"number": 2, "start": "AP02SDT",
             "end": "AP02EDT", "treatment": "TRT02P"},
        ],
    )
    .to_frame()
)
Source code in src/fleet/adam/adsl_namespace.py
def derive_period(
    self,
    *,
    periods: list[dict[str, Any]],
) -> Self:
    """Create standard Subperiod / Phase variables.

    Generates ``AP{nn}SDT``, ``AP{nn}EDT``, and optionally
    ``TRT{nn:02d}P`` columns for each period.

    Args:
        periods: List of dicts each with ``"number"``, ``"start"``,
            ``"end"``, and optionally ``"treatment"``.

    Returns:
        Self: The namespace with period columns appended.

    Examples:
        Define two treatment periods::

            adsl = (
                dm_raw.adam.adsl
                .derive_period(
                    periods=[
                        {"number": 1, "start": "AP01SDT",
                         "end": "AP01EDT", "treatment": "TRT01P"},
                        {"number": 2, "start": "AP02SDT",
                         "end": "AP02EDT", "treatment": "TRT02P"},
                    ],
                )
                .to_frame()
            )
    """
    exprs: list[pl.Expr] = []
    for period in periods:
        n = period["number"]
        prefix = f"AP{n:02d}"
        sdt_col = period["start"]
        edt_col = period["end"]

        exprs.append(pl.col(sdt_col).alias(f"{prefix}SDT"))
        exprs.append(pl.col(edt_col).alias(f"{prefix}EDT"))

        trt = period.get("treatment")
        if trt is not None:
            trt_target = f"TRT{n:02d}P"
            if trt in self._lazy.collect_schema().names():
                exprs.append(pl.col(trt).alias(trt_target))
            else:
                exprs.append(pl.lit(trt).alias(trt_target))

    return self._chain(*exprs)

options: show_root_heading: true heading_level: 2 members_order: source filters: ["!^_"]