示例#1
0
def get_qubic_map(instrument, sampling, scene, input_maps, withplanck=True, covlim=0.1):
    acq = QubicAcquisition(instrument, sampling, scene, photon_noise=True, effective_duration=1)
    C = acq.get_convolution_peak_operator()
    coverage = acq.get_coverage()
    observed = coverage > covlim * np.max(coverage)
    acq_restricted = acq[:, :, observed]
    H = acq_restricted.get_operator()
    x0_convolved = C(input_maps)
    if not withplanck:
        pack = PackOperator(observed, broadcast='rightward')
        y_noiseless = H(pack(x0_convolved))
        noise = acq.get_noise()
        y = y_noiseless + noise
        invntt = acq.get_invntt_operator()
        A = H.T * invntt * H
        b = (H.T * invntt)(y)
        preconditioner = DiagonalOperator(1 / coverage[observed], broadcast='rightward')
        solution_qubic = pcg(A, b, M=preconditioner, disp=True, tol=1e-3, maxiter=1000)
        maps = pack.T(solution_qubic['x'])
        maps[~observed] = 0
    else:
        acq_planck = PlanckAcquisition(150, acq.scene, true_sky=x0_convolved)#, fix_seed=True)
        acq_fusion = QubicPlanckAcquisition(acq, acq_planck)
        map_planck_obs=acq_planck.get_observation()
        H = acq_fusion.get_operator()
        invntt = acq_fusion.get_invntt_operator()
        y = acq_fusion.get_observation()
        A = H.T * invntt * H
        b = H.T * invntt * y
        solution_fusion = pcg(A, b, disp=True, maxiter=1000, tol=1e-3)
        maps = solution_fusion['x']
        maps[~observed] = 0
    x0_convolved[~observed] = 0
    return(maps, x0_convolved, observed)    
示例#2
0
    def tod2map(self, tod, tol=1e-5, maxiter=1000, verbose=True):
        p = self.planck
        H = []
        for q, w in zip(self.qubic, self.weights):
            H.append(QubicPlanckAcquisition(q, p).get_operator() * w)
        invntt = self.get_invntt_operator()

        A_columns = []
        for h1 in H:
            c = []
            for h2 in H:
                c.append(h2.T * invntt * h1)
            A_columns.append(BlockColumnOperator(c, axisout=0))
        A = BlockRowOperator(A_columns, axisin=0)

        b = (self.get_operator()).T * (invntt * tod)
        sh = b.shape
        if len(sh) == 3:
            b = b.reshape((sh[0] * sh[1], sh[2]))
        else:
            b = b.reshape((sh[0] * sh[1]))

        solution = pcg(A, b, disp=verbose, tol=tol, maxiter=maxiter)
        if len(sh) == 3:
            maps_recon = solution["x"].reshape(sh[0], sh[1], sh[2])
        else:
            maps_recon = solution["x"].reshape(sh[0], sh[1])
        return maps_recon
示例#3
0
def tod2map(tod,pointing,instrument_in,detector_list=False,disp=True,kmax=2,displaytime=False):
    t0=time.time()
    #### Detectors
    mask_packed = np.ones(len(instrument_in.detector.packed), bool)
    if detector_list:
        mask_packed[detector_list] = False
        mask_unpacked = instrument_in.unpack(mask_packed)
        instrument = QubicInstrument('monochromatic', removed=mask_unpacked,nside=instrument_in.sky.nside)
    else:
        instrument = instrument_in
        
    #### Observations
    obs = QubicAcquisition(instrument, pointing)
    projection = obs.get_projection_peak_operator(kmax=kmax)
    coverage = projection.pT1()
    mask = coverage == 0
    projection = pack_projection_inplace(projection, mask)
    hwp = obs.get_hwp_operator()
    polgrid = DenseOperator([[0.5, 0.5, 0],
                             [0.5,-0.5, 0]])
    H = polgrid * hwp * projection
    preconditioner = DiagonalOperator(1/coverage[~mask], broadcast='rightward')
    solution = pcg(H.T * H, H.T(tod), M=preconditioner, disp=disp, tol=1e-3)
    output_map = unpack(solution['x'], mask)
    t1=time.time()
    if displaytime: print(' Map done in {0:.4f} seconds'.format(t1-t0))
    return output_map,coverage
def reconstruct(Y,nus,sub_nus,bands,sub_deltas,deltas):

	################
	### Coverage ###
	################

	instruments = [QubicInstrument(filter_nu=nus[i]*10**9,detector_nep=4.7e-17*np.sqrt(len(sampling) * sampling.period / (365 * 86400))) for i in range(Nbfreq)]
	acqs = [QubicAcquisition(instruments[i], sampling, scene) for i in range(Nbfreq)]
	covlim=0.1
	coverage = np.array([acqs[i].get_coverage() for i in range(len(nus))])
	observed = [(coverage[i] > covlim*np.max(coverage[i])) for i in range(Nbfreq)]
	obs=reduce(logical_and,tuple(observed[i] for i in range(Nbfreq)))
	pack = PackOperator(obs, broadcast='rightward')


	######################
	### Reconstruction ###
	######################

	sub_instruments=[QubicInstrument(filter_nu=sub_nus[i]*10**9,detector_nep=2.7e-17) for i in range(Nbsubfreq)]
	sub_acqs=[QubicAcquisition(sub_instruments[i], sampling, scene,photon_noise=True, effective_duration=3) for i in range(Nbsubfreq)]
	sub_acqs_restricted=[sub_acqs[i][:,:,obs] for i in range(Nbsubfreq)]
	operators=np.array([sub_acqs_restricted[i].get_operator() for i in range(Nbsubfreq)])
	K=np.array([np.sum([operators[j]*sub_deltas[j]*10**9 for j in bands[i]],axis=0) for i in range(Nbfreq)])
	H=BlockRowOperator([K[i] for i in range(Nbfreq)], new_axisin=0)
	invntt = acqs[0].get_invntt_operator()
	A=H.T*invntt*H
	b = (H.T * invntt)(Y)
	preconditioner = BlockDiagonalOperator([DiagonalOperator(1 / coverage[0][obs], broadcast='rightward') for i in range(Nbfreq)], new_axisin=0) 
	solution_qubic = pcg(A, b, M=preconditioner ,disp=True, tol=1e-3, maxiter=1000)
	blockpack = BlockDiagonalOperator([pack for i in range(Nbfreq)], new_axisin=0)
	maps =blockpack.T(solution_qubic['x'])  
	maps[:,~obs] = 0
	return maps
示例#5
0
    def tod2map(self, tod, tol=1e-5, maxiter=1000, verbose=True):
        H = self.get_operator()
        invntt = self.get_invntt_operator()
        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(H)
        solution = pcg(A, b, M=preconditioner, disp=verbose, tol=tol, maxiter=maxiter)
        return solution['x']
示例#6
0
    def tod2map(self, tod, tol=1e-5, maxiter=1000, verbose=True):
        H = self.get_operator()
        invntt = self.get_invntt_operator()
        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(H)
        solution = pcg(A, b, M=preconditioner, disp=verbose, tol=tol, maxiter=maxiter)
        return solution['x']
