Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions brainspace/plotting/colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,31 @@ def _interp_lut(stops, n=256):
])
BuGyRd = _interp_lut(_BuGyRd_stops, n=256)

def _build_cat35():
"""35-entry categorical palette (black + 34 distinguishable colors).

Built deterministically from matplotlib's ``tab20`` + ``tab20b`` (BSD
license) so we don't ship colors copied from a non-BSD-compatible
source. The 35-entry size is convenient for parcellations like
Desikan-Killiany (34 cortical labels + 1 unknown/medial-wall slot).
"""
import matplotlib as _mpl
pick = _mpl.colormaps['tab20'](np.linspace(0, 1, 20))[:, :3]
extra = _mpl.colormaps['tab20b'](np.linspace(0, 1, 20))[:, :3][:14]
rgb = np.vstack([np.zeros((1, 3)), pick, extra]) # leading black slot
rgba = np.empty((rgb.shape[0], 4), dtype=np.uint8)
rgba[:, :3] = (rgb * 255).round().astype(np.uint8)
rgba[:, 3] = 255
return rgba


cat35_colors = _build_cat35()


colormaps = {
'yeo7': yeo7_colors,
'eco_kos': eco_kos_colors,
'spec_5': spec_5_colors,
'BuGyRd': BuGyRd,
'cat35': cat35_colors,
}
14 changes: 13 additions & 1 deletion brainspace/tests/test_colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@


def test_registered_names():
for name in ('yeo7', 'eco_kos', 'spec_5', 'BuGyRd'):
for name in ('yeo7', 'eco_kos', 'spec_5', 'BuGyRd', 'cat35'):
assert name in colormaps
cm = colormaps[name]
assert cm.dtype == np.uint8
assert cm.shape[1] == 4


def test_cat35_shape_and_first_slot():
cm = colormaps['cat35']
assert cm.shape == (35, 4)
# First slot reserved for unknown/medial-wall: pure black, opaque.
np.testing.assert_array_equal(cm[0], [0, 0, 0, 255])
# All entries opaque.
assert np.all(cm[:, 3] == 255)
# All 35 colors should be distinct.
unique_rows = {tuple(row) for row in cm}
assert len(unique_rows) == 35


def test_BuGyRd_is_256_smooth_lut():
assert BuGyRd.shape == (256, 4)
assert np.all(BuGyRd[:, 3] == 255)
Expand Down
Loading