def gradient(self,x=None,y=None): """ Computes the gradient of the components of the spin1 field at each point :param x: optional, x positions at which to evaluate the gradient :type x: array with units :param y: optional, y positions at which to evaluate the gradient :type y: array with units :returns: the gradient of the spin1 field in array form, of shape (4,:,:) where the four components are, respectively, 1x,1y,2x,2y; the units for the finite difference are pixels """ if self.data.shape[0] > 2: raise ValueError("Gradients are nor defined yet for spin>1 fields!!") if (x is not None) and (y is not None): assert x.shape==y.shape,"x and y must have the same shape!" #x coordinates if type(x)==quantity.Quantity: assert x.unit.physical_type=="angle" j = np.mod(((x / self.resolution).decompose().value).astype(np.int32),self.data.shape[1]) else: j = np.mod((x / self.resolution.to(rad).value).astype(np.int32),self.data.shape[1]) #y coordinates if type(y)==quantity.Quantity: assert y.unit.physical_type=="angle" i = np.mod(((y / self.resolution).decompose().value).astype(np.int32),self.data.shape[1]) else: i = np.mod((y / self.resolution.to(rad).value).astype(np.int32),self.data.shape[1]) else: i = None j = None #Call the C backend grad1x,grad1y = _topology.gradient(self.data[0],j,i) grad2x,grad2y = _topology.gradient(self.data[1],j,i) #Return if (x is not None) and (y is not None): return np.array([grad1x.reshape(x.shape),grad1y.reshape(y.shape),grad2x.reshape(x.shape),grad2y.reshape(y.shape)]) else: return np.array([grad1x,grad1y,grad2x,grad2y])
def gradient(self, x=None, y=None): """ Computes the gradient of the components of the spin1 field at each point :param x: optional, x positions at which to evaluate the gradient :type x: array with units :param y: optional, y positions at which to evaluate the gradient :type y: array with units :returns: the gradient of the spin1 field in array form, of shape (4,:,:) where the four components are, respectively, 1x,1y,2x,2y; the units for the finite difference are pixels """ if self.data.shape[0] > 2: raise ValueError( "Gradients are nor defined yet for spin>1 fields!!") if (x is not None) and (y is not None): assert x.shape == y.shape, "x and y must have the same shape!" #x coordinates if type(x) == quantity.Quantity: assert x.unit.physical_type == "angle" j = np.mod( ((x / self.resolution).decompose().value).astype(np.int32), self.data.shape[1]) else: j = np.mod( (x / self.resolution.to(rad).value).astype(np.int32), self.data.shape[1]) #y coordinates if type(y) == quantity.Quantity: assert y.unit.physical_type == "angle" i = np.mod( ((y / self.resolution).decompose().value).astype(np.int32), self.data.shape[1]) else: i = np.mod( (y / self.resolution.to(rad).value).astype(np.int32), self.data.shape[1]) else: i = None j = None #Call the C backend grad1x, grad1y = _topology.gradient(self.data[0], j, i) grad2x, grad2y = _topology.gradient(self.data[1], j, i) #Return if (x is not None) and (y is not None): return np.array([ grad1x.reshape(x.shape), grad1y.reshape(y.shape), grad2x.reshape(x.shape), grad2y.reshape(y.shape) ]) else: return np.array([grad1x, grad1y, grad2x, grad2y])
def gradient(self, x=None, y=None, save=True): """ Computes the gradient of the map and sets the gradient_x,gradient_y attributes accordingly :param x: optional, x positions at which to evaluate the gradient :type x: array with units :param y: optional, y positions at which to evaluate the gradient :type y: array with units :param save: if True saves the gradient as attrubutes :type save: bool. :returns: tuple -- (gradient_x,gradient_y) >>> test_map = ConvergenceMap.load("map.fit") >>> gx,gy = test_map.gradient() """ if (x is not None) and (y is not None): assert x.shape == y.shape, "x and y must have the same shape!" #x coordinates if type(x) == quantity.Quantity: assert x.unit.physical_type == self.side_angle.unit.physical_type j = np.mod( ((x / self.resolution).decompose().value).astype(np.int32), self.data.shape[1]) else: j = np.mod( (x / self.resolution.to(rad).value).astype(np.int32), self.data.shape[1]) #y coordinates if type(y) == quantity.Quantity: assert y.unit.physical_type == self.side_angle.unit.physical_type i = np.mod( ((y / self.resolution).decompose().value).astype(np.int32), self.data.shape[0]) else: i = np.mod( (y / self.resolution.to(rad).value).astype(np.int32), self.data.shape[0]) else: i = None j = None #Call the C backend gradient_x, gradient_y = _topology.gradient(self.data, j, i) #Return the gradients if (x is not None) and (y is not None): return gradient_x.reshape(x.shape), gradient_y.reshape(x.shape) else: if save: self.gradient_x = gradient_x self.gradient_y = gradient_y return gradient_x, gradient_y