def reset(self, **traits): """Creates the dataset afresh or resets existing data source.""" # First set the attributes without really doing anything since # the notification handlers are not called. self.set(trait_change_notify=False, **traits) vectors = self.vectors scalars = self.scalars x, y, z = [np.atleast_3d(a) for a in self.x, self.y, self.z] u, v, w = self.u, self.v, self.w if 'vectors' in traits: u = vectors[:, 0].ravel() v = vectors[:, 1].ravel() w = vectors[:, 2].ravel() self.set(u=u, v=v, w=w, trait_change_notify=False) else: if u is not None and len(u) > 0: #vectors = np.concatenate([u[..., np.newaxis], # v[..., np.newaxis], # w[..., np.newaxis] ], # axis=3) vectors = np.c_[u.ravel(), v.ravel(), w.ravel()].ravel() vectors.shape = (u.shape[0], u.shape[1], w.shape[2], 3) self.set(vectors=vectors, trait_change_notify=False) if vectors is not None and len(vectors) > 0 and scalars is not None: assert len(scalars) == len(vectors) if x.shape[0] <= 1: dx = 1 else: dx = x[1, 0, 0] - x[0, 0, 0] if y.shape[1] <= 1: dy = 1 else: dy = y[0, 1, 0] - y[0, 0, 0] if z.shape[2] <= 1: dz = 1 else: dz = z[0, 0, 1] - z[0, 0, 0] if self.m_data is None: ds = ArraySource(transpose_input_array=True) else: ds = self.m_data old_scalar = ds.scalar_data ds.set(vector_data=vectors, origin=[x.min(), y.min(), z.min()], spacing=[dx, dy, dz], scalar_data=scalars) if scalars is old_scalar: ds._scalar_data_changed(scalars) self.dataset = ds.image_data self.m_data = ds
def reset(self, **traits): """Creates the dataset afresh or resets existing data source.""" # First set the attributes without really doing anything since # the notification handlers are not called. self.set(trait_change_notify=False, **traits) x, y, mask = self.x, self.y, self.mask scalars = self.scalars # We may have used this without specifying x and y at all in # which case we set them from the shape of scalars. nx, ny = scalars.shape #Build X and Y from shape of Scalars if they are none if x is None and y is None: x, y = np.mgrid[-nx / 2.:nx / 2, -ny / 2.:ny / 2] if mask is not None and len(mask) > 0: scalars[mask.astype('bool')] = np.nan # The NaN trick only works with floats. scalars = scalars.astype('float') self.set(scalars=scalars, trait_change_notify=False) z = np.array([0]) self.set(x=x, y=y, z=z, trait_change_notify=False) # Do some magic to extract the first row/column, independently of # the shape of x and y x = np.atleast_2d(x.squeeze().T)[0, :].squeeze() y = np.atleast_2d(y.squeeze())[0, :].squeeze() if x.ndim == 0: dx = 1 else: dx = x[1] - x[0] if y.ndim == 0: dy = 1 else: dy = y[1] - y[0] if self.m_data is None: ds = ArraySource(transpose_input_array=True) else: ds = self.m_data old_scalar = ds.scalar_data ds.set(origin=[x.min(), y.min(), 0], spacing=[dx, dy, 1], scalar_data=scalars) if old_scalar is scalars: ds._scalar_data_changed(scalars) self.dataset = ds.image_data self.m_data = ds