def make_corr(filename, verbose=False, identifier=None, cross_power=False, multiplier=1.0): """wrap the plot correlation class which reads correlation object shelve files; uses new binning methods in freq-slices. Note that correlations are converted into units of mK. """ output = {} corr_shelve = shelve.open(filename + ".shelve") if multiplier != 1.0: print "WARNING: using a multiplier of: " + repr(multiplier) corr = corr_shelve["corr"] * multiplier run_params = corr_shelve["params"] try: corr_counts = corr_shelve["counts"] except KeyError: print "WARNING: unable to find counts weighting for correlation" corr_counts = None if identifier: print "binning the correlation function in: " + filename + ".shelve" + " with id=" + identifier else: print "binning the correlation function in: " + filename + ".shelve" if verbose: for key in run_params: print key + ": " + repr(run_params[key]) # np.set_printoptions(threshold=np.nan) # print corr_shelve["corr"] corr[np.isnan(corr)] = 0.0 corr[np.isinf(corr)] = 0.0 lags = sp.array(run_params["lags"]) real_lags = lags.copy() real_lags[0] = 0 real_lags[1:] -= sp.diff(lags) / 2.0 frange = run_params["freq"] realrange = corr_shelve["freq_axis"] corr_2D = fs.rebin_corr_freq_lag(corr, realrange[list(frange)], weights=corr_counts, return_fbins=True, nfbins=200) corr_1D = fs.collapse_correlation_1D(corr_2D[0], corr_2D[2], real_lags, weights=corr_2D[1]) if cross_power: correlation_1D = corr_1D[0] * 1.0e3 correlation_2D = corr_2D[0] * 1.0e3 else: print "Taking the signed square root" correlation_1D = sp.sign(corr_1D[0]) * sp.sqrt(abs(corr_1D[0])) * 1e3 correlation_2D = sp.sign(corr_2D[0]) * sp.sqrt(abs(corr_2D[0])) * 1e3 output["run_params"] = run_params output["lags"] = run_params["lags"] output["real_lags"] = real_lags # uncomment these only if you need them in the shelve file; makes it huge # output["corr"] = corr # output["corr_counts"] = corr_counts output["freq"] = run_params["freq"] output["freq_axis"] = corr_shelve["freq_axis"] output["corr1D"] = correlation_1D output["corr1D_weights"] = corr_1D[1] output["corr1D_lags"] = corr_1D[2] output["corr2D"] = correlation_2D output["corr2D_weights"] = corr_2D[1] output["corr2D_fbins"] = corr_2D[2] if identifier: output["identifier"] = identifier return output
def save_1D_corr(mode_number, high, save): '''very hardcoded''' # Load pickle file. # file_name = "/mnt/raid-project/gmrt/eswitzer/wiggleZ/modetest/73_ABCD_all_%d_modes_real3map/New_Slices_object.pkl" % (mode_number) file_name = "/mnt/raid-project/gmrt/calinliv/wiggleZ/corr/doctest/New_Slices_object.pkl" f = open(file_name, "r") F = cPickle.load(f) f.close() # Setting axis info after pickling. map_file = F.params["input_root"] + "sec_A_15hr_41-73_clean_map_I.npy" exMap = algebra.make_vect(algebra.load(map_file)) for Pair in F.Pairs: Pair.Map1.info = exMap.info Pair.Map2.info = exMap.info Pair.Noise_inv1.info = exMap.info Pair.Noise_inv2.info = exMap.info ########## Getting 1D thing. ##################################### out_list = [] d1_list = [] for i in range(0, 6): # The corr to use. corr = F.Pairs[i].corr # The lags used lags = sp.array(F.params['lags']) real_lags = copy.deepcopy(lags) real_lags[0] = 0 real_lags[1:] -= sp.diff(lags) / 2.0 # The range selected in ini file. frange = F.params['freq'] # The corresponding real frequencies for that range. realrange = [F.Pairs[i].Map1.get_axis('freq')[f] for f in frange] # The 2D correlation. out = fs.rebin_corr_freq_lag(corr, realrange, nfbins=200, weights=F.Pairs[i].counts, return_fbins=True) out_list.append(out[0]) #plt.figure() #plt.imshow(out[0]) #plt.colorbar() # the 1D correlation. d1 = fs.collapse_correlation_1D(out[0], out[2], real_lags, out[1]) d1_list.append(copy.deepcopy(d1[0])) # plt.figure() # plt.plot(d1[2], d1[0],'.') x_axis = d1[2][1] matrixx = [] for d in d1_list: matrixx.append(d.tolist()) matrixx = sp.array(matrixx) print matrixx vals = [] std = [] for i in range(0, matrixx.shape[1]): # Get the sqrt to get mK. vals.append( sp.mean(sp.sign(matrixx[:, i]) * sp.sqrt(abs(matrixx[:, i])))) std.append(sp.std( sp.sign(matrixx[:, i]) * sp.sqrt(abs(matrixx[:, i])))) vals = sp.array(vals) std = sp.array(std) print print for i in range(0, matrixx.shape[0]): print sp.sign(matrixx[i, :]) * sp.sqrt(abs(matrixx[i, :])) # Set plot up for log log axes. plt.figure() ax = plt.gca() ax.set_yscale("log") ax.set_xscale("log") # Plot positives. t_inds = vals >= 0 n_inds = vals < 0 plt.plot(x_axis[t_inds], vals[t_inds] * 1000, 'b.') # take out *1000 for simmaps. plt.plot(x_axis[t_inds], (vals[t_inds] + std[t_inds]) * 1000, 'g_') plt.plot(x_axis[t_inds], (vals[t_inds] - std[t_inds]) * 1000, 'g_') # Plot negatives. neg_vals = -1 * vals plt.plot(x_axis[n_inds], neg_vals[n_inds] * 1000, 'r.') # take out *1000 for simmaps. plt.plot(x_axis[n_inds], (neg_vals[n_inds] + std[n_inds]) * 1000, 'g_') plt.plot(x_axis[n_inds], (neg_vals[n_inds] - std[n_inds]) * 1000, 'g_') #plt.axis([1, 100, 0.01, 500.0]) plt.xlabel('lag (Mpc/h)') plt.ylabel('correlation (mK)') # Plot the model(?). t_lags = sp.arange(0.1, 100, 0.1) r0 = 5.5 rb = 7.0 t = (sp.sqrt(((rb + t_lags) / r0)**(-1.8))) t = t * 0.15 / t[0] f = plt.plot(t_lags, t, marker='None', color='k', linestyle='-') if high: plt.axis([0.9, 100, 0.0001, 1.0]) else: plt.axis([0.9, 100, 0.001, 1.0]) if save: name = "/cita/h/home-2/calinliv/Desktop/figures/check/1D_corr_map_%d_doctest.png" % ( mode_number) #f = open(name, "w") plt.savefig(name)
def make_autocorr(filename, identifier=None, thousand_multiplier=True, multiplier=1.0): """Same as above but for autocorrs in NewSlices pickle objects. filename is the full path to the file and should inlude the .pkl ending. wrap the plot correlation class which reads correlation object shelve files; uses new binning methods in freq-slices. Note that correlations are converted into units of mK. """ output = {} # Load the New_Slices_object.pkl f = open(filename, "r") print filename F = cPickle.load(f) f.close() # Setting axis info after pickling. Make sure to use a map with the proper # info set. map_file = "/mnt/raid-project/gmrt/calinliv/wiggleZ/maps/" + "sec_A_15hr_41-73_clean_map_I.npy" exMap = algebra.make_vect(algebra.load(map_file)) for Pair in F.Pairs: Pair.Map1.info = exMap.info Pair.Map2.info = exMap.info Pair.Noise_inv1.info = exMap.info Pair.Noise_inv2.info = exMap.info # 3D->2D->1D corr_2D_list = [] corr_1D_list = [] for i in range(0, len(F.Pairs)): # The corr to use. corr = F.Pairs[i].corr if multiplier != 1.0: print "WARNING: using a multiplier of: " + repr(multiplier) corr *= multiplier # The lags used lags = sp.array(F.params["lags"]) real_lags = copy.deepcopy(lags) real_lags[0] = 0 real_lags[1:] -= sp.diff(lags) / 2.0 # The range selected in ini file. frange = F.params["freq"] # The corresponding real frequencies for that range. realrange = [F.Pairs[i].Map1.get_axis("freq")[f] for f in frange] # The 2D correlation. corr_2D = fs.rebin_corr_freq_lag(corr, realrange, nfbins=200, weights=F.Pairs[i].counts, return_fbins=True) corr_2D_list.append(corr_2D[0]) # The 1D correlation. corr_1D = fs.collapse_correlation_1D(corr_2D[0], corr_2D[2], real_lags, corr_2D[1]) corr_1D_list.append(copy.deepcopy(corr_1D[0])) # The values for x_left, x_centre, x_right. x_axis = corr_1D[2] # Put the 1D correlations into a matrix to be averaged easily. matrix_1D = [] for corr_1D in corr_1D_list: matrix_1D.append(corr_1D.tolist()) matrix_1D = sp.array(matrix_1D) # Get the average 1D corr and it's sample variance. vals = [] std = [] for i in range(0, matrix_1D.shape[1]): # Get the sqrt to get mK. vals.append(sp.mean(sp.sign(matrix_1D[:, i]) * sp.sqrt(abs(matrix_1D[:, i])))) std.append(sp.std(sp.sign(matrix_1D[:, i]) * sp.sqrt(abs(matrix_1D[:, i])))) vals = sp.array(vals) std = sp.array(std) # Go from K to mK if True. If data is already in mK, then make it False. if thousand_multiplier: vals *= 1000.0 std *= 1000.0 # Build output dictionary. output["run_params"] = F.params output["lags"] = F.params["lags"] output["real_lags"] = real_lags # uncomment these only if you need them in the shelve file; makes it huge # output["corr"] = corr # Not supported for 6 pairs # output["corr_counts"] = corr_counts # not supported for 6 pairs. output["freq"] = F.params["freq"] output["freq_axis"] = F.Pairs[0].Map1.get_axis("freq") output["corr1D"] = vals output["corr1D_std"] = std # output["corr1D_weights"] = corr_1D[1] # output["corr1D_lags"] = corr_1D[2] #This is now the x-axis output["x_axis"] = x_axis # output["corr2D"] = correlation_2D # There are 6 of these now so it's weird. # output["corr2D_weights"] = corr_2D[1] # Same as above. output["corr2D_fbins"] = corr_2D[2] # Ok. Bins are the same for each pair. if identifier: return (identifier, output) return output
def save_1D_corr(mode_number, high, save): '''very hardcoded''' # Load pickle file. # file_name = "/mnt/raid-project/gmrt/eswitzer/wiggleZ/modetest/73_ABCD_all_%d_modes_real3map/New_Slices_object.pkl" % (mode_number) file_name = "/mnt/raid-project/gmrt/calinliv/wiggleZ/corr/doctest/New_Slices_object.pkl" f = open(file_name, "r") F = cPickle.load(f) f.close() # Setting axis info after pickling. map_file = F.params["input_root"] + "sec_A_15hr_41-73_clean_map_I.npy" exMap = algebra.make_vect(algebra.load(map_file)) for Pair in F.Pairs: Pair.Map1.info = exMap.info Pair.Map2.info = exMap.info Pair.Noise_inv1.info = exMap.info Pair.Noise_inv2.info = exMap.info ########## Getting 1D thing. ##################################### out_list=[] d1_list=[] for i in range(0,6): # The corr to use. corr = F.Pairs[i].corr # The lags used lags = sp.array(F.params['lags']) real_lags = copy.deepcopy(lags) real_lags[0] = 0 real_lags[1:] -= sp.diff(lags)/2.0 # The range selected in ini file. frange = F.params['freq'] # The corresponding real frequencies for that range. realrange = [F.Pairs[i].Map1.get_axis('freq')[f] for f in frange] # The 2D correlation. out = fs.rebin_corr_freq_lag(corr, realrange, nfbins=200, weights=F.Pairs[i].counts, return_fbins=True) out_list.append(out[0]) #plt.figure() #plt.imshow(out[0]) #plt.colorbar() # the 1D correlation. d1 = fs.collapse_correlation_1D(out[0], out[2], real_lags, out[1]) d1_list.append(copy.deepcopy(d1[0])) # plt.figure() # plt.plot(d1[2], d1[0],'.') x_axis = d1[2][1] matrixx=[] for d in d1_list: matrixx.append(d.tolist()) matrixx = sp.array(matrixx) print matrixx vals=[] std=[] for i in range(0, matrixx.shape[1]): # Get the sqrt to get mK. vals.append(sp.mean(sp.sign(matrixx[:,i])*sp.sqrt(abs(matrixx[:,i])))) std.append(sp.std(sp.sign(matrixx[:,i])*sp.sqrt(abs(matrixx[:,i])))) vals = sp.array(vals) std = sp.array(std) print print for i in range(0, matrixx.shape[0]): print sp.sign(matrixx[i,:])*sp.sqrt(abs(matrixx[i,:])) # Set plot up for log log axes. plt.figure() ax = plt.gca() ax.set_yscale("log") ax.set_xscale("log") # Plot positives. t_inds = vals >= 0 n_inds = vals < 0 plt.plot(x_axis[t_inds],vals[t_inds]*1000,'b.') # take out *1000 for simmaps. plt.plot(x_axis[t_inds],(vals[t_inds]+std[t_inds])*1000,'g_') plt.plot(x_axis[t_inds],(vals[t_inds]-std[t_inds])*1000,'g_') # Plot negatives. neg_vals = -1*vals plt.plot(x_axis[n_inds],neg_vals[n_inds]*1000,'r.') # take out *1000 for simmaps. plt.plot(x_axis[n_inds],(neg_vals[n_inds]+std[n_inds])*1000,'g_') plt.plot(x_axis[n_inds],(neg_vals[n_inds]-std[n_inds])*1000,'g_') #plt.axis([1, 100, 0.01, 500.0]) plt.xlabel('lag (Mpc/h)') plt.ylabel('correlation (mK)') # Plot the model(?). t_lags = sp.arange(0.1, 100, 0.1) r0 = 5.5 rb = 7.0 t = (sp.sqrt(((rb + t_lags) / r0)**(-1.8))) t = t * 0.15 / t[0] f = plt.plot(t_lags, t, marker='None', color='k', linestyle='-') if high: plt.axis([0.9, 100, 0.0001, 1.0]) else: plt.axis([0.9, 100, 0.001, 1.0]) if save: name = "/cita/h/home-2/calinliv/Desktop/figures/check/1D_corr_map_%d_doctest.png" % (mode_number) #f = open(name, "w") plt.savefig(name)