示例#7
0
    def tod2map(self, tod, tol=1e-5, maxiter=1000, verbose=True):
        H = self.get_operator()
        invntt = self.get_invntt_operator()

        A = H.T * invntt * H
        b = H.T * invntt * tod

        solution = pcg(A, b, disp=verbose, tol=tol, maxiter=maxiter)
        return solution["x"]
def reconstruct(Y,nu_min,nu_max,delta_nu,subdelta_nu,obs,sampling,scene,effective_duration):

	###################
	### Frequencies ###
	###################

	#frequencies to reconstruct
	Nbfreq=int(floor(log(nu_max/nu_min)/log(1+delta_nu)))+1
	nus=nu_min*np.logspace(0,Nbfreq-1,Nbfreq,endpoint=True,base=delta_nu+1)#edge frequencies of reconstructed bands
	deltas=(delta_nu)*(nus)

	#frequencies assumed to have been used for construction of TOD
	subnu_min=nu_min
	subnu_max=nu_max
	Nbsubfreq=int(floor(log(subnu_max/subnu_min)/log(1+subdelta_nu)))+1
	sub_nus=subnu_min*np.logspace(0,Nbsubfreq-1,Nbsubfreq,endpoint=True,base=subdelta_nu+1)
	sub_deltas=(subdelta_nu)*(sub_nus)
	sub_nus=sub_nus+0.5*sub_deltas #central frequencies

	#Bands
	bands=[sub_nus[reduce(logical_and,(sub_nus<=nus[i],sub_nus>=nus[i-1]))] for i in range(Nbfreq)][1:]
	bands.append(sub_nus[sub_nus>=np.max(nus)])
	numbers=np.cumsum(np.array([len(bands[i]) for i in range(Nbfreq)]))
	numbers=np.append(0,numbers)
	bands_numbers=np.array([(np.arange(numbers[i-1],numbers[i])) for i in range(Nbfreq+1)])[1:]

	################
	### Coverage ###
	################
	sub_instruments=[QubicInstrument(filter_nu=sub_nus[i]*10**9,detector_nep=2.7e-17) for i in range(Nbsubfreq)]
	sub_acqs=[QubicAcquisition(sub_instruments[i], sampling, scene,photon_noise=True, effective_duration=effective_duration) for i in range(Nbsubfreq)]
	covlim=0.1
	coverage = np.array([sub_acqs[i].get_coverage() for i in range(Nbsubfreq)])
	observed = [(coverage[i] > covlim*np.max(coverage[i])) for i in range(Nbsubfreq)]
	obs=reduce(logical_and,tuple(observed[i] for i in range(Nbsubfreq)))
	pack = PackOperator(obs, broadcast='rightward')

	######################
	### Reconstruction ###
	######################
	sub_instruments=[QubicInstrument(filter_nu=sub_nus[i]*10**9,detector_nep=2.7e-17) for i in range(Nbsubfreq)]
	sub_acqs=[QubicAcquisition(sub_instruments[i], sampling, scene,photon_noise=True, effective_duration=3) for i in range(Nbsubfreq)]
	sub_acqs_restricted=[sub_acqs[i][:,:,obs] for i in range(Nbsubfreq)]
	operators=np.array([sub_acqs_restricted[i].get_operator() for i in range(Nbsubfreq)])
	K=np.array([np.sum([operators[j]*sub_deltas[j]*10**9 for j in bands_numbers[i]],axis=0) for i in range(Nbfreq)])
	H=BlockRowOperator([K[i] for i in range(Nbfreq)], new_axisin=0)
	invntt = sub_acqs[0].get_invntt_operator()
	A=H.T*invntt*H
	b = (H.T * invntt)(Y)
	preconditioner = BlockDiagonalOperator([DiagonalOperator(1 / coverage[0][obs], broadcast='rightward') for i in range(Nbfreq)], new_axisin=0) 
	solution_qubic = pcg(A, b, M=preconditioner ,disp=True, tol=1e-3, maxiter=1000)
	blockpack = BlockDiagonalOperator([pack for i in range(Nbfreq)], new_axisin=0)
	maps =blockpack.T(solution_qubic['x'])  
	maps[:,~obs] = 0
	return maps, bands
示例#9
0
    def tod2map(self, tod, cov=None, tol=1e-5, maxiter=1000, verbose=True):
        '''
        Reconstruct map from tod
        '''
        H = self.get_operator()
        invntt = self.get_invntt_operator()

        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(cov)
        solution = pcg(A, b, M=preconditioner, 
            disp=verbose, tol=tol, maxiter=maxiter)
        return solution['x']
示例#10
0
    def tod2map(self, tod, cov=None, tol=1e-5, maxiter=1000, verbose=True):
        '''
        Reconstruct map from tod
        '''
        H = self.get_operator()
        invntt = self.get_invntt_operator()

        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(cov)
        solution = pcg(A, b, M=preconditioner, 
            disp=verbose, tol=tol, maxiter=maxiter)
        return solution['x']
示例#11
0
def toloop(arguments):
    print(arguments)
    print('Getting noise')
    noise = acq.get_noise()/np.sqrt(real_duration*ndetectors)
    print('noise done')
    #noise = highpass_tod(noise, ts, 0.05)
    y = y_noiseless + noise
    b = (H.T * invntt)(y)
    solution_qubic = pcg(A, b, M=preconditioner, disp=True, tol=1e-3, maxiter=1000)
    maps = pack.T(solution_qubic['x'])
    maps[~observed] = 0
    x0_convolved[~observed,:]=0    
    residuals = maps-x0_convolved
    pxsize_arcmin2 = 4*pi*(180/pi)**2 / (12*nside**2) * 60**2
    sigs= np.std(residuals[observed,:], axis=0)*sqrt(pxsize_arcmin2)
    return(sigs)
示例#12
0
def test_ls():
    H = projection
    m = pcg(H.T * invntt * H, (H.T*invntt)(tod), M=M, tol=1e-7)
    map_ls_packed = Map(m['x'])
    map_ls_packed.header['TIME'] = m['time']

    #print('Elapsed time:', map_ls_packed.header['TIME'])
    #from tamasis import mapper_ls
    #map_ls_packed = mapper_ls(tod, projection, invntt=invntt, tol=1e-7, M=M,
    #                          callback=callback, criterion=False,
    #                          profile=profile)
    if profile:
        return
    print('Elapsed time:', map_ls_packed.header['TIME'])
    assert m['nit'] < 50
    ref = packing(Map(path + 'madmapSpirePSW.fits'))
    assert_allclose(map_ls_packed, ref, atol=1e-5)
示例#13
0
    def tod2map(self, tod, d):
        tol = d['tol']
        maxiter = d['maxiter']
        verbose = d['verbose']
        H = self.get_operator()
        invntt = self.get_invntt_operator()
        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(H)
        solution = pcg(A,
                       b,
                       M=preconditioner,
                       disp=verbose,
                       tol=tol,
                       maxiter=maxiter)
        return solution['x']
