-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
121 lines (100 loc) · 2.94 KB
/
Copy pathfeatures.py
File metadata and controls
121 lines (100 loc) · 2.94 KB
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
"""
features from metadata
"""
from typing import Union
import numpy as np
import pandas as pd
from helper_code import get_age, get_cpc, get_ohca, get_outcome, get_rosc, get_sex, get_shockable_rhythm, get_ttm # get_vfib,
__all__ = [
"get_features",
"get_labels",
]
def get_features(patient_metadata: str, ret_type: str = "np") -> Union[np.ndarray, pd.DataFrame, dict]:
"""Extract features from the patient metadata.
Adapted from the official repo.
Parameters
----------
patient_metadata : str
The patient metadata.
ret_type : {"np", "pd", "dict"}
The return type, by default "np".
Returns
-------
np.ndarray or pd.DataFrame or dict
The patient features.
"""
age = get_age(patient_metadata)
sex = get_sex(patient_metadata)
rosc = get_rosc(patient_metadata)
ohca = get_ohca(patient_metadata)
vfib = get_shockable_rhythm(patient_metadata)
ttm = get_ttm(patient_metadata)
# Use one-hot encoding for sex; add more variables
sex_features = np.zeros(2, dtype=int)
if sex == "Female":
female = 1
male = 0
other = 0
elif sex == "Male":
female = 0
male = 1
other = 0
else:
female = 0
male = 0
other = 1
# Combine the patient features.
if ret_type == "np":
patient_features = np.array([age, female, male, other, rosc, ohca, vfib, ttm])
elif ret_type == "pd":
patient_features = pd.DataFrame(
{
"age": age,
"sex_female": female,
"sex_male": male,
"sex_other": other,
"rosc": rosc,
"ohca": ohca,
"vfib": vfib,
"ttm": ttm,
},
index=[0],
)
elif ret_type == "dict":
patient_features = {
"age": age,
"sex_female": female,
"sex_male": male,
"sex_other": other,
"rosc": rosc,
"ohca": ohca,
"vfib": vfib,
"ttm": ttm,
}
return patient_features
def get_labels(patient_metadata: str, ret_type: str = "dict") -> Union[np.ndarray, pd.DataFrame, dict]:
"""Extract labels from the patient metadata.
Adapted from the official repo.
Parameters
----------
patient_metadata : str
The patient metadata.
ret_type : {"np", "pd", "dict"}
The return type, by default "dict".
Returns
-------
dict or np.ndarray or pd.DataFrame
The patient labels, including
- "outcome" (int)
- "cpc" (float)
"""
labels = {}
labels["outcome"] = get_outcome(patient_metadata)
labels["cpc"] = get_cpc(patient_metadata)
if ret_type == "dict":
pass
elif ret_type == "np":
labels = np.array([labels["outcome"], labels["cpc"]])
elif ret_type == "pd":
labels = pd.DataFrame(labels, index=[0])
return labels