Skip to main content

covnorm

Scikit-learn compatible transformer for robust conditional Z-score normalization of a continuous marker against categorical and continuous covariates.

A single polynomial curve is fitted over mu and sigma across the entire dataset using rolling overlapping bins sorted by the continuous covariate. Mu and sigma per bin are estimated via Q-Q regression on Box-Cox transformed values, with iterative conditional Z-score outlier rejection (samples with |Z| > 3.372 against the fitted surface are dropped and the surface is refit, repeated up to n_iterations times). The Box-Cox lambda is selected by a grid search that maximises Q-Q linearity rather than marginal normality.

After fitting the shared surface, a post-hoc categorical correction is applied: the mean and standard deviation of the base Z-scores are computed within each categorical group and used to rescale final Z-scores, giving Z_corrected = (Z_base - mu_cat) / sigma_cat. This avoids overfitting independent surfaces to small groups.

Data requirement: target values must be strictly positive for Box-Cox (default). If your data contains zeros, set zero_handles="eps" (adds a small epsilon before Box-Cox) or zero_handles="yeojohnson" (switches to Yeo-Johnson transform). Negative values are not supported.

Supports up to 2 categorical covariates and up to 2 continuous covariates. With 2 continuous covariates, k-NN overlapping windows replace the 1D rolling windows; set n_bins >= 10 in that case.

Installation

pip install covnorm

Usage

import numpy as np
from covnorm import RobustConditionalNormalizer

# Separate covariates from markers before passing to the normalizer.
data = np.load("data.npy")  # shape (n_samples, 4): [sex, batch, age, marker]

sex_batch = data[:, [0, 1]]  # categorical covariates, shape (n_samples, 2)
age       = data[:, [2]]     # continuous covariate,  shape (n_samples, 1)
marker    = data[:, [3]]     # marker to normalize,   shape (n_samples, 1)

normalizer = RobustConditionalNormalizer(
    categorical_vals=sex_batch,
    continuous_vals=age,
    n_bins=6,                      # target number of rolling windows
    bin_size=120,                  # samples per rolling window
    transform_continuous="zscore", # training-set Z-score for polynomial stability
)

marker_norm = normalizer.fit_transform(marker)

marker_norm has shape (n_samples, n_markers) and contains only the Z-scored marker columns — no covariate columns are included in the output.

It follows the scikit-learn fit / transform / fit_transform API and is compatible with Pipeline.

Inference on new samples

transform() accepts optional categorical_vals and continuous_vals overrides so a fitted normalizer can be applied to new samples with different covariate values:

marker_new_norm = normalizer.transform(
    marker_new,
    categorical_vals=sex_batch_new,
    continuous_vals=age_new,
)

Important: when marker_new contains fewer rows than the training set (including the common case of a single sample), you must pass categorical_vals and continuous_vals whose row count matches marker_new.shape[0]. If the overrides are omitted, transform() falls back to the training-time covariate arrays, which have a different number of rows, causing an IndexError.

Parameters

Parameter Default Description
categorical_vals — Array of shape (n_samples, n_cat) or (n_samples,) with categorical covariate values (e.g. sex, batch). Pass [] when there are no categorical covariates.
continuous_vals — Array of shape (n_samples, n_cont) or (n_samples,) with continuous covariate values (e.g. age, BMI). Pass [] when there are no continuous covariates.
n_bins 6 Target number of rolling windows (controls stride)
bin_size 120 Samples per rolling window (Mørkved et al. use 120)
degree 3 Polynomial degree of the mu/sigma curve
n_iterations 3 Maximum iterative conditional outlier-removal passes
transform_continuous None Continuous-covariate transform: None keeps the original values, "log10" applies a base-10 logarithm, and "zscore" subtracts the training mean and divides by the population standard deviation. Fitted parameters are reused at inference.
log_transform_continuous False Backward-compatible alias for transform_continuous="log10". Cannot be combined with transform_continuous="zscore".
zero_handles "eps" Strategy for zero values in the target: "eps" adds a small epsilon before Box-Cox; "yeojohnson" switches to Yeo-Johnson transform (supports zeros and negatives)

transform_continuous acts on the continuous covariates, whereas Box-Cox or Yeo-Johnson acts on each marker. The two transformations serve different purposes. "zscore" improves the numerical conditioning of polynomial features without changing the polynomial function class. "log10" is a non-linear modelling choice and requires all continuous covariates to be strictly positive.

Plotting

from covnorm import plot_covariate_space

fig = plot_covariate_space(
    normalizer,
    data,
    covariate_labels=["age"],
    analyte_label="marker",
)
fig.savefig("normalization_surface.png", dpi=150)

plot_covariate_space visualises the fitted polynomial surface on top of the raw data:

  • 1 continuous covariate — scatter of raw values with a filled ribbon μ(x) ± n_sigma · σ(x) back-transformed to the original space.
  • 2 continuous covariates — 3-D surface plot of μ(x₁, x₂) with the per-bin estimates scattered on top.

Metadata

Release files for covnorm 0.7.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for covnorm 0.7.5
File Size Uploaded Provenance Actions
covnorm-0.7.5.tar.gz 28.7 kB With provenance Details for covnorm-0.7.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for covnorm 0.7.5
File Interpreter ABI Platform Actions
covnorm-0.7.5-py3-none-any.whl Python 3 none any Details for covnorm-0.7.5-py3-none-any.whl

Total release size: 51.2 kB

Release files / covnorm-0.7.5.tar.gz

Download URL covnorm-0.7.5.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b9d6dda95c7d5e46a99857c5459adfcc8df18c5ae22bfbe72a7191b892944214
BLAKE2b-256 checksum
How to use checksums
96a84219aa6a213e02af494da0b3a4aaf59833ed5ee241f9470a0d89af9d85fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.

Transparency log

Release files / covnorm-0.7.5-py3-none-any.whl

Download URL covnorm-0.7.5-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b8dbc630562c3b10ddf586301837d0e95f8c5fbf8a9ed7cfadda5d1b3f4b533
BLAKE2b-256 checksum
How to use checksums
32a334383cd8472b7169b6c823950f818f1f2daf589fc3278ab004ffb3a0f030
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.

Transparency log

Security for covnorm 0.7.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for covnorm-0.7.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page