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_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_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
: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)
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(
observation, background, source_function
) # type: SpectrumLike
return generator.get_simulated_dataset(name)
# Save that
self._n_models = 1
# Wrap the function so that self._model_getter will return a list of one element
self._model_getter = lambda id: [model_getter(id)]
else:
# More than one model
# Check that all models are instances of Model
for this_model in model_or_models:
assert isinstance(
this_model, Model
), "The model getter function should return a model or a list of models"
# No need for a wrapper in this case
self._model_getter = model_getter
# save the number of models
self._n_models = n_models
# Set up some attributes we will need
self._n_iterations = n_iterations
# This is used only to print error messages
self._iteration_name = iteration_name
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
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
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