-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxray.py
575 lines (505 loc) · 19.8 KB
/
xray.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
from pathlib import Path
from typing import Callable, Dict, Optional
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import torch
from torch.utils.data import Dataset
from skimage import io
from torchvision.transforms import ToTensor, Resize, CenterCrop
from data_handling.base import BaseDataModuleClass
from datetime import datetime
import os
from data_handling.caching import SharedCache
from data_handling.augmentations import DataAugmentationDINO
# Please update this with your own paths.
DATA_DIR_RSNA = Path("/vol/biomedic3/mb121/rsna-pneumonia-detection-challenge")
DATA_DIR_RSNA_PROCESSED_IMAGES = DATA_DIR_RSNA / "preprocess_224_224"
PATH_TO_PNEUMONIA_WITH_METADATA_CSV = (
Path(__file__).parent / "pneumonia_dataset_with_metadata.csv"
)
cluster_root = os.getenv("HOME", None)
if Path("/data2/PadChest").exists():
PADCHEST_ROOT = Path("/data2/PadChest")
PADCHEST_IMAGES = PADCHEST_ROOT / "preprocessed"
elif (Path(cluster_root) / "PadChest").exists():
PADCHEST_ROOT = Path(cluster_root) / "PadChest"
PADCHEST_IMAGES = PADCHEST_ROOT / "preprocessed"
else:
PADCHEST_ROOT = Path("/vol/biodata/data/chest_xray/BIMCV-PADCHEST")
PADCHEST_IMAGES = PADCHEST_ROOT / "images"
def prepare_padchest_csv():
df = pd.read_csv(
PADCHEST_ROOT / "PADCHEST_chest_x_ray_images_labels_160K_01.02.19.csv"
)
df = df.loc[df.Projection == "PA"]
df = df.loc[df.Pediatric == "No"]
def process(x, target):
if isinstance(x, str):
list_labels = x[1:-1].split(",")
list_labels = [label.replace("'", "").strip() for label in list_labels]
return target in list_labels
else:
return False
for label in [
"pneumonia",
"exclude",
"suboptimal study",
]:
df[label] = df.Labels.astype(str).apply(lambda x: process(x, label))
print(df[label].value_counts())
df = df.loc[~df.exclude]
df = df.loc[~df["suboptimal study"]]
df["Manufacturer"] = df.Manufacturer_DICOM.apply(
lambda x: "Phillips" if x == "PhilipsMedicalSystems" else "Imaging"
)
df = df.loc[df["PatientSex_DICOM"].isin(["M", "F"])]
df["PatientAge"] = (
df.StudyDate_DICOM.apply(lambda x: datetime.strptime(str(x), "%Y%M%d").year)
- df.PatientBirth
)
invalid_filenames = [
"216840111366964013829543166512013353113303615_02-092-190.png",
"216840111366964013962490064942014134093945580_01-178-104.png",
"216840111366964012989926673512011151082430686_00-157-045.png",
"216840111366964012558082906712009327122220177_00-102-064.png",
"216840111366964012959786098432011033083840143_00-176-115.png",
"216840111366964012373310883942009152114636712_00-102-045.png",
"216840111366964012487858717522009280135853083_00-075-001.png",
"216840111366964012819207061112010307142602253_04-014-084.png",
"216840111366964012989926673512011074122523403_00-163-058.png",
"216840111366964013590140476722013058110301622_02-056-111.png",
"216840111366964012339356563862009072111404053_00-043-192.png",
"216840111366964013590140476722013043111952381_02-065-198.png",
"216840111366964012819207061112010281134410801_00-129-131.png",
"216840111366964013686042548532013208193054515_02-026-007.png",
"216840111366964012989926673512011083134050913_00-168-009.png"
# '216840111366964013590140476722013058110301622_02-056-111.png'
]
df = df.loc[~df.ImageID.isin(invalid_filenames)]
return df
class PadChestDataModule(BaseDataModuleClass):
def create_datasets(self):
df = prepare_padchest_csv()
label_col = self.config.data.label
train_val_id, test_id = train_test_split(
df.PatientID.unique(),
test_size=0.20,
random_state=33,
)
train_id, val_id = train_test_split(
train_val_id,
test_size=0.10,
random_state=33,
)
if self.config.data.prop_train < 1.0:
rng = np.random.default_rng(self.config.seed)
train_id = rng.choice(
train_id,
size=int(self.config.data.prop_train * train_id.shape[0]),
replace=False,
)
self.dataset_train = PadChestDataset(
df=df.loc[df.PatientID.isin(train_id)],
transform=self.train_tsfm,
label_column=label_col,
parents=self.parents,
cache=self.config.data.cache,
use_counterfactuals=self.config.data.use_counterfactuals,
counterfactual_contrastive_pairs=self.config.data.counterfactual_contrastive,
)
self.dataset_val = PadChestDataset(
df=df.loc[df.PatientID.isin(val_id)],
transform=self.val_tsfm,
label_column=label_col,
parents=self.parents,
cache=self.config.data.cache,
)
self.dataset_test = PadChestDataset(
df=df.loc[df.PatientID.isin(test_id)],
transform=self.val_tsfm,
label_column=label_col,
cache=True,
)
@property
def dataset_name(self):
return "padchest"
@property
def num_classes(self):
return 2
class CheXpertDataModule(BaseDataModuleClass):
def create_datasets(self):
label_col = self.config.data.label
df = pd.read_csv("/vol/biodata/data/chest_xray/CheXpert-v1.0/meta/train.csv")
df.fillna(0, inplace=True) # assume no mention is like negative
df = df.loc[df["AP/PA"] == "PA"]
df = df.loc[df[self.config.data.label] != -1] # remove the uncertain cases
df["PatientID"] = df["Path"].apply(
lambda x: int(Path(x).parent.parent.stem[-5:])
)
patient_id = df["PatientID"].unique()
train_val_id, test_id = train_test_split(
patient_id, test_size=0.4, random_state=33
)
train_id, val_id = train_test_split(
train_val_id, test_size=0.15, random_state=33
)
if self.config.data.prop_train < 1.0:
rng = np.random.default_rng(self.config.seed)
train_id = rng.choice(
train_id,
size=int(self.config.data.prop_train * train_id.shape[0]),
replace=False,
)
self.dataset_train = CheXpertDataset(
df=df.loc[df.PatientID.isin(train_id)],
transform=self.train_tsfm,
cache=self.config.data.cache,
label_col=label_col,
)
self.dataset_val = CheXpertDataset(
df=df.loc[df.PatientID.isin(val_id)],
transform=self.val_tsfm,
cache=self.config.data.cache,
label_col=label_col,
)
self.dataset_test = CheXpertDataset(
df=df.loc[df.PatientID.isin(test_id)],
transform=self.val_tsfm,
cache=True,
label_col=label_col,
)
@property
def dataset_name(self):
return "chexpert"
@property
def num_classes(self):
return 2
class PadChestDataset(Dataset):
def __init__(
self,
df: pd.DataFrame,
label_column: str,
transform: Callable,
parents: Optional = None,
cache: bool = False,
use_counterfactuals: bool = False,
counterfactual_contrastive_pairs: bool = True,
):
super().__init__()
print(f"Len {len(df)}")
self.counterfactual_contrastive_pairs = counterfactual_contrastive_pairs
self.parents = parents
self.use_counterfactuals = use_counterfactuals
self.label_col = label_column
self.pneumonia = df.pneumonia.astype(int).values
self.img_paths = df.ImageID.values
self.genders = df.PatientSex_DICOM.values
self.ages = df.PatientAge.values
self.manufacturers = df.Manufacturer.values
self.cache = cache
self.transform = transform
if cache:
self.cache = SharedCache(
size_limit_gib=24,
dataset_len=self.img_paths.shape[0],
data_dims=[1, 224, 224],
dtype=torch.float32,
)
else:
self.cache = None
def __len__(self):
return len(self.img_paths)
def read_image(self, idx):
try:
img = io.imread(PADCHEST_IMAGES / self.img_paths[idx], as_gray=True)
except: # noqa
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
print(self.img_paths[idx])
img = io.imread(PADCHEST_IMAGES / self.img_paths[idx], as_gray=True)
print("success")
ImageFile.LOAD_TRUNCATED_IMAGES = False
img = img / (img.max() + 1e-12)
img = CenterCrop(224)(Resize(224, antialias=True)(ToTensor()(img)))
return img.float()
def __getitem__(self, idx: int) -> Dict:
if self.cache is not None:
img = self.cache.get_slot(idx)
if img is None:
img = self.read_image(idx)
self.cache.set_slot(idx, img, allow_overwrite=True)
else:
img = self.read_image(idx)
sample = {}
sample["pneumonia"] = self.pneumonia[idx]
sample["age"] = self.ages[idx] / 100
sample["sex"] = 0 if self.genders[idx] == "M" else 1
sample["scanner"] = 0 if self.manufacturers[idx] == "Phillips" else 1
sample["y"] = sample[self.label_col]
sample["shortpath"] = self.img_paths[idx]
if self.parents is not None:
sample["pa"] = torch.cat(
[
sample[c]
if isinstance(sample[c], torch.Tensor)
else torch.tensor([sample[c]])
for c in self.parents
]
).detach()
if isinstance(self.transform, DataAugmentationDINO):
if self.use_counterfactuals:
if self.counterfactual_contrastive_pairs:
# Sample domain if same as real then use real
cfx = (
self.load_counterfactual_image(idx)
if torch.rand(1).item() > 0.5
else img.clone()
)
else:
if torch.rand(1).item() > 0.5:
cfx = self.load_counterfactual_image(idx)
img = cfx.clone()
else:
cfx = img.clone()
img = torch.stack([img, cfx], dim=0)
img = self.transform(img.float())
sample.update(img)
else:
if self.use_counterfactuals:
if self.counterfactual_contrastive_pairs:
cfx = self.load_counterfactual_image(idx)
else:
if torch.rand(1).item() > 0.5:
cfx = self.load_counterfactual_image(idx)
img = cfx.clone()
else:
cfx = img.clone()
img = self.transform(img)
cfx = self.transform(cfx)
img = torch.stack([img, cfx], dim=0)
else:
img = self.transform(img)
sample["x"] = img.float()
return sample
def load_counterfactual_image(self, idx):
cf_dir = Path("/vol/biomedic3/mb121/causal-contrastive/padchest_cf_images_v0")
short_path = self.img_paths[idx][:-4]
filename = cf_dir / f"{short_path}_sc_cf.png"
img = io.imread(str(filename), as_gray=True) / 255.0
img = img / (img.max() + 1e-12)
img = ToTensor()(img)
assert img.max() <= 1
return img
class CheXpertDataset(Dataset):
def __init__(
self,
df: pd.DataFrame,
label_col: str,
transform: Callable,
cache: bool = False,
):
super().__init__()
print(f"Len dataset {len(df)}")
df.fillna(0, inplace=True)
self.labels = df[label_col].astype(int).values
self.img_paths = df.Path.values
self.genders = df.Sex.values
self.ages = df.Age.values
self.cache = cache
self.transform = transform
if cache:
self.cache = SharedCache(
size_limit_gib=24,
dataset_len=self.img_paths.shape[0],
data_dims=[1, 224, 224],
dtype=torch.float32,
)
else:
self.cache = None
def __len__(self):
return len(self.img_paths)
def read_image(self, idx):
img = io.imread(
Path("/vol/biodata/data/chest_xray") / self.img_paths[idx], as_gray=True
)
img = img / (img.max() + 1e-12)
img = CenterCrop(224)(Resize(224, antialias=True)(ToTensor()(img)))
return img
def __getitem__(self, idx: int) -> Dict:
if self.cache is not None:
img = self.cache.get_slot(idx)
if img is None:
img = self.read_image(idx)
self.cache.set_slot(idx, img, allow_overwrite=True)
else:
img = self.read_image(idx)
sample = {}
sample["age"] = self.ages[idx] / 100
sample["sex"] = 0 if self.genders[idx] == "Male" else 1
sample["y"] = self.labels[idx]
sample["shortpath"] = self.img_paths[idx]
sample["x"] = self.transform(img).float()
return sample
class RNSAPneumoniaDetectionDataset(Dataset):
def __init__(
self,
df: pd.DataFrame,
transform: Callable,
parents: Optional = None,
cache: bool = False,
use_counterfactuals: bool = False,
counterfactual_contrastive_pairs: bool = True,
) -> None:
"""
Torchvision dataset for loading RSNA dataset.
Args:
root: the data directory where the images can be found
dataframe: the csv file mapping patient id, metadata, file names and label.
transform: the transformation (i.e. preprocessing and / or augmentation)
to apply to the image after loading them.
This dataset returns a dictionary with the image data, label and metadata.
"""
super().__init__()
self.transform = transform
self.parents = parents
self.use_counterfactuals = use_counterfactuals
self.counterfactual_contrastive_pairs = counterfactual_contrastive_pairs
self.df = df
self.targets = self.df.label_rsna_pneumonia.values.astype(np.int64)
self.subject_ids = self.df.patientId.unique()
self.filenames = [
DATA_DIR_RSNA_PROCESSED_IMAGES / f"{subject_id}.png"
for subject_id in self.subject_ids
]
self.genders = self.df["Patient Gender"].values
self.ages = self.df["Patient Age"].values.astype(int)
if cache:
self.cache = SharedCache(
size_limit_gib=24,
dataset_len=len(self.filenames),
data_dims=[1, 224, 224],
dtype=torch.float32,
)
else:
self.cache = None
def read_image(self, idx):
img = io.imread(self.filenames[idx], as_gray=True)
img = img / (img.max() + 1e-12)
img = CenterCrop(224)(Resize(224, antialias=True)(ToTensor()(img)))
return img
def __getitem__(self, index: int):
img = self.read_image(index)
sample = {
"y": self.targets[index],
"gender": self.genders[index],
"pneumonia": self.targets[index],
"sex": 1 if self.genders[index] == "M" else 0,
"age": self.ages[index],
"scanner": np.nan,
}
if self.parents is not None:
sample["pa"] = torch.cat(
[
sample[c]
if isinstance(sample[c], torch.Tensor)
else torch.tensor([sample[c]])
for c in self.parents
]
).detach()
if self.use_counterfactuals:
if torch.rand(1).item() > 0.5:
cfx = img.clone()
else:
cfx = self.load_counterfactual_image(index)
if not self.counterfactual_contrastive_pairs:
img = cfx.clone()
img = self.transform(img)
cfx = self.transform(cfx)
img = torch.stack([img, cfx], dim=0).float()
else:
img = self.transform(img).float()
sample["x"] = img
return sample
def __len__(self) -> int:
return len(self.filenames)
def load_counterfactual_image(self, index):
raise NotImplementedError
class RSNAPneumoniaDataModule(BaseDataModuleClass):
def create_datasets(self):
"""
Pytorch Lightning DataModule defining train / val / test splits for the RSNA dataset.
"""
if not DATA_DIR_RSNA_PROCESSED_IMAGES.exists():
print(
f"Data dir: {DATA_DIR_RSNA_PROCESSED_IMAGES} does not exist."
+ " Have you updated default_paths.py?"
)
if not PATH_TO_PNEUMONIA_WITH_METADATA_CSV.exists():
print(
"""
The dataset can be found at
https://www.kaggle.com/c/rsna-pneumonia-detection-challenge
This dataset is originally a (relabelled) subset of the NIH dataset
https://www.kaggle.com/datasets/nih-chest-xrays/data from
which i took the metadata.
To get the full csv with all the metadata please run
data_handling/csv_generation_code/rsna_generate_full_csv.py
"""
)
df_with_all_labels = pd.read_csv(PATH_TO_PNEUMONIA_WITH_METADATA_CSV)
df_with_all_labels = df_with_all_labels.loc[
df_with_all_labels["View Position"] == "PA"
]
random_seed_for_splits = 33
indices_train_val, indices_test = train_test_split(
np.arange(len(df_with_all_labels)),
test_size=0.3,
random_state=random_seed_for_splits,
)
train_val_df = df_with_all_labels.iloc[indices_train_val]
test_df = df_with_all_labels.iloc[indices_test]
# Further split train and val
indices_train, indices_val = train_test_split(
np.arange(len(train_val_df)),
test_size=0.15,
random_state=random_seed_for_splits,
)
if self.config.data.prop_train < 1.0:
rng = np.random.default_rng(33)
indices_train = rng.choice(
indices_train,
size=int(self.config.data.prop_train * indices_train.shape[0]),
replace=False,
)
train_df = train_val_df.iloc[indices_train]
val_df = train_val_df.iloc[indices_val]
print(
f"N patients train {indices_train.shape[0]}, val {indices_val.shape[0]}, test {indices_test.shape[0]}" # noqa
)
self.dataset_train = RNSAPneumoniaDetectionDataset(
df=train_df,
transform=self.train_tsfm,
parents=self.parents,
cache=self.config.data.cache,
use_counterfactuals=self.config.data.use_counterfactuals,
counterfactual_contrastive_pairs=self.config.data.counterfactual_contrastive,
)
self.dataset_val = RNSAPneumoniaDetectionDataset(
df=val_df,
transform=self.val_tsfm,
parents=self.parents,
cache=self.config.data.cache,
)
self.dataset_test = RNSAPneumoniaDetectionDataset(
df=test_df,
transform=self.val_tsfm,
parents=self.parents,
cache=True,
)
print("#train: ", len(self.dataset_train))
print("#val: ", len(self.dataset_val))
print("#test: ", len(self.dataset_test))
@property
def num_classes(self):
return 2