def calculate_rho(data_fr, coadd_fr, freq_factor, return_err=False, use_t=False): ''' Takes (un)weighted T/Q/U maps and an unweighted T/Q/U coadd, as well as the weights from the map you want rho for, and calculates the polarization rotation angle rho. ''' maps.RemoveWeights(data_fr) maps.RemoveWeights(coadd_fr) q = data_fr['Q'] u = data_fr['U'] qc = coadd_fr['Q'] uc = coadd_fr['U'] weights = data_fr['Wpol'] qweight = weights.QQ uweight = weights.UU quweight = weights.QU if use_t: t = data_fr['T'] tc = coadd_fr['T'] tweight = weights.TT tqweight = weights.TQ tuweight = weights.TU num = np.asarray(tuweight*qc*(t-tc) - tqweight*uc*(t-tc) - qweight*uc*(q-qc) + \ uweight*qc*(u-uc) + quweight*(qc*q - qc**2 + uc**2 - uc*u)) else: num = np.asarray(qweight*uc*(qc-q) + uweight*qc*(u-uc) + \ quweight*(qc*q - qc**2 + uc**2 - uc*u)) den = np.asarray(qweight * uc**2 + uweight * qc**2 - 2 * quweight * qc * uc) #num[num == np.inf] = 0 #den[den == np.inf] = 0 print(np.nansum(den)) rho = np.nansum(num) / np.nansum(den) if return_err: try: err = get_errs(rho, data_fr, coadd_fr, freq_factor, use_t=use_t) except: err = np.nan return rho, err else: return rho
def calculate_chi2(data_fr, coadd_fr, freq_factor, rho, use_t=False, return_map=False): ''' Takes (un)weighted T/Q/U maps and an (un)weighted T/Q/U coadd, and calculates the chi^2 for a given input rotation angle rho. ''' maps.RemoveWeights(data_fr) maps.RemoveWeights(coadd_fr) q = data_fr['Q'] u = data_fr['U'] qc = coadd_fr['Q'] uc = coadd_fr['U'] weights = data_fr['Wpol'] qweight = weights.QQ uweight = weights.UU quweight = weights.QU chiqq = qweight * (q - qc + rho * uc)**2 chiuu = uweight * (u - uc - rho * qc)**2 chiqu = 2 * quweight * (q - qc + rho * uc) * (u - uc - rho * qc) if use_t: t = data_fr['T'] tc = coadd_fr['T'] tweight = weights.TT tqweight = weights.TQ tuweight = weights.TU chitt = tweight * (t - tc)**2 chitq = 2 * tqweight * ((t - tc) * (q - qc) + rho * uc * (t - tc)) chitu = 2 * tuweight * ((t - tc) * (u - uc) - rho * qc * (t - tc)) chi2_map = (chiqq + chiuu + chiqu + chitt + chitq + chitu) / (freq_factor**2) else: chi2_map = (chiqq + chiuu + chiqu) / (freq_factor**2) chi2 = np.nansum(chi2_map) if return_map: return chi2, chi2_map else: return chi2
def inject_signal_in_map(amp, data_fr, coadd_fr): ''' Inject a map-space signal, rotating Q into U and vice-versa. amp must be in radians. ''' maps.RemoveWeights(data_fr) maps.RemoveWeights(coadd_fr) q = data_fr.pop('Q') u = data_fr.pop('U') qc = coadd_fr['Q'] uc = coadd_fr['U'] data_fr['Q'] = q - amp * uc data_fr['U'] = u + amp * qc return data_fr
def get_errs(min_rho, data_fr, coadd_fr, freq_factor, use_t=False): maps.RemoveWeights(data_fr) maps.RemoveWeights(coadd_fr) chi2s = [] rhos = np.linspace(min_rho - 5. * (np.pi / 180), min_rho + 5. * (np.pi / 180), 11) for rho in rhos: chi2s.append( calculate_chi2(data_fr, coadd_fr, freq_factor, rho, use_t=use_t)) p = np.polyfit(rhos, np.array(chi2s), deg=2) p[2] = p[2] - chi2s[5] - 1 err_1, err_2 = np.roots(p) err_down = np.min([err_1, err_2]) err_up = np.max([err_1, err_2]) return np.mean((min_rho - err_down, err_up - min_rho))
def convert_to_fits_raw(g3file, fitsfile=None, outpath='', hdr=None, overwrite=True, compress=False, logger=None): t0 = time.time() if not logger: logger = LOGGER # Extract the relevante metadata if not hdr: hdr = get_metadata(g3file, logger=logger) # Define fitsfile name only if undefined if fitsfile is None: basename = get_g3basename(g3file) fitsfile = os.path.join(outpath, get_folder_date(hdr), f"{basename}.fits") # Skip if fitsfile exists and overwrite not True if os.path.isfile(fitsfile) and not overwrite: logger.warning(f"File exists, skipping: {fitsfile}") return # Populate additional metadata for DB hdr['FITSNAME'] = (os.path.basename(fitsfile), 'Name of fits file') # Second loop to write FITS g3 = core.G3File(g3file) logger.info(f"Loading 2: {g3file}") for frame in g3: # Convert to FITS if frame.type == core.G3FrameType.Map: logger.info( f"Transforming to FITS: {frame.type} -- Id: {frame['Id']}") maps.RemoveWeights(frame, zero_nans=True) # Make sure that the folder exists: dirname = os.path.dirname(fitsfile) if not os.path.isdir(dirname): logger.info(f"Creating directory {os.path.dirname(fitsfile)}") os.mkdir(dirname) maps.fitsio.save_skymap_fits(fitsfile, frame['T'], overwrite=overwrite, compress=compress, W=frame['Wunpol'], hdr=hdr) logger.info(f"Created: {fitsfile}") logger.info(f"FITS creation time: {elapsed_time(t0)}") return
def plot_map(frame): if frame.type == core.G3FrameType.Map and 'Id' in frame and \ fnmatch(frame['Id'], 'combined_*'): maps.RemoveWeights(frame) for pol in ['T', 'Q', 'U']: f = plt.figure(figsize=(12, 8)) if pol == 'T': vmag = 50 else: vmag = 10 plt.imshow(frame[pol] / (core.G3Units.microkelvin), vmin=-1 * vmag, vmax=vmag) plt.colorbar() plt.title(pol) plt.tight_layout() plt.savefig('{}_map_{}_{}.png'.format( os.path.basename(args.output).split('.')[0], jmap, pol), dpi=200) plt.close(f)
from spt3g import core, maps from scipy.stats import norm import os.path coadd_dir = '/sptlocal/user/kferguson/full_daniel_maps/' with open('weights_var_factors_backup.pkl', 'rb') as f: data_kstest = pickle.load(f) for field in data_kstest['ks_result']: for obsid in data_kstest['ks_result'][field]: for band in data_kstest['ks_result'][field][obsid]: if data_kstest['ks_result'][field][obsid][band].pvalue < 0.01: map_fname = '{}_{}GHz_{}_map.g3.gz'.format(field, band, obsid) map_frame = list(core.G3File(os.path.join(coadd_dir, map_fname)))[0] maps.RemoveWeights(map_frame) q_arr = np.array(map_frame['Q']) q_weight = np.array(map_frame['Wpol'].QQ) q_arr_finite = q_arr[np.isfinite(q_arr)] q_weight_finite = q_weight[np.isfinite(q_arr)] factor = q_arr_finite * np.sqrt(q_weight_finite) plt.figure(1) plt.hist(factor / np.std(factor), bins=np.linspace(-4, 4, 101), density=True) plt.plot(np.linspace(-4, 4, 101), norm.pdf(np.linspace(-4, 4, 101))) plt.xlim([-10,10]) plt.xlabel('$[Q_i \\times \sqrt{W_{qqi}}] / $std') plt.ylabel('pixels')
field = result.group(1) obsid = int(result.group(2)) if field not in factor: factor[field] = {} if field not in ks_test: ks_test[field] = {} if obsid not in factor[field]: factor[field][obsid] = {} if obsid not in ks_test[field]: ks_test[field][obsid] = {} if args.group == '2018ee': real_data = list(core.G3File(fname))[0] maps.RemoveWeights(real_data) q_arr = np.array(real_data['Q']) q_weight = np.array(real_data['Wpol'].QQ) q_arr_finite = q_arr[np.isfinite(q_arr)] q_weight_finite = q_weight[np.isfinite(q_arr)] factor[field][obsid][band] = np.std(q_arr_finite * np.sqrt(q_weight_finite)) ks_result = kstest(q_arr_finite * np.sqrt(q_weight_finite) / factor[field][obsid][band], cdf='norm') ks_test[field][obsid][band] = ks_result elif args.group == '2019':