Skip to content

Baseline characteristics

Imports

from importlib.resources import files

import polars as pl

import rtflite as rtf

Ingest data

Load data from parquet file:

data_path = files("rtflite.data").joinpath("baseline.parquet")
df = pl.read_parquet(data_path)
print(df)
shape: (14, 10)
┌────────┬──────────┬───────┬──────────┬───┬───────┬──────────┬──────────┬─────────────┐
│ var    ┆ 1        ┆ 1_pct ┆ 2        ┆ … ┆ 3_pct ┆ 9999     ┆ 9999_pct ┆ var_label   │
│ ---    ┆ ---      ┆ ---   ┆ ---      ┆   ┆ ---   ┆ ---      ┆ ---      ┆ ---         │
│ str    ┆ str      ┆ f64   ┆ str      ┆   ┆ f64   ┆ str      ┆ f64      ┆ str         │
╞════════╪══════════╪═══════╪══════════╪═══╪═══════╪══════════╪══════════╪═════════════╡
│ Female ┆ 53       ┆ 10.4  ┆ 50       ┆ … ┆ 7.9   ┆ 143      ┆ 28.1     ┆ Gender      │
│ Male   ┆ 33       ┆ 6.5   ┆ 34       ┆ … ┆ 8.7   ┆ 111      ┆ 21.9     ┆ Gender      │
│ <65    ┆ 14       ┆ 2.8   ┆ 8        ┆ … ┆ 2.2   ┆ 33       ┆ 6.5      ┆ Age (Years) │
│ 65-80  ┆ 42       ┆ 8.3   ┆ 47       ┆ … ┆ 10.8  ┆ 144      ┆ 28.3     ┆ Age (Years) │
│ >80    ┆ 30       ┆ 5.9   ┆ 29       ┆ … ┆ 3.5   ┆ 77       ┆ 15.2     ┆ Age (Years) │
│ …      ┆ …        ┆ …     ┆ …        ┆ … ┆ …     ┆ …        ┆ …        ┆ …           │
│ Median ┆ 76.0     ┆ null  ┆ 77.5     ┆ … ┆ null  ┆ 77.0     ┆ null     ┆ Age (Years) │
│ Range  ┆ 52 to 89 ┆ null  ┆ 51 to 88 ┆ … ┆ null  ┆ 51 to 89 ┆ null     ┆ Age (Years) │
│ White  ┆ 78       ┆ 15.4  ┆ 78       ┆ … ┆ 14.6  ┆ 230      ┆ 45.3     ┆ Race        │
│ Black  ┆ 8        ┆ 1.6   ┆ 6        ┆ … ┆ 1.8   ┆ 23       ┆ 4.5      ┆ Race        │
│ Other  ┆ 0        ┆ 0.0   ┆ 0        ┆ … ┆ 0.2   ┆ 1        ┆ 0.2      ┆ Race        │
└────────┴──────────┴───────┴──────────┴───┴───────┴──────────┴──────────┴─────────────┘

Create header rows:

header1 = ["", "Placebo", "Drug Low Dose", "Drug High Dose", "Total"]
header2 = ["", "n", "(%)", "n", "(%)", "n", "(%)", "n", "(%)"]

Compose RTF

Create RTF document:

doc = rtf.RTFDocument(
    df=df,
    rtf_title=rtf.RTFTitle(
        text=["Demographic and Anthropometric Characteristics", "ITT Subjects"]
    ),
    rtf_column_header=[
        rtf.RTFColumnHeader(text=header1, col_rel_width=[3] + [2] * 4),
        rtf.RTFColumnHeader(
            text=header2,
            col_rel_width=[3] + [1.2, 0.8] * 4,
            border_top=[""] + ["single"] * 8,
            border_left=["single"] + ["single", ""] * 4,
        ),
    ],
    rtf_body=rtf.RTFBody(
        page_by=["var_label"],
        col_rel_width=[3] + [1.2, 0.8] * 4 + [3],
        text_justification=["l"] + ["c"] * 8 + ["l"],
        text_format=[""] * 9 + ["b"],
        border_left=["single"] + ["single", ""] * 4 + ["single"],
        border_top=[""] * 9 + ["single"],
        border_bottom=[""] * 9 + ["single"],
    ),
)

doc.write_rtf("example-baseline-char.rtf")