def put(arr, ids, values): """ The opposite of `take`. The values of `arr` at the locations specified by `ids` are set to the corresponding value of `values`. The following is to test improvments to puts with masked arrays. Places in the code were assuming incorrect put behavior. >>> maskValue = 999999 >>> arr = zeros(3, 'l') >>> ids = MA.masked_values((2, maskValue), maskValue) >>> values = MA.masked_values((4, maskValue), maskValue) >>> put(arr, ids, values) ## this should work >>> print arr [0 0 4] >>> arr = MA.masked_values((maskValue, 5, 10), maskValue) >>> ids = MA.masked_values((2, maskValue), maskValue) >>> values = MA.masked_values((4, maskValue), maskValue) >>> put(arr, ids, values) >>> print arr ## works as expected [-- 5 4] >>> arr = MA.masked_values((maskValue, 5, 10), maskValue) >>> ids = MA.masked_values((maskValue, 2), maskValue) >>> values = MA.masked_values((4, maskValue), maskValue) >>> put(arr, ids, values) >>> print arr ## should be [-- 5 --] maybe?? [-- 5 999999] """ if _isPhysical(arr): arr.put(ids, values) elif MA.isMaskedArray(arr): if NUMERIX.sometrue(MA.getmaskarray(ids)): if numpy_version == 'old': pvalues = MA.array(values, mask=MA.getmaskarray(ids)) else: pvalues = MA.array(values.filled(), mask=MA.getmaskarray(ids)) MA.put(arr, ids.compressed(), pvalues.compressed()) else: MA.put(arr, ids, values) elif MA.isMaskedArray(ids): NUMERIX.put(arr, ids.compressed(), MA.array(values, mask=MA.getmaskarray(ids)).compressed()) else: NUMERIX.put(arr, ids, values)
def reshape(arr, shape): """ Change the shape of `arr` to `shape`, as long as the product of all the lenghts of all the axes is constant (the total number of elements does not change). """ if -1 in shape: # e.g., NUMERIX.reshape(a, (-1, N)) fails if N == 0 oldShape = array(getShape(arr)) oldShape[oldShape == 0] = 1 if hasattr(shape, 'index'): index = shape.index(-1) else: index = list(shape).index(-1) left = shape[:index] right = shape[index + 1:] newShape = array(left + right) newShape[newShape == 0] = 1 shape = left + (oldShape.prod() / newShape.prod(), ) + right if _isPhysical(arr): return arr.reshape(shape) elif type(arr) is type(array((0))): return NUMERIX.reshape(arr, tuple(shape)) elif type(arr) is type(MA.array((0))): return MA.reshape(arr, shape) else: return NUMERIX.reshape(array(arr), tuple(shape))
def reshape(arr, shape): """ Change the shape of `arr` to `shape`, as long as the product of all the lenghts of all the axes is constant (the total number of elements does not change). """ if -1 in shape: # e.g., NUMERIX.reshape(a, (-1, N)) fails if N == 0 oldShape = array(getShape(arr)) oldShape[oldShape == 0] = 1 if hasattr(shape, 'index'): index = shape.index(-1) else: index = list(shape).index(-1) left = shape[:index] right = shape[index+1:] newShape = array(left + right) newShape[newShape == 0] = 1 shape = left + (oldShape.prod() / newShape.prod(),) + right if _isPhysical(arr): return arr.reshape(shape) elif type(arr) is type(array((0))): return NUMERIX.reshape(arr, tuple(shape)) elif type(arr) is type(MA.array((0))): return MA.reshape(arr, shape) else: return NUMERIX.reshape(array(arr), tuple(shape))
def take(a, indices, axis=0, fill_value=None): """ Selects the elements of `a` corresponding to `indices`. """ if _isPhysical(a): taken = a.take(indices, axis=axis) elif type(indices) is type(MA.array((0))): ## Replaces `MA.take`. `MA.take` does not always work when ## `indices` is a masked array. ## taken = MA.take(a, MA.filled(indices, 0), axis=axis) mask = MA.getmask(indices) if mask is not MA.nomask: mask = MA.getmaskarray(indices) if taken.shape != mask.shape: mask = MA.repeat(mask[NewAxis, ...], taken.shape[0], axis=0) mask = MA.mask_or(MA.getmask(taken), mask) if mask is not MA.nomask: taken = MA.array(data=taken, mask=mask) else: if MA.getmask(taken) is MA.nomask and numpy_version == 'old': # numpy 1.1 returns normal array when masked array is filled taken = taken.filled() elif type(a) in (type(array((0))), type(()), type([])): taken = NUMERIX.take(a, indices, axis=axis) elif type(a) is type(MA.array((0))): taken = MA.take(a, indices, axis=axis) else: raise TypeError, 'cannot take from %s object: %s' % (type(a), `a`) if fill_value is not None and type(taken) is type(MA.array((0))): taken = taken.filled(fill_value=fill_value) return taken
def take(a, indices, axis=0, fill_value=None): """ Selects the elements of `a` corresponding to `indices`. """ if _isPhysical(a): taken = a.take(indices, axis=axis) elif type(indices) is type(MA.array((0))): ## Replaces `MA.take`. `MA.take` does not always work when ## `indices` is a masked array. ## taken = MA.take(a, MA.filled(indices, 0), axis=axis) mask = MA.getmask(indices) if mask is not MA.nomask: mask = MA.getmaskarray(indices) if taken.shape != mask.shape: mask = MA.repeat(mask[NewAxis, ...], taken.shape[0], axis=0) mask = MA.mask_or(MA.getmask(taken), mask) if mask is not MA.nomask: taken = MA.array(data=taken, mask=mask) else: if MA.getmask(taken) is MA.nomask and numpy_version == 'old': # numpy 1.1 returns normal array when masked array is filled taken = taken.filled() elif type(a) in (type(array((0))), type(()), type([])): taken = NUMERIX.take(a, indices, axis=axis) elif type(a) is type(MA.array((0))): taken = MA.take(a, indices, axis=axis) else: raise TypeError, 'cannot take from %s object: %s' % (type(a), ` a `) if fill_value is not None and type(taken) is type(MA.array((0))): taken = taken.filled(fill_value=fill_value) return taken
def sqrtDot(a1, a2): """Return array of square roots of vector dot-products for arrays a1 and a2 of vectors v1 and v2 Usually used with v1==v2 to return magnitude of v1. """ from fipy.tools.dimensions import physicalField unit1 = unit2 = physicalField._unity def dimensionlessUnmasked(a): unit = physicalField._unity mask = False if _isPhysical(a): unit = a.inBaseUnits().unit a = a.numericValue if MA.isMaskedArray(a): mask = a.mask a = a.filled(fill_value=1) if not a.flags['C_CONTIGUOUS']: a = a.copy('C') return (a, unit, mask) a1, unit1, mask1 = dimensionlessUnmasked(a1) a2, unit2, mask2 = dimensionlessUnmasked(a2) NJ, ni = NUMERIX.shape(a1) result1 = NUMERIX.zeros((ni, ), 'd') inline._runInline(""" int j; result1[i] = 0.; for (j = 0; j < NJ; j++) { result1[i] += a1[i + j * ni] * a2[i + j * ni]; } result1[i] = sqrt(result1[i]); """, result1=result1, a1=a1, a2=a2, ni=ni, NJ=NJ) if NUMERIX.any(mask1) or NUMERIX.any(mask2): result1 = MA.array(result1, mask=NUMERIX.logical_or(mask1, mask2)) if unit1 != physicalField._unity or unit2 != physicalField._unity: from fipy.tools.dimensions.physicalField import PhysicalField result1 = PhysicalField(value=result, unit=(unit1 * unit2)**0.5) return result1
def sum(arr, axis=0): """ The sum of all the elements of `arr` along the specified axis. """ if _isPhysical(arr): return arr.sum(axis) elif type(arr) is type(MA.array((0))): return MA.sum(arr, axis) else: if type(arr) in (float, int) or len(arr) == 0 or 0 in arr.shape: return NUMERIX.sum(arr, axis) else: if axis is None: axis = 0 return NUMERIX.tensordot(NUMERIX.ones(arr.shape[axis], 'l'), arr, (0, axis))
def sqrtDot(a1, a2): """Return array of square roots of vector dot-products for arrays a1 and a2 of vectors v1 and v2 Usually used with v1==v2 to return magnitude of v1. """ from fipy.tools.dimensions import physicalField unit1 = unit2 = physicalField._unity def dimensionlessUnmasked(a): unit = physicalField._unity mask = False if _isPhysical(a): unit = a.inBaseUnits().unit a = a.numericValue if MA.isMaskedArray(a): mask = a.mask a = a.filled(fill_value=1) if not a.flags['C_CONTIGUOUS']: a = a.copy('C') return (a, unit, mask) a1, unit1, mask1 = dimensionlessUnmasked(a1) a2, unit2, mask2 = dimensionlessUnmasked(a2) NJ, ni = NUMERIX.shape(a1) result1 = NUMERIX.zeros((ni,), 'd') inline._runInline(""" int j; result1[i] = 0.; for (j = 0; j < NJ; j++) { result1[i] += a1[i + j * ni] * a2[i + j * ni]; } result1[i] = sqrt(result1[i]); """, result1=result1, a1=a1, a2=a2, ni=ni, NJ=NJ) if NUMERIX.any(mask1) or NUMERIX.any(mask2): result1 = MA.array(result1, mask=NUMERIX.logical_or(mask1, mask2)) if unit1 != physicalField._unity or unit2 != physicalField._unity: from fipy.tools.dimensions.physicalField import PhysicalField result1 = PhysicalField(value=result, unit=(unit1 * unit2)**0.5) return result1
def isNumpy(aa): return type(aa) in (type(MA.array(0)), type(NUMERIX.array(0)))
print "--------------------" chi = NglA.chiinv([0.99], [2]) print chi, type(chi) print "\nchiinv (tuple input)" print "---------------------" chi = NglA.chiinv((0.99), (2)) print chi, type(chi) # # linmsg # x = MA.array([ \ [ 1190., 1455., 1550., -999., 1745., 1770., \ 1900., -999., -999., -999., 2335., 2490., \ 2720., 2710., 2530., 2900., 2760., -999.], \ [ 1115., -999., 1515., 1794., -999. ,1710., \ 1830., 1920., 1970., 2300., 2280., 2520., \ 2630., -999. ,-999. ,2800., -999.,-999. ] \ ],fill_value=-999.) ix = MA.array([ \ [ 1190, 1455, 1550, -999, 1745, 1770, \ 1900, -999, -999, -999, 2335, 2490, \ 2720, 2710, 2530, 2900, 2760, -999], \ [ 1115, -999, 1515, 1794, -999 ,1710, \ 1830, 1920, 1970, 2300, 2280, 2520, \ 2630, -999 ,-999 ,2800, -999,-999 ] \ ],fill_value=-999) lx =[ \ [ 1190, 1455, 1550, -999, 1745, 1770, \
def normToPoints(self, minMJD=None, maxMJD=None, zerotype="mean", clipsigma=3.): import numpy.core.ma as ma zerodates = None # Make sure things that should be floats if defined are floats if minMJD is not None: minMJD = float(minMJD) if maxMJD is not None: maxMJD = float(maxMJD) if clipsigma is not None: clipsigma = float(clipsigma) # should always be defined if minMJD is None and maxMJD is None: self.normToLastPoint() return else: if minMJD is not None and maxMJD is not None: if minMJD < maxMJD: zerodates = [ i for (i, d) in enumerate(self.dates) if (d >= minMJD) and (d <= maxMJD) ] else: zerodates = [ i for (i, d) in enumerate(self.dates) if (d >= minMJD) or (d <= maxMJD) ] elif minMJD is not None: zerodates = [ i for (i, d) in enumerate(self.dates) if d >= minMJD ] elif maxMJD is not None: zerodates = [ i for (i, d) in enumerate(self.dates) if d <= maxMJD ] if zerodates is None or len(zerodates) <= 0: print( "NN2: No points found in given MJD range to use as zero points for this filter." ) print("NN2: Leaving points unchanged.") return # There's got to be a better way of doing the following masked array stuff zeroflux_mask = [not isfinite(f) for f in self.V[zerodates]] zerofluxerr_mask = [not isfinite(df) for df in self.Verr[zerodates]] zeroflux = ma.array(self.V[zerodates], mask=zeroflux_mask) zerofluxerr = ma.array(self.Verr[zerodates], mask=zerofluxerr_mask) # Now finally adjust the flux by the zeroflux calculated. # If there's only one entry then we just take it if len(zeroflux) == 1: zerofluxnorm = zeroflux[0] elif zerotype == "mean": # zerofluxnorm = ma.average(zeroflux, weights=1./(zerofluxerr*zerofluxerr)) zerofluxnorm = ma.average(zeroflux, weights=1. / zerofluxerr * zerofluxerr) elif zerotype == "clipmean": zeroavg = ma.average(zeroflux, weights=1. / (zerofluxerr * zerofluxerr)) residual = zeroflux - zeroavg residual /= zerofluxerr # error-weighted # 2006/02/13 - MWV: # Need the additional check to make sure the value isn't masked w = [i for (i,f) in enumerate(ma.fabs(residual)) \ if f is not ma.masked and f < clipsigma] if len(w) <= 0: print("No good zero flux points") print("No flux zero applied to points.") try: zerofluxnorm = ma.average(zeroflux[w], weights=1. / (zerofluxerr[w] * zerofluxerr[w])) this_resid = residual[w] * residual[w] zerofluxchisq = this_resid.sum() print(zerofluxchisq) dof = len(residual) - 1 zerofluxchisqnu = zerofluxchisq / dof print("Chi^2/DoF of zero points : %f / %f = %f " % (zerofluxchisq, dof, zerofluxchisqnu)) except 'DIE': zerofluxnorm = 0. zerofluxchisq = 0. dof = 0. else: print("zerotype == '%s' unknown or not yet supported." % (zerotype)) print("No flux zero applied to points.") zerofluxnorm = 0. # Subtract the fiducial flux self.V -= zerofluxnorm