[d9566e]: / sybil / datasets / mgh.py

Download this file

457 lines (383 with data), 17.2 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import numpy as np
from tqdm import tqdm
from ast import literal_eval
from sybil.datasets.nlst import NLST_Survival_Dataset
from collections import Counter
import copy
DEVICE_ID = {
"GE MEDICAL SYSTEMS": 0,
"TOSHIBA": 1,
"Philips": 2,
"SIEMENS": 3,
"Siemens Healthcare": 3, # note: same id as SIEMENS
"Vital Images, Inc.": 4,
"Hitachi Medical Corporation": 5,
"LightSpeed16": 6,
}
class MGH_Dataset(NLST_Survival_Dataset):
"""
MGH Dataset Cohort 1
"""
def create_dataset(self, split_group):
"""
Gets the dataset from the paths and labels in the json.
Arguments:
split_group(str): One of ['train'|'dev'|'test'].
Returns:
The dataset as a dictionary with img paths, label,
and additional information regarding exam or participant
"""
dataset = []
# if split probs is set, randomly assign new splits, (otherwise default is 70% train, 15% dev and 15% test)
if self.args.assign_splits:
np.random.seed(self.args.cross_val_seed)
self.assign_splits(self.metadata_json)
for mrn_row in tqdm(self.metadata_json):
pid, split, exams = mrn_row["pid"], mrn_row["split"], mrn_row["accessions"]
# pt_metadata missing
for exam_dict in exams:
studyuid = exam_dict["StudyInstanceUID"]
bridge_uid = exam_dict["bridge_uid"]
days_to_last_exam = -int(
exam_dict["diff_days"]
) # no. of days to the oldest exam (0 or a negative int)
exam_no = self.get_exam_no(days_to_last_exam, exams)
y, y_seq, y_mask, time_at_event = self.get_label(exam_dict, exams)
for series_id, series_dict in exam_dict["image_series"].items():
if self.skip_sample(series_dict, exam_dict, mrn_row, split_group):
continue
img_paths = series_dict["paths"]
img_paths = [p.replace("Data082021", "pngs") for p in img_paths]
slice_locations = series_dict["image_posn"]
series_data = series_dict["series_data"]
device = DEVICE_ID[series_data["Manufacturer"]]
sorted_img_paths, sorted_slice_locs = self.order_slices(
img_paths, slice_locations
)
sample = {
"paths": sorted_img_paths,
"slice_locations": sorted_slice_locs,
"y": int(y),
"time_at_event": time_at_event,
"y_seq": y_seq,
"y_mask": y_mask,
"exam": int(
"{}{}".format(
studyuid.replace(".", "")[-5:],
series_id.replace(".", "")[-5:],
)
), # last 5 of study id + last 5 of series id
"exam_str": "{}_{}".format(bridge_uid, exam_no),
"accession": exam_no,
"study": studyuid,
"series": series_id,
"pid": pid,
"device": device,
"lung_rads": -1
if exam_dict["lung_rads"] == np.nan
else exam_dict["lung_rads"],
"IV_contrast": exam_dict["IV_contrast"],
"lung_cancer_screening": exam_dict["lung_cancer_screening"],
"cancer_location": np.zeros(14), # mgh has no annotations
"cancer_laterality": np.zeros(
3, dtype=np.int
), # has to be int, while cancer_location has to be float
"num_original_slices": len(series_dict["paths"]),
"annotations": [],
"pixel_spacing": series_dict["pixel_spacing"]
+ [series_dict["slice_thickness"]],
"slice_thickness": self.get_slice_thickness_class(
series_dict["slice_thickness"]
),
}
if self.args.use_risk_factors:
sample["risk_factors"] = self.get_risk_factors(
exam_dict, return_dict=False
)
if self.args.use_annotations:
# mgh has no annotations, so set everything to zero / false
sample["volume_annotations"] = np.array(
[0 for _ in sample["paths"]]
)
sample["annotations"] = [
{"image_annotations": None} for path in sample["paths"]
]
dataset.append(sample)
return dataset
def skip_sample(self, series_dict, exam_dict, mrn_row, split):
if not mrn_row["split"] == split:
return True
if mrn_row["in_cohort2"]:
return True
# check if screen is localizer screen or not enough images
if self.is_localizer(series_dict["series_data"]):
return True
slice_thickness = series_dict["slice_thickness"]
# check if restricting to specific slice thicknesses
if (self.args.slice_thickness_filter is not None) and (
(slice_thickness in ["", None])
or (slice_thickness > self.args.slice_thickness_filter)
or (slice_thickness < 0)
):
return True
if series_dict["pixel_spacing"] is None:
return True
# remove where slice location doesn't change (different axis):
if len(set(series_dict["image_posn"])) < 2:
return True
if len(series_dict["paths"]) < self.args.min_num_images:
return True
return False
def get_exam_no(self, diff_days, exams):
"""Gets the index of the exam, compared to the other exams"""
sorted_days = sorted([-exam["diff_days"] for exam in exams], reverse=True)
return sorted_days.index(diff_days)
def get_label(self, exam_dict, exams):
is_cancer_cohort = exam_dict["cancer_cohort_yes_no"] == "yes"
days_to_last_followup = -exam_dict["diff_days"]
years_to_last_followup = days_to_last_followup // 365
y = 0
y_seq = np.zeros(self.args.max_followup)
if is_cancer_cohort:
days_to_cancer = -exam_dict["diff_days_exam_lung_cancer_diagnosis"]
years_to_cancer = int(days_to_cancer // 365)
y = years_to_cancer < self.args.max_followup
time_at_event = min(years_to_cancer, self.args.max_followup - 1)
y_seq[years_to_cancer:] = 1
else:
time_at_event = min(years_to_last_followup, self.args.max_followup - 1)
y_mask = np.array(
[1] * (time_at_event + 1)
+ [0] * (self.args.max_followup - (time_at_event + 1))
)
y_mask = y_mask[: self.args.max_followup]
return y, y_seq.astype("float64"), y_mask.astype("float64"), time_at_event
def get_risk_factors(self, exam_dict, return_dict=False):
risk_factors = {
"age_at_exam": exam_dict["age_at_exam"],
"pack_years": exam_dict["pack_years"],
"race": exam_dict["race"],
"sex": exam_dict["sex"],
"smoking_status": exam_dict["smoking_status"],
}
if return_dict:
return risk_factors
else:
return np.array(
[v for v in risk_factors.values() if not isinstance(v, str)]
)
def is_localizer(self, series_dict):
is_localizer = "LOCALIZER" in literal_eval(series_dict["ImageType"])
return is_localizer
@staticmethod
def set_args(args):
args.num_classes = args.max_followup
def get_summary_statement(self, dataset, split_group):
summary = "Constructed MGH CT Cancer Survival {} dataset with {} records, {} exams, {} patients, and the following class balance \n {}"
class_balance = Counter([d["y"] for d in dataset])
exams = set([d["exam"] for d in dataset])
patients = set([d["pid"] for d in dataset])
statement = summary.format(
split_group,
len(dataset),
len(exams),
len(patients),
class_balance,
)
statement += "\n" + "Censor Times: {}".format(
Counter([d["time_at_event"] for d in dataset])
)
return statement
def assign_splits(self, meta):
for idx in range(len(meta)):
meta[idx]["split"] = np.random.choice(
["train", "dev", "test"], p=self.args.split_probs
)
class MGH_Screening(NLST_Survival_Dataset):
"""
MGH Dataset Cohort 2
"""
def create_dataset(self, split_group):
"""
Gets the dataset from the paths and labels in the json.
Arguments:
split_group(str): One of ['train'|'dev'|'test'].
Returns:
The dataset as a dictionary with img paths, label,
and additional information regarding exam or participant
"""
assert not self.args.train, "Cohort 2 should not be used for training"
dataset = []
for mrn_row in tqdm(self.metadata_json):
pid, exams = mrn_row["pid"], mrn_row["accessions"]
for exam_dict in exams:
for series_id, series_dict in exam_dict["image_series"].items():
if self.skip_sample(series_dict, exam_dict, mrn_row):
continue
sample = self.get_volume_dict(
series_id, series_dict, exam_dict, mrn_row
)
if len(sample) == 0:
continue
dataset.append(sample)
return dataset
def skip_sample(self, series_dict, exam_dict, mrn_row):
# unknown cancer status
if exam_dict["Future_cancer"] == "unkown":
return True
if (exam_dict["days_before_cancer_dx"] < 0) or (
exam_dict["days_to_last_follow_up"] < 0
):
return True
# check if screen is localizer screen or not enough images
if self.is_localizer(series_dict["series_data"]):
return True
slice_thickness = series_dict["SliceThickness"]
# check if restricting to specific slice thicknesses
if (self.args.slice_thickness_filter is not None) and (
(slice_thickness in ["", None])
or (slice_thickness > self.args.slice_thickness_filter)
or (slice_thickness < 0)
):
return True
if series_dict["PixelSpacing"] is None:
return True
if len(series_dict["paths"]) < self.args.min_num_images:
return True
return False
def get_volume_dict(self, series_id, series_dict, exam_dict, mrn_row):
img_paths = series_dict["paths"]
img_paths = [
p.replace("MIT_Lung_Cancer_Screening", "screening_pngs").replace(
".dcm", ".png"
)
for p in img_paths
]
slice_locations = series_dict["slice_location"]
series_data = series_dict["series_data"]
pixel_spacing = series_dict["PixelSpacing"] + [series_dict["SliceThickness"]]
sorted_img_paths, sorted_slice_locs = self.order_slices(
img_paths, slice_locations, reverse=True
)
device = DEVICE_ID[series_data["Manufacturer"]]
studyuid = exam_dict["StudyInstanceUID"]
bridge_uid = exam_dict["bridge_uid"]
y, y_seq, y_mask, time_at_event = self.get_label(exam_dict, mrn_row)
sample = {
"paths": sorted_img_paths,
"slice_locations": sorted_slice_locs,
"y": int(y),
"time_at_event": time_at_event,
"y_seq": y_seq,
"y_mask": y_mask,
"exam": int(
"{}{}".format(
studyuid.replace(".", "")[-5:],
series_id.replace(".", "")[-5:],
)
), # last 5 of study id + last 5 of series id
"study": studyuid,
"series": series_id,
"pid": mrn_row["pid"],
"bridge_uid": bridge_uid,
"device": device,
"lung_rads": exam_dict["LR Score"],
"cancer_location": np.zeros(14), # mgh has no annotations
"cancer_laterality": np.zeros(
3, dtype=np.int
), # has to be int, while cancer_location has to be float
"num_original_slices": len(series_dict["paths"]),
"marital_status": exam_dict["marital_status"],
"religion": exam_dict["religion"],
"primary_site": exam_dict["Primary Site"],
"laterality1": exam_dict["Laterality"],
"laterality2": exam_dict["Laterality.1"],
"icdo3": exam_dict["Histo/Behavior ICD-O-3"],
"pixel_spacing": pixel_spacing,
"slice_thickness": self.get_slice_thickness_class(pixel_spacing[-1]),
}
if self.args.use_risk_factors:
sample["risk_factors"] = self.get_risk_factors(exam_dict, return_dict=False)
if self.args.use_annotations:
# mgh has no annotations, so set everything to zero / false
sample["volume_annotations"] = np.array([0 for _ in sample["paths"]])
sample["annotations"] = [
{"image_annotations": None} for path in sample["paths"]
]
return sample
def get_label(self, exam_dict, mrn_row):
is_cancer_cohort = exam_dict["Future_cancer"].lower().strip() == "yes"
days_to_cancer = exam_dict["days_before_cancer_dx"]
y = False
if (
is_cancer_cohort
and (not np.isnan(days_to_cancer))
and (days_to_cancer > -1)
):
years_to_cancer = int(days_to_cancer // 365)
y = years_to_cancer < self.args.max_followup
y_seq = np.zeros(self.args.max_followup)
if y:
time_at_event = years_to_cancer
y_seq[years_to_cancer:] = 1
else:
if is_cancer_cohort:
assert (days_to_cancer < 0) or (
years_to_cancer >= self.args.max_followup
)
time_at_event = self.args.max_followup - 1
else:
days_to_last_neg_followup = exam_dict["days_to_last_follow_up"]
years_to_last_neg_followup = int(days_to_last_neg_followup // 365)
time_at_event = min(
years_to_last_neg_followup, self.args.max_followup - 1
)
y_mask = np.array(
[1] * (time_at_event + 1)
+ [0] * (self.args.max_followup - (time_at_event + 1))
)
y_mask = y_mask[: self.args.max_followup]
return y, y_seq.astype("float64"), y_mask.astype("float64"), time_at_event
def get_risk_factors(self, exam_dict, return_dict=False):
risk_factors = {
"race": exam_dict["race"],
"pack_years": exam_dict["Packs Years"],
"age_at_exam": exam_dict["age at the exam"],
"gender": exam_dict["gender"],
"smoking_status": exam_dict["Smoking Status"],
"lung_rads": exam_dict["LR Score"],
"years_since_quit_smoking": exam_dict["Year Since Last Smoked"],
}
if return_dict:
return risk_factors
else:
return np.array(
[v for v in risk_factors.values() if not isinstance(v, str)]
)
def is_localizer(self, series_dict):
is_localizer = "LOCALIZER" in literal_eval(series_dict["ImageType"])
return is_localizer
@staticmethod
def set_args(args):
args.num_classes = args.max_followup
def get_summary_statement(self, dataset, split_group):
summary = "Constructed MGH CT Cancer Survival {} dataset with {} records, {} exams, {} patients, and the following class balance \n {}"
class_balance = Counter([d["y"] for d in dataset])
exams = set([d["exam"] for d in dataset])
patients = set([d["pid"] for d in dataset])
statement = summary.format(
split_group,
len(dataset),
len(exams),
len(patients),
class_balance,
)
statement += "\n" + "Censor Times: {}".format(
Counter([d["time_at_event"] for d in dataset])
)
return statement
def assign_splits(self, meta):
for idx in range(len(meta)):
meta[idx]["split"] = np.random.choice(
["train", "dev", "test"], p=self.args.split_probs
)