-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorize_meta.py
198 lines (176 loc) · 6 KB
/
factorize_meta.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
import argparse
import os
import pickle
import numpy as np
import pandas as pd
invalid = [
"ethnicity not specified",
"Other",
"Decline to State",
"nan",
"Unnamed",
"Unknown",
"declined (please note reason drops were declined)", # attr. clinical_pupilDilation
"other dilating agents (please note dilating agents used)", # attr. clinical_pupilDilation
"not necessary", # attr. clinical_pupilDilation
]
def get_mapping_categorical_meta(
metadata,
map_invalids=False,
):
mapping = {}
for col in metadata.columns:
if metadata[col].isnull().values.any() and metadata[col].dtype != float:
meta = metadata[col].replace(np.nan, "nan", regex=True)
else:
meta = metadata[col]
unique_entries = np.unique(meta.to_numpy())
if invalid is not None:
attributes = np.array(
[entry for entry in unique_entries if str(entry) not in invalid]
)
attributes = np.sort(unique_entries)
mapping[col] = {
str(attr): i
for i, attr in enumerate(attributes)
if str(attr) not in invalid
}
if map_invalids:
invalids_in_attributes = list(
set([str(entry) for entry in unique_entries]) & set(invalid)
)
mapping[col].update({l: -1 for l in invalids_in_attributes})
mapping[col] = {
key: (i if value != -1 else -1)
for i, (key, value) in enumerate(mapping[col].items())
if value >= -1
}
return mapping
def factorize_categorical_meta(
metadata,
mapping,
):
meta_fac = metadata.copy()
for col in metadata.columns:
factorized = [mapping[col][str(entry)] for entry in metadata[col]]
meta_fac[col] = factorized
return meta_fac
def onehot_encoding(x, num_classes):
if x >= 0:
return list(np.eye(num_classes)[x])
else:
return x
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Factorize eyepacs metadata",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--root_dir",
type=str,
help="eyepacs root directory",
default="/gpfs01/berens/data/data/eyepacs/",
)
parser.add_argument(
"--columns_mapping_file",
type=str,
help="file name to save columns mapping",
default="/gpfs01/berens/data/data/eyepacs/data_processed/metadata/factorized/meta_categorical_columns_mapping.pkl",
)
parser.add_argument(
"--factorized_metadata",
type=str,
help="file name to save factorized metadata",
default="/gpfs01/berens/data/data/eyepacs/data_processed/metadata/factorized/metadata.csv",
)
parser.add_argument(
"--metadata_onehot",
type=str,
help="file name to save factorized, onehot encoded metadata",
default=None,
)
args = parser.parse_args()
root_dir = args.root_dir
metadata_train = pd.read_csv(
os.path.join(root_dir, "data_processed/metadata/splits_circular_crop/train.csv")
)
metadata_val = pd.read_csv(
os.path.join(root_dir, "data_processed/metadata/splits_circular_crop/val.csv")
)
metadata_test = pd.read_csv(
os.path.join(root_dir, "data_processed/metadata/splits_circular_crop/test.csv")
)
metadata = pd.concat([metadata_train, metadata_val, metadata_test])
meta_image_paths = metadata["image_path"]
meta_categorical_attribues = metadata[
[
"camera",
"eye_side",
"image_side",
"image_field",
"patient_ethnicity",
"patient_gender",
"diagnosis_image_dr_level",
"diagnosis_dme",
"diagnosis_type_diabetes",
"diagnosis_maculopathy",
"diagnosis_cataract",
"diagnosis_glaucoma",
"diagnosis_occlusion",
"clinical_hypertension",
"clinical_pupilDilation",
# "clinical_siteIdentifier",
"clinical_insulinDependent",
"clinical_yearsWithDiabetes",
"clinical_insulinDependDuration",
"session_num_diagnoses",
"session_num_consults",
"session_image_quality",
]
]
meta_continuous_attributes = metadata[
[
"patient_age",
"clinical_encounterDate",
"mask_ratio_vt",
"mask_ratio_vb",
]
]
# age correction
age_diff = 2022 - meta_continuous_attributes.clinical_encounterDate.values
meta_continuous_attributes["patient_age"] = (
meta_continuous_attributes.patient_age.values - age_diff
)
meta_categorical_columns_mapping = get_mapping_categorical_meta(
meta_categorical_attribues, map_invalids=True
)
meta_categorical = factorize_categorical_meta(
meta_categorical_attribues,
meta_categorical_columns_mapping,
)
with open(
args.columns_mapping_file,
"wb",
) as f:
pickle.dump(meta_categorical_columns_mapping, f)
metadata_factorized = pd.concat(
[meta_image_paths, meta_categorical, meta_continuous_attributes], axis=1
)
metadata_factorized.to_csv(args.factorized_metadata)
if args.metadata_onehot is not None:
meta_categorical_onehot = pd.DataFrame(columns=meta_categorical.columns)
for col in meta_categorical.columns:
data = []
num_classes = max(list(meta_categorical_columns_mapping[col].values()))
for entry in meta_categorical[col]:
data.append(onehot_encoding(entry, num_classes + 1))
meta_categorical_onehot[col] = data
metadata_onehot = pd.concat(
[
meta_image_paths.reset_index(),
meta_categorical_onehot,
meta_continuous_attributes.reset_index(),
],
axis=1,
)
metadata_onehot.to_pickle(args.metadata_onehot)