def radial_symetry_af2(background, inds=None, tots=None, is_fft_shifted=True): if (inds is None) or (tots is None): print('initialising...') i = np.fft.fftfreq(background.shape[0]) * background.shape[0] j = np.fft.fftfreq(background.shape[1]) * background.shape[1] k = np.fft.fftfreq(background.shape[2]) * background.shape[2] i, j, k = np.meshgrid(i, j, k, indexing='ij') rs = np.rint(np.sqrt(i**2 + j**2 + k**2)).astype(np.int) if is_fft_shifted is False: rs = np.fft.fftshift(rs) rs = afnumpy.array(rs.ravel()) # store a list of indexes and totals inds = [] tots = [] for i in range(0, afnumpy.max(rs) + 1): m = (rs == i) j = afnumpy.where(m) inds.append(j) tots.append(afnumpy.float(afnumpy.sum(m))) out = background.ravel() for ind, tot in zip(inds[:5], tots[:5]): out[ind] = 2. #a = afnumpy.sum(background[ind]) #out[ind] = afnumpy.sum(background[ind]) #/ tot return out, inds, tots
def radial_symetry_af2(background, inds = None, tots = None, is_fft_shifted = True): if (inds is None) or (tots is None) : print 'initialising...' i = np.fft.fftfreq(background.shape[0]) * background.shape[0] j = np.fft.fftfreq(background.shape[1]) * background.shape[1] k = np.fft.fftfreq(background.shape[2]) * background.shape[2] i, j, k = np.meshgrid(i, j, k, indexing='ij') rs = np.rint(np.sqrt(i**2 + j**2 + k**2)).astype(np.int) if is_fft_shifted is False : rs = np.fft.fftshift(rs) rs = afnumpy.array(rs.ravel()) # store a list of indexes and totals inds = [] tots = [] for i in range(0, afnumpy.max(rs)+1): m = (rs == i) j = afnumpy.where(m) inds.append(j) tots.append(afnumpy.float(afnumpy.sum(m))) out = background.ravel() for ind, tot in zip(inds[:5], tots[:5]) : out[ind] = 2. #a = afnumpy.sum(background[ind]) #out[ind] = afnumpy.sum(background[ind]) #/ tot return out, inds, tots
def test_where(): a1 = afnumpy.array([1,2,3]) b1 = numpy.array(a1) a2 = afnumpy.array([0,2,1]) b2 = numpy.array(a2) # Test where with input as indices iassert(afnumpy.where(a2, a1, a2), numpy.where(b2, b1, b2)) # Test where with input as indices iassert(afnumpy.where(a2), numpy.where(b2)) # Test where with input as booleans iassert(afnumpy.where(a2 < 2, a1, a2), numpy.where(b2 < 2, b1, b2)) # Test where with input as booleans iassert(afnumpy.where(a2 < 2), numpy.where(b2 < 2)) # And now multidimensional a1 = afnumpy.array([[1,2,3],[4,5,6]]) b1 = numpy.array(a1) a2 = afnumpy.array([[0,2,1],[1,0,1]]) b2 = numpy.array(a2) # Test where with input as indices iassert(afnumpy.where(a2, a1, a2), numpy.where(b2, b1, b2)) # Test where with input as indices iassert(afnumpy.where(a2), numpy.where(b2)) # And now multidimensional b1 = numpy.random.random((3,3,3)) > 0.5 a1 = afnumpy.array(b1) # Test where with input as indices iassert(afnumpy.where(a1), numpy.where(b1))
def test_where(): a1 = afnumpy.array([1, 2, 3]) b1 = numpy.array(a1) a2 = afnumpy.array([0, 2, 1]) b2 = numpy.array(a2) # Test where with input as indices iassert(afnumpy.where(a2, a1, a2), numpy.where(b2, b1, b2)) # Test where with input as indices iassert(afnumpy.where(a2), numpy.where(b2)) # Test where with input as booleans iassert(afnumpy.where(a2 < 2, a1, a2), numpy.where(b2 < 2, b1, b2)) # Test where with input as booleans iassert(afnumpy.where(a2 < 2), numpy.where(b2 < 2)) # And now multidimensional a1 = afnumpy.array([[1, 2, 3], [4, 5, 6]]) b1 = numpy.array(a1) a2 = afnumpy.array([[0, 2, 1], [1, 0, 1]]) b2 = numpy.array(a2) # Test where with input as indices iassert(afnumpy.where(a2, a1, a2), numpy.where(b2, b1, b2)) # Test where with input as indices iassert(afnumpy.where(a2), numpy.where(b2)) # And now multidimensional b1 = numpy.random.random((3, 3, 3)) > 0.5 a1 = afnumpy.array(b1) # Test where with input as indices iassert(afnumpy.where(a1), numpy.where(b1))
def __setitem__(self, idx, value): try: if idx.dtype == numpy.dtype('bool') or (idx[0].dtype == 'bool' and len(idx) == 1): # Special case for boolean setitem self_flat = self.flat idx = afnumpy.where(idx.flat) self_flat[idx] = value return except AttributeError: pass except RuntimeError: # idx is all False return idx, idx_shape = indexing.__convert_dim__(self.shape, idx) if any(x is None for x in idx): # one of the indices is empty return idx = tuple(idx) if len(idx) == 0: idx = tuple([0]) if(isinstance(value, ndarray)): if(value.dtype != self.dtype): raise TypeError('left hand side must have same dtype as right hand side') value = indexing.__expand_dim__(self.shape, value, idx).d_array elif(isinstance(value, numbers.Number)): pass else: raise NotImplementedError('values must be a afnumpy.ndarray') self.d_array[idx] = value # This is a hack to be able to make it look like arrays with stride[0] > 1 can still be views # In practise right now it only applies to ndarray.real and ndarray.imag try: self._base[self._base_index] = self except AttributeError: pass
def __getitem__(self, args): if not isinstance(args, tuple): args = (args,) if len(args) == 1 and isinstance(args[0], afnumpy.ndarray) and args[0].dtype == numpy.dtype('bool'): # Special case for boolean getitem return self.flat[afnumpy.where(args[0].flat)] idx, new_shape = indexing.__convert_dim__(self.shape, args) if any(x is None for x in idx): # one of the indices is empty return ndarray(indexing.__index_shape__(self.shape, idx), dtype=self.dtype) idx = tuple(idx) if len(idx) == 0: idx = tuple([0]) s = self.d_array[idx] shape = pu.af_shape(s) array = ndarray(shape, dtype=self.dtype, af_array=s) if(shape != new_shape): array = array.reshape(new_shape) if new_shape == () and Ellipsis not in args: # Return the actual scalar return numpy.array(array)[()] return array