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

new color map code, based on a BSD-3 licensed module #2335

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install wheel setuptools
python -m pip install numpy scipy matplotlib docutils "pytest<6" sphinx unittest-xml-reporting tinycc lxml h5py sphinx pyparsing html5lib reportlab pybind11 appdirs six numba mako ipython qtconsole xhtml2pdf unittest-xml-reporting pylint qt5reactor periodictable PyQt5 html2text importlib_resources
python -m pip install numpy scipy matplotlib docutils "pytest<6" sphinx unittest-xml-reporting tinycc lxml h5py sphinx pyparsing html5lib reportlab pybind11 appdirs six numba mako ipython qtconsole xhtml2pdf unittest-xml-reporting pylint qt5reactor periodictable PyQt5 html2text importlib_resources superqt

- name: Install pyopencl
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/installers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
python -m pip install tinycc h5py sphinx pyparsing html5lib reportlab==3.6.6 pybind11 appdirs
python -m pip install six numba mako ipython qtconsole xhtml2pdf unittest-xml-reporting pylint
python -m pip install qt5reactor periodictable uncertainties dominate importlib_resources
python -m pip install html2text
python -m pip install html2text superqt


- name: Install PyQt (Windows + Linux)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
python -m pip install tinycc h5py sphinx pyparsing html5lib reportlab==3.6.6 pybind11 appdirs
python -m pip install six numba mako ipython qtconsole xhtml2pdf unittest-xml-reporting pylint
python -m pip install qt5reactor periodictable uncertainties dominate importlib_resources
python -m pip install html2text
python -m pip install html2text superqt


- name: Install PyQt (Windows + Linux)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install wheel setuptools
python -m pip install numpy scipy matplotlib docutils pytest pytest_qt pytest-mock sphinx unittest-xml-reporting tinycc lxml h5py sphinx pyparsing html5lib reportlab pybind11 appdirs six numba mako ipython qtconsole xhtml2pdf unittest-xml-reporting pylint qt5reactor periodictable PyQt5 html2text importlib_resources uncertainties dominate html2text
python -m pip install numpy scipy matplotlib docutils pytest pytest_qt pytest-mock sphinx unittest-xml-reporting tinycc lxml h5py sphinx pyparsing html5lib reportlab pybind11 appdirs six numba mako ipython qtconsole xhtml2pdf unittest-xml-reporting pylint qt5reactor periodictable PyQt5 html2text importlib_resources uncertainties dominate html2text superqt

- name: Install pyopencl (Linux + macOS)
if: ${{ matrix.os != 'windows-latest' }}
Expand Down
2 changes: 2 additions & 0 deletions build_tools/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ importlib-resources
bumps
html2text
jsonschema
superqt

