【发布时间】:2021-10-20 16:51:44
【问题描述】:
我想在 pyqtgraph 的 ImageView() 类中显示 hdf5 文件的数据。显示 ImageView() 绘图的裸代码是:
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
# Interpret image data as row-major instead of col-major
pg.setConfigOptions(leftButtonPan = False, imageAxisOrder='row-major')
app = QtGui.QApplication([])
## Create window with ImageView widget
win = QtGui.QMainWindow()
win.resize(800,800)
imv = pg.ImageView()
win.setCentralWidget(imv)
win.show()
win.setWindowTitle('pyqtgraph example: ImageView')
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
然而,pyqtgraph 示例集中还有一个 hdf5 示例。不幸的是,我无法让它工作。我对示例进行了一些更改以使其满足我的需要,但出现错误。首先是代码:
import numpy as np
import h5py
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
pg.mkQApp()
plt = pg.plot()
plt.setWindowTitle('pyqtgraph example: HDF5 big data')
plt.enableAutoRange(False, False)
plt.setXRange(0, 500)
class HDF5Plot(pg.ImageItem):
def __init__(self, *args, **kwds):
self.hdf5 = None
self.limit = 10000 # maximum number of samples to be plotted
pg.ImageItem.__init__(self, *args, **kwds)
def setHDF5(self, data):
self.hdf5 = data
self.updateHDF5Plot()
def viewRangeChanged(self):
self.updateHDF5Plot()
def updateHDF5Plot(self):
if self.hdf5 is None:
self.setData([])
return
vb = self.getViewBox()
if vb is None:
return # no ViewBox yet
# Determine what data range must be read from HDF5
xrange = vb.viewRange()[0]
start = max(0, int(xrange[0]) - 1)
stop = min(len(self.hdf5), int(xrange[1] + 2))
# Decide by how much we should downsample
ds = int((stop - start) / self.limit) + 1
if ds == 1:
# Small enough to display with no intervention.
visible = self.hdf5[start:stop]
scale = 1
else:
# Here convert data into a down-sampled array suitable for visualizing.
# Must do this piecewise to limit memory usage.
samples = 1 + ((stop - start) // ds)
visible = np.zeros(samples * 2, dtype=self.hdf5.dtype)
sourcePtr = start
targetPtr = 0
# read data in chunks of ~1M samples
chunkSize = (1000000 // ds) * ds
while sourcePtr < stop - 1:
chunk = self.hdf5[sourcePtr:min(stop, sourcePtr + chunkSize)]
sourcePtr += len(chunk)
# reshape chunk to be integral multiple of ds
chunk = chunk[:(len(chunk) // ds) * ds].reshape(len(chunk) // ds, ds)
# compute max and min
chunkMax = chunk.max(axis=1)
chunkMin = chunk.min(axis=1)
# interleave min and max into plot data to preserve envelope shape
visible[targetPtr:targetPtr + chunk.shape[0] * 2:2] = chunkMin
visible[1 + targetPtr:1 + targetPtr + chunk.shape[0] * 2:2] = chunkMax
targetPtr += chunk.shape[0] * 2
visible = visible[:targetPtr]
scale = ds * 0.5
self.setData(visible) # update the plot
self.setPos(start, 0) # shift to match starting index
self.resetTransform()
self.scale(scale, 1) # scale to match downsampling
f = h5py.File('test.hdf5', 'r')
curve = HDF5Plot()
curve.setHDF5(f['data'])
plt.addItem(curve)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
这是错误:
Traceback (most recent call last):
File "pyqtg.py", line 206, in <module>
curve.setHDF5(f['data'])
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/h5py-3.3.0-py3.8-linux-x86_64.egg/h5py/_hl/group.py", line 305, in __getitem__
oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'data' doesn't exist)"
问题是我不知道 hdf5 文件的外观/外观,所以我不确定如何用正确的术语替换“数据”,或者它本身是否完全不同。非常感谢任何帮助。
编辑 1:
我从运行python -m pyqtgraph.examples 中获得了示例。一旦 GUI 从列表中弹出,您将看到“HDF5 大数据”。我的代码源于那个例子。在示例中,从上数第三个 ImageView 是我想用来显示 HDF5 文件的代码。
编辑 2: 下面是运行第二部分代码 kcw78 的结果: http://pastie.org/p/3scRyUm1ZFVJNMwTHQHCBv
编辑 3: 所以我运行了上面的代码,但在 kcw78 的帮助下做了一个小改动。我改变了:
f = h5py.File('test.hdf5', 'r')
curve = HDF5Plot()
curve.setHDF5(f['data'])
plt.addItem(curve)
到:
with h5py.File('test.hdf5', 'r') as h5f:
curve = HDF5Plot()
curve.setHDF5(h5f['aggea'])
plt.addItem(curve)
得到了错误:
Traceback (most recent call last):
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/pyqtgraph/graphicsItems/GraphicsObject.py", line 23, in itemChange
self.parentChanged()
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/pyqtgraph/graphicsItems/GraphicsItem.py", line 458, in parentChanged
self._updateView()
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/pyqtgraph/graphicsItems/GraphicsItem.py", line 514, in _updateView
self.viewRangeChanged()
File "pyqtg.py", line 25, in viewRangeChanged
self.updateHDF5Plot()
File "pyqtg.py", line 77, in updateHDF5Plot
self.setData(visible) # update the plot
TypeError: setData(self, int, Any): argument 1 has unexpected type 'numpy.ndarray'
Traceback (most recent call last):
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/pyqtgraph/graphicsItems/GraphicsObject.py", line 23, in itemChange
self.parentChanged()
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/pyqtgraph/graphicsItems/GraphicsItem.py", line 458, in parentChanged
self._updateView()
File "/home/anaconda3/envs/img/lib/python3.8/site-packages/pyqtgraph/graphicsItems/GraphicsItem.py", line 514, in _updateView
self.viewRangeChanged()
File "pyqtg.py", line 25, in viewRangeChanged
self.updateHDF5Plot()
File "pyqtg.py", line 77, in updateHDF5Plot
self.setData(visible) # update the plot
TypeError: setData(self, int, Any): argument 1 has unexpected type 'numpy.ndarray'
Traceback (most recent call last):
File "pyqtg.py", line 25, in viewRangeChanged
self.updateHDF5Plot()
File "pyqtg.py", line 77, in updateHDF5Plot
self.setData(visible) # update the plot
TypeError: setData(self, int, Any): argument 1 has unexpected type 'numpy.ndarray'
编辑 4:
这是结果的照片:https://imgur.com/a/tVHNdx9。我从创建 2d hdf5 文件和使用我的 2d 数据文件得到相同的空结果。
with h5py.File('mytest.hdf5', 'r') as h5fr, \
h5py.File('test_1d.hdf5', 'w') as h5fw:
arr = h5fr['aggea'][:].reshape(-1,)
h5fw.create_dataset('data', data=arr)
print(h5fw['data'].shape, h5fw['data'].dtype)
编辑 5:运行和绘图的代码
import sys, os
import numpy as np
import h5py
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
pg.mkQApp()
plt = pg.plot()
plt.setWindowTitle('pyqtgraph example: HDF5 big data')
plt.enableAutoRange(False, False)
plt.setXRange(0, 500)
class HDF5Plot(pg.PlotCurveItem):
def __init__(self, *args, **kwds):
self.hdf5 = None
self.limit = 10000 # maximum number of samples to be plotted
pg.PlotCurveItem.__init__(self, *args, **kwds)
def setHDF5(self, data):
self.hdf5 = data
self.updateHDF5Plot()
def viewRangeChanged(self):
self.updateHDF5Plot()
def updateHDF5Plot(self):
if self.hdf5 is None:
self.setData([])
return
vb = self.getViewBox()
if vb is None:
return # no ViewBox yet
# Determine what data range must be read from HDF5
xrange = vb.viewRange()[0]
start = max(0, int(xrange[0]) - 1)
stop = min(len(self.hdf5), int(xrange[1] + 2))
# Decide by how much we should downsample
ds = int((stop - start) / self.limit) + 1
if ds == 1:
# Small enough to display with no intervention.
visible = self.hdf5[start:stop]
scale = 1
else:
# Here convert data into a down-sampled array suitable for visualizing.
# Must do this piecewise to limit memory usage.
samples = 1 + ((stop - start) // ds)
visible = np.zeros(samples * 2, dtype=self.hdf5.dtype)
sourcePtr = start
targetPtr = 0
# read data in chunks of ~1M samples
chunkSize = (1000000 // ds) * ds
while sourcePtr < stop - 1:
chunk = self.hdf5[sourcePtr:min(stop, sourcePtr + chunkSize)]
sourcePtr += len(chunk)
# reshape chunk to be integral multiple of ds
chunk = chunk[:(len(chunk) // ds) * ds].reshape(len(chunk) // ds, ds)
# compute max and min
chunkMax = chunk.max(axis=1)
chunkMin = chunk.min(axis=1)
# interleave min and max into plot data to preserve envelope shape
visible[targetPtr:targetPtr + chunk.shape[0] * 2:2] = chunkMin
visible[1 + targetPtr:1 + targetPtr + chunk.shape[0] * 2:2] = chunkMax
targetPtr += chunk.shape[0] * 2
visible = visible[:targetPtr]
scale = ds * 0.5
self.setData(visible) # update the plot
self.setPos(start, 0) # shift to match starting index
self.resetTransform()
self.scale(scale, 1) # scale to match downsampling
with h5py.File('mytest.hdf5', 'r') as h5fr, \
h5py.File('test_1d.hdf5', 'w') as h5fw:
arr = h5fr['aggea'][:].reshape(-1,)
h5fw.create_dataset('data', data=arr)
curve = HDF5Plot()
curve.setHDF5(h5fw['data'])
plt.addItem(curve)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
编辑 6: 最终效果如何:
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import h5py
import pyqtgraph as pg
import matplotlib.pyplot as plt
app = QtGui.QApplication([])
win = QtGui.QMainWindow()
win.resize(800,800)
imv = pg.ImageView()
win.setCentralWidget(imv)
win.show()
win.setWindowTitle('pyqtgraph example: ImageView')
with h5py.File('test.hdf5', 'r') as h5fr:
data = h5fr.get('aggea')[()] #this gets the values. You can also use hf.get('dataset_name').value as this gives insight what `[()]` is doing, though it's deprecated
imv.setImage(data)
# hf = h5py.File('test.hdf5', 'r')
# n1 = np.array(hf['/pathtodata'][:])
# print(n1.shape)
## Set a custom color map
colors = [
(0, 0, 0),
(45, 5, 61),
(84, 42, 55),
(150, 87, 60),
(208, 171, 141),
(255, 255, 255)
]
cmap = pg.ColorMap(pos=np.linspace(0.0, 1.0, 6), color=colors)
imv.setColorMap(cmap)
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
【问题讨论】: