Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow random cmap creation for map.get_color_map #641

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
13 changes: 12 additions & 1 deletion siibra/volumes/parcellationmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,14 +690,15 @@
name=f"Custom colorization of {self}"
)

def get_colormap(self, region_specs: Iterable = None):
def get_colormap(self, region_specs: Iterable = None, *, allow_random_colors: 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

Returns
-------
Expand All @@ -710,6 +711,10 @@
"matplotlib not available. Please install matplotlib to create a matplotlib colormap."
)
raise e
if allow_random_colors:
seed = len(self.regions)
np.random.seed(seed)
logger.info(f"Random colors are allowed for regions without preconfgirued colors. Random seee: {seed}.")

Check warning on line 717 in siibra/volumes/parcellationmap.py

View check run for this annotation

Codecov / codecov/patch

siibra/volumes/parcellationmap.py#L714-L717

Added lines #L714 - L717 were not covered by tests

colors = {}
if region_specs is not None:
Expand All @@ -730,6 +735,12 @@
region = self.get_region(index=index)
if region.rgb is not None:
colors[index.label] = region.rgb
elif allow_random_colors:
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

Check warning on line 742 in siibra/volumes/parcellationmap.py

View check run for this annotation

Codecov / codecov/patch

siibra/volumes/parcellationmap.py#L738-L742

Added lines #L738 - L742 were not covered by tests

if len(colors) == 0:
raise exceptions.NoPredifinedColormapException(
f"There is no predefined/preconfigured colormap for '{self}'."
Expand Down
Loading