示例#14
0
def TOD2map(tod,pointing,nside,kmax=2,disp=True,P=False,covmin=10):
    qubic = QubicInstrument('monochromatic,nopol',nside=nside)
    #### configure observation
    obs = QubicAcquisition(qubic, pointing)
    if not P:
        P = obs.get_projection_peak_operator(kmax=kmax)
    coverage = P.T(np.ones_like(tod))
    mask=coverage < covmin
    P.matrix.pack(mask)
    P_packed = ProjectionInMemoryOperator(P.matrix)
    unpack = UnpackOperator(mask)
    # data
    solution = pcg(P_packed.T * P_packed, P_packed.T(tod), M=DiagonalOperator(1/coverage[~mask]), disp=disp)
    output_map = unpack(solution['x'])
    output_map[mask] = np.nan
    coverage[mask]=np.nan
    return(output_map,mask,coverage)
示例#15
0
    def tod2map(self, tod, d, cov=None):
        tol = d['tol']
        maxiter = d['maxiter']
        verbose = d['verbose']
        p = self.planck
        H = []
        for q, w in zip(self.qubic, self.weights):
            H.append(QubicPlanckAcquisition(q, p).get_operator() * w)
        H = np.array(H)
        H = [H[(self.qubic.nus > mi) * (self.qubic.nus < ma)].sum() * \
             self.weights[(self.qubic.nus > mi) * (self.qubic.nus < ma)].sum() \
             for (mi, ma) in self.qubic.bands]
        invntt = self.get_invntt_operator()

        A_columns = []
        for h1 in H:
            c = []
            for h2 in H:
                c.append(h2.T * invntt * h1)
            A_columns.append(BlockColumnOperator(c, axisout=0))
        A = BlockRowOperator(A_columns, axisin=0)

        H = [h.T for h in H]
        b = BlockColumnOperator(H, new_axisout=0) * (invntt * tod)
        sh = b.shape
        if (len(self.qubic.nus) -
                1) > 1:  # If number of subbands is more than one
            if len(sh) == 3:
                b = b.reshape((sh[0] * sh[1], sh[2]))
            else:
                b = b.reshape((sh[0], sh[1]))

        preconditioner = self.get_preconditioner(cov)
        solution = pcg(A,
                       b,
                       M=preconditioner,
                       disp=verbose,
                       tol=tol,
                       maxiter=maxiter)
        #        solution = pcg(A, b, disp=verbose, tol=tol, maxiter=maxiter)
        if len(sh) == 3:
            maps_recon = solution['x'].reshape(sh[0], sh[1], sh[2])
        else:
            maps_recon = solution['x'].reshape(sh[0], sh[1])
        return maps_recon
示例#16
0
    def tod2map(self, tod, d, cov=None):
        """
        Reconstruct map from tod
        """
        tol = d['tol']
        maxiter = d['maxiter']
        verbose = d['verbose']
        H = self.get_operator()
        invntt = self.get_invntt_operator()

        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(cov)
        solution = pcg(A,
                       b,
                       M=preconditioner,
                       disp=verbose,
                       tol=tol,
                       maxiter=maxiter)
        return solution['x'], solution['nit'], solution['error']
示例#17
0
    def tod2map(self, tod, cov=None, tol=1e-5, maxiter=1000, verbose=True):
        p = self.planck
        H = []
        for q, w in zip(self.qubic, self.weights):
            H.append(QubicPlanckAcquisition(q, p).get_operator() * w)
        H = np.array(H)
        H = [H[(self.qubic.nus > mi) * (self.qubic.nus < ma)].sum() * \
             self.weights[(self.qubic.nus > mi) * (self.qubic.nus < ma)].sum() \
                for (mi, ma) in self.qubic.bands]
        invntt = self.get_invntt_operator()

        A_columns = []
        for h1 in H:
            c = []
            for h2 in H:
                c.append(h2.T * invntt * h1)
            A_columns.append(BlockColumnOperator(c, axisout=0))
        A = BlockRowOperator(A_columns, axisin=0)

        H = [h.T for h in H]
        b = BlockColumnOperator(H, new_axisout=0) * (invntt * tod)
        sh = b.shape
        if len(self.qubic.nus) - 1 > 1: # If number of subbands is more than one
            if len(sh) == 3:
                b = b.reshape((sh[0] * sh[1], sh[2]))
            else:
                b = b.reshape((sh[0] * sh[1]))

        preconditioner = self.get_preconditioner(cov)
        solution = pcg(A, b, M=preconditioner, disp=verbose, tol=tol, maxiter=maxiter)
#        solution = pcg(A, b, disp=verbose, tol=tol, maxiter=maxiter)
        if len(sh) == 3:
            maps_recon = solution['x'].reshape(sh[0], sh[1], sh[2])
        else:
            maps_recon = solution['x'].reshape(sh[0], sh[1])
        return maps_recon
示例#18
0
    def tod2map(self, tod, d):
        '''
        Reconstruct map from tod
        '''
        tol = d['tol']
        maxiter = d['maxiter']
        cov = d['cov']
        verbose = d['verbose']
        sampling = self[0].sampling

        H = self.get_operator()
        invntt = self.get_invntt_operator(d)

        A = H.T * invntt * H
        b = H.T * invntt * tod

        preconditioner = self.get_preconditioner(cov)
        solution = pcg(A,
                       b,
                       M=preconditioner,
                       disp=verbose,
                       tol=tol,
                       maxiter=maxiter)
        return solution['x']
    input_map = np.zeros((hp.nside2npix(nside), 3))
    acq_planck = PlanckAcquisition(band, acq_qubic.scene, true_sky=input_map)
    acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck)

    obs_noiseless = acq_fusion.get_observation(noiseless=True)
    H = acq_fusion.get_operator()
    invntt = acq_fusion.get_invntt_operator()
    A = H.T * invntt * H

    for realization in xrange(nrealizations):
        print 'rank={}: realization {} / {}'.format(rank, realization + 1, nrealizations)

        obs = obs_noiseless + acq_fusion.get_noise()
        b = H.T * invntt * obs

        solution = pcg(A, b, disp=True, maxiter=maxiter)
        rec_map = solution['x']

        if rank == 0:
            file_name = 'angspeed{}_delta_az{}_realization{}.fits'.format(angspeed,
                                                                          delta_az,
                                                                          realization)
            with open(directory + '/rec_map_' + file_name, 'w') as f:
                write_map(f, rec_map, mask=coverage>0.)

    # release the pointing matrix
    del H, A
    gc.collect()
    print rank, 'got here'

print('finished')
coverage = H.T(np.ones(H.shapeout))
mask = coverage > 0

# restrict the scene to the observed pixels
acq_restricted = acq[..., mask]
H_restricted = acq_restricted.get_operator()
x0_restricted = x0[mask]
y = H_restricted(x0_restricted)
invntt = acq_restricted.get_invntt_operator()

# solve for x
A = H_restricted.T * invntt * H_restricted
b = H_restricted.T(invntt(y))
solution = pcg(A,
               b,
               M=DiagonalOperator(1 / coverage[mask]),
               disp=True,
               tol=1e-4)
x = np.zeros_like(x0)
x[mask] = solution['x']


