def test_ffthalfcomplex3(): partition = (100,300,5,1000-100-300-5) ffts = [FftHalfComplexOperator(p) for p in partition] fft = BlockDiagonalOperator(ffts, partitionin=partition, axisin=-1) a = np.random.random((10,np.sum(partition)))+1 b = fft(a) b_ = np.hstack([ffts[0](a[:,:100]), ffts[1](a[:,100:400]), ffts[2](a[:,400:405]), ffts[3](a[:,405:])]) assert np.allclose(b, b_) b = fft.T(fft(a)) assert np.allclose(a, b)
def get_projection_operator(self, sampling, scene, verbose=True): """ Return the peak sampling operator. Convert units from W to W/sr. Parameters ---------- sampling : QubicSampling The pointing information. scene : QubicScene The observed scene. verbose : bool, optional If true, display information about the memory allocation. """ horn = getattr(self, 'horn', None) primary_beam = getattr(self, 'primary_beam', None) rotation = sampling.cartesian_galactic2instrument return BlockDiagonalOperator([ self._get_projection_operator(rotation, scene, nu, self.detector.points, self.synthbeam, horn, primary_beam, verbose=verbose) for nu in self.filter.nu ], new_axisin=0)
def get_invntt_operator(self): """ Return the inverse time-time noise correlation matrix as an Operator. """ return BlockDiagonalOperator( [self.instrument.get_invntt_operator(self.sampling[b]) for b in self.block], axisin=1)
def get_invntt_operator(self, fftw_flag='FFTW_MEASURE', nthreads=None): ops = [ SymmetricBandToeplitzOperator((self.get_ndetectors(), b.n), self._filters, fftw_flag=fftw_flag, nthreads=nthreads) for b in self.block ] return BlockDiagonalOperator(ops, axisin=-1)
def get_convolution_transfer_operator(self, **keywords): fwhms = self.synthbeam.peak150.fwhm * (150e9 / self.filter.nu) fwhm_min = fwhms[-1] fwhms_transfer = [np.sqrt(fwhm**2 - fwhm_min**2) for fwhm in fwhms] return BlockDiagonalOperator([ HealpixConvolutionGaussianOperator(fwhm=fwhm, **keywords) for fwhm in fwhms_transfer ], new_axisin=0)
def get_hwp_operator(self): """ Return the operator for the bolometer responses. """ return BlockDiagonalOperator([ self.instrument.get_hwp_operator(self.sampling[b], self.scene) for b in self.block ], axisin=1)
def get_polarizer_operator(self): """ Return operator for the polarizer grid. """ return BlockDiagonalOperator([ self.instrument.get_polarizer_operator( self.sampling[b], self.scene) for b in self.block ], axisin=1)
def get_preconditioner(self, cov): if cov is not None: cov_inv = np.array([1. / cov[(self.nus > mi) * (self.nus < ma)].mean(axis=0) \ for (mi, ma) in self.bands]) cov_inv[np.isinf(cov_inv)] = 0. # cov_inv = np.nan_to_num(cov_inv) return BlockDiagonalOperator(\ [DiagonalOperator(ci, broadcast='rightward') for ci in cov_inv], new_axisin=0) else: return None
def totoeplitz(n, firstrow): if isinstance(n, tuple): n_ = n[-1] return BlockDiagonalOperator( [totoeplitz(n_, f_) for f_ in firstrow], new_axisin=0) ncorr = len(firstrow) - 1 dense = np.zeros((n, n)) for i in range(n): for j in range(n): if abs(i - j) <= ncorr: dense[i, j] = firstrow[abs(i - j)] return DenseOperator(dense, shapein=dense.shape[1])
def func(shapein, shapeout, extrainput): datashape = shapeout + shapein inputshape = extrainput + shapein d = np.arange(product(datashape)).reshape(datashape) b = DenseOperator(d, naxesin=len(shapein), naxesout=len(shapeout), shapein=inputshape) bdense = b.todense() n = product(extrainput) d_ = d.reshape((product(shapeout), product(shapein))) expected = BlockDiagonalOperator(n * [d_], axisin=0).todense() assert_equal(bdense, expected)
def test_partition4(): o1 = HomothetyOperator(1, shapein=1) o2 = HomothetyOperator(2, shapein=2) o3 = HomothetyOperator(3, shapein=3) @flags.separable class Op(Operator): pass op = Op() p = BlockDiagonalOperator([o1, o2, o3], axisin=0) r = (op + p + op) * p assert isinstance(r, BlockDiagonalOperator)
def get_preconditioner(self, cov): print('This is the new preconditionner') if cov is not None: cov_av = np.array([cov[(self.nus > mi) * (self.nus < ma)].mean(axis=0) \ for (mi, ma) in self.bands]) cov_inv = np.zeros_like(cov_av) seenpix = cov_av != 0 cov_inv[seenpix] = 1. / cov_av[seenpix] return BlockDiagonalOperator( \ [DiagonalOperator(ci, broadcast='rightward') for ci in cov_inv], new_axisin=0) else: return None
def func(shapein, shapeout, extradata, extrainput): datashape = extradata + shapeout + shapein d = np.arange(product(datashape)).reshape(datashape) b = DenseBlockDiagonalOperator(d, naxesin=len(shapein), naxesout=len(shapeout)) new_shape = broadcast_shapes(extradata, extrainput) bdense = b.todense(shapein=new_shape + shapein) d_ = reshape_broadcast(d, new_shape + shapeout + shapein) d_ = d_.reshape(-1, product(shapeout), product(shapein)) expected = BlockDiagonalOperator( [_ for _ in d_], axisin=0).todense(shapein=product(new_shape + shapein)) assert_same(bdense, expected) bTdense = b.T.todense(shapein=new_shape + shapeout) assert_same(bTdense, expected.T)
def get_invntt_operator(self): """ Return the inverse covariance matrix of the fused observation as an operator. """ invntt_qubic = self.qubic.get_invntt_operator() R_qubic = ReshapeOperator(invntt_qubic.shapeout, invntt_qubic.shape[0]) invntt_planck = self.planck.get_invntt_operator() R_planck = ReshapeOperator(invntt_planck.shapeout, invntt_planck.shape[0]) return BlockDiagonalOperator([ R_qubic(invntt_qubic(R_qubic.T)), R_planck(invntt_planck(R_planck.T)) ], axisout=0)
def __new__(cls, obs=None, method='uncorrelated', **keywords): if obs is None: return Operator.__new__(cls) nsamples = obs.get_nsamples() comm_tod = obs.instrument.comm method = method.lower() if method not in ('uncorrelated', 'uncorrelated python'): raise ValueError("Invalid method '{0}'.".format(method)) filter = obs.get_filter_uncorrelated(**keywords) if filter.ndim == 2: filter = filter[np.newaxis, ...] nfilters = filter.shape[0] if nfilters != 1 and nfilters != len(nsamples): raise ValueError( "Incompatible number of filters '{0}'. Expected nu" "mber is '{1}'".format(nfilters, len(nsamples))) ncorrelations = filter.shape[-1] - 1 filter_length = np.asarray(2**np.ceil(np.log2(np.array(nsamples) + \ 2 * ncorrelations)), int) fft_filters = [] for i, n in enumerate(filter_length): i = min(i, nfilters - 1) fft_filter, status = tmf.fft_filter_uncorrelated(filter[i].T, n) if status != 0: raise RuntimeError() np.maximum(fft_filter, 0, fft_filter) fft_filters.append(fft_filter.T) norm = comm_tod.allreduce(max(np.max(f) for f in fft_filters), op=MPI.MAX) for f in fft_filters: np.divide(f, norm, f) if method == 'uncorrelated': cls = InvNttUncorrelatedOperator else: cls = InvNttUncorrelatedPythonOperator #XXX should generate BlockDiagonalOperator with no duplicates... return BlockDiagonalOperator([cls(f, ncorrelations, n) \ for f, n in zip(fft_filters, nsamples)], axisin=-1)
def func(ops, p): op = BlockDiagonalOperator(ops, partitionin=p, axisin=0) assert_eq(op.todense(6), r, str(op))
def func(axis, s): op = BlockDiagonalOperator(ops, new_axisin=axis) assert_eq(op.shapein, s) assert_eq(op.shapeout, s)
def func(axisp, axiss): op = BlockDiagonalOperator(shape[axisp] * [Stretch(axiss)], new_axisin=axisp) axisp_ = axisp if axisp >= 0 else axisp + 4 axiss_ = axiss if axisp_ > axiss else axiss + 1 assert_eq(op(i), Stretch(axiss_)(i))
def test_partition(): clss = (ConstantOperator, DiagonalOperator, DiagonalNumexprOperator, HomothetyOperator, IdentityOperator, MaskOperator, PackOperator, UnpackOperator) valids = ((True, False, False), (True, True, True), (True, True, True), (True, True, True), (True, True, True), (True, True, True), (True, False, True), (True, True, False)) def func(a, b, operation, apply_rule): p = operation([a, b]) if not apply_rule: if isinstance(a, IdentityOperator) or \ isinstance(b, IdentityOperator): return assert not isinstance(p, BlockDiagonalOperator) return assert_is_instance(p, BlockDiagonalOperator) with rule_manager(none=True): q = operation([a, b]) assert_equal(p.todense(), q.todense()) for cls, (commutative, left, right) in zip(clss, valids): for ndims in range(3): shape = tuple(range(2, 2 + ndims)) def sfunc1(ndim): s = list(range(2, ndim + 2)) data = np.arange(product(s)).reshape(s) + 2 if cls is MaskOperator: data = (data % 2).astype(bool) return data def sfunc2(ndim): s = list(range(2 + ndims - ndim, 2 + ndims)) data = np.arange(product(s)).reshape(s) + 2 if cls is MaskOperator: data = (data % 2).astype(bool) return data if cls in (HomothetyOperator, IdentityOperator): ops = [get_operator(cls, np.array(2))] else: ops = [get_operator(cls, sfunc1(ndim)) for ndim in range(ndims+1)] + \ [get_operator(cls, sfunc2(ndim), broadcast='leftward') for ndim in range(1, ndims+1)] + \ [get_operator(cls, sfunc1(ndim), broadcast='rightward') for ndim in range(1, ndims+1)] def toone(index): list_ = list(shape) list_[index] = 1 return list_ def remove(index): list_ = list(shape) list_.pop(index) return list_ block = \ [BlockDiagonalOperator([HomothetyOutplaceOperator( v, shapein=toone(axis)) for v in range(2, 2+shape[axis])], axisin=axis, partitionin=shape[axis]*[1]) for axis in range(-ndims, ndims)] + \ [BlockDiagonalOperator([HomothetyOutplaceOperator( v, shapein=remove(axis)) for v in range(2, 2+shape[axis])], new_axisin=axis, partitionin=shape[axis]*[1]) for axis in range(-ndims, ndims)] for o, b in itertools.product(ops, block): if (o.shapein is None or o.shapein == b.shapein) and \ (o.shapeout is None or o.shapeout == b.shapeout): yield func, o, b, AdditionOperator, commutative yield func, o, b, MultiplicationOperator, commutative if o.shapein is None or o.shapein == b.shapeout: yield func, o, b, CompositionOperator, right if o.shapeout is None or b.shapein == o.shapeout: yield func, b, o, CompositionOperator, left
def get_polarizer_operator(self, sampling, scene): return BlockDiagonalOperator([ self._get_polarizer_operator(sampling, scene) for nu in self.filter.nu ], new_axisin=0)
def func(axisp, p, axiss): op = BlockDiagonalOperator(3 * [Stretch(axiss)], partitionin=p, axisin=axisp) assert_eq(op(i), Stretch(axiss)(i))