From 85d7116482e4827b7f6ec20105983732fd5905be Mon Sep 17 00:00:00 2001 From: blink1073 Date: Sat, 23 Nov 2013 23:02:19 -0600 Subject: [PATCH 1/5] Speedups for making ARGB arrays --- pyqtgraph/functions.py | 602 +++++++++++++++++++++-------------------- 1 file changed, 308 insertions(+), 294 deletions(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 3f0c6a3e..df7ead47 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -16,7 +16,7 @@ Colors = { 'y': (255,255,0,255), 'k': (0,0,0,255), 'w': (255,255,255,255), -} +} SI_PREFIXES = asUnicode('yzafpnµm kMGTPEZY') SI_PREFIXES_ASCII = 'yzafpnum kMGTPEZY' @@ -47,16 +47,16 @@ from . import debug def siScale(x, minVal=1e-25, allowUnicode=True): """ Return the recommended scale factor and SI prefix string for x. - + Example:: - + siScale(0.0001) # returns (1e6, 'μ') # This indicates that the number 0.0001 is best represented as 0.0001 * 1e6 = 100 μUnits """ - + if isinstance(x, decimal.Decimal): x = float(x) - + try: if np.isnan(x) or np.isinf(x): return(1, '') @@ -68,7 +68,7 @@ def siScale(x, minVal=1e-25, allowUnicode=True): x = 0 else: m = int(np.clip(np.floor(np.log(abs(x))/np.log(1000)), -9.0, 9.0)) - + if m == 0: pref = '' elif m < -8 or m > 8: @@ -79,27 +79,27 @@ def siScale(x, minVal=1e-25, allowUnicode=True): else: pref = SI_PREFIXES_ASCII[m+8] p = .001**m - - return (p, pref) + + return (p, pref) def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, allowUnicode=True): """ Return the number x formatted in engineering notation with SI prefix. - + Example:: siFormat(0.0001, suffix='V') # returns "100 μV" """ - + if space is True: space = ' ' if space is False: space = '' - - + + (p, pref) = siScale(x, minVal, allowUnicode) if not (len(pref) > 0 and pref[0] == 'e'): pref = space + pref - + if error is None: fmt = "%." + str(precision) + "g%s%s" return fmt % (x*p, pref, suffix) @@ -110,16 +110,16 @@ def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, al plusminus = " +/- " fmt = "%." + str(precision) + "g%s%s%s%s" return fmt % (x*p, pref, suffix, plusminus, siFormat(error, precision=precision, suffix=suffix, space=space, minVal=minVal)) - + def siEval(s): """ Convert a value written in SI notation to its equivalent prefixless value - + Example:: - + siEval("100 μV") # returns 0.0001 """ - + s = asUnicode(s) m = re.match(r'(-?((\d+(\.\d*)?)|(\.\d+))([eE]-?\d+)?)\s*([u' + SI_PREFIXES + r']?).*$', s) if m is None: @@ -135,35 +135,35 @@ def siEval(s): else: n = SI_PREFIXES.index(p) - 8 return v * 1000**n - + class Color(QtGui.QColor): def __init__(self, *args): QtGui.QColor.__init__(self, mkColor(*args)) - + def glColor(self): """Return (r,g,b,a) normalized for use in opengl""" return (self.red()/255., self.green()/255., self.blue()/255., self.alpha()/255.) - + def __getitem__(self, ind): return (self.red, self.green, self.blue, self.alpha)[ind]() - - + + def mkColor(*args): """ Convenience function for constructing QColor from a variety of argument types. Accepted arguments are: - + ================ ================================================ - 'c' one of: r, g, b, c, m, y, k, w + 'c' one of: r, g, b, c, m, y, k, w R, G, B, [A] integers 0-255 (R, G, B, [A]) tuple of integers 0-255 float greyscale, 0.0-1.0 int see :func:`intColor() ` (int, hues) see :func:`intColor() ` "RGB" hexadecimal strings; may begin with '#' - "RGBA" - "RRGGBB" - "RRGGBBAA" + "RGBA" + "RRGGBB" + "RRGGBBAA" QColor QColor instance; makes a copy. ================ ================================================ """ @@ -221,7 +221,7 @@ def mkColor(*args): (r, g, b, a) = args else: raise Exception(err) - + args = [r,g,b,a] args = [0 if np.isnan(a) or np.isinf(a) else a for a in args] args = list(map(int, args)) @@ -250,25 +250,25 @@ def mkBrush(*args, **kwds): def mkPen(*args, **kargs): """ - Convenience function for constructing QPen. - + Convenience function for constructing QPen. + Examples:: - + mkPen(color) mkPen(color, width=2) mkPen(cosmetic=False, width=4.5, color='r') mkPen({'color': "FF0", width: 2}) mkPen(None) # (no pen) - + In these examples, *color* may be replaced with any arguments accepted by :func:`mkColor() ` """ - + color = kargs.get('color', None) width = kargs.get('width', 1) style = kargs.get('style', None) dash = kargs.get('dash', None) cosmetic = kargs.get('cosmetic', True) hsv = kargs.get('hsv', None) - + if len(args) == 1: arg = args[0] if isinstance(arg, dict): @@ -281,14 +281,14 @@ def mkPen(*args, **kargs): color = arg if len(args) > 1: color = args - + if color is None: color = mkColor(200, 200, 200) if hsv is not None: color = hsvColor(*hsv) else: color = mkColor(color) - + pen = QtGui.QPen(QtGui.QBrush(color), width) pen.setCosmetic(cosmetic) if style is not None: @@ -303,7 +303,7 @@ def hsvColor(hue, sat=1.0, val=1.0, alpha=1.0): c.setHsvF(hue, sat, val, alpha) return c - + def colorTuple(c): """Return a tuple (R,G,B,A) from a QColor""" return (c.red(), c.green(), c.blue(), c.alpha()) @@ -315,10 +315,10 @@ def colorStr(c): def intColor(index, hues=9, values=1, maxValue=255, minValue=150, maxHue=360, minHue=0, sat=255, alpha=255, **kargs): """ Creates a QColor from a single index. Useful for stepping through a predefined list of colors. - + The argument *index* determines which color from the set will be returned. All other arguments determine what the set of predefined colors will be - - Colors are chosen by cycling across hues while varying the value (brightness). + + Colors are chosen by cycling across hues while varying the value (brightness). By default, this selects from a list of 9 hues.""" hues = int(hues) values = int(values) @@ -330,7 +330,7 @@ def intColor(index, hues=9, values=1, maxValue=255, minValue=150, maxHue=360, mi else: v = maxValue h = minHue + (indh * (maxHue-minHue)) / hues - + c = QtGui.QColor() c.setHsv(h, sat, v) c.setAlpha(alpha) @@ -344,7 +344,7 @@ def glColor(*args, **kargs): c = mkColor(*args, **kargs) return (c.red()/255., c.green()/255., c.blue()/255., c.alpha()/255.) - + def makeArrowPath(headLen=20, tipAngle=20, tailLen=20, tailWidth=3, baseAngle=0): """ @@ -370,25 +370,25 @@ def makeArrowPath(headLen=20, tipAngle=20, tailLen=20, tailWidth=3, baseAngle=0) path.lineTo(headLen, headWidth) path.lineTo(0,0) return path - - - + + + def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, **kargs): """ Take a slice of any orientation through an array. This is useful for extracting sections of multi-dimensional arrays such as MRI images for viewing as 1D or 2D data. - + The slicing axes are aribtrary; they do not need to be orthogonal to the original data or even to each other. It is possible to use this function to extract arbitrary linear, rectangular, or parallelepiped shapes from within larger datasets. The original data is interpolated onto a new array of coordinates using scipy.ndimage.map_coordinates (see the scipy documentation for more information about this). - + For a graphical interface to this function, see :func:`ROI.getArrayRegion ` - + ============== ==================================================================================================== Arguments: *data* (ndarray) the original dataset *shape* the shape of the slice to take (Note the return value may have more dimensions than len(shape)) *origin* the location in the original dataset that will become the origin of the sliced data. - *vectors* list of unit vectors which point in the direction of the slice axes. Each vector must have the same - length as *axes*. If the vectors are not unit length, the result will be scaled relative to the - original data. If the vectors are not orthogonal, the result will be sheared relative to the + *vectors* list of unit vectors which point in the direction of the slice axes. Each vector must have the same + length as *axes*. If the vectors are not unit length, the result will be scaled relative to the + original data. If the vectors are not orthogonal, the result will be sheared relative to the original data. *axes* The axes in the original dataset which correspond to the slice *vectors* *order* The order of spline interpolation. Default is 1 (linear). See scipy.ndimage.map_coordinates @@ -398,23 +398,23 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, *All extra keyword arguments are passed to scipy.ndimage.map_coordinates.* -------------------------------------------------------------------------------------------------------------------- ============== ==================================================================================================== - - Note the following must be true: - - | len(shape) == len(vectors) + + Note the following must be true: + + | len(shape) == len(vectors) | len(origin) == len(axes) == len(vectors[i]) - + Example: start with a 4D fMRI data set, take a diagonal-planar slice out of the last 3 axes - + * data = array with dims (time, x, y, z) = (100, 40, 40, 40) - * The plane to pull out is perpendicular to the vector (x,y,z) = (1,1,1) + * The plane to pull out is perpendicular to the vector (x,y,z) = (1,1,1) * The origin of the slice will be at (x,y,z) = (40, 0, 0) * We will slice a 20x20 plane from each timepoint, giving a final shape (100, 20, 20) - + The call for this example would look like:: - + affineSlice(data, shape=(20,20), origin=(40,0,0), vectors=((-1, 1, 0), (-1, 0, 1)), axes=(1,2,3)) - + """ if not HAVE_SCIPY: raise Exception("This function requires the scipy library, but it does not appear to be importable.") @@ -427,7 +427,7 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, for v in vectors: if len(v) != len(axes): raise Exception("each vector must be same length as axes.") - + shape = list(map(np.ceil, shape)) ## transpose data so slice axes come first @@ -438,7 +438,7 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, data = data.transpose(tr1) #print "tr1:", tr1 ## dims are now [(slice axes), (other axes)] - + ## make sure vectors are arrays if not isinstance(vectors, np.ndarray): @@ -446,8 +446,8 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, if not isinstance(origin, np.ndarray): origin = np.array(origin) origin.shape = (len(axes),) + (1,)*len(shape) - - ## Build array of sample locations. + + ## Build array of sample locations. grid = np.mgrid[tuple([slice(0,x) for x in shape])] ## mesh grid of indexes #print shape, grid.shape x = (grid[np.newaxis,...] * vectors.transpose()[(Ellipsis,) + (np.newaxis,)*len(shape)]).sum(axis=1) ## magic @@ -461,7 +461,7 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, ind = (Ellipsis,) + inds #print data[ind].shape, x.shape, output[ind].shape, output.shape output[ind] = scipy.ndimage.map_coordinates(data[ind], x, order=order, **kargs) - + tr = list(range(output.ndim)) trb = [] for i in range(min(axes)): @@ -481,20 +481,20 @@ def transformToArray(tr): """ Given a QTransform, return a 3x3 numpy array. Given a QMatrix4x4, return a 4x4 numpy array. - + Example: map an array of x,y coordinates through a transform:: - + ## coordinates to map are (1,5), (2,6), (3,7), and (4,8) coords = np.array([[1,2,3,4], [5,6,7,8], [1,1,1,1]]) # the extra '1' coordinate is needed for translation to work - + ## Make an example transform tr = QtGui.QTransform() tr.translate(3,4) tr.scale(2, 0.1) - + ## convert to array m = pg.transformToArray()[:2] # ignore the perspective portion of the transformation - + ## map coordinates through transform mapped = np.dot(m, coords) """ @@ -515,24 +515,24 @@ def transformCoordinates(tr, coords, transpose=False): Map a set of 2D or 3D coordinates through a QTransform or QMatrix4x4. The shape of coords must be (2,...) or (3,...) The mapping will _ignore_ any perspective transformations. - + For coordinate arrays with ndim=2, this is basically equivalent to matrix multiplication. - Most arrays, however, prefer to put the coordinate axis at the end (eg. shape=(...,3)). To + Most arrays, however, prefer to put the coordinate axis at the end (eg. shape=(...,3)). To allow this, use transpose=True. - + """ - + if transpose: ## move last axis to beginning. This transposition will be reversed before returning the mapped coordinates. coords = coords.transpose((coords.ndim-1,) + tuple(range(0,coords.ndim-1))) - + nd = coords.shape[0] if isinstance(tr, np.ndarray): m = tr else: m = transformToArray(tr) m = m[:m.shape[0]-1] # remove perspective - + ## If coords are 3D and tr is 2D, assume no change for Z axis if m.shape == (2,3) and nd == 3: m2 = np.zeros((3,4)) @@ -540,34 +540,34 @@ def transformCoordinates(tr, coords, transpose=False): m2[:2, 3] = m[:2,2] m2[2,2] = 1 m = m2 - + ## if coords are 2D and tr is 3D, ignore Z axis if m.shape == (3,4) and nd == 2: m2 = np.empty((2,3)) m2[:,:2] = m[:2,:2] m2[:,2] = m[:2,3] m = m2 - + ## reshape tr and coords to prepare for multiplication m = m.reshape(m.shape + (1,)*(coords.ndim-1)) coords = coords[np.newaxis, ...] - - # separate scale/rotate and translation - translate = m[:,-1] + + # separate scale/rotate and translation + translate = m[:,-1] m = m[:, :-1] - + ## map coordinates and return mapped = (m*coords).sum(axis=1) ## apply scale/rotate mapped += translate - + if transpose: ## move first axis to end. mapped = mapped.transpose(tuple(range(1,mapped.ndim)) + (0,)) return mapped - - - + + + def solve3DTransform(points1, points2): """ Find a 3D transformation matrix that maps points1 onto points2. @@ -577,21 +577,21 @@ def solve3DTransform(points1, points2): raise Exception("This function depends on the scipy library, but it does not appear to be importable.") A = np.array([[points1[i].x(), points1[i].y(), points1[i].z(), 1] for i in range(4)]) B = np.array([[points2[i].x(), points2[i].y(), points2[i].z(), 1] for i in range(4)]) - + ## solve 3 sets of linear equations to determine transformation matrix elements matrix = np.zeros((4,4)) for i in range(3): matrix[i] = scipy.linalg.solve(A, B[:,i]) ## solve Ax = B; x is one row of the desired transformation matrix - + return matrix - + def solveBilinearTransform(points1, points2): """ Find a bilinear transformation matrix (2x4) that maps points1 onto points2. Points must be specified as a list of 4 Vector, Point, QPointF, etc. - + To use this matrix to map a point [x,y]:: - + mapped = np.dot(matrix, [x*y, x, y, 1]) """ if not HAVE_SCIPY: @@ -600,30 +600,30 @@ def solveBilinearTransform(points1, points2): ## B is 4 rows (points) x 2 columns (x, y) A = np.array([[points1[i].x()*points1[i].y(), points1[i].x(), points1[i].y(), 1] for i in range(4)]) B = np.array([[points2[i].x(), points2[i].y()] for i in range(4)]) - + ## solve 2 sets of linear equations to determine transformation matrix elements matrix = np.zeros((2,4)) for i in range(2): matrix[i] = scipy.linalg.solve(A, B[:,i]) ## solve Ax = B; x is one row of the desired transformation matrix - + return matrix - + def rescaleData(data, scale, offset, dtype=None): """Return data rescaled and optionally cast to a new dtype:: - + data => (data-offset) * scale - + Uses scipy.weave (if available) to improve performance. """ if dtype is None: dtype = data.dtype else: dtype = np.dtype(dtype) - + try: if not pg.getConfigOption('useWeave'): raise Exception('Weave is disabled; falling back to slower version.') - + ## require native dtype when using weave if not data.dtype.isnative: data = data.astype(data.dtype.newbyteorder('=')) @@ -631,11 +631,11 @@ def rescaleData(data, scale, offset, dtype=None): weaveDtype = dtype.newbyteorder('=') else: weaveDtype = dtype - + newData = np.empty((data.size,), dtype=weaveDtype) flat = np.ascontiguousarray(data).reshape(data.size) size = data.size - + code = """ double sc = (double)scale; double off = (double)offset; @@ -652,61 +652,61 @@ def rescaleData(data, scale, offset, dtype=None): if pg.getConfigOption('weaveDebug'): debug.printExc("Error; disabling weave.") pg.setConfigOption('useWeave', False) - + #p = np.poly1d([scale, -offset*scale]) #data = p(data).astype(dtype) d2 = data-offset d2 *= scale data = d2.astype(dtype) return data - + def applyLookupTable(data, lut): """ Uses values in *data* as indexes to select values from *lut*. The returned data has shape data.shape + lut.shape[1:] - + Uses scipy.weave to improve performance if it is available. Note: color gradient lookup tables can be generated using GradientWidget. """ if data.dtype.kind not in ('i', 'u'): data = data.astype(int) - + ## using np.take appears to be faster than even the scipy.weave method and takes care of clipping as well. - return np.take(lut, data, axis=0, mode='clip') - - ### old methods: + return np.take(lut, data, axis=0, mode='clip') + + ### old methods: #data = np.clip(data, 0, lut.shape[0]-1) - + #try: #if not USE_WEAVE: #raise Exception('Weave is disabled; falling back to slower version.') - + ### number of values to copy for each LUT lookup #if lut.ndim == 1: #ncol = 1 #else: #ncol = sum(lut.shape[1:]) - + ### output array #newData = np.empty((data.size, ncol), dtype=lut.dtype) - + ### flattened input arrays #flatData = data.flatten() #flatLut = lut.reshape((lut.shape[0], ncol)) - + #dataSize = data.size - - ### strides for accessing each item + + ### strides for accessing each item #newStride = newData.strides[0] / newData.dtype.itemsize #lutStride = flatLut.strides[0] / flatLut.dtype.itemsize #dataStride = flatData.strides[0] / flatData.dtype.itemsize - + ### strides for accessing individual values within a single LUT lookup #newColStride = newData.strides[1] / newData.dtype.itemsize #lutColStride = flatLut.strides[1] / flatLut.dtype.itemsize - + #code = """ - + #for( int i=0; i0 and max->*scale*:: - + rescaled = (clip(data, min, max) - min) * (*scale* / (max - min)) - + It is also possible to use a 2D (N,2) array of values for levels. In this case, - it is assumed that each pair of min,max values in the levels array should be - applied to a different subset of the input data (for example, the input data may - already have RGB values and the levels are used to independently scale each + it is assumed that each pair of min,max values in the levels array should be + applied to a different subset of the input data (for example, the input data may + already have RGB values and the levels are used to independently scale each channel). The use of this feature requires that levels.shape[0] == data.shape[-1]. - scale The maximum value to which data will be rescaled before being passed through the + scale The maximum value to which data will be rescaled before being passed through the lookup table (or returned if there is no lookup table). By default this will be set to the length of the lookup table, or 256 is no lookup table is provided. For OpenGL color specifications (as in GLColor4f) use scale=1.0 lut Optional lookup table (array with dtype=ubyte). Values in data will be converted to color by indexing directly from lut. The output data shape will be input.shape + lut.shape[1:]. - + Note: the output of makeARGB will have the same dtype as the lookup table, so for conversion to QImage, the dtype must be ubyte. - + Lookup tables can be built using GradientWidget. - useRGBA If True, the data is returned in RGBA order (useful for building OpenGL textures). - The default is False, which returns in ARGB order for use with QImage - (Note that 'ARGB' is a term used by the Qt documentation; the _actual_ order + useRGBA If True, the data is returned in RGBA order (useful for building OpenGL textures). + The default is False, which returns in ARGB order for use with QImage + (Note that 'ARGB' is a term used by the Qt documentation; the _actual_ order is BGRA). + transpose Whether to pre-transpose the data in preparation for use in Qt ============ ================================================================================== """ prof = debug.Profiler('functions.makeARGB', disabled=True) - + if lut is not None and not isinstance(lut, np.ndarray): lut = np.array(lut) if levels is not None and not isinstance(levels, np.ndarray): levels = np.array(levels) - + ## sanity checks #if data.ndim == 3: #if data.shape[2] not in (3,4): @@ -791,7 +792,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): ##raise Exception("can not use lookup table with 3D data") #elif data.ndim != 2: #raise Exception("data must be 2D or 3D") - + #if lut is not None: ##if lut.ndim == 2: ##if lut.shape[1] : @@ -800,7 +801,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): ##raise Exception("lut must be 1D or 2D") #if lut.dtype != np.ubyte: #raise Exception('lookup table must have dtype=ubyte (got %s instead)' % str(lut.dtype)) - + if levels is not None: if levels.ndim == 1: @@ -824,7 +825,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): #raise Exception('Can not use 2D levels and lookup table together.') #else: #raise Exception("Levels must have shape (2,) or (3,2) or (4,2)") - + prof.mark('1') if scale is None: @@ -835,7 +836,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): ## Apply levels if given if levels is not None: - + if isinstance(levels, np.ndarray) and levels.ndim == 2: ## we are going to rescale each channel independently if levels.shape[0] != data.shape[-1]: @@ -865,9 +866,10 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): ## copy data into ARGB ordered array - imgData = np.empty(data.shape[:2]+(4,), dtype=np.ubyte) - if data.ndim == 2: - data = data[..., np.newaxis] + if transpose: + imgData = np.empty((data.shape[1], data.shape[0], 4), dtype=np.ubyte) + else: + imgData = np.empty(data.shape[:2]+(4,), dtype=np.ubyte) prof.mark('4') @@ -875,27 +877,39 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): order = [0,1,2,3] ## array comes out RGBA else: order = [2,1,0,3] ## for some reason, the colors line up as BGR in the final image. - - if data.shape[2] == 1: + + if data.ndim == 2: for i in range(3): - imgData[..., order[i]] = data[..., 0] + if transpose: + imgData[..., i] = data.T + else: + imgData[..., i] = data + elif data.shape[2] == 1: + for i in range(3): + if transpose: + imgData[..., i] = data[..., 0].T + else: + imgData[..., i] = data[..., 0] else: for i in range(0, data.shape[2]): - imgData[..., order[i]] = data[..., i] - + if transpose: + imgData[..., order[i]] = data[..., order[i]].T + else: + imgData[..., order[i]] = data[..., order[i]] + prof.mark('5') - - if data.shape[2] == 4: - alpha = True - else: + + if data.ndim == 2 or data.shape[2] == 3: alpha = False imgData[..., 3] = 255 - + else: + alpha = True + prof.mark('6') - + prof.finish() return imgData, alpha - + def makeQImage(imgData, alpha=None, copy=True, transpose=True): """ @@ -904,11 +918,11 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): be reflected in the image. The image will be given a 'data' attribute pointing to the array which shares its data to prevent python freeing that memory while the image is in use. - + =========== =================================================================== Arguments: - imgData Array of data to convert. Must have shape (width, height, 3 or 4) - and dtype=ubyte. The order of values in the 3rd axis must be + imgData Array of data to convert. Must have shape (width, height, 3 or 4) + and dtype=ubyte. The order of values in the 3rd axis must be (b, g, r, a). alpha If True, the QImage returned will have format ARGB32. If False, the format will be RGB32. By default, _alpha_ is True if @@ -917,19 +931,19 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): If False, the new QImage points directly to the data in the array. Note that the array must be contiguous for this to work (see numpy.ascontiguousarray). - transpose If True (the default), the array x/y axes are transposed before - creating the image. Note that Qt expects the axes to be in - (height, width) order whereas pyqtgraph usually prefers the + transpose If True (the default), the array x/y axes are transposed before + creating the image. Note that Qt expects the axes to be in + (height, width) order whereas pyqtgraph usually prefers the opposite. - =========== =================================================================== + =========== =================================================================== """ ## create QImage from buffer prof = debug.Profiler('functions.makeQImage', disabled=True) - + ## If we didn't explicitly specify alpha, check the array shape. if alpha is None: alpha = (imgData.shape[2] == 4) - + copied = False if imgData.shape[2] == 3: ## need to make alpha channel (even if alpha==False; QImage requires 32 bpp) if copy is True: @@ -940,25 +954,25 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): copied = True else: raise Exception('Array has only 3 channels; cannot make QImage without copying.') - + if alpha: imgFormat = QtGui.QImage.Format_ARGB32 else: imgFormat = QtGui.QImage.Format_RGB32 - + if transpose: imgData = imgData.transpose((1, 0, 2)) ## QImage expects the row/column order to be opposite - + if not imgData.flags['C_CONTIGUOUS']: if copy is False: extra = ' (try setting transpose=False)' if transpose else '' raise Exception('Array is not contiguous; cannot make QImage without copying.'+extra) imgData = np.ascontiguousarray(imgData) copied = True - + if copy is True and copied is False: imgData = imgData.copy() - + if USE_PYSIDE: ch = ctypes.c_char.from_buffer(imgData, 0) img = QtGui.QImage(ch, imgData.shape[1], imgData.shape[0], imgFormat) @@ -969,7 +983,7 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): #addr = ctypes.c_char.from_buffer(imgData, 0) #try: #img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) - #except TypeError: + #except TypeError: #addr = ctypes.addressof(addr) #img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) try: @@ -981,14 +995,14 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): else: # mutable, but leaks memory img = QtGui.QImage(memoryview(imgData), imgData.shape[1], imgData.shape[0], imgFormat) - + img.data = imgData return img #try: #buf = imgData.data #except AttributeError: ## happens when image data is non-contiguous #buf = imgData.data - + #prof.mark('1') #qimage = QtGui.QImage(buf, imgData.shape[1], imgData.shape[0], imgFormat) #prof.mark('2') @@ -999,7 +1013,7 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): def imageToArray(img, copy=False, transpose=True): """ Convert a QImage into numpy array. The image must have format RGB32, ARGB32, or ARGB32_Premultiplied. - By default, the image is not copied; changes made to the array will appear in the QImage as well (beware: if + By default, the image is not copied; changes made to the array will appear in the QImage as well (beware: if the QImage is collected before the array, there may be trouble). The array will have shape (width, height, (b,g,r,a)). """ @@ -1010,34 +1024,34 @@ def imageToArray(img, copy=False, transpose=True): else: ptr.setsize(img.byteCount()) arr = np.asarray(ptr) - + if fmt == img.Format_RGB32: arr = arr.reshape(img.height(), img.width(), 3) elif fmt == img.Format_ARGB32 or fmt == img.Format_ARGB32_Premultiplied: arr = arr.reshape(img.height(), img.width(), 4) - + if copy: arr = arr.copy() - + if transpose: return arr.transpose((1,0,2)) else: return arr - + def colorToAlpha(data, color): """ - Given an RGBA image in *data*, convert *color* to be transparent. - *data* must be an array (w, h, 3 or 4) of ubyte values and *color* must be + Given an RGBA image in *data*, convert *color* to be transparent. + *data* must be an array (w, h, 3 or 4) of ubyte values and *color* must be an array (3) of ubyte values. This is particularly useful for use with images that have a black or white background. - + Algorithm is taken from Gimp's color-to-alpha function in plug-ins/common/colortoalpha.c Credit: /* * Color To Alpha plug-in v1.0 by Seth Burgess, sjburges@gimp.org 1999/05/14 * with algorithm by clahey */ - + """ data = data.astype(float) if data.shape[-1] == 3: ## add alpha channel if needed @@ -1045,11 +1059,11 @@ def colorToAlpha(data, color): d2[...,:3] = data d2[...,3] = 255 data = d2 - + color = color.astype(float) alpha = np.zeros(data.shape[:2]+(3,), dtype=float) output = data.copy() - + for i in [0,1,2]: d = data[...,i] c = color[i] @@ -1057,18 +1071,18 @@ def colorToAlpha(data, color): alpha[...,i][mask] = (d[mask] - c) / (255. - c) imask = d < c alpha[...,i][imask] = (c - d[imask]) / c - + output[...,3] = alpha.max(axis=2) * 255. - + mask = output[...,3] >= 1.0 ## avoid zero division while processing alpha channel correction = 255. / output[...,3][mask] ## increase value to compensate for decreased alpha for i in [0,1,2]: output[...,i][mask] = ((output[...,i][mask]-color[i]) * correction) + color[i] output[...,3][mask] *= data[...,3][mask] / 255. ## combine computed and previous alpha values - + #raise Exception() return np.clip(output, 0, 255).astype(np.ubyte) - + def arrayToQPath(x, y, connect='all'): @@ -1170,28 +1184,28 @@ def arrayToQPath(x, y, connect='all'): #""" #Generate isosurface from volumetric data using marching tetrahedra algorithm. #See Paul Bourke, "Polygonising a Scalar Field Using Tetrahedrons" (http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/) - + #*data* 3D numpy array of scalar values #*level* The level at which to generate an isosurface #""" - + #facets = [] - + ### mark everything below the isosurface level #mask = data < level - - #### make eight sub-fields + + #### make eight sub-fields #fields = np.empty((2,2,2), dtype=object) #slices = [slice(0,-1), slice(1,None)] #for i in [0,1]: #for j in [0,1]: #for k in [0,1]: #fields[i,j,k] = mask[slices[i], slices[j], slices[k]] - - - + + + ### split each cell into 6 tetrahedra - ### these all have the same 'orienation'; points 1,2,3 circle + ### these all have the same 'orienation'; points 1,2,3 circle ### clockwise around point 0 #tetrahedra = [ #[(0,1,0), (1,1,1), (0,1,1), (1,0,1)], @@ -1201,15 +1215,15 @@ def arrayToQPath(x, y, connect='all'): #[(0,1,0), (1,0,0), (1,1,0), (1,0,1)], #[(0,1,0), (1,1,0), (1,1,1), (1,0,1)] #] - + ### each tetrahedron will be assigned an index ### which determines how to generate its facets. - ### this structure is: + ### this structure is: ### facets[index][facet1, facet2, ...] - ### where each facet is triangular and its points are each + ### where each facet is triangular and its points are each ### interpolated between two points on the tetrahedron ### facet = [(p1a, p1b), (p2a, p2b), (p3a, p3b)] - ### facet points always circle clockwise if you are looking + ### facet points always circle clockwise if you are looking ### at them from below the isosurface. #indexFacets = [ #[], ## all above @@ -1229,15 +1243,15 @@ def arrayToQPath(x, y, connect='all'): #[[(0,1), (0,3), (0,2)]], # 1,2,3 below #[] ## all below #] - + #for tet in tetrahedra: - + ### get the 4 fields for this tetrahedron #tetFields = [fields[c] for c in tet] - + ### generate an index for each grid cell #index = tetFields[0] + tetFields[1]*2 + tetFields[2]*4 + tetFields[3]*8 - + ### add facets #for i in xrange(index.shape[0]): # data x-axis #for j in xrange(index.shape[1]): # data y-axis @@ -1251,32 +1265,32 @@ def arrayToQPath(x, y, connect='all'): #facets.append(pts) #return facets - + def isocurve(data, level, connected=False, extendToEdge=False, path=False): """ Generate isocurve from 2D data using marching squares algorithm. - + ============= ========================================================= Arguments data 2D numpy array of scalar values level The level at which to generate an isosurface connected If False, return a single long list of point pairs - If True, return multiple long lists of connected point - locations. (This is slower but better for drawing + If True, return multiple long lists of connected point + locations. (This is slower but better for drawing continuous lines) - extendToEdge If True, extend the curves to reach the exact edges of - the data. - path if True, return a QPainterPath rather than a list of + extendToEdge If True, extend the curves to reach the exact edges of + the data. + path if True, return a QPainterPath rather than a list of vertex coordinates. This forces connected=True. ============= ========================================================= - + This function is SLOW; plenty of room for optimization here. - """ - + """ + if path is True: connected = True - + if extendToEdge: d2 = np.empty((data.shape[0]+2, data.shape[1]+2), dtype=data.dtype) d2[1:-1, 1:-1] = data @@ -1289,7 +1303,7 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): d2[-1,0] = d2[-1,1] d2[-1,-1] = d2[-1,-2] data = d2 - + sideTable = [ [], [0,1], @@ -1308,20 +1322,20 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): [0,1], [] ] - + edgeKey=[ [(0,1), (0,0)], [(0,0), (1,0)], [(1,0), (1,1)], [(1,1), (0,1)] ] - - + + lines = [] - + ## mark everything below the isosurface level mask = data < level - + ### make four sub-fields and compute indexes for grid cells index = np.zeros([x-1 for x in data.shape], dtype=np.ubyte) fields = np.empty((2,2), dtype=object) @@ -1335,10 +1349,10 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): index += fields[i,j] * 2**vertIndex #print index #print index - + ## add lines for i in range(index.shape[0]): # data x-axis - for j in range(index.shape[1]): # data y-axis + for j in range(index.shape[1]): # data y-axis sides = sideTable[index[i,j]] for l in range(0, len(sides), 2): ## faces for this grid cell edges = sides[l:l+2] @@ -1351,26 +1365,26 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): f = (level-v1) / (v2-v1) fi = 1.0 - f p = ( ## interpolate between corners - p1[0]*fi + p2[0]*f + i + 0.5, + p1[0]*fi + p2[0]*f + i + 0.5, p1[1]*fi + p2[1]*f + j + 0.5 ) if extendToEdge: ## check bounds p = ( min(data.shape[0]-2, max(0, p[0]-1)), - min(data.shape[1]-2, max(0, p[1]-1)), + min(data.shape[1]-2, max(0, p[1]-1)), ) if connected: gridKey = i + (1 if edges[m]==2 else 0), j + (1 if edges[m]==3 else 0), edges[m]%2 pts.append((p, gridKey)) ## give the actual position and a key identifying the grid location (for connecting segments) else: pts.append(p) - + lines.append(pts) if not connected: return lines - + ## turn disjoint list of segments into continuous lines #lines = [[2,5], [5,4], [3,4], [1,3], [6,7], [7,8], [8,6], [11,12], [12,15], [11,13], [13,14]] @@ -1397,9 +1411,9 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): while True: if x == chain[-1][1]: break ## nothing left to do on this chain - + x = chain[-1][1] - if x == k: + if x == k: break ## chain has looped; we're done and can ignore the opposite chain y = chain[-2][1] connects = points[x] @@ -1412,9 +1426,9 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): if chain[0][1] == chain[-1][1]: # looped chain; no need to continue the other direction chains.pop() break - - ## extract point locations + + ## extract point locations lines = [] for chain in points.values(): if len(chain) == 2: @@ -1422,25 +1436,25 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): else: chain = chain[0] lines.append([p[0] for p in chain]) - + if not path: return lines ## a list of pairs of points - + path = QtGui.QPainterPath() for line in lines: path.moveTo(*line[0]) for p in line[1:]: path.lineTo(*p) - + return path - - + + def traceImage(image, values, smooth=0.5): """ Convert an image to a set of QPainterPath curves. One curve will be generated for each item in *values*; each curve outlines the area of the image that is closer to its value than to any others. - + If image is RGB or RGBA, then the shape of values should be (nvals, 3/4) The parameter *smooth* is expressed in pixels. """ @@ -1452,11 +1466,11 @@ def traceImage(image, values, smooth=0.5): diff = np.abs(image-values) if values.ndim == 4: diff = diff.sum(axis=2) - + labels = np.argmin(diff, axis=2) - + paths = [] - for i in range(diff.shape[-1]): + for i in range(diff.shape[-1]): d = (labels==i).astype(float) d = ndi.gaussian_filter(d, (smooth, smooth)) lines = isocurve(d, 0.5, connected=True, extendToEdge=True) @@ -1465,31 +1479,31 @@ def traceImage(image, values, smooth=0.5): path.moveTo(*line[0]) for p in line[1:]: path.lineTo(*p) - + paths.append(path) return paths - - - + + + IsosurfaceDataCache = None def isosurface(data, level): """ Generate isosurface from volumetric data using marching cubes algorithm. - See Paul Bourke, "Polygonising a Scalar Field" + See Paul Bourke, "Polygonising a Scalar Field" (http://paulbourke.net/geometry/polygonise/) - + *data* 3D numpy array of scalar values *level* The level at which to generate an isosurface - - Returns an array of vertex coordinates (Nv, 3) and an array of - per-face vertex indexes (Nf, 3) + + Returns an array of vertex coordinates (Nv, 3) and an array of + per-face vertex indexes (Nf, 3) """ ## For improvement, see: - ## + ## ## Efficient implementation of Marching Cubes' cases with topological guarantees. ## Thomas Lewiner, Helio Lopes, Antonio Wilson Vieira and Geovan Tavares. ## Journal of Graphics Tools 8(2): pp. 1-15 (december 2003) - + ## Precompute lookup tables on the first run global IsosurfaceDataCache if IsosurfaceDataCache is None: @@ -1530,7 +1544,7 @@ def isosurface(data, level): 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190, 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0 ], dtype=np.uint16) - + ## Table of triangles to use for filling each grid cell. ## Each set of three integers tells us which three edges to ## draw a triangle between. @@ -1792,9 +1806,9 @@ def isosurface(data, level): [0, 9, 1], [0, 3, 8], [] - ] + ] edgeShifts = np.array([ ## maps edge ID (0-11) to (x,y,z) cell offset and edge ID (0-2) - [0, 0, 0, 0], + [0, 0, 0, 0], [1, 0, 0, 1], [0, 1, 0, 0], [0, 0, 0, 1], @@ -1817,23 +1831,23 @@ def isosurface(data, level): faceTableI[faceTableInds[:,0]] = np.array([triTable[j] for j in faceTableInds]) faceTableI = faceTableI.reshape((len(triTable), i, 3)) faceShiftTables.append(edgeShifts[faceTableI]) - + ## Let's try something different: #faceTable = np.empty((256, 5, 3, 4), dtype=np.ubyte) # (grid cell index, faces, vertexes, edge lookup) #for i,f in enumerate(triTable): #f = np.array(f + [12] * (15-len(f))).reshape(5,3) #faceTable[i] = edgeShifts[f] - - + + IsosurfaceDataCache = (faceShiftTables, edgeShifts, edgeTable, nTableFaces) else: faceShiftTables, edgeShifts, edgeTable, nTableFaces = IsosurfaceDataCache - + ## mark everything below the isosurface level mask = data < level - + ### make eight sub-fields and compute indexes for grid cells index = np.zeros([x-1 for x in data.shape], dtype=np.ubyte) fields = np.empty((2,2,2), dtype=object) @@ -1844,23 +1858,23 @@ def isosurface(data, level): fields[i,j,k] = mask[slices[i], slices[j], slices[k]] vertIndex = i - 2*j*i + 3*j + 4*k ## this is just to match Bourk's vertex numbering scheme index += fields[i,j,k] * 2**vertIndex - + ### Generate table of edges that have been cut cutEdges = np.zeros([x+1 for x in index.shape]+[3], dtype=np.uint32) edges = edgeTable[index] - for i, shift in enumerate(edgeShifts[:12]): + for i, shift in enumerate(edgeShifts[:12]): slices = [slice(shift[j],cutEdges.shape[j]+(shift[j]-1)) for j in range(3)] cutEdges[slices[0], slices[1], slices[2], shift[3]] += edges & 2**i - + ## for each cut edge, interpolate to see where exactly the edge is cut and generate vertex positions m = cutEdges > 0 vertexInds = np.argwhere(m) ## argwhere is slow! vertexes = vertexInds[:,:3].astype(np.float32) dataFlat = data.reshape(data.shape[0]*data.shape[1]*data.shape[2]) - + ## re-use the cutEdges array as a lookup table for vertex IDs cutEdges[vertexInds[:,0], vertexInds[:,1], vertexInds[:,2], vertexInds[:,3]] = np.arange(vertexInds.shape[0]) - + for i in [0,1,2]: vim = vertexInds[:,3] == i vi = vertexInds[vim, :3] @@ -1868,9 +1882,9 @@ def isosurface(data, level): v1 = dataFlat[viFlat] v2 = dataFlat[viFlat + data.strides[i]//data.itemsize] vertexes[vim,i] += (level-v1) / (v2-v1) - - ### compute the set of vertex indexes for each face. - + + ### compute the set of vertex indexes for each face. + ## This works, but runs a bit slower. #cells = np.argwhere((index != 0) & (index != 255)) ## all cells with at least one face #cellInds = index[cells[:,0], cells[:,1], cells[:,2]] @@ -1879,9 +1893,9 @@ def isosurface(data, level): #verts[...,:3] += cells[:,np.newaxis,np.newaxis,:] ## we now have indexes into cutEdges #verts = verts[mask] #faces = cutEdges[verts[...,0], verts[...,1], verts[...,2], verts[...,3]] ## and these are the vertex indexes we want. - - - ## To allow this to be vectorized efficiently, we count the number of faces in each + + + ## To allow this to be vectorized efficiently, we count the number of faces in each ## grid cell and handle each group of cells with the same number together. ## determine how many faces to assign to each grid cell nFaces = nTableFaces[index] @@ -1890,7 +1904,7 @@ def isosurface(data, level): ptr = 0 #import debug #p = debug.Profiler('isosurface', disabled=False) - + ## this helps speed up an indexing operation later on cs = np.array(cutEdges.strides)//cutEdges.itemsize cutEdges = cutEdges.flatten() @@ -1909,14 +1923,14 @@ def isosurface(data, level): #cellInds = index[(cells*ins[np.newaxis,:]).sum(axis=1)] cellInds = index[cells[:,0], cells[:,1], cells[:,2]] ## index values of cells to process for this round #p.mark('3') - + ### expensive: verts = faceShiftTables[i][cellInds] #p.mark('4') verts[...,:3] += cells[:,np.newaxis,np.newaxis,:] ## we now have indexes into cutEdges verts = verts.reshape((verts.shape[0]*i,)+verts.shape[2:]) #p.mark('5') - + ### expensive: #print verts.shape verts = (verts * cs[np.newaxis, np.newaxis, :]).sum(axis=2) @@ -1928,15 +1942,15 @@ def isosurface(data, level): faces[ptr:ptr+nv] = vertInds #.reshape((nv, 3)) #p.mark('8') ptr += nv - + return vertexes, faces - + def invertQTransform(tr): """Return a QTransform that is the inverse of *tr*. Rasises an exception if tr is not invertible. - + Note that this function is preferred over QTransform.inverted() due to bugs in that method. (specifically, Qt has floating-point precision issues when determining whether a matrix is invertible) @@ -1949,25 +1963,25 @@ def invertQTransform(tr): arr = np.array([[tr.m11(), tr.m12(), tr.m13()], [tr.m21(), tr.m22(), tr.m23()], [tr.m31(), tr.m32(), tr.m33()]]) inv = scipy.linalg.inv(arr) return QtGui.QTransform(inv[0,0], inv[0,1], inv[0,2], inv[1,0], inv[1,1], inv[1,2], inv[2,0], inv[2,1]) - - + + def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): """ Used for examining the distribution of values in a set. Produces scattering as in beeswarm or column scatter plots. - + Given a list of x-values, construct a set of y-values such that an x,y scatter-plot will not have overlapping points (it will look similar to a histogram). """ inds = np.arange(len(data)) if shuffle: np.random.shuffle(inds) - + data = data[inds] - + if spacing is None: spacing = 2.*np.std(data)/len(data)**0.5 s2 = spacing**2 - + yvals = np.empty(len(data)) if len(data) == 0: return yvals @@ -1977,10 +1991,10 @@ def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): x0 = data[:i] # all x values already placed y0 = yvals[:i] # all y values already placed y = 0 - + dx = (x0-x)**2 # x-distance to each previous point xmask = dx < s2 # exclude anything too far away - + if xmask.sum() > 0: if bidir: dirs = [-1, 1] @@ -1990,24 +2004,24 @@ def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): for direction in dirs: y = 0 dx2 = dx[xmask] - dy = (s2 - dx2)**0.5 + dy = (s2 - dx2)**0.5 limits = np.empty((2,len(dy))) # ranges of y-values to exclude limits[0] = y0[xmask] - dy - limits[1] = y0[xmask] + dy + limits[1] = y0[xmask] + dy while True: # ignore anything below this y-value if direction > 0: mask = limits[1] >= y else: mask = limits[0] <= y - + limits2 = limits[:,mask] - + # are we inside an excluded region? mask = (limits2[0] < y) & (limits2[1] > y) if mask.sum() == 0: break - + if direction > 0: y = limits2[:,mask].max() else: @@ -2018,5 +2032,5 @@ def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): else: y = yopts[0] yvals[i] = y - + return yvals[np.argsort(inds)] ## un-shuffle values before returning From ddce17dc62164e6899040ef263ade106091b933f Mon Sep 17 00:00:00 2001 From: blink1073 Date: Sat, 23 Nov 2013 23:08:18 -0600 Subject: [PATCH 2/5] Undo remove trailing whitespace --- pyqtgraph/functions.py | 572 ++++++++++++++++++++--------------------- 1 file changed, 286 insertions(+), 286 deletions(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index df7ead47..bd2ed314 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -16,7 +16,7 @@ Colors = { 'y': (255,255,0,255), 'k': (0,0,0,255), 'w': (255,255,255,255), -} +} SI_PREFIXES = asUnicode('yzafpnµm kMGTPEZY') SI_PREFIXES_ASCII = 'yzafpnum kMGTPEZY' @@ -47,16 +47,16 @@ from . import debug def siScale(x, minVal=1e-25, allowUnicode=True): """ Return the recommended scale factor and SI prefix string for x. - + Example:: - + siScale(0.0001) # returns (1e6, 'μ') # This indicates that the number 0.0001 is best represented as 0.0001 * 1e6 = 100 μUnits """ - + if isinstance(x, decimal.Decimal): x = float(x) - + try: if np.isnan(x) or np.isinf(x): return(1, '') @@ -68,7 +68,7 @@ def siScale(x, minVal=1e-25, allowUnicode=True): x = 0 else: m = int(np.clip(np.floor(np.log(abs(x))/np.log(1000)), -9.0, 9.0)) - + if m == 0: pref = '' elif m < -8 or m > 8: @@ -79,27 +79,27 @@ def siScale(x, minVal=1e-25, allowUnicode=True): else: pref = SI_PREFIXES_ASCII[m+8] p = .001**m - - return (p, pref) + + return (p, pref) def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, allowUnicode=True): """ Return the number x formatted in engineering notation with SI prefix. - + Example:: siFormat(0.0001, suffix='V') # returns "100 μV" """ - + if space is True: space = ' ' if space is False: space = '' - - + + (p, pref) = siScale(x, minVal, allowUnicode) if not (len(pref) > 0 and pref[0] == 'e'): pref = space + pref - + if error is None: fmt = "%." + str(precision) + "g%s%s" return fmt % (x*p, pref, suffix) @@ -110,16 +110,16 @@ def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, al plusminus = " +/- " fmt = "%." + str(precision) + "g%s%s%s%s" return fmt % (x*p, pref, suffix, plusminus, siFormat(error, precision=precision, suffix=suffix, space=space, minVal=minVal)) - + def siEval(s): """ Convert a value written in SI notation to its equivalent prefixless value - + Example:: - + siEval("100 μV") # returns 0.0001 """ - + s = asUnicode(s) m = re.match(r'(-?((\d+(\.\d*)?)|(\.\d+))([eE]-?\d+)?)\s*([u' + SI_PREFIXES + r']?).*$', s) if m is None: @@ -135,35 +135,35 @@ def siEval(s): else: n = SI_PREFIXES.index(p) - 8 return v * 1000**n - + class Color(QtGui.QColor): def __init__(self, *args): QtGui.QColor.__init__(self, mkColor(*args)) - + def glColor(self): """Return (r,g,b,a) normalized for use in opengl""" return (self.red()/255., self.green()/255., self.blue()/255., self.alpha()/255.) - + def __getitem__(self, ind): return (self.red, self.green, self.blue, self.alpha)[ind]() - - + + def mkColor(*args): """ Convenience function for constructing QColor from a variety of argument types. Accepted arguments are: - + ================ ================================================ - 'c' one of: r, g, b, c, m, y, k, w + 'c' one of: r, g, b, c, m, y, k, w R, G, B, [A] integers 0-255 (R, G, B, [A]) tuple of integers 0-255 float greyscale, 0.0-1.0 int see :func:`intColor() ` (int, hues) see :func:`intColor() ` "RGB" hexadecimal strings; may begin with '#' - "RGBA" - "RRGGBB" - "RRGGBBAA" + "RGBA" + "RRGGBB" + "RRGGBBAA" QColor QColor instance; makes a copy. ================ ================================================ """ @@ -221,7 +221,7 @@ def mkColor(*args): (r, g, b, a) = args else: raise Exception(err) - + args = [r,g,b,a] args = [0 if np.isnan(a) or np.isinf(a) else a for a in args] args = list(map(int, args)) @@ -250,25 +250,25 @@ def mkBrush(*args, **kwds): def mkPen(*args, **kargs): """ - Convenience function for constructing QPen. - + Convenience function for constructing QPen. + Examples:: - + mkPen(color) mkPen(color, width=2) mkPen(cosmetic=False, width=4.5, color='r') mkPen({'color': "FF0", width: 2}) mkPen(None) # (no pen) - + In these examples, *color* may be replaced with any arguments accepted by :func:`mkColor() ` """ - + color = kargs.get('color', None) width = kargs.get('width', 1) style = kargs.get('style', None) dash = kargs.get('dash', None) cosmetic = kargs.get('cosmetic', True) hsv = kargs.get('hsv', None) - + if len(args) == 1: arg = args[0] if isinstance(arg, dict): @@ -281,14 +281,14 @@ def mkPen(*args, **kargs): color = arg if len(args) > 1: color = args - + if color is None: color = mkColor(200, 200, 200) if hsv is not None: color = hsvColor(*hsv) else: color = mkColor(color) - + pen = QtGui.QPen(QtGui.QBrush(color), width) pen.setCosmetic(cosmetic) if style is not None: @@ -303,7 +303,7 @@ def hsvColor(hue, sat=1.0, val=1.0, alpha=1.0): c.setHsvF(hue, sat, val, alpha) return c - + def colorTuple(c): """Return a tuple (R,G,B,A) from a QColor""" return (c.red(), c.green(), c.blue(), c.alpha()) @@ -315,10 +315,10 @@ def colorStr(c): def intColor(index, hues=9, values=1, maxValue=255, minValue=150, maxHue=360, minHue=0, sat=255, alpha=255, **kargs): """ Creates a QColor from a single index. Useful for stepping through a predefined list of colors. - + The argument *index* determines which color from the set will be returned. All other arguments determine what the set of predefined colors will be - - Colors are chosen by cycling across hues while varying the value (brightness). + + Colors are chosen by cycling across hues while varying the value (brightness). By default, this selects from a list of 9 hues.""" hues = int(hues) values = int(values) @@ -330,7 +330,7 @@ def intColor(index, hues=9, values=1, maxValue=255, minValue=150, maxHue=360, mi else: v = maxValue h = minHue + (indh * (maxHue-minHue)) / hues - + c = QtGui.QColor() c.setHsv(h, sat, v) c.setAlpha(alpha) @@ -344,7 +344,7 @@ def glColor(*args, **kargs): c = mkColor(*args, **kargs) return (c.red()/255., c.green()/255., c.blue()/255., c.alpha()/255.) - + def makeArrowPath(headLen=20, tipAngle=20, tailLen=20, tailWidth=3, baseAngle=0): """ @@ -370,25 +370,25 @@ def makeArrowPath(headLen=20, tipAngle=20, tailLen=20, tailWidth=3, baseAngle=0) path.lineTo(headLen, headWidth) path.lineTo(0,0) return path - - - + + + def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, **kargs): """ Take a slice of any orientation through an array. This is useful for extracting sections of multi-dimensional arrays such as MRI images for viewing as 1D or 2D data. - + The slicing axes are aribtrary; they do not need to be orthogonal to the original data or even to each other. It is possible to use this function to extract arbitrary linear, rectangular, or parallelepiped shapes from within larger datasets. The original data is interpolated onto a new array of coordinates using scipy.ndimage.map_coordinates (see the scipy documentation for more information about this). - + For a graphical interface to this function, see :func:`ROI.getArrayRegion ` - + ============== ==================================================================================================== Arguments: *data* (ndarray) the original dataset *shape* the shape of the slice to take (Note the return value may have more dimensions than len(shape)) *origin* the location in the original dataset that will become the origin of the sliced data. - *vectors* list of unit vectors which point in the direction of the slice axes. Each vector must have the same - length as *axes*. If the vectors are not unit length, the result will be scaled relative to the - original data. If the vectors are not orthogonal, the result will be sheared relative to the + *vectors* list of unit vectors which point in the direction of the slice axes. Each vector must have the same + length as *axes*. If the vectors are not unit length, the result will be scaled relative to the + original data. If the vectors are not orthogonal, the result will be sheared relative to the original data. *axes* The axes in the original dataset which correspond to the slice *vectors* *order* The order of spline interpolation. Default is 1 (linear). See scipy.ndimage.map_coordinates @@ -398,23 +398,23 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, *All extra keyword arguments are passed to scipy.ndimage.map_coordinates.* -------------------------------------------------------------------------------------------------------------------- ============== ==================================================================================================== - - Note the following must be true: - - | len(shape) == len(vectors) + + Note the following must be true: + + | len(shape) == len(vectors) | len(origin) == len(axes) == len(vectors[i]) - + Example: start with a 4D fMRI data set, take a diagonal-planar slice out of the last 3 axes - + * data = array with dims (time, x, y, z) = (100, 40, 40, 40) - * The plane to pull out is perpendicular to the vector (x,y,z) = (1,1,1) + * The plane to pull out is perpendicular to the vector (x,y,z) = (1,1,1) * The origin of the slice will be at (x,y,z) = (40, 0, 0) * We will slice a 20x20 plane from each timepoint, giving a final shape (100, 20, 20) - + The call for this example would look like:: - + affineSlice(data, shape=(20,20), origin=(40,0,0), vectors=((-1, 1, 0), (-1, 0, 1)), axes=(1,2,3)) - + """ if not HAVE_SCIPY: raise Exception("This function requires the scipy library, but it does not appear to be importable.") @@ -427,7 +427,7 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, for v in vectors: if len(v) != len(axes): raise Exception("each vector must be same length as axes.") - + shape = list(map(np.ceil, shape)) ## transpose data so slice axes come first @@ -438,7 +438,7 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, data = data.transpose(tr1) #print "tr1:", tr1 ## dims are now [(slice axes), (other axes)] - + ## make sure vectors are arrays if not isinstance(vectors, np.ndarray): @@ -446,8 +446,8 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, if not isinstance(origin, np.ndarray): origin = np.array(origin) origin.shape = (len(axes),) + (1,)*len(shape) - - ## Build array of sample locations. + + ## Build array of sample locations. grid = np.mgrid[tuple([slice(0,x) for x in shape])] ## mesh grid of indexes #print shape, grid.shape x = (grid[np.newaxis,...] * vectors.transpose()[(Ellipsis,) + (np.newaxis,)*len(shape)]).sum(axis=1) ## magic @@ -461,7 +461,7 @@ def affineSlice(data, shape, origin, vectors, axes, order=1, returnCoords=False, ind = (Ellipsis,) + inds #print data[ind].shape, x.shape, output[ind].shape, output.shape output[ind] = scipy.ndimage.map_coordinates(data[ind], x, order=order, **kargs) - + tr = list(range(output.ndim)) trb = [] for i in range(min(axes)): @@ -481,20 +481,20 @@ def transformToArray(tr): """ Given a QTransform, return a 3x3 numpy array. Given a QMatrix4x4, return a 4x4 numpy array. - + Example: map an array of x,y coordinates through a transform:: - + ## coordinates to map are (1,5), (2,6), (3,7), and (4,8) coords = np.array([[1,2,3,4], [5,6,7,8], [1,1,1,1]]) # the extra '1' coordinate is needed for translation to work - + ## Make an example transform tr = QtGui.QTransform() tr.translate(3,4) tr.scale(2, 0.1) - + ## convert to array m = pg.transformToArray()[:2] # ignore the perspective portion of the transformation - + ## map coordinates through transform mapped = np.dot(m, coords) """ @@ -515,24 +515,24 @@ def transformCoordinates(tr, coords, transpose=False): Map a set of 2D or 3D coordinates through a QTransform or QMatrix4x4. The shape of coords must be (2,...) or (3,...) The mapping will _ignore_ any perspective transformations. - + For coordinate arrays with ndim=2, this is basically equivalent to matrix multiplication. - Most arrays, however, prefer to put the coordinate axis at the end (eg. shape=(...,3)). To + Most arrays, however, prefer to put the coordinate axis at the end (eg. shape=(...,3)). To allow this, use transpose=True. - + """ - + if transpose: ## move last axis to beginning. This transposition will be reversed before returning the mapped coordinates. coords = coords.transpose((coords.ndim-1,) + tuple(range(0,coords.ndim-1))) - + nd = coords.shape[0] if isinstance(tr, np.ndarray): m = tr else: m = transformToArray(tr) m = m[:m.shape[0]-1] # remove perspective - + ## If coords are 3D and tr is 2D, assume no change for Z axis if m.shape == (2,3) and nd == 3: m2 = np.zeros((3,4)) @@ -540,34 +540,34 @@ def transformCoordinates(tr, coords, transpose=False): m2[:2, 3] = m[:2,2] m2[2,2] = 1 m = m2 - + ## if coords are 2D and tr is 3D, ignore Z axis if m.shape == (3,4) and nd == 2: m2 = np.empty((2,3)) m2[:,:2] = m[:2,:2] m2[:,2] = m[:2,3] m = m2 - + ## reshape tr and coords to prepare for multiplication m = m.reshape(m.shape + (1,)*(coords.ndim-1)) coords = coords[np.newaxis, ...] - - # separate scale/rotate and translation - translate = m[:,-1] + + # separate scale/rotate and translation + translate = m[:,-1] m = m[:, :-1] - + ## map coordinates and return mapped = (m*coords).sum(axis=1) ## apply scale/rotate mapped += translate - + if transpose: ## move first axis to end. mapped = mapped.transpose(tuple(range(1,mapped.ndim)) + (0,)) return mapped + + - - - + def solve3DTransform(points1, points2): """ Find a 3D transformation matrix that maps points1 onto points2. @@ -577,21 +577,21 @@ def solve3DTransform(points1, points2): raise Exception("This function depends on the scipy library, but it does not appear to be importable.") A = np.array([[points1[i].x(), points1[i].y(), points1[i].z(), 1] for i in range(4)]) B = np.array([[points2[i].x(), points2[i].y(), points2[i].z(), 1] for i in range(4)]) - + ## solve 3 sets of linear equations to determine transformation matrix elements matrix = np.zeros((4,4)) for i in range(3): matrix[i] = scipy.linalg.solve(A, B[:,i]) ## solve Ax = B; x is one row of the desired transformation matrix - + return matrix - + def solveBilinearTransform(points1, points2): """ Find a bilinear transformation matrix (2x4) that maps points1 onto points2. Points must be specified as a list of 4 Vector, Point, QPointF, etc. - + To use this matrix to map a point [x,y]:: - + mapped = np.dot(matrix, [x*y, x, y, 1]) """ if not HAVE_SCIPY: @@ -600,30 +600,30 @@ def solveBilinearTransform(points1, points2): ## B is 4 rows (points) x 2 columns (x, y) A = np.array([[points1[i].x()*points1[i].y(), points1[i].x(), points1[i].y(), 1] for i in range(4)]) B = np.array([[points2[i].x(), points2[i].y()] for i in range(4)]) - + ## solve 2 sets of linear equations to determine transformation matrix elements matrix = np.zeros((2,4)) for i in range(2): matrix[i] = scipy.linalg.solve(A, B[:,i]) ## solve Ax = B; x is one row of the desired transformation matrix - + return matrix - + def rescaleData(data, scale, offset, dtype=None): """Return data rescaled and optionally cast to a new dtype:: - + data => (data-offset) * scale - + Uses scipy.weave (if available) to improve performance. """ if dtype is None: dtype = data.dtype else: dtype = np.dtype(dtype) - + try: if not pg.getConfigOption('useWeave'): raise Exception('Weave is disabled; falling back to slower version.') - + ## require native dtype when using weave if not data.dtype.isnative: data = data.astype(data.dtype.newbyteorder('=')) @@ -631,11 +631,11 @@ def rescaleData(data, scale, offset, dtype=None): weaveDtype = dtype.newbyteorder('=') else: weaveDtype = dtype - + newData = np.empty((data.size,), dtype=weaveDtype) flat = np.ascontiguousarray(data).reshape(data.size) size = data.size - + code = """ double sc = (double)scale; double off = (double)offset; @@ -652,61 +652,61 @@ def rescaleData(data, scale, offset, dtype=None): if pg.getConfigOption('weaveDebug'): debug.printExc("Error; disabling weave.") pg.setConfigOption('useWeave', False) - + #p = np.poly1d([scale, -offset*scale]) #data = p(data).astype(dtype) d2 = data-offset d2 *= scale data = d2.astype(dtype) return data - + def applyLookupTable(data, lut): """ Uses values in *data* as indexes to select values from *lut*. The returned data has shape data.shape + lut.shape[1:] - + Uses scipy.weave to improve performance if it is available. Note: color gradient lookup tables can be generated using GradientWidget. """ if data.dtype.kind not in ('i', 'u'): data = data.astype(int) - + ## using np.take appears to be faster than even the scipy.weave method and takes care of clipping as well. - return np.take(lut, data, axis=0, mode='clip') - - ### old methods: + return np.take(lut, data, axis=0, mode='clip') + + ### old methods: #data = np.clip(data, 0, lut.shape[0]-1) - + #try: #if not USE_WEAVE: #raise Exception('Weave is disabled; falling back to slower version.') - + ### number of values to copy for each LUT lookup #if lut.ndim == 1: #ncol = 1 #else: #ncol = sum(lut.shape[1:]) - + ### output array #newData = np.empty((data.size, ncol), dtype=lut.dtype) - + ### flattened input arrays #flatData = data.flatten() #flatLut = lut.reshape((lut.shape[0], ncol)) - + #dataSize = data.size - - ### strides for accessing each item + + ### strides for accessing each item #newStride = newData.strides[0] / newData.dtype.itemsize #lutStride = flatLut.strides[0] / flatLut.dtype.itemsize #dataStride = flatData.strides[0] / flatData.dtype.itemsize - + ### strides for accessing individual values within a single LUT lookup #newColStride = newData.strides[1] / newData.dtype.itemsize #lutColStride = flatLut.strides[1] / flatLut.dtype.itemsize - + #code = """ - + #for( int i=0; i0 and max->*scale*:: - + rescaled = (clip(data, min, max) - min) * (*scale* / (max - min)) - + It is also possible to use a 2D (N,2) array of values for levels. In this case, - it is assumed that each pair of min,max values in the levels array should be - applied to a different subset of the input data (for example, the input data may - already have RGB values and the levels are used to independently scale each + it is assumed that each pair of min,max values in the levels array should be + applied to a different subset of the input data (for example, the input data may + already have RGB values and the levels are used to independently scale each channel). The use of this feature requires that levels.shape[0] == data.shape[-1]. - scale The maximum value to which data will be rescaled before being passed through the + scale The maximum value to which data will be rescaled before being passed through the lookup table (or returned if there is no lookup table). By default this will be set to the length of the lookup table, or 256 is no lookup table is provided. For OpenGL color specifications (as in GLColor4f) use scale=1.0 lut Optional lookup table (array with dtype=ubyte). Values in data will be converted to color by indexing directly from lut. The output data shape will be input.shape + lut.shape[1:]. - + Note: the output of makeARGB will have the same dtype as the lookup table, so for conversion to QImage, the dtype must be ubyte. - + Lookup tables can be built using GradientWidget. - useRGBA If True, the data is returned in RGBA order (useful for building OpenGL textures). - The default is False, which returns in ARGB order for use with QImage - (Note that 'ARGB' is a term used by the Qt documentation; the _actual_ order + useRGBA If True, the data is returned in RGBA order (useful for building OpenGL textures). + The default is False, which returns in ARGB order for use with QImage + (Note that 'ARGB' is a term used by the Qt documentation; the _actual_ order is BGRA). transpose Whether to pre-transpose the data in preparation for use in Qt ============ ================================================================================== """ prof = debug.Profiler('functions.makeARGB', disabled=True) - + if lut is not None and not isinstance(lut, np.ndarray): lut = np.array(lut) if levels is not None and not isinstance(levels, np.ndarray): levels = np.array(levels) - + ## sanity checks #if data.ndim == 3: #if data.shape[2] not in (3,4): @@ -792,7 +792,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F ##raise Exception("can not use lookup table with 3D data") #elif data.ndim != 2: #raise Exception("data must be 2D or 3D") - + #if lut is not None: ##if lut.ndim == 2: ##if lut.shape[1] : @@ -801,7 +801,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F ##raise Exception("lut must be 1D or 2D") #if lut.dtype != np.ubyte: #raise Exception('lookup table must have dtype=ubyte (got %s instead)' % str(lut.dtype)) - + if levels is not None: if levels.ndim == 1: @@ -825,7 +825,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F #raise Exception('Can not use 2D levels and lookup table together.') #else: #raise Exception("Levels must have shape (2,) or (3,2) or (4,2)") - + prof.mark('1') if scale is None: @@ -836,7 +836,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F ## Apply levels if given if levels is not None: - + if isinstance(levels, np.ndarray) and levels.ndim == 2: ## we are going to rescale each channel independently if levels.shape[0] != data.shape[-1]: @@ -877,7 +877,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F order = [0,1,2,3] ## array comes out RGBA else: order = [2,1,0,3] ## for some reason, the colors line up as BGR in the final image. - + if data.ndim == 2: for i in range(3): if transpose: @@ -895,21 +895,21 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F if transpose: imgData[..., order[i]] = data[..., order[i]].T else: - imgData[..., order[i]] = data[..., order[i]] - + imgData[..., order[i]] = data[..., order[i]] + prof.mark('5') - + if data.ndim == 2 or data.shape[2] == 3: alpha = False imgData[..., 3] = 255 else: alpha = True - + prof.mark('6') - + prof.finish() return imgData, alpha - + def makeQImage(imgData, alpha=None, copy=True, transpose=True): """ @@ -918,11 +918,11 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): be reflected in the image. The image will be given a 'data' attribute pointing to the array which shares its data to prevent python freeing that memory while the image is in use. - + =========== =================================================================== Arguments: - imgData Array of data to convert. Must have shape (width, height, 3 or 4) - and dtype=ubyte. The order of values in the 3rd axis must be + imgData Array of data to convert. Must have shape (width, height, 3 or 4) + and dtype=ubyte. The order of values in the 3rd axis must be (b, g, r, a). alpha If True, the QImage returned will have format ARGB32. If False, the format will be RGB32. By default, _alpha_ is True if @@ -931,19 +931,19 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): If False, the new QImage points directly to the data in the array. Note that the array must be contiguous for this to work (see numpy.ascontiguousarray). - transpose If True (the default), the array x/y axes are transposed before - creating the image. Note that Qt expects the axes to be in - (height, width) order whereas pyqtgraph usually prefers the + transpose If True (the default), the array x/y axes are transposed before + creating the image. Note that Qt expects the axes to be in + (height, width) order whereas pyqtgraph usually prefers the opposite. - =========== =================================================================== + =========== =================================================================== """ ## create QImage from buffer prof = debug.Profiler('functions.makeQImage', disabled=True) - + ## If we didn't explicitly specify alpha, check the array shape. if alpha is None: alpha = (imgData.shape[2] == 4) - + copied = False if imgData.shape[2] == 3: ## need to make alpha channel (even if alpha==False; QImage requires 32 bpp) if copy is True: @@ -954,25 +954,25 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): copied = True else: raise Exception('Array has only 3 channels; cannot make QImage without copying.') - + if alpha: imgFormat = QtGui.QImage.Format_ARGB32 else: imgFormat = QtGui.QImage.Format_RGB32 - + if transpose: imgData = imgData.transpose((1, 0, 2)) ## QImage expects the row/column order to be opposite - + if not imgData.flags['C_CONTIGUOUS']: if copy is False: extra = ' (try setting transpose=False)' if transpose else '' raise Exception('Array is not contiguous; cannot make QImage without copying.'+extra) imgData = np.ascontiguousarray(imgData) copied = True - + if copy is True and copied is False: imgData = imgData.copy() - + if USE_PYSIDE: ch = ctypes.c_char.from_buffer(imgData, 0) img = QtGui.QImage(ch, imgData.shape[1], imgData.shape[0], imgFormat) @@ -983,7 +983,7 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): #addr = ctypes.c_char.from_buffer(imgData, 0) #try: #img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) - #except TypeError: + #except TypeError: #addr = ctypes.addressof(addr) #img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) try: @@ -995,14 +995,14 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): else: # mutable, but leaks memory img = QtGui.QImage(memoryview(imgData), imgData.shape[1], imgData.shape[0], imgFormat) - + img.data = imgData return img #try: #buf = imgData.data #except AttributeError: ## happens when image data is non-contiguous #buf = imgData.data - + #prof.mark('1') #qimage = QtGui.QImage(buf, imgData.shape[1], imgData.shape[0], imgFormat) #prof.mark('2') @@ -1013,7 +1013,7 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): def imageToArray(img, copy=False, transpose=True): """ Convert a QImage into numpy array. The image must have format RGB32, ARGB32, or ARGB32_Premultiplied. - By default, the image is not copied; changes made to the array will appear in the QImage as well (beware: if + By default, the image is not copied; changes made to the array will appear in the QImage as well (beware: if the QImage is collected before the array, there may be trouble). The array will have shape (width, height, (b,g,r,a)). """ @@ -1024,34 +1024,34 @@ def imageToArray(img, copy=False, transpose=True): else: ptr.setsize(img.byteCount()) arr = np.asarray(ptr) - + if fmt == img.Format_RGB32: arr = arr.reshape(img.height(), img.width(), 3) elif fmt == img.Format_ARGB32 or fmt == img.Format_ARGB32_Premultiplied: arr = arr.reshape(img.height(), img.width(), 4) - + if copy: arr = arr.copy() - + if transpose: return arr.transpose((1,0,2)) else: return arr - + def colorToAlpha(data, color): """ - Given an RGBA image in *data*, convert *color* to be transparent. - *data* must be an array (w, h, 3 or 4) of ubyte values and *color* must be + Given an RGBA image in *data*, convert *color* to be transparent. + *data* must be an array (w, h, 3 or 4) of ubyte values and *color* must be an array (3) of ubyte values. This is particularly useful for use with images that have a black or white background. - + Algorithm is taken from Gimp's color-to-alpha function in plug-ins/common/colortoalpha.c Credit: /* * Color To Alpha plug-in v1.0 by Seth Burgess, sjburges@gimp.org 1999/05/14 * with algorithm by clahey */ - + """ data = data.astype(float) if data.shape[-1] == 3: ## add alpha channel if needed @@ -1059,11 +1059,11 @@ def colorToAlpha(data, color): d2[...,:3] = data d2[...,3] = 255 data = d2 - + color = color.astype(float) alpha = np.zeros(data.shape[:2]+(3,), dtype=float) output = data.copy() - + for i in [0,1,2]: d = data[...,i] c = color[i] @@ -1071,18 +1071,18 @@ def colorToAlpha(data, color): alpha[...,i][mask] = (d[mask] - c) / (255. - c) imask = d < c alpha[...,i][imask] = (c - d[imask]) / c - + output[...,3] = alpha.max(axis=2) * 255. - + mask = output[...,3] >= 1.0 ## avoid zero division while processing alpha channel correction = 255. / output[...,3][mask] ## increase value to compensate for decreased alpha for i in [0,1,2]: output[...,i][mask] = ((output[...,i][mask]-color[i]) * correction) + color[i] output[...,3][mask] *= data[...,3][mask] / 255. ## combine computed and previous alpha values - + #raise Exception() return np.clip(output, 0, 255).astype(np.ubyte) - + def arrayToQPath(x, y, connect='all'): @@ -1184,28 +1184,28 @@ def arrayToQPath(x, y, connect='all'): #""" #Generate isosurface from volumetric data using marching tetrahedra algorithm. #See Paul Bourke, "Polygonising a Scalar Field Using Tetrahedrons" (http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/) - + #*data* 3D numpy array of scalar values #*level* The level at which to generate an isosurface #""" - + #facets = [] - + ### mark everything below the isosurface level #mask = data < level - - #### make eight sub-fields + + #### make eight sub-fields #fields = np.empty((2,2,2), dtype=object) #slices = [slice(0,-1), slice(1,None)] #for i in [0,1]: #for j in [0,1]: #for k in [0,1]: #fields[i,j,k] = mask[slices[i], slices[j], slices[k]] - - - + + + ### split each cell into 6 tetrahedra - ### these all have the same 'orienation'; points 1,2,3 circle + ### these all have the same 'orienation'; points 1,2,3 circle ### clockwise around point 0 #tetrahedra = [ #[(0,1,0), (1,1,1), (0,1,1), (1,0,1)], @@ -1215,15 +1215,15 @@ def arrayToQPath(x, y, connect='all'): #[(0,1,0), (1,0,0), (1,1,0), (1,0,1)], #[(0,1,0), (1,1,0), (1,1,1), (1,0,1)] #] - + ### each tetrahedron will be assigned an index ### which determines how to generate its facets. - ### this structure is: + ### this structure is: ### facets[index][facet1, facet2, ...] - ### where each facet is triangular and its points are each + ### where each facet is triangular and its points are each ### interpolated between two points on the tetrahedron ### facet = [(p1a, p1b), (p2a, p2b), (p3a, p3b)] - ### facet points always circle clockwise if you are looking + ### facet points always circle clockwise if you are looking ### at them from below the isosurface. #indexFacets = [ #[], ## all above @@ -1243,15 +1243,15 @@ def arrayToQPath(x, y, connect='all'): #[[(0,1), (0,3), (0,2)]], # 1,2,3 below #[] ## all below #] - + #for tet in tetrahedra: - + ### get the 4 fields for this tetrahedron #tetFields = [fields[c] for c in tet] - + ### generate an index for each grid cell #index = tetFields[0] + tetFields[1]*2 + tetFields[2]*4 + tetFields[3]*8 - + ### add facets #for i in xrange(index.shape[0]): # data x-axis #for j in xrange(index.shape[1]): # data y-axis @@ -1265,32 +1265,32 @@ def arrayToQPath(x, y, connect='all'): #facets.append(pts) #return facets - + def isocurve(data, level, connected=False, extendToEdge=False, path=False): """ Generate isocurve from 2D data using marching squares algorithm. - + ============= ========================================================= Arguments data 2D numpy array of scalar values level The level at which to generate an isosurface connected If False, return a single long list of point pairs - If True, return multiple long lists of connected point - locations. (This is slower but better for drawing + If True, return multiple long lists of connected point + locations. (This is slower but better for drawing continuous lines) - extendToEdge If True, extend the curves to reach the exact edges of - the data. - path if True, return a QPainterPath rather than a list of + extendToEdge If True, extend the curves to reach the exact edges of + the data. + path if True, return a QPainterPath rather than a list of vertex coordinates. This forces connected=True. ============= ========================================================= - + This function is SLOW; plenty of room for optimization here. - """ - + """ + if path is True: connected = True - + if extendToEdge: d2 = np.empty((data.shape[0]+2, data.shape[1]+2), dtype=data.dtype) d2[1:-1, 1:-1] = data @@ -1303,7 +1303,7 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): d2[-1,0] = d2[-1,1] d2[-1,-1] = d2[-1,-2] data = d2 - + sideTable = [ [], [0,1], @@ -1322,20 +1322,20 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): [0,1], [] ] - + edgeKey=[ [(0,1), (0,0)], [(0,0), (1,0)], [(1,0), (1,1)], [(1,1), (0,1)] ] - - + + lines = [] - + ## mark everything below the isosurface level mask = data < level - + ### make four sub-fields and compute indexes for grid cells index = np.zeros([x-1 for x in data.shape], dtype=np.ubyte) fields = np.empty((2,2), dtype=object) @@ -1349,10 +1349,10 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): index += fields[i,j] * 2**vertIndex #print index #print index - + ## add lines for i in range(index.shape[0]): # data x-axis - for j in range(index.shape[1]): # data y-axis + for j in range(index.shape[1]): # data y-axis sides = sideTable[index[i,j]] for l in range(0, len(sides), 2): ## faces for this grid cell edges = sides[l:l+2] @@ -1365,26 +1365,26 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): f = (level-v1) / (v2-v1) fi = 1.0 - f p = ( ## interpolate between corners - p1[0]*fi + p2[0]*f + i + 0.5, + p1[0]*fi + p2[0]*f + i + 0.5, p1[1]*fi + p2[1]*f + j + 0.5 ) if extendToEdge: ## check bounds p = ( min(data.shape[0]-2, max(0, p[0]-1)), - min(data.shape[1]-2, max(0, p[1]-1)), + min(data.shape[1]-2, max(0, p[1]-1)), ) if connected: gridKey = i + (1 if edges[m]==2 else 0), j + (1 if edges[m]==3 else 0), edges[m]%2 pts.append((p, gridKey)) ## give the actual position and a key identifying the grid location (for connecting segments) else: pts.append(p) - + lines.append(pts) if not connected: return lines - + ## turn disjoint list of segments into continuous lines #lines = [[2,5], [5,4], [3,4], [1,3], [6,7], [7,8], [8,6], [11,12], [12,15], [11,13], [13,14]] @@ -1411,9 +1411,9 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): while True: if x == chain[-1][1]: break ## nothing left to do on this chain - + x = chain[-1][1] - if x == k: + if x == k: break ## chain has looped; we're done and can ignore the opposite chain y = chain[-2][1] connects = points[x] @@ -1426,9 +1426,9 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): if chain[0][1] == chain[-1][1]: # looped chain; no need to continue the other direction chains.pop() break + - - ## extract point locations + ## extract point locations lines = [] for chain in points.values(): if len(chain) == 2: @@ -1436,25 +1436,25 @@ def isocurve(data, level, connected=False, extendToEdge=False, path=False): else: chain = chain[0] lines.append([p[0] for p in chain]) - + if not path: return lines ## a list of pairs of points - + path = QtGui.QPainterPath() for line in lines: path.moveTo(*line[0]) for p in line[1:]: path.lineTo(*p) - + return path - - + + def traceImage(image, values, smooth=0.5): """ Convert an image to a set of QPainterPath curves. One curve will be generated for each item in *values*; each curve outlines the area of the image that is closer to its value than to any others. - + If image is RGB or RGBA, then the shape of values should be (nvals, 3/4) The parameter *smooth* is expressed in pixels. """ @@ -1466,11 +1466,11 @@ def traceImage(image, values, smooth=0.5): diff = np.abs(image-values) if values.ndim == 4: diff = diff.sum(axis=2) - + labels = np.argmin(diff, axis=2) - + paths = [] - for i in range(diff.shape[-1]): + for i in range(diff.shape[-1]): d = (labels==i).astype(float) d = ndi.gaussian_filter(d, (smooth, smooth)) lines = isocurve(d, 0.5, connected=True, extendToEdge=True) @@ -1479,31 +1479,31 @@ def traceImage(image, values, smooth=0.5): path.moveTo(*line[0]) for p in line[1:]: path.lineTo(*p) - + paths.append(path) return paths - - - + + + IsosurfaceDataCache = None def isosurface(data, level): """ Generate isosurface from volumetric data using marching cubes algorithm. - See Paul Bourke, "Polygonising a Scalar Field" + See Paul Bourke, "Polygonising a Scalar Field" (http://paulbourke.net/geometry/polygonise/) - + *data* 3D numpy array of scalar values *level* The level at which to generate an isosurface - - Returns an array of vertex coordinates (Nv, 3) and an array of - per-face vertex indexes (Nf, 3) + + Returns an array of vertex coordinates (Nv, 3) and an array of + per-face vertex indexes (Nf, 3) """ ## For improvement, see: - ## + ## ## Efficient implementation of Marching Cubes' cases with topological guarantees. ## Thomas Lewiner, Helio Lopes, Antonio Wilson Vieira and Geovan Tavares. ## Journal of Graphics Tools 8(2): pp. 1-15 (december 2003) - + ## Precompute lookup tables on the first run global IsosurfaceDataCache if IsosurfaceDataCache is None: @@ -1544,7 +1544,7 @@ def isosurface(data, level): 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190, 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0 ], dtype=np.uint16) - + ## Table of triangles to use for filling each grid cell. ## Each set of three integers tells us which three edges to ## draw a triangle between. @@ -1806,9 +1806,9 @@ def isosurface(data, level): [0, 9, 1], [0, 3, 8], [] - ] + ] edgeShifts = np.array([ ## maps edge ID (0-11) to (x,y,z) cell offset and edge ID (0-2) - [0, 0, 0, 0], + [0, 0, 0, 0], [1, 0, 0, 1], [0, 1, 0, 0], [0, 0, 0, 1], @@ -1831,23 +1831,23 @@ def isosurface(data, level): faceTableI[faceTableInds[:,0]] = np.array([triTable[j] for j in faceTableInds]) faceTableI = faceTableI.reshape((len(triTable), i, 3)) faceShiftTables.append(edgeShifts[faceTableI]) - + ## Let's try something different: #faceTable = np.empty((256, 5, 3, 4), dtype=np.ubyte) # (grid cell index, faces, vertexes, edge lookup) #for i,f in enumerate(triTable): #f = np.array(f + [12] * (15-len(f))).reshape(5,3) #faceTable[i] = edgeShifts[f] - - + + IsosurfaceDataCache = (faceShiftTables, edgeShifts, edgeTable, nTableFaces) else: faceShiftTables, edgeShifts, edgeTable, nTableFaces = IsosurfaceDataCache - + ## mark everything below the isosurface level mask = data < level - + ### make eight sub-fields and compute indexes for grid cells index = np.zeros([x-1 for x in data.shape], dtype=np.ubyte) fields = np.empty((2,2,2), dtype=object) @@ -1858,23 +1858,23 @@ def isosurface(data, level): fields[i,j,k] = mask[slices[i], slices[j], slices[k]] vertIndex = i - 2*j*i + 3*j + 4*k ## this is just to match Bourk's vertex numbering scheme index += fields[i,j,k] * 2**vertIndex - + ### Generate table of edges that have been cut cutEdges = np.zeros([x+1 for x in index.shape]+[3], dtype=np.uint32) edges = edgeTable[index] - for i, shift in enumerate(edgeShifts[:12]): + for i, shift in enumerate(edgeShifts[:12]): slices = [slice(shift[j],cutEdges.shape[j]+(shift[j]-1)) for j in range(3)] cutEdges[slices[0], slices[1], slices[2], shift[3]] += edges & 2**i - + ## for each cut edge, interpolate to see where exactly the edge is cut and generate vertex positions m = cutEdges > 0 vertexInds = np.argwhere(m) ## argwhere is slow! vertexes = vertexInds[:,:3].astype(np.float32) dataFlat = data.reshape(data.shape[0]*data.shape[1]*data.shape[2]) - + ## re-use the cutEdges array as a lookup table for vertex IDs cutEdges[vertexInds[:,0], vertexInds[:,1], vertexInds[:,2], vertexInds[:,3]] = np.arange(vertexInds.shape[0]) - + for i in [0,1,2]: vim = vertexInds[:,3] == i vi = vertexInds[vim, :3] @@ -1882,9 +1882,9 @@ def isosurface(data, level): v1 = dataFlat[viFlat] v2 = dataFlat[viFlat + data.strides[i]//data.itemsize] vertexes[vim,i] += (level-v1) / (v2-v1) - - ### compute the set of vertex indexes for each face. - + + ### compute the set of vertex indexes for each face. + ## This works, but runs a bit slower. #cells = np.argwhere((index != 0) & (index != 255)) ## all cells with at least one face #cellInds = index[cells[:,0], cells[:,1], cells[:,2]] @@ -1893,9 +1893,9 @@ def isosurface(data, level): #verts[...,:3] += cells[:,np.newaxis,np.newaxis,:] ## we now have indexes into cutEdges #verts = verts[mask] #faces = cutEdges[verts[...,0], verts[...,1], verts[...,2], verts[...,3]] ## and these are the vertex indexes we want. - - - ## To allow this to be vectorized efficiently, we count the number of faces in each + + + ## To allow this to be vectorized efficiently, we count the number of faces in each ## grid cell and handle each group of cells with the same number together. ## determine how many faces to assign to each grid cell nFaces = nTableFaces[index] @@ -1904,7 +1904,7 @@ def isosurface(data, level): ptr = 0 #import debug #p = debug.Profiler('isosurface', disabled=False) - + ## this helps speed up an indexing operation later on cs = np.array(cutEdges.strides)//cutEdges.itemsize cutEdges = cutEdges.flatten() @@ -1923,14 +1923,14 @@ def isosurface(data, level): #cellInds = index[(cells*ins[np.newaxis,:]).sum(axis=1)] cellInds = index[cells[:,0], cells[:,1], cells[:,2]] ## index values of cells to process for this round #p.mark('3') - + ### expensive: verts = faceShiftTables[i][cellInds] #p.mark('4') verts[...,:3] += cells[:,np.newaxis,np.newaxis,:] ## we now have indexes into cutEdges verts = verts.reshape((verts.shape[0]*i,)+verts.shape[2:]) #p.mark('5') - + ### expensive: #print verts.shape verts = (verts * cs[np.newaxis, np.newaxis, :]).sum(axis=2) @@ -1942,15 +1942,15 @@ def isosurface(data, level): faces[ptr:ptr+nv] = vertInds #.reshape((nv, 3)) #p.mark('8') ptr += nv - + return vertexes, faces - + def invertQTransform(tr): """Return a QTransform that is the inverse of *tr*. Rasises an exception if tr is not invertible. - + Note that this function is preferred over QTransform.inverted() due to bugs in that method. (specifically, Qt has floating-point precision issues when determining whether a matrix is invertible) @@ -1963,25 +1963,25 @@ def invertQTransform(tr): arr = np.array([[tr.m11(), tr.m12(), tr.m13()], [tr.m21(), tr.m22(), tr.m23()], [tr.m31(), tr.m32(), tr.m33()]]) inv = scipy.linalg.inv(arr) return QtGui.QTransform(inv[0,0], inv[0,1], inv[0,2], inv[1,0], inv[1,1], inv[1,2], inv[2,0], inv[2,1]) - - + + def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): """ Used for examining the distribution of values in a set. Produces scattering as in beeswarm or column scatter plots. - + Given a list of x-values, construct a set of y-values such that an x,y scatter-plot will not have overlapping points (it will look similar to a histogram). """ inds = np.arange(len(data)) if shuffle: np.random.shuffle(inds) - + data = data[inds] - + if spacing is None: spacing = 2.*np.std(data)/len(data)**0.5 s2 = spacing**2 - + yvals = np.empty(len(data)) if len(data) == 0: return yvals @@ -1991,10 +1991,10 @@ def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): x0 = data[:i] # all x values already placed y0 = yvals[:i] # all y values already placed y = 0 - + dx = (x0-x)**2 # x-distance to each previous point xmask = dx < s2 # exclude anything too far away - + if xmask.sum() > 0: if bidir: dirs = [-1, 1] @@ -2004,24 +2004,24 @@ def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): for direction in dirs: y = 0 dx2 = dx[xmask] - dy = (s2 - dx2)**0.5 + dy = (s2 - dx2)**0.5 limits = np.empty((2,len(dy))) # ranges of y-values to exclude limits[0] = y0[xmask] - dy - limits[1] = y0[xmask] + dy + limits[1] = y0[xmask] + dy while True: # ignore anything below this y-value if direction > 0: mask = limits[1] >= y else: mask = limits[0] <= y - + limits2 = limits[:,mask] - + # are we inside an excluded region? mask = (limits2[0] < y) & (limits2[1] > y) if mask.sum() == 0: break - + if direction > 0: y = limits2[:,mask].max() else: @@ -2032,5 +2032,5 @@ def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): else: y = yopts[0] yvals[i] = y - + return yvals[np.argsort(inds)] ## un-shuffle values before returning From a08b28c958e8b36a5476e63081230266297fb6ab Mon Sep 17 00:00:00 2001 From: blink1073 Date: Sun, 24 Nov 2013 15:50:28 -0600 Subject: [PATCH 3/5] Simplify to take transpose logic out of makeARGB function --- pyqtgraph/functions.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index bd2ed314..b1934d0a 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -733,7 +733,7 @@ def makeRGBA(*args, **kwds): kwds['useRGBA'] = True return makeARGB(*args, **kwds) -def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=False): +def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): """ Convert an array of values into an ARGB array suitable for building QImages, OpenGL textures, etc. @@ -774,7 +774,6 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F The default is False, which returns in ARGB order for use with QImage (Note that 'ARGB' is a term used by the Qt documentation; the _actual_ order is BGRA). - transpose Whether to pre-transpose the data in preparation for use in Qt ============ ================================================================================== """ prof = debug.Profiler('functions.makeARGB', disabled=True) @@ -866,10 +865,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F ## copy data into ARGB ordered array - if transpose: - imgData = np.empty((data.shape[1], data.shape[0], 4), dtype=np.ubyte) - else: - imgData = np.empty(data.shape[:2]+(4,), dtype=np.ubyte) + imgData = np.empty(data.shape[:2]+(4,), dtype=np.ubyte) prof.mark('4') @@ -880,22 +876,13 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False, transpose=F if data.ndim == 2: for i in range(3): - if transpose: - imgData[..., i] = data.T - else: - imgData[..., i] = data + imgData[..., i] = data elif data.shape[2] == 1: for i in range(3): - if transpose: - imgData[..., i] = data[..., 0].T - else: - imgData[..., i] = data[..., 0] + imgData[..., i] = data[..., 0] else: for i in range(0, data.shape[2]): - if transpose: - imgData[..., order[i]] = data[..., order[i]].T - else: - imgData[..., order[i]] = data[..., order[i]] + imgData[..., order[i]] = data[..., order[i]] prof.mark('5') From bd2330af9f4c38d7f50818f797d95ce874a318f0 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Sun, 24 Nov 2013 20:45:10 -0500 Subject: [PATCH 4/5] ImageItem performance boost by avoiding makeQImage(transpose=True) --- pyqtgraph/functions.py | 3 +++ pyqtgraph/graphicsItems/ImageItem.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index b1934d0a..2930b0e7 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -875,6 +875,9 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): order = [2,1,0,3] ## for some reason, the colors line up as BGR in the final image. if data.ndim == 2: + # This is tempting: + # imgData[..., :3] = data[..., np.newaxis] + # ..but it turns out this is faster: for i in range(3): imgData[..., i] = data elif data.shape[2] == 1: diff --git a/pyqtgraph/graphicsItems/ImageItem.py b/pyqtgraph/graphicsItems/ImageItem.py index 530db7fb..82807982 100644 --- a/pyqtgraph/graphicsItems/ImageItem.py +++ b/pyqtgraph/graphicsItems/ImageItem.py @@ -260,8 +260,8 @@ class ImageItem(GraphicsObject): #print lut.shape #print self.lut - argb, alpha = fn.makeARGB(self.image, lut=lut, levels=self.levels) - self.qimage = fn.makeQImage(argb, alpha) + argb, alpha = fn.makeARGB(self.image.T, lut=lut, levels=self.levels) + self.qimage = fn.makeQImage(argb, alpha, transpose=False) prof.finish() From 71ee4deb8402f635ddb785b15adca3b77277fb73 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Sun, 24 Nov 2013 21:10:06 -0500 Subject: [PATCH 5/5] - fixed ImageItem handling of rgb images - fixed makeARGB re-ordering of color channels --- pyqtgraph/functions.py | 31 +--------------------------- pyqtgraph/graphicsItems/ImageItem.py | 2 +- 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 2930b0e7..2f10f85c 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -783,25 +783,6 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): if levels is not None and not isinstance(levels, np.ndarray): levels = np.array(levels) - ## sanity checks - #if data.ndim == 3: - #if data.shape[2] not in (3,4): - #raise Exception("data.shape[2] must be 3 or 4") - ##if lut is not None: - ##raise Exception("can not use lookup table with 3D data") - #elif data.ndim != 2: - #raise Exception("data must be 2D or 3D") - - #if lut is not None: - ##if lut.ndim == 2: - ##if lut.shape[1] : - ##raise Exception("lut.shape[1] must be 3 or 4") - ##elif lut.ndim != 1: - ##raise Exception("lut must be 1D or 2D") - #if lut.dtype != np.ubyte: - #raise Exception('lookup table must have dtype=ubyte (got %s instead)' % str(lut.dtype)) - - if levels is not None: if levels.ndim == 1: if len(levels) != 2: @@ -814,16 +795,6 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): else: print(levels) raise Exception("levels argument must be 1D or 2D.") - #levels = np.array(levels) - #if levels.shape == (2,): - #pass - #elif levels.shape in [(3,2), (4,2)]: - #if data.ndim == 3: - #raise Exception("Can not use 2D levels with 3D data.") - #if lut is not None: - #raise Exception('Can not use 2D levels and lookup table together.') - #else: - #raise Exception("Levels must have shape (2,) or (3,2) or (4,2)") prof.mark('1') @@ -885,7 +856,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): imgData[..., i] = data[..., 0] else: for i in range(0, data.shape[2]): - imgData[..., order[i]] = data[..., order[i]] + imgData[..., i] = data[..., order[i]] prof.mark('5') diff --git a/pyqtgraph/graphicsItems/ImageItem.py b/pyqtgraph/graphicsItems/ImageItem.py index 82807982..5c4a09ef 100644 --- a/pyqtgraph/graphicsItems/ImageItem.py +++ b/pyqtgraph/graphicsItems/ImageItem.py @@ -260,7 +260,7 @@ class ImageItem(GraphicsObject): #print lut.shape #print self.lut - argb, alpha = fn.makeARGB(self.image.T, lut=lut, levels=self.levels) + argb, alpha = fn.makeARGB(self.image.transpose((1, 0, 2)[:self.image.ndim]), lut=lut, levels=self.levels) self.qimage = fn.makeQImage(argb, alpha, transpose=False) prof.finish()