# some display
def display(x, title, lim=200):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x,
                rot=center_gal,
                reso=5,
                xsize=600,
                min=-lim,
示例#21
0
for i in xrange(len(all_instruments)):
	acq_qubic = QubicAcquisition(150, sampling, nside=nside,
                             detector_nep=detector_nep[i])
	all_coverages.append(acq_qubic.get_coverage())
	convolved_sky = acq_qubic.instrument.get_convolution_peak_operator()(sky)
	acq_planck = PlanckAcquisition(150, acq_qubic.scene, true_sky=convolved_sky)
	acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck)

	H = acq_fusion.get_operator()
	invntt = acq_fusion.get_invntt_operator()
	obs = acq_fusion.get_observation()

	A = H.T * invntt * H
	b = H.T * invntt * obs

	solution_fusion = pcg(A, b, disp=True)
	all_solutions_fusion.append(solution_fusion)





mask = all_coverages[0] > np.max(all_coverages[0]/10)

reso=3
Trange=[10, 10, 10]
for i in xrange(len(nbptg)):
	figure(i)
	resid = all_solutions_fusion[i]['x'] - convolved_sky
	resid[~mask,:] = 0
	display(resid, 'Difference map', iplot=7, reso=reso, Trange=Trange)
示例#22
0
def _tod2map(acq, tod, coverage_threshold, max_nbytes, callback, disp_pcg,
             maxiter, tol, criterion, full_output, save_map, hyper):
    # coverage normalization:
    # sum coverage = #detectors x #samplings for a uniform secondary beam
    H = acq.get_operator()
    coverage = H.T(ones(H.shapeout))
    if acq.scene.kind == 'IQU':
        coverage = coverage[..., 0]
    elif acq.scene.kind == 'QU':
        raise NotImplementedError()
    theta, phi = acq.instrument.detector.theta, acq.instrument.detector.phi
    ndetectors = acq.instrument.detector.comm.allreduce(
        np.sum(acq.instrument.secondary_beam(theta, phi)))
    nsamplings = acq.sampling.comm.allreduce(len(acq.sampling))
    coverage *= ndetectors * nsamplings / np.sum(coverage)
    cov = coverage[coverage > 0]
    i = np.argsort(cov)
    cdf = np.cumsum(cov[i])
    j = np.argmax(cdf >= coverage_threshold * cdf[-1])
    threshold = cov[i[j]]
    mask = coverage >= threshold
    rejected = 1 - np.sum(mask) / cov.size
    if acq.comm.rank == 0 and coverage_threshold > 0:
        print('Total coverage:', cdf[-1])
        print('Threshold coverage set to:', threshold)
        print('Fraction of rejected observed pixels:', rejected)
    header = OrderedDict()
    coverage = coverage.view(ndarraywrap)
    coverage.header = header
    header['thresrel'] = coverage_threshold, 'Relative coverage threshold'
    header['thresabs'] = threshold, 'Absolute coverage threshold'
    header['fracrej'] = rejected, 'Fraction of rejected observed pixels'

    acq_restricted = acq[..., mask]
    H = acq_restricted.get_operator()
    invNtt = acq_restricted.get_invntt_operator()
    M = (H.T * H * np.ones(H.shapein))[..., 0]
    preconditioner = DiagonalOperator(1 / M, broadcast='rightward')
    #    preconditioner = DiagonalOperator(1/coverage[mask], broadcast='rightward')
    nsamplings = acq.comm.allreduce(len(acq.sampling))
    npixels = np.sum(mask)

    A = H.T * invNtt * H / nsamplings
    if hyper != 0:
        L = HealpixLaplacianOperator(acq.scene.nside)
        L = L.restrict(mask, inplace=True).corestrict(mask, inplace=True)
        A = A - hyper / npixels / 4e5 * L

    if criterion:

        def f(x):
            Hx_y = H(x)
            Hx_y -= tod
            out = [np.dot(Hx_y.ravel(), invNtt(Hx_y).ravel()) / nsamplings]
            if hyper != 0:
                out += [
                    -np.dot(x.ravel(), hyper / npixels / 4e5 * L(x).ravel())
                ]
            else:
                out += [0.]
            return out

        def callback(self):
            criteria = f(self.x)
            if len(criteria) == 1:
                details = ''
            else:
                fmt = ', '.join(len(criteria) * ['{:e}'])
                details = ' (' + fmt.format(*criteria) + ')'
            print('{:4}: {:e} {:e}{}'.format(self.niterations, self.error,
                                             sum(criteria), details))
            if not hasattr(self, 'history'):
                self.history = {}
                self.history['criterion'] = []
                self.history['error'] = []
                self.history['iteration'] = []
            self.history['criterion'].append(criteria)
            self.history['error'].append(self.error)
            self.history['iteration'].append(self.niterations)
            if save_map is not None and self.niterations in save_map:
                if not hasattr(self, 'xs'):
                    self.xs = {}
                self.xs[self.niterations] = self.x.copy()

    solution = pcg(A,
                   H.T(invNtt(tod)) / nsamplings,
                   M=preconditioner,
                   callback=callback,
                   disp=disp_pcg,
                   maxiter=maxiter,
                   tol=tol)
    output = acq_restricted.scene.unpack(solution['x']), coverage
    if full_output:
        algo = solution['algorithm']
        algo.H = H
        if criterion:
            pack = PackOperator(mask, broadcast='rightward')
            algo.f = asoperator(f, shapeout=2)(pack)
        output += (algo, )
    return output
示例#23
0
def reconstruction(
        racenter, deccenter, NPOINTS, NFREQS, filter_name, scene, 
        maxiter, tol, rank, n, start):
    sampling = create_random_pointings([racenter, deccenter], 1000, 10)
    detector_nep = 4.7e-17 * np.sqrt(
        len(sampling) * sampling.period / (365 * 86400))

    #x0 = read_map(PATH + 'syn256_pol.fits')
    x0 = np.zeros((12*nside**2, 3))
    q = MultiQubicInstrument(
        NPOINTS=NPOINTS, NFREQS=NFREQS, filter_name=filter_name, 
        detector_nep=detector_nep)
    
    C_nf = q.get_convolution_peak_operator()
    conv_sky_ = C_nf(x0)
    
    fwhm_t = np.sqrt(q.synthbeam.peak150.fwhm**2 - C_nf.fwhm**2)
    C_transf = HealpixConvolutionGaussianOperator(fwhm=fwhm_t)
    
    acq = MultiQubicAcquisition(q, sampling, scene=scene)
    H = acq.get_operator()
    coverage = acq.get_coverage(H)
    
    acq_planck = MultiPlanckAcquisition(
        np.int(filter_name/1e9), scene, true_sky=conv_sky_) 
    acq_fusion = QubicPlanckAcquisition(acq, acq_planck)
    
    H = acq_fusion.get_operator()
    y = acq_fusion.get_observation()
    invntt = acq_fusion.get_invntt_operator()
    
    A = H.T * invntt * H
    b = H.T * invntt * y
    
    solution_fusion = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)
    x_rec_fusion_ = solution_fusion['x']
    x_rec_fusion = C_transf(x_rec_fusion_)

    #H = acq.get_operator()
    #COV = acq.get_coverage(H)
    #y = acq.get_observation(conv_sky_)
    #invntt = acq.get_invntt_operator()
    
    # A = H.T * invntt * H
    # b = H.T * invntt * y
    
    # solution_qubic = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)
    # x_rec_qubic_ = solution_qubic['x']
    # x_rec_qubic = C_transf(x_rec_qubic_)

    # conv_sky = C_transf(conv_sky_)
    
    path = '/home/fincardona/Qubic/map_making/maps/montecarlo'
    if rank==0:
        hp.write_map('%s/fusion_n{}_N{}_s{}_mi{}_niter{}.fits'.format(
            NFREQS, NPOINTS, len(sampling), maxiter, n+start) % path, 
            x_rec_fusion.T)
        #hp.write_map('maps_test/qubic_n{}_N{}_s{}_mi{}.fits'.format(
        #    NFREQS, NPOINTS, len(sampling), maxiter), x_rec_qubic.T)
    gc.collect()
    return coverage, sampling, path
