Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fit(self, function, minimizer="minuit", verbose=False):
"""
Fit the data with the provided function (an astromodels function)
:param function: astromodels function
:param minimizer: the minimizer to use
:param verbose: print every step of the fit procedure
:return: best fit results
"""
# This is a wrapper to give an easier way to fit simple data without having to go through the definition
# of sources
pts = PointSource("source", 0.0, 0.0, function)
model = Model(pts)
self.set_model(model)
self._joint_like_obj = JointLikelihood(model, DataList(self), verbose=verbose)
self._joint_like_obj.set_minimizer(minimizer)
return self._joint_like_obj.fit()
def get_bayesian_analysis_object_simple_likelihood():
minus_log_L = Simple()
minus_log_L.mu.set_uninformative_prior(Log_uniform_prior)
# Instance a plugin (in this case a special one for illustrative purposes)
plugin = CustomLikelihoodLike("custom")
# Set the log likelihood function explicitly. This is not needed for any other
# plugin
plugin.set_minus_log_likelihood(minus_log_L)
# Make the data list (in this case just one dataset)
data = DataList(plugin)
src = PointSource("test", ra=0.0, dec=0.0, spectral_shape=minus_log_L)
model = Model(src)
bayes = BayesianAnalysisWrap(model, data, verbose=False)
return bayes, model
"""
Generate an XYLike plugin from an astromodels function instance
:param name: name of plugin
:param function: astromodels function instance
:param x: where to simulate
:param yerr: y errors or None for Poisson data
:param kwargs: kwargs from xylike constructor
:return: XYLike plugin
"""
y = function(x)
xyl_gen = XYLike("generator", x, y, yerr, **kwargs)
pts = PointSource("fake", 0.0, 0.0, function)
model = Model(pts)
xyl_gen.set_model(model)
return xyl_gen.get_simulated_dataset(name)
def get_joint_likelihood_object_complex_likelihood():
minus_log_L = Complex()
# Instance a plugin (in this case a special one for illustrative purposes)
plugin = CustomLikelihoodLike("custom")
# Set the log likelihood function explicitly. This is not needed for any other
# plugin
plugin.set_minus_log_likelihood(minus_log_L)
# Make the data list (in this case just one dataset)
data = DataList(plugin)
src = PointSource("test", ra=0.0, dec=0.0, spectral_shape=minus_log_L)
model = Model(src)
jl = JointLikelihoodWrap(model, data, verbose=False)
return jl, model
def get_joint_likelihood_object_simple_likelihood():
minus_log_L = Simple()
# Instance a plugin (in this case a special one for illustrative purposes)
plugin = CustomLikelihoodLike("custom")
# Set the log likelihood function explicitly. This is not needed for any other
# plugin
plugin.set_minus_log_likelihood(minus_log_L)
# Make the data list (in this case just one dataset)
data = DataList(plugin)
src = PointSource("test", ra=0.0, dec=0.0, spectral_shape=minus_log_L)
model = Model(src)
jl = JointLikelihoodWrap(model, data, verbose=False)
return jl, model
is_poisson=is_poisson,
mission="fake_mission",
instrument="fake_instrument",
tstart=0.0,
tstop=1.0,
)
# now we have to generate the background counts
# we treat the background as a simple observation with no
# other background
background_gen = SpectrumLike(
"generator", tmp_background, None, verbose=False
)
pts_background = PointSource(
"fake_background", 0.0, 0.0, background_function
)
background_model = Model(pts_background)
background_gen.set_model(background_model)
sim_background = background_gen.get_simulated_dataset("fake")
background = sim_background._observed_spectrum
else:
background = None
generator = cls._get_synthetic_plugin(
def _get_synthetic_plugin(cls, observation, background, source_function):
speclike_gen = cls("generator", observation, background, verbose=False)
pts = PointSource("fake", 0.0, 0.0, source_function)
model = Model(pts)
speclike_gen.set_model(model)
return speclike_gen
def get_bayesian_analysis_object_complex_likelihood():
minus_log_L = Complex()
minus_log_L.mu.set_uninformative_prior(Log_uniform_prior)
# Instance a plugin (in this case a special one for illustrative purposes)
plugin = CustomLikelihoodLike("custom")
# Set the log likelihood function explicitly. This is not needed for any other
# plugin
plugin.set_minus_log_likelihood(minus_log_L)
# Make the data list (in this case just one dataset)
data = DataList(plugin)
src = PointSource("test", ra=0.0, dec=0.0, spectral_shape=minus_log_L)
model = Model(src)
bayes = BayesianAnalysisWrap(model, data, verbose=False)
return bayes, model