Reduce ColorMap inefficiencies (#1927)

* Avoid regenerating QColor lists, speed up generation where unavoidable

* whitespace and typo reduction
This commit is contained in:
Nils Nemitz 2021-07-30 12:50:10 +09:00 committed by GitHub
parent eb8965c2f4
commit 5084d9b537
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -608,7 +608,7 @@ class ColorMap(object):
if np.isscalar(data): if np.isscalar(data):
return QtGui.QColor(*interp) return QtGui.QColor(*interp)
else: else:
return [QtGui.QColor(*x) for x in interp] return [QtGui.QColor(*x.tolist()) for x in interp]
else: else:
return interp return interp
@ -636,6 +636,9 @@ class ColorMap(object):
When no parameters are given for `p1` and `p2`, the gradient is mapped to the When no parameters are given for `p1` and `p2`, the gradient is mapped to the
`y` coordinates 0 to 1, unless the color map is defined for a more limited range. `y` coordinates 0 to 1, unless the color map is defined for a more limited range.
This is a somewhat expensive operation, and it is recommended to store and reuse the returned
gradient instead of repeatedly regenerating it.
Parameters Parameters
---------- ----------
p1: QtCore.QPointF, default (0,0) p1: QtCore.QPointF, default (0,0)
@ -650,8 +653,7 @@ class ColorMap(object):
p2 = QtCore.QPointF(self.pos.max()-self.pos.min(),0) p2 = QtCore.QPointF(self.pos.max()-self.pos.min(),0)
grad = QtGui.QLinearGradient(p1, p2) grad = QtGui.QLinearGradient(p1, p2)
pos, color = self.getStops(mode=self.BYTE) pos, color = self.getStops(mode=self.QCOLOR)
color = [QtGui.QColor(*x) for x in color]
if self.mapping_mode == self.MIRROR: if self.mapping_mode == self.MIRROR:
pos_n = (1. - np.flip(pos)) / 2 pos_n = (1. - np.flip(pos)) / 2
col_n = np.flip( color, axis=0 ) col_n = np.flip( color, axis=0 )
@ -670,6 +672,8 @@ class ColorMap(object):
When the mapping mode is set to `ColorMap.MIRROR`, the selected span includes the color map twice, When the mapping mode is set to `ColorMap.MIRROR`, the selected span includes the color map twice,
first in reversed order and then normal. first in reversed order and then normal.
It is recommended to store and reuse this gradient brush instead of regenerating it repeatedly.
Parameters Parameters
---------- ----------
span : tuple (min, max), default (0.0, 1.0) span : tuple (min, max), default (0.0, 1.0)
@ -696,6 +700,9 @@ class ColorMap(object):
""" """
Returns a QPen that draws according to the color map based on vertical or horizontal position. Returns a QPen that draws according to the color map based on vertical or horizontal position.
It is recommended to store and reuse this gradient pen instead of regenerating it repeatedly.
Parameters Parameters
---------- ----------
span : tuple (min, max), default (0.0, 1.0) span : tuple (min, max), default (0.0, 1.0)
@ -756,7 +763,7 @@ class ColorMap(object):
elif mode == self.QCOLOR: elif mode == self.QCOLOR:
if color.dtype.kind == 'f': if color.dtype.kind == 'f':
color = (color*255).astype(np.ubyte) color = (color*255).astype(np.ubyte)
color = [QtGui.QColor(*x) for x in color] color = [QtGui.QColor(*x.tolist()) for x in color]
self.stopsCache[mode] = (self.pos, color) self.stopsCache[mode] = (self.pos, color)
return self.stopsCache[mode] return self.stopsCache[mode]