def l2norm(self, delta, array0): num = 0 den = 0 for k in list(delta.keys()): num += afnumpy.sum((delta[k] * delta[k].conj()).real) den += afnumpy.sum((array0[k] * array0[k].conj()).real) return np.sqrt(num / den)
def l2norm(self, delta, array0): num = 0 den = 0 for k in delta.keys(): num += afnumpy.sum( (delta[k] * delta[k].conj()).real ) den += afnumpy.sum( (array0[k] * array0[k].conj()).real ) return np.sqrt(num / den)
def test_sum(): b = numpy.random.random(3) a = afnumpy.array(b) fassert(afnumpy.sum(a), numpy.sum(b)) fassert(afnumpy.sum(a,axis=0), numpy.sum(b,axis=0)) fassert(afnumpy.sum(a,keepdims=True), numpy.sum(b,keepdims=True)) b = numpy.random.random((2,3)) a = afnumpy.array(b) fassert(afnumpy.sum(a), numpy.sum(b)) fassert(afnumpy.sum(a,axis=0), numpy.sum(b,axis=0))
def test_sum(): b = numpy.random.random(3) a = afnumpy.array(b) fassert(afnumpy.sum(a), numpy.sum(b)) fassert(afnumpy.sum(a, axis=0), numpy.sum(b, axis=0)) fassert(afnumpy.sum(a, keepdims=True), numpy.sum(b, keepdims=True)) b = numpy.random.random((2, 3)) a = afnumpy.array(b) fassert(afnumpy.sum(a), numpy.sum(b)) fassert(afnumpy.sum(a, axis=0), numpy.sum(b, axis=0))
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 radial_symetry_af(background, rs=None, is_fft_shifted=True): if rs is None: 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) out = background for i in range(0, afnumpy.max(rs) + 1): m = (rs == i) msum = afnumpy.sum(m) if msum > 1: out[m] = afnumpy.sum(background[m]) / float(msum) return out, rs, None
def radial_symetry_af(background, rs = None, is_fft_shifted = True): if rs is None : 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) out = background for i in range(0, afnumpy.max(rs)+1): m = (rs == i) msum = afnumpy.sum(m) if msum > 1 : out[m] = afnumpy.sum(background[m]) / float(msum) return out, rs, None
def choose_N_highest_pixels(array, N, tol=1.0e-5, maxIters=1000, support=None): """ Use bisection to find the root of e(x) = \sum_i (array_i > x) - N then return (array_i > x) a boolean mask This is faster than using percentile (surprising) If support0 is not None then values outside the support are ignored. """ s0 = array.max() s1 = array.min() if support is not None: a = array[support > 0] else: a = array support = 1 for i in range(maxIters): s = (s0 + s1) / 2. e = afnumpy.sum(a > s) - N if np.abs(e) < tol: break if e < 0: s0 = s else: s1 = s S = (array > s) * support #print 'number of pixels in support:', afnumpy.sum(support), i, s, e, type(support) return S
def choose_N_highest_pixels(array, N, tol = 1.0e-5, maxIters=1000, support=None): """ Use bisection to find the root of e(x) = \sum_i (array_i > x) - N then return (array_i > x) a boolean mask This is faster than using percentile (surprising) If support0 is not None then values outside the support are ignored. """ s0 = array.max() s1 = array.min() if support is not None : a = array[support > 0] else : a = array support = 1 for i in range(maxIters): s = (s0 + s1) / 2. e = afnumpy.sum(a > s) - N if np.abs(e) < tol : break if e < 0 : s0 = s else : s1 = s S = (array > s) * support #print 'number of pixels in support:', afnumpy.sum(support), i, s, e, type(support) return S
def Emod(self, modes): M = self.Imap(modes) eMod = afnumpy.sum(self.mask * (afnumpy.sqrt(M) - self.amp)**2) eMod = afnumpy.sqrt(eMod / self.I_norm) return eMod
def norm(x, ord=None, axis=None, keepdims=False): x = asarray(x) # Check the default case first and handle it immediately. if ord is None and axis is None: ndim = x.ndim x = x.ravel(order='K') if isComplexType(x.dtype.type): sqnorm = dot(x.real, x.real) + dot(x.imag, x.imag) else: sqnorm = dot(x, x) ret = sqrt(sqnorm) if keepdims: ret = ret.reshape(ndim*[1]) return ret # Normalize the `axis` argument to a tuple. nd = x.ndim if axis is None: axis = tuple(range(nd)) elif not isinstance(axis, tuple): try: axis = int(axis) except: raise TypeError("'axis' must be None, an integer or a tuple of integers") axis = (axis,) if len(axis) == 1: if ord == Inf: return abs(x).max(axis=axis, keepdims=keepdims) elif ord == -Inf: return abs(x).min(axis=axis, keepdims=keepdims) elif ord == 0: # Zero norm return (x != 0).sum(axis=axis, keepdims=keepdims) elif ord == 1: # special case for speedup return afnumpy.sum(abs(x), axis=axis, keepdims=keepdims) elif ord is None or ord == 2: # special case for speedup s = (x.conj() * x).real return sqrt(afnumpy.sum(s, axis=axis, keepdims=keepdims)) else: try: ord + 1 except TypeError: raise ValueError("Invalid norm order for vectors.") if x.dtype.type is longdouble: # Convert to a float type, so integer arrays give # float results. Don't apply asfarray to longdouble arrays, # because it will downcast to float64. absx = abs(x) else: absx = x if isComplexType(x.dtype.type) else asfarray(x) if absx.dtype is x.dtype: absx = abs(absx) else: # if the type changed, we can safely overwrite absx abs(absx, out=absx) absx **= ord return afnumpy.sum(absx, axis=axis, keepdims=keepdims) ** (1.0 / ord) elif len(axis) == 2: row_axis, col_axis = axis if not (-nd <= row_axis < nd and -nd <= col_axis < nd): raise ValueError('Invalid axis %r for an array with shape %r' % (axis, x.shape)) if row_axis % nd == col_axis % nd: raise ValueError('Duplicate axes given.') if ord == 2: ret = _multi_svd_norm(x, row_axis, col_axis, amax) elif ord == -2: ret = _multi_svd_norm(x, row_axis, col_axis, amin) elif ord == 1: if col_axis > row_axis: col_axis -= 1 ret = afnumpy.sum(abs(x), axis=row_axis).max(axis=col_axis) elif ord == Inf: if row_axis > col_axis: row_axis -= 1 ret = afnumpy.sum(abs(x), axis=col_axis).max(axis=row_axis) elif ord == -1: if col_axis > row_axis: col_axis -= 1 ret = afnumpy.sum(abs(x), axis=row_axis).min(axis=col_axis) elif ord == -Inf: if row_axis > col_axis: row_axis -= 1 ret = afnumpy.sum(abs(x), axis=col_axis).min(axis=row_axis) elif ord in [None, 'fro', 'f']: ret = sqrt(afnumpy.sum((x.conj() * x).real, axis=axis)) else: raise ValueError("Invalid norm order for matrices.") if keepdims: ret_shape = list(x.shape) ret_shape[axis[0]] = 1 ret_shape[axis[1]] = 1 ret = ret.reshape(ret_shape) return ret else: raise ValueError("Improper number of dimensions to norm.")
def norm(x, ord=None, axis=None, keepdims=False): x = asarray(x) # Check the default case first and handle it immediately. if ord is None and axis is None: ndim = x.ndim x = x.ravel(order='K') if isComplexType(x.dtype.type): sqnorm = dot(x.real, x.real) + dot(x.imag, x.imag) else: sqnorm = dot(x, x) ret = sqrt(sqnorm) if keepdims: ret = ret.reshape(ndim * [1]) return ret # Normalize the `axis` argument to a tuple. nd = x.ndim if axis is None: axis = tuple(range(nd)) elif not isinstance(axis, tuple): try: axis = int(axis) except: raise TypeError( "'axis' must be None, an integer or a tuple of integers") axis = (axis, ) if len(axis) == 1: if ord == Inf: return abs(x).max(axis=axis, keepdims=keepdims) elif ord == -Inf: return abs(x).min(axis=axis, keepdims=keepdims) elif ord == 0: # Zero norm return (x != 0).sum(axis=axis, keepdims=keepdims) elif ord == 1: # special case for speedup return afnumpy.sum(abs(x), axis=axis, keepdims=keepdims) elif ord is None or ord == 2: # special case for speedup s = (x.conj() * x).real return sqrt(afnumpy.sum(s, axis=axis, keepdims=keepdims)) else: try: ord + 1 except TypeError: raise ValueError("Invalid norm order for vectors.") if x.dtype.type is longdouble: # Convert to a float type, so integer arrays give # float results. Don't apply asfarray to longdouble arrays, # because it will downcast to float64. absx = abs(x) else: absx = x if isComplexType(x.dtype.type) else asfarray(x) if absx.dtype is x.dtype: absx = abs(absx) else: # if the type changed, we can safely overwrite absx abs(absx, out=absx) absx **= ord return afnumpy.sum(absx, axis=axis, keepdims=keepdims)**(1.0 / ord) elif len(axis) == 2: row_axis, col_axis = axis if not (-nd <= row_axis < nd and -nd <= col_axis < nd): raise ValueError('Invalid axis %r for an array with shape %r' % (axis, x.shape)) if row_axis % nd == col_axis % nd: raise ValueError('Duplicate axes given.') if ord == 2: ret = _multi_svd_norm(x, row_axis, col_axis, amax) elif ord == -2: ret = _multi_svd_norm(x, row_axis, col_axis, amin) elif ord == 1: if col_axis > row_axis: col_axis -= 1 ret = afnumpy.sum(abs(x), axis=row_axis).max(axis=col_axis) elif ord == Inf: if row_axis > col_axis: row_axis -= 1 ret = afnumpy.sum(abs(x), axis=col_axis).max(axis=row_axis) elif ord == -1: if col_axis > row_axis: col_axis -= 1 ret = afnumpy.sum(abs(x), axis=row_axis).min(axis=col_axis) elif ord == -Inf: if row_axis > col_axis: row_axis -= 1 ret = afnumpy.sum(abs(x), axis=col_axis).min(axis=row_axis) elif ord in [None, 'fro', 'f']: ret = sqrt(afnumpy.sum((x.conj() * x).real, axis=axis)) else: raise ValueError("Invalid norm order for matrices.") if keepdims: ret_shape = list(x.shape) ret_shape[axis[0]] = 1 ret_shape[axis[1]] = 1 ret = ret.reshape(ret_shape) return ret else: raise ValueError("Improper number of dimensions to norm.")
def Emod(self, modes): M = self.Imap(modes) eMod = afnumpy.sum( self.mask * ( afnumpy.sqrt(M) - self.amp )**2 ) eMod = afnumpy.sqrt( eMod / self.I_norm ) return eMod