def testWrite(make_data_path): fname = make_data_path('3c273.pi') ui.load_pha(1, fname) pha_orig = ui.get_data(1) ofh = tempfile.NamedTemporaryFile(suffix='sherpa_test') ui.save_pha(1, ofh.name, ascii=False, clobber=True) # limited checks pha = ui.unpack_pha(ofh.name) assert isinstance(pha, DataPHA) for key in ["channel", "counts"]: newval = getattr(pha, key) oldval = getattr(pha_orig, key) assert_allclose(oldval, newval, err_msg=key) # at present grouping and quality are not written out for key in ["exposure", "backscal", "areascal"]: newval = getattr(pha, key) oldval = getattr(pha_orig, key) assert newval == pytest.approx(oldval), key """
def test_fits_io(self): """ Test that basic FITS I/O functions work. This test ensures that the FITS backend can be used to perform basic I/O functions. """ from sherpa.astro import datastack folder = os.path.dirname(datastack.__file__) infile = os.path.join(folder, "tests", "data", "acisf07867_000N001_r0002_pha3.fits") ui.load_pha(infile) with NamedTemporaryFile() as f: ui.save_pha(f.name, ascii=False, clobber=True) # And can we read it back in? ui.load_pha(2, f.name) # Check the data is the same (note: although counts/channels are integers # we use approximate equality checks here as easier to do). d1 = ui.get_data(1) d2 = ui.get_data(2) assert_almost_equal(d2.channel, d1.channel) assert_almost_equal(d2.counts, d1.counts) assert_almost_equal(d2.exposure, d1.exposure) assert_almost_equal(np.log10(d2.backscal), np.log10(d1.backscal))
def test_fits_io(self): """ Test that basic FITS I/O functions work. This test ensures that the FITS backend can be used to perform basic I/O functions. """ ui.load_pha(self.fits) with NamedTemporaryFile() as f: ui.save_pha(f.name, ascii=False, clobber=True)
def testWrite(self): ofh = tempfile.NamedTemporaryFile(suffix='sherpa_test') ui.save_pha(self._id, ofh.name, ascii=False, clobber=True) # limited checks pha = ui.unpack_pha(ofh.name) self.assertIsInstance(pha, DataPHA) for key in ["channel", "counts"]: newval = getattr(pha, key) oldval = getattr(self._pha, key) assert_allclose(oldval, newval, err_msg=key) # at present grouping and quality are not written out for key in ["exposure", "backscal", "areascal"]: newval = getattr(pha, key) oldval = getattr(self._pha, key) self.assertAlmostEqual(oldval, newval, msg=key) """
def test_save_pha(tmp_path): """Does save_pha work for DataPHA?""" # This import requires an I/O backend hence it is done here # from sherpa.astro.io.meta import Meta ui.load_arrays(1, [1, 2, 3], [5, 4, 3], ui.DataPHA) # The code requires DataPHA to have a "valid" header so # fake one. Ideally we would not require it. # hdr = Meta() ui.get_data().header = hdr out = tmp_path / "data.dat" outfile = str(out) ui.save_pha(outfile) # just check the first line; the output may depend on the FITS backend cts = out.read_text()[:80].split() assert cts[0] == 'SIMPLE' assert cts[1] == '=' assert cts[2] == 'T'
def test_fits_io(self): ui.load_pha(self.fits) with NamedTemporaryFile() as f: ui.save_pha(f.name, ascii=False, clobber=True)
def test_fake_pha_issue_1209(make_data_path, clean_astro_ui, tmp_path): """Check issue #1209. See also sherpa/astro/tests/test_fake_pha.py for test_fake_pha_has_valid_ogip_keywords_all_fake test_fake_pha_has_valid_ogip_keywords_from_real The session fake_pha includes quite a lot of logic which makes that the test case for #1209 should be done at this level, to complement the tests mentioned above. """ infile = make_data_path("acisf01575_001N001_r0085_pha3.fits.gz") ui.load_pha(infile) ui.set_source(ui.powlaw1d.pl) pl.gamma = 1.8 pl.ampl = 1e-4 arf = ui.get_arf() rmf = ui.get_rmf() # check the TOTCTS setting in the input file d1 = ui.get_data() assert d1.header["TOTCTS"] == 855 assert d1.counts.sum() == 855 ui.set_source("newid", pl) ui.fake_pha("newid", exposure=ui.get_exposure(), bkg=ui.get_bkg(), rmf=rmf, arf=arf, backscal=ui.get_backscal()) stat = ui.calc_stat("newid") outfile = tmp_path / "sim.pha" ui.save_pha("newid", str(outfile)) ui.load_pha(3, str(outfile)) d3 = ui.get_data(3) assert isinstance(d3, ui.DataPHA) assert d3.exposure == pytest.approx(37664.157219191) assert d3.areascal == pytest.approx(1.0) assert d3.backscal == pytest.approx(2.2426552620567e-06) assert d3.background_ids == [] assert d3.response_ids == [] # check the header hdr = d3.header assert hdr["TELESCOP"] == "CHANDRA" assert hdr["INSTRUME"] == "ACIS" assert hdr["FILTER"] == "none" # check some other values related to #1209 and #488 (OGIP) # assert "TOTCTS" not in hdr assert hdr["GROUPING"] == 0 assert hdr["QUALITY"] == 0 assert hdr["SYS_ERR"] == 0 # We should get the same value - the responses are not written # to the temporary directory and so we need to load them # directly. # ui.set_rmf(3, rmf) ui.set_arf(3, arf) ui.set_source(3, pl) assert ui.calc_stat(3) == pytest.approx(stat)