def test_tablediff(): """ Test diff-ing two simple Table objects. """ a = Table.read("""name obs_date mag_b mag_v M31 2012-01-02 17.0 16.0 M82 2012-10-29 16.2 15.2 M101 2012-10-31 15.1 15.5""", format='ascii') b = Table.read("""name obs_date mag_b mag_v M31 2012-01-02 17.0 16.5 M82 2012-10-29 16.2 15.2 M101 2012-10-30 15.1 15.5 NEW 2018-05-08 nan 9.0""", format='ascii') f = io.StringIO() identical = report_diff_values(a, b, fileobj=f) assert not identical out = f.getvalue() assert out == (' name obs_date mag_b mag_v\n' ' ---- ---------- ----- -----\n' ' a> M31 2012-01-02 17.0 16.0\n' ' ? ^\n' ' b> M31 2012-01-02 17.0 16.5\n' ' ? ^\n' ' M82 2012-10-29 16.2 15.2\n' ' a> M101 2012-10-31 15.1 15.5\n' ' ? ^\n' ' b> M101 2012-10-30 15.1 15.5\n' ' ? ^\n' ' b> NEW 2018-05-08 nan 9.0\n') # Identical assert report_diff_values(a, a, fileobj=f)
def test_get_epic_metadata(self): tap_url = "http://nxsadev.esac.esa.int/tap-server/tap/" target_name = "4XMM J122934.7+015657" radius = 0.01 epic_source_table = "xsa.v_epic_source" epic_source_column = "epic_source_equatorial_spoint" cat_4xmm_table = "xsa.v_epic_source_cat" cat_4xmm_column = "epic_source_cat_equatorial_spoint" stack_4xmm_table = "xsa.v_epic_xmm_stack_cat" stack_4xmm_column = "epic_stack_cat_equatorial_spoint" slew_source_table = "xsa.v_slew_source_cat" slew_source_column = "slew_source_cat_equatorial_spoint" xsa = XMMNewtonClass(TapPlus(url=tap_url)) epic_source, cat_4xmm, stack_4xmm, slew_source = xsa.get_epic_metadata( target_name=target_name, radius=radius) c = SkyCoord.from_name(target_name, parse=True) query = ("select * from {} " "where 1=contains({}, circle('ICRS', {}, {}, {}));") table = xsa.query_xsa_tap( query.format(epic_source_table, epic_source_column, c.ra.degree, c.dec.degree, radius)) assert report_diff_values(epic_source, table) table = xsa.query_xsa_tap( query.format(cat_4xmm_table, cat_4xmm_column, c.ra.degree, c.dec.degree, radius)) assert report_diff_values(cat_4xmm, table) table = xsa.query_xsa_tap( query.format(stack_4xmm_table, stack_4xmm_column, c.ra.degree, c.dec.degree, radius)) assert report_diff_values(stack_4xmm, table) table = xsa.query_xsa_tap( query.format(slew_source_table, slew_source_column, c.ra.degree, c.dec.degree, radius)) assert report_diff_values(slew_source, table)
def test_exec_sync(): # save results in a file # create the VOTable result # example from http://docs.astropy.org/en/stable/io/votable/ votable = VOTableFile() resource = Resource() votable.resources.append(resource) table = Table(votable) resource.tables.append(table) table.fields.extend([ Field(votable, name="filename", datatype="char", arraysize="*"), Field(votable, name="matrix", datatype="double", arraysize="2x2")]) table.create_arrays(2) table.array[0] = ('test1.xml', [[1, 0], [0, 1]]) table.array[1] = ('test2.xml', [[0.5, 0.3], [0.2, 0.1]]) buffer = BytesIO() votable.to_xml(buffer) cadc = Cadc(auth_session=requests.Session()) response = Mock() response.to_table.return_value = buffer.getvalue() cadc.cadctap.search = Mock(return_value=response) output_file = '{}/test_vooutput.xml'.format(tempfile.tempdir) cadc.exec_sync('some query', output_file=output_file) actual = parse(output_file) assert len(votable.resources) == len(actual.resources) == 1 assert len(votable.resources[0].tables) ==\ len(actual.resources[0].tables) == 1 actual_table = actual.resources[0].tables[0] try: # TODO remove when astropy LTS upgraded from astropy.utils.diff import report_diff_values assert report_diff_values(table, actual_table, fileobj=sys.stdout) except ImportError: pass
def test_calc_deltas(engdb, data_path): """Test `calc_deltas` basic running""" model = dm.ImageModel(data_path) deltas = ps.calc_deltas([model]) truth = Table.read(DATA_PATH / 'calc_deltas_truth.ecsv') assert report_diff_values(truth, deltas, fileobj=sys.stderr)
def test_dl2(tmp_path, dl2_shower_geometry_file, dl2_proton_geometry_file): from ctapipe.tools.merge import MergeTool output = tmp_path / "merged.dl2.h5" ret = run_tool( MergeTool(), argv=[ f"--output={output}", str(dl2_shower_geometry_file), str(dl2_proton_geometry_file), ], ) assert ret == 0, f"Running merge for dl2 files failed with exit code {ret}" table1 = read_table(dl2_shower_geometry_file, "/dl2/event/subarray/geometry/HillasReconstructor") table2 = read_table(dl2_proton_geometry_file, "/dl2/event/subarray/geometry/HillasReconstructor") table_merged = read_table( output, "/dl2/event/subarray/geometry/HillasReconstructor") diff = StringIO() identical = report_diff_values(vstack([table1, table2]), table_merged, fileobj=diff) assert ( identical ), f"Merged table not equal to individual tables. Diff:\n {diff.getvalue()}"
def test_load(self): name = "ic2395" file = name + ".vot" original_table = load_remote( table="gaiadr2.gaia_source", name="ic2395", radius=u.Quantity("30", u.arcminute), limit=55, columns=["ra", "dec", "pmra", "pmdec", "phot_g_mean_mag"], filters={"phot_g_mean_mag": "<12"}, ) load_remote( name="ic2395", radius=u.Quantity("30", u.arcminute), limit=55, columns=["ra", "dec", "pmra", "pmdec", "phot_g_mean_mag"], filters={"phot_g_mean_mag": "<12"}, dump_to_file=True, output_file=name + ".vot", ) loaded_table = load_file(file) identical = report_diff_values(original_table, loaded_table) assert identical if os.path.exists(file): os.remove(file)
def assertAstropyTablesEqual(self, tables, expectedTables): """Verify that a list of astropy tables matches a list of expected astropy tables. Parameters ---------- tables : `astropy.table.Table` or iterable [`astropy.table.Table`] The table or tables that should match the expected tables. expectedTables : `astropy.table.Table` or iterable [`astropy.table.Table`] The tables with expected values to which the tables under test will be compared. """ # If a single table is passed in for tables or expectedTables, put it # in a list. if isinstance(tables, AstropyTable): tables = [tables] if isinstance(expectedTables, AstropyTable): expectedTables = [expectedTables] diff = io.StringIO() self.assertEqual(len(tables), len(expectedTables)) for table, expected in zip(tables, expectedTables): # Assert that we are testing what we think we are testing: self.assertIsInstance(table, AstropyTable) self.assertIsInstance(expected, AstropyTable) # Assert that they match: self.assertTrue(report_diff_values(table, expected, fileobj=diff), msg="\n" + diff.getvalue())
def test_over_tiome(engdb): """Test v1_calculate_over_time for basic running""" v1_table = v1c.v1_calculate_over_time( Time(GOOD_STARTTIME).mjd, Time(GOOD_ENDTIME).mjd) v1_formatted = v1c.simplify_table(v1_table) truth = Table.read(DATA_PATH / 'v1_time_truth.ecsv') assert report_diff_values(truth, v1_formatted, fileobj=sys.stderr)
def get_astropy_diff(self): report = None if report_diff_values: s = StringIO() same = report_diff_values(self.table, self.table2, s) if not same: s.seek(0) report = ''.join(s.readlines()) s.close() return report
def test_from_models(engdb): """Test v1_calculate_from_models for basic running""" model = ImageModel() model.meta.exposure.start_time = Time(GOOD_STARTTIME).mjd model.meta.exposure.end_time = Time(GOOD_ENDTIME).mjd v1_table = v1c.v1_calculate_from_models([model]) v1_formatted = v1c.simplify_table(v1_table) truth = Table.read(DATA_PATH / 'v1_calc_truth.ecsv') assert report_diff_values(truth, v1_formatted, fileobj=sys.stderr)
def test_diff_types(): """ Regression test for https://github.com/astropy/astropy/issues/4122 """ f = io.StringIO() a = 1.0 b = '1.0' identical = report_diff_values(a, b, fileobj=f) assert not identical out = f.getvalue() assert out == (" (float) a> 1.0\n" " (str) b> '1.0'\n" " ? + +\n")
def test_diff_shaped_array_comparison(): """ Test diff-ing two differently shaped arrays. """ f = io.StringIO() a = np.empty((1, 2, 3)) identical = report_diff_values(a, a[0], fileobj=f) assert not identical out = f.getvalue() assert out == (' Different array shapes:\n' ' a> (1, 2, 3)\n' ' ? ---\n' ' b> (2, 3)\n')
def test_calc_deltas(engdb, data_path): """Test `calc_deltas` basic running""" with ImageModel(data_path) as model: deltas = ps.calc_deltas([model]) truth = Table.read(DATA_PATH / 'calc_deltas_truth.ecsv') # round the delta values to a reasonable level deltas[0][4] = round(deltas[0][4], 8) deltas[0][5] = round(deltas[0][5], 8) truth[0][4] = round(truth[0][4], 8) truth[0][5] = round(truth[0][5], 8) assert report_diff_values(truth, deltas, fileobj=sys.stderr)
def test_float_comparison(): """ Regression test for https://github.com/spacetelescope/PyFITS/issues/21 """ f = io.StringIO() a = np.float32(0.029751372) b = np.float32(0.029751368) identical = report_diff_values(a, b, fileobj=f) assert not identical out = f.getvalue() # This test doesn't care about what the exact output is, just that it # did show a difference in their text representations assert 'a>' in out assert 'b>' in out
def test_loadVOTable(self): name = "ic2395" file = "./tests/" + name + ".vot" original_table = cone_search(name=name, radius=0.1, row_limit=80) cone_search( name=name, radius=0.1, output_file=file, dump_to_file=True, row_limit=80, ) loaded_table = load_VOTable(file) identical = report_diff_values(original_table, loaded_table) assert len(loaded_table) == 80 assert identical if os.path.exists(file): os.remove(file)
def test_array_comparison(): """ Test diff-ing two arrays. """ f = io.StringIO() a = np.arange(9).reshape(3, 3) b = a + 1 identical = report_diff_values(a, b, fileobj=f) assert not identical out = f.getvalue() assert out == (' at [0, 0]:\n' ' a> 0\n' ' b> 1\n' ' at [0, 1]:\n' ' a> 1\n' ' b> 2\n' ' at [0, 2]:\n' ' a> 2\n' ' b> 3\n' ' ...and at 6 more indices.\n')
def test_match(self): cat1 = ascii.read('cat1.csv', delimiter=',', guess=False, fast_reader=False) cat2 = ascii.read('cat2.csv', delimiter=',', guess=False, fast_reader=False) table = crossmatch.CrossMatch.crossmatch(cat1, cat2, 'RA1', 'DEC1', 'Z1', 'RA2', 'DEC2', 'Z2', 20, use_z=True) final_table = table['NAME1', 'NAME2', 'RA1', 'RA2', 'DEC1', 'DEC2', 'DELTA_Z'] cat3 = ascii.read('cat3.csv') self.assertTrue(report_diff_values(cat3, final_table))
def test_get_records(engdb): """Test getting records""" records = engdb._get_records(*QUERY) assert engdb.response.text == EXPECTED_RESPONSE assert report_diff_values(records, EXPECTED_RECORDS)
def test_diff_numeric_scalar_types(): """ Test comparison of different numeric scalar types. """ f = io.StringIO() assert not report_diff_values(1.0, 1, fileobj=f) out = f.getvalue() assert out == ' (float) a> 1.0\n (int) b> 1\n'