-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_temporalsequence_omop.py
55 lines (48 loc) · 1.78 KB
/
example_temporalsequence_omop.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
# This is an example of how to sequence a patient from the OMOP database
# Note: this file should be run from a location where it can import the mcs package (e.g., parent directory of mcs)
import pyodbc
import getpass
from MedicalCodingSequence import *
# SQL server config
sql_config = {
'driver': 'ODBC Driver 17 for SQL Server',
'server': '<server address>',
'database': '<user>',
'uid': '<user>'
}
pwd=getpass.getpass()
conn = pyodbc.connect(**sql_config, pwd=pwd)
cursor = conn.cursor()
# Get all conditions, drugs, procedures, and measurements for a person
person_id = 123456789
sql = """
(SELECT co.condition_concept_id AS concept_id, co.condition_start_datetime AS start_datetime
FROM dbo.condition_occurrence co
WHERE co.person_id = ?)
UNION ALL
(SELECT do.drug_concept_id AS concept_id, do.drug_exposure_start_datetime AS start_datetime
FROM dbo.drug_exposure do
WHERE do.person_id = ?)
UNION ALL
(SELECT po.procedure_concept_id AS concept_id, po.procedure_datetime AS start_datetime
FROM dbo.procedure_occurrence po
WHERE po.person_id = ?)
UNION ALL
(SELECT m.measurement_concept_id AS concept_id, m.measurement_datetime AS start_datetime
FROM dbo.measurement m
WHERE m.person_id = ?)
"""
cursor.execute(sql, person_id, person_id, person_id, person_id)
res = cursor.fetchall()
# Create TemporalSequencer and add records
ts = TemporalSequencer(metadata={'person_id': person_id})
for x in res:
ts.add_data(x[1], x[0])
# Serialize in strict temporal order (no shuffling)
print('strict order')
ser = ts.serialize(shuffle_level=None)
print(ser)
# Serialize with codes within the same day shuffled
print('shuffle @ days')
ser = ts.serialize(shuffle_level='d')
print(ser)