Plotting#

corr_vars.plot draws cohort figures with matplotlib.

Flowcharts#

Cohort flowcharts drawn with matplotlib.

Two layouts read the same ChangeTrackerPipeline:

  • cascade — every tracker step in sequence, each with a dashed side box holding the number of observations it removed (and its substeps, if any). The audit view.

  • profile — a Lancet-style trial profile: screened → one box listing every exclusion reason → included → optional arms. The publication view.

Layout uses explicit coordinates: the axes fill the figure, one data unit is one inch and y runs downward from the top of the page, so box heights computed from the font size are exact. Text widths are measured with TextPath before any figure exists, which lets every wrap point and the page height be fixed up front.

Most studies never call these functions directly: Cohort.to_figureone() draws the cohort’s inclusion/exclusion steps and passes its keyword arguments on.

# Cascade: every step, each with what it removed
fig = cohort.to_figureone()

# Trial profile, split into arms by a column of obs, saved as PDF
fig = cohort.to_figureone(
    layout="profile",
    groupby="sex",
    group_labels={"M": "Male", "F": "Female"},
    output_file="flowchart.pdf",
)

# Style fields of FlowchartStyle can be passed the same way
fig = cohort.to_figureone(thousands_sep="'", page_width=7.0)
ChangeTrackerPipeline.create_flowchart(title=None, output_file=None, *, layout='cascade', obs=None, **kwargs)[source]#

Draws a flowchart of the tracked changes with matplotlib.

Parameters:
  • title (str | None) – Optional title above the chart.

  • output_file (PathLike | str | None) – If given, save the figure there; the format follows the extension (.pdf, .svg, .png, …).

  • layout (Literal['cascade', 'profile']) – "cascade" draws every step with a dashed box of what it removed (substeps listed). "profile" draws a trial profile: screened, one box with all exclusion reasons, included and optional arms; it requires a simple narrowing chain.

  • obs (DataFrame | None) – Observations of the final cohort, needed by layout="profile" with groupby.

  • **kwargs (Any) – Passed to corr_vars.plot.flowchart.cascade_flowchart() or corr_vars.plot.flowchart.profile_flowchart() (text templates, screen_step, labels, groupby, group_labels, group_order) and style fields of corr_vars.plot.flowchart.FlowchartStyle (e.g. thousands_sep, font_family, page_width).

Returns:

The flowchart.

Return type:

matplotlib.figure.Figure

Examples

>>> fig = tracker.create_flowchart(layout="profile", thousands_sep="'")
>>> fig.savefig("flowchart.pdf")
corr_vars.plot.flowchart.cascade_flowchart(tracker, *, title=None, output_file=None, step_text='{description} (n={n})', dropped_text='Dropped (n={n})', style=None, **style_overrides)[source]#

Draw every tracker step in sequence, each with a dashed box of what it removed.

Parameters:
  • tracker (ChangeTrackerPipeline) – The change tracker to draw.

  • title (str | None) – Optional title above the chart.

  • output_file (PathLike | str | None) – If given, save the figure there; the format follows the extension.

  • step_text (str) – Template of a main box; {description} and {n} (remaining).

  • dropped_text (str) – Header of a dropped box; {n} (removed by the step).

  • style (FlowchartStyle | None) – Base style; style_overrides replace single fields of it.

  • style_overrides (Any)

Returns:

The flowchart.

Return type:

matplotlib.figure.Figure

corr_vars.plot.flowchart.profile_flowchart(tracker, obs=None, *, title=None, output_file=None, screen_step=None, labels=None, groupby=None, group_labels=None, group_order=None, screened_text='{n} assessed for eligibility', excluded_text='{n} excluded', included_text='{n} included in the analysis', arm_text='{n} {label}', footnote_text='Criteria that excluded no patients: {reasons}.', style=None, **style_overrides)[source]#

Draw a trial profile: screened → excluded (all reasons) → included → arms.

Step-level counts only: substeps are not listed, since their matches may overlap. Steps that removed nobody are named in a footnote.

