Skip to content

Commit

Permalink
Map.get_colormap(): allow_random_colors -> fill_uncolored. Disp…
Browse files Browse the repository at this point in the history
…lay colorless regions
  • Loading branch information
AhmetNSimsek committed Feb 14, 2025
1 parent 9f5cbcf commit 10d8992
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions siibra/volumes/parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,15 +690,16 @@ def colorize(self, values: dict, **kwargs) -> _volume.Volume:
name=f"Custom colorization of {self}"
)

def get_colormap(self, region_specs: Iterable = None, *, allow_random_colors: bool = False):
def get_colormap(self, region_specs: Iterable = None, *, fill_uncolored: bool = False):
"""
Generate a matplotlib colormap from known rgb values of label indices.
Parameters
----------
region_specs: iterable(regions), optional
Optional parameter to only color the desired regions.
allow_random_colors: bool , optional
fill_uncolored: bool , optional
If a region has no preconfigured color, a color will be randomly (reproducible) created.
Returns
-------
Expand All @@ -711,7 +712,7 @@ def get_colormap(self, region_specs: Iterable = None, *, allow_random_colors: bo
"matplotlib not available. Please install matplotlib to create a matplotlib colormap."
)
raise e
if allow_random_colors:
if fill_uncolored:
seed = len(self.regions)
np.random.seed(seed)
logger.info(f"Random colors are allowed for regions without preconfgirued colors. Random seee: {seed}.")
Expand All @@ -724,6 +725,7 @@ def get_colormap(self, region_specs: Iterable = None, *, allow_random_colors: bo
else:
include_region_names = None

no_predefined_color = []
for regionname, indices in self._indices.items():
for index in indices:
if index.label is None:
Expand All @@ -735,16 +737,25 @@ def get_colormap(self, region_specs: Iterable = None, *, allow_random_colors: bo
region = self.get_region(index=index)
if region.rgb is not None:
colors[index.label] = region.rgb
elif allow_random_colors:
elif fill_uncolored:
random_clr = [np.random.randint(0, 255) for r in range(3)]
while random_clr in list(colors.values()):
random_clr = [np.random.randint(0, 255) for r in range(3)]
colors[index.label] = random_clr
else:
no_predefined_color.append(region.name)

if len(colors) == 0:
raise exceptions.NoPredifinedColormapException(
f"There is no predefined/preconfigured colormap for '{self}'."
"Set `allow_random_colors=True` to a colormap with random values"
"Set `fill_uncolored=True` to get a reproducible colormap."
)

if no_predefined_color:
logger.info(
f"No preconfigured color found for the follwing regions."

Check failure on line 756 in siibra/volumes/parcellationmap.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

follwing ==> following
"Use `fill_uncolored=True` to display with a non-background color.\n"
f"{no_predefined_color}"
)

palette = np.array(
Expand Down

0 comments on commit 10d8992

Please sign in to comment.