def init_function_pars(): global FUNCTION_PAR_NAMES global FUNCTION_NORM_PARS global FUNCTION_DEFAULT_PARS FUNCTION_PAR_NAMES = {} FUNCTION_NORM_PARS = {} funcFactory = pyLike.SourceFactory_funcFactory() names = pyLike.StringVector() funcFactory.getFunctionNames(names) for fname in names: pars = FUNCTION_DEFAULT_PARS.setdefault(fname, {}) par_names = FUNCTION_PAR_NAMES.setdefault(fname, []) if 'EblAtten' in fname and fname[len('EblAtten::' ):] in FUNCTION_DEFAULT_PARS: pars.update(FUNCTION_DEFAULT_PARS[fname[len('EblAtten::'):]]) fn = funcFactory.create(fname) try: FUNCTION_NORM_PARS[fname] = fn.normPar().getName() except Exception: FUNCTION_NORM_PARS[fname] = None params = pyLike.ParameterVector() fn.getParams(params) for i, p in enumerate(params): pname = p.getName() par_names += [pname] if pname == 'Scale': pars.setdefault(pname, DEFAULT_SCALE_DICT) elif pname == 'Prefactor': pars.setdefault(pname, DEFAULT_NORM_DICT) else: pars.setdefault(pname, {}) bounds = p.getBounds() par_dict = dict(name=pname, value=p.getValue(), min=bounds[0], max=bounds[1], scale=1.0, free=False) par_dict.update(copy.deepcopy(pars[pname])) par_dict['name'] = pname pars[pname] = par_dict
def constrain_params(self, srcNames, cov_scale=1.0): for name in srcNames: free_pars = pyLike.ParameterVector() self.srcs[name].funcs["Spectrum"].getFreeParams(free_pars) for i in range(free_pars.size()): par = free_pars[i] err = par.error() val = par.getValue() if par.error() == 0.0 or not par.isFree(): continue self.addGaussianPrior(name, par.getName(), val, err * cov_scale)
def minosError(self, component, srcname, parname, level=1): freeParams = pyLike.ParameterVector() self.composite.getFreeParams(freeParams) saved_values = [par.getValue() for par in freeParams] indx = self._compositeIndex(component, srcname, parname) if indx == -1: raise RuntimeError("Invalid parameter specification.") try: errors = self.optObject.Minos(indx, level) self.composite.setFreeParamValues(saved_values) return errors except RuntimeError, message: print "Minos error encountered for parameter %i." % indx self.composite.setFreeParamValues(saved_values)
def gtlike_spectrum_to_dict(spectrum): """ Convert a pyLikelihood object to a python dictionary which can be easily saved to a file.""" parameters = pyLike.ParameterVector() spectrum.getParams(parameters) d = dict(spectrum_type=spectrum.genericName()) for p in parameters: pname = p.getName() pval = p.getTrueValue() perr = abs(p.error() * p.getScale()) if p.isFree() else np.nan d[pname] = np.array([pval, perr]) if d['spectrum_type'] == 'FileFunction': ff = pyLike.FileFunction_cast(spectrum) d['file'] = ff.filename() return d
def __init__(self, like): self.like = like self.sources = dict() all_names = like.sourceNames() self.covariance = like.covariance for name in all_names: spectrum = like[name].src.spectrum() parameters = pyLikelihood.ParameterVector() spectrum.getParams(parameters) self.sources[name] = d = dict(parameters=dict(), spectrum=spectrum) for p in parameters: d['parameters'][p.getName()] = _Parameter(p)
def gtlike_spectrum_to_vectors(spectrum): """ Convert a pyLikelihood object to a python dictionary which can be easily saved to a file.""" o = {'param_names': np.zeros(10, dtype='S32'), 'param_values': np.empty(10, dtype=float) * np.nan, 'param_errors': np.empty(10, dtype=float) * np.nan, } parameters = pyLike.ParameterVector() spectrum.getParams(parameters) for i, p in enumerate(parameters): o['param_names'][i] = p.getName() o['param_values'][i] = p.getTrueValue() perr = abs(p.error() * p.getScale()) if p.isFree() else np.nan o['param_errors'][i] = perr return o
def minosError(self, component_name, srcname, parname, level=1): freeParams = pyLike.ParameterVector() self.composite.getFreeParams(freeParams) saved_values = [par.getValue() for par in freeParams] indx = self._compositeIndex(component_name, srcname, parname) if indx == -1: raise RuntimeError("Invalid parameter specification") try: errors = self.optObject.Minos(indx, level) self.composite.setFreeParamValues(saved_values) return errors except RuntimeError, message: print "Minos error encountered for parameter %i." % par_index print "Attempting to reset free parameters." for tiedName, component in zip(self.srcNames, self.components): if component_name == tiedName: component.thaw(component.par_index(srcname, parname)) break self.composite.setFreeParamValues(saved_values) raise RuntimeError(message)