Skip to content

Commit 928f03d

Browse files
committed
Add tests.
1 parent 706335b commit 928f03d

2 files changed

Lines changed: 170 additions & 0 deletions

File tree

tests/test_plotting.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import os
2+
import numpy as np
3+
import importlib.util
4+
from brainplotlib import brain_plot
5+
6+
7+
class TestPlotting:
8+
def test_icoorder5_masked(self, tmp_path):
9+
values = np.arange(9372), np.arange(9370)
10+
img = brain_plot(*values, vmax=18741, vmin=0, cmap=None)
11+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
12+
assert img.dtype == np.float64
13+
assert np.all(img <= 1)
14+
assert np.all(img >= 0)
15+
if importlib.util.find_spec('cv2'):
16+
import cv2
17+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder5_masked.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
18+
19+
def test_icoorder5_masked_random(self, tmp_path):
20+
rng = np.random.default_rng()
21+
values = rng.random((9372, )), rng.random((9370, ))
22+
img = brain_plot(*values, vmax=1, vmin=0, cmap=None)
23+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
24+
assert img.dtype == np.float64
25+
assert np.all(img <= 1)
26+
assert np.all(img >= 0)
27+
if importlib.util.find_spec('cv2'):
28+
import cv2
29+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder5_masked_random.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
30+
31+
def test_icoorder5_nonmasked(self, tmp_path):
32+
values = np.arange(10242), np.arange(10242)
33+
img = brain_plot(*values, vmax=20483, vmin=0, cmap=None)
34+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
35+
assert img.dtype == np.float64
36+
assert np.all(img <= 1)
37+
assert np.all(img >= 0)
38+
if importlib.util.find_spec('cv2'):
39+
import cv2
40+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder5_nonmasked.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
41+
42+
def test_icoorder5_nonmasked_random(self, tmp_path):
43+
rng = np.random.default_rng()
44+
values = rng.random((10242, )), rng.random((10242, ))
45+
img = brain_plot(*values, vmax=1, vmin=0, cmap=None)
46+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
47+
assert img.dtype == np.float64
48+
assert np.all(img <= 1)
49+
assert np.all(img >= 0)
50+
if importlib.util.find_spec('cv2'):
51+
import cv2
52+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder5_nonmasked_random.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
53+
54+
def test_icoorder3_masked(self, tmp_path):
55+
values = np.arange(588), np.arange(587)
56+
img = brain_plot(*values, vmax=1174, vmin=0, cmap=None)
57+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
58+
assert img.dtype == np.float64
59+
assert np.all(img <= 1)
60+
assert np.all(img >= 0)
61+
if importlib.util.find_spec('cv2'):
62+
import cv2
63+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder3_masked.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
64+
65+
def test_icoorder3_masked_random(self, tmp_path):
66+
rng = np.random.default_rng()
67+
values = rng.random((588, )), rng.random((587, ))
68+
img = brain_plot(*values, vmax=1, vmin=0, cmap=None)
69+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
70+
assert img.dtype == np.float64
71+
assert np.all(img <= 1)
72+
assert np.all(img >= 0)
73+
if importlib.util.find_spec('cv2'):
74+
import cv2
75+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder3_masked_random.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
76+
77+
def test_icoorder3_nonmasked(self, tmp_path):
78+
values = np.arange(642), np.arange(642)
79+
img = brain_plot(*values, vmax=1283, vmin=0, cmap=None)
80+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
81+
assert img.dtype == np.float64
82+
assert np.all(img <= 1)
83+
assert np.all(img >= 0)
84+
if importlib.util.find_spec('cv2'):
85+
import cv2
86+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder3_nonmasked.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
87+
88+
def test_icoorder3_nonmasked(self, tmp_path):
89+
rng = np.random.default_rng()
90+
values = rng.random((642, )), rng.random((642, ))
91+
img = brain_plot(*values, vmax=1, vmin=0, cmap=None)
92+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
93+
assert img.dtype == np.float64
94+
assert np.all(img <= 1)
95+
assert np.all(img >= 0)
96+
if importlib.util.find_spec('cv2'):
97+
import cv2
98+
cv2.imwrite(os.path.join(tmp_path, 'test_icoorder3_nonmasked_random.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
99+
100+
101+
class TestColormaps:
102+
def test_bwr_cmap(self, tmp_path):
103+
rng = np.random.default_rng()
104+
values = rng.random((588, )), rng.random((587, ))
105+
img = brain_plot(*values, vmax=1, vmin=0, cmap='bwr')
106+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
107+
assert img.dtype == np.float64
108+
assert np.all(img <= 1)
109+
assert np.all(img >= 0)
110+
if importlib.util.find_spec('cv2'):
111+
import cv2
112+
cv2.imwrite(os.path.join(tmp_path, 'test_bwr_cmap.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])
113+
114+
def test_jet_cmap(self, tmp_path):
115+
rng = np.random.default_rng()
116+
values = rng.random((588, )), rng.random((587, ))
117+
img = brain_plot(*values, vmax=1, vmin=0, cmap='jet')
118+
assert img.shape in [(1560, 1728, 4), (1560, 1728, 3)]
119+
assert img.dtype == np.float64
120+
assert np.all(img <= 1)
121+
assert np.all(img >= 0)
122+
if importlib.util.find_spec('cv2'):
123+
import cv2
124+
cv2.imwrite(os.path.join(tmp_path, 'test_jet_cmap.png'), np.round(img * 255).astype(np.uint8)[:, :, [2, 1, 0, 3]])

tests/test_unmask_upsample.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
from brainplotlib import prepare_data, unmask_and_upsample
3+
4+
5+
class TestUnmaskUpsample:
6+
def test_icoorder5_masked(self):
7+
lh = np.arange(9372)
8+
rh = np.arange(9370) + 9372
9+
values = unmask_and_upsample(lh, rh, 'fsaverage', 5, True)
10+
assert np.nanmin(values) == 0
11+
assert np.nanmax(values) == 18741
12+
assert np.any(np.isnan(values))
13+
14+
def test_icoorder5_nonmasked(self):
15+
lh = np.arange(10242)
16+
rh = np.arange(10242) + 10242
17+
values = unmask_and_upsample(lh, rh, 'fsaverage', 5, False)
18+
assert values.min() == 0
19+
assert values.max() == 20483
20+
assert np.all(np.isfinite(values))
21+
22+
23+
class TestPrepareData:
24+
def test_tuple_input(self):
25+
lh = np.arange(9372)
26+
rh = np.arange(9370) + 9372
27+
values = prepare_data((lh, rh))
28+
assert values.shape == (327684, )
29+
30+
def test_list_input(self):
31+
lh = np.arange(9372)
32+
rh = np.arange(9370) + 9372
33+
values = prepare_data([lh, rh])
34+
assert values.shape == (327684, )
35+
36+
def test_serial_input(self):
37+
lh = np.arange(9372)
38+
rh = np.arange(9370) + 9372
39+
values = prepare_data(lh, rh)
40+
assert values.shape == (327684, )
41+
42+
def test_concatenated_input(self):
43+
lh = np.arange(9372)
44+
rh = np.arange(9370) + 9372
45+
values = prepare_data(np.concatenate([lh, rh], axis=0))
46+
assert values.shape == (327684, )

0 commit comments

Comments
 (0)