def linear_integral(g, nside, T): # Integral on a linear grid weights = linear_weights(g) sb = np.zeros(12*nside**2) bar = progress_bar(len(g)) for i, nu in enumerate(g): sb_ = read_map('sb_nside{}_nu{:.3e}.fits'.format(nside, nu)) sb += sb_*deriv(nu, nside, T)*weights[i] bar.update() write_map('sb_nside{}_poly{}_{}Ghz.fits'.format( nside, len(g), int(np.mean(grid)/1e9)), sb)
def non_linear_integral(g, n, nside, T): f_norm = quadratic_grid(g[0], g[len(g)-1], n) weights = non_linear_weights(f_norm) bar = progress_bar(n) sb = np.zeros(12*nside**2) for i, f in enumerate(f_norm): sb_ = interpolation(g, f, nside) sb += sb_*deriv(f, nside, T)*weights[i] bar.update() write_map('sb_nside{}_poly{}_{}Ghz.fits'.format( nside, int(n), int(np.mean(grid)/1e9)), sb)
def bin_camblib(Namaster, filename, nside, verbose=True, return_unbinned=False): """ Bin the spectra using Namaster. Parameters ---------- Namaster filename nside verbose return_unbinned Returns ------- """ lll, rvalues, spec, specunlensed = read_camblib(filename) ellb, b = Namaster.get_binning(nside) nbins = len(ellb) nr = len(rvalues) binned_spec = np.zeros((nbins, 4, nr)) binned_specunlensed = np.zeros((nbins, 4, nr)) fact = 2 * np.pi / (ellb * (ellb + 1)) if verbose: bar = progress_bar(nr, 'Binning CAMB Librairy') for ir in range(nr): for j in range(4): binned_spec[:, j, ir] = fact * b.bin_cell( np.reshape(spec[:Namaster.lmax + 1, j, ir], (1, Namaster.lmax + 1))) binned_specunlensed[:, j, ir] = fact * b.bin_cell( np.reshape(specunlensed[:Namaster.lmax + 1, j, ir], (1, Namaster.lmax + 1))) if verbose: bar.update() if return_unbinned: return [ ellb, rvalues, binned_spec, binned_specunlensed, [lll, rvalues, spec, specunlensed] ] else: return [ellb, rvalues, binned_spec, binned_specunlensed]
def map_corr_neighbtheta(themap_in, ipok_in, thetamin, thetamax, nbins, degrade=None, verbose=True): if degrade is None: themap = themap_in.copy() ipok = ipok_in.copy() else: themap = hp.ud_grade(themap_in, degrade) mapbool = themap_in < -1e30 mapbool[ipok_in] = True mapbool = hp.ud_grade(mapbool, degrade) ip = np.arange(12 * degrade ** 2) ipok = ip[mapbool] rthmin = np.radians(thetamin) rthmax = np.radians(thetamax) thvals = np.linspace(rthmin, rthmax, nbins + 1) ns = hp.npix2nside(len(themap)) thesum = np.zeros(nbins) thesum2 = np.zeros(nbins) thecount = np.zeros(nbins) if verbose: bar = progress_bar(len(ipok), 'Pixels') for i in range(len(ipok)): if verbose: bar.update() valthis = themap[ipok[i]] v = hp.pix2vec(ns, ipok[i]) # ipneighb_inner = [] ipneighb_inner = list(hp.query_disc(ns, v, np.radians(thetamin))) for k in range(nbins): thmin = thvals[k] thmax = thvals[k + 1] ipneighb_outer = list(hp.query_disc(ns, v, thmax)) ipneighb = ipneighb_outer.copy() for l in ipneighb_inner: ipneighb.remove(l) valneighb = themap[ipneighb] thesum[k] += np.sum(valthis * valneighb) thesum2[k] += np.sum((valthis * valneighb) ** 2) thecount[k] += len(valneighb) ipneighb_inner = ipneighb_outer.copy() mm = thesum / thecount mm2 = thesum2 / thecount errs = np.sqrt(mm2 - mm ** 2) / np.sqrt(np.sqrt(thecount)) corrfct = thesum / thecount mythetas = np.degrees(thvals[:-1] + thvals[1:]) / 2 return mythetas, corrfct, errs
def rcamblib(rvalues, lmax=3 * 256, save=None): """ Make CAMB library """ lll, totDL, unlensedDL = get_camb_Dl(lmax=lmax, r=0) spec = np.zeros((len(lll), 4, len(rvalues))) specunlensed = np.zeros((len(lll), 4, len(rvalues))) i = 0 bar = progress_bar(len(rvalues), 'CAMB Spectra') for r in rvalues: bar.update() ls, spec[:, :, i], specunlensed[:, :, i] = get_camb_Dl(lmax=lmax, r=r) i += 1 camblib = [lll, rvalues, spec, specunlensed] if save: with open(save, 'wb') as handle: pickle.dump(camblib, handle, protocol=pickle.HIGHEST_PROTOCOL) return camblib
def fold_data(time, dd, period, lowcut, highcut, nbins, notch=None): tfold = time % period FREQ_SAMPLING = 1./(time[1]-time[0]) sh = np.shape(dd) ndet = sh[0] folded = np.zeros((ndet,nbins)) folded_nonorm = np.zeros((ndet,nbins)) bar = progress_bar(ndet, 'Detectors ') for THEPIX in xrange(ndet): bar.update() data = dd[THEPIX,:] filt = scsig.butter(3, [lowcut/FREQ_SAMPLING, highcut/FREQ_SAMPLING], btype='bandpass', output='sos') newdata = scsig.sosfilt(filt, data) if notch is not None: for i in xrange(len(notch)): ftocut = notch[i][0] bw = notch[i][1] newdata = notch_filter(newdata, ftocut, bw, FREQ_SAMPLING) t, yy, dx, dy = profile(tfold,newdata,range=[0, period],nbins=nbins,dispersion=False, plot=False) folded[THEPIX,:] = (yy-np.mean(yy))/np.std(yy) folded_nonorm[THEPIX,:] = (yy-np.mean(yy)) return folded, t, folded_nonorm
plot([freq_mod, freq_mod], [1e4, 1e13], label='Modulation Frequency: {}Hz'.format(freq_mod)) yscale('log') xscale('log') xlim(0.01, 3.) ylim(1e4, 1e13) legend(loc='best') # =============== Loop on all TES an both ASICS ================ allres = np.zeros((256, 4)) allerr = np.zeros((256, 4)) allamp_peak = np.zeros(256) for asic in [1, 2]: a.read_qubicstudio_dataset(thedir, asic=asic) bar = progress_bar(128, 'ASIC #{}'.format(AsicNum)) for tes in np.arange(128) + 1: t_data, data, FREQ_SAMPLING, freq_f, spectrum_f, amp_peak, okfit, res = get_amp_first_harmonic( a, tes, freq_mod) bar.update() tes_index = (tes - 1) + 128 * (asic - 1) allres[tes_index, :] = res[1] allerr[tes_index, :] = res[2] allamp_peak[tes_index] = amp_peak amps = allres[:, 2] # amps = allamp_peak img_one = ft.image_asics(all1=amps) mm, ss = ft.meancut(amps, 3) imshow(img_one, vmin=0, vmax=mm + 3 * ss, interpolation='nearest') colorbar()
N = [512, 1024, 2048] T_cmb = 2.7255 # Kelvin nu_cent = 150e9 cut_on = 130e9 # Ideal Filter 150 GHz - 25% bandwidht cut_off = 170e9 res = 401 #nu_cent = 220e9 #cut_on = 190e9 # Ideal Filter 220 GHz - 25% bandwidht #cut_off = 250e9 #res = 601 grid = np.linspace(cut_on, cut_off, res) sample = np.unique(np.round(np.logspace(0, np.log10(res)))) for nside in N: bar = progress_bar(len(sample)) for n in sample: if n == 1: sb = read_map('sb_nside{}_nu{:.3e}.fits'.format(nside, nu_cent)) sb *= deriv(nu_cent, nside, T_cmb)*(cut_off - cut_on) write_map('sb_nside{}_mono_{}Ghz.fits'.format(nside, int(nu_cent/1e9)), sb) elif n == res: linear_integral(grid, nside, T_cmb) else: non_linear_integral(grid, n, nside, T_cmb) bar.update()
llgal = np.degrees(phgal) - 180 bbgal = 90 - np.degrees(thgal) ra, dec = gal2equ(llgal, bbgal) mp.clf() mp.subplot(211, projection='mollweide') mp.plot(np.radians(llgal),np.radians(bbgal), '.') mp.title('Galactic') mp.subplot(212, projection='mollweide') mp.plot(np.radians(ra-180), np.radians(dec), '.') mp.title('Equatorial') #### create pointings at the center of each healpix pixel npointings = 12 * nsmap**2 initpointing = create_random_pointings([ra[0], dec[0]], npointings, 0.001) bar = progress_bar(npointings) for i in np.arange(npointings): bla = create_random_pointings([ra[i], dec[i]], 2, 0.01) initpointing[i] = bla[0] bar.update() fracvals = [0.3, 0.5, 0.9, 0.999] mapmean_cmb = np.zeros((len(fracvals), npointings)) mapmean_dust = np.zeros((len(fracvals), npointings)) mapmean_all = np.zeros((len(fracvals), npointings)) maprms_cmb = np.zeros((len(fracvals), npointings)) maprms_dust = np.zeros((len(fracvals), npointings)) maprms_all = np.zeros((len(fracvals), npointings)) for j in np.arange(len(fracvals)): frac = fracvals[j] print(j, len(fracvals))
return np.sum((syb_ref - syb_ga)**2) nside = 1024 scene = SceneHealpixCMB(nside, kind='I') nu = 150e9 #fixed syb_f = 1 #fixed det = np.arange(0, 992) t_max = [30, 60] bounds = [0.37, 0.39] fwhm150 = np.zeros((len(t_max), 2)) bar = progress_bar(len(det)) for idet in det: for j, theta_max in enumerate(t_max): fwhm150_energy = sp.optimize.brentq( deltaEoverE, bounds[0], bounds[1], args=( scene, nu, idet, theta_max, syb_f)) fwhm150_residuals = sp.optimize.minimize_scalar( residuals, args=(scene, nu, idet, theta_max, syb_f), bounds=(bounds[0], bounds[1]), method='bounded') fwhm150[j] = [fwhm150_energy, fwhm150_residuals.x] np.save('fwhm150s/fwhm150_nside{}_det{}_sybf{}_thetamax{}'.format( nside, idet, syb_f, t_max), fwhm150) gc.collect() bar.update()
maskok = np.degrees(np.arccos(cosang)) < maxang maskmap = apodize_mask(maskok, 5) hp.gnomview(maskmap, rot=[racenter, deccenter], coord=['G', 'C'], reso=15) # Xpol estimation through MC np.random.seed(0) xpol = Xpol(maskmap, lmin, lmax, delta_ell) ell_binned = xpol.ell_binned spectra_binned = xpol.bin_spectra(spectra) nbins = len(ell_binned) nbmc = 100 allclsout = np.zeros((nbmc, 6, nbins)) allcls = np.zeros((nbmc, 6, lmax+1)) bar = progress_bar(nbmc) for i in np.arange(nbmc): maps = hp.synfast(spectra, nside, fwhm=0, pixwin=True, new=True, verbose=False) allcls[i], allclsout[i] = xpol.get_spectra(maps) bar.update() # get MC results mclsout = np.mean(allclsout, axis=0) sclsout = np.std(allclsout, axis=0) / np.sqrt(nbmc) mcls = np.mean(allcls, axis=0) scls = np.std(allcls, axis=0) / np.sqrt(nbmc) ell = np.arange(lmax+1) pw = hp.pixwin(nside, pol=True)
def fold_data(time, dd, period, lowcut, highcut, nbins, notch=None, rebin=None, verbose=None, return_error=False, silent=False, median=False, return_noise_harmonics=None): """ Parameters ---------- time : array dd : array Data signal. period : float Data will be folded on this period. lowcut : float Low cut for the band filter. highcut : float High cut for the band filter. nbins notch return_error """ tfold = time % period FREQ_SAMPLING = 1. / (time[1] - time[0]) sh = np.shape(dd) ndet = sh[0] if return_noise_harmonics is not None: #### We estimate the noise in between the harmonics of the signal between harm=1 and #### harm=return_noise_harmonics #### First we find the corresponding frequencies, below we measure the nosie nharm = return_noise_harmonics margin = 0.2 fmin = np.zeros(nharm) fmax = np.zeros(nharm) fnoise = np.zeros(nharm) noise = np.zeros((ndet, nharm)) for i in range(nharm): fmin[i] = 1. / period * (i + 1) * (1 + margin / (i + 1)) fmax[i] = 1. / period * (i + 2) * (1 - margin / (i + 1)) fnoise[i] = 0.5 * (fmin[i] + fmax[i]) folded = np.zeros((ndet, nbins)) folded_nonorm = np.zeros((ndet, nbins)) dfolded = np.zeros((ndet, nbins)) dfolded_nonorm = np.zeros((ndet, nbins)) if not silent: bar = progress_bar(ndet, 'Detectors ') for THEPIX in range(ndet): if not silent: bar.update() data = dd[THEPIX, :] newdata = filter_data(time, data, lowcut, highcut, notch=notch, rebin=rebin, verbose=verbose) t, yy, dx, dy, others = profile(tfold, newdata, range=[0, period], nbins=nbins, dispersion=False, plot=False, cutbad=False, median=median) folded[THEPIX, :] = (yy - np.mean(yy)) / np.std(yy) folded_nonorm[THEPIX, :] = (yy - np.mean(yy)) dfolded[THEPIX, :] = dy / np.std(yy) dfolded_nonorm[THEPIX, :] = dy if return_noise_harmonics is not None: spectrum, freq = power_spectrum(time, newdata, rebin=True) for i in range(nharm): ok = (freq >= fmin[i]) & (freq < fmax[i]) noise[THEPIX, i] = np.sqrt(np.mean(spectrum[ok])) if return_error: if return_noise_harmonics: return folded, t, folded_nonorm, dfolded, dfolded_nonorm, newdata, fnoise, noise else: return folded, t, folded_nonorm, dfolded, dfolded_nonorm, newdata else: if return_noise_harmonics: return folded, t, folded_nonorm, newdata, fnoise, noise else: return folded, t, folded_nonorm, newdata
def calibrate(fib, pow_maynooth, allparams, allerr, allok, cutparam=None, cuterr=None, bootstrap=None): """ Parameters ---------- fib pow_maynooth allparams allerr allok cutparam cuterr bootstrap Returns ------- """ img_maynooth = image_asics(all1=pow_maynooth) clf() subplot(2, 2, 1) plot(allparams[allok, 3], allerr[allok, 3], 'k.') if cuterr is not None: thecut_err = cuterr else: thecut_err = 1e10 if cutparam is not None: thecut_amp = cutparam else: thecut_amp = 1e10 newok = allok * (allerr[:, 3] < thecut_err) * (allparams[:, 3] < thecut_amp) plot([np.min(allparams[allok, 3]), np.max(allparams[allok, 3])], [thecut_err, thecut_err], 'g--') plot([thecut_amp, thecut_amp], [np.min(allerr[allok, 3]), np.max(allerr[allok, 3])], 'g--') plot(allparams[newok, 3], allerr[newok, 3], 'r.') allparams[~newok, :] = np.nan ylabel('$\sigma_{amp}$ [nA]') xlabel('Amp Fib{} [nA]'.format(fib)) subplot(2, 2, 3) errorbar(pow_maynooth[newok], allparams[newok, 3], yerr=allerr[newok, 3], fmt='r.') xx = pow_maynooth[newok] yy = allparams[newok, 3] yyerr = allerr[newok, 3] res = do_minuit(xx, yy, yyerr, np.array([1., 0]), fixpars=[0, 0]) paramfit = res[1] if bootstrap is None: errfit = res[2] typerr = 'Minuit' else: bsres = [] bar = progress_bar(bootstrap, 'Bootstrap') for i in range(bootstrap): bar.update() order = np.argsort(np.random.rand(len(xx))) xxbs = xx.copy() yybs = yy[order] yybserr = yyerr[order] theres = do_minuit(xxbs, yybs, yybserr, np.array([1., 0]), fixpars=[0, 0], verbose=False) bsres.append(theres[1]) bsres = np.array(bsres) errfit = np.std(bsres, axis=0) typerr = 'Bootstrap' xxx = np.linspace(0, np.max(pow_maynooth), 100) plot(xxx, thepolynomial(xxx, res[1]), 'g', lw=3, label='a={0:8.3f} +/- {1:8.3f} \n b={2:8.3f} +/- {3:8.3f}'.format( paramfit[0], errfit[0], paramfit[1], errfit[1])) if bootstrap is not None: bsdata = np.zeros((bootstrap, len(xxx))) for i in range(bootstrap): bsdata[i, :] = thepolynomial(xxx, bsres[i, :]) mm = np.mean(bsdata, axis=0) ss = np.std(bsdata, axis=0) fill_between(xxx, mm - ss, y2=mm + ss, color='b', alpha=0.3) fill_between(xxx, mm - 2 * ss, y2=mm + 2 * ss, color='b', alpha=0.2) fill_between(xxx, mm - 3 * ss, y2=mm + 3 * ss, color='b', alpha=0.1) plot(xxx, mm, 'b', label='Mean bootstrap') # indices = np.argsort(np.random.rand(bootstrap))[0:1000] # for i in range(len(indices)): # plot(xxx, thepolynomial(xxx, bsres[indices[i],:]), 'k', alpha=0.01) ylim(0, np.max(allparams[newok, 3]) * 1.1) xlim( np.min(pow_maynooth[newok]) * 0.99, np.max(pow_maynooth[newok]) * 1.01) ylabel('Amp Fib{} [nA]'.format(fib)) xlabel('Maynooth [mW]') legend(fontsize=8, framealpha=0.5) subplot(2, 2, 2) imshow(img_maynooth, vmin=np.min(pow_maynooth), vmax=np.max(pow_maynooth), interpolation='nearest') colorbar() title('Maynooth [mW]') subplot(2, 2, 4) img = image_asics(all1=allparams[:, 3] / res[1][0]) imshow(img, interpolation='nearest') colorbar() title('Amp Fib{} converted to mW'.format(fib)) tight_layout() return res[1], res[2], newok
def fit_all(t, folded, av, initpars=None, fixpars=[0, 0, 0, 0], stop_each=False, functname=simsig, rangepars=None): """ Parameters ---------- t folded av initpars fixpars stop_each functname rangepars Returns ------- """ npix, nbins = np.shape(folded) print(' Got {} pixels to fit'.format(npix)) ##### Now fit each TES fixing cycle to dc and t0 to the one fitted on the median allparams = np.zeros((npix, 4)) allerr = np.zeros((npix, 4)) allchi2 = np.zeros(npix) bar = progress_bar(npix, 'Detectors ') ok = np.zeros(npix, dtype=bool) for i in range(npix): bar.update() thedd = folded[i, :] #### First a fit with no error correction in order to have a chi2 distribution theres = do_minuit(t, thedd, np.ones(len(t)), initpars, functname=functname, fixpars=fixpars, rangepars=rangepars, force_chi2_ndf=True, verbose=False, nohesse=True) ndf = theres[5] params = theres[1] err = theres[2] allparams[i, :] = params allerr[i, :] = err allchi2[i] = theres[4] if stop_each: clf() plot(t, thedd, color='k') plot(t, av, color='b', lw=4, alpha=0.2, label='Median') plot( t, functname(t, theres[1]), 'r--', lw=4, label= 'Fitted: \n cycle={0:8.3f}+/-{1:8.3f} \n tau = {2:8.3f}+/-{3:8.3f}s \n t0 = {4:8.3f}+/-{5:8.3f}s \n amp = {6:8.3f}+/-{7:8.3f}' .format(params[0], err[0], params[1], err[1], params[2], err[2], params[3], err[3])) legend() show() msg = 'TES #{}'.format(i) if i in [3, 35, 67, 99]: msg = 'Channel #{} - BEWARE THIS IS A THERMOMETER !'.format(i) title(msg) # Changing so 'i' select prompts plot inversion bla = raw_input( "Press [y] if fit OK, [i] to invert, other key otherwise...") if bla == 'y': ok[i] = True # invert to check if TES okay, # thedd refers to the indexed TES in loop if bla == 'i': # clf() plot(t, thedd * (-1.0), color='olive') show() ibla = raw_input( "Press [y] if INVERTED fit OK, otherwise anykey") # and invert thedd in the original datset if ibla == 'y': ok[i] = True folded[i, :] = thedd * -1.0 print(ok[i]) return allparams, allerr, allchi2, ndf, ok
cosang = np.dot(veccenter, vecpix) maskok = np.degrees(np.arccos(cosang)) < maxang maskmap = apodize_mask(maskok, 5) hp.gnomview(maskmap, rot=[racenter, deccenter], coord=['G', 'C'], reso=15) # Xpol estimation through MC np.random.seed(0) xpol = Xpol(maskmap, lmin, lmax, delta_ell) ell_binned = xpol.ell_binned spectra_binned = xpol.bin_spectra(spectra) nbins = len(ell_binned) nbmc = 100 allclsout = np.zeros((nbmc, 6, nbins)) allcls = np.zeros((nbmc, 6, lmax + 1)) bar = progress_bar(nbmc) for i in np.arange(nbmc): maps = hp.synfast(spectra, nside, fwhm=0, pixwin=True, new=True, verbose=False) allcls[i], allclsout[i] = xpol.get_spectra(maps) bar.update() # get MC results mclsout = np.mean(allclsout, axis=0) sclsout = np.std(allclsout, axis=0) / np.sqrt(nbmc) mcls = np.mean(allcls, axis=0) scls = np.std(allcls, axis=0) / np.sqrt(nbmc)
plot(allmaps[:,0,0]-meanmaps[0,0], alpha=0.2, color='red') plot(allmaps[:,0,0]*0, color='red') plot(allmaps[:,0,100]-meanmaps[0,100], alpha=0.2, color='blue') plot(allmaps[:,0,100]*0, color='blue') plot(allmaps[:,0,1000]-meanmaps[0,1000], alpha=0.2, color='green') plot(allmaps[:,0,1000]*0, color='green') allmaps_zero_t = (allmaps-meanmaps).T npx=npix cm = np.zeros((3*npx,3*npx)) for iqup in [0,1,2]: for iquq in [0,1,2]: print(iqup, iquq) bar = progress_bar(npx) for p in xrange(npx): bar.update(p) for q in xrange(npx): cm[iqup*npx+p, iquq*npx+q] = np.dot( allmaps_zero_t[p, iqup, :], allmaps_zero_t[q, iquq, :])/len(allfiles) npx=npix cm = np.zeros((3*npx,3*npx)) for iqup in xrange(2): for iquq in xrange(iqup,2): print(iqup, iquq) bar = progress_bar(npx) for p in xrange(npx): bar.update(p) for q in xrange(p,npx):