示例#24
0
#### configure observation
obs = QubicConfiguration(qubic, pointings)
C = obs.get_convolution_peak_operator()
P = obs.get_projection_peak_operator(kmax=kmax)
H = P * C

# Produce the Time-Ordered data
tod = H(input_map)

# map-making
coverage = P.T(np.ones_like(tod))
mask = coverage < 10
P.matrix.pack(mask)
P_packed = ProjectionInMemoryOperator(P.matrix)
unpack = UnpackOperator(mask)
solution = pcg(P_packed.T * P_packed, P_packed.T(tod), M=DiagonalOperator(1/coverage[~mask]), disp=True)
output_map = unpack(solution['x'])

# some display
output_map[mask] = np.nan
hp.gnomview(input_map, rot=[0,90], reso=0.3, xsize=600,title='Input Map')
hp.gnomview(output_map, rot=[0,90], reso=0.3, xsize=600,title='Output Map')

# get theta
iprings=np.arange(12*nside**2)
vecs=hp.pix2vec(int(nside),iprings[~mask])
vec0=hp.pix2vec(int(nside),0)
angles=np.arccos(np.dot(np.transpose(vec0),vecs))
themap=output_map[~mask]
clf()
plot(angles*180/np.pi,themap,'b.')
示例#25
0
sampling = create_random_pointings([racenter, deccenter], 1000, 10)
scene = QubicScene(nside)

acq_qubic = QubicAcquisition(150, sampling, scene, effective_duration=1)
convolved_sky = acq_qubic.instrument.get_convolution_peak_operator()(sky)
acq_planck = PlanckAcquisition(150, acq_qubic.scene, true_sky=convolved_sky)
acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck)

H = acq_fusion.get_operator()
invntt = acq_fusion.get_invntt_operator()
y = acq_fusion.get_observation()

A = H.T * invntt * H
b = H.T * invntt * y

solution_fusion = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)

acq_qubic = QubicAcquisition(150, sampling, scene, effective_duration=1)
H = acq_qubic.get_operator()
invntt = acq_qubic.get_invntt_operator()
y, sky_convolved = acq_qubic.get_observation(sky, convolution=True)

A = H.T * invntt * H
b = H.T * invntt * y

solution_qubic = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)


# some display
def display(input, msg, iplot=1):
    out = []
示例#26
0
        coverage = acq.get_coverage()
        observed = coverage > covlim * np.max(coverage)
        acq_restricted = acq[:, :, observed]
        H = acq_restricted.get_operator()
        x0_convolved = C(input_maps)
        pack = PackOperator(observed, broadcast='rightward')
        y_noiseless = H(pack(x0_convolved))
        invntt = acq.get_invntt_operator()
        A = H.T * invntt * H
        preconditioner = DiagonalOperator(1 / coverage[observed], broadcast='rightward')
        
        for nn in xrange(nbmc):
            noise = acq.get_noise()/np.sqrt(real_duration*ndetectors)
            y = y_noiseless + noise
            b = (H.T * invntt)(y)
            solution_qubic = pcg(A, b, M=preconditioner, disp=True, tol=1e-3, maxiter=1000)
            maps = pack.T(solution_qubic['x'])
            maps[~observed] = 0
            x0_convolved[~observed,:]=0    
        

            residuals = maps-x0_convolved
            freq, ps = powspec_inst(ts, y_noiseless+noise)

            pxsize_arcmin2 = 4*pi*(180/pi)**2 / (12*nside**2) * 60**2
            sigs= np.std(residuals[observed,:], axis=0)*sqrt(pxsize_arcmin2)
            print(s, f, nn, sigs)
            allsigs[i,j,nn,:] = sigs

            clf()
            subplot(2,1,1)
示例#27
0
detector_nep = 4.7e-17/np.sqrt(365*86400 / len(sampling)*sampling.period)

acq_qubic = QubicAcquisition(150, sampling, nside=nside,
                             detector_nep=detector_nep)
convolved_sky = acq_qubic.instrument.get_convolution_peak_operator()(sky)
acq_planck = PlanckAcquisition(150, acq_qubic.scene, true_sky=convolved_sky)
acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck)

H = acq_fusion.get_operator()
invntt = acq_fusion.get_invntt_operator()
obs = acq_fusion.get_observation()

A = H.T * invntt * H
b = H.T * invntt * obs

solution_fusion = pcg(A, b, disp=True)



H = acq_qubic.get_operator()
invntt = acq_qubic.get_invntt_operator()
obs, sky_convolved = acq_qubic.get_observation(sky, convolution=True)

A = H.T * invntt * H
b = H.T * invntt * obs

solution_qubic = pcg(A, b, disp=True)

# some display

示例#28
0
    detector_nep = 4.7e-17/np.sqrt(365*86400 / len(sampling)*sampling.period)/fact[i]

    acq_qubic = QubicAcquisition(150, sampling, nside=nside,
                                 detector_nep=detector_nep)
    convolved_sky = acq_qubic.instrument.get_convolution_peak_operator()(sky)
    acq_planck = PlanckAcquisition(150, acq_qubic.scene, true_sky=convolved_sky)
    acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck)

    H = acq_fusion.get_operator()
    invntt = acq_fusion.get_invntt_operator()
    obs = acq_fusion.get_observation()

    A = H.T * invntt * H
    b = H.T * invntt * obs

    solution_fusion = pcg(A, b, disp=True, maxiter=1000000, tol=1e-5)
    sols.append(solution_fusion)



# some display
center = equ2gal(racenter, deccenter)

def display(input, msg, iplot=1, reso=5, Trange=[100, 5, 5], xsize=800, subx=3, suby=3):
    out = []
    for i, (kind, lim) in enumerate(zip('IQU', Trange)):
        map = input[..., i]
        out += [hp.gnomview(map, rot=center, reso=reso, xsize=xsize, min=-lim,
                            max=lim, title=msg + ' ' + kind,
                            sub=(suby, subx, iplot + i), return_projected_map=True)]
    return out
