From f9a7dad5dcef18c087f3dd5b7830779ae17acf6c Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Sun, 9 Sep 2012 18:56:48 -0400 Subject: [PATCH] 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 --- flowchart/eq.py | 11 +++++++++-- graphicsItems/ROI.py | 2 ++ metaarray/MetaArray.py | 31 +++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/flowchart/eq.py b/flowchart/eq.py index f929a938..ad1e357c 100644 --- a/flowchart/eq.py +++ b/flowchart/eq.py @@ -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 - return e.all() + 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))) diff --git a/graphicsItems/ROI.py b/graphicsItems/ROI.py index 68fb65ed..a4621187 100644 --- a/graphicsItems/ROI.py +++ b/graphicsItems/ROI.py @@ -965,6 +965,8 @@ class ROI(GraphicsObject): See :func:`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'] diff --git a/metaarray/MetaArray.py b/metaarray/MetaArray.py index 9cb6d492..5076dd53 100644 --- a/metaarray/MetaArray.py +++ b/metaarray/MetaArray.py @@ -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):