[637b40]: / notebooks / stratification_checks_sep_24.py

Download this file

194 lines (152 with data), 4.4 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
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
# %%
from collections import defaultdict
import json
import os
import numpy as np
import pandas as pd
from matplotlib.pyplot import hist
from skmultilearn.model_selection import iterative_train_test_split
# enable lib loading even if not installed as a pip package or in PYTHONPATH
# also convenient for relative paths in example config files
from pathlib import Path
os.chdir(Path(__file__).resolve().parent.parent)
# %%
from adpkd_segmentation.data.data_utils import ( # noqa
display_sample,
get_labeled,
get_y_Path,
make_dcmdicts,
path_2dcm_int16,
path_2label,
)
from adpkd_segmentation.data.data_utils import ( # noqa
PATIENT,
SEQUENCE,
KIDNEY_PIXELS,
MR,
VOXEL_VOLUME,
)
from adpkd_segmentation.data.link_data import makelinks # noqa
STUDY_TKV = "study_tkv"
# %% needed once for new data
makelinks()
# %%
dcm_paths = sorted(get_labeled())
dcm2attribs, patient2dcm = make_dcmdicts(tuple(dcm_paths))
all_ids = list(patient2dcm.keys())
# TKV checks
# %%
def TKV_update(dcm2attribs):
studies = defaultdict(int)
for dcm, attribs in dcm2attribs.items():
study_id = (attribs[PATIENT], attribs[MR])
studies[study_id] += attribs[KIDNEY_PIXELS] * attribs[VOXEL_VOLUME]
for dcm, attribs in dcm2attribs.items():
tkv = studies[(attribs[PATIENT], attribs[MR])]
attribs[STUDY_TKV] = tkv
return studies, dcm2attribs
# %%
studies, dcm2attribs = TKV_update(dcm2attribs)
hist(studies.values(), bins=40)
# %%
hist(np.log(list(studies.values())), bins=40)
# %%
# Patient info
patient_info = set()
for dcm_path, attribs in dcm2attribs.items():
patient = attribs[PATIENT]
seq = attribs[SEQUENCE]
tkv = attribs[STUDY_TKV]
mr = attribs[MR]
patient_info.add((patient, seq, mr, tkv))
print(patient_info)
# %%
df = pd.DataFrame.from_records(
list(patient_info),
columns=[PATIENT, SEQUENCE, MR, STUDY_TKV],
index=PATIENT,
).sort_index()
# %%
df.to_csv("./notebooks/patients_2020_11_02.csv")
# %%
print(df.index.value_counts())
# %%
print(df.seq.value_counts())
# %%
print(df.study_tkv.describe())
print(np.log(df.study_tkv).describe())
# %%
def create_label_arrays(patient_info, all_ids):
patient_to_label = {}
for id_ in all_ids:
patient_to_label[id_] = np.zeros(8, dtype=np.uint8)
for patient, seq, mr, tkv in patient_info:
# sequence labeling
# the same patient can have more
if "AX SSFSE HR KIDNEYS" == seq:
patient_to_label[patient][0] = 1
elif "AXL FIESTA" == seq:
patient_to_label[patient][1] = 1
elif "PEL" in seq:
patient_to_label[patient][2] = 1
else:
patient_to_label[patient][3] = 1
# LOG TKV category
# 13.69 to 14.74 interquartile range
# 14.19 median
log_tkv = np.log(tkv)
if log_tkv < 13.7:
patient_to_label[patient][4] = 1
elif 13.7 <= log_tkv < 14.2:
patient_to_label[patient][5] = 1
elif 14.2 <= log_tkv < 14.7:
patient_to_label[patient][6] = 1
elif 14.7 <= log_tkv:
patient_to_label[patient][7] = 1
return patient_to_label
# %%
patient2label = create_label_arrays(patient_info, all_ids)
# %%
labels = [patient2label[id_] for id_ in all_ids]
X = np.array(all_ids)[..., np.newaxis]
y = np.stack(labels)
print(y.shape)
print(len(all_ids))
print(X.shape)
# %%
# Split to train, val, test
TRAIN = 0.7
VAL = 0.15
TEST = 0.15
np.random.seed = 42
X_train_val, y_train_val, X_test, y_test = iterative_train_test_split(
X, y, test_size=TEST
)
X_train, y_train, X_val, y_val = iterative_train_test_split(
X_train_val, y_train_val, test_size=VAL / (TRAIN + VAL)
)
# %%
print("Sizes: ", len(X_train), len(X_val), len(X_test))
# %%
print(y_train)
print(y_val)
print(y_test)
# %%
print(df[df.index.isin(X_test.squeeze())])
print(df[df.index.isin(X_val.squeeze())])
# %%
split_dict = {
"train": list(X_train.squeeze()),
"val": list(X_val.squeeze()),
"test": list(X_test.squeeze()),
}
# %%
# move patient with transplanted kidneys to train
# out of scope
# outlier = WC-ADPKD-____-
# split_dict["val"].remove(outlier)
# split_dict["train"].append(outlier)
# %%
with open("./stratification/strat_split_2020_11_03.json", "w") as f:
json.dump(split_dict, f, indent=4)
# %%