-
Notifications
You must be signed in to change notification settings - Fork 382
/
mri_data.py
546 lines (471 loc) · 21.4 KB
/
mri_data.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
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import os
import pickle
import random
import xml.etree.ElementTree as etree
from pathlib import Path
from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union
from warnings import warn
import h5py
import numpy as np
import pandas as pd
import requests
import torch
import yaml
def et_query(
root: etree.Element,
qlist: Sequence[str],
namespace: str = "http://www.ismrm.org/ISMRMRD",
) -> str:
"""
ElementTree query function.
This can be used to query an xml document via ElementTree. It uses qlist
for nested queries.
Args:
root: Root of the xml to search through.
qlist: A list of strings for nested searches, e.g. ["Encoding",
"matrixSize"]
namespace: Optional; xml namespace to prepend query.
Returns:
The retrieved data as a string.
"""
s = "."
prefix = "ismrmrd_namespace"
ns = {prefix: namespace}
for el in qlist:
s = s + f"//{prefix}:{el}"
value = root.find(s, ns)
if value is None:
raise RuntimeError("Element not found")
return str(value.text)
def fetch_dir(
key: str, data_config_file: Union[str, Path, os.PathLike] = "fastmri_dirs.yaml"
) -> Path:
"""
Data directory fetcher.
This is a brute-force simple way to configure data directories for a
project. Simply overwrite the variables for `knee_path` and `brain_path`
and this function will retrieve the requested subsplit of the data for use.
Args:
key: key to retrieve path from data_config_file. Expected to be in
("knee_path", "brain_path", "log_path").
data_config_file: Optional; Default path config file to fetch path
from.
Returns:
The path to the specified directory.
"""
data_config_file = Path(data_config_file)
if not data_config_file.is_file():
default_config = {
"knee_path": "/path/to/knee",
"brain_path": "/path/to/brain",
"log_path": ".",
}
with open(data_config_file, "w") as f:
yaml.dump(default_config, f)
data_dir = default_config[key]
warn(
f"Path config at {data_config_file.resolve()} does not exist. "
"A template has been created for you. "
"Please enter the directory paths for your system to have defaults."
)
else:
with open(data_config_file, "r") as f:
data_dir = yaml.safe_load(f)[key]
return Path(data_dir)
class CombinedSliceDataset(torch.utils.data.Dataset):
"""
A container for combining slice datasets.
"""
def __init__(
self,
roots: Sequence[Path],
challenges: Sequence[str],
transforms: Optional[Sequence[Optional[Callable]]] = None,
sample_rates: Optional[Sequence[Optional[float]]] = None,
volume_sample_rates: Optional[Sequence[Optional[float]]] = None,
use_dataset_cache: bool = False,
dataset_cache_file: Union[str, Path, os.PathLike] = "dataset_cache.pkl",
num_cols: Optional[Tuple[int]] = None,
):
"""
Args:
roots: Paths to the datasets.
challenges: "singlecoil" or "multicoil" depending on which
challenge to use.
transforms: Optional; A sequence of callable objects that
preprocesses the raw data into appropriate form. The transform
function should take 'kspace', 'target', 'attributes',
'filename', and 'slice' as inputs. 'target' may be null for
test data.
sample_rates: Optional; A sequence of floats between 0 and 1.
This controls what fraction of the slices should be loaded.
When creating subsampled datasets either set sample_rates
(sample by slices) or volume_sample_rates (sample by volumes)
but not both.
volume_sample_rates: Optional; A sequence of floats between 0 and 1.
This controls what fraction of the volumes should be loaded.
When creating subsampled datasets either set sample_rates
(sample by slices) or volume_sample_rates (sample by volumes)
but not both.
use_dataset_cache: Whether to cache dataset metadata. This is very
useful for large datasets like the brain data.
dataset_cache_file: Optional; A file in which to cache dataset
information for faster load times.
num_cols: Optional; If provided, only slices with the desired
number of columns will be considered.
"""
if sample_rates is not None and volume_sample_rates is not None:
raise ValueError(
"either set sample_rates (sample by slices) or volume_sample_rates (sample by volumes) but not both"
)
if transforms is None:
transforms = [None] * len(roots)
if sample_rates is None:
sample_rates = [None] * len(roots)
if volume_sample_rates is None:
volume_sample_rates = [None] * len(roots)
if not (
len(roots)
== len(transforms)
== len(challenges)
== len(sample_rates)
== len(volume_sample_rates)
):
raise ValueError(
"Lengths of roots, transforms, challenges, sample_rates do not match"
)
self.datasets = []
self.examples: List[Tuple[Path, int, Dict[str, object]]] = []
for i in range(len(roots)):
self.datasets.append(
SliceDataset(
root=roots[i],
transform=transforms[i],
challenge=challenges[i],
sample_rate=sample_rates[i],
volume_sample_rate=volume_sample_rates[i],
use_dataset_cache=use_dataset_cache,
dataset_cache_file=dataset_cache_file,
num_cols=num_cols,
)
)
self.examples = self.examples + self.datasets[-1].examples
def __len__(self):
return sum(len(dataset) for dataset in self.datasets)
def __getitem__(self, i):
for dataset in self.datasets:
if i < len(dataset):
return dataset[i]
else:
i = i - len(dataset)
class SliceDataset(torch.utils.data.Dataset):
"""
A PyTorch Dataset that provides access to MR image slices.
"""
def __init__(
self,
root: Union[str, Path, os.PathLike],
challenge: str,
transform: Optional[Callable] = None,
use_dataset_cache: bool = False,
sample_rate: Optional[float] = None,
volume_sample_rate: Optional[float] = None,
dataset_cache_file: Union[str, Path, os.PathLike] = "dataset_cache.pkl",
num_cols: Optional[Tuple[int]] = None,
):
"""
Args:
root: Path to the dataset.
challenge: "singlecoil" or "multicoil" depending on which challenge
to use.
transform: Optional; A callable object that pre-processes the raw
data into appropriate form. The transform function should take
'kspace', 'target', 'attributes', 'filename', and 'slice' as
inputs. 'target' may be null for test data.
use_dataset_cache: Whether to cache dataset metadata. This is very
useful for large datasets like the brain data.
sample_rate: Optional; A float between 0 and 1. This controls what fraction
of the slices should be loaded. Defaults to 1 if no value is given.
When creating a sampled dataset either set sample_rate (sample by slices)
or volume_sample_rate (sample by volumes) but not both.
volume_sample_rate: Optional; A float between 0 and 1. This controls what fraction
of the volumes should be loaded. Defaults to 1 if no value is given.
When creating a sampled dataset either set sample_rate (sample by slices)
or volume_sample_rate (sample by volumes) but not both.
dataset_cache_file: Optional; A file in which to cache dataset
information for faster load times.
num_cols: Optional; If provided, only slices with the desired
number of columns will be considered.
"""
if challenge not in ("singlecoil", "multicoil"):
raise ValueError('challenge should be either "singlecoil" or "multicoil"')
if sample_rate is not None and volume_sample_rate is not None:
raise ValueError(
"either set sample_rate (sample by slices) or volume_sample_rate (sample by volumes) but not both"
)
self.dataset_cache_file = Path(dataset_cache_file)
self.transform = transform
self.recons_key = (
"reconstruction_esc" if challenge == "singlecoil" else "reconstruction_rss"
)
self.examples = []
# set default sampling mode if none given
if sample_rate is None:
sample_rate = 1.0
if volume_sample_rate is None:
volume_sample_rate = 1.0
# load dataset cache if we have and user wants to use it
if self.dataset_cache_file.exists() and use_dataset_cache:
with open(self.dataset_cache_file, "rb") as f:
dataset_cache = pickle.load(f)
else:
dataset_cache = {}
# check if our dataset is in the cache
# if there, use that metadata, if not, then regenerate the metadata
if dataset_cache.get(root) is None or not use_dataset_cache:
files = list(Path(root).iterdir())
for fname in sorted(files):
metadata, num_slices = self._retrieve_metadata(fname)
self.examples += [
(fname, slice_ind, metadata) for slice_ind in range(num_slices)
]
if dataset_cache.get(root) is None and use_dataset_cache:
dataset_cache[root] = self.examples
logging.info(f"Saving dataset cache to {self.dataset_cache_file}.")
with open(self.dataset_cache_file, "wb") as f:
pickle.dump(dataset_cache, f)
else:
logging.info(f"Using dataset cache from {self.dataset_cache_file}.")
self.examples = dataset_cache[root]
# subsample if desired
if sample_rate < 1.0: # sample by slice
random.shuffle(self.examples)
num_examples = round(len(self.examples) * sample_rate)
self.examples = self.examples[:num_examples]
elif volume_sample_rate < 1.0: # sample by volume
vol_names = sorted(list(set([f[0].stem for f in self.examples])))
random.shuffle(vol_names)
num_volumes = round(len(vol_names) * volume_sample_rate)
sampled_vols = vol_names[:num_volumes]
self.examples = [
example for example in self.examples if example[0].stem in sampled_vols
]
if num_cols:
self.examples = [
ex
for ex in self.examples
if ex[2]["encoding_size"][1] in num_cols # type: ignore
]
def _retrieve_metadata(self, fname):
with h5py.File(fname, "r") as hf:
et_root = etree.fromstring(hf["ismrmrd_header"][()])
enc = ["encoding", "encodedSpace", "matrixSize"]
enc_size = (
int(et_query(et_root, enc + ["x"])),
int(et_query(et_root, enc + ["y"])),
int(et_query(et_root, enc + ["z"])),
)
rec = ["encoding", "reconSpace", "matrixSize"]
recon_size = (
int(et_query(et_root, rec + ["x"])),
int(et_query(et_root, rec + ["y"])),
int(et_query(et_root, rec + ["z"])),
)
lims = ["encoding", "encodingLimits", "kspace_encoding_step_1"]
enc_limits_center = int(et_query(et_root, lims + ["center"]))
enc_limits_max = int(et_query(et_root, lims + ["maximum"])) + 1
padding_left = enc_size[1] // 2 - enc_limits_center
padding_right = padding_left + enc_limits_max
num_slices = hf["kspace"].shape[0]
metadata = {
"padding_left": padding_left,
"padding_right": padding_right,
"encoding_size": enc_size,
"recon_size": recon_size,
}
return metadata, num_slices
def __len__(self):
return len(self.examples)
def __getitem__(self, i: int):
fname, dataslice, metadata = self.examples[i]
with h5py.File(fname, "r") as hf:
kspace = hf["kspace"][dataslice]
mask = np.asarray(hf["mask"]) if "mask" in hf else None
target = hf[self.recons_key][dataslice] if self.recons_key in hf else None
attrs = dict(hf.attrs)
attrs.update(metadata)
if self.transform is None:
sample = (kspace, mask, target, attrs, fname.name, dataslice)
else:
sample = self.transform(kspace, mask, target, attrs, fname.name, dataslice)
return sample
class AnnotatedSliceDataset(SliceDataset):
"""
A PyTorch Dataset that provides access to MR image slices with annotation.
This is a subclass from SliceDataset that incorporates functionality of the fastMRI+ dataset.
It can be used to download the csv file from fastMRI+ based on the specified version using git hash.
It parses the csv and links it to samples in SliceDataset as annotated_examples.
Github: https://github.com/microsoft/fastmri-plus
Paper: https://arxiv.org/abs/2109.03812
"""
def __init__(
self,
root: Union[str, Path, os.PathLike],
challenge: str,
subsplit: str,
multiple_annotation_policy: str,
transform: Optional[Callable] = None,
use_dataset_cache: bool = False,
sample_rate: Optional[float] = None,
volume_sample_rate: Optional[float] = None,
dataset_cache_file: Union[str, Path, os.PathLike] = "dataset_cache.pkl",
num_cols: Optional[Tuple[int]] = None,
annotation_version: Optional[str] = None,
):
"""
Args:
root: Path to the dataset.
challenge: "singlecoil" or "multicoil" depending on which challenge
to use.
subsplit: 'knee' or 'brain' depending on which dataset to use.
multiple_annotation_policy: 'first', 'random' or 'all'.
If 'first', then only use the first annotation.
If 'random', then pick an annotation at random.
If 'all' then two or more copies of the same slice for each annotation
will be extended.
transform: Optional; A callable object that pre-processes the raw
data into appropriate form. The transform function should take
'kspace', 'target', 'attributes', 'filename', and 'slice' as
inputs. 'target' may be null for test data.
use_dataset_cache: Whether to cache dataset metadata. This is very
useful for large datasets like the brain data.
sample_rate: Optional; A float between 0 and 1. This controls what fraction
of the slices should be loaded. Defaults to 1 if no value is given.
When creating a sampled dataset either set sample_rate (sample by slices)
or volume_sample_rate (sample by volumes) but not both.
volume_sample_rate: Optional; A float between 0 and 1. This controls what fraction
of the volumes should be loaded. Defaults to 1 if no value is given.
When creating a sampled dataset either set sample_rate (sample by slices)
or volume_sample_rate (sample by volumes) but not both.
dataset_cache_file: Optional; A file in which to cache dataset
information for faster load times.
num_cols: Optional; If provided, only slices with the desired
number of columns will be considered.
annotation_version: Optional; If provided, a specific version of csv file will be downloaded based on its git hash.
Default value is None, then the latest version will be used.
"""
# subclass SliceDataset
super().__init__(
root,
challenge,
transform,
use_dataset_cache,
sample_rate,
volume_sample_rate,
dataset_cache_file,
num_cols,
)
self.annotated_examples = []
if subsplit not in ("knee", "brain"):
raise ValueError('subsplit should be either "knee" or "brain"')
if multiple_annotation_policy not in ("first", "random", "all"):
raise ValueError(
'multiple_annotation_policy should be "single", "random", or "all"'
)
# download csv file from github using git hash to find certain version
annotation_name = f"{subsplit}{annotation_version}.csv"
annotation_path = Path(os.getcwd(), ".annotation_cache", annotation_name)
if not annotation_path.is_file():
annotation_path = self.download_csv(
annotation_version, subsplit, annotation_path
)
annotations_csv = pd.read_csv(annotation_path)
for example in self.examples:
fname, slice_ind, metadata = example
# using filename and slice to find desired annotation
annotations_df = annotations_csv[
(annotations_csv["file"] == fname.stem)
& (annotations_csv["slice"] == slice_ind)
]
annotations_list = annotations_df.itertuples(index=True, name="Pandas")
# if annotation (filename or slice) not found, fill in empty values
if len(annotations_df) == 0:
annotation = self.get_annotation(True, None)
metadata["annotation"] = annotation
self.annotated_examples.append(
list([fname, slice_ind, metadata.copy()])
)
elif len(annotations_df) == 1:
rows = list(annotations_list)[0]
annotation = self.get_annotation(False, rows)
metadata["annotation"] = annotation
self.annotated_examples.append(
list([fname, slice_ind, metadata.copy()])
)
else:
# only use the first annotation
if multiple_annotation_policy == "first":
rows = list(annotations_list)[0]
annotation = self.get_annotation(False, rows)
metadata["annotation"] = annotation
self.annotated_examples.append(
list([fname, slice_ind, metadata.copy()])
)
# use an annotation at random
elif multiple_annotation_policy == "random":
random_number = torch.randint(len(annotations_df) - 1, (1,))
rows = list(annotations_list)[random_number]
annotation = self.get_annotation(False, rows)
metadata["annotation"] = annotation
self.annotated_examples.append(
list([fname, slice_ind, metadata.copy()])
)
# extend examples to have tow copies of the same slice, one for each annotation
elif multiple_annotation_policy == "all":
for rows in annotations_list:
annotation = self.get_annotation(False, rows)
metadata["annotation"] = annotation
self.annotated_examples.append(
list([fname, slice_ind, metadata.copy()])
)
def get_annotation(self, empty_value, row):
if empty_value is True:
annotation = {
"fname": "",
"slice": "",
"study_level": "",
"x": -1,
"y": -1,
"width": -1,
"height": -1,
"label": "",
}
else:
annotation = {
"fname": str(row.file),
"slice": int(row.slice),
"study_level": str(row.study_level),
"x": int(row.x),
"y": 320 - int(row.y) - int(row.height) - 1,
"width": int(row.width),
"height": int(row.height),
"label": str(row.label),
}
return annotation
def download_csv(self, version, subsplit, path):
# request file by git hash and mri type
url = f"https://raw.githubusercontent.com/microsoft/fastmri-plus/{version}/Annotations/{subsplit}.csv"
request = requests.get(url, timeout=10, stream=True)
# create temporary folders
Path(".annotation_cache").mkdir(parents=True, exist_ok=True)
# download csv from github and save it locally
with open(path, "wb") as fh:
for chunk in request.iter_content(1024 * 1024):
fh.write(chunk)
return path