def ccsn_limits(sn_type): base = "analyses/ccsn/stasik_2017/calculate_sensitivity/" path = base + sn_type + "/real_unblind/" savepath = limit_output_path(path) print("Loading limits from", savepath) with open(savepath, "r") as f: results = Pickle.load(f) return results
def limit_sens(mh_name, pdf_type): base = f'analyses/ccsn/stasik2017/calculate_sensitivity/{mh_name}/{pdf_type}/' sub_base = base.split(os.sep) for i, _ in enumerate(sub_base): p = sub_base[0] for d in range(1, i): p += f'{os.sep}{sub_base[d]}' p = limits_dir + p if not os.path.isdir(p): logging.debug(f'making directory {p}') os.mkdir(p) return limit_output_path(base)
def limit_sens(base): # base = f'{raw_output_dir}/calculate_sensitivity/{mh_name}/{pdf_type}/' sub_base = base.split(os.sep) for i, _ in enumerate(sub_base): p = sub_base[0] for d in range(1, i): p += f"{os.sep}{sub_base[d]}" p = limits_dir + p if not os.path.isdir(p): logging.debug(f"making directory {p}") os.mkdir(p) return limit_output_path(base)
def __init__(self, unblind_dict, seed=None, scan_2d=False, **kwargs): self.unblind_dict = unblind_dict unblind_dict["unblind_bool"] = True unblind_dict["mock_unblind_bool"] = mock_unblind unblind_dict["inj_dict"] = {} if np.logical_and(not mock_unblind, not disable_warning): self.check_unblind() if mock_unblind is False: self.mock_unblind = False else: self.mock_unblind = True ParentMiminisationHandler.__init__(self, unblind_dict) try: if self.mock_unblind: self.limit_path = limit_output_path( self.unblind_dict["name"] + "mock_unblind/") self.unblind_res_path = unblinding_output_path( self.unblind_dict["name"] + "mock_unblind/") else: self.limit_path = limit_output_path( self.unblind_dict["name"] + "real_unblind/") self.unblind_res_path = unblinding_output_path( self.unblind_dict["name"] + "real_unblind/") except KeyError: self.limit_path = np.nan self.unblind_res_path = np.nan if self.name != " /": if self.mock_unblind: self.name += "mock_unblind/" else: self.name += "real_unblind/" self.plot_dir = plot_output_dir(self.name) try: os.makedirs(os.path.dirname(self.unblind_res_path)) except OSError: pass # Avoid redoing unblinding if not reunblind: try: logger.info( "Not re-unblinding, loading results from {0}".format( self.unblind_res_path)) with open(self.unblind_res_path, "rb") as f: self.res_dict = pickle.load(f) except FileNotFoundError: logger.warning( "No pickle files containing unblinding results found. " "Re-unblinding now.") # Otherwise unblind if not hasattr(self, "res_dict"): logger.info("Unblinding catalogue") # Minimise likelihood and produce likelihood scans self.res_dict = self.simulate_and_run(0, seed) logger.info(self.res_dict) # Quantify the TS value significance self.ts = np.array([self.res_dict["TS"]])[0] self.ts_type = unblind_dict.get('ts_type', 'Standard') self.sigma = np.nan logger.info("Test Statistic of: {0}".format(self.ts)) ub_res_dict = { "Parameters": self.res_dict['Parameters'], "TS": self.res_dict['TS'], "Flag": self.res_dict['Flag'] } logger.info("Saving unblinding results to {0}".format( self.unblind_res_path)) with open(self.unblind_res_path, "wb") as f: pickle.dump(ub_res_dict, f) try: path = self.unblind_dict["background_ts"] self.pickle_dir = name_pickle_output_dir(path) self.output_file = self.plot_dir + "TS.pdf" self.compare_to_background_TS() except KeyError: logger.warning("No Background TS Distribution specified. " "Cannot assess significance of TS value.") if full_plots: self.calculate_upper_limits() if isinstance(self, FlareMinimisationHandler): self.neutrino_lightcurve() else: self.scan_likelihood(scan_2d=scan_2d)
def __init__(self, unblind_dict, seed=None, scan_2d=False, **kwargs): self.unblind_dict = unblind_dict unblind_dict["unblind_bool"] = True unblind_dict["mock_unblind_bool"] = mock_unblind unblind_dict["inj_dict"] = {} # specifies whether all available background distributions should be merged # to improve statistics (see loop over subdirs in self.compare_to_background_TS() self.use_merged_background_ts_distributions = kwargs.get( "merge_background_ts_distr", True) self.background_median = np.nan # gives the medians for the separate background distributions # this is used when background TS distributions are not merged self.background_median_gamma = dict() if np.logical_and(not mock_unblind, not disable_warning): self.check_unblind() if mock_unblind is False: self.mock_unblind = False else: self.mock_unblind = True ParentMiminisationHandler.__init__(self, unblind_dict) try: if self.mock_unblind: self.limit_path = limit_output_path( self.unblind_dict["name"] + "mock_unblind/") self.unblind_res_path = unblinding_output_path( self.unblind_dict["name"] + "mock_unblind/") else: self.limit_path = limit_output_path( self.unblind_dict["name"] + "real_unblind/") self.unblind_res_path = unblinding_output_path( self.unblind_dict["name"] + "real_unblind/") except KeyError: self.limit_path = np.nan self.unblind_res_path = np.nan if self.name != " /": if self.mock_unblind: self.name += "mock_unblind/" else: self.name += "real_unblind/" self.plot_dir = plot_output_dir(self.name) try: os.makedirs(os.path.dirname(self.unblind_res_path)) except OSError: pass # Avoid redoing unblinding if not reunblind: try: logger.info( "Not re-unblinding, loading results from {0}".format( self.unblind_res_path)) with open(self.unblind_res_path, "rb") as f: self.res_dict = pickle.load(f) except FileNotFoundError: logger.warning( "No pickle files containing unblinding results found. " "Re-unblinding now.") # Otherwise unblind if not hasattr(self, "res_dict"): logger.info("Unblinding catalogue") # Minimise likelihood and produce likelihood scans self.res_dict = self.simulate_and_run(0, seed) logger.info(self.res_dict) # Quantify the TS value significance self.bkg_ts_array = None self.ts = np.array([self.res_dict["TS"]])[0] self.sigma = np.nan # the raw pre-trial p-value will be calculated using only the TS distributions without fitting them # This is only valid if you ran enough background trials! self.raw_pre_trial_pvalue = np.nan self.ts_type = get_ts_fit_type(unblind_dict) logger.info("Test Statistic of: {0}".format(self.ts)) ub_res_dict = { "Parameters": self.res_dict["Parameters"], "TS": self.res_dict["TS"], "Flag": self.res_dict["Flag"], } logger.info("Saving unblinding results to {0}".format( self.unblind_res_path)) with open(self.unblind_res_path, "wb") as f: pickle.dump(ub_res_dict, f) try: path = self.unblind_dict["background_ts"] self.pickle_dir = name_pickle_output_dir(path) self.output_file = self.plot_dir + "TS.pdf" self.compare_to_background_TS() except KeyError as e: logger.warning(f"No Background TS Distribution specified. " f"Cannot assess significance of TS value: {e}!") if full_plots: self.calculate_upper_limits() if isinstance(self, FlareMinimisationHandler): self.neutrino_lightcurve() else: self.scan_likelihood(scan_2d=scan_2d)