def filter_median(x, length=10, mask=None, partition=None, out=None): """ Median filtering, O(1) in window length """ from pyoperators.utils import strelapsed time0 = time.time() if out is None: out = x.copy() if mask is not None : mask = np.ascontiguousarray(mask, np.bool8).view(np.int8) elif hasattr(x, 'mask') and x.mask is not None and \ x.mask is not np.ma.nomask: mask = x.mask.view(np.int8) else: mask = np.zeros(x.shape, np.int8) n = x.shape[-1] if partition is None: partition = (n,) status = tmf.filter_median(out.reshape((-1,n)).T, mask.reshape((-1,n)).T, length, np.array(partition, np.int32, ndmin=1)) if status != 0: raise RuntimeError() print(strelapsed(time0, 'Median filtering') + ' (length={0})'.format(length)) return out
def filter_median(x, length=10, mask=None, partition=None, out=None): """ Median filtering, O(1) in window length """ from pyoperators.utils import strelapsed time0 = time.time() if out is None: out = x.copy() if mask is not None: mask = np.ascontiguousarray(mask, np.bool8).view(np.int8) elif hasattr(x, 'mask') and x.mask is not None and \ x.mask is not np.ma.nomask: mask = x.mask.view(np.int8) else: mask = np.zeros(x.shape, np.int8) n = x.shape[-1] if partition is None: partition = (n, ) status = tmf.filter_median( out.reshape((-1, n)).T, mask.reshape((-1, n)).T, length, np.array(partition, np.int32, ndmin=1)) if status != 0: raise RuntimeError() print( strelapsed(time0, 'Median filtering') + ' (length={0})'.format(length)) return out
def _deglitch(tod, projection, nsigma, method): time0 = time.time() if method not in ('std', 'mad'): raise ValueError('Invalid deglitching method.') func = {'std': tmf.deglitch_l2b_std, 'mad': tmf.deglitch_l2b_mad}[method] if isinstance(projection, ProjectionBaseOperator): matrix = projection.matrix npixels_per_sample = matrix.shape[-1] elif hasattr(projection, 'operands') and \ all([isinstance(p, ProjectionBaseOperator) for p in projection.operands]): ops = projection.operands npixels_per_sample = max(p.matrix.shape[-1] for p in ops) if tod.shape[-1] != sum(p.matrix.shape[-2] for p in ops): raise ValueError('The tod has an incompatible shape.') matrix = np.zeros(tod.shape + (npixels_per_sample, ), ops[0].matrix.dtype) matrix['index'] = -1 dest = 0 for p in ops: nsamples = p.matrix.shape[-2] matrix[..., dest:dest + nsamples, :p.matrix.shape[-1]] = p.matrix dest += nsamples else: raise TypeError('The operator is not a projection.') ny, nx = projection.shapein if tod.mask is None: mask = np.zeros(tod.shape, np.bool8) else: mask = tod.mask.copy() percent = func( matrix.view(int).ravel(), nx, ny, tod.T, mask.view(np.int8).T, nsigma, npixels_per_sample) # IFORT stores .true. as -1, it conflicts with numpy algebra on booleans np.abs(mask, mask) print( strelapsed(time0, 'Deglitching ({0})'.format(method)) + ' (flagged samples: {0}%)'.format(percent)) return mask
def _deglitch(tod, projection, nsigma, method): time0 = time.time() if method not in ('std', 'mad'): raise ValueError('Invalid deglitching method.') func = {'std':tmf.deglitch_l2b_std, 'mad':tmf.deglitch_l2b_mad}[method] if isinstance(projection, ProjectionBaseOperator): matrix = projection.matrix npixels_per_sample = matrix.shape[-1] elif hasattr(projection, 'operands') and \ all([isinstance(p, ProjectionBaseOperator) for p in projection.operands]): ops = projection.operands npixels_per_sample = max(p.matrix.shape[-1] for p in ops) if tod.shape[-1] != sum(p.matrix.shape[-2] for p in ops): raise ValueError('The tod has an incompatible shape.') matrix = np.zeros(tod.shape+(npixels_per_sample,), ops[0].matrix.dtype) matrix['index'] = -1 dest = 0 for p in ops: nsamples = p.matrix.shape[-2] matrix[...,dest:dest+nsamples,:p.matrix.shape[-1]] = p.matrix dest += nsamples else: raise TypeError('The operator is not a projection.') ny, nx = projection.shapein if tod.mask is None: mask = np.zeros(tod.shape, np.bool8) else: mask = tod.mask.copy() percent = func(matrix.view(int).ravel(), nx, ny, tod.T, mask.view(np.int8).T, nsigma, npixels_per_sample) # IFORT stores .true. as -1, it conflicts with numpy algebra on booleans np.abs(mask, mask) print(strelapsed(time0, 'Deglitching ({0})'.format(method)) + ' (flagged samples: {0}%)'.format(percent)) return mask