Skip to content
Snippets Groups Projects
Unverified Commit c298bffd authored by Ahmet Nihat Simsek's avatar Ahmet Nihat Simsek Committed by GitHub
Browse files

Merge pull request #641 from FZJ-INM1-BDA/enh_allow_random_cmap

Allow random cmap creation for `map.get_color_map`
parents 8e273685 f88a7d5e
No related branches found
No related tags found
No related merge requests found
Pipeline #58823 failed
...@@ -690,7 +690,7 @@ class Map(concept.AtlasConcept, configuration_folder="maps"): ...@@ -690,7 +690,7 @@ class Map(concept.AtlasConcept, configuration_folder="maps"):
name=f"Custom colorization of {self}" 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. Generate a matplotlib colormap from known rgb values of label indices.
...@@ -698,6 +698,7 @@ class Map(concept.AtlasConcept, configuration_folder="maps"): ...@@ -698,6 +698,7 @@ class Map(concept.AtlasConcept, configuration_folder="maps"):
---------- ----------
region_specs: iterable(regions), optional region_specs: iterable(regions), optional
Optional parameter to only color the desired regions. Optional parameter to only color the desired regions.
allow_random_colors: bool , optional
Returns Returns
------- -------
...@@ -710,6 +711,10 @@ class Map(concept.AtlasConcept, configuration_folder="maps"): ...@@ -710,6 +711,10 @@ class Map(concept.AtlasConcept, configuration_folder="maps"):
"matplotlib not available. Please install matplotlib to create a matplotlib colormap." "matplotlib not available. Please install matplotlib to create a matplotlib colormap."
) )
raise e 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}.")
colors = {} colors = {}
if region_specs is not None: if region_specs is not None:
...@@ -730,6 +735,12 @@ class Map(concept.AtlasConcept, configuration_folder="maps"): ...@@ -730,6 +735,12 @@ class Map(concept.AtlasConcept, configuration_folder="maps"):
region = self.get_region(index=index) region = self.get_region(index=index)
if region.rgb is not None: if region.rgb is not None:
colors[index.label] = region.rgb 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
if len(colors) == 0: if len(colors) == 0:
raise exceptions.NoPredifinedColormapException( raise exceptions.NoPredifinedColormapException(
f"There is no predefined/preconfigured colormap for '{self}'." f"There is no predefined/preconfigured colormap for '{self}'."
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment