Minor changes:

- ROI.getArrayRegion makes sure the ROI and image share the same scene
  - Added a few binary operator methods to metaarray 
  - Fixed flowchart/eq.py handling of metaarray objects
This commit is contained in:
Luke Campagnola 2012-09-09 18:56:48 -04:00
parent 5a4fd82cd9
commit f9a7dad5dc
3 changed files with 38 additions and 6 deletions

View File

@ -1,8 +1,12 @@
# -*- coding: utf-8 -*-
from numpy import ndarray, bool_
from metaarray import MetaArray
def eq(a, b):
"""The great missing equivalence function: Guaranteed evaluation to a single bool value."""
if a is b:
return True
try:
e = a==b
except ValueError:
@ -18,12 +22,15 @@ def eq(a, b):
return e
elif t is bool_:
return bool(e)
elif isinstance(e, ndarray):
elif isinstance(e, ndarray) or (hasattr(e, 'implements') and e.implements('MetaArray')):
try: ## disaster: if a is an empty array and b is not, then e.all() is True
if a.shape != b.shape:
return False
except:
return False
if (hasattr(e, 'implements') and e.implements('MetaArray')):
return e.asarray().all()
else:
return e.all()
else:
raise Exception("== operator returned type %s" % str(type(e)))

View File

@ -965,6 +965,8 @@ class ROI(GraphicsObject):
See :func:`getArrayRegion <pyqtgraph.ROI.getArrayRegion>` for more information.
"""
if self.scene() is not img.scene():
raise Exception("ROI and target item must be members of the same scene.")
shape = self.state['size']

View File

@ -305,14 +305,37 @@ class MetaArray(object):
#return lambda *args, **kwargs: MetaArray(getattr(a.view(ndarray), attr)(*args, **kwargs)
def __eq__(self, b):
if isinstance(b, MetaArray):
b = b.asarray()
return self._data == b
return self._binop('__eq__', b)
def __ne__(self, b):
return self._binop('__ne__', b)
#if isinstance(b, MetaArray):
#b = b.asarray()
#return self.asarray() != b
def __sub__(self, b):
return self._binop('__sub__', b)
#if isinstance(b, MetaArray):
#b = b.asarray()
#return MetaArray(self.asarray() - b, info=self.infoCopy())
def __add__(self, b):
return self._binop('__add__', b)
def __mul__(self, b):
return self._binop('__mul__', b)
def __div__(self, b):
return self._binop('__div__', b)
def _binop(self, op, b):
if isinstance(b, MetaArray):
b = b.asarray()
return self._data != b
a = self.asarray()
c = getattr(a, op)(b)
if c.shape != a.shape:
raise Exception("Binary operators with MetaArray must return an array of the same shape (this shape is %s, result shape was %s)" % (a.shape, c.shape))
return MetaArray(c, info=self.infoCopy())
def asarray(self):
if isinstance(self._data, np.ndarray):