60 changes: 41 additions & 19 deletions src/sas/qtgui/Plotting/ColorMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
Allows users to change the range of the current graph
"""
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets

import matplotlib as mpl
import numpy
from typing import Union, Tuple

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from sas.qtgui.Plotting.PlotterData import Data2D
from sas.qtgui.Utilities.GuiUtils import formatNumber, DoubleValidator
from .rangeSlider import RangeSlider
from superqt import QDoubleRangeSlider

DEFAULT_MAP = 'jet'

Expand All @@ -31,13 +31,23 @@ def __init__(self, parent=None, cmap=None, vmin=0.0, vmax=100.0, data=None):

self.data = data
self._cmap_orig = self._cmap = cmap if cmap is not None else DEFAULT_MAP
self.all_maps = [m for m in mpl.cm.datad]
self.maps = sorted(m for m in self.all_maps if not m.endswith("_r"))
self.rmaps = sorted(set(self.all_maps) - set(self.maps))

self.maps = [m for m in mpl.cm.datad]
self.rmaps = [m + '_r' for m in self.maps]
self.all_maps = self.maps + self.rmaps

# see if data2d has preexisting info on vmin/vmax
if hasattr(self.data, 'vmin'):
vmin = self.data.vmin
if hasattr(self.data, 'vmax'):
vmax = self.data.vmax
self.vmin = self.vmin_orig = vmin
self.vmax = self.vmax_orig = vmax

# save instance-tied values
self.data.vmin = vmin
self.data.vmax = vmax

# Initialize detector labels
self.initDetectorData()

Expand Down Expand Up @@ -66,7 +76,6 @@ def __init__(self, parent=None, cmap=None, vmin=0.0, vmax=100.0, data=None):

# Handle combobox changes
self.cbColorMap.currentIndexChanged.connect(self.onMapIndexChange)

# Handle checkbox changes
self.chkReverse.stateChanged.connect(self.onColorMapReversed)

Expand Down Expand Up @@ -106,8 +115,8 @@ def onReset(self):
self.initMapCombobox()
self.slider.setMinimum(self.vmin)
self.slider.setMaximum(self.vmax)
self.slider.setLowValue(self.vmin)
self.slider.setHighValue(self.vmax)
self.slider.setRange(self.vmin, self.vmax)
self.slider.setSliderPosition([self.vmin, self.vmax])
# Redraw the widget
self.redrawColorBar()
self.canvas.draw()
Expand Down Expand Up @@ -154,27 +163,35 @@ def initRangeSlider(self):
"""
Create and display the double slider for data range mapping.
"""
self.slider = RangeSlider()
self.slider.setMinimum(self.vmin)
self.slider.setMaximum(self.vmax)
self.slider.setLowValue(self.vmin)
self.slider.setHighValue(self.vmax)
self.slider.setOrientation(QtCore.Qt.Horizontal)
self.slider = QDoubleRangeSlider(QtCore.Qt.Horizontal)

# see if data2d has preexisting range info
self.slider.setValue([self.vmin, self.vmax])
self.slider.setRange(self.vmin, self.vmax)

self.slider.setTickPosition(QtWidgets.QSlider.TicksAbove)
self.slider.setSliderPosition([self.vmin, self.vmax])

self.slider_label = QtWidgets.QLabel()
self.slider_label.setText("Drag the sliders to adjust color range.")

def set_vmin(value):
self.vmin = value
self.txtMinAmplitude.setText(str(value))
self.txtMinAmplitude.setText(formatNumber(value))
self.updateMap()
def set_vmax(value):
self.vmax = value
self.txtMaxAmplitude.setText(str(value))
self.txtMaxAmplitude.setText(formatNumber(value))
self.updateMap()

self.slider.lowValueChanged.connect(set_vmin)
self.slider.highValueChanged.connect(set_vmax)
def set_values(values: Tuple[Union[int,float], Union[int,float]]):
v1, v2 = values
if v1 != self.vmin:
set_vmin(v1)
if v2 != self.vmax:
set_vmax(v2)

self.slider.valueChanged.connect(set_values)

def updateMap(self):
self._norm = mpl.colors.Normalize(vmin=self.vmin, vmax=self.vmax)
Expand Down Expand Up @@ -263,7 +280,12 @@ def onAmplitudeChange(self):
max_amp = float(self.txtMaxAmplitude.text())
except ValueError:
pass

if min_amp >= max_amp:
min_amp = self.vmin
max_amp = self.vmax
self.txtMinAmplitude.setText(str(formatNumber(min_amp)))
self.txtMaxAmplitude.setText(str(formatNumber(max_amp)))
return
self._norm = mpl.colors.Normalize(vmin=min_amp, vmax=max_amp)
self.redrawColorBar()
self.canvas.draw()
19 changes: 12 additions & 7 deletions src/sas/qtgui/Plotting/Plotter2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, parent=None, manager=None, quickplot=False, dimension=2):
self.vmin = None
self.vmax = None
self.im = None
self.cb = None

self.manager = manager

Expand Down Expand Up @@ -523,6 +524,7 @@ def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax,
extent=(self.xmin, self.xmax,
self.ymin, self.ymax))

# color bar for the plot
cbax = self.figure.add_axes([0.88, 0.2, 0.02, 0.7])

# Current labels for axes
Expand All @@ -533,20 +535,23 @@ def showPlot(self, data, qx_data, qy_data, xmin, xmax, ymin, ymax,
if not self.quickplot:
self.ax.set_title(label=self._title)

# remove color bar in case we have it on screen
if self.cb is not None:
self.cb.remove()
if cbax is None:
self.ax.set_frame_on(False)
cb = self.figure.colorbar(self.im, shrink=0.8, aspect=20)
self.cb = self.figure.colorbar(self.im, shrink=0.8, aspect=20)
else:
cb = self.figure.colorbar(self.im, cax=cbax)
self.cb = self.figure.colorbar(self.im, cax=cbax)

cb.update_normal(self.im)
cb.set_label('$' + self.scale + '$')
self.cb.update_normal(self.im)
self.cb.set_label('$' + self.scale + '$')

self.vmin = cb.vmin
self.vmax = cb.vmax
self.vmin = self.cb.vmin
self.vmax = self.cb.vmax

if show_colorbar is False:
cb.remove()
self.cb.remove()

else:
# clear the previous 2D from memory
Expand Down
Loading