-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPS.py
276 lines (236 loc) · 8.45 KB
/
PPS.py
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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import datetime as dt
import os
# Pulls timing-related concept IDs
def data_pull(concepts_table):
concepts = '(' + concepts_table["domain_concept_id"].astype(dtype="str").str.cat(sep=', ') + ')'
visit_query = """
with AFAB as (
select
person_id
from """ + os.environ["WORKSPACE_CDR"] + """.person
where
sex_at_birth_concept_id = 45878463
)
select distinct
vis.person_id,
vis.visit_end_date,
con.condition_concept_id as concept_id,
NULL as value_as_number,
'Condition' as Concept_Domain
from
""" + os.environ["WORKSPACE_CDR"] + """.visit_occurrence vis
inner join
""" + os.environ["WORKSPACE_CDR"] + """.condition_occurrence con
on vis.visit_occurrence_id = con.visit_occurrence_id
where
vis.person_id in (select person_id from AFAB)
and con.condition_concept_id in """ + concepts + """
union distinct
select distinct
vis.person_id,
vis.visit_end_date,
obs.observation_concept_id as concept_id,
value_as_number,
'Observation' as Concept_Domain
from
""" + os.environ["WORKSPACE_CDR"] + """.visit_occurrence vis
inner join
""" + os.environ["WORKSPACE_CDR"] + """.observation obs
on vis.visit_occurrence_id = obs.visit_occurrence_id
where
vis.person_id in (select person_id from AFAB)
and obs.observation_concept_id in """ + concepts + """
union distinct
select distinct
vis.person_id,
vis.visit_end_date,
proc.procedure_concept_id as concept_id,
NULL as value_as_number,
'Procedure' as Concept_Domain
from
""" + os.environ["WORKSPACE_CDR"] + """.visit_occurrence vis
inner join
""" + os.environ["WORKSPACE_CDR"] + """.procedure_occurrence proc
on vis.visit_occurrence_id = proc.visit_occurrence_id
where
vis.person_id in (select person_id from AFAB)
and proc.procedure_concept_id in """ + concepts + """
union distinct
select distinct
vis.person_id,
vis.visit_end_date,
mes.measurement_concept_id as concept_id,
value_as_number,
'Measurement' as Concept_Domain
from
""" + os.environ["WORKSPACE_CDR"] + """.visit_occurrence vis
inner join
""" + os.environ["WORKSPACE_CDR"] + """.measurement mes
on vis.visit_occurrence_id = mes.visit_occurrence_id
where
vis.person_id in (select person_id from AFAB)
and mes.measurement_concept_id in """ + concepts + """
"""
visit_table = pd.read_gbq(
visit_query,
dialect="standard",
use_bqstorage_api=("BIGQUERY_STORAGE_API_ENABLED" in os.environ),
progress_bar_type="tqdm_notebook")
return visit_table
def get_episodes(visit_table,concept_table):
# Combines visits with min/max month timing and orders by date
visits_w_ranges = (
pd.merge(
visit_table[['person_id','visit_end_date','concept_id']]
.assign(visit_end_date = lambda x : pd.to_datetime(x.visit_end_date)),
concept_table[['domain_concept_id','min_month','max_month']]
.rename({'domain_concept_id':'concept_id'},axis=1)
)
.sort_values(['person_id','visit_end_date','concept_id'])
.reset_index(drop=True)
.assign(visit_rank = lambda x : (
x
.assign(temp = 1)
.groupby('person_id')['temp']
.cumsum()
)
)
)
# Compares visits with all previous ones
# Get max and min diff in gestation timing, compares with actual time difference
get_diffs = (
pd.merge(
visits_w_ranges,
visits_w_ranges
.rename({
'visit_end_date' : 'prev_visit',
'concept_id' : 'prev_concept',
'min_month' : 'prev_min',
'max_month' : 'prev_max',
'visit_rank' : 'prev_rank'
},axis=1),
how='left'
)
.query('visit_rank > prev_rank or (visit_rank == 1 and prev_rank == 1)')
.reset_index(drop=True)
.assign(
t_diff = lambda x : (x.visit_end_date - x.prev_visit).dt.days/30,
max_diff = lambda x : x.max_month - x.prev_min + 2,
min_diff = lambda x : x.min_month - x.prev_max - 2,
agree = lambda x : (x.t_diff <= x.max_diff) & (x.t_diff >= x.min_diff),
bridge_val = lambda x : x.visit_rank - x.prev_rank
)
)
# Consecuitively checks if visit agrees with previous visits
# Flags and numbers start of new episodes
get_eps = (
get_diffs
.groupby(['person_id','visit_end_date','concept_id'])[['agree']]
.sum()
.reset_index()
.assign(
t_diff = lambda x : (x.visit_end_date - (
x
.groupby('person_id')['visit_end_date']
.shift(1))).dt.days/30,
new_ep = lambda x : ((x.agree == 0) & (x.t_diff > 2)) | (x.t_diff.isnull()) | (x.t_diff > 10),
episode = lambda x : (
x
.groupby('person_id')['new_ep']
.cumsum()
)
)[['person_id','visit_end_date','concept_id','episode']]
)
# Flags episode to remove if length is not feasible
remove_eps = (
pd.merge(
get_eps,
get_eps
.groupby(['person_id','episode'])['visit_end_date']
.agg(['min','max'])
.reset_index()
.rename({'min' : 'ep_min','max' : 'ep_max'},axis=1)
.assign(
ep_len = lambda x : (x.ep_max - x.ep_min).dt.days/30
)
)
.query('ep_len > 12')[['person_id','episode']]
.drop_duplicates()
.rename({'episode' : 'rem_ep'},axis=1)
)
# Remove flagged episodes, and decrement subsequent episodes
rem_eps = (
pd.merge(
get_eps.drop(['episode'],axis=1),
pd.merge(
get_eps,
remove_eps,
how = 'left'
)
.query('rem_ep.isnull() or episode != rem_ep')
.assign(
dec = lambda x : x.episode > x.rem_ep,
)
.groupby(['person_id','visit_end_date','concept_id','episode'])[['dec']]
.sum()
.reset_index()
.assign(
episode = lambda x : x.episode - x.dec
)[['person_id','visit_end_date','concept_id','episode']],
how = 'inner'
)
)
return rem_eps
def get_range(episodes):
# Get recorded range for each episode
ep_range = (
episodes
.groupby(['person_id','episode'])['visit_end_date']
.agg(['min','max'])
.reset_index()
.rename({'min' : 'ep_min','max' : 'ep_max'},axis=1)
.assign(ep_max_plus_two = lambda x : x.ep_max + pd.to_timedelta(60,'days'))
)
# Flag episodes where episode freuqncy is too great
freq_too_high = (
pd.merge(
ep_range
.groupby('person_id')['episode']
.max()
.reset_index(),
pd.merge(
ep_range
.groupby(['person_id'])[['ep_min']]
.min()
.reset_index(),
ep_range
.groupby(['person_id'])[['ep_max']]
.max()
.reset_index()
)
)
.assign(
rng = lambda x : (x.ep_max - x.ep_min).dt.days/365,
freq = lambda x : x.episode / x.rng
)
.query('freq >= 5 and episode > 1')['person_id']
)
# Remove episodes where episode freuqncy is too great
final_episodes = (
pd.merge(
episodes,
ep_range[~ep_range['person_id'].isin(freq_too_high)]
)
)
return final_episodes
def main(concepts_file):
concepts_table = pd.read_csv(concepts_file)
visits = data_pull(concepts_table)
eps = get_episodes(visits,concepts_table)
final_episodes = get_range(eps)
return final_episodes