omop-alchemy 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. omop_alchemy-0.5.1/LICENSE +202 -0
  2. omop_alchemy-0.5.1/PKG-INFO +116 -0
  3. omop_alchemy-0.5.1/README.md +79 -0
  4. omop_alchemy-0.5.1/omop_alchemy/__init__.py +20 -0
  5. omop_alchemy-0.5.1/omop_alchemy/cdm/__init__.py +0 -0
  6. omop_alchemy-0.5.1/omop_alchemy/cdm/base/__init__.py +34 -0
  7. omop_alchemy-0.5.1/omop_alchemy/cdm/base/cdm_constants.py +5 -0
  8. omop_alchemy-0.5.1/omop_alchemy/cdm/base/cdm_table_base.py +14 -0
  9. omop_alchemy-0.5.1/omop_alchemy/cdm/base/column_helpers.py +42 -0
  10. omop_alchemy-0.5.1/omop_alchemy/cdm/base/column_mixins.py +107 -0
  11. omop_alchemy-0.5.1/omop_alchemy/cdm/base/decorators.py +31 -0
  12. omop_alchemy-0.5.1/omop_alchemy/cdm/base/domain_checking.py +70 -0
  13. omop_alchemy-0.5.1/omop_alchemy/cdm/base/modifier_interface.py +50 -0
  14. omop_alchemy-0.5.1/omop_alchemy/cdm/base/reference_context.py +39 -0
  15. omop_alchemy-0.5.1/omop_alchemy/cdm/base/typing.py +45 -0
  16. omop_alchemy-0.5.1/omop_alchemy/cdm/model/__init__.py +47 -0
  17. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/__init__.py +24 -0
  18. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/clinical_event_union.py +40 -0
  19. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/condition_occurrence.py +81 -0
  20. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/death.py +48 -0
  21. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/device_exposure.py +53 -0
  22. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/drug_exposure.py +89 -0
  23. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/measurement.py +51 -0
  24. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/observation.py +47 -0
  25. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/person.py +230 -0
  26. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/procedure_occurrence.py +135 -0
  27. omop_alchemy-0.5.1/omop_alchemy/cdm/model/clinical/specimin.py +37 -0
  28. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/__init__.py +15 -0
  29. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/cohort.py +21 -0
  30. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/cohort_definition.py +28 -0
  31. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/condition_era.py +22 -0
  32. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/dose_era.py +22 -0
  33. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/drug_era.py +22 -0
  34. omop_alchemy-0.5.1/omop_alchemy/cdm/model/derived/observation_period.py +20 -0
  35. omop_alchemy-0.5.1/omop_alchemy/cdm/model/extended/__init__.py +5 -0
  36. omop_alchemy-0.5.1/omop_alchemy/cdm/model/extended/event_timeline.py +275 -0
  37. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_economic/__init__.py +6 -0
  38. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_economic/cost.py +45 -0
  39. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_economic/payer_plan_period.py +37 -0
  40. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_system/__init__.py +7 -0
  41. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_system/care_site.py +24 -0
  42. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_system/location.py +29 -0
  43. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_system/provider.py +30 -0
  44. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_system/visit_detail.py +39 -0
  45. omop_alchemy-0.5.1/omop_alchemy/cdm/model/health_system/visit_occurrence.py +43 -0
  46. omop_alchemy-0.5.1/omop_alchemy/cdm/model/metadata/__init__.py +7 -0
  47. omop_alchemy-0.5.1/omop_alchemy/cdm/model/metadata/cdm_source.py +34 -0
  48. omop_alchemy-0.5.1/omop_alchemy/cdm/model/metadata/metadata.py +27 -0
  49. omop_alchemy-0.5.1/omop_alchemy/cdm/model/structural/__init__.py +13 -0
  50. omop_alchemy-0.5.1/omop_alchemy/cdm/model/structural/episode.py +106 -0
  51. omop_alchemy-0.5.1/omop_alchemy/cdm/model/structural/episode_event.py +86 -0
  52. omop_alchemy-0.5.1/omop_alchemy/cdm/model/structural/fact_relationship.py +37 -0
  53. omop_alchemy-0.5.1/omop_alchemy/cdm/model/unstructured/__init__.py +4 -0
  54. omop_alchemy-0.5.1/omop_alchemy/cdm/model/unstructured/image.py +0 -0
  55. omop_alchemy-0.5.1/omop_alchemy/cdm/model/unstructured/image_feature.py +0 -0
  56. omop_alchemy-0.5.1/omop_alchemy/cdm/model/unstructured/note.py +122 -0
  57. omop_alchemy-0.5.1/omop_alchemy/cdm/model/unstructured/note_nlp.py +77 -0
  58. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/__init__.py +25 -0
  59. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/concept.py +107 -0
  60. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/concept_ancestor.py +15 -0
  61. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/concept_class.py +14 -0
  62. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/concept_relationship.py +16 -0
  63. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/concept_synonym.py +11 -0
  64. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/domain.py +14 -0
  65. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/drug_strength.py +34 -0
  66. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/relationship.py +17 -0
  67. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/source_to_concept_map.py +40 -0
  68. omop_alchemy-0.5.1/omop_alchemy/cdm/model/vocabulary/vocabulary.py +30 -0
  69. omop_alchemy-0.5.1/omop_alchemy/cdm/registry/__init__.py +5 -0
  70. omop_alchemy-0.5.1/omop_alchemy/cdm/registry/domain_rule.py +9 -0
  71. omop_alchemy-0.5.1/omop_alchemy/cdm/specification/__init__.py +7 -0
  72. omop_alchemy-0.5.1/omop_alchemy/cdm/specification/config.py +10 -0
  73. omop_alchemy-0.5.1/omop_alchemy/config.py +53 -0
  74. omop_alchemy-0.5.1/omop_alchemy/errors.py +2 -0
  75. omop_alchemy-0.5.1/omop_alchemy/py.typed +0 -0
  76. omop_alchemy-0.5.1/omop_alchemy.egg-info/PKG-INFO +116 -0
  77. omop_alchemy-0.5.1/omop_alchemy.egg-info/SOURCES.txt +83 -0
  78. omop_alchemy-0.5.1/omop_alchemy.egg-info/dependency_links.txt +1 -0
  79. omop_alchemy-0.5.1/omop_alchemy.egg-info/requires.txt +17 -0
  80. omop_alchemy-0.5.1/omop_alchemy.egg-info/top_level.txt +1 -0
  81. omop_alchemy-0.5.1/pyproject.toml +75 -0
  82. omop_alchemy-0.5.1/setup.cfg +4 -0
  83. omop_alchemy-0.5.1/tests/test_config_and_setup.py +23 -0
  84. omop_alchemy-0.5.1/tests/test_convention_enforcement.py +0 -0
  85. omop_alchemy-0.5.1/tests/test_load_vocab.py +127 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,116 @@
