Codebook#

Privacy-safe codebook of a cohort: types, missingness and coarse distributions.

The codebook describes the shape of obs and obsm without any patient-level value, so it can leave the server and be read by an analyst or a coding agent that has no access to the data:

  • every column: dtype, n, % missing, n unique;

  • numeric and duration columns: percentiles from p1 / p25 / p50 / p75 / p99, no min or max. A percentile is published only when at least min_count values lie on each side of it (p25 / p75 need 4 * min_count values, p1 / p99 100 * min_count), and it is an observed value (nearest rank), never an interpolation between two values;

  • numeric columns with at most MAX_NUMERIC_LEVELS distinct values (0/1 flags, scores, years): counts per value like a categorical column, because percentiles of a few distinct values reveal counts;

  • categorical and boolean columns: counts per level. Levels seen fewer than min_count times are not named, only counted (n_levels_suppressed);

  • datetimes: the month of p1 … p99 under the same rule, and counts per year (rare years suppressed like levels). No per-month counts;

  • dates of birth (a date/datetime column named like birthdate or date_of_birth): dtype and missingness only, as for ID-like and free-text columns;

  • struct columns (*_detail): one entry per field;

  • obsm frames: only rows of observations still in obs.

Suppression is complementary: when the suppressed levels add up to fewer than min_count, the next-smallest published levels are suppressed as well, so a small count cannot be read off as the total minus the published counts. It can still be bounded; the codebook is a safeguard against accidental disclosure, not a formal anonymisation.

The usual entry points are Cohort.codebook() (see Codebook) and the corr codebook command (see The corr command line). The functions below are the building blocks they use.

corr_vars.core.codebook.build_codebook(obs, obsm, *, primary_key, anchor, min_count=10, concept_versions=None)[source]#

Build the codebook of a cohort’s obs and obsm.

Parameters:
  • obs (pl.DataFrame) – One row per observation.

  • obsm (Mapping[str, pl.DataFrame]) – Time-series frames by variable.

  • primary_key (str) – Primary key column of obs.

  • anchor (str) – obs column the obsm record times are measured from.

  • min_count (int) – Smallest count published.

  • concept_versions (Mapping[str, Any] | None) – Cohort.concept_versions; each variable’s concept, source and version are copied in.

Returns:

{"min_count", "n_observations", "obs", "obsm"}.

Return type:

dict[str, Any]

corr_vars.core.codebook.summarise_column(series, *, min_count=10)[source]#

Describe one column without any patient-level value.

Parameters:
  • series (pl.Series) – The column.

  • min_count (int) – Smallest count published.

Returns:

dtype, kind, n, pct_missing, n_unique and, by kind:

  • numeric: quantiles, or levels + n_levels_suppressed when it has at most MAX_NUMERIC_LEVELS distinct values;

  • duration: quantiles_hours;

  • categorical, boolean: levels + n_levels_suppressed;

  • datetime: quantiles_month ("YYYY-MM") and years + n_years_suppressed;

  • struct: fields;

  • id, date_of_birth, free_text, other: nothing more.

Return type:

dict[str, Any]

corr_vars.core.codebook.summarise_frame(frame, obs, *, primary_key, anchor, min_count=10)[source]#

Describe one obsm frame.

Parameters:
  • frame (pl.DataFrame) – The time-series frame.

  • obs (pl.DataFrame) – The cohort’s obs (for the keys and the anchor).

  • primary_key (str) – Primary key column.

  • anchor (str) – obs column the record times are measured from.

  • min_count (int) – Smallest count published.

Returns:

Columns and dtypes, rows per observation, hours from the anchor and summaries of value and value_unit, all counted over the rows whose primary_key is in obs (obsm can still hold rows of observations excluded after extraction).

Return type:

dict[str, Any]

corr_vars.core.codebook.codebook_markdown(codebook)[source]#

Render a codebook as Markdown, for people and coding agents.

Parameters:

codebook (Mapping[str, Any]) – A result of build_codebook().

Returns:

The Markdown document.

Return type:

str

corr_vars.core.codebook.write_codebook(codebook, output_dir)[source]#

Write codebook.md and codebook.json into output_dir.

Parameters:
  • codebook (Mapping[str, Any]) – A result of build_codebook().

  • output_dir (str | PathLike[str]) – Target directory, created if missing.

Returns:

The Markdown and JSON paths.

Return type:

tuple[Path, Path]

corr_vars.core.codebook.DEFAULT_MIN_COUNT: Final[int] = 10#

Smallest count published; smaller level, year and sample counts are suppressed.