示例#29
0
    tod = H(input_map)
    tods[kind] = tod
    pTxs[kind] = H.T(tod)[coverage > 0]
    if kind != 'QU':
        pTx_pT1 = P.pTx_pT1(tod)
        pTx_pT1s[kind] = pTx_pT1[0][coverage > 0], pTx_pT1[1][coverage > 0]
    cbik = P.canonical_basis_in_kernel()
    mask = coverage > 10
    P = P.restrict(mask, inplace=True)
    unpack = UnpackOperator(mask, broadcast='rightward')
    x0 = unpack.T(input_map)

    M = DiagonalOperator(1 / coverage[mask], broadcast='rightward')

    H = W * P
    solution = pcg(H.T * H, H.T(tod), M=M, disp=True, tol=1e-5)
    pT1s[kind] = coverage
    cbiks[kind] = cbik
    outputs[kind] = solution['x']


def test_sky():
    assert_same(tods['I'], tods['IQU'][..., 0].astype(np.float32))
    assert_same(tods['QU'], tods['IQU'][..., 1:].astype(np.float32))

    assert_same(pTxs['I'], pTxs['IQU'][..., 0].astype(np.float32))
    assert_same(pTxs['QU'], pTxs['IQU'][..., 1:].astype(np.float32))

    assert_same(pTx_pT1s['I'][0], pTx_pT1s['IQU'][0].astype(np.float32))
    assert_same(pTx_pT1s['I'][1], pTx_pT1s['IQU'][1].astype(np.float32))
示例#30
0
def reconstruction(racenter, deccenter, NPOINTS, NFREQS, filter_name, scene,
                   maxiter, tol, rank, n, start):
    sampling = create_random_pointings([racenter, deccenter], 1000, 10)
    detector_nep = 4.7e-17 * np.sqrt(
        len(sampling) * sampling.period / (365 * 86400))

    #x0 = read_map(PATH + 'syn256_pol.fits')
    x0 = np.zeros((12 * nside**2, 3))
    q = MultiQubicInstrument(NPOINTS=NPOINTS,
                             NFREQS=NFREQS,
                             filter_name=filter_name,
                             detector_nep=detector_nep)

    C_nf = q.get_convolution_peak_operator()
    conv_sky_ = C_nf(x0)

    fwhm_t = np.sqrt(q.synthbeam.peak150.fwhm**2 - C_nf.fwhm**2)
    C_transf = HealpixConvolutionGaussianOperator(fwhm=fwhm_t)

    acq = MultiQubicAcquisition(q, sampling, scene=scene)
    H = acq.get_operator()
    coverage = acq.get_coverage(H)

    acq_planck = MultiPlanckAcquisition(np.int(filter_name / 1e9),
                                        scene,
                                        true_sky=conv_sky_)
    acq_fusion = QubicPlanckAcquisition(acq, acq_planck)

    H = acq_fusion.get_operator()
    y = acq_fusion.get_observation()
    invntt = acq_fusion.get_invntt_operator()

    A = H.T * invntt * H
    b = H.T * invntt * y

    solution_fusion = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)
    x_rec_fusion_ = solution_fusion['x']
    x_rec_fusion = C_transf(x_rec_fusion_)

    #H = acq.get_operator()
    #COV = acq.get_coverage(H)
    #y = acq.get_observation(conv_sky_)
    #invntt = acq.get_invntt_operator()

    # A = H.T * invntt * H
    # b = H.T * invntt * y

    # solution_qubic = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)
    # x_rec_qubic_ = solution_qubic['x']
    # x_rec_qubic = C_transf(x_rec_qubic_)

    # conv_sky = C_transf(conv_sky_)

    path = '/home/fincardona/Qubic/map_making/maps/montecarlo'
    if rank == 0:
        hp.write_map(
            '%s/fusion_n{}_N{}_s{}_mi{}_niter{}.fits'.format(
                NFREQS, NPOINTS, len(sampling), maxiter, n + start) % path,
            x_rec_fusion.T)
        #hp.write_map('maps_test/qubic_n{}_N{}_s{}_mi{}.fits'.format(
        #    NFREQS, NPOINTS, len(sampling), maxiter), x_rec_qubic.T)
    gc.collect()
    return coverage, sampling, path
示例#31
0
y = H(x0)

# noise
psd = _gaussian_psd_1f(len(acq.sampling), sigma=sigma, fknee=fknee,
                       fslope=fslope, sampling_frequency=1/ts)
invntt = acq.get_invntt_operator()
noise = acq.get_noise()

# map-making
coverage = P.pT1()
mask = coverage > 10
P = P.restrict(mask, inplace=True)
unpack = UnpackOperator(mask)

# map without covariance matrix
solution1 = pcg(P.T * P, P.T(y + noise),
                M=DiagonalOperator(1/coverage[mask]), disp=True)
x1 = unpack(solution1['x'])

# map with covariance matrix
solution2 = pcg(P.T * invntt * P, (P.T * invntt)(y + noise),
                M=DiagonalOperator(1/coverage[mask]), disp=True)
x2 = unpack(solution2['x'])


def display(x, title):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x, rot=center, reso=5, xsize=600, min=-200, max=200,
                title=title)

center = equ2gal(racenter, deccenter)
x0_convolved = acq.get_convolution_peak_operator()(x0)
H = acq.get_operator()
coverage = H.T(np.ones(H.shapeout))
mask = coverage > 0

# restrict the scene to the observed pixels
acq_restricted = acq[..., mask]
H_restricted = acq_restricted.get_operator()
x0_restricted = x0[mask]
y = H_restricted(x0_restricted)
invntt = acq_restricted.get_invntt_operator()

# solve for x
A = H_restricted.T * invntt * H_restricted
b = H_restricted.T(invntt(y))
solution = pcg(
    A, b, M=DiagonalOperator(1/coverage[mask]), disp=True, tol=1e-4)
x = np.zeros_like(x0)
x[mask] = solution['x']


# some display
def display(x, title, lim=200):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x, rot=center_gal, reso=5, xsize=600, min=-lim, max=lim,
                title=title)

display(x0, 'Original map')
display(x0_convolved, 'Convolved original map')
display(x, 'Reconstructed map (gaussian approximation)')
display(x - x0_convolved, 'Residual map (gaussian approximation)')
示例#33
0
clf()
xn,yn,dxn,dyn=pyquad.profile(ang.flatten(),cormc.flatten(),0.001,50,200,plot=True,dispersion=False)

clf()
imshow(abs(cormc),interpolation='nearest',vmin=0,vmax=0.2)
title('Map Correlation Matrix')
colorbar()



clf()
errorbar(xn,yn,xerr=dxn,yerr=dyn,fmt='bo')
from Homogeneity import SplineFitting
spl=SplineFitting.MySplineFitting(xn,yn,dyn,100)
#plot(xn,spl(xn),'r',lw=3)
plot(xn,xn*0,'k--')
ylabel('Noise Correlation Matrix')
xlabel('Angle [degrees]')




sol2 = pcg(P_packed.T * P_packed, P_packed.T(tod*0+1), M=DiagonalOperator(1/coverage[~mask]), disp=True)
errmap = unpack(sol2['x'])
hp.gnomview(errmap,rot=[0,90],reso=14)
mm=np.zeros(12*nside**2)
mm[maskok]=sqrt(diag(covmc))
hp.gnomview(mm,rot=[0,90],reso=14)


