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

Some colormaps in the get_colormaps dictionary are not Colormap instances #1487

Closed
djhoese opened this issue Jun 19, 2018 · 5 comments · Fixed by #2066
Closed

Some colormaps in the get_colormaps dictionary are not Colormap instances #1487

djhoese opened this issue Jun 19, 2018 · 5 comments · Fixed by #2066
Assignees

Comments

@djhoese
Copy link
Member

djhoese commented Jun 19, 2018

If I recall correctly this was added by @larsoner. In vispy/color/colormap.py there is a _colormaps dictionary that can be accessed via the public get_colormaps(). However, as a user, I expect this dictionary to map colormap names to colormap instances, some of these values though are colormap classes or functions. For example, cubehelix -> CubeHelixColormap and single_hue -> _SingleHue. This is "worked around" by having the get_colormap function create these colormaps with optional kwargs passed to the class.

I'm not a huge fan of this so unless someone can argue for this behavior I would like to propose that we replace these non-instance colormaps with basic instances or remove them from the colormap dictionary returned from get_colormaps.

@larsoner
Copy link
Member

fine by me

@kmuehlbauer
Copy link
Contributor

@djhoese I can have a look early next week.

@djhoese
Copy link
Member Author

djhoese commented Jun 19, 2018

Here is a copy of the raw dictionary:

_colormaps = dict(
    # Some colormap presets
    autumn=Colormap([(1., 0., 0., 1.), (1., 1., 0., 1.)]),
    blues=Colormap([(1., 1., 1., 1.), (0., 0., 1., 1.)]),
    cool=Colormap([(0., 1., 1., 1.), (1., 0., 1., 1.)]),
    greens=Colormap([(1., 1., 1., 1.), (0., 1., 0., 1.)]),
    reds=Colormap([(1., 1., 1., 1.), (1., 0., 0., 1.)]),
    spring=Colormap([(1., 0., 1., 1.), (1., 1., 0., 1.)]),
    summer=Colormap([(0., .5, .4, 1.), (1., 1., .4, 1.)]),
    fire=_Fire(),
    grays=_Grays(),
    hot=_Hot(),
    ice=_Ice(),
    winter=_Winter(),
    light_blues=_SingleHue(),
    orange=_SingleHue(hue=35),
    viridis=Colormap(ColorArray(_viridis_data)),
    # Diverging presets
    coolwarm=Colormap(ColorArray(
        [
            (226, 0.59, 0.92), (222, 0.44, 0.99), (218, 0.26, 0.97),
            (30, 0.01, 0.87),
            (20, 0.3, 0.96), (15, 0.5, 0.95), (8, 0.66, 0.86)
        ],
        color_space="hsv"
    )),
    PuGr=_Diverging(145, 280, 0.85, 0.30),
    GrBu=_Diverging(255, 133, 0.75, 0.6),
    GrBu_d=_Diverging(255, 133, 0.75, 0.6, "dark"),
    RdBu=_Diverging(220, 20, 0.75, 0.5),

    # Configurable colormaps
    cubehelix=CubeHelixColormap,
    single_hue=_SingleHue,
    hsl=_HSL,
    husl=_HUSL,
    diverging=_Diverging,
    RdYeBuCy=_RedYellowBlueCyan,
)

And visuals like ImageVisual typically run get_colormap(cmap) when a user does my_img.cmap = 'cubehelix' which will automatically generate the instance of the colormap.

@EtienneCmb
Copy link
Contributor

I've a suggestion. If the colormap is not in the _colormaps dictionary and if matplotlib is installed, it might be interesting to build the colormap with Matplotlib? Something like :

if (cmap not in _colormaps) and has_mpl:
    from matplotlib import cm
    # Get the list of implemented colormaps in Matplotlib :
    full_mpl_cmaps = list(cm.datad.keys()) + list(cm.cmaps_listed.keys())
    if cmap not in full_mpl_cmaps:
        raise ValueError("%s colormap not in matplotlib" % cmap)
    
    sc = cm.ScalarMappable(cmap=cmap)
    x = np.arange(256)
    cmap_vec = np.array(sc.to_rgba(x, alpha=1.))
    return Colormap(cmap_vec)

@djhoese
Copy link
Member Author

djhoese commented Jun 21, 2018

@EtienneCmb Now that vispy supports texture-based colormaps this should be a lot easier. If you want to make a pull request for this I'd be ok with that. If mpl colormaps need more than 256 colors we can do that too (ex. viridis).

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

Successfully merging a pull request may close this issue.

4 participants