import polars as pl
import rtflite as rtf
from importlib.resources import files
= pl.read_parquet(files("rtflite.data").joinpath("adsl.parquet"))
adsl = ["Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"] treatments
2 Study Population
This article demonstrates how to create an analysis population overview table.
2.1 Setup
2.2 Create Population Table
# Define populations to analyze
= {
populations "Participants in population": None,
"Participants included in ITT population": "ITTFL",
"Participants included in efficacy population": "EFFFL",
"Participants included in safety population": "SAFFL"
}
# Calculate statistics for each population
= []
table_data = adsl.group_by("TRT01P").agg(total=pl.len())
totals
for pop_name, flag in populations.items():
= [pop_name]
row
# Filter and calculate counts
= adsl if flag is None else adsl.filter(pl.col(flag) == "Y")
df_pop = (
stats "TRT01P")
df_pop.group_by(=pl.len())
.agg(n="TRT01P")
.join(totals, on=(100.0 * pl.col("n") / pl.col("total")).round(1))
.with_columns(pct
)
# Format results for each treatment
for trt in treatments:
= stats.filter(pl.col("TRT01P") == trt)
trt_data if trt_data.height > 0:
= trt_data["n"][0], trt_data["pct"][0]
n, pct str(n) if flag is None else f"{n} ({pct:5.1f})")
row.append(else:
"0 ( 0.0)")
row.append(
table_data.append(row)
= pl.DataFrame(table_data, schema=[""] + treatments, orient="row") df_overview
2.3 Create RTF Output
= rtf.RTFDocument(
doc_overview =df_overview,
df=rtf.RTFTitle(
rtf_title=["Analysis Population", "All Participants Randomized"]
text
),=rtf.RTFColumnHeader(
rtf_column_header=["", "Placebo\nn (%)", "Xanomeline Low Dose\nn (%)", "Xanomeline High Dose\nn (%)"],
text=[4, 2, 2, 2],
col_rel_width=["l", "c", "c", "c"],
text_justification
),=rtf.RTFBody(
rtf_body=[4, 2, 2, 2],
col_rel_width=["l", "c", "c", "c"],
text_justification
),=rtf.RTFSource(text=["Source: ADSL dataset"])
rtf_source
)
"../rtf/tlf_population.rtf") doc_overview.write_rtf(