Added python3 support for efficient method in arrayToQPath
This commit is contained in:
parent
6e253e409b
commit
934e50ad55
|
@ -1082,8 +1082,23 @@ def arrayToQPath(x, y, connect='all'):
|
||||||
## vertices can be read in at once. This binary format may change in future versions of Qt,
|
## vertices can be read in at once. This binary format may change in future versions of Qt,
|
||||||
## so the original (slower) method is left here for emergencies:
|
## so the original (slower) method is left here for emergencies:
|
||||||
#path.moveTo(x[0], y[0])
|
#path.moveTo(x[0], y[0])
|
||||||
|
#if connect == 'all':
|
||||||
#for i in range(1, y.shape[0]):
|
#for i in range(1, y.shape[0]):
|
||||||
#path.lineTo(x[i], y[i])
|
#path.lineTo(x[i], y[i])
|
||||||
|
#elif connect == 'pairs':
|
||||||
|
#for i in range(1, y.shape[0]):
|
||||||
|
#if i%2 == 0:
|
||||||
|
#path.lineTo(x[i], y[i])
|
||||||
|
#else:
|
||||||
|
#path.moveTo(x[i], y[i])
|
||||||
|
#elif isinstance(connect, np.ndarray):
|
||||||
|
#for i in range(1, y.shape[0]):
|
||||||
|
#if connect[i] == 1:
|
||||||
|
#path.lineTo(x[i], y[i])
|
||||||
|
#else:
|
||||||
|
#path.moveTo(x[i], y[i])
|
||||||
|
#else:
|
||||||
|
#raise Exception('connect argument must be "all", "pairs", or array')
|
||||||
|
|
||||||
## Speed this up using >> operator
|
## Speed this up using >> operator
|
||||||
## Format is:
|
## Format is:
|
||||||
|
@ -1098,13 +1113,14 @@ def arrayToQPath(x, y, connect='all'):
|
||||||
path = QtGui.QPainterPath()
|
path = QtGui.QPainterPath()
|
||||||
|
|
||||||
#prof = debug.Profiler('PlotCurveItem.generatePath', disabled=True)
|
#prof = debug.Profiler('PlotCurveItem.generatePath', disabled=True)
|
||||||
if sys.version_info[0] == 2: ## So this is disabled for python 3... why??
|
|
||||||
n = x.shape[0]
|
n = x.shape[0]
|
||||||
# create empty array, pad with extra space on either end
|
# create empty array, pad with extra space on either end
|
||||||
arr = np.empty(n+2, dtype=[('x', '>f8'), ('y', '>f8'), ('c', '>i4')])
|
arr = np.empty(n+2, dtype=[('x', '>f8'), ('y', '>f8'), ('c', '>i4')])
|
||||||
# write first two integers
|
# write first two integers
|
||||||
#prof.mark('allocate empty')
|
#prof.mark('allocate empty')
|
||||||
arr.data[12:20] = struct.pack('>ii', n, 0)
|
byteview = arr.view(dtype=np.ubyte)
|
||||||
|
byteview[:12] = 0
|
||||||
|
byteview.data[12:20] = struct.pack('>ii', n, 0)
|
||||||
#prof.mark('pack header')
|
#prof.mark('pack header')
|
||||||
# Fill array with vertex values
|
# Fill array with vertex values
|
||||||
arr[1:-1]['x'] = x
|
arr[1:-1]['x'] = x
|
||||||
|
@ -1127,15 +1143,18 @@ def arrayToQPath(x, y, connect='all'):
|
||||||
#prof.mark('fill array')
|
#prof.mark('fill array')
|
||||||
# write last 0
|
# write last 0
|
||||||
lastInd = 20*(n+1)
|
lastInd = 20*(n+1)
|
||||||
arr.data[lastInd:lastInd+4] = struct.pack('>i', 0)
|
byteview.data[lastInd:lastInd+4] = struct.pack('>i', 0)
|
||||||
#prof.mark('footer')
|
#prof.mark('footer')
|
||||||
# create datastream object and stream into path
|
# create datastream object and stream into path
|
||||||
|
|
||||||
## Avoiding this method because QByteArray(str) leaks memory in PySide
|
## Avoiding this method because QByteArray(str) leaks memory in PySide
|
||||||
#buf = QtCore.QByteArray(arr.data[12:lastInd+4]) # I think one unnecessary copy happens here
|
#buf = QtCore.QByteArray(arr.data[12:lastInd+4]) # I think one unnecessary copy happens here
|
||||||
|
|
||||||
path.strn = arr.data[12:lastInd+4] # make sure data doesn't run away
|
path.strn = byteview.data[12:lastInd+4] # make sure data doesn't run away
|
||||||
|
try:
|
||||||
buf = QtCore.QByteArray.fromRawData(path.strn)
|
buf = QtCore.QByteArray.fromRawData(path.strn)
|
||||||
|
except TypeError:
|
||||||
|
buf = QtCore.QByteArray(bytes(path.strn))
|
||||||
#prof.mark('create buffer')
|
#prof.mark('create buffer')
|
||||||
ds = QtCore.QDataStream(buf)
|
ds = QtCore.QDataStream(buf)
|
||||||
|
|
||||||
|
@ -1143,26 +1162,6 @@ def arrayToQPath(x, y, connect='all'):
|
||||||
#prof.mark('load')
|
#prof.mark('load')
|
||||||
|
|
||||||
#prof.finish()
|
#prof.finish()
|
||||||
else:
|
|
||||||
## This does exactly the same as above, but less efficiently (and more simply).
|
|
||||||
path.moveTo(x[0], y[0])
|
|
||||||
if connect == 'all':
|
|
||||||
for i in range(1, y.shape[0]):
|
|
||||||
path.lineTo(x[i], y[i])
|
|
||||||
elif connect == 'pairs':
|
|
||||||
for i in range(1, y.shape[0]):
|
|
||||||
if i%2 == 0:
|
|
||||||
path.lineTo(x[i], y[i])
|
|
||||||
else:
|
|
||||||
path.moveTo(x[i], y[i])
|
|
||||||
elif isinstance(connect, np.ndarray):
|
|
||||||
for i in range(1, y.shape[0]):
|
|
||||||
if connect[i] == 1:
|
|
||||||
path.lineTo(x[i], y[i])
|
|
||||||
else:
|
|
||||||
path.moveTo(x[i], y[i])
|
|
||||||
else:
|
|
||||||
raise Exception('connect argument must be "all", "pairs", or array')
|
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user