def read_and_dump_ntuple(fname, half_life, spectrum_name, save_path): """ Creates both mc and reco spectra from ntuple files, dumping the results as a spectrum object in a hdf5 file Args: fname (str): The file to be evaluated half_life (float): Half-life of isotope spectrum_name (str): Name to be applied to the spectrum save_path (str): Path to a directory where the hdf5 files will be dumped Returns: None """ mc_spec = fill_spectrum.fill_mc_ntuple_spectrum(fname, half_life, spectrumname="%s_mc" % (spectrum_name)) reco_spec = fill_spectrum.fill_reco_ntuple_spectrum( fname, half_life, spectrumname="%s_reco" % (spectrum_name)) # Plot plot_spectrum(mc_spec) plot_spectrum(reco_spec) # Dump to file store.dump("%s/%s_mc.hdf5" % (save_path, spectrum_name), mc_spec) store.dump("%s/%s_reco.hdf5" % (save_path, spectrum_name), reco_spec)
def test_serialisation(self): """ Test saving and then reloading a test spectra. """ test_decays = 10 test_spectra = spectra.Spectra("Test", test_decays) for x in range(0, test_decays): energy = random.uniform(0, test_spectra._energy_high) radius = random.uniform(0, test_spectra._radial_high) time = random.uniform(0, test_spectra._time_high) test_spectra.fill(energy, radius, time) store.dump("test.hdf5", test_spectra) loaded_spectra = store.load("test.hdf5") self.assertTrue(loaded_spectra.sum() == test_decays) self.assertTrue(numpy.array_equal(test_spectra._data, loaded_spectra._data)) self.assertTrue(test_spectra._energy_low == loaded_spectra._energy_low) self.assertTrue(test_spectra._energy_high == loaded_spectra._energy_high) self.assertTrue(test_spectra._energy_bins == loaded_spectra._energy_bins) self.assertTrue(test_spectra._energy_width == loaded_spectra._energy_width) self.assertTrue(test_spectra._radial_low == loaded_spectra._radial_low) self.assertTrue(test_spectra._radial_high == loaded_spectra._radial_high) self.assertTrue(test_spectra._radial_bins == loaded_spectra._radial_bins) self.assertTrue(test_spectra._radial_width == loaded_spectra._radial_width) self.assertTrue(test_spectra._time_low == loaded_spectra._time_low) self.assertTrue(test_spectra._time_high == loaded_spectra._time_high) self.assertTrue(test_spectra._time_bins == loaded_spectra._time_bins) self.assertTrue(test_spectra._time_width == loaded_spectra._time_width) self.assertTrue(test_spectra._num_decays == loaded_spectra._num_decays)
def read_and_dump_root(fname, config_path, spectrum_name, save_path, bipo, fv_radius, outer_radius): """ Creates both mc and reco spectra from ROOT files, dumping the results as a spectrum object in a hdf5 file Args: fname (str): The file to be evaluated config_path (str): Path to the config file spectrum_name (str): Name to be applied to the spectrum save_path (str): Path to a directory where the hdf5 files will be dumped bipo (bool): Apply Bi*Po* cuts when extracting data if True. fv_radius (float): Cut events outside the fiducial volume of this radius. outer_radius (float): Used for calculating the radial3 parameter. See :class:`echidna.core.dsextract` for details. """ config = SpectraConfig.load_from_file(config_path) if outer_radius: if "radial3" not in config.get_dims(): raise ValueError("Outer radius passed as an command line arg " "but no radial3 in the config file.") spectrum = fill_spectrum.fill_from_root( fname, spectrum_name="%s" % (spectrum_name), config=config, bipo=bipo, fv_radius=fv_radius, outer_radius=outer_radius) else: spectrum = fill_spectrum.fill_from_root( fname, spectrum_name="%s" % (spectrum_name), config=config, bipo=bipo, fv_radius=fv_radius) # Plot plot_spectrum(spectrum, config) # Dump to file store.dump("%s/%s.hdf5" % (save_path, spectrum_name), spectrum)
def create_combined_ntuple_spectrum(data_path, half_life, bkgnd_name, save_path): """ Creates both mc and reco spectra from directory containing background ntuples, dumping the results as a spectrum object in a hdf5 file. Args: data_path (str): Path to directory containing the ntuples to be evaluated half_life (float): Half-life of isotope bkgnd_name (str): Name of the background being processed save_path (str): Path to a directory where the hdf5 files will be dumped Returns: None """ file_list = os.listdir(data_path) for idx, fname in enumerate(file_list): file_path = "%s/%s" % (data_path, fname) if idx == 0: mc_spec = fill_spectrum.fill_mc_ntuple_spectrum(file_path, half_life, spectrumname = "%s_mc" % bkgnd_name) reco_spec = fill_spectrum.fill_reco_ntuple_spectrum(file_path, half_life, spectrumname = "%s_reco" % bkgnd_name) else: mc_spec = fill_spectrum.fill_mc_ntuple_spectrum(file_path, half_life, spectrum = mc_spec) reco_spec = fill_spectrum.fill_reco_ntuple_spectrum(file_path, half_life, spectrum = reco_spec) # Plot plot_spectrum(mc_spec) plot_spectrum(reco_spec) # Dump to file store.dump("%s%s_mc.hdf5" % (save_path, bkgnd_name), mc_spec) store.dump("%s%s_reco.hdf5" % (save_path, bkgnd_name), reco_spec)
def main(args): """Smears energy and dumps spectra. Args: args (Namespace): Container for arguments. See >>> python dump_smeared_energy.py -h """ if args.dest: if os.path.isdir(args.dest): directory = args.dest if directory[-1] != "/": directory += "/" else: raise ValueError("%s does not exist" % args.dest) else: directory = os.path.dirname(args.path)+"/" # strip filename # strip directory and extension filename = os.path.splitext(os.path.basename(args.path))[0] if args.energy_resolution: if args.gaus: energy_smear = smear.EnergySmearRes(poisson=False) else: energy_smear = smear.EnergySmearRes(poisson=True) energy_smear.set_resolution(args.energy_resolution) else: # use light yield if args.gaus: energy_smear = smear.EnergySmearLY(poisson=False) else: energy_smear = smear.EnergySmearLY(poisson=True) energy_smear.set_resolution(args.light_yield) spectrum = store.load(args.path) if args.smear_method == "weight": # Use default smear method for par in spectrum.get_config().get_pars(): if "energy" in par: energy_par = par spectrum = energy_smear.weighted_smear(spectrum, par=energy_par) elif args.smear_method == "random": for par in spectrum.get_config().get_pars(): if "energy" in par: energy_par = par spectrum = energy_smear.random_smear(spectrum, par=energy_par) else: # Not a valid smear method parser.error(args.smear_method + " is not a valid smear method") if args.energy_resolution: str_rs = str(args.energy_resolution) filename = directory + filename + "_" + str_rs + "rs.hdf5" else: str_ly = str(args.light_yield) if str_ly[-2:] == '.0': str_ly = str_ly[:-2] filename = directory + filename + "_" + str_ly + "ly.hdf5" store.dump(filename, spectrum)
def main(args): """Smears energy and dumps spectra. Args: args (Namespace): Container for arguments. See:: python dump_smeared_energy.py -h """ if not args.input: parser.print_help() raise ValueError("No input file provided") if not args.pars: parser.print_help() raise ValueError("No parameters provided") if not args.low and not args.up: parser.print_help() raise ValueError("Must provide lower and/or upper bounds to" "shrink to.") spectrum = store.load(args.input) num_pars = len(args.pars) if args.low and args.up: if len(args.low) != num_pars or len(args.up) != num_pars: raise ValueError("Must have the same number of pars as bounds") shrink = {} for i, par in enumerate(args.pars): par_low = par+"_low" par_high = par+"_high" shrink[par_low] = args.low[i] shrink[par_high] = args.up[i] spectrum.shrink(**shrink) elif args.low: if len(args.low) != num_pars: raise ValueError("Must have the same number of pars as bounds") shrink = {} for i, par in enumerate(args.pars): par_low = par+"_low" shrink[par_low] = args.low[i] spectrum.shrink(**shrink) else: if len(args.up) != num_pars: raise ValueError("Must have the same number of pars as bounds") shrink = {} for i, par in enumerate(args.pars): par_high = par+"_high" shrink[par_high] = args.up[i] spectrum.shrink(**shrink) f_out = args.output if not f_out: directory = os.path.dirname(args.input) filename = os.path.splitext(os.path.basename(args.input))[0] f_out = directory + "/" + filename + "_shrunk.hdf5" store.dump(f_out, spectrum) _logger.info("Shrunk "+str(args.input)+", saved to "+str(f_out))
def combiner(path,filename): flist=np.array(glob.glob(path)) print flist first = True for hdf5 in flist: print hdf5 if first: spectrum1 = store.load(hdf5) first = False else: spectrum2 = store.load(hdf5) spectrum1.add(spectrum2) store.dump(filename, spectrum1)
def create_combined_ntuple_spectrum(data_path, config_path, bkgnd_name, save_path, bipo, fv_radius, outer_radius): """ Creates both mc, truth and reco spectra from directory containing background ntuples, dumping the results as a spectrum object in an hdf5 file. Args: data_path (str): Path to directory containing the ntuples to be evaluated config_path (str): Path to config file bkgnd_name (str): Name of the background being processed save_path (str): Path to a directory where the hdf5 files will be dumped bipo (bool): Apply Bi*Po* cuts when extracting data if True. fv_radius (float): Cut events outside the fiducial volume of this radius. outer_radius (float): Used for calculating the radial3 parameter. See :mod:`echidna.core.dsextract` for details. """ config = SpectraConfig.load_from_file(config_path) file_list = os.listdir(data_path) if outer_radius: if "radial3" not in config.get_dims(): raise ValueError("Outer radius passed as an command line arg " "but no radial3 in the config file.") for idx, fname in enumerate(file_list): file_path = "%s/%s" % (data_path, fname) if idx == 0: spec = fill_spectrum.fill_from_ntuple( file_path, spectrum_name="%s" % bkgnd_name, config=config, bipo=bipo, fv_radius=fv_radius, outer_radius=outer_radius) else: spec = fill_spectrum.fill_from_ntuple( file_path, spectrum=spec, bipo=bipo, fv_radius=fv_radius, outer_radius=outer_radius) else: for idx, fname in enumerate(file_list): file_path = "%s/%s" % (data_path, fname) if idx == 0: spec = fill_spectrum.fill_from_ntuple( file_path, spectrum_name="%s" % bkgnd_name, config=config, bipo=bipo, fv_radius=fv_radius) else: spec = fill_spectrum.fill_from_ntuple( file_path, spectrum=spec, bipo=bipo, fv_radius=fv_radius) # Plot plot_spectrum(spec, config) # Dump to file store.dump("%s%s.hdf5" % (save_path, bkgnd_name), spec)
def main(args): """ Scales and dumps spectra. Args: args (Namespace): Container for arguments. See:: $ python dump_scaled.py -h Raises: IOError: If no file is given to scale ValueError: If no scale factor is given ValueError: If no parameter is given to scale. ValueError: If destination directory does not exits. """ if not args.file: parser.print_help() raise IOError("No file given in command line to scale") if not args.scale: parser.print_help() raise ValueError("No scale factor given") if not args.par: parser.print_help() raise ValueError("No parameter to scale given") if args.dest: if os.path.isdir(args.dest): directory = args.dest if directory[-1] != "/": directory += "/" else: raise ValueError("%s does not exist." % args.dest) else: directory = os.path.dirname(args.file) + "/" filename = os.path.splitext(os.path.basename(args.file))[0] scaler = scale.Scale() scaler.set_scale_factor(args.scale) spectrum = store.load(args.file) scaled_spectrum = scaler.scale(spectrum, args.par) str_sc = str(args.scale) if str_sc[-2:] == '.0': str_sc = str_sc[:-2] str_sc.rstrip('0') filename = directory + filename + "_" + str_sc + "sc.hdf5" store.dump(filename, scaled_spectrum)
def read_and_dump_root(fname, half_life, spectrum_name, save_path): """ Creates both mc and reco spectra from ROOT files, dumping the results as a spectrum object in a hdf5 file. Args: fname (str): The file to be evaluated. half_life (float): Half-life of isotope. spectrum_name (str): Name to be applied to the spectrum. save_path (str): Path to a directory where the hdf5 files will be dumped. """ mc_spec = fill_spectrum.fill_mc_spectrum( fname, half_life, spectrumname="%s_mc" % (spectrum_name)) reco_spec = fill_spectrum.fill_reco_spectrum( fname, half_life, spectrumname="%s_reco" % (spectrum_name)) truth_spec = fill_spectrum.fill_truth_spectrum( fname, half_life, spectrumname="%s_truth" % (spectrum_name)) # Plot plot_spectrum(mc_spec) plot_spectrum(reco_spec) plot_spectrum(truth_spec) # Dump to file store.dump("%s/%s_mc.hdf5" % (save_path, spectrum_name), mc_spec) store.dump("%s/%s_reco.hdf5" % (save_path, spectrum_name), reco_spec) store.dump("%s/%s_truth.hdf5" % (save_path, spectrum_name), truth_spec)
def test_serialisation(self): """ Test saving and then reloading a test spectra. """ test_decays = 10 test_spectra = spectra.Spectra("Test", test_decays) for x in range(0, test_decays): energy = random.uniform(0, test_spectra._energy_high) radius = random.uniform(0, test_spectra._radial_high) time = random.uniform(0, test_spectra._time_high) test_spectra.fill(energy, radius, time) store.dump("test.hdf5", test_spectra) loaded_spectra = store.load("test.hdf5") self.assertTrue(loaded_spectra.sum() == test_decays) self.assertTrue( numpy.array_equal(test_spectra._data, loaded_spectra._data)) self.assertTrue(test_spectra._energy_low == loaded_spectra._energy_low) self.assertTrue( test_spectra._energy_high == loaded_spectra._energy_high) self.assertTrue( test_spectra._energy_bins == loaded_spectra._energy_bins) self.assertTrue( test_spectra._energy_width == loaded_spectra._energy_width) self.assertTrue(test_spectra._radial_low == loaded_spectra._radial_low) self.assertTrue( test_spectra._radial_high == loaded_spectra._radial_high) self.assertTrue( test_spectra._radial_bins == loaded_spectra._radial_bins) self.assertTrue( test_spectra._radial_width == loaded_spectra._radial_width) self.assertTrue(test_spectra._time_low == loaded_spectra._time_low) self.assertTrue(test_spectra._time_high == loaded_spectra._time_high) self.assertTrue(test_spectra._time_bins == loaded_spectra._time_bins) self.assertTrue(test_spectra._time_width == loaded_spectra._time_width) self.assertTrue(test_spectra._num_decays == loaded_spectra._num_decays)
def create_combined_ntuple_spectrum(data_path, half_life, bkgnd_name, save_path): """ Creates both mc and reco spectra from directory containing background ntuples, dumping the results as a spectrum object in a hdf5 file. Args: data_path (str): Path to directory containing the ntuples to be evaluated half_life (float): Half-life of isotope bkgnd_name (str): Name of the background being processed save_path (str): Path to a directory where the hdf5 files will be dumped Returns: None """ file_list = os.listdir(data_path) for idx, fname in enumerate(file_list): file_path = "%s/%s" % (data_path, fname) if idx == 0: mc_spec = fill_spectrum.fill_mc_ntuple_spectrum( file_path, half_life, spectrumname="%s_mc" % bkgnd_name) reco_spec = fill_spectrum.fill_reco_ntuple_spectrum( file_path, half_life, spectrumname="%s_reco" % bkgnd_name) else: mc_spec = fill_spectrum.fill_mc_ntuple_spectrum(file_path, half_life, spectrum=mc_spec) reco_spec = fill_spectrum.fill_reco_ntuple_spectrum( file_path, half_life, spectrum=reco_spec) # Plot plot_spectrum(mc_spec) plot_spectrum(reco_spec) # Dump to file store.dump("%s%s_mc.hdf5" % (save_path, bkgnd_name), mc_spec) store.dump("%s%s_reco.hdf5" % (save_path, bkgnd_name), reco_spec)
parser.add_argument("-m", "--smear_method", nargs='?', const="weight", type=str, default="weight", help="specify the smearing method to use") parser.add_argument("path", type=str, help="specify path to hdf5 file") args = parser.parse_args() directory = args.path[:args.path.rfind("/") + 1] # strip filename # strip directory and extension filename = args.path[args.path.rfind("/") + 1:args.path.rfind(".")] smearer = smear.Smear() spectrum = store.load(args.path) if args.smear_method == "weight": # Use default smear method smeared_spectrum = smearer.weight_gaussian_energy_spectra(spectrum) smeared_spectrum = smearer.weight_gaussian_radius_spectra( smeared_spectrum) elif args.smear_method == "random": smeared_spectrum = smearer.random_gaussian_energy_spectra(spectrum) smeared_spectrum = smearer.random_gaussian_radius_spectra( smeared_spectrum) else: # Not a valid smear method parser.error(args.smear_method + " is not a valid smear method") filename = directory + filename + "_smeared" + ".hdf5" store.dump(filename, smeared_spectrum)
""" from echidna.output import store from echidna.core import spectra if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", type=str, help="Input spectra to rebin") parser.add_argument("-o", "--output", type=str, default=None, help="Name of output. Default adds _rebin to name") parser.add_argument("-b", "--bins", nargs='+', type=int, help="Number of bins for each dimension") args = parser.parse_args() directory = args.input[:args.input.rfind("/")+1] # strip filename # strip directory and extension filename = args.input[args.input.rfind("/")+1:args.input.rfind(".")] spectrum = store.load(args.input) new_bins = args.bins print spectrum._data.shape print "sum pre bin", spectrum.sum() spectrum.rebin(new_bins) print 'Sum post bin:', spectrum.sum() print spectrum._data.shape f_out = args.output if not f_out: f_out = directory + filename + "_rebin" + ".hdf5" print "Rebinned", args.input, ", saved to", f_out store.dump(f_out, spectrum)
def get_limit(self, limit=2.71, min_stat=None, store_limit=True, store_fits=False, store_spectra=False, limit_fname=None): """ Get the limit using the signal spectrum. Args: limit (float, optional): The value of the test statisic which corresponds to the limit you want to set. The default is 2.71 which corresponds to 90% CL when using a chi-squared test statistic. stat_zero (float or :class:`numpy.ndarray`, optional): Enables calculation of e.g. delta chi-squared. Include values of test statistic for zero signal contribution, so these can be subtracted from the values of the test statistic, with signal. store_limit (bool, optional): If True (default) then a hdf5 file containing the :class:`echidna.fit.fit_results.LimitResults` object is saved. store_fits (bool, optional): If True then the :class:`echidna.fit.fit_results.FitResults` objects at each signal scale is stored in the :class:`echidna.fit.fit_results.LimitResults` object. Default is False. store_spectra (bool, optional): If True then the spectra used for fitting are saved to hdf5. Default is False. limit_fname (string): Filename to save the `:class:`echidna.fit.fit_results.LimitResults` to. Raises: LimitError: If all values in the array are below limit. Returns: float: The signal scaling at the limit you are setting. """ par = self._signal.get_fit_config().get_par("rate") if min_stat: # If supplied specific stat_zero use this self._logger.warning("Overriding min_stat with supplied value") logging.getLogger("extra").warning(" --> %s" % min_stat) else: # check zero signal stat in case its not in self._stats self._fitter.remove_signal() fit_stats = self._fitter.fit() if type(fit_stats) is tuple: fit_stats = fit_stats[0] min_stat = copy.copy(fit_stats) self._logger.info("Calculated stat_zero: %s" % min_stat) fit_results = copy.deepcopy(self._fitter.get_minimiser()) if fit_results: self._logger.info("Fit summary:") logging.getLogger("extra").info( "\n%s\n" % json.dumps(fit_results.get_summary())) # Create summary scales = par.get_values() if type(min_stat) is numpy.ndarray: shape = self._signal.get_fit_config().get_shape() + min_stat.shape else: shape = self._signal.get_fit_config().get_shape() stats = numpy.zeros(shape, dtype=numpy.float64) # Create stats array self._logger.debug("Creating stats array with shape %s" % str(shape)) # Loop through signal scalings self._logger.debug("Testing signal scalings:\n\n") logging.getLogger("extra").debug(str(par.get_values())) for i, scale in enumerate(par.get_values()): self._logger.debug("signal scale: %.4g" % scale) print "scale", scale if not numpy.isclose(scale, 0.): if self._fitter.get_signal() is None: self._fitter.set_signal(self._signal, shrink=False) self._fitter._signal.scale(scale) else: # want no signal contribution self._fitter.remove_signal() self._logger.warning( "Removing signal in fit for scale %.4g" % scale) fit_stats = self._fitter.fit() if type(fit_stats) is tuple: fit_stats = fit_stats[0] stats[i] = fit_stats fit_results = copy.deepcopy(self._fitter.get_minimiser()) if fit_results: results_summary = fit_results.get_summary() for par_name, value in results_summary.iteritems(): self._limit_results.set_best_fit(i, value.get("best_fit"), par_name) self._limit_results.set_penalty_term( i, value.get("penalty_term"), par_name) if store_fits: self._limit_results.set_fit_result(i, fit_results) # Convert stats to delta - subtracting minimum stats -= min_stat # Also want to know index of minimum min_bin = numpy.argmin(stats) self._limit_results._stats = stats stats = self._limit_results.get_full_stats() try: # Slice from min_bin upwards log_text = "" i_limit = numpy.where(stats[min_bin:] > limit)[0][0] limit = par.get_values()[min_bin + i_limit] log_text += "\n===== Limit Summary =====\nLimit found at:\n" log_text += "Signal Decays: %.4g\n" % limit for parameter in self._fitter.get_fit_config().get_pars(): cur_par = self._fitter.get_fit_config().get_par(parameter) log_text += "--- systematic: %s ---\n" % parameter log_text += ("Best fit: %4g\n" % self._limit_results.get_best_fit(i_limit, parameter)) log_text += ("Prior: %.4g\n" % cur_par.get_prior()) log_text += ("Sigma: %.4g\n" % cur_par.get_sigma()) log_text += ("Penalty term: %.4g\n" % self._limit_results.get_penalty_term(i_limit, parameter)) log_text += "----------------------------\n" log_text += "Test statistic: %.4f\n" % numpy.sum(stats[i_limit]) log_text += "N.D.F.: 1\n" # Only fit one dof currently logging.getLogger("extra").info("\n%s\n" % log_text) if store_limit: if limit_fname: if limit_fname[-5:] != '.hdf5': limit_fname += '.hdf5' else: timestamp = datetime.datetime.now().strftime( "%Y-%m-%d_%H-%M-%S") path = output.__default_save_path__ + "/" fname = (self._limit_results.get_name() + "_" + timestamp + ".hdf5") limit_fname = path + fname store.dump_limit_results(limit_fname, self._limit_results) self._logger.info("Saved summary of %s to file %s" % (self._limit_results.get_name(), limit_fname)) if store_spectra: path = output.__default_save_path__ + "/" fname = self._fitter.get_data()._name + "_data.hdf5" store.dump(path + fname, self._fitter.get_data(), append=True, group_name="data") if self._fitter.get_fixed_background(): fname = (self._fitter.get_fixed_background()._name + "_fixed.hdf5") store.dump(path + fname, self._fitter.get_fixed_background(), append=True, group_name="fixed") if self._fitter.get_floating_backgrounds(): for background in self._fitter.get_floating_backgrounds(): fname = background._name + "_float.hdf5" store.dump(path + fname, background, append=True, group_name=background.get_name()) fname = self._signal._name + "_signal.hdf5" store.dump(path + fname, self._signal, append=True, group_name="signal") return limit except IndexError as detail: # Slice from min_bin upwards log_text = "" i_limit = numpy.argmax(stats[min_bin:]) limit = par.get_values()[min_bin + i_limit] log_text += "\n===== Limit Summary =====\nNo limit found:\n" log_text += "Signal Decays (at max stat): %.4g\n" % limit for parameter in self._fitter.get_fit_config().get_pars(): cur_par = self._fitter.get_fit_config().get_par(parameter) log_text += "--- systematic: %s ---\n" % parameter log_text += ("Best fit: %4g\n" % self._limit_results.get_best_fit(i_limit, parameter)) log_text += ("Prior: %.4g\n" % cur_par.get_prior()) log_text += ("Sigma: %.4g\n" % cur_par.get_sigma()) log_text += ("Penalty term: %.4g\n" % self._limit_results.get_penalty_term(i_limit, parameter)) log_text += "----------------------------\n" log_text += "Test statistic: %.4f\n" % numpy.sum(stats[i_limit]) log_text += "N.D.F.: 1\n" # Only fit one dof currently logging.getLogger("extra").info("\n%s" % log_text) if store_limit: timestamp = datetime.datetime.now().strftime( "%Y-%m-%d_%H-%M-%S") path = output.__default_save_path__ + "/" fname = (self._limit_results.get_name() + "_" + timestamp + ".hdf5") store.dump_limit_results(path + fname, self._limit_results) self._logger.info("Saved summary of %s to file %s" % (self._limit_results.get_name(), path + fname)) if store_spectra: store.dump(path + fname, self._fitter.get_data(), append=True, group_name="data") if self._fitter.get_fixed_background(): store.dump(path + fname, self._fitter.get_fixed_background(), append=True, group_name="fixed") if self._fitter.get_floating_backgrounds(): for background in self._fitter.get_floating_backgrounds(): store.dump(path + fname, background, append=True, group_name=background.get_name()) store.dump(path + fname, self._signal, append=True, group_name="signal") self._logger.error("Recieived: IndexError: %s" % detail) raise LimitError("Unable to find limit. Max stat: %s, Limit: %s" % (stats.max(), limit))
$ python echidna/scripts/combine_hdf5.py -f /path/to/example1.hdf5 /path/to/example2.hdf5 This will create the hdf5 file ``combined.hdf5``. There is no limit to the number of files you can combine. """ from echidna.output import store import argparse if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-f", "--files", nargs='+', type=str, help="Space seperated hdf5 files to combine.") args = parser.parse_args() if not args.files: parser.print_help() parser.error("Must pass more than 1 file to combine") if len(args.files) < 2: parser.print_help() parser.error("Must pass more than 1 file to combine") first = True for hdf5 in args.files: if first: spectrum1 = store.load(hdf5) first = False else: spectrum2 = store.load(hdf5) spectrum1.add(spectrum2) store.dump("combined.hdf5", spectrum1)
else: # use light yield energy_smear = smear.EnergySmearLY() radial_smear = smear.RadialSmear() spectrum = store.load(args.path) if args.smear_method == "weight": # Use default smear method for par in spectrum.get_config().get_pars(): if "energy" in par: energy_par = par spectrum = energy_smear.weighted_smear(spectrum, par=energy_par) elif "radial" in par: radial_par = par spectrum = radial_smear.weighted_smear(spectrum, par=radial_par) elif args.smear_method == "random": for par in spectrum.get_config().get_pars(): if "energy" in par: energy_par = par spectrum = energy_smear.random_smear(spectrum, par=energy_par) elif "radial" in par: radial_par = par spectrum = radial_smear.random_smear(spectrum, par=radial_par) else: # Not a valid smear method parser.error(args.smear_method + " is not a valid smear method") filename = directory + filename + "_smeared.hdf5" store.dump(filename, spectrum)
This will create the hdf5 file ``combined.hdf5``. There is no limit to the number of files you can combine. """ from echidna.output import store import argparse if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-f", "--files", nargs='+', type=str, help="Space seperated hdf5 files to combine.") args = parser.parse_args() if not args.files: parser.print_help() parser.error("Must pass more than 1 file to combine") if len(args.files) < 2: parser.print_help() parser.error("Must pass more than 1 file to combine") first = True for hdf5 in args.files: if first: spectrum1 = store.load(hdf5) first = False else: spectrum2 = store.load(hdf5) spectrum1.add(spectrum2) store.dump("combined.hdf5", spectrum1)
def test_serialisation(self): """ Test saving and then reloading a test spectra. """ test_spectra = self._test_spectra # Save values spectra_config = test_spectra.get_config() spectra_pars = spectra_config.get_pars() energy_high = spectra_config.get_par("energy_mc").get_high() energy_bins = spectra_config.get_par("energy_mc").get_bins() energy_low = spectra_config.get_par("energy_mc").get_low() radial_high = spectra_config.get_par("radial_mc").get_high() radial_bins = spectra_config.get_par("radial_mc").get_bins() radial_low = spectra_config.get_par("radial_mc").get_low() energy_width = spectra_config.get_par("energy_mc").get_width() radial_width = spectra_config.get_par("radial_mc").get_width() spectra_fit_config = test_spectra.get_fit_config() spectra_fit_pars = spectra_fit_config.get_pars() rate_prior = spectra_fit_config.get_par("rate").get_prior() rate_sigma = spectra_fit_config.get_par("rate").get_sigma() rate_low = spectra_fit_config.get_par("rate").get_low() rate_high = spectra_fit_config.get_par("rate").get_high() rate_bins = spectra_fit_config.get_par("rate").get_bins() # Fill spectrum for x in range(0, self._test_decays): energy = random.uniform(energy_low, energy_high) radius = random.uniform(radial_low, radial_high) test_spectra.fill(energy_mc=energy, radial_mc=radius) # Dump spectrum store.dump("test.hdf5", test_spectra) # Re-load spectrum loaded_spectra = store.load("test.hdf5") # Re-load saved values spectra_config = loaded_spectra.get_config() spectra_pars2 = spectra_config.get_pars() energy_high2 = spectra_config.get_par("energy_mc").get_high() energy_bins2 = spectra_config.get_par("energy_mc").get_bins() energy_low2 = spectra_config.get_par("energy_mc").get_low() radial_high2 = spectra_config.get_par("radial_mc").get_high() radial_bins2 = spectra_config.get_par("radial_mc").get_bins() radial_low2 = spectra_config.get_par("radial_mc").get_low() energy_width2 = spectra_config.get_par("energy_mc").get_width() radial_width2 = spectra_config.get_par("radial_mc").get_width() spectra_fit_config = loaded_spectra.get_fit_config() spectra_fit_pars2 = spectra_fit_config.get_pars() rate_prior2 = spectra_fit_config.get_par("rate").get_prior() rate_sigma2 = spectra_fit_config.get_par("rate").get_sigma() rate_low2 = spectra_fit_config.get_par("rate").get_low() rate_high2 = spectra_fit_config.get_par("rate").get_high() rate_bins2 = spectra_fit_config.get_par("rate").get_bins() # Run tests self.assertTrue(loaded_spectra.sum() == self._test_decays, msg="Original decays: %.3f, loaded spectra sum %3f" % (float(self._test_decays), float(loaded_spectra.sum()))) self.assertTrue(numpy.array_equal(self._test_spectra._data, loaded_spectra._data), msg="Original _data does not match loaded _data") self.assertTrue(test_spectra._num_decays == loaded_spectra._num_decays, msg="Original num decays: %.3f, Loaded: %.3f" % (float(test_spectra._num_decays), float(loaded_spectra._num_decays))) # Check order of parameters self.assertListEqual(spectra_pars, spectra_pars2) self.assertListEqual(spectra_fit_pars, spectra_fit_pars2) self.assertTrue(energy_low == energy_low2, msg="Original energy low: %.4f, Loaded: %.4f" % (energy_low, energy_low2)) self.assertTrue(energy_high == energy_high2, msg="Original energy high: %.4f, Loaded: %.4f" % (energy_high, energy_high2)) self.assertTrue(energy_bins == energy_bins2, msg="Original energy bins: %.4f, Loaded: %.4f" % (energy_bins, energy_bins2)) self.assertTrue(energy_width == energy_width2, msg="Original energy width: %.4f, Loaded: %.4f" % (energy_width, energy_width2)) self.assertTrue(radial_low == radial_low2, msg="Original radial low: %.4f, Loaded: %.4f" % (radial_low, radial_low2)) self.assertTrue(radial_high == radial_high2, msg="Original radial high: %.4f, Loaded: %.4f" % (radial_high, radial_high2)) self.assertTrue(radial_bins == radial_bins2, msg="Original radial bins: %.4f, Loaded: %.4f" % (radial_bins, radial_bins2)) self.assertTrue(radial_width == radial_width2, msg="Original radial width: %.4f, Loaded: %.4f" % (radial_width, radial_width2)) self.assertTrue(rate_prior == rate_prior2, msg="Original rate prior: %.4f, Loaded: %.4f" % (rate_prior, rate_prior2)) self.assertTrue(rate_sigma == rate_sigma2, msg="Original rate sigma: %.4f, Loaded: %.4f" % (rate_sigma, rate_sigma2)) self.assertTrue(rate_low == rate_low2, msg="Original rate low: %.4f, Loaded: %.4f" % (rate_low, rate_low2)) self.assertTrue(rate_high == rate_high2, msg="Original rate high: %.4f, Loaded: %.4f" % (rate_high, rate_high2)) self.assertTrue(rate_bins == rate_bins2, msg="Original rate bins: %.4f, Loaded: %.4f" % (rate_bins, rate_bins2))
type=str, default="weight", help="specify the smearing method to use") parser.add_argument("-r", "--energy_resolution", default=None, type=float, help="specify energy resolution " "e.g. 0.05 for 5 percent") parser.add_argument("path", type=str, help="specify path to hdf5 file") args = parser.parse_args() directory = args.path[:args.path.rfind("/")+1] # strip filename # strip directory and extension filename = args.path[args.path.rfind("/")+1:args.path.rfind(".")] if args.energy_resolution is not None: smearer = smear.EResSmear(args.energy_resolution) else: # use light yield smearer = smear.Smear() spectrum = store.load(args.path) if args.smear_method == "weight": # Use default smear method smeared_spectrum = smearer.weight_gaussian_energy_spectra(spectrum) smeared_spectrum = smearer.weight_gaussian_radius_spectra(smeared_spectrum) elif args.smear_method == "random": smeared_spectrum = smearer.random_gaussian_energy_spectra(spectrum) smeared_spectrum = smearer.random_gaussian_radius_spectra(smeared_spectrum) else: # Not a valid smear method parser.error(args.smear_method + " is not a valid smear method") filename = directory + filename + "_smeared" + ".hdf5" store.dump(filename, smeared_spectrum)