Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
217 lines (187 loc) · 7.61 KB

File metadata and controls

217 lines (187 loc) · 7.61 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
import asyncio
from pathlib import Path
import pandas as pd
from sqlalchemy import func, insert, select, update
from sqlalchemy.ext.automap import automap_base
def test_loader_person_backfill_and_fk_fix(tmp_path):
from src.pyomop import CdmEngineFactory
from src.pyomop.cdm54 import Base
from src.pyomop.loader import CdmCsvLoader
from src.pyomop.vocabulary import CdmVocabulary
async def run():
db_path = tmp_path / "test_loader.sqlite"
cdm = CdmEngineFactory(db="sqlite", name=str(db_path))
_ = cdm.engine # initialize engine
await cdm.init_models(Base.metadata)
vocab = CdmVocabulary(cdm)
await vocab.create_vocab("tests")
loader = CdmCsvLoader(cdm)
await loader.load(
csv_path="tests/fhir_export.csv",
mapping_path=str(Path("src/pyomop/mapping.default.json")),
chunk_size=200,
)
async_session = cdm.session
async with async_session() as session:
conn = await session.connection()
BaseMap = automap_base()
def _prepare(sync_conn):
BaseMap.prepare(autoload_with=sync_conn)
await conn.run_sync(_prepare)
person = getattr(BaseMap.classes, "person").__table__
total_persons = (
await session.execute(select(func.count()).select_from(person))
).scalar_one()
assert total_persons > 0
with_bd = (
await session.execute(
select(func.count())
.select_from(person)
.where(person.c.birth_datetime.isnot(None))
)
).scalar_one()
if with_bd > 0:
zeros_y = (
await session.execute(
select(func.count())
.select_from(person)
.where(
person.c.birth_datetime.isnot(None),
(person.c.year_of_birth.is_(None))
| (person.c.year_of_birth == 0),
)
)
).scalar_one()
zeros_m = (
await session.execute(
select(func.count())
.select_from(person)
.where(
person.c.birth_datetime.isnot(None),
(person.c.month_of_birth.is_(None))
| (person.c.month_of_birth == 0),
)
)
).scalar_one()
zeros_d = (
await session.execute(
select(func.count())
.select_from(person)
.where(
person.c.birth_datetime.isnot(None),
(person.c.day_of_birth.is_(None))
| (person.c.day_of_birth == 0),
)
)
).scalar_one()
assert zeros_y == 0
assert zeros_m == 0
assert zeros_d == 0
try:
observation = getattr(BaseMap.classes, "observation").__table__
persons_alias = person.alias()
orphans = (
await session.execute(
select(func.count())
.select_from(
observation.outerjoin(
persons_alias,
observation.c.person_id == persons_alias.c.person_id,
)
)
.where(persons_alias.c.person_id.is_(None))
)
).scalar_one()
obs_total = (
await session.execute(select(func.count()).select_from(observation))
).scalar_one()
if obs_total > 0:
assert orphans == 0
except AttributeError:
pass
asyncio.run(run())
def test_concept_mapping_uses_first_of_comma_separated(tmp_path):
from src.pyomop import CdmEngineFactory
from src.pyomop.cdm54 import Base
from src.pyomop.loader import CdmCsvLoader
from src.pyomop.vocabulary import CdmVocabulary
async def run():
db_path = tmp_path / "test_loader_concept.sqlite"
cdm = CdmEngineFactory(db="sqlite", name=str(db_path))
_ = cdm.engine # initialize engine
await cdm.init_models(Base.metadata)
vocab = CdmVocabulary(cdm)
await vocab.create_vocab("tests")
loader = CdmCsvLoader(cdm)
await loader.load(
csv_path="tests/fhir_export.csv",
mapping_path=str(Path("src/pyomop/mapping.default.json")),
chunk_size=200,
)
async_session = cdm.session
async with async_session() as session:
conn = await session.connection()
BaseMap = automap_base()
def _prepare(sync_conn):
BaseMap.prepare(autoload_with=sync_conn)
await conn.run_sync(_prepare)
concept = getattr(BaseMap.classes, "concept").__table__
person = getattr(BaseMap.classes, "person").__table__
observation = getattr(BaseMap.classes, "observation").__table__
code_row = (
await session.execute(
select(concept.c.concept_code, concept.c.concept_id).limit(1)
)
).first()
assert code_row is not None
test_code, expected_cid = code_row
obs_pk_col = list(observation.primary_key.columns)[0]
obs_row = (
await session.execute(
select(obs_pk_col, observation.c.person_id).limit(1)
)
).first()
if obs_row is None:
pid_row = (
await session.execute(select(person.c.person_id).limit(1))
).first()
assert pid_row is not None
pid = pid_row[0]
ins_stmt = insert(observation).values(
person_id=pid,
observation_concept_id=0,
observation_type_concept_id=0,
observation_date=pd.Timestamp("2020-01-01").date(),
observation_source_value="placeholder",
)
await session.execute(ins_stmt)
await session.commit()
obs_row = (
await session.execute(
select(obs_pk_col, observation.c.person_id).limit(1)
)
).first()
assert obs_row is not None
obs_id = obs_row[0]
upd = (
update(observation)
.where(obs_pk_col == obs_id)
.values(
observation_source_value=f"{test_code},OTHER",
observation_concept_id=0,
)
)
await session.execute(upd)
import json
mapping = json.loads(Path("src/pyomop/mapping.default.json").read_text())
await loader.apply_concept_mappings(session, BaseMap, mapping)
await session.commit()
check = (
await session.execute(
select(observation.c.observation_concept_id).where(
obs_pk_col == obs_id
)
)
).scalar_one()
assert check == expected_cid
asyncio.run(run())
Morty Proxy This is a proxified and sanitized view of the page, visit original site.