Skip to content

Query Filtering

omop_alchemy.cdm.query provides ConceptFilter, a shared, reusable way to filter CDM concept-table queries by domain, vocabulary, concept ID, and standard/active status, with an optional row-count limit.

It exists so that packages consuming OMOP Alchemy (e.g. omop-emb, omop-graph) don't each need to reimplement the same filtering logic against their own copy of Concept's column names — since this package owns the Concept model directly, the filter can reference real columns rather than duck-typing against an opaquely-imported table.

from sqlalchemy import select
from omop_alchemy.cdm.model.vocabulary import Concept
from omop_alchemy.cdm.query import ConceptFilter

concept_filter = ConceptFilter(
    domains=("Condition", "Drug"),
    require_standard=True,
)
query = concept_filter.apply(select(Concept))

All fields are optional and combinable.

Search constraints for plain CDM concept-table queries.

All fields are optional. Unset fields impose no constraint.

Attributes:

Name Type Description
concept_ids (tuple[int, ...], optional)

Restrict results to this set of concept IDs.

domains (tuple[str, ...], optional)

Restrict results to concepts in these OMOP domains.

vocabularies (tuple[str, ...], optional)

Restrict results to concepts from these vocabularies.

require_standard bool

When True, only concepts where Concept.is_standard is True are returned (standard_concept == 'S', tolerating surrounding whitespace). Classification concepts ('C') are excluded because they are not valid mapping targets. Default False.

include_classification bool

Only meaningful with require_standard. When True, classification concepts ('C') are admitted alongside standard ones. Default False.

require_active bool

When True, only concepts where Concept.is_valid is True are returned (invalid_reason is NULL/blank/whitespace, i.e. not 'D' or 'U'). Default False.

limit (int, optional)

Maximum number of rows to return. If not set, all matching rows are returned.

apply

apply(query: Select) -> sa.Select

Apply filter constraints to a Select already targeting Concept.

is_empty

is_empty() -> bool

Return True if no constraints are set.