Modules
Derivative based on the original work here: https://github.com/thehyve/omop-cdm/blob/main/src/omop_cdm/regular/cdm600/tables.py Modifications made to this file: - Removed support for schema. - Added new tables
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at *
- https://www.apache.org/licenses/LICENSE-2.0 *
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
Derivative based on the original work here: https://github.com/thehyve/omop-cdm/blob/main/src/omop_cdm/regular/cdm54/tables.py Modifications made to this file: - Removed support for schema. - Added new tables
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at *
- https://www.apache.org/licenses/LICENSE-2.0 *
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
Engine and session factory for OMOP CDM databases.
This module provides an asynchronous SQLAlchemy engine factory with helpers to create/init CDM schemas and obtain async sessions across supported backends (SQLite, MySQL, PostgreSQL).
CdmEngineFactory
¶
Bases: object
Factory to create async SQLAlchemy engines and sessions for OMOP CDM.
Supports SQLite (default), MySQL, and PostgreSQL. Exposes convenience properties for the configured engine and async session maker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db
|
Database type: "sqlite", "mysql", or "pgsql". |
'sqlite'
|
|
host
|
Database host (ignored for SQLite). |
'localhost'
|
|
port
|
Database port (ignored for SQLite). |
5432
|
|
user
|
Database user (ignored for SQLite). |
'root'
|
|
pw
|
Database password (ignored for SQLite). |
'pass'
|
|
name
|
Database name or SQLite filename. |
'cdm.sqlite'
|
|
schema
|
PostgreSQL schema to use for CDM. |
''
|
Source code in src/pyomop/engine_factory.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
async_session
property
¶
Alias for session to maintain backward compatibility.
base
property
¶
Return automapped classes when an engine exists, otherwise None.
db
property
writable
¶
Return the configured database type (sqlite/mysql/pgsql).
engine
property
¶
Create or return the async engine for the configured backend.
Returns:
Type | Description |
---|---|
Async engine instance bound to the configured database. |
host
property
writable
¶
Return the configured database host (if applicable).
name
property
writable
¶
Return the configured database name or SQLite filename.
port
property
writable
¶
Return the configured database port (if applicable).
pw
property
writable
¶
Return the configured database password (if applicable).
schema
property
writable
¶
Return the configured schema (PostgreSQL).
session
property
¶
Return an async_sessionmaker for creating AsyncSession objects.
user
property
writable
¶
Return the configured database user (if applicable).
init_models(metadata)
async
¶
Drop and re-create all tables from provided metadata.
This is mainly used for tests and quick local setups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata
|
SQLAlchemy |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the engine has not been initialized. |
Source code in src/pyomop/engine_factory.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
LLM-oriented SQLDatabase wrapper for OMOP CDM.
This module provides a thin wrapper around llama_index.core.SQLDatabase
that is aware of the OMOP CDM metadata reflected from this package's
SQLAlchemy models. It enables LLM-powered query components to reason about
available tables, columns, and foreign keys using the OMOP metadata directly.
This file is import-safe even when the optional LLM extras are not installed;
in that case, attempting to instantiate CDMDatabase
will raise a clear
ImportError directing you to install pyomop[llm]
.
CDMDatabase
¶
Bases: SQLDatabase
OMOP-aware SQLDatabase for LLM query engines.
This class adapts llama-index's SQLDatabase
to use the OMOP CDM
SQLAlchemy metadata bundled with this package, making it easy to expose
concise schema information to LLM components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
Engine
|
SQLAlchemy |
required |
schema
|
Optional[str]
|
Optional database schema name. |
None
|
ignore_tables
|
Optional[List[str]]
|
Tables to hide from the LLM context. |
None
|
include_tables
|
Optional[List[str]]
|
Explicit subset of tables to expose. |
None
|
sample_rows_in_table_info
|
int
|
Kept for API parity (unused here). |
3
|
indexes_in_table_info
|
bool
|
Kept for API parity (unused here). |
False
|
custom_table_info
|
Optional[dict]
|
Optional overrides for table descriptions. |
None
|
view_support
|
bool
|
Whether to reflect views as well (unused here). |
False
|
max_string_length
|
int
|
Max length of generated descriptions. |
300
|
version
|
str
|
OMOP CDM version label ("cdm54" or "cdm6"). |
'cdm54'
|
Source code in src/pyomop/llm_engine.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
get_single_table_info(table_name)
¶
Return a concise description of columns and foreign keys for a table.
The format matches what llama-index expects when building table context.
Source code in src/pyomop/llm_engine.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
get_table_columns(table_name)
¶
Return list of column names for a table.
This uses the OMOP SQLAlchemy MetaData
instead of DB inspector.
Source code in src/pyomop/llm_engine.py
161 162 163 164 165 166 167 |
|
usable_tables()
¶
Return the sorted list of tables exposed to the LLM.
This respects include/ignore settings passed at initialization.
Source code in src/pyomop/llm_engine.py
190 191 192 193 194 195 196 |
|
LLM query utilities over the OMOP CDM schema.
This module wires llama-index components to an OMOP-aware CDMDatabase
so
you can build semantic and SQL-first query engines that know about your CDM
tables. All LLM-related imports are optional and performed lazily at runtime.
CdmLLMQuery
¶
Helper that prepares an LLM-backed SQL query engine for OMOP.
It constructs an object index of selected CDM tables and exposes a retriever-backed query engine that can generate SQL or run SQL-only queries depending on configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sql_database
|
CDMDatabase
|
A |
required |
llm
|
Any
|
Optional LLM implementation to plug into llama-index settings. |
None
|
similarity_top_k
|
int
|
Top-k tables to retrieve for each query. |
1
|
embed_model
|
str
|
HuggingFace embedding model name. |
'BAAI/bge-small-en-v1.5'
|
**kwargs
|
Any
|
Reserved for future expansion. |
{}
|
Source code in src/pyomop/llm_query.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
object_index
property
¶
The underlying llama-index object index used for retrieval.
query_engine
property
¶
A retriever-backed SQL query engine over the CDM tables.
table_node_mapping
property
¶
Mapping between tables and nodes used by the object index.
table_schema_objs
property
¶
List of table schema objects indexed for retrieval.
CSV-to-OMOP loader.
This module implements a flexible CSV loader that can populate multiple OMOP CDM tables according to a JSON mapping file. It also performs helpful cleanup operations like foreign key normalization, birthdate backfilling, gender mapping, and concept code lookups.
CdmCsvLoader
¶
Load a single CSV into multiple OMOP CDM tables using a JSON mapping file.
Mapping file format (JSON):
{ "csv_key": "patient_id", # optional, CSV column that contains the patient/person identifier "tables": [ { "name": "cohort", # target table name as in the database "filters": [ # optional row filters applied to CSV before mapping {"column": "resourceType", "equals": "Encounter"} ], "columns": { # mapping of target_table_column -> value "cohort_definition_id": {"const": 1}, # constant value "subject_id": "patient_id", # copy from CSV column "cohort_start_date": "period.start", # copy from CSV column "cohort_end_date": "period.end" # copy from CSV column } } ] }
Notes
- Constants are provided via {"const": value}.
- If a required column is missing from mapping, it's left as None (DB default or nullable required).
- Primary keys that are Integer types will autoincrement where supported (SQLite/PostgreSQL typical behavior).
- Dates/times are converted to proper Python types where possible based on reflected column types.
Source code in src/pyomop/loader.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
|
__init__(cdm_engine_factory, version='cdm54')
¶
Create a loader bound to a specific database engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cdm_engine_factory
|
An initialized |
required | |
version
|
str
|
OMOP CDM version label ("cdm54" or "cdm6"). |
'cdm54'
|
Source code in src/pyomop/loader.py
80 81 82 83 84 85 86 87 88 89 90 91 |
|
apply_concept_mappings(session, automap, mapping)
async
¶
Based on the "concept" key in the mapping JSON, populate target *_concept_id columns
by looking up concept.concept_id using codes found in the specified source column.
Rules:
- If the source value is a comma-separated string, use only the first element for lookup.
- Find by equality on concept.concept_code.
- Update the target column with the matching concept.concept_id.
Source code in src/pyomop/loader.py
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
|
backfill_person_birth_fields(session, automap)
async
¶
In the person table, replace 0 or NULL values in year_of_birth, month_of_birth,
and day_of_birth with values derived from birth_datetime.
This runs in Python for portability across backends.
Source code in src/pyomop/loader.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
|
fix_person_id(session, automap)
async
¶
Update all tables so that person_id foreign keys store the canonical person.person_id (integer), replacing any rows where person_id currently contains the person_source_value (string/UUID).
Approach: - Build a mapping from person_source_value -> person_id from the person table. - For each table (except person) having a person_id column, run updates: SET person_id = person.person_id WHERE CAST(person_id AS TEXT) = person_source_value. - This is safe for SQLite (used in examples). For stricter RDBMS, ensure types are compatible or adjust as needed.
Source code in src/pyomop/loader.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
|
load(csv_path, mapping_path=None, chunk_size=1000)
async
¶
Load a CSV into multiple OMOP tables based on a mapping file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_path
|
str
|
Path to the input CSV file. |
required |
mapping_path
|
str | None
|
Path to the JSON mapping file. Defaults to the
package's |
None
|
chunk_size
|
int
|
Batch size for INSERT statements. |
1000
|
Source code in src/pyomop/loader.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
update_person_gender_concept_id(session, automap)
async
¶
Update person.gender_concept_id from person.gender_source_value using static mapping:
- male (or 'm') -> 8507
- female (or 'f') -> 8532
- anything else -> 0 (unknown)
Only updates rows where the computed value differs from the current value or where gender_concept_id is NULL.
Source code in src/pyomop/loader.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
|
Command-line interface for pyomop.
Provides commands to create CDM tables, load vocabulary CSVs, and import FHIR Bulk Export data into an OMOP database.
main_routine()
¶
Top-level runner used by python -m pyomop
.
Source code in src/pyomop/main.py
136 137 138 139 140 141 |
|
Vocabulary utilities for loading and querying OMOP vocab tables.
Provides helpers to import vocabulary CSVs into the database and to look up concepts by id or code. Uses async SQLAlchemy sessions.
CdmVocabulary
¶
Bases: object
Helpers for OMOP Vocabulary management and lookups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cdm
|
An initialized |
required | |
version
|
CDM version string ("cdm54" or "cdm6"). Defaults to "cdm54". |
'cdm54'
|
Source code in src/pyomop/vocabulary.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
concept_code
property
¶
Current concept_code for this helper (if set).
concept_id
property
writable
¶
Current concept_id for this helper (if set).
concept_name
property
¶
Current concept_name for this helper (if set).
domain_id
property
¶
Current domain_id for this helper (if set).
vocabulary_id
property
¶
Current vocabulary_id for this helper (if set).
create_vocab(folder, sample=None)
async
¶
Load vocabulary CSV files from a folder into the database.
This imports the standard OMOP vocab tables (drug_strength, concept, concept_relationship, concept_ancestor, concept_synonym, vocabulary, relationship, concept_class, domain).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder
|
Path to the folder containing OMOP vocabulary CSVs. |
required | |
sample
|
Optional number of rows to limit per file during import. |
None
|
Source code in src/pyomop/vocabulary.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
|
get_concept(concept_id)
async
¶
Fetch a concept row by id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
concept_id
|
Concept identifier. |
required |
Returns:
Type | Description |
---|---|
The ORM Concept instance. |
Source code in src/pyomop/vocabulary.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
get_concept_by_code(concept_code, vocabulary_id)
async
¶
Fetch a concept by code within a vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
concept_code
|
The vocabulary-specific code string. |
required | |
vocabulary_id
|
Vocabulary identifier (e.g., 'SNOMED', 'LOINC'). |
required |
Returns:
Type | Description |
---|---|
The ORM Concept instance. |
Source code in src/pyomop/vocabulary.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
get_session()
async
¶
Yield an async session bound to the current engine.
Yields:
Name | Type | Description |
---|---|---|
AsyncSession |
AsyncGenerator[AsyncSession, None]
|
An async SQLAlchemy session. |
Source code in src/pyomop/vocabulary.py
277 278 279 280 281 282 283 284 285 |
|
set_concept(concept_code, vocabulary_id=None)
¶
Set the active concept context by code and vocabulary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
concept_code
|
The concept code string to resolve. |
required | |
vocabulary_id
|
Vocabulary identifier. Required. |
None
|
Notes
On success, populates concept fields on this instance. On failure,
sets _vocabulary_id
and _concept_id
to 0.
Source code in src/pyomop/vocabulary.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
write_vocab(df, table, if_exists='replace', chunk_size=1000)
async
¶
Write a DataFrame to a vocabulary table with type-safe defaults.
Ensures required columns exist with reasonable defaults, coerces types, and performs chunked inserts via SQLAlchemy core for performance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
Pandas DataFrame with data to insert. |
required | |
table
|
Target table name (e.g., 'concept'). |
required | |
if_exists
|
Compatibility only. This method always inserts. |
'replace'
|
|
chunk_size
|
Number of rows per batch insert. |
1000
|
Source code in src/pyomop/vocabulary.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
Utilities to execute queries and convert results to DataFrames.
Exposes a small helper class around async SQLAlchemy execution and integration with OHDSI QueryLibrary.
CdmVector
¶
Bases: object
Query execution utility for OMOP CDM.
Methods let you run raw SQL or QueryLibrary snippets and turn results into pandas DataFrames.
Source code in src/pyomop/vector.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
execute(cdm, sqldict=None, query=None, chunksize=1000)
async
¶
Execute a SQL query asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cdm
|
CdmEngineFactory instance. |
required | |
sqldict
|
Optional key from |
None
|
|
query
|
Raw SQL string (used if provided). |
None
|
|
chunksize
|
Unused; kept for future streaming support. |
1000
|
Returns:
Type | Description |
---|---|
SQLAlchemy AsyncResult. |
Source code in src/pyomop/vector.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
query_library(cdm, resource='person', query_name='PE02')
async
¶
Fetch a query from OHDSI QueryLibrary and execute it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cdm
|
CdmEngineFactory instance. |
required | |
resource
|
Query resource subfolder (e.g., "person"). |
'person'
|
|
query_name
|
Query markdown file name (e.g., "PE02"). |
'PE02'
|
Returns:
Type | Description |
---|---|
SQLAlchemy AsyncResult. |
Source code in src/pyomop/vector.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
result_to_df(result)
¶
Convert a Result to a DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
SQLAlchemy Result or AsyncResult. |
required |
Returns:
Type | Description |
---|---|
pandas.DataFrame of result mappings. |
Source code in src/pyomop/vector.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|