示例#34
0
def _tod2map(acq, tod, coverage_threshold, max_nbytes, callback,
             disp_pcg, maxiter, tol, criterion, full_output, save_map, hyper):
    # coverage normalization:
    # sum coverage = #detectors x #samplings for a uniform secondary beam
    H = acq.get_operator()
    coverage = H.T(ones(H.shapeout))
    if acq.scene.kind == 'IQU':
        coverage = coverage[..., 0]
    elif acq.scene.kind == 'QU':
        raise NotImplementedError()
    theta, phi = acq.instrument.detector.theta, acq.instrument.detector.phi
    ndetectors = acq.instrument.detector.comm.allreduce(
        np.sum(acq.instrument.secondary_beam(theta, phi)))
    nsamplings = acq.sampling.comm.allreduce(len(acq.sampling))
    coverage *= ndetectors * nsamplings / np.sum(coverage)
    cov = coverage[coverage > 0]
    i = np.argsort(cov)
    cdf = np.cumsum(cov[i])
    j = np.argmax(cdf >= coverage_threshold * cdf[-1])
    threshold = cov[i[j]]
    mask = coverage >= threshold
    rejected = 1 - np.sum(mask) / cov.size
    if acq.comm.rank == 0 and coverage_threshold > 0:
        print('Total coverage:', cdf[-1])
        print('Threshold coverage set to:', threshold)
        print('Fraction of rejected observed pixels:', rejected)
    header = OrderedDict()
    coverage = coverage.view(ndarraywrap)
    coverage.header = header
    header['thresrel'] = coverage_threshold, 'Relative coverage threshold'
    header['thresabs'] = threshold, 'Absolute coverage threshold'
    header['fracrej'] = rejected, 'Fraction of rejected observed pixels'

    acq_restricted = acq[..., mask]
    H = acq_restricted.get_operator()
    invNtt = acq_restricted.get_invntt_operator()
    M = (H.T * H * np.ones(H.shapein))[..., 0]
    preconditioner = DiagonalOperator(1/M, broadcast='rightward')
#    preconditioner = DiagonalOperator(1/coverage[mask], broadcast='rightward')
    nsamplings = acq.comm.allreduce(len(acq.sampling))
    npixels = np.sum(mask)

    A = H.T * invNtt * H / nsamplings
    if hyper != 0:
        L = HealpixLaplacianOperator(acq.scene.nside)
        L = L.restrict(mask, inplace=True).corestrict(mask, inplace=True)
        A = A - hyper / npixels / 4e5 * L

    if criterion:
        def f(x):
            Hx_y = H(x)
            Hx_y -= tod
            out = [np.dot(Hx_y.ravel(), invNtt(Hx_y).ravel()) / nsamplings]
            if hyper != 0:
                out += [-np.dot(x.ravel(),
                                hyper / npixels / 4e5 * L(x).ravel())]
            else:
                out += [0.]
            return out

        def callback(self):
            criteria = f(self.x)
            if len(criteria) == 1:
                details = ''
            else:
                fmt = ', '.join(len(criteria) * ['{:e}'])
                details = ' (' + fmt.format(*criteria) + ')'
            print('{:4}: {:e} {:e}{}'.format(self.niterations, self.error,
                                             sum(criteria), details))
            if not hasattr(self, 'history'):
                self.history = {}
                self.history['criterion'] = []
                self.history['error'] = []
                self.history['iteration'] = []
            self.history['criterion'].append(criteria)
            self.history['error'].append(self.error)
            self.history['iteration'].append(self.niterations)
            if save_map is not None and self.niterations in save_map:
                if not hasattr(self, 'xs'):
                    self.xs = {}
                self.xs[self.niterations] = self.x.copy()

    solution = pcg(A, H.T(invNtt(tod)) / nsamplings, M=preconditioner,
                   callback=callback, disp=disp_pcg, maxiter=maxiter, tol=tol)
    output = acq_restricted.scene.unpack(solution['x']), coverage
    if full_output:
        algo = solution['algorithm']
        algo.H = H
        if criterion:
            pack = PackOperator(mask, broadcast='rightward')
            algo.f = asoperator(f, shapeout=2)(pack)
        output += (algo,)
    return output
# noise
sigma = acq.instrument.detector.nep / np.sqrt(2 * sampling.period)
psd = _gaussian_psd_1f(len(acq.sampling), sigma=sigma, fknee=fknee,
                       fslope=fslope, sampling_frequency=1/ts)
invntt = acq.get_invntt_operator()
noise = acq.get_noise()
noise[...] = 0

# map-making
coverage = acq.get_coverage()
mask = coverage / coverage.max() > 0.01

acq_red = acq[..., mask]
H_ga_red = acq_red.get_operator()
# map without covariance matrix
solution1 = pcg(H_ga_red.T * H_ga_red, H_ga_red.T(y + noise),
                M=DiagonalOperator(1/coverage[mask]), disp=True)
x1 = acq_red.scene.unpack(solution1['x'])

# map with covariance matrix
solution2 = pcg(H_ga_red.T * invntt * H_ga_red,
                (H_ga_red.T * invntt)(y + noise),
                M=DiagonalOperator(1/coverage[mask]), disp=True)
x2 = acq_red.scene.unpack(solution2['x'])


def display(x, title, min=None, max=None):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x, rot=center, reso=5, xsize=600, min=min, max=max,
                title=title)
                       fknee=fknee,
                       fslope=fslope,
                       sampling_frequency=1 / ts)
invntt = acq.get_invntt_operator()
noise = acq.get_noise()
noise[...] = 0

# map-making
coverage = acq.get_coverage()
mask = coverage / coverage.max() > 0.01

acq_red = acq[..., mask]
H_ga_red = acq_red.get_operator()
# map without covariance matrix
solution1 = pcg(H_ga_red.T * H_ga_red,
                H_ga_red.T(y + noise),
                M=DiagonalOperator(1 / coverage[mask]),
                disp=True)
x1 = acq_red.scene.unpack(solution1['x'])

# map with covariance matrix
solution2 = pcg(H_ga_red.T * invntt * H_ga_red,
                (H_ga_red.T * invntt)(y + noise),
                M=DiagonalOperator(1 / coverage[mask]),
                disp=True)
x2 = acq_red.scene.unpack(solution2['x'])