Parameters:
  • tracker (ChangeTrackerPipeline) – The change tracker to draw.

  • obs (DataFrame | None) – Observations of the final cohort; required when groupby is given.

  • title (str | None) – Optional title above the chart.

  • output_file (PathLike | str | None) – If given, save the figure there; the format follows the extension.

  • screen_step (str | None) – Substring of the step description whose result is the screened population. Default: the tracker’s initial snapshot.

  • labels (Mapping[str, str] | None) – Description substring → display text for the exclusion reasons. Steps without a match show their description without tracker scaffolding.

  • groupby (str | None) – Column of obs that splits the included cohort into arms.

  • group_labels (Mapping[Any, str] | None) – Group value → arm label.

  • group_order (Sequence[str] | None) – Arm labels in display order; unnamed arms follow, sorted.

  • screened_text (str) – Template of the top box; {n}.

  • excluded_text (str) – Header of the exclusion box; {n}.

  • included_text (str) – Template of the included box; {n}.

  • arm_text (str | Mapping[str, str]) – Template of an arm box ({n}, {label}), or a mapping from arm label to such a template.

  • footnote_text (str) – Template of the footnote; {reasons}.

  • style (FlowchartStyle | None) – Base style; style_overrides replace single fields of it.

  • style_overrides (Any)

Returns:

The flowchart.

Return type:

matplotlib.figure.Figure

Raises:

ValueError – See profile_counts().

corr_vars.plot.flowchart.profile_counts(tracker, obs=None, *, screen_step=None, labels=None, groupby=None, group_labels=None, group_order=None)[source]#

Reduce the tracker to the tiers of a trial profile and check they balance.

Raises:

ValueError – If screen_step matches no step, a step adds observations, the reasons do not sum to screened − included, groupby is given without obs or is not a column of it, or the arms do not sum to included.

Parameters:
  • tracker (ChangeTrackerPipeline)

  • obs (DataFrame | None)

  • screen_step (str | None)

  • labels (Mapping[str, str] | None)

  • groupby (str | None)

  • group_labels (Mapping[Any, str] | None)

  • group_order (Sequence[str] | None)

Return type:

ProfileCounts

class corr_vars.plot.flowchart.ProfileCounts(screened, included, reasons, zero_reasons, arms)[source]#

Every number the profile layout draws.

Parameters:
  • screened (int)

  • included (int)

  • reasons (list[tuple[str, int]])

  • zero_reasons (list[str])

  • arms (list[tuple[str, int]])

class corr_vars.plot.flowchart.FlowchartStyle(page_width=8.27, margin=0.25, top_margin=0.3, bottom_margin=0.25, spine_x=2.45, main_box_width=3.4, side_x=5.05, arm_box_width=2.0, arm_gap=0.3, pad_x=0.1, pad_y=0.09, line_spacing=1.24, count_col_width=0.42, count_gap=0.1, header_gap=0.09, item_gap=0.035, side_top_offset=0.3, drop_after_side=0.4, min_drop=0.7, split_gap=0.6, split_bar_frac=0.5, footnote_gap=0.32, title_gap=0.2, font_family='DejaVu Sans', font_size=9.0, footnote_size=7.5, title_size=11.0, box_linewidth=0.8, connector_linewidth=0.8, arrow_size=8.0, ink='black', fill='white', thousands_sep=',', dpi=300)[source]#

Geometry, typography and number format of a flowchart.

All lengths are in inches and double as drawing coordinates. Override single fields by passing them as keyword arguments to the flowchart functions, e.g. create_flowchart(thousands_sep="'", page_width=7.0).

Parameters:
  • page_width (float)

  • margin (float)

  • top_margin (float)

  • bottom_margin (float)

  • spine_x (float)

  • main_box_width (float)

  • side_x (float)

  • arm_box_width (float)

  • arm_gap (float)

  • pad_x (float)

  • pad_y (float)

  • line_spacing (float)

  • count_col_width (float)

  • count_gap (float)

  • header_gap (float)

  • item_gap (float)

  • side_top_offset (float)

  • drop_after_side (float)

  • min_drop (float)

  • split_gap (float)

  • split_bar_frac (float)

  • footnote_gap (float)

  • title_gap (float)

  • font_family (str)

  • font_size (float)

  • footnote_size (float)

  • title_size (float)

  • box_linewidth (float)

  • connector_linewidth (float)

  • arrow_size (float)

  • ink (str)

  • fill (str)

  • thousands_sep (str)

  • dpi (int)

Movements#

plot_movements() draws the ICU stays of one hospital case on a timeline, one lane per ICU stay. It needs a cohort with obs_level="icu_stay".

from corr_vars.plot.movement import plot_movements

case_id = cohort.obs["case_id"][0]
fig = plot_movements(cohort, case_id)
fig.savefig("movements.png", dpi=150)
corr_vars.plot.movement.plot_movements(cohort, case_id)[source]#
Parameters:
  • cohort (Cohort)

  • case_id (str)

Return type:

Figure