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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies = [

[project.optional-dependencies]
csv = ["pandas>=1"]
plot = ["distinctipy>=1.3.4", "matplotlib>=3.4"]
plot = ["colorcet", "matplotlib>=3.4"]
video = ["ffmpeg-python>=0.2.0"]
sklearn = [
"scikit-learn>=1.6.1",
Expand Down
4 changes: 2 additions & 2 deletions src/torchio/external/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def get_pandas() -> ModuleType:
return _check_and_import(module='pandas', extra='csv')


def get_distinctipy() -> ModuleType:
return _check_and_import(module='distinctipy', extra='plot')
def get_colorcet() -> ModuleType:
return _check_and_import(module='colorcet', extra='plot')


def get_ffmpeg() -> ModuleType:
Expand Down
10 changes: 7 additions & 3 deletions src/torchio/visualization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import warnings
from itertools import cycle
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
Expand Down Expand Up @@ -47,6 +48,7 @@ def rotate(image: np.ndarray, *, radiological: bool = True, n: int = -1) -> np.n

def _create_categorical_colormap(
data: torch.Tensor,
cmap_name: str = 'glasbey_category10',
) -> tuple[ListedColormap, BoundaryNorm]:
num_classes = int(data.max())
mpl, _ = import_mpl_plt()
Expand All @@ -56,10 +58,12 @@ def _create_categorical_colormap(
(1, 1, 1), # white for class 1
]
if num_classes > 1:
from .external.imports import get_distinctipy
from .external.imports import get_colorcet

distinctipy = get_distinctipy()
distinct_colors = distinctipy.get_colors(num_classes - 1, rng=0)
colorcet = get_colorcet()
cmap = getattr(colorcet.cm, cmap_name)
color_cycle = cycle(cmap.colors)
distinct_colors = [next(color_cycle) for _ in range(num_classes - 1)]
colors.extend(distinct_colors)
boundaries = np.arange(-0.5, num_classes + 1.5, 1)
colormap = mpl.colors.ListedColormap(colors)
Expand Down
Loading