def display(x, title, min=None, max=None):
    x = x.copy()
    x[~mask] = np.nan
    hp.gnomview(x,
示例#37
0
def reconstruct(Y,nu_min,nu_max,delta_nu,subdelta_nu,sampling,scene,effective_duration, verbose=True,return_mono=False, photon_noise=True):

    ###################
    ### Frequencies ###
    ###################

    #frequencies to reconstruct
    Nbfreq=int(floor(log(nu_max/nu_min)/log(1+delta_nu)))+1 ## number of edge frequencies
    nus_edge=nu_min*np.logspace(0,log(nu_max/nu_min)/log(1+delta_nu),Nbfreq,endpoint=True,base=delta_nu+1) #edge frequencies of reconstructed bands
    nus=np.array([(nus_edge[i]+nus_edge[i-1])/2 for i in range(1,Nbfreq)])
    deltas=(delta_nu)*(nus)
    deltas=np.array([(nus_edge[i]-nus_edge[i-1]) for i in range(1,Nbfreq)])
    Nbbands=len(nus)

    #frequencies assumed to have been used for construction of TOD
    subnu_min=nu_min
    subnu_max=nu_max
    Nbsubfreq=int(floor(log(subnu_max/subnu_min)/log(1+subdelta_nu)))+1
    sub_nus_edge=subnu_min*np.logspace(0,log(subnu_max/subnu_min)/log(1+subdelta_nu),Nbsubfreq,endpoint=True,base=subdelta_nu+1)
    sub_nus=np.array([(sub_nus_edge[i]+sub_nus_edge[i-1])/2 for i in range(1,Nbsubfreq)])
    sub_deltas=np.array([(sub_nus_edge[i]-sub_nus_edge[i-1]) for i in range(1,Nbsubfreq)])
    Nbsubbands=len(sub_nus)


    #Bands
    bands=[sub_nus[reduce(logical_and,(sub_nus<=nus_edge[i+1],sub_nus>=nus_edge[i]))] for i in range(Nbbands)]
    numbers=np.cumsum(np.array([len(bands[i]) for i in range(Nbbands)]))
    numbers=np.append(0,numbers)
    bands_numbers=np.array([(np.arange(numbers[i],numbers[i+1])) for i in range(Nbbands)])

    if verbose:
        print('Nombre de bandes utilisées pour la reconstruction : '+str(Nbsubbands))
        print('Nombre de bandes reconstruites : '+str(Nbbands))
        print('Résolution spectrale : '+str(delta_nu))
        print ('Bandes reconstruites : ' + str(bands))


    ################
    ### Coverage ###
    ################
    sub_instruments=[QubicInstrument(filter_nu=sub_nus[i]*10**9,filter_relative_bandwidth=sub_deltas[i]/sub_nus[i],detector_nep=2.7e-17) for i in range(Nbsubbands)]
    sub_acqs=[QubicAcquisition(sub_instruments[i], sampling, scene,photon_noise=photon_noise, effective_duration=effective_duration) for i in range(Nbsubbands)]
    covlim=0.1
    coverage = np.array([sub_acqs[i].get_coverage() for i in range(Nbsubbands)])
    observed = [(coverage[i] > covlim*np.max(coverage[i])) for i in range(Nbsubbands)]
    obs=reduce(logical_and,tuple(observed[i] for i in range(Nbsubbands)))
    pack = PackOperator(obs, broadcast='rightward')

    ######################
    ### Reconstruction ###
    ######################
    sub_acqs_restricted=[sub_acqs[i][:,:,obs] for i in range(Nbsubbands)]
    operators=np.array([sub_acqs_restricted[i].get_operator() for i in range(Nbsubbands)])
    K=np.array([np.sum([operators[j] for j in bands_numbers[i]],axis=0) for i in range(Nbbands)])
    H=BlockRowOperator([K[i] for i in range(Nbbands)], new_axisin=0)
    invntt = sub_acqs[0].get_invntt_operator()
    A=H.T*invntt*H
    b = (H.T * invntt)(Y)
    preconditioner = BlockDiagonalOperator([DiagonalOperator(1 / coverage[0][obs], broadcast='rightward') for i in range(Nbbands)], new_axisin=0) 
    solution_qubic = pcg(A, b, M=preconditioner ,disp=True, tol=1e-3, maxiter=1000)
    blockpack = BlockDiagonalOperator([pack for i in range(Nbbands)], new_axisin=0)
    maps =blockpack.T(solution_qubic['x'])
    if Nbbands==1:
        sh=np.shape(maps)
        maps=maps.reshape((1,sh[0],sh[1]))
    maps[:,~obs] = 0

    #################
    ### Input map ###
    #################

    # x0=np.zeros((Nbsubbands,Nbpixels,3))
    # for i in range(Nbsubbands):
    #     #x0[i,:,0]=cmb.T[0]+dust.T[0]*scaling_dust(150,sub_nus[i]e-9,1.59)
    #     x0[i,:,1]=cmb.T[1]+dust.T[1]*scaling_dust(150,sub_nus[i],1.59)
    #     x0[i,:,2]=cmb.T[2]+dust.T[2]*scaling_dust(150,sub_nus[i],1.59)

    # maps_mono=np.zeros((Nbbands,Nbpixels,3))
    # if return_mono:
    #     (m,n)=shape(Y)
    #     Y_mono=np.zeros((Nbbands,m,n))
    #     for i in range(Nbbands):
    #         for j in bands_numbers[i]:
    #             C=HealpixConvolutionGaussianOperator(fwhm=sub_instruments[j].synthbeam.peak150.fwhm * (150 / (sub_nus[j])))
    #             Y_mono[i]=Y_mono[i]+operators[j]*pack*C*x0[j]
    #         Global_instrument=QubicInstrument(filter_nu=nus[i],filter_relative_bandwidth=deltas[i]/nus[i],detector_nep=2.7e-17)
    #         Global_acquisition=QubicAcquisition(Global_instrument, sampling, scene,photon_noise=photon_noise, effective_duration=effective_duration)
    #         noise=Global_acquisition.get_noise()
    #         Y_mono[i]=Y_mono[i]+noise
    #         H_mono=np.sum([operators[j] for j in bands_numbers[i]],axis=0)
    #         A_mono=H_mono.T*invntt*H_mono
    #         b_mono = (H_mono.T * invntt)(Y_mono[i])
    #         preconditioner_mono = DiagonalOperator(1 / coverage[0][obs], broadcast='rightward') 
    #         solution_qubic_mono = pcg(A_mono, b_mono, M=preconditioner_mono ,disp=True, tol=1e-3, maxiter=1000)
    #         maps_mono[i] =pack.T(solution_qubic_mono['x'])  
    #         maps_mono[i,~obs] = 0
    #     return maps, maps_mono, bands, deltas

    return maps, bands, deltas
示例#38
0
cov = acq_qubic.get_coverage()
mask = cov < cov.max() * 0.2
acq_planck = PlanckAcquisition(150,
                               acq_qubic.scene,
                               true_sky=convolved_sky,
                               mask=mask)
acq_fusion = QubicPlanckAcquisition(acq_qubic, acq_planck)

H = acq_fusion.get_operator()
invntt = acq_fusion.get_invntt_operator()
y = acq_fusion.get_observation()

A = H.T * invntt * H
b = H.T * invntt * y

solution_fusion = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)

acq_qubic = QubicAcquisition(150, sampling, scene, effective_duration=1)
H = acq_qubic.get_operator()
invntt = acq_qubic.get_invntt_operator()
y, sky_convolved = acq_qubic.get_observation(sky, convolution=True)

A = H.T * invntt * H
b = H.T * invntt * y

solution_qubic = pcg(A, b, disp=True, maxiter=maxiter, tol=tol)


# some display
def display(input, msg, iplot=1):
    out = []