OMOP Schema
omop_schema is a Python package designed to read, manage, and convert OMOP (Observational Medical Outcomes Partnership) data into the correct schema. It provides tools to handle OMOP CDM (Common Data Model) tables, convert schemas between different formats (e.g., PyArrow, Polars, Pandas), and load datasets efficiently.
Features
- Schema Management: Schemas for every table of OMOP CDM 5.3, 5.4 and 5.5, checked field by field against the official CDM specification.
- Reading Tables: Lazily read an OMOP table (CSV, gzipped CSV, Parquet, or a directory of shards) into its schema with Polars, keeping site extension columns.
- Schema Conversion: Convert PyArrow schemas to Polars or Pandas-compatible schemas.
- Dataset Loading: Load datasets from CSV files into PyArrow tables, ensuring they match the defined schema.
- Optional Dependencies: Support for Polars and Pandas as optional dependencies for schema conversion.
Installation
Install the package using pip:
pip install omop_schema
To include optional dependencies:
pip install omop_schema[polars]
pip install omop_schema[pandas]
pip install omop_schema[polars,pandas]
Usage
1. Define and Retrieve OMOP Schemas
The package provides predefined schemas for OMOP CDM tables. Pick one by version (5.3, 5.4 or 5.5; "v5.4.2"
also names 5.4) and retrieve the schema of a table:
from omop_schema.versions import SUPPORTED_VERSIONS, get_schema_loader
# Or OMOPSchemaV54() from omop_schema.schema.v5_4.
schema_v54 = get_schema_loader("5.4")
# Or .get_polars_schema("concept").
concept_schema = schema_v54.get_pyarrow_schema("concept")
print(SUPPORTED_VERSIONS, schema_v54.version, schema_v54.get_table_names())
tests/cdm_spec/ holds the official CDM field lists, and the tests require every schema to have exactly their
tables, fields and types.
2. Read a Table with Polars
scan_table lazily reads a .csv, .csv.gz or .parquet file, or a directory of such shards, in the
table's schema:
from omop_schema.scan import scan_table
from omop_schema.versions import get_schema_loader
# The table name is taken from the path.
notes = scan_table("path/to/omop/note", get_schema_loader("5.4"))
- Column names are lowercased first, so uppercase headers (as SQL Server exports write them) get their types.
- Columns outside the schema, such as site extension columns, are kept; pass
keep_extra_columns=Falseto keep only schema columns, or a Polarsselectorto choose. - Shards are stacked by column name, so they may order their columns differently or lack one.
- DATE columns written with a midnight time (
2005-05-07 00:00:00) are read as dates. Text dates may also beYYYY/MM/DD,DD-MM-YYYY,DD/MM/YYYYorYYYYMMDD. - A value that does not fit its type raises when the table is collected;
strict=Falsemakes it null instead. add_missing_columns=Trueadds schema columns the input lacks, as nulls.
3. Load Datasets with PyArrow
You can load datasets from a folder containing CSV files. The files are matched to the predefined schemas:
from omop_schema.schema.v5_4 import OMOPSchemaV54
schema_v54 = OMOPSchemaV54()
datasets = schema_v54.load_csv_dataset("path/to/csv/folder")
# Access a specific table
concept_table = datasets["concept"]
print(concept_table)
4. Convert PyArrow Schema to Polars Schema
If Polars is installed, you can convert a PyArrow schema to a Polars-compatible schema:
from omop_schema.utils import pyarrow_to_polars_schema
import pyarrow as pa
arrow_schema = pa.schema(
[
pa.field("column1", pa.int64()),
pa.field("column2", pa.string()),
]
)
polars_schema = pyarrow_to_polars_schema(arrow_schema)
print(polars_schema)
5. Convert PyArrow Schema to Pandas Schema
If Pandas is installed, you can convert a PyArrow schema to a Pandas-compatible schema:
from omop_schema.utils import pyarrow_to_pandas_schema
import pyarrow as pa
arrow_schema = pa.schema(
[
pa.field("column1", pa.int64()),
pa.field("column2", pa.string()),
]
)
pandas_schema = pyarrow_to_pandas_schema(arrow_schema)
print(pandas_schema)
Optional Dependencies
- Polars (>= 1.26): For reading tables with
scan_tableand converting PyArrow schemas to Polars schemas. - Pandas: For converting PyArrow schemas to Pandas schemas.
OMOP Dataset Validation - Graphical Method
This documentation provides an overview of how to use the validate_omop_dataset_graphically function to validate OMOP datasets and display the results in a graphical format.
The validate_omop_dataset_graphically function validates an OMOP dataset against a predefined schema and displays the results in a graphical table format using the rich library. It also logs the validation process for real-time feedback.
validate_omop_dataset_graphically(validator, dataset_path="path/to/dataset")
The function will:
- Log the validation progress in real-time.
- Display a graphical table summarizing:
- Missing columns
- Mismatched columns
- Extra columns
- Correct columns
Example Output
Console Log
2023-10-01 12:00:00 - INFO - Validating table: person
2023-10-01 12:00:01 - INFO - Validation results for table 'person': Missing: month_of_birth, Mismatched: None, Extra: extra_column_1, Correct: person_id, gender_concept_id
2023-10-01 12:00:01 - INFO - Validation process completed.
Graphical Table
+-------------+-----------------------------+-----------------------------------+----------------------+----------------------+
| Table Name | Missing Columns | Mismatched Columns | Extra Columns | Correct Columns |
+-------------+-----------------------------+-----------------------------------+----------------------+----------------------+
| person | month_of_birth: int64 | None | extra_column_1: str | person_id: int64 |
| | day_of_birth: int64 | | | gender_concept_id: |
| | | | | int64 |
+-------------+-----------------------------+-----------------------------------+----------------------+----------------------+
Parameters
| Parameter | Type | Description |
|---|---|---|
validator |
OMOPValidator |
The validator object containing the schema for validation. |
dataset_path |
`str | Path` |
load_with_expected_schema |
bool |
Whether to load the dataset with the expected schema. Default is True. |
Notes
- Ensure the dataset files are in a supported format (e.g.,
.csv,.parquet). - The function uses the
richlibrary for graphical output andloggingfor real-time feedback. - Missing or mismatched columns are highlighted in the output for easy identification.
Troubleshooting
- Missing Dependencies: Install the required libraries using
pip install rich. - Invalid Dataset Path: Ensure the dataset path is correct and accessible.
- Schema Issues: Verify the schema file is correctly defined and matches the dataset structure.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Metadata
Release files for omop-schema 0.1.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 |
|---|---|---|---|---|
| omop_schema-0.1.0.tar.gz | 43.3 kB | With provenance | Details for omop_schema-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| omop_schema-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for omop_schema-0.1.0-py3-none-any.whl |
Total release size: 68.2 kB
Release files / omop_schema-0.1.0.tar.gz
| Download URL | omop_schema-0.1.0.tar.gz |
|---|---|
| Size | 43.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9e9335310681bd94ed0fcfb046ec91852a65ce785910c0d3f556d3d051de033
|
|
BLAKE2b-256 checksum How to use checksums |
00be714581b614ec815736bf705b2c0bb028827da38e1cc27cc19cb96585a896
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 3, 2026.
Transparency logRelease files / omop_schema-0.1.0-py3-none-any.whl
| Download URL | omop_schema-0.1.0-py3-none-any.whl |
|---|---|
| Size | 24.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f04b6d6b4aa1d58c3f41cdccd4289576ddd721c0cc3745b65cc9d6e8c6cc5e60
|
|
BLAKE2b-256 checksum How to use checksums |
f32bd35881aaa6237d2d6b027991b43beffd2a90d101d05eb254723ee1a64fce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 3, 2026.
Transparency logSecurity for omop-schema 0.1.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for omop_schema-0.1.0.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 Nov 3, 2026.
Transparency logA 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.