def test_ui_source_methods_with_full_model(clean_ui, setup_ui_full): ui.load_data('full', setup_ui_full.ascii) ui.set_full_model('full', 'powlaw1d.p1') # Test Case 1 with pytest.raises(IdentifierErr) as exc: ui.get_source('full') emsg = "Convolved model\n'powlaw1d.p1'\n is set for dataset full. You should use get_model instead." assert str(exc.value) == emsg with pytest.raises(IdentifierErr) as exc: ui.plot_source('full') emsg = "Convolved model\n'powlaw1d.p1'\n is set for dataset full. You should use plot_model instead." assert str(exc.value) == emsg with pytest.raises(IdentifierErr) as exc: ui.get_source_plot('full') emsg = "Convolved model\n'powlaw1d.p1'\n is set for dataset full. You should use get_model_plot instead." assert str(exc.value) == emsg # Test Case 2 ui.set_source('full', 'powlaw1d.p2') ui.get_source('full') # Test Case 3 ui.load_data('not_full', setup_ui_full.ascii) with pytest.raises(IdentifierErr) as exc: ui.get_source('not_full') emsg = 'source not_full has not been set, consider using set_source() or set_model()' assert emsg == str(exc.value)
def test_source_methods_with_full_model(self): from sherpa.utils.err import IdentifierErr ui.load_data('full', self.ascii) ui.set_full_model('full', 'powlaw1d.p1') # Test Case 1 try: ui.get_source('full') except IdentifierErr as e: self.assertRegex( str(e), "Convolved model\n.*\n is set for dataset full. You should use get_model instead.", str(e)) try: ui.plot_source('full') except IdentifierErr as e: self.assertRegex( str(e), "Convolved model\n.*\n is set for dataset full. You should use plot_model instead.", str(e)) # Test Case 2 ui.set_source('full', 'powlaw1d.p2') ui.get_source('full') # Test Case 3 ui.load_data('not_full', self.ascii) try: ui.get_source('not_full') except IdentifierErr as e: self.assertEqual( 'source not_full has not been set, consider using set_source() or set_model()', str(e))
def test_source_methods_with_full_model(self): from sherpa.utils.err import IdentifierErr ui.load_data('full', self.ascii) ui.set_full_model('full', 'powlaw1d.p1') # Test Case 1 try: ui.get_source('full') except IdentifierErr as e: self.assertRegexpMatches(str(e), "Convolved model\n.*\n is set for dataset full. You should use get_model instead.", str(e)) try: ui.plot_source('full') except IdentifierErr as e: self.assertEquals("Convolved model\n'p1'\n is set for dataset full. You should use plot_model instead.", str(e)) # Test Case 2 ui.set_source('full', 'powlaw1d.p2') ui.get_source('full') # Test Case 3 ui.load_data('not_full', self.ascii) try: ui.get_source('not_full') except IdentifierErr as e: self.assertEquals('source not_full has not been set, consider using set_source() or set_model()', str(e))
def setUp(self): self._old_logger_level = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) ui.clean() self.ascii = self.make_path('sim.poisson.1.dat') self.wrong_stat_msg = "Fit statistic must be cash, cstat or wstat, not {}" self.wstat_err_msg = "No background data has been supplied. Use cstat" self.no_covar_msg = "covariance has not been performed" self.fail_msg = "Call should not have succeeded" self.right_stats = {'cash', 'cstat', 'wstat'} self.model = PowLaw1D("p1") ui.load_data(self.ascii) ui.set_model(self.model)
def test_ascii(self): ui.load_data(1, self.ascii) ui.load_data(1, self.ascii, 2) ui.load_data(1, self.ascii, 2, ("col2", "col1"))
# coding: utf-8 import sherpa.ui as ui ui.load_data("default_interp", "load_template_with_interpolation-bb_data.dat") ui.load_template_model('bb1', "bb_index.dat") ui.load_template_model('bb2', "bb_index.dat") ui.set_model("default_interp", bb1+bb2) ui.freeze("bb1.dummy") ui.freeze("bb2.dummy") ui.fit("default_interp")
def test_reading_strings(self): ui.load_data(self.template_idx, require_floats=False)
# coding: utf-8 import sherpa.ui as ui from sherpa.models.template import KNNInterpolator ui.load_data("custom_interp", "load_template_interpolator-bb_data.dat") ui.load_template_interpolator('knn', KNNInterpolator, k=2, order=1) ui.load_template_model('bb1', "bb_index.dat", template_interpolator_name='knn') ui.set_model("custom_interp", "bb1") ui.freeze("bb1.dummy") ui.fit("custom_interp")
def setup_covar(make_data_path): print("A") ui.load_data(make_data_path('sim.poisson.1.dat')) ui.set_model(PowLaw1D("p1"))
def test_ui_ascii(clean_ui, setup_ui_full): ui.load_data(1, setup_ui_full.ascii) ui.load_data(1, setup_ui_full.ascii, 2) ui.load_data(1, setup_ui_full.ascii, 2, ("col2", "col1"))
def test_not_reading_header_without_comment(make_data_path): with pytest.raises(ValueError): ui.load_data(make_data_path('agn2'))
# coding: utf-8 import sherpa.ui as ui ui.load_data("load_template_without_interpolation-bb_data.dat") ui.load_template_model('bb1', "bb_index.dat", template_interpolator_name=None) ui.set_source('bb1') assert ui.get_source().is_discrete ui.set_source('bb1*const1d.c1+gauss1d.g2**const1d.c2') assert ui.get_source().is_discrete
# coding: utf-8 import sherpa.ui as ui ui.load_data("bb_data.dat") ui.load_template_model('bb1', "bb_index.dat", template_interpolator_name=None) ui.set_method('gridsearch') ui.set_method_opt('sequence', [[2234, 0],[3512,0]]) ui.set_source('bb1') ui.fit()
# coding: utf-8 import sherpa.ui as ui ui.load_data("default_interp", "bb_data.dat") ui.load_template_model('bb1', "bb_index.dat") ui.load_template_model('bb2', "bb_index.dat") ui.set_model("default_interp", bb1+bb2) ui.freeze("bb1.dummy") ui.freeze("bb2.dummy") ui.fit("default_interp")
def test_require_float(make_data_path): with pytest.raises(ValueError): ui.load_data(make_data_path('agn2'))
def test_reading_floats(make_data_path): ui.load_data(make_data_path('agn2_fixed'))
def test_reading_strings(make_data_path): ui.load_data(make_data_path('table.txt'), require_floats=False)
# coding: utf-8 import sherpa.ui as ui from sherpa.models.template import KNNInterpolator ui.load_data("custom_interp", "bb_data.dat") ui.load_template_interpolator('knn', KNNInterpolator, k=2, order=1) ui.load_template_model('bb1', "bb_index.dat", template_interpolator_name='knn') ui.set_model("custom_interp", "bb1") ui.freeze("bb1.dummy") ui.fit("custom_interp")
def test_reading_floats(self): ui.load_data(self.agn2_fixed)
# coding: utf-8 import sherpa.ui as ui ui.load_data("default_interp", "load_template_with_interpolation-bb_data.dat") ui.load_template_model('bb1', "bb_index.dat") ui.set_model("default_interp", bb1) ui.set_method('gridsearch') ui.set_method_opt('sequence', ui.get_model_component('bb1').parvals) ui.fit("default_interp")
def fit_evol(dateglob='20?????', rootdir='darkhist_peaknorm', outroot='', xmin=25.0, xmax=4000, conf=True, gauss=False): results = {} fileglob = os.path.join(rootdir, '{}.dat'.format(dateglob)) for i, filename in enumerate(glob(fileglob)): filedate = re.search(r'(\d{7})', filename).group(1) print "\n\n*************** {} *****************".format(filename) plt.figure(1) ui.load_data(1, filename, 2) data = ui.get_data() ui.ignore(None, xmin) ui.ignore(xmax, None) dark_models.xall = data.x # dark_models.imin = np.where(xall > xmin)[0][0] # dark_models.imax = np.where(xall > xmax)[0][0] sbp.gamma1 = 0.05 sbp.gamma2 = 3.15 sbp.gamma2.min = 2. sbp.gamma2.max = 4. sbp.x_b = 130. sbp.x_b.min = 100. sbp.x_b.max = 160. sbp.x_r = 50. ok = (data.x > 40) & (data.x < 60) sbp.ampl1 = np.mean(data.y[ok]) if gauss: fit_gauss_sbp() else: fit_sbp() pars = (sbp.gamma1.val, sbp.gamma2.val, sbp.x_b.val, sbp.x_r.val, sbp.ampl1.val) fit_y = dark_models.smooth_broken_pow(pars, data.x) if conf: ui.set_conf_opt('numcores', 1) ui.conf() res = ui.get_conf_results() result = dict((x, getattr(res, x)) for x in ('parnames', 'parmins', 'parvals', 'parmaxes')) result['x'] = data.x result['y'] = data.y result['y_fit'] = fit_y results[filedate] = result if outroot is not None: ui.notice(0, xmax) ui.set_xlog() ui.set_ylog() ui.plot_fit() plt.xlim(1, 1e4) plt.ylim(0.5, 1e5) plt.grid(True) plt.xlabel('Dark current (e-/sec)') outfile = os.path.join(rootdir, '{}{}.png'.format(outroot, filedate)) print 'Writing', outfile plt.savefig(outfile) if conf: outfile = os.path.join(rootdir, '{}{}.pkl'.format(outroot, filedate)) print 'Writing', outfile pickle.dump(result, open(outfile, 'w'), protocol=-1) if outroot is not None: outfile = os.path.join(rootdir, '{}fits.pkl'.format(outroot)) print 'Writing', outfile pickle.dump(results, open(outfile, 'w'), protocol=-1) return results