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_countvalues lie on each side of it (p25 / p75 need4 * min_countvalues, p1 / p99100 * min_count), and it is an observed value (nearest rank), never an interpolation between two values;numeric columns with at most
MAX_NUMERIC_LEVELSdistinct 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_counttimes 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
birthdateordate_of_birth): dtype and missingness only, as for ID-like and free-text columns;struct columns (
*_detail): one entry per field;obsmframes: only rows of observations still inobs.
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
obsandobsm.- 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) –
obscolumn theobsmrecord 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_uniqueand, by kind:numeric:
quantiles, orlevels+n_levels_suppressedwhen it has at mostMAX_NUMERIC_LEVELSdistinct values;duration:
quantiles_hours;categorical, boolean:
levels+n_levels_suppressed;datetime:
quantiles_month("YYYY-MM") andyears+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
obsmframe.- 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) –
obscolumn 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
valueandvalue_unit, all counted over the rows whose primary_key is in obs (obsmcan 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.mdandcodebook.jsoninto 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.