Skip to content

Conversation

@tompollard
Copy link
Owner

This pull request adds two new arguments to address #132:

    show_histograms : bool, default=False
        Whether to include mini-histograms for continuous variables.
    clip_histograms : tuple or None, default (1, 99)
        If show_histograms=True, specify a (lower_percentile, upper_percentile) range to clip the
        data before generating histograms. This reduces the influence of extreme outliers.
        For example, (1, 99) clips to the 1st and 99th percentiles.
        Set to None to disable clipping and use the full range of values.

When show_histograms is set to True, histograms will be added at the end of the table. A simple example is shown below:

from tableone import TableOne, load_dataset

data = load_dataset('pn2012')
table1 = TableOne(data, show_histograms=True)
print(table1.tabulate(tablefmt="rounded_outline"))

╭───────────────────┬──────┬───────────┬──────────────┬─────────────╮
│                   │      │ Missing   │ Overall      │ Histogram   │
├───────────────────┼──────┼───────────┼──────────────┼─────────────┤
│ n                 │      │           │ 1000         │             │
│ Age, mean (SD)    │      │ 0         │ 65.0 (17.2)  │ ▂▂▃▄▆▇█▇    │
│ SysABP, mean (SD) │      │ 291       │ 114.3 (40.2) │ ▂▁▁▃█▆▃▁    │
│ Height, mean (SD) │      │ 475       │ 170.1 (22.1) │ ▁▃▅▇▇█▂▁    │
│ Weight, mean (SD) │      │ 302       │ 82.9 (23.8)  │ ▃▆█▆▄▃▁▁    │
│ ICU, n (%)        │ CCU  │           │ 162 (16.2)   │             │
│                   │ CSRU │           │ 202 (20.2)   │             │
│                   │ MICU │           │ 380 (38.0)   │             │
│                   │ SICU │           │ 256 (25.6)   │             │
│ MechVent, n (%)   │ 0    │           │ 540 (54.0)   │             │
│                   │ 1    │           │ 460 (46.0)   │             │
│ LOS, mean (SD)    │      │ 0         │ 14.2 (14.2)  │ █▆▃▁▁▁▁▁    │
│ death, n (%)      │ 0    │           │ 864 (86.4)   │             │
│                   │ 1    │           │ 136 (13.6)   │             │
╰───────────────────┴──────┴───────────┴──────────────┴─────────────╯

An example of a stratified table is below:

╭───────────────────┬──────┬───────────┬──────────────┬──────────────┬───────────────┬───────────────┬─────────────────────╮
│                   │      │ Missing   │ 0            │ 1            │ 0 Histogram   │ 1 Histogram   │ Overall Histogram   │
├───────────────────┼──────┼───────────┼──────────────┼──────────────┼───────────────┼───────────────┼─────────────────────┤
│ n                 │      │           │ 864          │ 136          │               │               │                     │
│ Age, mean (SD)    │      │ 0         │ 64.0 (17.4)  │ 71.7 (14.0)  │ ▂▂▄▄▆▇█▇      │ ▁▂▃▄▄▆██      │ ▂▂▃▄▆▇█▇            │
│ SysABP, mean (SD) │      │ 291       │ 115.4 (38.3) │ 107.6 (49.4) │ ▂▁▁▂█▇▄▁      │ ▄▁▂▆██▄▂      │ ▂▁▁▃█▆▃▁            │
│ Height, mean (SD) │      │ 475       │ 170.3 (23.2) │ 168.5 (11.3) │ ▁▂▆▇▇█▂▁      │ ▂▆▄▅█▄▅▆      │ ▁▃▅▇▇█▂▁            │
│ Weight, mean (SD) │      │ 302       │ 83.0 (23.6)  │ 82.3 (25.4)  │ ▂▆█▆▄▂▁▁      │ ▅▇█▅▃▄▁▂      │ ▃▆█▆▄▃▁▁            │
│ ICU, n (%)        │ CCU  │           │ 137 (15.9)   │ 25 (18.4)    │               │               │                     │
│                   │ CSRU │           │ 194 (22.5)   │ 8 (5.9)      │               │               │                     │
│                   │ MICU │           │ 318 (36.8)   │ 62 (45.6)    │               │               │                     │
│                   │ SICU │           │ 215 (24.9)   │ 41 (30.1)    │               │               │                     │
╰───────────────────┴──────┴───────────┴──────────────┴──────────────┴───────────────┴───────────────┴─────────────────────╯

@tompollard tompollard merged commit f18c88d into main Apr 26, 2025
3 checks passed
@tompollard tompollard deleted the tp/histogram branch April 26, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants