Skip to content

About

Validate YAML clinical data contracts against Parquet files

Topics

Resources

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

clinical-contract

Write healthcare data contracts, validate their structure, and check CSV or Parquet datasets against them.

PyPI Python License: MIT

clinical-contract is a lightweight tool for teams that need to describe, share, and verify clinical data expectations.

It helps you:

  • write a data contract with a guided web editor or YAML;
  • validate that the contract is correctly composed;
  • check that a real .parquet or .csv file conforms to the expected schema;
  • run expressive SQL quality rules with DuckDB using equality, thresholds, or ranges;
  • use the same logic in the browser, the CLI, and Python pipelines.

clinical-contract web editor demo

Live Web App

The project includes a static browser application designed for GitHub Pages:

Open the web editor

The web app lets users create or load a contract, edit it visually, inspect the generated YAML, validate the contract, upload a CSV/Parquet file, preview the dataset, and run schema/quality checks directly in the browser with PyScript, Pyodide, and DuckDB.

Why It Exists

Clinical data exchanges often fail because the expected dataset is documented in one place while the real delivered file follows another reality.

clinical-contract keeps the expectation and the verification close together:

  • the contract explains what the dataset should contain;
  • the checker verifies what the dataset actually contains;
  • the quality rules make important assumptions executable.

This makes data delivery easier to review, easier to automate, and easier to discuss between data producers and data consumers.

What It Checks

clinical-contract currently focuses on three practical layers:

  1. Contract structure Required metadata, description fields, schema definitions, columns, and quality rules are validated before checking data.

  2. Schema compatibility Required columns must exist and detected DuckDB types must match the contract logical or physical types.

  3. Quality rules SQL checks are executed against the loaded CSV/Parquet tables and reported as passed or failed. Rules may join several contract tables.

Python Package

The same engine is available as a Python package on PyPI.

Python 3.11 or newer is required.

pip install clinical-contract

Validate a contract:

clinical-contract validate site/examples/clinical-template.yaml

Check a data file:

clinical-contract check site/examples/clinical-template.yaml site/examples/clinical_template.parquet

For a multi-table contract, pass one file per table. Each filename stem must match its schema name:

clinical-contract check contract.yaml orders.parquet line_items.csv

Display the installed version with clinical-contract --version.

Use it from Python to validate a contract structure:

from clinical_contract import DataContract, load_raw

raw_contract = load_raw("contract.yaml")
report = DataContract.validate_structure(raw_contract)

print(report.success)

Or check a data file against a contract:

from clinical_contract import load_contract

contract, _ = load_contract("contract.yaml")
report = contract.check("data.parquet")

print(report.success)

Multi-table checks accept a list of named files or an explicit mapping, which is useful for in-memory data:

report = contract.check(["orders.parquet", "line_items.csv"])
report = contract.check({"orders": orders_bytes, "line_items": lines_bytes})

Security Model

Data contracts are treated as untrusted input. Each SQL quality rule must contain exactly one read-only SELECT statement and return one finite numeric value. Commands that modify data, files, extensions, or DuckDB settings are rejected.

Before quality rules run, each selected CSV or Parquet file is loaded into an isolated in-memory DuckDB connection. External file and network access is then disabled, extensions are locked down, and execution resources are limited. The web application performs these checks locally in the browser and does not upload the contract or dataset.

Read-only SQL can still be computationally expensive. Contracts received from an untrusted organization should therefore be reviewed before automated execution on shared production infrastructure.

Current Limitations

  • Only CSV and Parquet data files are currently supported.
  • Type validation is limited to the logical and physical types listed in the supported type catalog. Unsupported types are rejected during contract validation.
  • CSV column types are inferred and are therefore less reliable than the explicit types stored in Parquet files.
  • The web application runs entirely in the browser and does not upload contracts or datasets. Its performance and maximum practical file size therefore depend on the memory and processing resources available on the user's machine.

See planned improvements and open issues.

Local Development

Clone the repository:

git clone https://github.com/artheioupfat/clinical-contract.git
cd clinical-contract

Install Python dependencies:

uv sync --extra dev

Run Python tests:

uv run pytest -v

Install web dependencies:

npm ci

Run site tests:

npm run test:site

Build the site CSS:

npm run build:site:css

Serve the static site locally:

python3 -m http.server 8000 --directory site

Then open http://127.0.0.1:8000.

Project Structure

src/clinical_contract/   Python package and validation engine
site/                    Static web app for GitHub Pages
site/examples/           Bundled contract/dataset example pairs
tests/                   Python test suite
site/tests/              JavaScript site tests

Status

The project is still evolving. The current focus is to keep the Python library, CLI, and browser app aligned around one DuckDB-based validation engine.

License

MIT — see LICENSE for details.

Author

Arthur PRIGENT — GitHub

About

Validate YAML clinical data contracts against Parquet files

Topics

Resources

Contributing

Security policy

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages