-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_CNFUN.py
85 lines (70 loc) · 2.72 KB
/
query_CNFUN.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
import sys
from query_common import filter_records, ProjectMixins
from redcap import Project # note this is from PyCap.redcap
from typing import List
"""
This class of functions are responsible of retrieving relevant data structures from the CNFUN tables
"""
class CNFUN_project(ProjectMixins):
"""
One baby can have many admissions CaseIDs.
One hospital record can have many CaseIDs.
One baby has only one hospital record number.
"""
def __init__(
self, Token, URL, get_all_field=False,
):
"""
Create a project using PyCap
:param Token:
:param URL:
:return:
"""
# Several key properties we'll use throughout
self.project = Project(URL, Token)
# These are very important ID fields from the
fields_keyid = ["patientID", "cf_p_cnnpatientui"]
# For now, make sure to onyl get the data related to these key ids to reduce load time
self.data = self.get_fields(fields_keyid)
# if specified, get all the records.
if get_all_field:
self.data = self.project.export_records()
def filter_with_CNNPatientUI(self, CNNPatientUI: str or List[str]):
"""
Check the list, only retain the relevant records with matching PatientID are retained.
:param dataset: CNBPIDs & record ID correspondence list.
:param CNNPatientUI:
:return:
"""
list_filtered = None
filtered_field = "cf_p_cnnpatientui"
# Handling when babyIDs is string instead of list (allowing batch function).
if type(CNNPatientUI) is str:
CNNPatientUI = [CNNPatientUI]
list_filtered = filter_records(self.data, filtered_field, CNNPatientUI)
return list_filtered
def get_PatientID_with_CNNPatientUI(self, CNNPatientUI: str or List[str]):
"""
PatientID has 1:1 correspondence with CNNPatientUI which is the same as PatientUI from CNN Baby table.
:return:
"""
# Listify the CNNPatientUI
if type(CNNPatientUI) is str:
CNNPatientUI = [CNNPatientUI]
# Filter with the information
list_filtered_dict = self.filter_with_CNNPatientUI(CNNPatientUI)
# Aggregate the list_PatientID
list_PatientID = []
for case in list_filtered_dict:
list_PatientID.append(case["patientid"])
return list_PatientID
def get_records_CNFUN(self, PatientID: str or List[str]):
"""
Retrieve the cases based on their INDEX which is the
:param cases:
:return:
"""
if type(PatientID) is str:
PatientID = [PatientID]
cases_data = self.project.export_records(records=PatientID)
return cases_data