Repository navigation
Expand file tree
/
Copy pathtest_image.py
More file actions
172 lines (138 loc) · 5.95 KB
/
Copy pathtest_image.py
File metadata and controls
172 lines (138 loc) · 5.95 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
# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Iterable, Sequence
import numpy as np
import pytest
from nibabel.nifti1 import Nifti1Pair
from nibabel.spatialimages import SpatialImage
from brainiak.image import (mask_image, mask_images, MaskedMultiSubjectData,
multimask_images, SingleConditionSpec)
@pytest.fixture
def masked_multi_subject_data(masked_images):
return np.stack(masked_images, axis=-1)
class TestMaskedMultiSubjectData:
def test_from_masked_images(self, masked_images,
masked_multi_subject_data):
result = MaskedMultiSubjectData.from_masked_images(masked_images,
len(masked_images))
assert np.array_equal(np.moveaxis(result, 1, 0),
masked_multi_subject_data)
@pytest.fixture
def condition_spec() -> SingleConditionSpec:
return np.array([[[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0]]],
dtype=np.int8).view(SingleConditionSpec)
class TestUniqueLabelConditionSpec:
def test_extract_labels(self, condition_spec: SingleConditionSpec
) -> None:
assert np.array_equal(condition_spec.extract_labels(),
np.array([0, 1]))
@pytest.fixture
def spatial_image() -> SpatialImage:
return Nifti1Pair(np.array([[[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]]]).reshape(4, 4, 4, 1),
np.eye(4))
@pytest.fixture
def mask() -> np.ndarray:
return np.array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]], dtype=np.bool)
@pytest.fixture
def masked_data() -> np.ndarray:
return np.array([[1, 0, 0, 1, 0, 1, 1, 0]]).reshape(8, 1)
@pytest.fixture
def images(spatial_image: SpatialImage) -> Iterable[SpatialImage]:
images = [spatial_image]
image_data = spatial_image.get_data().copy()
image_data[1, 1, 1, 0] = 2
images.append(Nifti1Pair(image_data, np.eye(4)))
return images
@pytest.fixture
def masks(mask: np.ndarray) -> Sequence[np.ndarray]:
masks = [mask]
mask2 = mask.copy()
mask2[0, 0, 0] = 1
masks.append(mask2)
mask3 = mask.copy()
mask3[2, 2, 2] = 0
masks.append(mask3)
return masks
@pytest.fixture
def multimasked_images(masked_data) -> Iterable[Iterable[np.ndarray]]:
masked_data_2 = np.concatenate(([[2]], masked_data[1:, :]))
return [[masked_data, np.concatenate(([[0]], masked_data)),
masked_data[:-1, :]],
[masked_data_2, np.concatenate(([[0]], masked_data_2)),
masked_data_2[:-1, :]]]
@pytest.fixture
def masked_images(multimasked_images) -> Iterable[np.ndarray]:
return [multimasked_image[0] for multimasked_image in multimasked_images]
def test_mask_image(spatial_image: SpatialImage, mask: np.ndarray,
masked_data: np.ndarray) -> None:
result = mask_image(spatial_image, mask)
assert np.array_equal(result, masked_data)
def test_mask_image_with_type(spatial_image: SpatialImage, mask: np.ndarray,
masked_data: np.ndarray) -> None:
masked_data_type = np.float32
result = mask_image(spatial_image, mask, masked_data_type)
assert result.dtype == masked_data_type
assert np.allclose(result, masked_data)
def test_multimask_images(
images: Iterable[SpatialImage],
masks: Sequence[np.ndarray],
multimasked_images: Iterable[Iterable[np.ndarray]]
) -> None:
result = multimask_images(images, masks)
for result_images, expected_images in zip(result,
multimasked_images):
for result_image, expected_image in zip(result_images,
expected_images):
assert np.array_equal(result_image, expected_image)
def test_mask_images(
images: Iterable[SpatialImage],
mask: np.ndarray,
masked_images: Iterable[np.ndarray]
) -> None:
result = mask_images(images, mask)
for result_image, expected_image in zip(result, masked_images):
assert np.array_equal(result_image, expected_image)