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

Provide color map for mask set with flag_values #1011

Closed
forman opened this issue Jun 7, 2024 · 2 comments · Fixed by #1012 or #1019
Closed

Provide color map for mask set with flag_values #1011

forman opened this issue Jun 7, 2024 · 2 comments · Fixed by #1012 or #1019
Assignees

Comments

@forman
Copy link
Member

forman commented Jun 7, 2024

Is your feature request related to a problem? Please describe.

For dataset variables that provide a flag_values attribute, it would be convenient to get a matching matplotlib color mapping. For some variables we also provide flag_colors that should be used if so.

Describe the solution you'd like

Add method get_cmap() to xcube.core.maskset.MaskSet.

Additional context

CCI Land Cover datasets, lccs_class variable.

@konstntokas
Copy link
Contributor

Describe the bug
MaskSet.get_cmap() method does not work, which has been added to xcube in #1012.

To Reproduce
In DeepESDL, use the CCI C3S land cover classification, create a mask and try to create a cmap. It creates a matplotlib.colors.ListedColormapinstance, and therefore the unit tests are working fine. If one wants to use the cmap, the following error is returned.
ValueError: data mapping points must start with x=0 and end with x=1

Additional context
The values of the colormap needs to be normalized. Furthermore, no color flag is given for 'no_data'. Here is a workable example, where we address the two issues.

def get_cmap(maskset):
    vals = maskset._flag_values
    extent = vals.max() - vals.min()
    vals_norm = (vals - vals.min()) / extent
    colors = ["#000000"] + maskset._flag_colors
    color_mapping = [(v, c) for v, c in zip(vals_norm, colors)]
    return matplotlib.colors.LinearSegmentedColormap.from_list(
        str(maskset._flag_var.name), color_mapping, N=len(vals)
    )

@konstntokas konstntokas reopened this Jun 19, 2024
@forman
Copy link
Member Author

forman commented Jun 19, 2024

PR underway...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants