forked from facebookresearch/fastMRI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data.py
138 lines (108 loc) · 4.81 KB
/
test_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
"""
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.
"""
from fastmri.data.mri_data import (
SliceDataset,
CombinedSliceDataset,
AnnotatedSliceDataset,
)
def test_slice_datasets(fastmri_mock_dataset, monkeypatch):
knee_path, brain_path, metadata = fastmri_mock_dataset
def retrieve_metadata_mock(a, fname):
return metadata[str(fname)]
monkeypatch.setattr(SliceDataset, "_retrieve_metadata", retrieve_metadata_mock)
for challenge in ("multicoil", "singlecoil"):
for split in ("train", "val", "test", "challenge"):
dataset = SliceDataset(
knee_path / f"{challenge}_{split}", transform=None, challenge=challenge
)
assert len(dataset) > 0
assert dataset[0] is not None
assert dataset[-1] is not None
for challenge in ("multicoil",):
for split in ("train", "val", "test", "challenge"):
dataset = SliceDataset(
brain_path / f"{challenge}_{split}", transform=None, challenge=challenge
)
assert len(dataset) > 0
assert dataset[0] is not None
assert dataset[-1] is not None
def test_combined_slice_dataset(fastmri_mock_dataset, monkeypatch):
knee_path, brain_path, metadata = fastmri_mock_dataset
def retrieve_metadata_mock(a, fname):
return metadata[str(fname)]
monkeypatch.setattr(SliceDataset, "_retrieve_metadata", retrieve_metadata_mock)
roots = [knee_path / "multicoil_train", knee_path / "multicoil_val"]
challenges = ["multicoil", "multicoil"]
transforms = [None, None]
dataset1 = SliceDataset(
root=roots[0], challenge=challenges[0], transform=transforms[0]
)
dataset2 = SliceDataset(
root=roots[1], challenge=challenges[1], transform=transforms[1]
)
comb_dataset = CombinedSliceDataset(
roots=roots, challenges=challenges, transforms=transforms
)
assert len(comb_dataset) == len(dataset1) + len(dataset2)
assert comb_dataset[0] is not None
assert comb_dataset[-1] is not None
roots = [brain_path / "multicoil_train", brain_path / "multicoil_val"]
challenges = ["multicoil", "multicoil"]
transforms = [None, None]
dataset1 = SliceDataset(
root=roots[0], challenge=challenges[0], transform=transforms[0]
)
dataset2 = SliceDataset(
root=roots[1], challenge=challenges[1], transform=transforms[1]
)
comb_dataset = CombinedSliceDataset(
roots=roots, challenges=challenges, transforms=transforms
)
assert len(comb_dataset) == len(dataset1) + len(dataset2)
assert comb_dataset[0] is not None
assert comb_dataset[-1] is not None
def test_annotated_slice_dataset(
fastmri_mock_dataset, fastmri_mock_annotation, monkeypatch
):
knee_path, brain_path, metadata = fastmri_mock_dataset
annotation_knee_csv, annotation_brain_csv = fastmri_mock_annotation
def download_csv_mock(a, version, subsplit, path):
if subsplit == "knee":
return annotation_knee_csv
else:
return annotation_brain_csv
def retrieve_metadata_mock(a, fname):
return metadata[str(fname)]
monkeypatch.setattr(AnnotatedSliceDataset, "download_csv", download_csv_mock)
monkeypatch.setattr(SliceDataset, "_retrieve_metadata", retrieve_metadata_mock)
for challenge in ("multicoil", "singlecoil"):
for split in ("train", "val", "test", "challenge"):
for multiple_annotation_policy in ("first", "random", "all"):
dataset = AnnotatedSliceDataset(
knee_path / f"{challenge}_{split}",
challenge=challenge,
subsplit="knee",
multiple_annotation_policy=multiple_annotation_policy,
)
assert len(dataset) > 0
assert dataset[0] is not None
assert dataset[-1] is not None
assert dataset[0][3]["annotation"] is not None
assert dataset[-1][3]["annotation"] is not None
for challenge in ("multicoil",):
for split in ("train", "val", "test", "challenge"):
for multiple_annotation_policy in ("first", "random", "all"):
dataset = AnnotatedSliceDataset(
brain_path / f"{challenge}_{split}",
challenge=challenge,
subsplit="brain",
multiple_annotation_policy=multiple_annotation_policy,
)
assert len(dataset) > 0
assert dataset[0] is not None
assert dataset[-1] is not None
assert dataset[0][3]["annotation"] is not None
assert dataset[-1][3]["annotation"] is not None