class test_more_ui(SherpaTestCase): def assign_model(self, name, obj): self.locals[name] = obj def run_thread(self, name, scriptname='fit.py'): ui.clean() ui.set_model_autoassign_func(self.assign_model) self.locals = {} cwd = os.getcwd() os.chdir(self.make_path('ciao4.3', name)) try: execfile(scriptname, {}, self.locals) finally: os.chdir(cwd) @unittest.skipIf(test_data_missing(), "required test data missing") def setUp(self): self.img = self.make_path('img.fits') self.pha = self.make_path('threads/simultaneous/pi2286.fits') self.rmf = self.make_path('threads/simultaneous/rmf2286.fits') self.nan = self.make_path('ciao4.3/filternan/with_nan.fits') self.loggingLevel = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) def tearDown(self): if hasattr(self, 'loggingLevel'): logger.setLevel(self.loggingLevel) # bug 12784 @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_filter_nan(self): self.run_thread('filternan') self.assertFalse(numpy.isnan(ui.get_fit_results().statval))
class test_more_ui(SherpaTestCase): def assign_model(self, name, obj): self.locals[name] = obj def run_thread(self, name, scriptname='fit.py'): ui.clean() ui.set_model_autoassign_func(self.assign_model) self.locals = {} os.chdir(os.path.join(self.datadir, 'ciao4.3', name)) execfile(scriptname, {}, self.locals) @unittest.skipIf(test_data_missing(), "required test data missing") def setUp(self): self.img = self.datadir + '/img.fits' self.pha = self.datadir + '/threads/simultaneous/pi2286.fits' self.rmf = self.datadir + '/threads/simultaneous/rmf2286.fits' self.nan = self.datadir + '/ciao4.3/filternan/with_nan.fits' logger.setLevel(logging.ERROR) # bug 12784 @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_filter_nan(self): self.run_thread('filternan') self.assertFalse(numpy.isnan(ui.get_fit_results().statval))
class test_more_ui(SherpaTestCase): @unittest.skipIf(test_data_missing(), "required test data missing") def setUp(self): self._old_logger_level = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) self.img = self.datadir + '/img.fits' self.pha = self.datadir + '/threads/simultaneous/pi2286.fits' self.rmf = self.datadir + '/threads/simultaneous/rmf2286.fits' self.pha3c273 = self.datadir + '/ciao4.3/pha_intro/3c273.pi' logger.setLevel(logging.ERROR) def tearDown(self): logger.setLevel(self._old_logger_level) #bug #12732 @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_string_model_with_rmf(self): ui.load_pha("foo", self.pha) ui.load_rmf("foo", self.rmf) # Check that get_rmf(id)('modelexpression') works caught = False try: m=ui.get_rmf("foo")("powlaw1d.pl1") except: caught = True if caught: self.fail("Exception caught when it shouldn't") from sherpa.astro.instrument import RMFModelPHA self.assertTrue(isinstance(m, RMFModelPHA)) #bug #38 @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_bug38(self): ui.load_pha('3c273', self.pha3c273) ui.notice_id('3c273', 0.3, 2) ui.group_counts('3c273', 30) ui.group_counts('3c273', 15)
class test_image_12578(SherpaTestCase): @unittest.skipIf(test_data_missing(), "required test data missing") def setUp(self): self.img = self.make_path('img.fits') self.loggingLevel = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) ui.clean() def tearDown(self): if hasattr(self, 'loggingLevel'): logger.setLevel(self.loggingLevel) # bug #12578 @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_set_coord_bad_coord(self): from sherpa.utils.err import IdentifierErr, DataErr # Test Case #1: if the list of ids is empty, raise # IdentifierErr['nodatasets'] caught = False try: ui.set_coord('image') except IdentifierErr: caught = True if not caught: self.fail("Test Case #1: IdentifierErr Exception not caught") # Test Case #2: check the user expected behavior. The call # set_coord("sky") will result in the error message # DataErr: unknown coordinates: 'sky'\n \ # Valid coordinates: logical, image, physical, world, wcs ui.load_image(self.img) caught = False try: ui.set_coord("sky") except DataErr as e: okmsg = "unknown coordinates: 'sky'\nValid options: logical, image, physical, world, wcs" self.assertEqual(okmsg, e.message) caught = True if not caught: self.fail("Test Case #2: DataErr Exception not caught")
class test_query(SherpaTestCase): def setUp(self): datastack.clear_stack() ui.clean() datastack.set_template_id("__ID") self._this_dir = os.path.dirname(sys.modules[self.__module__].__file__) self.loggingLevel = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) def tearDown(self): datastack.clear_stack() ui.clean() datastack.set_template_id("__ID") datastack.set_stack_verbose(False) logger.setLevel(self.loggingLevel) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') def test_case_7(self): datastack.load_pha('@' + '/'.join((self._this_dir, 'data', 'pha.lis'))) f = datastack.query_by_header_keyword('INSTRUME', 'ACIS') assert f == [1, 2] f = datastack.query_by_obsid('7867') assert f == [2] ds = datastack.DataStack() ds.load_pha('@' + '/'.join((self._this_dir, 'data', 'pha.lis'))) f = ds.query_by_obsid('4938') assert f == [3] f = datastack.query_by_obsid(ds, '7867') assert f == [4]
class test_ui(SherpaTestCase): @unittest.skipIf(test_data_missing(), "required test data missing") def setUp(self): self.ascii = self.make_path('threads/ascii_table/sim.poisson.1.dat') self.fits = self.make_path('1838_rprofile_rmid.fits') self.singledat = self.make_path('single.dat') self.singletbl = self.make_path('single.fits') self.doubledat = self.make_path('double.dat') self.doubletbl = self.make_path('double.fits') self.img = self.make_path('img.fits') self.filter_single_int_ascii = self.make_path( 'filter_single_integer.dat') self.filter_single_int_table = self.make_path( 'filter_single_integer.fits') self.filter_single_log_table = self.make_path( 'filter_single_logical.fits') self.func = lambda x: x ui.dataspace1d(1, 1000, dstype=ui.Data1D) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_ascii(self): ui.load_ascii(1, self.ascii) ui.load_ascii(1, self.ascii, 2) ui.load_ascii(1, self.ascii, 2, ("col2", "col1")) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_table(self): ui.load_table(1, self.fits) ui.load_table(1, self.fits, 3) ui.load_table(1, self.fits, 3, ["RMID", "SUR_BRI", "SUR_BRI_ERR"]) ui.load_table(1, self.fits, 4, ('R', "SUR_BRI", 'SUR_BRI_ERR'), ui.Data1DInt) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') def test_load_table_fits(self): # QUS: why is this not in the sherpa-test-data repository? this_dir = os.path.dirname(os.path.abspath(__file__)) ui.load_table(1, os.path.join(this_dir, 'data', 'two_column_x_y.fits.gz')) data = ui.get_data(1) self.assertEqualWithinTol(data.x, [1, 2, 3]) self.assertEqualWithinTol(data.y, [4, 5, 6]) # Test table model @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_table_model_ascii_table(self): ui.load_table_model('tbl', self.singledat) ui.load_table_model('tbl', self.doubledat) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_table_model_fits_table(self): ui.load_table_model('tbl', self.singletbl) ui.load_table_model('tbl', self.doubletbl) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_table_model_fits_image(self): ui.load_table_model('tbl', self.img) # Test user model @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_user_model_ascii_table(self): ui.load_user_model(self.func, 'mdl', self.singledat) ui.load_user_model(self.func, 'mdl', self.doubledat) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_user_model_fits_table(self): ui.load_user_model(self.func, 'mdl', self.singletbl) ui.load_user_model(self.func, 'mdl', self.doubletbl) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_filter_ascii(self): ui.load_filter(self.filter_single_int_ascii) ui.load_filter(self.filter_single_int_ascii, ignore=True) # Test load_filter @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_filter_table(self): ui.load_filter(self.filter_single_int_table) ui.load_filter(self.filter_single_int_table, ignore=True) ui.load_filter(self.filter_single_log_table) ui.load_filter(self.filter_single_log_table, ignore=True)
class test_stats_ui(SherpaTestCase): @unittest.skipIf(test_data_missing(), "required test data missing") def setUp(self): self.data = self.make_path('threads/chi2/3c273.pi') ui.clean() # bugs #11400, #13297, #12365 @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_chi2(self): # Case 1: first ds has no error, second has, chi2-derived (chi2gehrels) # statistic. I expect stat.name to be chi2gehrels for ds1, chi2 for # ds2, chi2gehrels for ds1,2 ui.load_data(1, self.data) ui.load_data(2, self.data, use_errors=True) ui.set_source(1, "gauss1d.g1") ui.set_source(2, "gauss1d.g1") ui.set_stat("chi2gehrels") si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('chi2gehrels', stat1) self.assertEqual('chi2', stat2) self.assertEqual('chi2gehrels', stat12) # Case 2: first ds has errors, second has not, chi2-derived # (chi2gehrels) statistic. I expect stat.name to be chi2 for ds1, # chi2gehrels for ds2, chi2gehrels for ds1,2 ui.load_data(2, self.data) ui.load_data(1, self.data, use_errors=True) si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('chi2gehrels', stat2) self.assertEqual('chi2', stat1) self.assertEqual('chi2gehrels', stat12) # Case 3: both datasets have errors, chi2-derived (chi2gehrels) # statistic. I expect stat.name to be chi2 for all of them. ui.load_data(2, self.data, use_errors=True) ui.load_data(1, self.data, use_errors=True) si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('chi2', stat2) self.assertEqual('chi2', stat1) self.assertEqual('chi2', stat12) # Case 4: first ds has errors, second has not, LeastSq statistic # I expect stat.name to be leastsq for all of them. ui.load_data(2, self.data) ui.load_data(1, self.data, use_errors=True) ui.set_stat("leastsq") si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('leastsq', stat2) self.assertEqual('leastsq', stat1) self.assertEqual('leastsq', stat12) # Case 5: both ds have errors, LeastSq statistic # I expect stat.name to be leastsq for all of them. ui.load_data(2, self.data, use_errors=True) ui.load_data(1, self.data, use_errors=True) ui.set_stat("leastsq") si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('leastsq', stat2) self.assertEqual('leastsq', stat1) self.assertEqual('leastsq', stat12) # Case 6: first ds has errors, second has not, CStat statistic # I expect stat.name to be cstat for all of them. ui.load_data(2, self.data) ui.load_data(1, self.data, use_errors=True) ui.set_stat("cstat") si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('cstat', stat2) self.assertEqual('cstat', stat1) self.assertEqual('cstat', stat12) # Case7: select chi2 as statistic. One of the ds does not provide # errors. I expect sherpa to raise a StatErr exception. ui.set_stat('chi2') caught = False from sherpa.utils.err import StatErr try: ui.get_stat_info() except StatErr: caught = True self.assertTrue(caught, msg='StatErr was not caught') # Case8: select chi2 as statistic. Both datasets provide errors # I expect stat to be 'chi2' ui.load_data(2, self.data, use_errors=True) si = ui.get_stat_info() stat1 = si[0].statname stat2 = si[1].statname stat12 = si[2].statname self.assertEqual('chi2', stat2) self.assertEqual('chi2', stat1) self.assertEqual('chi2', stat12)
@staticmethod def mycal_staterror(data): return numpy.ones_like(data) @staticmethod def chi_nobkg(data, model, staterror=None, syserror=None, weight=None): fvec = ((data - model) / staterror)**2 stat = fvec.sum() return (stat, fvec) calc_stat = chi_nobkg calc_staterror = mycal_staterror @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") class test_stats(SherpaTestCase): _fit_mycash_results_bench = { 'succeeded': 1, 'numpoints': 446, 'dof': 443, 'istatval': 796.401435754, 'statval': -14889.3202844, 'parnames': ('abs1.nH', 'abs1.gamma', 'abs1.ampl'), 'parvals': numpy.array( [5886.0660236942495, 1.6556198746259132, 30098.968589487202])
class test_pha(SherpaTestCase): def setUp(self): datastack.clear_stack() ui.clean() datastack.set_template_id("ID") self._this_dir = os.path.dirname(sys.modules[self.__module__].__file__) self.loggingLevel = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) def tearDown(self): datastack.clear_stack() ui.clean() datastack.set_template_id("__ID") logger.setLevel(self.loggingLevel) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') def test_case_6(self): datadir = '/'.join((self._this_dir, 'data')) ls = '@' + '/'.join((datadir, 'pha.lis')) rmf1 = '/'.join((datadir, "acisf04938_000N002_r0043_rmf3.fits")) rmf2 = '/'.join((datadir, "acisf07867_000N001_r0002_rmf3.fits")) arf1 = '/'.join((datadir, "acisf04938_000N002_r0043_arf3.fits")) arf2 = '/'.join((datadir, "acisf07867_000N001_r0002_arf3.fits")) datastack.load_pha(ls) datastack.load_bkg_rmf([], rmf1) datastack.load_bkg_rmf([], rmf2) datastack.load_bkg_arf([], arf1) datastack.load_bkg_arf([], arf2) # Define background models bkg_arfs = datastack.get_bkg_arf([]) bkg_scales = datastack.get_bkg_scale([]) bkg_models = [ ui.const1d.c1 * acis_bkg_model('acis7s'), ui.const1d.c2 * acis_bkg_model('acis7s') ] bkg_rsps = datastack.get_response([], bkg_id=1) for i in range(2): id_ = i + 1 # Make the ARF spectral response flat. This is required for using # the acis_bkg_model. bkg_arfs[i].specresp = bkg_arfs[i].specresp * 0 + 1. datastack.set_bkg_full_model(id_, bkg_rsps[i](bkg_models[i])) # Fit background datastack.notice(0.5, 8.) datastack.set_method("neldermead") datastack.set_stat("cash") datastack.thaw(c1.c0) datastack.thaw(c2.c0) datastack.fit_bkg() datastack.freeze(c1.c0) datastack.freeze(c2.c0) # Define source models rsps = datastack.get_response([]) src_model = ui.powlaw1d.pow1 src_models = [src_model, src_model * ui.const1d.ratio_12] for i in range(2): id_ = i + 1 datastack.set_full_model( id_, (rsps[i](src_models[i]) + bkg_scales[i] * bkg_rsps[i](bkg_models[i]))) datastack.fit()
class test_design(SherpaTestCase): def setUp(self): datastack.clear_stack() ui.clean() datastack.set_template_id("ID") self.loggingLevel = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) datastack.set_stack_verbosity(logging.ERROR) self._this_dir = os.path.dirname(sys.modules[self.__module__].__file__) def tearDown(self): datastack.clear_stack() ui.clean() datastack.set_template_id("__ID") logger.setLevel(self.loggingLevel) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') def test_case_1(self): datadir = '/'.join((self._this_dir, 'data')) ls = '@' + '/'.join((datadir, '3c273.lis')) datastack.load_pha(ls, use_errors=True) assert 2 == len(datastack.DATASTACK.datasets) assert 2 == len(ui._session._data) datastack.load_pha("myid", '/'.join((datadir, "3c273.pi"))) assert 2 == len(datastack.DATASTACK.datasets) assert 3 == len(ui._session._data) datastack.load_pha('/'.join((datadir, "3c273.pi"))) assert 3 == len(datastack.DATASTACK.datasets) assert 4 == len(ui._session._data) datastack.load_pha([], '/'.join((datadir, "3c273.pi"))) assert 4 == len(datastack.DATASTACK.datasets) assert 5 == len(ui._session._data) ds = datastack.DataStack() datastack.load_pha(ds, ls) assert 4 == len(datastack.DATASTACK.datasets) assert 7 == len(ui._session._data) assert 2 == len(ds.datasets) datastack.load_pha(ds, '/'.join((datadir, "3c273.pi"))) assert 4 == len(datastack.DATASTACK.datasets) assert 8 == len(ui._session._data) assert 3 == len(ds.datasets) dids = datastack.DATASTACK.get_stack_ids() assert dids == [1, 2, 3, 4] sids = ui._session._data.keys() assert sids == [1, 2, 3, 4, 5, 6, 7, "myid"] datastack.set_source([1, 2], "powlaw1d.pID") datastack.set_source([3, 4], "brokenpowerlaw.bpID") dsids = ds.get_stack_ids() assert dsids == [5, 6, 7] p1 = ui._session._model_components['p1'] p2 = ui._session._model_components['p2'] bp3 = ui._session._model_components['bp3'] bp4 = ui._session._model_components['bp4'] assert p1 is not None assert p2 is not None assert bp3 is not None assert bp4 is not None datastack.set_source(1, "polynom1d.poly1") datastack.set_source([2, 3, 4], "atten.attID") poly1 = ui._session._model_components['poly1'] a2 = ui._session._model_components['att2'] a3 = ui._session._model_components['att3'] a4 = ui._session._model_components['att4'] assert poly1 is not None assert a2 is not None assert a3 is not None assert a4 is not None datastack.clean() assert 0 == len(datastack.DATASTACK.datasets) assert 0 == len(ui._session._data) assert 3 == len(ds.datasets)
class test_load(SherpaTestCase): def setUp(self): datastack.clear_stack() ui.clean() datastack.set_template_id("__ID") self._this_dir = os.path.dirname(sys.modules[self.__module__].__file__) self.create_files() self.loggingLevel = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) def tearDown(self): datastack.clear_stack() ui.clean() datastack.set_template_id("__ID") os.remove(self.lisname) os.remove(self.name1) os.remove(self.name2) datastack.set_stack_verbose(False) logger.setLevel(self.loggingLevel) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') def test_case_3(self): datastack.load_ascii("@{}".format(self.lisname)) assert len(ui._session._data) == 2 assert len(datastack.DATASTACK.datasets) == 2 datastack.load_data("@{}".format(self.lisname)) assert len(ui._session._data) == 4 assert len(datastack.DATASTACK.datasets) == 4 datastack.load_data("@" + "/".join((self._this_dir, 'data', 'pha.lis'))) assert len(ui._session._data) == 6 assert len(datastack.DATASTACK.datasets) == 6 def create_files(self): fd1, self.name1 = tempfile.mkstemp() fd2, self.name2 = tempfile.mkstemp() fdlis, self.lisname = tempfile.mkstemp() ascii1 = open(self.name1, 'w') ascii2 = open(self.name2, 'w') lis = open(self.lisname, 'w') x1 = np.arange(50) + 100 y1 = 2 * (3 * x1**2 + x1) x2 = np.arange(50) y2 = 2 * (x2**2 + 3 * x2) for x, y in zip(x1, y1): ascii1.write("{} {}\n".format(x, y)) ascii1.close() os.close(fd1) for x, y in zip(x2, y2): ascii2.write("{} {}\n".format(x, y)) ascii2.close() os.close(fd2) lis.write("{}\n".format(self.name1)) lis.write("{}\n".format(self.name2)) lis.close() os.close(fdlis)
class test_xspec(SherpaTestCase): def test_create_model_instances(self): import sherpa.astro.xspec as xs count = 0 for cls in dir(xs): if not cls.startswith('XS'): continue cls = getattr(xs, cls) if is_proper_subclass( cls, (xs.XSAdditiveModel, xs.XSMultiplicativeModel)): m = cls() count += 1 self.assertEqual(count, 164) def test_evaluate_model(self): import sherpa.astro.xspec as xs m = xs.XSbbody() out = m([1, 2, 3, 4]) if m.calc.__name__.startswith('C_'): otype = numpy.float64 else: otype = numpy.float32 self.assert_(out.dtype.type is otype) self.assertEqual(int(numpy.flatnonzero(out == 0.0)), 3) def test_xspec_models(self): import sherpa.astro.xspec as xs models = [model for model in dir(xs) if model[:2] == 'XS'] models.remove('XSModel') models.remove('XSMultiplicativeModel') models.remove('XSAdditiveModel') models.remove('XSTableModel') xx = numpy.arange(0.1, 11.01, 0.01, dtype=float) xlo = numpy.array(xx[:-1]) xhi = numpy.array(xx[1:]) for model in models: cls = getattr(xs, model) foo = cls('foo') vals = foo(xlo, xhi) try: self.assert_(not numpy.isnan(vals).any() and not numpy.isinf(vals).any()) except AssertionError: error('XS%s model evaluation failed' % model) raise @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_set_analysis_wave_fabrizio(self): rmf = self.datadir + '/ciao4.3/fabrizio/Data/3c273.rmf' arf = self.datadir + '/ciao4.3/fabrizio/Data/3c273.arf' ui.set_model("fabrizio", "xspowerlaw.p1") ui.fake_pha("fabrizio", arf, rmf, 10000) model = ui.get_model("fabrizio") bare_model, _ = ui._session._get_model_status("fabrizio") y = bare_model.calc([1, 1], model.xlo, model.xhi) y_m = numpy.mean(y) ui.set_analysis("fabrizio", "wave") model2 = ui.get_model("fabrizio") bare_model2, _ = ui._session._get_model_status("fabrizio") y2 = bare_model2.calc([1, 1], model2.xlo, model2.xhi) y2_m = numpy.mean(y2) self.assertAlmostEqual(y_m, y2_m) def test_xsxset_get(self): import sherpa.astro.xspec as xs # TEST CASE #1 Case insentitive keys xs.set_xsxset('fooBar', 'somevalue') self.assertEqual('somevalue', xs.get_xsxset('Foobar'))
class test_threads(SherpaTestCase): def setUp(self): self.is_crates_io = False try: import sherpa.astro.io if ("sherpa.astro.io.crates_backend" == sherpa.astro.io.backend.__name__): self.is_crates_io = True except: self.is_crates_io = False self.startdir = os.getcwd() self.old_state = ui._session.__dict__.copy() self.old_level = logger.getEffectiveLevel() logger.setLevel(logging.CRITICAL) def tearDown(self): os.chdir(self.startdir) ui._session.__dict__.update(self.old_state) logger.setLevel(self.old_level) def assign_model(self, name, obj): self.locals[name] = obj def run_thread(self, name, scriptname='fit.py'): ui.clean() ui.set_model_autoassign_func(self.assign_model) self.locals = {} os.chdir(os.path.join(self.datadir, 'ciao4.3', name)) execfile(scriptname, {}, self.locals) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_pha_intro(self): self.run_thread('pha_intro') # astro.ui imported as ui, instead of # being in global namespace self.assertEqualWithinTol(ui.get_fit_results().statval, 37.9079, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.902569, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.651155, 1e-4) self.assertEqualWithinTol(self.locals['p1'].gamma.val, 2.15852, 1e-4) self.assertEqualWithinTol(self.locals['p1'].ampl.val, 0.00022484, 1e-4) self.assertEqualWithinTol(ui.calc_photon_flux(), 0.000469964, 1e-4) self.assertEqualWithinTol(ui.calc_energy_flux(), 9.614847e-13, 1e-4) self.assertEqualWithinTol(ui.calc_data_sum(), 706.85714092, 1e-4) self.assertEqualWithinTol(ui.calc_model_sum(), 638.45693377, 1e-4) self.assertEqualWithinTol(ui.calc_source_sum(), 0.046996409, 1e-4) self.assertEqualWithinTol( ui.eqwidth(self.locals['p1'], ui.get_source()), -0.57731725, 1e-4) self.assertEqualWithinTol( ui.calc_kcorr([1, 1.2, 1.4, 1.6, 1.8, 2], 0.5, 2), [ 0.93341286, 0.93752836, 0.94325233, 0.94990140, 0.95678054, 0.96393515 ], 1e-4) self.assertEqual(ui.get_fit_results().nfev, 22) self.assertEqual(ui.get_fit_results().numpoints, 44) self.assertEqual(ui.get_fit_results().dof, 42) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_pha_read(self): self.run_thread('pha_read') self.assertEqual(type(ui.get_data()), DataPHA) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_basic(self): # In data1.dat for this test, there is a comment with one # word at the beginning -- deliberately would break when reading # with DM ASCII kernel, but passes because we have Sherpa code # to bypass that. self.run_thread('basic') self.assertEqualWithinTol(ui.get_fit_results().statval, 151.827, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 16.8697, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 3.68798e-28, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c0.val, 1.49843, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c1.val, 0.1447, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c2.val, 0.0322936, 1e-4) self.assertEqualWithinTol(self.locals['m1'].c3.val, -0.00277729, 1e-4) self.assertEqualWithinTol(self.locals['m2'].c0.val, 1.75548, 1e-4) self.assertEqualWithinTol(self.locals['m2'].c1.val, 0.198455, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 9) self.assertEqual(ui.get_fit_results().numpoints, 11) self.assertEqual(ui.get_fit_results().dof, 9) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_simultaneous(self): self.run_thread('simultaneous') self.assertEqualWithinTol(ui.get_fit_results().statval, 7.4429, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.531636, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.916288, 1e-4) self.assertEqualWithinTol(self.locals['abs1'].nh.val, 0.898162, 1e-2) self.assertEqualWithinTol(self.locals['pl1'].gamma.val, 1.645, 1e-4) self.assertEqualWithinTol(self.locals['pl1'].ampl.val, 2.28323e-05, 1e-3) self.assertEqualWithinTol(self.locals['pl2'].ampl.val, 2.44585e-05, 1e-3) self.assertEqual(ui.get_fit_results().numpoints, 18) self.assertEqual(ui.get_fit_results().dof, 14) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_sourceandbg(self): self.run_thread('sourceandbg') self.assertEqualWithinTol(ui.get_fit_results().statval, 947.5, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 0.715094, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 1, 1e-4) self.assertEqualWithinTol(self.locals['a1'].nh.val, 0.0342266, 1e-2) self.assertEqualWithinTol(self.locals['b1'].kt.val, 20, 1e-2) self.assertEqualWithinTol(self.locals['b1'].norm.val, 0.00953809, 1e-2) self.assertEqualWithinTol(self.locals['b2'].kt.val, 0.563109, 1e-2) self.assertEqualWithinTol(self.locals['b2'].norm.val, 1.16118e-05, 1e-2) self.assertEqual(ui.get_fit_results().numpoints, 1330) self.assertEqual(ui.get_fit_results().dof, 1325) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_spatial(self): self.run_thread('spatial') self.assertEqualWithinTol(ui.get_fit_results().statval, -59229.749441, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 61.5615, 1e-4) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 4070.45, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 4251.35, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 22.1269, 1e-4) self.assertEqualWithinTol(self.locals['g2'].fwhm.val, 6.20409, 1e-4) self.assertEqualWithinTol(self.locals['g2'].xpos.val, 4070.78, 1e-4) self.assertEqualWithinTol(self.locals['g2'].ypos.val, 4249.33, 1e-4) self.assertEqualWithinTol(self.locals['g2'].ampl.val, 226.563, 1e-4) #self.assertEqual(ui.get_fit_results().nfev,371) self.assertEqual(ui.get_fit_results().numpoints, 4881) self.assertEqual(ui.get_fit_results().dof, 4877) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_pileup(self): self.run_thread('pileup') self.assertEqualWithinTol(ui.get_fit_results().statval, 53.6112, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 1.44895, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0379417, 1e-4) self.assertEqualWithinTol(self.locals['jdp'].alpha.val, 0.522593, 1e-1) self.assertEqualWithinTol(self.locals['jdp'].f.val, 0.913458, 1e-2) self.assertEqualWithinTol(self.locals['abs1'].nh.val, 6.12101, 1e-2) self.assertEqualWithinTol(self.locals['power'].gamma.val, 1.41887, 1e-2) self.assertEqualWithinTol(self.locals['power'].ampl.val, 0.00199457, 1e-2) self.assertEqual(ui.get_fit_results().numpoints, 42) self.assertEqual(ui.get_fit_results().dof, 37) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_radpro(self): self.run_thread('radpro') self.assertEqualWithinTol(ui.get_fit_results().statval, 217.450, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 6.21287, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 125.829, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 4.1633, 1e-4) self.assertEqualWithinTol(self.locals['src'].xpos.val, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 4.42821, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 92) self.assertEqual(ui.get_fit_results().numpoints, 38) self.assertEqual(ui.get_fit_results().dof, 35) @unittest.skipIf(test_data_missing(), "required test data missing") def test_radpro_dm(self): # This test is completely redundant to test_radpro above. # The only difference is that here I test if using DM syntax # in the file name returns the correct arrays from CRATES. # Since any other I/O backend would not understand DM syntax, # I make this test a no-op unless CRATES is used as the I/O # backend. SMD 05/23/13 if (True == self.is_crates_io): self.run_thread('radpro_dm') self.assertEqualWithinTol(ui.get_fit_results().statval, 217.450, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().rstat, 6.21287, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().qval, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 125.829, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 4.1633, 1e-4) self.assertEqualWithinTol(self.locals['src'].xpos.val, 0.0, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 4.42821, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 92) self.assertEqual(ui.get_fit_results().numpoints, 38) self.assertEqual(ui.get_fit_results().dof, 35) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_psf2d(self): self.run_thread('psf') self.assertEqualWithinTol(ui.get_fit_results().statval, 4066.78, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 2.80117, 1e-2) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 77.2271, 1e-2) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 88.661, 1e-2) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 166.649, 1e-2) #self.assertEqual(ui.get_fit_results().nfev,342) self.assertEqual(ui.get_fit_results().numpoints, 4899) self.assertEqual(ui.get_fit_results().dof, 4895) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_fpsf2d(self): self.run_thread('fpsf') self.assertEqualWithinTol(ui.get_fit_results().statval, -4053.6635, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].xlow.val, -4.70832, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].xhi.val, 164.687, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].ylow.val, 0.83626, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].yhi.val, 142.603, 1e-4) # self.assertEqualWithinTol(self.locals['b1'].ampl.val, 0.956766, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 6.420237, 1e-4) self.assertEqualWithinTol(self.locals['g1'].xpos.val, 88.940712, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ypos.val, 76.577265, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 36344.48324, 1e-4) #self.assertEqual(ui.get_fit_results().nfev,978) self.assertEqual(ui.get_fit_results().numpoints, 4899) self.assertEqual(ui.get_fit_results().dof, 4895) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_radpro_psf(self): self.run_thread('radpro_psf') self.assertEqualWithinTol(ui.get_fit_results().statval, 200.949, 1e-4) self.assertEqualWithinTol(self.locals['src'].r0.val, 83.0997, 1e-4) self.assertEqualWithinTol(self.locals['src'].beta.val, 2.97737, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 5.27604, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 48) self.assertEqual(ui.get_fit_results().numpoints, 38) self.assertEqual(ui.get_fit_results().dof, 35) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_linepro(self): self.run_thread('linepro') self.assertEqualWithinTol(ui.get_fit_results().statval, 203.34, 1e-4) self.assertEqualWithinTol(self.locals['b1'].r0.val, 4.25557, 1e-4) self.assertEqualWithinTol(self.locals['b1'].beta.val, 0.492232, 1e-4) self.assertEqualWithinTol(self.locals['b1'].ampl.val, 11.8129, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 17) self.assertEqual(ui.get_fit_results().numpoints, 75) self.assertEqual(ui.get_fit_results().dof, 72) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_kernel(self): self.run_thread('kernel') self.assertEqualWithinTol(ui.get_fit_results().statval, 98.5793, 1e-4) self.assertEqualWithinTol(self.locals['b1'].r0.val, 19.2278, 1e-4) self.assertEqualWithinTol(self.locals['b1'].beta.val, 0.555464, 1e-4) self.assertEqualWithinTol(self.locals['b1'].ampl.val, 1.93706, 1e-4) self.assertEqual(ui.get_fit_results().nfev, 21) self.assertEqual(ui.get_fit_results().numpoints, 75) self.assertEqual(ui.get_fit_results().dof, 72) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_spectrum(self): self.run_thread('spectrum') self.assertEqualWithinTol(ui.get_fit_results().statval, 0.0496819, 1e-4) self.assertEqualWithinTol(self.locals['abs2'].nh.val, 1.1015, 1e-4) self.assertEqualWithinTol(self.locals['mek1'].kt.val, 0.841025, 1e-4) self.assertEqualWithinTol(self.locals['mek1'].norm.val, 0.699761, 1e-4) self.assertEqualWithinTol(self.locals['mek2'].kt.val, 2.35845, 1e-4) self.assertEqualWithinTol(self.locals['mek2'].norm.val, 1.03724, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 446) self.assertEqual(ui.get_fit_results().dof, 441) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_histo(self): self.run_thread('histo') self.assertEqualWithinTol(ui.get_fit_results().statval, 14.7264, 1e-4) self.assertEqualWithinTol(self.locals['g1'].fwhm.val, 0.0232473, 1e-4) self.assertEqualWithinTol(self.locals['g1'].pos.val, 1.26713, 1e-4) self.assertEqualWithinTol(self.locals['g1'].ampl.val, 40.4503, 1e-4) #self.assertEqual(ui.get_fit_results().nfev,19) self.assertEqual(ui.get_fit_results().numpoints, 50) self.assertEqual(ui.get_fit_results().dof, 47) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_xmm(self): self.run_thread('xmm') self.assertEqualWithinTol(ui.get_fit_results().statval, 118.085, 1e-4) self.assertEqualWithinTol(self.locals['intrin'].nh.val, 11.0769, 1e-2) self.assertEqualWithinTol(self.locals['phard'].phoindex.val, 1.49055, 1e-2) self.assertEqualWithinTol(self.locals['phard'].norm.val, 0.00140301, 1e-2) self.assertEqual(ui.get_fit_results().nfev, 95) self.assertEqual(ui.get_fit_results().numpoints, 162) self.assertEqual(ui.get_fit_results().dof, 159) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") # As of CIAO 4.5, can filter on channel number, even when # data are grouped! Test results should exactly match CIAO 4.4 # fit results in grouped/fit.py def test_grouped_ciao4_5(self): self.run_thread('grouped_ciao4.5') self.assertEqualWithinTol(ui.get_fit_results().statval, 18.8316, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 46) self.assertEqualWithinTol(self.locals['aa'].gamma.val, 1.83906, 1e-4) self.assertEqualWithinTol(self.locals['aa'].ampl.val, 0.000301258, 1e-4) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_proj(self): self.run_thread('proj') # Fit 1 self.assertEqualWithinTol(self.locals['fit_res1'].statval, 64.3803, 1e-2) self.assertEqualWithinTol(self.locals['g'].fwhm.val, 32.3536, 1e-2) self.assertEqualWithinTol(self.locals['g'].pos.val, 807.863, 1e-2) self.assertEqualWithinTol(self.locals['g'].ampl.val, 117.826, 1e-2) # Covar 1 self.assertEqualWithinTol(self.locals['covar_res1'].parmins[0], -0.49357, 1e-2) self.assertEqualWithinTol(self.locals['covar_res1'].parmins[1], -0.264056, 1e-2) self.assertEqualWithinTol(self.locals['covar_res1'].parmins[2], -2.58857, 1e-2) self.assertEqualWithinTol(self.locals['covar_res1'].parmaxes[0], 0.49357, 1e-2) self.assertEqualWithinTol(self.locals['covar_res1'].parmaxes[1], 0.264056, 1e-2) self.assertEqualWithinTol(self.locals['covar_res1'].parmaxes[2], 2.58857, 1e-2) # Projection 1 self.assertEqualWithinTol(self.locals['proj_res1'].parmins[0], -0.492815, 1e-2) self.assertEqualWithinTol(self.locals['proj_res1'].parmins[1], -0.263945, 1e-2) self.assertEqualWithinTol(self.locals['proj_res1'].parmins[2], -2.57957, 1e-2) self.assertEqualWithinTol(self.locals['proj_res1'].parmaxes[0], 0.49421, 1e-2) self.assertEqualWithinTol(self.locals['proj_res1'].parmaxes[1], 0.26389, 1e-2) self.assertEqualWithinTol(self.locals['proj_res1'].parmaxes[2], 2.59695, 1e-2) # Fit 2 self.assertEqualWithinTol(self.locals['fit_res2'].statval, 108.048, 1e-2) self.assertEqualWithinTol(self.locals['xs1'].nh.val, 0.0532586, 1e-2) self.assertEqualWithinTol(self.locals['p'].gamma.val, 1.4816, 1e-2) self.assertEqualWithinTol(self.locals['p'].ampl.val, 0.302343, 1e-2) # Covar 2 self.assertEqualWithinTol(self.locals['covar_res2'].parmins[0], -0.0379975, 5e-1) self.assertEqualWithinTol(self.locals['covar_res2'].parmins[1], -0.0877478, 5e-1) self.assertEqualWithinTol(self.locals['covar_res2'].parmins[2], -0.0831945, 5e-1) self.assertEqualWithinTol(self.locals['covar_res2'].parmaxes[0], 0.0379975, 5e-1) self.assertEqualWithinTol(self.locals['covar_res2'].parmaxes[1], 0.0877478, 5e-1) self.assertEqualWithinTol(self.locals['covar_res2'].parmaxes[2], 0.0831945, 5e-1) # Projection 2 self.assertEqualWithinTol(self.locals['proj_res2'].parmins[0], -0.0385636, 1e-2) self.assertEqualWithinTol(self.locals['proj_res2'].parmins[1], -0.0891261, 1e-2) self.assertEqualWithinTol(self.locals['proj_res2'].parmins[2], -0.0737413, 1e-2) self.assertEqualWithinTol(self.locals['proj_res2'].parmaxes[0], 0.0388651, 1e-2) self.assertEqualWithinTol(self.locals['proj_res2'].parmaxes[1], 0.0896566, 1e-2) self.assertEqualWithinTol(self.locals['proj_res2'].parmaxes[2], 0.0981627, 1e-2) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_proj_bubble(self): self.run_thread('proj_bubble') # Fit -- Results from reminimize self.assertEqualWithinTol(self.locals['mek1'].kt.val, 17.8849, 1e-2) self.assertEqualWithinTol(self.locals['mek1'].norm.val, 4.15418e-06, 1e-2) # Covar self.assertEqualWithinTol(ui.get_covar_results().parmins[0], -0.328832, 1e-2) self.assertEqualWithinTol(ui.get_covar_results().parmins[1], -8.847916e-07, 1e-2) self.assertEqualWithinTol(ui.get_covar_results().parmaxes[0], 0.328832, 1e-2) self.assertEqualWithinTol(ui.get_covar_results().parmaxes[1], 8.847916e-07, 1e-2) # Proj -- Upper bound of kT can't be found self.assertEqualWithinTol(ui.get_proj_results().parmins[0], -12.048069, 1e-2) self.assertEqualWithinTol(ui.get_proj_results().parmins[1], -9.510913e-07, 1e-2) self.assertEqual(ui.get_proj_results().parmaxes[0], None) self.assertEqualWithinTol(ui.get_proj_results().parmaxes[1], 2.403640e-06, 1e-2) # New tests based on SDS threads -- we should catch these errors # (if any occur) so SDS doesn't waste time tripping over them. @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_counts(self): self.run_thread('counts') self.assertEqualWithinTol(self.locals['counts_data1'], 52701.0, 1e-4) self.assertEqualWithinTol(self.locals['counts_data2'], 25032.0, 1e-4) self.assertEqualWithinTol(self.locals['counts_model1'], 73226263.55355, 1e-4) self.assertEqualWithinTol(self.locals['eflux1'], 1.495482e-08, 1e-4) self.assertEqualWithinTol(self.locals['counts_model2'], 46082543.21529, 1e-4) self.assertEqualWithinTol(self.locals['eflux2'], 1.39662954483e-08, 1e-3) self.assertEqualWithinTol(self.locals['pflux1'], 1.6178938637, 1e-2) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_stats_all(self): self.run_thread('stats_all') self.assertEqualWithinTol(self.locals['stat_lsqr'], 213746.236464, 1e-4) self.assertEqualWithinTol(self.locals['stat_chi2m'], 1232.0330242, 1e-4) self.assertEqualWithinTol(self.locals['stat_cash'], -1163566.90688, 1e-4) self.assertEqualWithinTol(self.locals['stat_chi2c'], 1411.60090961, 1e-4) self.assertEqualWithinTol(self.locals['stat_chi2g'], 972.388468358, 1e-4) self.assertEqualWithinTol(self.locals['stat_chi2d'], 1204.69363458, 1e-4) self.assertEqualWithinTol(self.locals['stat_chi2x'], 1204.69363458, 1e-4) self.assertEqualWithinTol(self.locals['stat_cstat'], 1210.56896183, 1e-4) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_lev3fft(self): self.run_thread('lev3fft', scriptname='bar.py') self.assertEqualWithinTol(self.locals['src'].fwhm.val, 0.04418584, 1e-4) self.assertEqualWithinTol(self.locals['src'].xpos.val, 150.016, 1e-4) self.assertEqualWithinTol(self.locals['src'].ypos.val, 2.66493839, 1e-4) self.assertEqualWithinTol(self.locals['src'].ampl.val, 1.56090546, 1e-4) self.assertEqualWithinTol(self.locals['bkg'].c0.val, -1.513700715, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().istatval, 19496.3, 1e-4) self.assertEqualWithinTol(ui.get_fit_results().statval, 592.32647, 1e-4) self.assertEqual(ui.get_fit_results().numpoints, 3307) self.assertEqual(ui.get_fit_results().dof, 3302) @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_setfullmodel(self): self.run_thread('setfullmodel') @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_bug13537(self): self.run_thread('bug13537') @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_xmm2(self): self.run_thread('xmm2') self.assertEqualWithinTol(ui.get_data().channel[0], 1.0, 1e-4) self.assertEqual(ui.get_rmf().detchans, 800) self.assertEqual(len(ui.get_rmf().energ_lo), 2400) self.assertEqual(len(ui.get_rmf().energ_hi), 2400) self.assertEqual(len(ui.get_rmf().n_grp), 2400) self.assertEqual(len(ui.get_rmf().f_chan), 2394) self.assertEqual(len(ui.get_rmf().n_chan), 2394) self.assertEqual(len(ui.get_rmf().matrix), 1281216) self.assertEqual(ui.get_rmf().offset, 0) self.assertEqual(len(ui.get_rmf().e_min), 800) self.assertEqual(len(ui.get_rmf().e_max), 800) self.assertEqual(len(ui.get_arf().energ_lo), 2400) self.assertEqual(len(ui.get_arf().energ_hi), 2400) self.assertEqual(len(ui.get_arf().specresp), 2400)
class test_xspec(SherpaTestCase): def setUp(self): from sherpa.astro import xspec ui.clean() self._defaults = xspec.get_xsstate() def tearDown(self): from sherpa.astro import xspec xspec.set_xsstate(self._defaults) def test_create_model_instances(self): import sherpa.astro.xspec as xs count = 0 for cls in dir(xs): if not cls.startswith('XS'): continue cls = getattr(xs, cls) if is_proper_subclass( cls, (xs.XSAdditiveModel, xs.XSMultiplicativeModel)): # Ensure that we can create an instance, but do # nothing with it. cls() count += 1 self.assertEqual(count, 164) def test_norm_works(self): # Check that the norm parameter for additive models # works, as it is handled separately from the other # parameters. import sherpa.astro.xspec as xs # need an additive model mdl = xs.XSpowerlaw() mdl.PhoIndex = 2 egrid = [0.1, 0.2, 0.3, 0.4] mdl.norm = 1.2 y1 = mdl(egrid) mfactor = 2.1 mdl.norm = mdl.norm.val * mfactor y2 = mdl(egrid) # check that the sum is not 0 and that it # scales as expected. s1 = y1.sum() s2 = y2.sum() self.assertGreater(s1, 0.0, msg='powerlaw is positive') self.assertAlmostEqual(s2, mfactor * s1, msg='powerlaw norm scaling') def test_evaluate_model(self): import sherpa.astro.xspec as xs mdl = xs.XSbbody() out = mdl([1, 2, 3, 4]) if mdl.calc.__name__.startswith('C_'): otype = numpy.float64 else: otype = numpy.float32 self.assertEqual(out.dtype.type, otype) self.assertEqual(int(numpy.flatnonzero(out == 0.0)), 3) def test_checks_input_length(self): import sherpa.astro.xspec as xs mdl = xs.XSpowerlaw() # Check when input array is too small (< 2 elements) self.assertRaises(TypeError, mdl, [0.1]) # Check when input arrays are not the same size (when the # low and high bin edges are given) self.assertRaises(TypeError, mdl, [0.1, 0.2, 0.3], [0.2, 0.3]) self.assertRaises(TypeError, mdl, [0.1, 0.2], [0.2, 0.3, 0.4]) def test_xspec_models(self): import sherpa.astro.xspec as xs models = get_xspec_models(xs) egrid, elo, ehi, wgrid, wlo, whi = make_grid() for model in models: cls = getattr(xs, model) mdl = cls(model) # use an identifier in case there is an error # The model checks that the values are all finite, # so there is no need to check that the output of # mdl does not contain non-finite values. # evals1 = mdl(egrid) evals2 = mdl(elo, ehi) wvals1 = mdl(wgrid) wvals2 = mdl(wlo, whi) emsg = "{} model evaluation failed: ".format(model) # It might be expected that the test should be # assert_allclose(evals1[:-1], evals2) # to ensure there's no floating-point issues, # but in this case the grid and parameter # values *should* be exactly the same, so the # results *should* be exactly equal, hence # the use of assert_array_equal assert_array_equal(evals1[:-1], evals2, err_msg=emsg + "energy comparison") assert_array_equal(wvals1[:-1], wvals2, err_msg=emsg + "wavelength comparison") # When comparing wavelength to energy values, have # to use allclose since the bins are not identically # equal. assert_allclose(evals1, wvals1, err_msg=emsg + "energy to wavelength") def test_xspec_models_noncontiguous2(self): """Note that there is no test that the non-contiguous grid results are similar to the result from using a contiguous grid and then filtering out the missing bins. The way that some models are implemented make this a tricky test to write (due to numerical tolerances), as bins at the edges may not match well. """ import sherpa.astro.xspec as xs models = get_xspec_models(xs) elo, ehi, wlo, whi = make_grid_noncontig2() for model in models: cls = getattr(xs, model) mdl = cls(model) # use an identifier in case there is an error evals2 = mdl(elo, ehi) wvals2 = mdl(wlo, whi) emsg = "{} non-contiguous model evaluation failed: ".format(model) assert_allclose(evals2, wvals2, err_msg=emsg + "energy to wavelength") @unittest.skipIf(test_data_missing(), "required test data missing") def test_tablemodel_checks_input_length(self): # see test_table_model for more information on the table # model being used. # ui.load_table_model('mdl', self.make_path('xspec/tablemodel/RCS.mod')) mdl = ui.get_model_component('mdl') # Check when input array is too small (< 2 elements) self.assertRaises(TypeError, mdl, [0.1]) # Check when input arrays are not the same size (when the # low and high bin edges are given) self.assertRaises(TypeError, mdl, [0.1, 0.2, 0.3], [0.2, 0.3]) self.assertRaises(TypeError, mdl, [0.1, 0.2], [0.2, 0.3, 0.4]) @unittest.skipIf(test_data_missing(), "required test data missing") def test_xspec_tablemodel(self): # Just test one table model; use the same scheme as # test_xspec_models_noncontiguous(). # # The table model is from # https://heasarc.gsfc.nasa.gov/xanadu/xspec/models/rcs.html # retrieved July 9 2015. The exact model is irrelevant for this # test, so this was chosen as it's relatively small. ui.load_table_model('tmod', self.make_path('xspec/tablemodel/RCS.mod')) # when used in the test suite it appears that the tmod # global symbol is not created, so need to access the component tmod = ui.get_model_component('tmod') self.assertEqual(tmod.name, 'xstablemodel.tmod') egrid, elo, ehi, wgrid, wlo, whi = make_grid() evals1 = tmod(egrid) evals2 = tmod(elo, ehi) wvals1 = tmod(wgrid) wvals2 = tmod(wlo, whi) emsg = "table model evaluation failed: " assert_array_equal(evals1[:-1], evals2, err_msg=emsg + "energy comparison") assert_allclose(evals1, wvals1, err_msg=emsg + "single arg") assert_allclose(evals2, wvals2, err_msg=emsg + "two args") @unittest.skipIf(test_data_missing(), "required test data missing") def test_xspec_tablemodel_noncontiguous2(self): ui.load_table_model('tmod', self.make_path('xspec/tablemodel/RCS.mod')) tmod = ui.get_model_component('tmod') elo, ehi, wlo, whi = make_grid_noncontig2() evals2 = tmod(elo, ehi) wvals2 = tmod(wlo, whi) emsg = "table model non-contiguous evaluation failed: " rtol = 1e-3 assert_allclose(evals2, wvals2, rtol=rtol, err_msg=emsg + "energy to wavelength") def test_convolution_model_cflux(self): # Use the cflux convolution model, since this gives # an easily-checked result. At present the only # interface to these models is via direct access # to the functions (i.e. there are no model classes # providing access to this functionality). # import sherpa.astro.xspec as xs if not hasattr(xs._xspec, 'C_cflux'): self.skipTest('cflux convolution model is missing') # The energy grid should extend beyond the energy grid # used to evaluate the model, to avoid any edge effects. # It also makes things easier if the elo/ehi values align # with the egrid bins. elo = 0.55 ehi = 1.45 egrid = numpy.linspace(0.5, 1.5, 101) mdl1 = xs.XSpowerlaw() mdl1.PhoIndex = 2 # flux of mdl1 over the energy range of interest; converting # from a flux in photon/cm^2/s to erg/cm^2/s, when the # energy grid is in keV. y1 = mdl1(egrid) idx, = numpy.where((egrid >= elo) & (egrid < ehi)) # To match XSpec, need to multiply by (Ehi^2-Elo^2)/(Ehi-Elo) # which means that we need the next bin to get Ehi. Due to # the form of the where statement, we should be missing the # Ehi value of the last bin e1 = egrid[idx] e2 = egrid[idx + 1] f1 = 8.01096e-10 * ((e2 * e2 - e1 * e1) * y1[idx] / (e2 - e1)).sum() # The cflux parameters are elo, ehi, and the log of the # flux within this range (this is log base 10 of the # flux in erg/cm^2/s). The parameters chosen for the # powerlaw, and energy range, should have f1 ~ 1.5e-9 # (log 10 of this is -8.8). lflux = -5.0 pars = [elo, ehi, lflux] y2 = xs._xspec.C_cflux(pars, y1, egrid) elo = egrid[:-1] ehi = egrid[1:] wgrid = _hc / egrid whi = wgrid[:-1] wlo = wgrid[1:] expected = y1 * 10**lflux / f1 numpy.testing.assert_allclose(expected, y2, err_msg='energy, single grid') y1 = mdl1(wgrid) y2 = xs._xspec.C_cflux(pars, y1, wgrid) numpy.testing.assert_allclose(expected, y2, err_msg='wavelength, single grid') expected = expected[:-1] y1 = mdl1(elo, ehi) y2 = xs._xspec.C_cflux(pars, y1, elo, ehi) numpy.testing.assert_allclose(expected, y2, err_msg='energy, two arrays') y1 = mdl1(wlo, whi) y2 = xs._xspec.C_cflux(pars, y1, wlo, whi) numpy.testing.assert_allclose(expected, y2, err_msg='wavelength, two arrays') def test_convolution_model_cpflux_noncontiguous(self): # The models should raise an error if given a non-contiguous # grid. import sherpa.astro.xspec as xs if not hasattr(xs._xspec, 'C_cpflux'): self.skipTest('cpflux convolution model is missing') elo, ehi, wlo, whi = make_grid_noncontig2() lflux = -5.0 pars = [0.2, 0.8, lflux] y1 = numpy.zeros(elo.size) self.assertRaises(ValueError, xs._xspec.C_cpflux, pars, y1, elo, ehi) self.assertRaises(ValueError, xs._xspec.C_cpflux, pars, y1, wlo, whi) @unittest.skipIf(not has_fits_support(), 'need pycrates, astropy.io.fits, or pyfits') @unittest.skipIf(test_data_missing(), "required test data missing") def test_set_analysis_wave_fabrizio(self): rmf = self.make_path('ciao4.3/fabrizio/Data/3c273.rmf') arf = self.make_path('ciao4.3/fabrizio/Data/3c273.arf') ui.set_model("fabrizio", "xspowerlaw.p1") ui.fake_pha("fabrizio", arf, rmf, 10000) parvals = [1, 1] model = ui.get_model("fabrizio") bare_model, _ = ui._session._get_model_status("fabrizio") y = bare_model.calc(parvals, model.xlo, model.xhi) y_m = numpy.mean(y) ui.set_analysis("fabrizio", "wave") model2 = ui.get_model("fabrizio") bare_model2, _ = ui._session._get_model_status("fabrizio") y2 = bare_model2.calc(parvals, model2.xlo, model2.xhi) y2_m = numpy.mean(y2) self.assertAlmostEqual(y_m, y2_m) def test_xsxset_get(self): import sherpa.astro.xspec as xs # TEST CASE #1 Case insentitive keys xs.set_xsxset('fooBar', 'somevalue') self.assertEqual('somevalue', xs.get_xsxset('Foobar'))
class test_sim(SherpaTestCase): @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") def setUp(self): try: from sherpa.astro.io import read_pha from sherpa.astro.xspec import XSwabs, XSpowerlaw except: return # self.startdir = os.getcwd() self.old_level = logger.getEffectiveLevel() logger.setLevel(logging.CRITICAL) pha = self.make_path("refake_0934_1_21_1e4.fak") # rmf = self.make_path("ccdid7_default.rmf") # arf = self.make_path("quiet_0934.arf") self.simarf = self.make_path("aref_sample.fits") self.pcaarf = self.make_path("aref_Cedge.fits") data = read_pha(pha) data.ignore(None, 0.3) data.ignore(7.0, None) rsp = Response1D(data) self.abs1 = XSwabs('abs1') self.p1 = XSpowerlaw('p1') model = rsp(self.abs1 * self.p1) self.fit = Fit(data, model, CStat(), NelderMead(), Covariance()) def tearDown(self): # os.chdir(self.startdir) if hasattr(self, 'old_level'): logger.setLevel(self.old_level) @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_pragbayes_simarf(self): mcmc = sim.MCMC() self.abs1.nh = 0.092886 self.p1.phoindex = 0.994544 self.p1.norm = 9.26369 mcmc.set_sampler("PragBayes") mcmc.set_sampler_opt("simarf", self.simarf) mcmc.set_sampler_opt("p_M", 0.5) mcmc.set_sampler_opt("nsubiter", 7) covar_results = self.fit.est_errors() cov = covar_results.extra_output niter = 10 stats, accept, params = mcmc.get_draws(self.fit, cov, niter=niter) # try: # assert (covar_results.parmaxes < params.std(1)).all() # except AssertionError: # print 'covar: ', str(covar_results.parmaxes) # print 'param: ', str(params.std(1)) # raise @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") def test_pragbayes_pcaarf(self): mcmc = sim.MCMC() self.abs1.nh = 0.092886 self.p1.phoindex = 0.994544 self.p1.norm = 9.26369 mcmc.set_sampler("pragBayes") mcmc.set_sampler_opt("simarf", self.pcaarf) mcmc.set_sampler_opt("p_M", 0.5) mcmc.set_sampler_opt("nsubiter", 5) covar_results = self.fit.est_errors() cov = covar_results.extra_output niter = 10 stats, accept, params = mcmc.get_draws(self.fit, cov, niter=niter)
@staticmethod def mycal_staterror(data): return numpy.ones_like(data) @staticmethod def chi_nobkg(data, model, staterror=None, syserror=None, weight=None): fvec = ((data - model) / staterror)**2 stat = fvec.sum() return (stat, fvec) calc_stat = chi_nobkg calc_staterror = mycal_staterror @unittest.skipIf(not has_fits_support(), 'need pycrates, pyfits or astropy.io.fits') @unittest.skipIf(not has_package_from_list('sherpa.astro.xspec'), "required sherpa.astro.xspec module missing") @unittest.skipIf(test_data_missing(), "required test data missing") class test_stats(SherpaTestCase): _fit_mycash_results_bench = { 'succeeded': 1, 'numpoints': 446, 'dof': 443, 'istatval': 796.401435754,