def fit(self): dummy_data = np.zeros(1) dummy_times = np.arange(1) ui.load_arrays(1, dummy_times, dummy_data) ui.set_method(self.method) ui.get_method().config.update(sherpa_configs.get(self.method, {})) ui.load_user_model(CalcModel(self.model), 'xijamod') # sets global xijamod ui.add_user_pars('xijamod', self.model.parnames) ui.set_model(1, 'xijamod') calc_stat = CalcStat(self.model, self.child_pipe, self.maxiter) ui.load_user_stat('xijastat', calc_stat, lambda x: np.ones_like(x)) ui.set_stat(xijastat) # Set frozen, min, and max attributes for each xijamod parameter for par in self.model.pars: xijamod_par = getattr(xijamod, par.full_name) xijamod_par.val = par.val xijamod_par.frozen = par.frozen xijamod_par.min = par.min xijamod_par.max = par.max if any(not par.frozen for par in self.model.pars): try: ui.fit(1) calc_stat.message['status'] = 'finished' fit_logger.info('Fit finished normally') except FitTerminated as err: calc_stat.message['status'] = 'terminated' fit_logger.warning('Got FitTerminated exception {}'.format(err)) self.child_pipe.send(calc_stat.message)
def fit(self): dummy_data = np.zeros(1) dummy_times = np.arange(1) ui.load_arrays(1, dummy_times, dummy_data) ui.set_method(self.method) ui.get_method().config.update(sherpa_configs.get(self.method, {})) ui.load_user_model(CalcModel(self.model), 'xijamod') # sets global xijamod ui.add_user_pars('xijamod', self.model.parnames) ui.set_model(1, 'xijamod') calc_stat = CalcStat(self.model, self.child_pipe) ui.load_user_stat('xijastat', calc_stat, lambda x: np.ones_like(x)) ui.set_stat(xijastat) # Set frozen, min, and max attributes for each xijamod parameter for par in self.model.pars: xijamod_par = getattr(xijamod, par.full_name) xijamod_par.val = par.val xijamod_par.frozen = par.frozen xijamod_par.min = par.min xijamod_par.max = par.max if any(not par.frozen for par in self.model.pars): try: ui.fit(1) calc_stat.message['status'] = 'finished' logging.debug('Fit finished normally') except FitTerminated as err: calc_stat.message['status'] = 'terminated' logging.debug('Got FitTerminated exception {}'.format(err)) self.child_pipe.send(calc_stat.message)
def fit(self, method='simplex'): """Initiate a fit of the model using Sherpa. :param method: Method to be used to fit the model (e.g. simplex, levmar, or moncar) """ dummy_data = np.zeros(1) dummy_times = np.arange(1) ui.load_arrays(1, dummy_times, dummy_data) ui.set_method(method) ui.get_method().config.update(sherpa_configs.get(method, {})) ui.load_user_model(CalcModel(self.model, self.fit_logger), 'xijamod') # sets global xijamod ui.add_user_pars('xijamod', self.model.parnames) ui.set_model(1, 'xijamod') calc_stat = CalcStat(self.model, self.fit_logger) ui.load_user_stat('xijastat', calc_stat, lambda x: np.ones_like(x)) ui.set_stat(xijastat) # Set frozen, min, and max attributes for each xijamod parameter for par in self.model.pars: xijamod_par = getattr(xijamod, par.full_name) xijamod_par.val = par.val xijamod_par.frozen = par.frozen xijamod_par.min = par.min xijamod_par.max = par.max ui.fit(1) self.save_snapshot(fit_stat=calc_stat.min_fit_stat, method=method)
def fit_model( model, comm=None, method='simplex', config=None, nofit=None, freeze_pars=freeze_pars, thaw_pars=[], ): dummy_data = np.zeros(1) dummy_times = np.arange(1) ui.load_arrays(1, dummy_times, dummy_data) ui.set_method(method) ui.get_method().config.update(config or sherpa_configs.get(method, {})) ui.load_user_model(CalcModel(model, comm), 'xijamod') ui.add_user_pars('xijamod', model.parnames) ui.set_model(1, 'xijamod') fit_parnames = set() for parname, parval in zip(model.parnames, model.parvals): getattr(xijamod, parname).val = parval fit_parnames.add(parname) if any([re.match(x + '$', parname) for x in freeze_pars]): fit_logger.info('Freezing ' + parname) ui.freeze(getattr(xijamod, parname)) fit_parnames.remove(parname) if any([re.match(x + '$', parname) for x in thaw_pars]): fit_logger.info('Thawing ' + parname) ui.thaw(getattr(xijamod, parname)) fit_parnames.add(parname) if 'tau' in parname: getattr(xijamod, parname).min = 0.1 calc_stat = CalcStat(model, comm) ui.load_user_stat('xijastat', calc_stat, lambda x: np.ones_like(x)) ui.set_stat(xijastat) if fit_parnames and not nofit: ui.fit(1) else: model.calc()
def fit_model(model, comm=None, method='simplex', config=None, nofit=None, freeze_pars=freeze_pars, thaw_pars=[], ): dummy_data = np.zeros(1) dummy_times = np.arange(1) ui.load_arrays(1, dummy_times, dummy_data) ui.set_method(method) ui.get_method().config.update(config or sherpa_configs.get(method, {})) ui.load_user_model(CalcModel(model, comm), 'xijamod') ui.add_user_pars('xijamod', model.parnames) ui.set_model(1, 'xijamod') fit_parnames = set() for parname, parval in zip(model.parnames, model.parvals): getattr(xijamod, parname).val = parval fit_parnames.add(parname) if any([re.match(x + '$', parname) for x in freeze_pars]): fit_logger.info('Freezing ' + parname) ui.freeze(getattr(xijamod, parname)) fit_parnames.remove(parname) if any([re.match(x + '$', parname) for x in thaw_pars]): fit_logger.info('Thawing ' + parname) ui.thaw(getattr(xijamod, parname)) fit_parnames.add(parname) if 'tau' in parname: getattr(xijamod, parname).min = 0.1 calc_stat = CalcStat(model, comm) ui.load_user_stat('xijastat', calc_stat, lambda x: np.ones_like(x)) ui.set_stat(xijastat) if fit_parnames and not nofit: ui.fit(1) else: model.calc()
def mwl_fit_high_level(): """Use high-level Sherpa API. High-level = session and convenience functions Example: http://cxc.harvard.edu/sherpa/threads/simultaneous/ Example: http://python4astronomers.github.io/fitting/spectrum.html """ import sherpa.ui as ui fermi_data = FermiData() ui.load_arrays(fermi_data.name, fermi_data.x, fermi_data.y, fermi_data.staterror) ui.load_user_stat('fermi_stat', FermiStat.calc_stat, FermiStat.calc_staterror) # TODO: is there a good way to get the stat?? # ui.get_stat('fermi_stat') # fermi_stat = ui._session._get_stat_by_name('fermi_stat') ui.set_stat(fermi_stat) # IPython.embed() iact_data = IACTData() ui.load_arrays(iact_data.name, iact_data.x, iact_data.y, iact_data.staterror) spec_model = ui.logparabola.spec_model spec_model.c1 = 0.5 spec_model.c2 = 0.2 spec_model.ampl = 5e-11 ui.set_source(fermi_data.name, spec_model) ui.set_source(iact_data.name, spec_model) ui.notice(lo=1e-3, hi=None) # IPython.embed() ui.fit() return dict(results=ui.get_fit_results(), model=spec_model)
def mwl_fit_high_level(): """Use high-level Sherpa API. High-level = session and convenience functions Example: http://cxc.harvard.edu/sherpa/threads/simultaneous/ Example: http://python4astronomers.github.io/fitting/spectrum.html """ import sherpa.ui as ui fermi_data = FermiData() ui.load_arrays(fermi_data.name, fermi_data.x, fermi_data.y, fermi_data.staterror) ui.load_user_stat('fermi_stat', FermiStat.calc_stat, FermiStat.calc_staterror) # TODO: is there a good way to get the stat?? # ui.get_stat('fermi_stat') # fermi_stat = ui._session._get_stat_by_name('fermi_stat') ui.set_stat(fermi_stat) # IPython.embed() iact_data = IACTData() ui.load_arrays(iact_data.name, iact_data.x, iact_data.y, iact_data.staterror) spec_model = ui.logparabola.spec_model spec_model.c1 = 0.5 spec_model.c2 = 0.2 spec_model.ampl = 5e-11 ui.set_source(fermi_data.name, spec_model) ui.set_source(iact_data.name, spec_model) ui.notice(lo=1e-3, hi=None) # IPython.embed() ui.fit() return Bunch(results=ui.get_fit_results(), model=spec_model)
ui.set_model(data_id, '%s_mod' % ftype) ui.load_arrays(data_id, times, failures[ftype]) fmod = ui.get_model_component('%s_mod' % ftype) fmod.b.min = 0 fmod.b.max = 1 fmod.m.min = 0 fmod.m.max = 0.5 fmod.b.val=1e-7 ui.load_user_stat("loglike", llh, my_err) ui.set_stat(loglike) # the tricky part here is that the "model" is the probability polynomial # we've defined evaluated at the data x values. # the model and the data are passed to the user stat/ llh # function as it is minimized. ui.fit(data_id) myfit = ui.get_fit_results() #axplot[ftype] = ui.get_model_plot(data_id) if myfit.succeeded: import pickle pickle.dump(myfit, open('%s_fitfile.pkl' % ftype, 'w')) rep_file = open('%s_fitfile.json' % ftype, 'w') rep_file.write(json.dumps(dict(time0=trend_start, datestop=trend_date_stop,
ui.load_user_model(lim_line, '%s_mod' % ftype) ui.add_user_pars('%s_mod' % ftype, ['m', 'b']) ui.set_model(data_id, '%s_mod' % ftype) ui.load_arrays(data_id, times, failures[ftype]) fmod = ui.get_model_component('%s_mod' % ftype) fmod.b.min = 0 fmod.b.max = 1 fmod.m.min = 0 fmod.m.max = 0.5 fmod.b.val = 1e-7 ui.load_user_stat("loglike", llh, my_err) ui.set_stat(loglike) # the tricky part here is that the "model" is the probability polynomial # we've defined evaluated at the data x values. # the model and the data are passed to the user stat/ llh # function as it is minimized. ui.fit(data_id) myfit = ui.get_fit_results() #axplot[ftype] = ui.get_model_plot(data_id) if myfit.succeeded: import pickle pickle.dump(myfit, open('%s_fitfile.pkl' % ftype, 'w')) rep_file = open('%s_fitfile.json' % ftype, 'w') rep_file.write( json.dumps(dict(