def get_models(self, id): # Make a copy of the best fit models, so that we don't touch the original models during the fit, and we # also always restart from the best fit (instead of the last iteration) new_model0 = clone_model(self._joint_likelihood_instance0.likelihood_model) new_model1 = clone_model(self._joint_likelihood_instance1.likelihood_model) return new_model0, new_model1
def get_models(self, id): # Make a copy of the best fit models, so that we don't touch the original models during the fit, and we # also always restart from the best fit (instead of the last iteration) new_model0 = clone_model( self._joint_likelihood_instance0.likelihood_model) new_model1 = clone_model( self._joint_likelihood_instance1.likelihood_model) return new_model0, new_model1
def compute_TS(self, source_name, alt_hyp_mlike_df): """ Computes the Likelihood Ratio Test statistic (TS) for the provided source :param source_name: name for the source :param alt_hyp_mlike_df: likelihood dataframe (it is the second output of the .fit() method) :return: a DataFrame containing the null hypothesis and the alternative hypothesis -log(likelihood) values and the value for TS for the source for each loaded dataset """ assert source_name in self._likelihood_model, ( "Source %s is not in the current model" % source_name) # Clone model model_clone = clone_model(self._likelihood_model) # Remove this source from the model _ = model_clone.remove_source(source_name) # Fit another_jl = JointLikelihood(model_clone, self._data_list) # Use the same minimizer as the parent object another_jl.set_minimizer(self.minimizer_in_use) # We do not need the covariance matrix, just the likelihood value _, null_hyp_mlike_df = another_jl.fit(quiet=True, compute_covariance=False, n_samples=1) # Compute TS for all datasets TSs = [] alt_hyp_mlikes = [] null_hyp_mlikes = [] for dataset in list(self._data_list.values()): this_name = dataset.name null_hyp_mlike = null_hyp_mlike_df.loc[this_name, "-log(likelihood)"] alt_hyp_mlike = alt_hyp_mlike_df.loc[this_name, "-log(likelihood)"] this_TS = 2 * (null_hyp_mlike - alt_hyp_mlike) TSs.append(this_TS) alt_hyp_mlikes.append(alt_hyp_mlike) null_hyp_mlikes.append(null_hyp_mlike) TS_df = pd.DataFrame(index=list(self._data_list.keys())) TS_df["Null hyp."] = null_hyp_mlikes TS_df["Alt. hyp."] = alt_hyp_mlikes TS_df["TS"] = TSs # Reassign the original likelihood model to the datasets self._assign_model_to_data(self._likelihood_model) return TS_df
def optimized_model(self): """ Returns a copy of the optimized model :return: a copy of the optimized model """ return astromodels.clone_model(self._optimized_model)
def get_model(self, id): # Make a copy of the best fit model, so that we don't touch the original model during the fit, and we # also always restart from the best fit (instead of the last iteration) new_model = clone_model(self._best_fit_model) return new_model
def get_model(self, id): # Make a copy of the best fit model, so that we don't touch the original model during the fit, and we # also always restart from the best fit (instead of the last iteration) new_model = clone_model(self._best_fit_model) return new_model
def test_input_output(): tm = TemplateModel('__test') tm.alpha = -0.95 tm.beta = -2.23 fake_source = PointSource("test", ra=0.0, dec=0.0, spectral_shape=tm) fake_model = Model(fake_source) clone = clone_model(fake_model) assert clone.get_number_of_point_sources() == 1 assert tm.data_file == clone.test.spectrum.main.shape.data_file assert clone.test.spectrum.main.shape.alpha.value == tm.alpha.value assert clone.test.spectrum.main.shape.beta.value == tm.beta.value xx = np.linspace(1, 10, 100) assert np.allclose(clone.test.spectrum.main.shape(xx), fake_model.test.spectrum.main.shape(xx)) # Test pickling dump = pickle.dumps(clone) clone2 = pickle.loads(dump) assert clone2.get_number_of_point_sources() == 1 assert tm.data_file == clone2.test.spectrum.main.shape.data_file assert np.allclose(clone2.test.spectrum.main.shape(xx), fake_model.test.spectrum.main.shape(xx)) # Test pickling with other functions new_shape = tm * Powerlaw() new_shape.index_2 = -2.256 dump2 = pickle.dumps(new_shape) clone3 = pickle.loads(dump2) assert clone3.index_2.value == new_shape.index_2.value # Now save to disk and reload fake_source2 = PointSource("test", ra=0.0, dec=0.0, spectral_shape=new_shape) fake_model2 = Model(fake_source2) fake_model2.save("__test.yml", overwrite=True) # Now try to reload reloaded_model = load_model("__test.yml") assert reloaded_model.get_number_of_point_sources() == 1 assert np.allclose(fake_model2.test.spectrum.main.shape(xx), reloaded_model.test.spectrum.main.shape(xx)) os.remove("__test.yml")
def compute_TS(self, source_name, alt_hyp_mlike_df): """ Computes the Likelihood Ratio Test statistic (TS) for the provided source :param source_name: name for the source :param alt_hyp_mlike_df: likelihood dataframe (it is the second output of the .fit() method) :return: a DataFrame containing the null hypothesis and the alternative hypothesis -log(likelihood) values and the value for TS for the source for each loaded dataset """ assert source_name in self._likelihood_model, "Source %s is not in the current model" % source_name # Clone model model_clone = clone_model(self._likelihood_model) # Remove this source from the model _ = model_clone.remove_source(source_name) # Fit another_jl = JointLikelihood(model_clone, self._data_list) # Use the same minimizer as the parent object another_jl.set_minimizer(self.minimizer_in_use) # We do not need the covariance matrix, just the likelihood value _, null_hyp_mlike_df = another_jl.fit(quiet=True, compute_covariance=False, n_samples=1) # Compute TS for all datasets TSs = [] alt_hyp_mlikes = [] null_hyp_mlikes = [] for dataset in self._data_list.values(): this_name = dataset.name null_hyp_mlike = null_hyp_mlike_df.loc[this_name, '-log(likelihood)'] alt_hyp_mlike = alt_hyp_mlike_df.loc[this_name, '-log(likelihood)'] this_TS = 2 * (null_hyp_mlike - alt_hyp_mlike) TSs.append(this_TS) alt_hyp_mlikes.append(alt_hyp_mlike) null_hyp_mlikes.append(null_hyp_mlike) TS_df = pd.DataFrame(index=self._data_list.keys()) TS_df['Null hyp.'] = null_hyp_mlikes TS_df['Alt. hyp.'] = alt_hyp_mlikes TS_df['TS'] = TSs # Reassign the original likelihood model to the datasets self._assign_model_to_data(self._likelihood_model) return TS_df
def do_contours_check(jl, minimizer): #make sure that model is restored after contour calculation jl.set_minimizer(minimizer) _ = jl.fit() model_clone = clone_model(jl._likelihood_model) _ = jl.get_contours( jl._likelihood_model.bn090217206.spectrum.main.Powerlaw.index, -3.5, -0.5, 30) for param in jl._likelihood_model.parameters: assert jl._likelihood_model.parameters[param].value == model_clone[ param].value
def __init__(self, joint_likelihood_instance, like_data_frame=None): self._jl_instance = joint_likelihood_instance # Make sure we have a fit assert self._jl_instance.results is not None, "You have to perform a fit before using GoodnessOfFit" if like_data_frame is None: like_data_frame = self._jl_instance.results.get_statistic_frame() # Restore best fit and store the reference value for the likelihood self._jl_instance.restore_best_fit() self._reference_like = like_data_frame['-log(likelihood)'] # Store best model self._best_fit_model = clone_model(self._jl_instance.likelihood_model)
def __init__(self, joint_likelihood_instance, like_data_frame=None): self._jl_instance = joint_likelihood_instance # Make sure we have a fit assert self._jl_instance.results is not None, "You have to perform a fit before using GoodnessOfFit" if like_data_frame is None: like_data_frame = self._jl_instance.results.get_statistic_frame() # Restore best fit and store the reference value for the likelihood self._jl_instance.restore_best_fit() self._reference_like = like_data_frame['-log(likelihood)'] # Store best model self._best_fit_model = clone_model(self._jl_instance.likelihood_model)
def test_xspec_saving(): s = XS_powerlaw() + XS_bbody() ps = PointSource("test", 0, 0, spectral_shape=s) model = Model(ps) cloned_model = clone_model(model) filename = "_test_xspec_model.yml" model.save(filename) reloaded_model = load_model(filename) p = Path(filename) p.unlink()
def __init__(self, optimized_model, samples, statistic_values, analysis_type, statistical_measures): # Safety checks self._n_free_parameters = len(optimized_model.free_parameters) assert samples.shape[1] == self._n_free_parameters, "Number of free parameters (%s) and set of samples (%s) " \ "do not agree." % (samples.shape[1], self._n_free_parameters) # NOTE: we clone the model so that whatever happens outside or after, this copy of the model will not be # changed self._optimized_model = astromodels.clone_model(optimized_model) # Save a transposed version of the samples for easier access self._samples_transposed = samples.T # Store likelihood values in a pandas Series self._optimal_statistic_values = pd.Series(statistic_values) # Store the statistical measures as a pandas Series self._statistical_measures = pd.Series(statistical_measures) # The .free_parameters property of the model is pretty costly because it needs to update all the parameters # to see if they are free. Since the saved model will not be touched we can cache that self._free_parameters = self._optimized_model.free_parameters # Gather also the optimized values of the parameters self._values = np.array( map(lambda x: x.value, self._free_parameters.values())) # Set the analysis type self._analysis_type = analysis_type
def compute_ppc(analysis: BayesianAnalysis, result: BayesianResults, n_sims: int, file_name: str, overwrite: bool = False, return_ppc: bool = False) -> Union["PPC", None]: """ Compute a posterior predictive check from a 3ML DispersionLike Plugin. The resulting posterior data simulations are stored in an HDF5 file which can be read by the PPC class :param analysis: 3ML bayesian analysis object :param result: 3ML analysis result :param n_sims: the number of posterior simulations to create :param file_name: the filename to save to :param overwrite: to overwrite an existsing file :param return_ppc: if true, PPC object will be return directy :returns: None :rtype: """ update_logging_level("WARNING") p = Path(file_name) if p.exists() and (not overwrite): raise RuntimeError(f"{file_name} already exists!") with h5py.File(file_name, 'w', libver='latest') as database: # first we collect the real data data and save it so that we will not have to # look it up in the future data_names = [] database.attrs['n_sims'] = n_sims for data in analysis.data_list.values(): data_names.append(data.name) grp = database.create_group(data.name) grp.attrs['exposure'] = data.exposure grp.create_dataset('ebounds', data=data.response.ebounds, compression='lzf') grp.create_dataset('obs_counts', data=data.observed_counts, compression='lzf') grp.create_dataset('bkg_counts', data=data.background_counts, compression='lzf') grp.create_dataset('mask', data=data.mask, compression='lzf') # select random draws from the posterior n_samples = len(result.samples.T) if n_samples < n_sims: print("too many sims") n_sims = n_samples choices = np.random.choice(len(result.samples.T), replace=False, size=n_sims) # for each posterior sample with silence_console_log(and_progress_bars=False): for j, choice in enumerate(tqdm(choices, desc="sampling posterior")): # get the parameters of the choice params = result.samples.T[choice] # set the analysis free parameters to the value of the posterior for i, (k, v) in enumerate( analysis.likelihood_model.free_parameters.items()): v.value = params[i] # create simulated data sets with these free parameters sim_dl = DataList(*[ data.get_simulated_dataset() for data in analysis.data_list.values() ]) # set the model of the simulated data to the model of the simulation for i, data in enumerate(sim_dl.values()): # clone the model for saftey's sake # and set the model. For now we do nothing with this data.set_model(clone_model(analysis.likelihood_model)) # store the PPC data in the file grp = database[data_names[i]] grp.create_dataset('ppc_counts_%d' % j, data=data.observed_counts, compression='lzf') grp.create_dataset('ppc_background_counts_%d' % j, data=data.background_counts, compression='lzf') # sim_dls.append(sim_dl) if return_ppc: return PPC(file_name)