def _get_site_registry(cls, force_download=False, force_builtin=False): """ Gets the site registry. The first time this either downloads or loads from the data file packaged with astropy. Subsequent calls will use the cached version unless explicitly overridden. Parameters ---------- force_download : bool or str If not False, force replacement of the cached registry with a downloaded version. If a str, that will be used as the URL to download from (if just True, the default URL will be used). force_builtin : bool If True, load from the data file bundled with astropy and set the cache to that. returns ------- reg : astropy.coordinates.sites.SiteRegistry """ # need to do this here at the bottom to avoid circular dependencies from .sites import get_builtin_sites, get_downloaded_sites if force_builtin and force_download: raise ValueError( 'Cannot have both force_builtin and force_download True') if force_builtin: reg = cls._site_registry = get_builtin_sites() else: reg = getattr(cls, '_site_registry', None) if force_download or not reg: try: if isinstance(force_download, str): reg = get_downloaded_sites(force_download) else: reg = get_downloaded_sites() except OSError: if force_download: raise msg = ('Could not access the online site list. Falling ' 'back on the built-in version, which is rather ' 'limited. If you want to retry the download, do ' '{0}._get_site_registry(force_download=True)') warn(AstropyUserWarning(msg.format(cls.__name__))) reg = get_builtin_sites() cls._site_registry = reg return reg
def __init__(self, fitter, sampler='mh', walker='mh'): self._mcmc = MCMC() if hasattr(fitter.fit_info, "statval"): self._fitter = fitter._fitter if not hasattr(fitter.error_info, "extra_output"): fitter.est_errors() self._cmatrix = fitter.error_info.extra_output pars = fitter._fitmodel.sherpa_model.pars self.parameter_map = OrderedDict(map(lambda x: (x.name, x), pars)) else: raise AstropyUserWarning("Must have valid fit! " "Covariance matrix is not present")
def _astropy_to_sherpa_model(model): """ Converts the model using sherpa's usermodel suppling the parameter detail to sherpa then using a decorator to allow the call method to act like the calc method """ def _calc2call(func): """This decorator makes call and calc work together.""" def _converter(inp, *x): if func.n_inputs == 1: retvals = func.evaluate(x[0], *inp) else: retvals = func.evaluate(x[0], x[1], *inp) return retvals return _converter if len(model.ineqcons) > 0 or len(model.eqcons) > 0: AstropyUserWarning('In/eqcons are not supported by sherpa these will be ignored!') pars = [] linkedpars = [] for pname in model.param_names: param = getattr(model, pname) vals = [param.name, param.value, param.min, param.max, param.min, param.max, None, param.fixed, False] attrnames = ["name", "val", "min", "max", "hard_min", "hard_max", "units", "frozen", "alwaysfrozen"] if model.name is None: model._name = "" pars.append(Parameter(modelname="wrap_" + model.name, **dict([(atr, val) for atr, val in zip(attrnames, vals) if val is not None]))) if param.tied is not False: linkedpars.append(pname) smodel = UserModel(model.name, pars) smodel.calc = _calc2call(model) for pname in linkedpars: param = getattr(model, pname) sparam = getattr(smodel, pname) sparam.link = param.tied(smodel) return smodel
def set_prior(self, parameter, prior): """ Set the prior function to use with a parameter. The default prior used by the `SherpaMCMC` function call for each parameter is flat, varying between the minimum and maximum values of the parameter (as given by the ``min`` and ``max`` attributes of the parameter object). Parameters ---------- par : sherpa.models.parameter.Parameter instance A parameter of a model instance. prior : function or `sherpa.models.model.Model` instance The function to use for a prior. It must accept a single argument and return a value of the same size as the input. Examples -------- Create a function (``lognorm``) and use it as the prior the ``nH`` parameter >> def lognorm(x): sigma = 0.5 x0 = 20 dx = np.log10(x) - x0 norm = sigma / np.sqrt(2 * np.pi) return norm * np.exp(-0.5*dx*dx/(sigma*sigma)) >> mcmc.set_prior('nH', lognorm) """ if parameter in self.parameter_map: self._mcmc.set_prior(self.parameter_map[parameter], prior) else: raise AstropyUserWarning("Parmater {name} not found in parameter" "map".format(name=parameter))