def test_img_real_filtered(coord, make_data_path, override_plot_backend, old_numpy_printing): """Filter the image. There is no significant check that the filtering has worked """ from sherpa.astro.io import read_image d = read_image( make_data_path('acisf07999_000N001_r0035_regevt3_srcimg.fits')) d.name = 'regevt3' d.set_coord('logical') d.notice2d("circle(90, 190, 20)") d.set_coord(coord) r = d._repr_html_() assert r is not None if plot.backend.name == 'pylab': assert '<summary>DataIMG Plot</summary>' in r assert '<summary>Coordinates: physical (3)</summary>' in r assert '<summary>Coordinates: world (6)</summary>' in r assert '<summary>Metadata (6)</summary>' in r assert '<div class="dataval">[ 0.5 0.5]</div>' in r assert '<div class="dataval">[ 4096.5 4096.5]</div>' in r assert '<div class="dataval">[-0.000137 0.000137]</div>' in r assert '<div class="dataval">destreak - CAT3.0.2</div>' in r
def test_save_resid_dataimg_fits(tmp_path): """Residual, DataIMG, FITS""" from sherpa.astro.io import read_image y, x = np.mgrid[10:12, 20:23] x = x.flatten() y = y.flatten() z = (x - 11)**2 + (y - 21)**2 ui.load_arrays(1, x, y, z, (2, 3), ui.DataIMG) ui.set_source(1, ui.const2d.cmdl) cmdl.c0 = 100 out = tmp_path / "resid" outfile = str(out) ui.save_resid(outfile) ans = read_image(outfile) assert ans.shape == (2, 3) # Is this correct? yl, xl = np.mgrid[1:3, 1:4] xl = xl.flatten() yl = yl.flatten() assert ans.x0 == pytest.approx(xl) assert ans.x1 == pytest.approx(yl) assert ans.y == pytest.approx(z - 100)
def test_img_real(coord, make_data_path, override_plot_backend, old_numpy_printing): """Use an image from a file (easy to set up)""" from sherpa.astro.io import read_image d = read_image( make_data_path('acisf07999_000N001_r0035_regevt3_srcimg.fits')) d.name = 'regevt3' d.set_coord(coord) r = d._repr_html_() assert r is not None if plot.backend.name == 'pylab': assert '<summary>DataIMG Plot</summary>' in r assert '<summary>Coordinates: physical (3)</summary>' in r assert '<summary>Coordinates: world (6)</summary>' in r assert '<summary>Metadata (6)</summary>' in r assert '<div class="dataval">[ 0.5 0.5]</div>' in r assert '<div class="dataval">[ 4096.5 4096.5]</div>' in r assert '<div class="dataval">[-0.000137 0.000137]</div>' in r assert '<div class="dataval">destreak - CAT3.0.2</div>' in r
def test_save_data_dataimg_fits(tmp_path): """Does save_data work for DataIMG? FITS""" from sherpa.astro.io import read_image y, x = np.mgrid[10:12, 20:23] x = x.flatten() y = y.flatten() z = (x - 11)**2 + (y - 21)**2 ui.load_arrays(1, x, y, z, (2, 3), ui.DataIMG) out = tmp_path / "data.dat" outfile = str(out) ui.save_data(outfile, ascii=False) ans = read_image(outfile) assert ans.shape == (2, 3) # Is this correct? yl, xl = np.mgrid[1:3, 1:4] xl = xl.flatten() yl = yl.flatten() assert ans.x0 == pytest.approx(xl) assert ans.x1 == pytest.approx(yl) assert ans.y == pytest.approx(z)
def read_test_image(make_data_path): from sherpa.astro.io import read_image filename = 'acisf07999_000N001_r0035_regevt3_srcimg.fits' d = read_image(make_data_path(filename)) d.name = 'test.img' return d
def test_image_write_basic(make_data_path, tmp_path): """Check we can write out an image file as a FITS file and keep useful data. """ def check_header(obj): # check header for a selected set of keywords hdr = obj.header # selected OGIP keywords if not backend_is("crates"): assert hdr["HDUNAME"] == "EVENTS_IMAGE" assert hdr["HDUCLASS"] == "OGIP" assert hdr["HDUCLAS1"] == "EVENTS" assert hdr["HDUCLAS2"] == "ACCEPTED" assert "HDUCLAS3" not in hdr assert hdr["HDUVERS"] == "1.0.0" # a few header keywords to check they are handled, # including data types (string, float, integer, # logical). # assert hdr["OBJECT"] == "CSC" assert hdr["INSTRUME"] == "ACIS" assert hdr["GRATING"] == "NONE" assert hdr["DETNAM"] == "ACIS-0" assert hdr["RAND_SKY"] == pytest.approx(0.5) assert hdr["RAND_PI"] == pytest.approx(1.0) assert hdr["DATE-OBS"] == "2006-11-25T09:25:17" assert isinstance(hdr["CLOCKAPP"], (bool, np.bool_)) assert hdr["CLOCKAPP"] assert hdr["TIMEZERO"] == 0 assert "EQUINOX" not in hdr def check_data(obj): """Basic checks of the data""" assert obj.shape == (377, 169) assert obj.staterror is None assert obj.syserror is None assert isinstance(obj.sky, io.wcs.WCS) assert isinstance(obj.eqpos, io.wcs.WCS) assert obj.sky.name == "physical" assert obj.sky.type == "LINEAR" assert obj.sky.crval == pytest.approx([3061.8101, 4332.6299]) assert obj.sky.crpix == pytest.approx([0.5, 0.5]) assert obj.sky.cdelt == pytest.approx([1.0, 1.0]) assert obj.eqpos.name == "world" assert obj.eqpos.type == "WCS" assert obj.eqpos.crval == pytest.approx([149.8853, 2.60795]) assert obj.eqpos.crpix == pytest.approx([4096.5, 4096.5]) cd = 0.492 / 3600 assert obj.eqpos.cdelt == pytest.approx([-cd, cd]) assert obj.eqpos.crota == pytest.approx(0) assert obj.eqpos.epoch == pytest.approx(2000.0) assert obj.eqpos.equinox == pytest.approx(2000.0) assert obj.coord == "logical" assert obj.x0.shape == (63713, ) assert obj.x1.shape == (63713, ) assert obj.y.shape == (63713, ) yexp, xexp = np.mgrid[1:378, 1:170] xexp = xexp.flatten() yexp = yexp.flatten() assert obj.x0 == pytest.approx(xexp) assert obj.x1 == pytest.approx(yexp) vals = obj.y.reshape((377, 169)) expected = np.asarray([0, 1, 0, 0, 0, 0, 1, 0, 0, 2]) assert vals[184:194, 83] == pytest.approx(expected) # What do we expect for the data types? It is backend-specific # assert obj.x0.dtype == np.dtype("float64") assert obj.x1.dtype == np.dtype("float64") if backend_is("crates"): assert obj.y.dtype == np.dtype("float64") elif backend_is("pyfits"): assert obj.y.dtype == np.dtype(">i2") else: pytest.fail("Unrecognized IO backend") infile = make_data_path("acisf07999_000N001_r0035_regevt3_srcimg.fits") indata = io.read_image(infile) assert isinstance(indata, DataIMG) assert indata.name.endswith("/acisf07999_000N001_r0035_regevt3_srcimg.fits") check_header(indata) check_data(indata) # check we can write it out - be explicit with all options # outfile = tmp_path / "test.img" outfile = str(outfile) # our IO routines don't recognize paths io.write_image(outfile, indata, ascii=False, clobber=False) outdata = io.read_image(outfile) assert isinstance(outdata, DataIMG) assert outdata.name.endswith("/test.img") check_header(outdata) check_data(outdata)
def test_save_image_dataimg_fits_wcs(writer, make_data_path, tmp_path): """Does save_image work for a FITS image with WCS We also check the input file, is read in, for fun. dmlist reports that this file has Block 1: EVENTS_IMAGE Image Int2(316x313) Physical Axis Transforms for Image Block EVENTS_IMAGE Group# Axis# 1 1,2 sky(x) = (+2986.8401) +(+1.0)* ((#1)-(+0.50)) (y) (+4362.6602) (+1.0) ((#2) (+0.50)) World Coordinate Axis Transforms for Image Block EVENTS_IMAGE Group# Axis# 1 1,2 EQPOS(RA ) = (+149.8853)[deg] +TAN[(-0.000136667)* (sky(x)-(+4096.50))] (DEC) (+2.6079 ) (+0.000136667) ( (y) (+4096.50)) """ from sherpa.astro.io import read_image from sherpa.astro.io.meta import Meta from sherpa.astro.io.wcs import WCS # It looks like we don't write out the header info, at least for # pyfits. Probably a bug. # def check_data(d, header=True): assert isinstance(d, ui.DataIMG) assert d.shape == (313, 316) assert d.y.shape == (313 * 316, ) assert d.y.sum() == 965 assert d.y.max() == 3 hdr = d.header assert isinstance(hdr, Meta) if header: assert hdr['OBJECT'] == 'CSC' assert hdr['ONTIME'] == pytest.approx(18220.799932122) assert hdr['TSTOP'] == pytest.approx(280770182.77747) assert isinstance(d.sky, WCS) assert d.sky.name == 'physical' assert d.sky.type == 'LINEAR' assert d.sky.crval == pytest.approx([2986.84008789, 4362.66015625]) assert d.sky.crpix == pytest.approx([0.5, 0.5]) assert d.sky.cdelt == pytest.approx([1, 1]) assert isinstance(d.eqpos, WCS) assert d.eqpos.name == 'world' assert d.eqpos.type == 'WCS' assert d.eqpos.epoch == pytest.approx(2000) assert d.eqpos.equinox == pytest.approx(2000) assert d.eqpos.crval == pytest.approx([149.88533198, 2.60794887]) assert d.eqpos.crpix == pytest.approx([4096.5, 4096.5]) assert d.eqpos.cdelt * 3600 == pytest.approx([-0.492, 0.492]) infile = make_data_path('acisf08478_000N001_r0043_regevt3_srcimg.fits') ui.load_data(2, infile) dorig = ui.get_data(2) check_data(dorig) out = tmp_path / "data.dat" outfile = str(out) writer(2, outfile, ascii=False) ans = read_image(outfile) check_data(ans, header=False)