|
| 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]]) |
0 commit comments