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

Allow users to register custom matplotlib colormaps #38587

Conversation

rosswhitfield
Copy link
Member

Description of work

This apparently work previously but stopped working at some point. The problem is that the list of available colormaps is initialized once when ColorbarWidget is first imported and we have users that like loaded register new colormaps in their scripts. This change makes it work again.

Summary of work

Fixes 8690: [Defect] Add MDF colormap to workbench and slice viewer

Further detail of work

To test:

from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap

plt.colormaps.register(cmap=ListedColormap([[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1,1,1]]), name="CUSTOM_MAP")

S  =range(0,100);
ERR=range(0,100);
ws=CreateMDHistoWorkspace(Dimensionality=2,Extents='-10,10,-10,10',SignalInput=S,ErrorInput=ERR,\
                           NumberOfBins='10,10',Names='Dim1,Dim2',Units='MomentumTransfer,EnergyTransfer')

then open ws with Sliceviewer and check if CUSTOM_MAP is now one of the available Colormaps.

This will throw an error if you select the Reverse option unless a map named CUSTOM_MAP_r is also added.


Reviewer

Please comment on the points listed below (full description).
Your comments will be used as part of the gatekeeper process, so please comment clearly on what you have checked during your review. If changes are made to the PR during the review process then your final comment will be the most important for gatekeepers. In this comment you should make it clear why any earlier review is still valid, or confirm that all requested changes have been addressed.

Code Review

  • Is the code of an acceptable quality?
  • Does the code conform to the coding standards?
  • Are the unit tests small and test the class in isolation?
  • If there is GUI work does it follow the GUI standards?
  • If there are changes in the release notes then do they describe the changes appropriately?
  • Do the release notes conform to the release notes guide?

Functional Tests

  • Do changes function as described? Add comments below that describe the tests performed?
  • Do the changes handle unexpected situations, e.g. bad input?
  • Has the relevant (user and developer) documentation been added/updated?

Does everything look good? Mark the review as Approve. A member of @mantidproject/gatekeepers will take care of it.

Gatekeeper

If you need to request changes to a PR then please add a comment and set the review status to "Request changes". This will stop the PR from showing up in the list for other gatekeepers.

@rosswhitfield rosswhitfield marked this pull request as ready for review January 10, 2025 02:06
@rosswhitfield rosswhitfield added this to the Release 6.12 milestone Jan 10, 2025
@sf1919
Copy link
Contributor

sf1919 commented Jan 10, 2025

Would it be possible to provide a bit more information about the work? Unfortunately unless we work at ORNL we can't access the linked issue. As this is functionality that is not ORNL specific there is wider interest.

@zjmorgan
Copy link
Contributor

Would it be possible to provide a bit more information about the work? Unfortunately unless we work at ORNL we can't access the linked issue. As this is functionality that is not ORNL specific there is wider interest.

User's previously could modify the listed colormaps in Matplotlib by overwriting one of the default maps using their own custom map file of RGB values. Matplotlib now does not let the maps to be overwritten, so this doesn't work. This issue makes it possible to define a separate custom map that can be read into sliceviewer.

@zjmorgan
Copy link
Contributor

image

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from matplotlib import cm

from mantid.simpleapi import *

# Load map file
Cmap_Name = 'MDF' # Colormap name
Loaded_Cmap = np.loadtxt('/HFIR/HB2C/shared/WANDscripts/colormaps/MDF.map')/255

# Register the loaded Colormap
Listed_CustomCmap = ListedColormap(Loaded_Cmap, name=Cmap_Name)

# Create and register the reverse colormap
Listed_CustomCmap_r = ListedColormap(Loaded_Cmap[::-1,:], name=(Cmap_Name + '_r') )

cm.register_cmap(name=Cmap_Name, cmap=Listed_CustomCmap, override_builtin=True)
cm.register_cmap(name=(Cmap_Name + '_r'), cmap= Listed_CustomCmap_r, override_builtin=True)   

ws = CreateMDWorkspace(Dimensions='3', 
                       EventType='MDEvent', 
                       Extents='-10,10,-5,5,-1,1',
                       Names='Q_lab_x,Q_lab_y,Q_lab_z', 
                       Units='1\A,1\A,1\A')
FakeMDEventData(ws, UniformParams="1000000")

works well. Thank you for this!

@zjmorgan zjmorgan self-requested a review January 10, 2025 11:58
Copy link
Contributor

@zjmorgan zjmorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing:

If you set the new map to default, close workbench and re-open workbench, you cannot open sliceviewer if the map is not reloaded. Can we just default back to viridis so at least the user can reopen sliceviewer?
image

image

@SilkeSchomann SilkeSchomann self-assigned this Jan 13, 2025
@SilkeSchomann SilkeSchomann merged commit e9176f3 into mantidproject:main Jan 13, 2025
10 checks passed
@rosswhitfield rosswhitfield deleted the sliceviewer_allow_custom_colormaps branch January 14, 2025 00:18
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.

4 participants