Skip to content

Colorbar Plottable - #681

Merged
swharden merged 8 commits into
masterfrom
colorbar
Dec 27, 2020
Merged

swharden merged 8 commits into
masterfrom
colorbar

Conversation

@swharden

@swharden swharden commented Dec 25, 2020 •

Copy link
Copy Markdown
Member

Previously colorbar rendering was achieved inside the heatmap module. This PR gives colorbar its own plot type, and AddColorbar() can be called on any plot. This addresses #678

The colorbar could benefit from continued refinement/refactoring (especially the tick system), but this is a strong start.

var hm = plt.AddHeatmap(intensities);

var cb = plt.AddColorbar(hm.Colormap);
cb.SetTicks(hm.ColorbarMin, hm.ColorbarMax);

image

This PR also makes some changes suggested by #680

@swharden

Copy link
Copy Markdown
Member Author

After separating the heatmap from the colorbar I was surprised how clunky it feels to ask the heatmap what the min/max limits are then load them into the colormap. It feels like the API could be improved still 🤔

@bclehmann

bclehmann commented Dec 26, 2020 •

Copy link
Copy Markdown
Member

how clunky it feels to ask the heatmap what the min/max limits are then load them into the colormap.

One option would be to grab the information from the Plot object, but that introduces a lot of coupling. I bring it up because that's similar to what the Legend renderable does, but the legend is pretty intrinsically tied to a Plot object whereas a heatmap is not.

You could give a heatmap a ColorBar plottable as one of its fields or allow it to create one via a public factory method. Then the heatmap could populate all the relevant state. The user would be free to create their own ColorBar on their own, but they would also have a convenient method to create a ColorBar from a heatmap. This method probably could be written pretty simply as well. Whether the heatmap holds the colorbar itself and is responsible for rendering it or a factory method returns the colorbar for the user to render is a design choice, if however the colorbar were to offer lots of configuration options it wouldn't be particularly elegant (in my opinion).

Note that if you go the factory method route there are a couple of snakes waiting should the heatmap be modified, I don't know if you want the user to take care of that or if you want to create something (such as a class inheriting from ColorBar) which can "sync up" with the parent heatmap. If you go that route you're going to have to make a choice about how to treat orphan ColorBars should their parent heatmap be disposed.

If you go with the observer pattern then that choice is delightfully known as "dead [objects] tell no tales", i.e. the colorbar will never change after its parent heatmap is disposed. However if you use another route, such as checking the parent heatmap has not changed before every render of the colorbar than that choice is simply to prevent the parent heatmap from being disposed by the garbage collector because there is a reference to the parent heatmap held by the colorbar. There is another option with this approach, if you create a weak reference (a reference that does not prohibit garbage collection) then you get a dangling reference, which would create a NullReferenceException when dereferenced. One could, in principle, detect this and instead choose any other behaviour, which is why I bring this up, this method is the most flexible. C# offers the System.WeakReference class so there's no need to mess about with pointers (or IntPtr). This class conveniently offers the IsAlive method to determine whether it's target has been garbage collected, so fancy deconstructors are not necessary.

@bclehmann

Copy link
Copy Markdown
Member

I implemented this with the observer pattern in #682.

Basically, the end result is you can just do this:

Colorbar cb = plt.AddColorbarFromHeatmap(hm);

If you want to disable syncing (i.e. create a colorbar from the heatmap and don't modify it on subsequent changes) you can do this:

Colorbar cb = plt.AddColorbarFromHeatmap(hm, synced: false);

@swharden

swharden commented Dec 26, 2020 •

Copy link
Copy Markdown
Member Author

Hey @Benny121221, thanks for your input! There is a lot of good discussion here.

Syncing

You gave a lot of thoughts about how to keep a colorbar and heamap sync'd. At the end of the day, I don't think I'm in favor of this though... It adds a lot of coupling and complexity to this library, all so the end-user doesn't have to write an extra line or two of code in the niche case that they want to change colormaps or tick labels after a heatmap is displayed.

You pointed out a lot of "snakes" above and gave some good ideas about how to overcome them, but altogether I think I'm leaning away from this type of syncing in general in an effort to keep the code minimal and well isolated.

However, observer pattern would be a good way to accomplish syncing, and the implementation in #682 is a good demonstration of this pattern! Interestingly, a strong argument could be made that the control should the other way around: A colorbar should be a type of Axis, and a heatmap should update its image any time the colorbar's colormap changes...

Although I don't think we should go down that road at this time, if we did want to pursue syncing I think we should go all-in and have an IHasColormap that plottables could implement so all coupling points to the interface (not to a concrete plot type). This would allow users to create new custom plot types that would work with colorbar.

Simpler API to Create a Heatmap and Colorbar

You could give a heatmap a ColorBar plottable as one of its fields or allow it to create one via a public factory method. Then the heatmap could populate all the relevant state.

Maybe we could collapse this into a single method? This could create both items at the same time and initialize the colorbar the appropriate state, and return both in case the user wants to fine-tune something 🤔

var (hm, cb) = plt.AddHeatmapAndColorbar(intensities);

Alternative Simpler / Explicit API

Colorbar arguments for initialization with min/max labels might make the creation simpler...

var hm = plt.AddHeatmap(intensities);
var cb = plt.AddColorbar(hm.Colormap, hm.MinLabel, hm.MaxLabel);

An overload in the plot module is possible (below) to mimic AddColorbarFromHeatmap implemented in #682, but I think I favor having the user explicitly define what they want to do (above)

var hm = plt.AddHeatmap(intensities);
var cb = plt.AddColorbar(hm);

@bclehmann

Copy link
Copy Markdown
Member

Of the options you listed my preference would be allow a user to create a colorbar with the ticks up front:

plt.AddColorbar​(​hm​.​Colormap​, ​hm​.​MinLabel​, hm.MaxLabel);

Or to create a colorbar from a heatmap:

plt.AddColorbar(hm);

On the topic of syncing, I mostly agree. While it is convenient, it's not a likely issue to come up, and if it does it's not difficult for a user to maintain the two objects with the same state. If they were so inclined they could even use the observer pattern with something like a decorator class.

@swharden

Copy link
Copy Markdown
Member Author

Or to create a colorbar from a heatmap:
plt.AddColorbar(hm);

@Benny121221 I created this method like you suggested and it really simplified the cookbook examples! I added a new cookbook section for Colorbar too.

There is certainly room for improvement/refinement for both Heatmap and Colorbar, but both seem usable as they are and the cookbook examples are looking much cleaner. Unless you have any other suggestions, I'll merge this and make a NuGet package later today then go back to working on #637

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 this pull request may close these issues.

2 participants