Central Metrics Library Schemas (Python)
Supports CML Proforma version 3.0
A lightweight Python package providing validated schemas for the Central Metrics Library (CML) in multiple formats.
Supported formats: Apache Spark (pyspark.sql.types.StructType) and pandas (pd.DataFrame).
The Central Metrics Library (CML) defines a common structure for metrics and the metadata that describes them, so analytical teams can produce, discover, and reuse metrics consistently across the NHS. This package implements those schemas for use in data pipelines.
Why this exists
Today, metrics live in many places and many shapes—hard to find, easy to duplicate, and sometimes inconsistent. The CML aims to unify metric structures and metadata into a single, curated, service-managed library so analysts can source authoritative, consistently defined metrics, supported by appropriate security tagging and clear SME-owned definitions (purpose, methods, limitations, differences from similar measures). This repo hosts code-first schemas aligned to that aim.
Status: BETA
The CML—and therefore these schemas—are in beta while we pilot with analytical teams and iterate on feedback. Expect breaking changes as the specification evolves. Please adopt resilient coding practices and pin schema versions where appropriate.
What's in the box
Schemas for core CML entities (available in both Spark and pandas formats):
METRIC_SCHEMA— the measured value(s) and identifiersDIMENSIONS_SCHEMA— base schema for dimensions used to slice metricsSOURCE_SCHEMA— source system metadataMETADATA_SCHEMA— descriptive info: purpose, methodology, caveats, lineage, etc.RELATIONSHIPS_SCHEMA— links between metrics and other artefacts
Helper functions (available in both spark_schemas and pandas_schemas):
get_metric_schema(metric_value_dtype)— returns a metric schema with a custom metric value typecreate_dimensions_schema(dimensions)— builds a full dimensions schema from a list of dimension column namesselect_from_schema(df, schema)— selects and reorders DataFrame columns to match a schemavalidate_schema(df, schema)— validates a DataFrame's column names and types against a schema
These mirror the "draft standardised schema" referenced in the CML materials and will track the official spec as it matures.
Installation
pip install cml-schemas
Tip: Pin to a specific version (
cml-schemas==x.y.z) to protect your pipelines from breaking changes during beta.
Quick start (Spark)
Use a built-in schema
from cml_schemas import spark_schemas
# Create an empty, schema-correct DataFrame
empty_df = spark.createDataFrame([], schema=spark_schemas.METRIC_SCHEMA)
Build a dimensions schema dynamically
from cml_schemas import spark_schemas
dimensions = ["AgeGroup", "Region", "Ethnicity"]
schema = spark_schemas.create_dimensions_schema(dimensions)
empty_df = spark.createDataFrame([], schema=schema)
Validate a DataFrame against a schema
from cml_schemas import spark_schemas
# Raises TypeError with all mismatches listed if validation fails
spark_schemas.validate_schema(df, spark_schemas.METRIC_SCHEMA)
Use a typed metric schema
METRIC_SCHEMA stores metric_value as IntegerType by default. CML rules also permit float metric values — if your pipeline produces floats, use get_metric_schema() to get a schema with the correct type enforced:
from cml_schemas import spark_schemas
# metric_value as FloatType
float_schema = spark_schemas.get_metric_schema("float")
empty_df = spark.createDataFrame([], schema=float_schema)
All other fields are identical to METRIC_SCHEMA.
Select and reorder columns to match a schema
from cml_schemas import spark_schemas
# Selects only the columns defined in the schema, in schema order
df = spark_schemas.select_from_schema(df, spark_schemas.METRIC_SCHEMA)
Quick start (pandas)
Use a built-in schema
from cml_schemas import pandas_schemas
# Validate an existing DataFrame against the metric schema
pandas_schemas.validate_schema(df, pandas_schemas.METRIC_SCHEMA)
Build a dimensions schema dynamically
from cml_schemas import pandas_schemas
dimensions = ["AgeGroup", "Region", "Ethnicity"]
schema = pandas_schemas.create_dimensions_schema(dimensions)
Use a typed metric schema
from cml_schemas import pandas_schemas
# metric_value as float64
float_schema = pandas_schemas.get_metric_schema("float")
Supported metric value types: "int", "float", "string", "bool".
Select and reorder columns to match a schema
from cml_schemas import pandas_schemas
# Selects only the columns defined in the schema, in schema order
df = pandas_schemas.select_from_schema(df, pandas_schemas.METRIC_SCHEMA)
Principles for usage
- Spec-first: Schemas track the CML Data Specification (draft during beta). When the official fields or formats change, this package revs a minor or major version, with changelog notes. We recommend locking to a specific version of this package to avoid breaking changes when the schema is updated.
- Build from tidy data where possible: Aim to produce metrics by first producing outputs in tidy-data format and converting from there to the CML spec. See the CML conversion helper functions.
- RAP: Aim to develop your pipelines in line with RAP (Reproducible Analytical Pipelines) principles — see the RAP Community of Practice website for guidance.
How this maps to the CML artefacts
- CML Proforma & Spec: Informs field names, types, nullability, and relationships for
metric,metadata,relationship,source,dimension. Producers can continue to complete the proforma as documentation while using these programmatic schemas in code. - Ownership & curation: This repo does not own business definitions; SMEs own and maintain metric definitions. We only provide the technical shapes to carry those definitions consistently.
- Discovery & serving: FDP National/Metadata Explore Hub will surface metrics/metadata to end users. This package helps you produce compliant data for that ecosystem.
Versioning
Note: package versions do not map to CML Proforma versions. See the top of this README for the currently supported proforma version, or CHANGELOG.md for the proforma version supported by each past release.
This package follows Semantic Versioning:
- Major (
x.0.0) — breaking changes to schema field names, types, or nullability (expect these during beta as the CML spec evolves) - Minor (
0.x.0) — new schemas or helper functions added in a backwards-compatible way - Patch (
0.0.x) — bug fixes and non-breaking internal changes
Pin to a specific version in your pipelines (cml-schemas==x.y.z) to protect yourself from breaking changes.
Contributing
We welcome issues and PRs, especially for:
- Gaps or mismatches vs the CML spec (with references)
- Additional runtime formats (e.g., JSON Schema, SQL DDL, Polars)
- Validation and test data generators
- Developer experience improvements
Branching
Create a branch from main using a prefix that describes the type of change:
feature/your-branch-name— new functionalitypatch/your-branch-name— bug fixes or minor tweakschore/your-branch-name— non-functional changes (docs, config, CI)
Making changes
All changes must be made via a pull request on GitHub and require at least one approval before merging.
Publishing to PyPI
Before you build and publish, make sure you:
- Bump the version in
pyproject.tomlfollowing semver (see above) - Update CHANGELOG.md with the new version and a summary of changes
- Update the README.md if new instructions are needed
- Open a PR, get it approved, and merge to
main
When you are ready:
python -m venv venv
source venv/bin/activate # if on Linux or...
source venv/Scripts/activate # if on Windows
pip install build twine
python -m build
python -m twine upload dist/* # for PyPi or...
python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* # for Test PyPi
You'll be prompted for your API token, paste it in and press enter.
License
MIT
Acknowledgements
This package is inspired by and aligned to the Central Metrics Library initiative, developed with analytical teams and Platform Modernisation to fit the developing FDP National platform.
Metadata
Release files for cml-schemas 3.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cml_schemas-3.0.0.tar.gz | 12.2 kB | Without provenance | Details for cml_schemas-3.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cml_schemas-3.0.0-py3-none-any.whl | Python 3 | none | any | Details for cml_schemas-3.0.0-py3-none-any.whl |
Total release size: 22.5 kB
Release files / cml_schemas-3.0.0.tar.gz
| Download URL | cml_schemas-3.0.0.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0941c1f87ede13066dd10d2fe9279c20e097d2b26e69c5a0d820fc97627a6f6d
|
|
BLAKE2b-256 checksum How to use checksums |
95946cf0bb8a02a984ed433e454b9516ee1be81d3af2a9e030d55bb3baac4433
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / cml_schemas-3.0.0-py3-none-any.whl
| Download URL | cml_schemas-3.0.0-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee8159c280d960912dfc53517f8e76c7a8a30bfa95c65f823d1b9bee0aefe42e
|
|
BLAKE2b-256 checksum How to use checksums |
d7e17e7468f1565c9d6ce127f588538433a34bab0a0d9edea31b2ea832a9f9b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for cml-schemas 3.0.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.