def fits_osiris_allclose(a, b): """Assert that two OSIRIS fits files are close.""" a = fits.open(a) b = fits.open(b) try: del a[0].header['COMMENT'] del b[0].header['COMMENT'] report = StringIO() diff = FITSDiff( a, b, ignore_keywords=["COMMENT"], ignore_comments=["SIMPLE"], ignore_fields=[], ignore_blanks=True, ignore_blank_cards=True, tolerance=1e-5) diff.report(fileobj=report) assert diff.identical, report.getvalue() finally: a.close() b.close()
def test_partially_identical_files3(self): """ Test files that have some identical HDUs but a different extension name. """ phdu = PrimaryHDU() ehdu = ImageHDU(name='FOO') hdula = HDUList([phdu, ehdu]) ehdu = BinTableHDU(name='BAR') ehdu.header['EXTVER'] = 2 ehdu.header['EXTLEVEL'] = 3 hdulb = HDUList([phdu, ehdu]) diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdus[0][0] == 1 hdu_diff = diff.diff_hdus[0][1] assert hdu_diff.diff_extension_types == ('IMAGE', 'BINTABLE') assert hdu_diff.diff_extnames == ('FOO', 'BAR') assert hdu_diff.diff_extvers == (1, 2) assert hdu_diff.diff_extlevels == (1, 3) report = diff.report() assert 'Extension types differ' in report assert 'a: IMAGE\n b: BINTABLE' in report assert 'Extension names differ' in report assert 'a: FOO\n b: BAR' in report assert 'Extension versions differ' in report assert 'a: 1\n b: 2' in report assert 'Extension levels differ' in report assert 'a: 1\n b: 2' in report
def pruebas_varias(): # from subprocess import Popen, PIPE # resultado = Popen(["fitsdiff", "-k", "filename,filtnam1,NUM-BS,NUM-OVPE,NUM-TRIM", "-n 1", "-d 1.e-1", "arc_rss.fits", "cArcCalibrationRecipe_arc_rss.fits" ], stdout=PIPE, stderr=PIPE) # if "Data contains differences" in resultado.stdout.read(): # print "Hay Diferencias" # else: # print "Son iguales" # print "*" * 40 # ejemplo = FITSDiff( "arc_rss.fits", "cArcCalibrationRecipe_arc_rss.fits",["filename","filtnam1","NUM-BS","NUM-OVPE","NUM-TRIM"]) ejemplo = FITSDiff( "arc_rss.fits", "cArcCalibrationRecipe_arc_rss.fits",["NUM-BS","NUM-OVPE"]) print ejemplo.report() if "Data contains differences" in ejemplo.report(): print "Hay Diferencias" else: print "Son iguales" print "*" * 40 # hd = HeaderDiff.fromdiff(ejemplo, "arc_rss.fits", "cArcCalibrationRecipe_arc_rss.fits") hd = HeaderDiff.fromdiff(ejemplo, "arc_image.fits", "cArcCalibrationRecipe_arc_rss.fits") print "Cabeceras identicas: ", hd.identical
def compare(cls, reference_file, test_file, atol=None, rtol=None): import astropy from astropy.io.fits.diff import FITSDiff from astropy.utils.introspection import minversion if minversion(astropy, '2.0'): diff = FITSDiff(reference_file, test_file, rtol=rtol) else: diff = FITSDiff(reference_file, test_file, tolerance=rtol) return diff.identical, diff.report()
def test_partially_identical_files2(self): """ Test files that have some identical HDUs but one different HDU. """ a = np.arange(100).reshape(10, 10) phdu = PrimaryHDU(data=a) ehdu = ImageHDU(data=a) ehdu2 = ImageHDU(data=(a + 1)) hdula = HDUList([phdu, ehdu, ehdu]) hdulb = HDUList([phdu, ehdu2, ehdu]) diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdu_count == () assert len(diff.diff_hdus) == 1 assert diff.diff_hdus[0][0] == 1 hdudiff = diff.diff_hdus[0][1] assert not hdudiff.identical assert hdudiff.diff_extnames == () assert hdudiff.diff_extvers == () assert hdudiff.diff_extension_types == () assert hdudiff.diff_headers.identical assert hdudiff.diff_data is not None datadiff = hdudiff.diff_data assert isinstance(datadiff, ImageDataDiff) assert not datadiff.identical assert datadiff.diff_dimensions == () assert (datadiff.diff_pixels == [((0, y), (y, y + 1)) for y in range(10)]) assert datadiff.diff_ratio == 1.0 assert datadiff.diff_total == 100 report = diff.report() # Primary HDU and 2nd extension HDU should have no differences assert 'Primary HDU' not in report assert 'Extension HDU 2' not in report assert 'Extension HDU 1' in report assert 'Headers contain differences' not in report assert 'Data contains differences' in report for y in range(10): assert 'Data differs at [{}, 1]'.format(y + 1) in report assert '100 different pixels found (100.00% different).' in report
def test_identical_files_basic(self): """Test identicality of two simple, extensionless files.""" a = np.arange(100).reshape(10, 10) hdu = PrimaryHDU(data=a) hdu.writeto(self.temp('testa.fits')) hdu.writeto(self.temp('testb.fits')) diff = FITSDiff(self.temp('testa.fits'), self.temp('testb.fits')) assert diff.identical report = diff.report() # Primary HDUs should contain no differences assert 'Primary HDU' not in report assert 'Extension HDU' not in report assert 'No differences found.' in report a = np.arange(10) ehdu = ImageHDU(data=a) diff = HDUDiff(ehdu, ehdu) assert diff.identical report = diff.report() assert 'No differences found.' in report
def test_partially_identical_files1(self): """ Test files that have some identical HDUs but a different extension count. """ a = np.arange(100).reshape(10, 10) phdu = PrimaryHDU(data=a) ehdu = ImageHDU(data=a) hdula = HDUList([phdu, ehdu]) hdulb = HDUList([phdu, ehdu, ehdu]) diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdu_count == (2, 3) # diff_hdus should be empty, since the third extension in hdulb # has nothing to compare against assert diff.diff_hdus == [] report = diff.report() assert 'Files contain different numbers of HDUs' in report assert 'a: 2\n b: 3' in report assert 'No differences found between common HDUs' in report
def test_nircam_setpointing(_jail, rtdata, fitsdiff_default_kwargs): """ Regression test of the set_telescope_pointing script on a level-1b NIRCam file. """ # Get SIAF PRD database file siaf_path = rtdata.get_data("common/prd.db") rtdata.get_data( "nircam/tsgrism/jw00721012001_03103_00001-seg001_nrcalong_uncal.fits") # The add_wcs function overwrites its input rtdata.output = rtdata.input # Call the WCS routine, using the ENGDB_Service add_wcs(rtdata.input, siaf_path=siaf_path, engdb_url=engdb_tools.ENGDB_BASE_URL) rtdata.get_truth( "truth/test_nircam_setpointing/jw00721012001_03103_00001-seg001_nrcalong_uncal.fits" ) fitsdiff_default_kwargs['rtol'] = 1e-6 diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_nirspec_ifu_mbkg_user(rtdata, fitsdiff_default_kwargs): """Test NIRSpec IFU data with a user-supplied background file.""" # Get user-supplied background user_background = "prism_bkg_x1d.fits" rtdata.get_data(f"nirspec/ifu/{user_background}") # Get input data rtdata.get_data("nirspec/ifu/prism_sci_bkg_cal.fits") MasterBackgroundStep.call(rtdata.input, user_background=user_background, save_results=True, suffix='master_background') output = "prism_sci_bkg_master_background.fits" rtdata.output = output # Get the truth file rtdata.get_truth(f"truth/test_nirspec_ifu_mbkg_user/{output}") # Compare the results diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_identical(orig_fits, current_fits): error = False for cfits in current_fits: # find corresponding original file try: ofits = next(ofits for ofits in orig_fits if ofits.name == cfits.name) except StopIteration: error = True warnings.warn( f"no corresponding file found for {cfits} in the original fits files" ) continue diff = FITSDiff( ofits, cfits, ignore_keywords=['CHECKSUM', 'DATASUM', 'DATE', 'VERS_SW']) if not diff.identical: error = True warnings.warn(diff.report()) if error: raise ValueError( "one or many errors\nthere are differentses in FITS files")
def fitsdiff(input1, input2, comment_excl_list='', value_excl_list='', field_excl_list='', maxdiff=10, delta=0.0, neglect_blanks=True, output=None): if isinstance(comment_excl_list, string_types): comment_excl_list = list_parse(comment_excl_list) if isinstance(value_excl_list, string_types): value_excl_list = list_parse(value_excl_list) if isinstance(field_excl_list, string_types): field_excl_list = list_parse(field_excl_list) diff = FITSDiff(input1, input2, ignore_keywords=value_excl_list, ignore_comments=comment_excl_list, ignore_fields=field_excl_list, numdiffs=maxdiff, tolerance=delta, ignore_blanks=neglect_blanks) if output is None: output = sys.stdout diff.report(output) return diff.identical
def test_nirspec_fs_mbkg_user(rtdata, fitsdiff_default_kwargs): """Run a test for NIRSpec FS data with a user-supplied background file.""" # Get user-supplied background user_background = "v2_nrs_bkg_user_clean_x1d.fits" rtdata.get_data(f"nirspec/fs/{user_background}") # Get input data rtdata.get_data("nirspec/fs/nrs_sci+bkg_cal.fits") MasterBackgroundStep.call(rtdata.input, save_results=True, suffix='master_background', user_background=user_background) output = "nrs_sci+bkg_master_background.fits" rtdata.output = output # Get the truth file rtdata.get_truth(f"truth/test_nirspec_fs_mbkg_user/{output}") # Compare the results diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_nirspec_fs_mbkg_user(rtdata, fitsdiff_default_kwargs): """Run a test for NIRSpec FS data with a user-supplied background file.""" # Get user-supplied background user_background = "v2_nrs_bkg_user_clean_x1d.fits" rtdata.get_data(f"nirspec/fs/{user_background}") # Get input data rtdata.get_data("nirspec/fs/nrs_sci+bkg_cal.fits") collect_pipeline_cfgs("config") args = ["config/master_background.cfg", rtdata.input, "--user_background", user_background] Step.from_cmdline(args) output = "nrs_sci+bkg_master_background.fits" rtdata.output = output # Get the truth file rtdata.get_truth(f"truth/test_nirspec_fs_mbkg_user/{output}") # Compare the results diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs) assert diff.identical, diff.report()
def test_diff_empty_tables(self): """ Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/178 Ensure that diffing tables containing empty data doesn't crash. """ c1 = Column('D', format='J') c2 = Column('E', format='J') thdu = BinTableHDU.from_columns([c1, c2], nrows=0) hdula = fits.HDUList([thdu]) hdulb = fits.HDUList([thdu]) diff = FITSDiff(hdula, hdulb) assert diff.identical
def test_identical_comp_image_hdus(self): """Regression test for https://aeon.stsci.edu/ssb/trac/pyfits/ticket/189 For this test we mostly just care that comparing to compressed images does not crash, and returns the correct results. Two compressed images will be considered identical if the decompressed data is the same. Obviously we test whether or not the same compression was used by looking for (or ignoring) header differences. """ data = np.arange(100.0).reshape(10, 10) hdu = fits.CompImageHDU(data=data) hdu.writeto(self.temp('test.fits')) hdula = fits.open(self.temp('test.fits')) hdulb = fits.open(self.temp('test.fits')) diff = FITSDiff(hdula, hdulb) assert diff.identical
def test_ignore_hdus(self): a = np.arange(100).reshape(10, 10) b = a.copy() ha = Header([('A', 1), ('B', 2), ('C', 3)]) xa = np.array([(1.0, 1), (3.0, 4)], dtype=[('x', float), ('y', int)]) xb = np.array([(1.0, 2), (3.0, 5)], dtype=[('x', float), ('y', int)]) phdu = PrimaryHDU(header=ha) ihdua = ImageHDU(data=a, name='SCI') ihdub = ImageHDU(data=b, name='SCI') bhdu1 = BinTableHDU(data=xa, name='ASDF') bhdu2 = BinTableHDU(data=xb, name='ASDF') hdula = HDUList([phdu, ihdua, bhdu1]) hdulb = HDUList([phdu, ihdub, bhdu2]) # ASDF extension should be different diff = FITSDiff(hdula, hdulb) assert not diff.identical assert diff.diff_hdus[0][0] == 2 # ASDF extension should be ignored diff = FITSDiff(hdula, hdulb, ignore_hdus=['ASDF']) assert diff.identical, diff.report() diff = FITSDiff(hdula, hdulb, ignore_hdus=['ASD*']) assert diff.identical, diff.report() # SCI extension should be different hdulb['SCI'].data += 1 diff = FITSDiff(hdula, hdulb, ignore_hdus=['ASDF']) assert not diff.identical # SCI and ASDF extensions should be ignored diff = FITSDiff(hdula, hdulb, ignore_hdus=['SCI', 'ASDF']) assert diff.identical, diff.report() # All EXTVER of SCI should be ignored ihduc = ImageHDU(data=a, name='SCI', ver=2) hdulb.append(ihduc) diff = FITSDiff(hdula, hdulb, ignore_hdus=['SCI', 'ASDF']) assert not any(diff.diff_hdus), diff.report() assert any(diff.diff_hdu_count), diff.report()
def compare(cls, reference_file, test_file, atol=None, rtol=None): from astropy.io.fits.diff import FITSDiff diff = FITSDiff(reference_file, test_file, tolerance=rtol) return diff.identical, diff.report()