1
+ Metadata-Version: 2.4
2
+ Name: omop-alchemy
3
+ Version: 0.5.1
4
+ Summary: SQLAlchemy-based models, validation, and utilities for the OHDSI OMOP Common Data Model
5
+ Author-email: Georgie Kennedy <georgie.kennedy@unsw.edu.au>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/AustralianCancerDataNetwork/OMOP_Alchemy
8
+ Project-URL: Repository, https://github.com/AustralianCancerDataNetwork/OMOP_Alchemy
9
+ Project-URL: Issues, https://github.com/AustralianCancerDataNetwork/OMOP_Alchemy/issues
10
+ Keywords: OMOP,OHDSI,clinical-data,health-informatics,sqlalchemy,ETL,real-world-evidence
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Intended Audience :: Healthcare Industry
14
+ Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Python: >=3.12
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: sqlalchemy>=2.0.45
22
+ Requires-Dist: pandas>=2.0
23
+ Requires-Dist: pyyaml>=6.0
24
+ Requires-Dist: python-dotenv>=1.0
25
+ Requires-Dist: orm-loader>=0.1.1
26
+ Provides-Extra: dev
27
+ Requires-Dist: ipython>=8.0; extra == "dev"
28
+ Requires-Dist: pytest>=7.0; extra == "dev"
29
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
30
+ Requires-Dist: mypy>=1.8; extra == "dev"
31
+ Requires-Dist: ruff>=0.4; extra == "dev"
32
+ Requires-Dist: rich>=13.0; extra == "dev"
33
+ Provides-Extra: docs
34
+ Requires-Dist: sphinx; extra == "docs"
35
+ Requires-Dist: myst-parser; extra == "docs"
36
+ Dynamic: license-file
37
+
38
+ # OMOP Alchemy
39
+
40
+ **OMOP Alchemy** provides a canonical, typed, SQLAlchemy-first representation of the
41
+ [OHDSI OMOP Common Data Model (CDM)](https://ohdsi.github.io/CommonDataModel/).
42
+
43
+ It is designed to support **research-ready analytics, validation, and exploration**
44
+ of OMOP data using modern Python tooling, without imposing ETL conventions or
45
+ execution-time side effects.
46
+
47
+ ---
48
+
49
+ ## Design goals
50
+
51
+ OMOP Alchemy is intentionally:
52
+
53
+ - **Declarative**
54
+ Defines tables, columns, relationships, and constraints
55
+
56
+ - **SQLAlchemy-native**
57
+ Built for SQLAlchemy 2.x ORM usage
58
+
59
+ - **Safe to import anywhere**
60
+ No implicit engine creation, no global state, no environment assumptions.
61
+
62
+ - **Typed and inspectable**
63
+ Models are fully typed and introspectable for validation, tooling, and IDE support.
64
+
65
+ - **Backend-agnostic**
66
+ Designed to work across PostgreSQL, SQLite, and other SQLAlchemy-supported databases.
67
+
68
+ ---
69
+
70
+ ## What this package does *not* do
71
+
72
+ OMOP Alchemy deliberately avoids:
73
+
74
+ - Enforcing ETL conventions or data pipelines
75
+ - Auto-creating databases or loading vocabularies
76
+ - Imposing analytics frameworks or dashboards
77
+ - Making assumptions about deployment environments
78
+
79
+ These concerns are intentionally left to downstream tooling.
80
+
81
+ ---
82
+
83
+ ## Core features
84
+
85
+ - SQLAlchemy ORM models for OMOP CDM tables
86
+ - Explicit foreign key and relationship definitions
87
+ - Read-only *View* classes for safe navigation and analytics
88
+ - Domain validation helpers for OMOP concept integrity
89
+ - CSV loading utilities for controlled ingestion and testing
90
+ - Lightweight schema and model validation against CDM specs
91
+
92
+ ---
93
+
94
+ ## Example (concept navigation)
95
+
96
+ ```python
97
+ from omop_alchemy.model.vocabulary import ConceptView
98
+
99
+ concept = session.get(ConceptView, 320128) # Lung cancer
100
+ concept.domain.domain_id # "Condition"
101
+ concept.vocabulary.vocabulary_id # "SNOMED"
102
+ concept.is_standard # True
103
+ ```
104
+
105
+ ---
106
+
107
+ ## Status
108
+
109
+ This project is currently beta.
110
+
111
+ The API is stabilising, but some modules may change as real-world use cases expand. Feedback and issues are welcome.
112
+
113
+ ### Some additional background
114
+
115
+ This work builds on earlier research and tooling presented at the 2023 OHDSI APAC Symposium
116
+ > see [background paper](https://github.com/AustralianCancerDataNetwork/OMOP_Alchemy/blob/main/notebooks/ORMforResearchReadyData_APAC2023.pdf).
@@ -0,0 +1,79 @@
1
+ # OMOP Alchemy
2
+
3
+ **OMOP Alchemy** provides a canonical, typed, SQLAlchemy-first representation of the
4
+ [OHDSI OMOP Common Data Model (CDM)](https://ohdsi.github.io/CommonDataModel/).
5
+
6
+ It is designed to support **research-ready analytics, validation, and exploration**
7
+ of OMOP data using modern Python tooling, without imposing ETL conventions or
8
+ execution-time side effects.
9
+
10
+ ---
11
+
12
+ ## Design goals
13
+
14
+ OMOP Alchemy is intentionally:
15
+
16
+ - **Declarative**
17
+ Defines tables, columns, relationships, and constraints
18
+
19
+ - **SQLAlchemy-native**
20
+ Built for SQLAlchemy 2.x ORM usage
21
+
22
+ - **Safe to import anywhere**
23
+ No implicit engine creation, no global state, no environment assumptions.
24
+
25
+ - **Typed and inspectable**
26
+ Models are fully typed and introspectable for validation, tooling, and IDE support.
27
+
28
+ - **Backend-agnostic**
29
+ Designed to work across PostgreSQL, SQLite, and other SQLAlchemy-supported databases.
30
+
31
+ ---
32
+
33
+ ## What this package does *not* do
34
+
35
+ OMOP Alchemy deliberately avoids:
36
+
37
+ - Enforcing ETL conventions or data pipelines
38
+ - Auto-creating databases or loading vocabularies
39
+ - Imposing analytics frameworks or dashboards
40
+ - Making assumptions about deployment environments
41
+
42
+ These concerns are intentionally left to downstream tooling.
43
+
44
+ ---
45
+
46
+ ## Core features
47
+
48
+ - SQLAlchemy ORM models for OMOP CDM tables
49
+ - Explicit foreign key and relationship definitions
50
+ - Read-only *View* classes for safe navigation and analytics
51
+ - Domain validation helpers for OMOP concept integrity
52
+ - CSV loading utilities for controlled ingestion and testing
53
+ - Lightweight schema and model validation against CDM specs
54
+
55
+ ---
56
+
57
+ ## Example (concept navigation)
58
+
59
+ ```python
60
+ from omop_alchemy.model.vocabulary import ConceptView
61
+
62
+ concept = session.get(ConceptView, 320128) # Lung cancer
63
+ concept.domain.domain_id # "Condition"
64
+ concept.vocabulary.vocabulary_id # "SNOMED"
65
+ concept.is_standard # True
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Status
71
+
72
+ This project is currently beta.
73
+
74
+ The API is stabilising, but some modules may change as real-world use cases expand. Feedback and issues are welcome.
75
+
76
+ ### Some additional background
77
+
78
+ This work builds on earlier research and tooling presented at the 2023 OHDSI APAC Symposium
79
+ > see [background paper](https://github.com/AustralianCancerDataNetwork/OMOP_Alchemy/blob/main/notebooks/ORMforResearchReadyData_APAC2023.pdf).
@@ -0,0 +1,20 @@
1
+ from .config import load_environment, get_engine_name, TEST_PATH, ROOT_PATH
2
+ from .errors import CDMValidationError
3
+
4
+ # from .cdm.base import Base, create_db
5
+ # from .cdm.utils import get_logger, configure_logging
6
+ # from .cdm.model.vocabulary import Concept
7
+
8
+ __all__ = [
9
+ # "Base",
10
+ # "Concept",
11
+ # "create_db",
12
+ # "get_logger",
13
+ # "configure_logging",
14
+ "load_environment",
15
+ "get_engine_name",
16
+ "TEST_PATH",
17
+ "ROOT_PATH",
18
+ ]
19
+
20
+ #logger = get_logger(__name__)
File without changes
@@ -0,0 +1,34 @@
1
+ from .domain_checking import ExpectedDomain
2
+ from .cdm_table_base import CDMTableBase
3
+ from .decorators import cdm_table
4
+ from .column_helpers import required_concept_fk, optional_concept_fk, optional_int, required_int
5
+ from .column_mixins import ValueMixin, ReferenceTable, DatedEvent, PersonScoped, HealthSystemContext, FactTable
6
+ from .domain_checking import DomainValidationMixin
7
+ from .reference_context import ReferenceContext
8
+ from .typing import HasConceptId, HasEpisodeId, HasPersonId, DomainSemanticTable, ClinicalEvent
9
+ from .modifier_interface import ModifierTargetMixin
10
+ from .cdm_constants import ModifierFieldConcepts
11
+ __all__ = [
12
+ "ExpectedDomain",
13
+ "CDMTableBase",
14
+ "cdm_table",
15
+ "required_concept_fk",
16
+ "optional_concept_fk",
17
+ "optional_int",
18
+ "required_int",
19
+ "ValueMixin",
20
+ "ReferenceTable",
21
+ "ReferenceContext",
22
+ "HasConceptId",
23
+ "HasEpisodeId",
24
+ "HasPersonId",
25
+ "DomainSemanticTable",
26
+ "ClinicalEvent",
27
+ "DatedEvent",
28
+ "PersonScoped",
29
+ "HealthSystemContext",
30
+ "DomainValidationMixin",
31
+ "FactTable",
32
+ "ModifierTargetMixin",
33
+ "ModifierFieldConcepts",
34
+ ]
@@ -0,0 +1,5 @@
1
+ class ModifierFieldConcepts:
2
+ CONDITION_OCCURRENCE = 1147127
3
+ PROCEDURE_OCCURRENCE = 1147082
4
+ DRUG_EXPOSURE = 1147707
5
+ EPISODE = 756290
@@ -0,0 +1,14 @@
1
+ from orm_loader.tables import CSVLoadableTableInterface, SerialisableTableInterface
2
+
3
+
4
+ class CDMTableBase(CSVLoadableTableInterface, SerialisableTableInterface):
5
+ """
6
+ Base class for CDM tables that support CSV loading and validation.
7
+ """
8
+ __abstract__ = True
9
+
10
+ """
11
+ Adds structural validation of ORM models against
12
+ the official OMOP CDM CSV specifications.
13
+ """
14
+ __cdm_extra_checks__: list[str] = []
@@ -0,0 +1,42 @@
1
+ import sqlalchemy as sa
2
+ import sqlalchemy.orm as so
3
+ from typing import Optional
4
+
5
+ def required_concept_fk(*, index: bool = True):
6
+ """
7
+ OMOP-required concept foreign key.
8
+
9
+ Semantics:
10
+ - Must exist
11
+ - Unknown allowed (concept_id = 0)
12
+ - Matches CDM Field-Level spec
13
+ """
14
+ return so.mapped_column(
15
+ sa.ForeignKey("concept.concept_id"),
16
+ nullable=False,
17
+ default=0,
18
+ index=index,
19
+ )
20
+
21
+ def optional_concept_fk(*, index: bool = False):
22
+ """
23
+ Optional concept foreign key.
24
+ """
25
+ return so.mapped_column(
26
+ sa.ForeignKey("concept.concept_id"),
27
+ nullable=True,
28
+ index=index,
29
+ )
30
+
31
+ def optional_fk(target: str, *, index: bool = False):
32
+ return so.mapped_column(
33
+ sa.ForeignKey(target),
34
+ nullable=True,
35
+ index=index,
36
+ )
37
+
38
+ def required_int():
39
+ return so.mapped_column(sa.Integer, nullable=False)
40
+
41
+ def optional_int():
42
+ return so.mapped_column(sa.Integer, nullable=True)
@@ -0,0 +1,107 @@
1
+ from __future__ import annotations
2
+
3
+ from datetime import date, datetime
4
+ from typing import Optional, List, TYPE_CHECKING, Type, Any
5
+ from dataclasses import dataclass
6
+ import sqlalchemy as sa
7
+ import sqlalchemy.orm as so
8
+ from orm_loader.helpers import get_model_by_tablename
9
+
10
+
11
+ """
12
+ OMOP CDM structural mixins.
13
+
14
+ These mixins encode *structural semantics* of the OMOP Common Data Model
15
+ as defined in the Table-Level and Field-Level CSV specifications.
16
+
17
+ They intentionally do NOT encode clinical or analytical meaning.
18
+ """
19
+
20
+ class PersonScoped:
21
+ """
22
+ Mixin for tables scoped to a person.
23
+ """
24
+ person_id: so.Mapped[int] = so.mapped_column(sa.ForeignKey("person.person_id"), nullable=False, index=True)
25
+
26
+ class ConceptTyped:
27
+ """
28
+ Mixin for tables whose primary meaning is encoded by a concept_id.
29
+ Subclasses MUST define <something>_concept_id.
30
+
31
+ Not currently used TBC if there is a use-case to retain that supports EAV queries?
32
+ """
33
+
34
+ @so.declared_attr
35
+ def concept_id(cls: Any) -> so.Mapped[int]:
36
+ raise NotImplementedError(
37
+ f"{cls.__name__} must define its own <x>_concept_id column"
38
+ )
39
+
40
+ class ValueMixin:
41
+ value_as_number: so.Mapped[Optional[float]] = so.mapped_column(sa.Float, nullable=True)
42
+ value_as_concept_id: so.Mapped[Optional[int]] = so.mapped_column(sa.ForeignKey("concept.concept_id"), nullable=True)
43
+
44
+ __table_args__ = (
45
+ sa.CheckConstraint(
46
+ "value_as_number IS NOT NULL OR value_as_concept_id IS NOT NULL",
47
+ name="ck_value_present",
48
+ ),
49
+ )
50
+
51
+ @so.validates("value_as_number", "value_as_concept_id")
52
+ def _validate_value(self, key, value):
53
+ other = (
54
+ self.value_as_concept_id
55
+ if key == "value_as_number"
56
+ else self.value_as_number
57
+ )
58
+
59
+ if value is None and other is None:
60
+ raise ValueError(
61
+ "At least one of value_as_number or value_as_concept_id must be set"
62
+ )
63
+ return value
64
+
65
+ class DatedEvent:
66
+ """
67
+ Mixin for tables with start/end date and datetime pairs.
68
+ """
69
+ start_date: so.Mapped[date] = so.mapped_column(nullable=False)
70
+ start_datetime: so.Mapped[Optional[datetime]] = so.mapped_column(nullable=True)
71
+
72
+ end_date: so.Mapped[Optional[date]] = so.mapped_column(nullable=True)
73
+ end_datetime: so.Mapped[Optional[datetime]] = so.mapped_column(nullable=True)
74
+
75
+
76
+ class HealthSystemContext:
77
+ """
78
+ Mixin for tables attributed to a provider.
79
+ """
80
+ provider_id: so.Mapped[Optional[int]] = so.mapped_column(sa.ForeignKey("provider.provider_id"),index=True,nullable=True)
81
+ visit_occurrence_id: so.Mapped[Optional[int]] = so.mapped_column(sa.ForeignKey("visit_occurrence.visit_occurrence_id"),index=True,nullable=True)
82
+ visit_detail_id: so.Mapped[Optional[int]] = so.mapped_column(sa.ForeignKey("visit_detail.visit_detail_id"),index=True,nullable=True)
83
+
84
+ class FactTable:
85
+ """
86
+ Marker mixin for OMOP fact tables.
87
+
88
+ Used for introspection, tooling, and documentation only.
89
+ """
90
+ pass
91
+
92
+ class ReferenceTable:
93
+ """Marker mixin for OMOP reference tables."""
94
+ pass
95
+
96
+ class SourceAttribution:
97
+ """
98
+ Mixin for *_source_value and *_source_concept_id patterns.
99
+ """
100
+ source_value: so.Mapped[Optional[str]] = so.mapped_column(sa.String, nullable=True)
101
+ source_concept_id: so.Mapped[Optional[int]] = so.mapped_column(sa.ForeignKey("concept.concept_id"), nullable=True)
102
+
103
+ class UnitConcept:
104
+ """
105
+ Mixin for unit_concept_id.
106
+ """
107
+ unit_concept_id: so.Mapped[Optional[int]] = so.mapped_column(sa.ForeignKey("concept.concept_id"), index=True, nullable=True)
@@ -0,0 +1,31 @@
1
+ from typing import Type, TypeVar
2
+ from .cdm_table_base import CDMTableBase
3
+
4
+ T = TypeVar("T", bound=type)
5
+
6
+ def cdm_table(cls: T) -> T:
7
+ """
8
+ Mark a SQLAlchemy declarative class as a concrete OMOP CDM table.
9
+
10
+ - Forces __abstract__ = False
11
+ - Ensures __tablename__ is defined
12
+ - Inherits from CDMTableBase
13
+ - Used to clearly distinguish real CDM tables from mixins
14
+ """
15
+
16
+ # Safety: require an explicit tablename
17
+ if not hasattr(cls, "__tablename__"):
18
+ raise TypeError(
19
+ f"@cdm_table applied to {cls.__name__} "
20
+ "but __tablename__ is not defined"
21
+ )
22
+
23
+ if not issubclass(cls, CDMTableBase):
24
+ raise TypeError(
25
+ f"{cls.__name__} must inherit from CDMTableBase "
26
+ )
27
+
28
+ # Explicitly mark as concrete
29
+ cls.__abstract__ = False
30
+
31
+ return cls