add fastpath for float images with nans#2970
Conversation
48dd6aa to
5ed6c93
Compare
j9ac9k
left a comment
There was a problem hiding this comment.
Thanks for the PR @pijyoi
As always, really appreciate the comments, it helps a ton as I read through this.
My only comment is regarding the assert statements, which can easily be bypassed if running Python with the -O argument. Outside of test code we should probably just have an if check and raise an exception of some kind (I don't have strong opinions on what exceptions to raise but maybe one of the numpy ones?)
pyqtgraph/functions_qimage.py
Outdated
| if lut.ndim == 1: | ||
| lut = lut[:, xp.newaxis] | ||
|
|
||
| assert lut.ndim == 2 and lut.shape[1] in (1, 3, 4) |
There was a problem hiding this comment.
Is an assert statement what we actually want here? These are bypassed when Python is run with the -O argument.
There was a problem hiding this comment.
I am not so sure that this makes more sense as an exception. Sometimes I use assertions as comments to document the code flow.
I converted it to an exception but I moved it to the top of the function, otherwise it doesn't make sense to raise a complaint that the lut was not 2d.
pyqtgraph/functions_qimage.py
Outdated
| # instead use it as an Indexed8 ColorTable. This is only | ||
| # applicable if the lut has <= 256 entries. | ||
|
|
||
| assert (not forceApplyLut) or (lut is not None) |
There was a problem hiding this comment.
Same comment about assert as earlier.
There was a problem hiding this comment.
This one makes sense to be an exception.
f6985de to
9196ac5
Compare
058310a to
157f89a
Compare
|
While improving the numba performance for the float with nans case, we also found out how to improve the numba performance for the float with no nans case. (24ms --> 12ms) The gap between numpy vs numba has widened; and the gap between numba and cupy has narrowed. testable with: On a particular machine, improvement was from 38 fps to 57 fps. (50% improvement)
|
|
Revisiting the "optimized" Surprisingly, for an import pyqtgraph as pg
import numpy as np
pg.setConfigOptions(imageAxisOrder='row-major')
# pg.setConfigOptions(useNumba=True)
Y = np.sinc(np.linspace(-6, 10, 4000))[:, np.newaxis]
X = np.sinc(np.linspace(-15, 9, 6000))
data = np.abs(Y * X)
data *= 2000
data = data.astype(np.uint16)
pg.mkQApp()
imv = pg.ImageView()
imv.show()
imv.setImage(data, levels=(0.0, 1000.0))
hlut = imv.getHistogramWidget()
hlut.gradient.setColorMap(pg.colormap.get("CET-C1"))
hlut.gradient.showTicks(False)
pg.exec()
print(imv.imageItem.qimage.format()) |
d92fa55 to
d9e0cd6
Compare
|
Change uint16 mono images to use On a particular machine, improvement was from 46 fps to 83 fps. (80% improvement)
|
d9e0cd6 to
63f4f27
Compare
|
Added support for float RGB images with nans. import pyqtgraph as pg
import numpy as np
pg.setConfigOptions(imageAxisOrder='row-major')
# pg.setConfigOptions(useNumba=True)
bias = 50
Y = np.sinc(np.linspace(-6, 10, 4000))[:, np.newaxis]
X = np.sinc(np.linspace(-15, 9, 6000))
data = np.abs(Y * X)
mask1 = data > 0.95
mask2 = (0.5 < data) & (data < 0.6)
data = 10*np.log10(data)
data += bias
data = np.dstack((data, data * 0.5, data * 0.25))
data[..., 1][mask1] = np.nan
data[..., 2][mask2] = np.nan
data = data.astype(np.float32)
pg.mkQApp()
imv = pg.ImageView(levelMode='mono')
imv.show()
imv.setImage(data, levels=(-50 + bias, 0 + bias))
pg.exec()
print(imv.imageItem.qimage.format()) |
a34d755 to
d5e2f6b
Compare
- don't restrict fastpath to c_contiguous - don't handle scaling of rgba images - update RawImageWidget to handle float images with nans
for some reason, rewriting the function using np.nditer runs 2x as fast as the guvectorize version. uint16 mono image: make use of the improved numba rescale_and_clip
with improvements in numpy, this code has become a pessimization instead. furthermore, the code is difficult to maintain.
d5e2f6b to
9ee68b5
Compare
|
that numba |
|
Every time I think you've gotten as much out of ImageItem performance as we can get, you prove me wrong 😆 Thanks for the PR @pijyoi Merging. |
This PR augments #1693 by adding support for float images containing nans.
To test it out.
Then run the following script and change the levels using the
HistogramLUTItem. The GUI should feel more responsive with this PR versus master.timings from
PYQTGRAPHPROFILEnumba codepath with col-major is no longer worse than numpy. If you were interested in performance, you wouldn't be using col-major anyway...