def sw_f(a, axis=None, masked=False, weights=None, N=None, sum_of_squares=False): """Return the sum of weights for an array. :Parameters: a: numpy array_like Input array axis: `int`, optional Axis along which to operate. By default, flattened input is used. masked: `bool`, optional weights: numpy array-like, optional Weights associated with values of the array. By default the statistics are unweighted. N: `numpy.ndarray` Sample size sum_of_squares: delta degrees of freedom :Returns: 2-`tuple` of `numpy.ndarray` The sample size and the sum of weights. """ if N is None: (N, ) = sample_size_f(a, axis=axis, masked=masked) if weights is not None: # A weights array has been provided weights = double_precision(weights) if weights.ndim < a.ndim: weights = broadcast_array(weights, a.shape) if masked: weights = numpy_ma_array(weights, mask=a.mask, copy=False) if sum_of_squares: weights = weights * weights sw = weights.sum(axis=axis) if not numpy_ndim(sw): sw = numpy_asanyarray(sw) else: # The sum of weights is equal to the sample size (i.e. an # unweighted sample) sw = N.copy() return asanyarray(N, sw)
def sw_f(a, axis=None, masked=False, weights=None, N=None, sum_of_squares=False): '''TODO ''' if N is None: N, = sample_size_f(a, axis=axis, masked=masked) if weights is not None: # A weights array has been provided weights = double_precision(weights) if weights.ndim < a.ndim: weights = broadcast_array(weights, a.shape) if masked: weights = numpy_ma_array(weights, mask=a.mask, copy=False) if sum_of_squares: weights = weights * weights sw = weights.sum(axis=axis) if not numpy_ndim(sw): sw = numpy_asanyarray(sw) else: # The sum of weights is equal to the sample size (i.e. an # unweighted sample) sw = N.copy() return asanyarray(N, sw)
def mask_where_too_few_values(Nmin, N, x): """Mask elements of N and x where N is strictly less than Nmin. :Parameters: Nmin: `int` N: `numpy.ndarray` x: `numpy.ndarray` :Returns: (`numpy.ndarray`, `numpy.ndarray`) A tuple containing *N* and *x*, both masked where *N* is strictly less than *Nmin*. """ if N.min() < Nmin: mask = N < Nmin N = numpy_ma_array(N, mask=mask, copy=False, shrink=False) x = numpy_ma_array(x, mask=mask, copy=False, shrink=True) return asanyarray(N, x)
def psum(x, y): """Add two arrays element-wise. If either or both of the arrays are masked then the output array is masked only where both input arrays are masked. :Parameters: x: numpy array-like *Might be updated in place*. y: numpy array-like Will not be updated in place. :Returns: `numpy.ndarray` **Examples:** >>> c = psum(a, b) """ if numpy_ma_isMA(x): if numpy_ma_isMA(y): # x and y are both masked x_mask = x.mask x = x.filled(0) x += y.filled(0) x = numpy_ma_array(x, mask=x_mask & y.mask, copy=False) else: # Only x is masked x = x.filled(0) x += y elif numpy_ma_isMA(y): # Only y is masked x += y.filled(0) else: # x and y are both unmasked x += y return x
def __getitem__(self, indices): '''x.__getitem__(indices) <==> x[indices] Returns a numpy array. ''' array = numpy_load(self._partition_file) indices = parse_indices(array.shape, indices) array = get_subspace(array, indices) if self._get_component('_masked_as_record'): # Convert a record array to a masked array array = numpy_ma_array(array['_data'], mask=array['_mask'], copy=False) array.shrink_mask() # Return the numpy array return array