WorldWideTelescope / wwt-web-client

The WorldWide Telescope web client lets you explore the universe in your browser.
https://worldwidetelescope.org/webclient/
MIT License
105 stars 35 forks source link

Add support for custom colormaps and color normalization #238

Closed astrofrog closed 5 years ago

astrofrog commented 5 years ago

In a similar vein to #237, this adds support for doing color-mapping between values and colormaps straight in the SDK, with a pre-normalization of the form:

value = (value - value_min) / (value_max - value_min)

applied beforehand. In many cases, astronomers don't have tables with point sizes already in the table - instead they will have some value, e.g. mass or temperature and will want to map these to actual colors. Rather than have to re-generate the table many times, the attributes here allow normalization to be turned on in the SDK, and to control the min/max, as well as the colormap used.

This will allow us to then implement a much more efficient approach for WorldWideTelescope/pywwt#138 in PyWWT - at the moment, we constantly re-generate data tables and send them from Python to JS - with this, we'll be able to just modify the min/max and the colormap dynamically.

Example usage in the JS SDK:

csv = 'a,b,c\r\n266,-29,1\r\n267,-29,3\r\n267,-30,5\r\n266,-30,10\r\n';
layer = wwtlib.LayerManager.createSpreadsheetLayer('Sky', 'My Layer', csv);
layer.referenceFrame = 'Sky';
layer.set_lngColumn(0);
layer.set_raUnits(1);
layer.set_latColumn(1);
layer.set_colorMapColumn(2);
layer.set_colorMapperName('viridis');
layer.set_dynamicColor(true);
layer.set_normalizeColorMap(true);
layer.set_normalizeColorMapMax(10);
layer.set_scaleFactor(100);
wwt.gotoRaDecZoom(266.5, -29.5, 10, true);

Screenshot (6)

A few notes:

pkgw commented 5 years ago

A few comments:

astrojonathan commented 5 years ago

I am going to take a look at this. In general we should try to not break old code, but if we handcuff our self too much then we can never move forward. We should also port many of these changes and fixes to the Windows Release as well.

After I review it we can have a conference call and figure out a way to deal with this class of fixes/improvements.

astrofrog commented 5 years ago

@pkgw - thanks for the feedback! I renamed DataColorMap to ColorMapper following your good suggestion. I'll wait to make colorMap public until @astrojonathan has taken a look in case there is a good reason it isn't public.

@astrojonathan - good idea about the conference call - I agree it would be good to figure out a way to move forward with these kinds of changes.

astrofrog commented 5 years ago

I've managed to successfully get this to work and result in tour files being created that are compatible with the Windows client:

Screenshot (5)

At the moment, this does not allow completely custom colormaps, because these can't yet easily be serialized. Instead I've implemented a few common colormaps from Matplotlib. I've also included Python code (in a comment) to add more colormaps if we are ok with this approach.

astrofrog commented 5 years ago

@pkgw @astrojonathan - this is now ready for review/merging (we can always add more colormaps, e.g. diverging ones, in a future PR)

astrofrog commented 5 years ago

@pkgw - I've renamed the file and now validate the colormap name (good catch!). Due to time zone weirdness on my PC, the commit appears above your comment but I did push a new commit (direct link: https://github.com/WorldWideTelescope/wwt-web-client/pull/238/commits/fba0bf4627d6fd50d3da1320c3e2d6aa5ca1c55a)