def custom_data_loader(path): dataset = data_loader(path, floor=False) percentile = np.ones_like(dataset["ra"]) * np.nan dataset = append_fields( dataset, "percentile", percentile, usemask=False, dtypes=[np.float] ) return dataset
weighted_quantiles = np.cumsum(sample_weight) - 0.5 * sample_weight weighted_quantiles /= np.sum(sample_weight) return np.interp(quantiles, weighted_quantiles, values) sin_dec_bins = IC86_1_dict["sinDec bins"] n_log_e_bins = 20 + 1 log_e_bins = np.linspace(2.0, 6.0, n_log_e_bins) quantiles = np.linspace(0.0, 1.0, n_log_e_bins) # mc_path = "/lustre/fs22/group/icecube/data_mirror/misc # /CombinedTracks_AllSky_Uncorrected_MC.npy" mc_path = IC86_234567_dict["mc_path"] mc = data_loader(mc_path, floor=False) exp = data_loader(IC86_234567_dict["exp_path"], floor=False) # sin_dec_bins = weighted_quantile( # mc["sinDec"], quantiles, np.ones_like(mc["sinDec"])) # for gamma in [1.0, 1.5, 2.0, 3.0, 3.7]: for gamma in [2.0, 3.5]: # X = [sin_dec_bins for _ in range(n_log_e_bins)] # Y = [] Z_quantile = np.ones((len(sin_dec_bins), n_log_e_bins)) Z_uniform = np.ones((len(sin_dec_bins), n_log_e_bins)) Z_floor = np.ones((len(sin_dec_bins), n_log_e_bins)) * np.nan Z_floor2 = np.ones((len(sin_dec_bins), n_log_e_bins)) * np.nan x = [] y = []
def plot_ratio(seasons): for season_dict in seasons: exp = data_loader(season_dict["exp_path"]) mc = data_loader(season_dict["mc_path"]) cut = 4.5 exp_cut = exp[exp["logE"] > cut] mc_cut = mc[mc["logE"] > cut] print(len(exp), len(exp_cut)) print(len(mc), len(mc_cut)) print("Min energy", np.exp(min(exp["logE"]))) print(exp.dtype.names) data = [ ("exp", exp["sinDec"], azimuth_proxy(exp, season_dict)), ("mc", np.sin(mc["dec"]), azimuth_proxy(mc, season_dict)), (("exp_cut"), exp_cut["sinDec"], azimuth_proxy(exp_cut, season_dict)), (("mc cut"), np.sin(mc_cut["dec"]), azimuth_proxy(mc_cut, season_dict)), ] root = plots_dir + "azimuth/" + season_dict["Data Sample"] + "/" for (name, sin_dec, az) in data: save_dir = root + name + "/" try: os.makedirs(save_dir) except OSError: pass if name in ["exp", "exp_cut"]: hist_2d = create_2d_hist(sin_dec, az, sin_dec_bins, weights=np.ones_like(sin_dec)).T else: ow = mc["ow"] trueE = mc["trueE"] weights = numexpr.evaluate("ow * trueE **(-2)") mem_use = str( float(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) / 1.0e6) print("") print("Memory usage max: %s (Gb)" % mem_use) if name == "mc": hist_2d = create_2d_hist( sin_dec, az, sin_dec_bins, weights=energy_pdf.weight_mc(mc, gamma=3.7), ).T else: hist_2d = create_2d_hist( sin_dec, az, sin_dec_bins, weights=energy_pdf.weight_mc(mc_cut, gamma=3.7), ).T hist_2d = np.array([x / np.mean(x) for x in hist_2d]) plt.figure() ax = plt.subplot(111) sin_bin_center = (sin_dec_bins[:-1] + sin_dec_bins[1:]) / 2.0 az_bin_center = (azimuth_bins[:-1] + azimuth_bins[1:]) / 2.0 X, Y = np.meshgrid(az_bin_center, sin_bin_center) cbar = ax.pcolormesh(X, Y, hist_2d, vmin=0.5, vmax=1.5, cmap=cm.get_cmap("seismic")) # ax.set_aspect('equal') plt.axis([ min(azimuth_bins), max(azimuth_bins), min(sin_dec_bins), max(sin_dec_bins), ]) ax.set_ylabel("Sin(Declination)") ax.set_xlabel("Azimuth") plt.colorbar(cbar) savename = save_dir + season_dict["Name"] + ".pdf" plt.savefig(savename) plt.close() spline = scipy.interpolate.RectBivariateSpline(az_bin_center, sin_bin_center, hist_2d.T, kx=3, ky=1, s=0) plt.figure() ax = plt.subplot(111) sin_bin_center = (sin_dec_bins[:-1] + sin_dec_bins[1:]) / 2.0 az_bin_center = (azimuth_bins[:-1] + azimuth_bins[1:]) / 2.0 z = [] for sin_dec in sin_dec_bins: z.append(list(spline(azimuth_bins, sin_dec).T.tolist()[0])) z = np.array(z).T X, Y = np.meshgrid(azimuth_bins, sin_dec_bins) cbar = ax.pcolormesh(X, Y, z.T, vmin=0.5, vmax=1.5, cmap=cm.get_cmap("seismic")) # ax.set_aspect('equal') plt.axis([ min(azimuth_bins), max(azimuth_bins), min(sin_dec_bins), max(sin_dec_bins), ]) ax.set_ylabel("Sin(Declination)") ax.set_xlabel("Azimuth") plt.colorbar(cbar) savename = save_dir + season_dict["Name"] + "_spline.pdf" plt.savefig(savename) plt.close() upgoing = [ azimuth_proxy(exp[exp["sinDec"] > 0.0], season_dict), azimuth_proxy(mc[mc["dec"] > 0.0], season_dict), ] downgoing = [ azimuth_proxy(exp[exp["sinDec"] < 0.0], season_dict), azimuth_proxy(mc[mc["dec"] < 0.0], season_dict), ] upgoing_cut = [ azimuth_proxy(exp_cut[exp_cut["sinDec"] > 0.0], season_dict), azimuth_proxy(mc_cut[mc_cut["dec"] > 0.0], season_dict), ] # downgoing_cut = [azimuth_proxy(exp_cut[exp_cut["sinDec"] < 0.], # season_dict), # azimuth_proxy(mc_cut[mc_cut["dec"] < 0.], season_dict)] for i, dataset in enumerate([upgoing, downgoing, upgoing_cut]): plt.figure() ax1 = plt.subplot2grid((4, 1), (0, 0), colspan=3, rowspan=3) weights = [np.ones_like(x) / float(len(x)) for x in dataset] n, edges, patches = plt.hist( dataset, bins=azimuth_bins, histtype="step", weights=weights, label=["Data", "MC"], color=["red", "b"], ) med_bkg = np.median(n[0]) mask = n[0] > med_bkg over = n[0][mask] sum_over = np.sum(over - med_bkg) title = (season_dict["Name"] + " " + [ "upgoing", "downgoing", "upgoing [Log(E) > " + str(cut) + "]", "downgoing [Log(E) > " + str(cut) + "]", ][i]) plt.axhline( med_bkg, linestyle="--", color="k", linewidth=0.5, ) message = "{0:.2f}".format(sum_over) plt.annotate(message, xy=(0.05, 0.9), xycoords="axes fraction", color="red") mids = (edges[:-1] + edges[1:]) / 2.0 fills = np.array(n[0]) fills[~mask] = med_bkg ax1.fill_between(mids, fills, med_bkg, facecolor="red", alpha=0.5) ax1.set_ylim(ymin=0.0) plt.title(title) plt.legend() plt.xlabel("Azimuth") plt.ylabel("Fraction of Total") ax2 = plt.subplot2grid((4, 1), (3, 0), colspan=3, rowspan=1, sharex=ax1) plt.plot(mids, n[1] / n[0], color="orange") plt.axhline(1.0, linestyle="--", color="k") plt.ylabel("Ratio (MC/Data)") plt.xlabel("Azimuth (rad)") xticklabels = ax1.get_xticklabels() plt.setp(xticklabels, visible=False) yticklabels = ax1.get_yticklabels() plt.setp(yticklabels[0], visible=False) plt.subplots_adjust(hspace=0.001) # ax2.set_xlim(0.0, 2*np.pi) plt.savefig(root + title + " 1D.pdf") plt.close()
of IC86_1 into a format useable for science with flarestack. The data files themselves are duplicates of those provided at: https://icecube.wisc.edu/science/data/PS-IC86-2011 """ import os import numpy as np import csv import pickle from flarestack.data.icecube.ps_tracks.ps_v002_p01 import IC86_1_dict from flarestack.icecube_utils.dataset_loader import data_loader from flarestack.shared import input_dir ref_data = data_loader(IC86_1_dict["exp_path"]) src_dir = os.path.dirname(os.path.realpath(__file__)) + "/" data_dir = src_dir + "raw_data/" output_path = input_dir + "public_datasets/" data_dtype = np.dtype([('ra', np.float), ('dec', np.float), ('logE', np.float), ('sigma', np.float), ('time', np.float), ('sinDec', np.float)]) def parse_numpy_dataset(): """Function to parse the .txt file of events into a numpy format readable by flarestack, which is the saved in the products/ subdirectory. """
def load_data(self, path, **kwargs): return data_loader(path, **kwargs)
def get_mc(floor_dict): return data_loader(floor_dict["season"]["mc_path"])
def get_data(season): mc = data_loader(season["mc_path"], floor=True) x = np.degrees(angular_distance( mc["ra"], mc["dec"], mc["trueRa"], mc["trueDec"])) y = np.degrees(mc["sigma"]) * 1.177 return mc, x, y
import os import matplotlib.pyplot as plt from flarestack.data.icecube.northern_tracks.nt_v002_p01 import diffuse_8year from flarestack.shared import plots_dir from flarestack.icecube_utils.dataset_loader import data_loader, grl_loader data_rate_dir = plots_dir + "data_rate/" # for season in txs_sample_v2 + gfu_v002_p01: for season in diffuse_8year: data = data_loader(season["exp_path"])[season["MJD Time Key"]] print(data_loader(season["exp_path"]).dtype.names) sample_dir = data_rate_dir + season["Data Sample"] + "/" if not os.path.isdir(sample_dir): os.makedirs(sample_dir) grl = grl_loader(season) print(grl.dtype.names) print(min(grl["start"]), max(grl["stop"])) print(min(grl["run"]), max(grl["run"])) print(grl[-10:]) plt.figure() plt.hist(data, bins=50, histtype="stepfilled") savepath = sample_dir + season["Name"] + ".pdf"
frac_s = np.sum(south < min_angular_err) / float(len(south)) fracs_south.append(frac_s) print("North", np.sum(north < min_angular_err), float(len(north)), \ frac_n) plt.figure() plt.plot(means, fracs_north, label="Northern Sky") plt.plot(means, fracs_south, label="Southern Sky") plt.xlabel("log(E)") plt.ylabel(r"Fraction of events sub-0.2$^\circ$ error") plt.legend() plt.savefig(sample_dir + season["Name"] + "_energy.pdf") plt.close() exp = data_loader(season["exp_path"]) mask = exp["sigma"] < min_angular_err plt.figure() plt.hist(np.degrees(exp["sigma"]), bins=100) plt.yscale("log") plt.savefig(sample_dir + season["Name"] + "_corrected.pdf") plt.close() print() print(season["Name"], "(read in with data_loader)") print(np.sum(mask), "events have an angular error less than", end=' ') print(np.degrees(min_angular_err), "degrees")