Skip to content

Commit

Permalink
resolving issue astropy#15782
Browse files Browse the repository at this point in the history
replaced all calls of:
    assert_equal() -> np.testing.assert_equal()
    assert_almost_equal() -> np.testing.assert_allclose()
    assert_true(a) -> assert a
in the following files:
    astropy/io/ascii/tests/test_c_reader.py
    astropy/io/ascii/tests/test_cds.py
    astropy/io/ascii/tests/test_rst.py
    astropy/io/ascii/tests/test_types.py
    astropy/io/ascii/tests/test_read.py
    astropy/io/ascii/tests/test_fixedwidth.py
    astropy/io/ascii/tests/test_cds_header_from_readme.py
  • Loading branch information
stefanarseneau committed Apr 3, 2024
1 parent 9b5d7fd commit 0a77151
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 230 deletions.
13 changes: 0 additions & 13 deletions astropy/io/ascii/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,3 @@ def setup_function(function):

def teardown_function(function):
os.chdir(CWD)


# Compatibility functions to convert from nose to pytest
def assert_equal(a, b):
assert a == b


def assert_almost_equal(a, b, **kwargs):
assert np.allclose(a, b, **kwargs)


def assert_true(a):
assert a
70 changes: 33 additions & 37 deletions astropy/io/ascii/tests/test_c_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
from astropy.utils.data import get_pkg_data_filename
from astropy.utils.exceptions import AstropyWarning

from .common import assert_almost_equal, assert_equal, assert_true

StringIO = lambda x: BytesIO(x.encode("ascii"))


Expand All @@ -41,24 +39,22 @@ def assert_table_equal(t1, t2, check_meta=False, rtol=1.0e-15, atol=1.0e-300):
Test equality of all columns in a table, with stricter tolerances for
float columns than the np.allclose default.
"""
assert_equal(len(t1), len(t2))
assert_equal(t1.colnames, t2.colnames)
np.testing.assert_equal(len(t1), len(t2))
np.testing.assert_equal(t1.colnames, t2.colnames)
if check_meta:
assert_equal(t1.meta, t2.meta)
np.testing.assert_equal(t1.meta, t2.meta)
for name in t1.colnames:
if len(t1) != 0:
assert_equal(t1[name].dtype.kind, t2[name].dtype.kind)
np.testing.assert_equal(t1[name].dtype.kind, t2[name].dtype.kind)
if not isinstance(t1[name], MaskedColumn):
for i, el in enumerate(t1[name]):
try:
if not isinstance(el, str) and np.isnan(el):
assert_true(
not isinstance(t2[name][i], str) and np.isnan(t2[name][i])
)
assert not isinstance(t2[name][i], str) and np.isnan(t2[name][i])
elif isinstance(el, str):
assert_equal(el, t2[name][i])
np.testing.assert_equal(el, t2[name][i])
else:
assert_almost_equal(el, t2[name][i], rtol=rtol, atol=atol)
np.testing.assert_allclose(el, t2[name][i], rtol=rtol, atol=atol)
except (TypeError, NotImplementedError):
pass # ignore for now

Expand Down Expand Up @@ -324,10 +320,10 @@ def test_conversion(read_basic):
4 2 -12 .4 +.e1 - + six
"""
table = read_basic(text)
assert_equal(table["A"].dtype.kind, "f")
np.testing.assert_equal(table["A"].dtype.kind, "f")
assert table["B"].dtype.kind in ("S", "U")
assert_equal(table["C"].dtype.kind, "i")
assert_equal(table["D"].dtype.kind, "f")
np.testing.assert_equal(table["C"].dtype.kind, "i")
np.testing.assert_equal(table["D"].dtype.kind, "f")
assert table["E"].dtype.kind in ("S", "U")
assert table["F"].dtype.kind in ("S", "U")
assert table["G"].dtype.kind in ("S", "U")
Expand Down Expand Up @@ -677,7 +673,7 @@ def test_fill_values(read_basic):
assert isinstance(table["A"], MaskedColumn)
assert table["A"][0] is ma.masked
# '0' rather than 0 because there is a string in the column
assert_equal(table["A"].data.data[0], "0")
np.testing.assert_equal(table["A"].data.data[0], "0")
assert table["A"][1] is not ma.masked

table = read_basic(text, delimiter=",", fill_values=("-999", "0"))
Expand All @@ -686,7 +682,7 @@ def test_fill_values(read_basic):
assert table["C"][2] is not ma.masked # -9999 is not an exact match
assert table["B"][1] is ma.masked
# Numeric because the rest of the column contains numeric data
assert_equal(table["B"].data.data[1], 0.0)
np.testing.assert_equal(table["B"].data.data[1], 0.0)
assert table["B"][0] is not ma.masked

table = read_basic(text, delimiter=",", fill_values=[])
Expand All @@ -702,11 +698,11 @@ def test_fill_values(read_basic):
assert table["B"][3] is not ma.masked
assert table["A"][0] is ma.masked
assert table["A"][2] is ma.masked
assert_equal(table["A"].data.data[0], "0")
assert_equal(table["A"].data.data[2], "999")
np.testing.assert_equal(table["A"].data.data[0], "0")
np.testing.assert_equal(table["A"].data.data[2], "999")
assert table["C"][0] is ma.masked
assert_almost_equal(table["C"].data.data[0], 999.0)
assert_almost_equal(table["C"][1], -3.4) # column is still of type float
np.testing.assert_allclose(table["C"].data.data[0], 999.0)
np.testing.assert_allclose(table["C"][1], -3.4) # column is still of type float


def test_fill_include_exclude_names(read_csv):
Expand Down Expand Up @@ -796,11 +792,11 @@ def test_read_tab(read_tab):
"""
text = '1\t2\t3\n a\t b \t\n c\t" d\n e"\t '
table = read_tab(text)
assert_equal(table["1"][0], " a") # preserve line whitespace
assert_equal(table["2"][0], " b ") # preserve field whitespace
np.testing.assert_equal(table["1"][0], " a") # preserve line whitespace
np.testing.assert_equal(table["2"][0], " b ") # preserve field whitespace
assert table["3"][0] is ma.masked # empty value should be masked
assert_equal(table["2"][1], " d\n e") # preserve whitespace in quoted fields
assert_equal(table["3"][1], " ") # preserve end-of-line whitespace
np.testing.assert_equal(table["2"][1], " d\n e") # preserve whitespace in quoted fields
np.testing.assert_equal(table["3"][1], " ") # preserve end-of-line whitespace


def test_default_data_start(read_basic):
Expand Down Expand Up @@ -862,9 +858,9 @@ def test_rdb(read_rdb):
table = read_rdb(text)
expected = Table([[1], [" 9"], [4.3]], names=("A", "B", "C"))
assert_table_equal(table, expected)
assert_equal(table["A"].dtype.kind, "i")
np.testing.assert_equal(table["A"].dtype.kind, "i")
assert table["B"].dtype.kind in ("S", "U")
assert_equal(table["C"].dtype.kind, "f")
np.testing.assert_equal(table["C"].dtype.kind, "f")

with pytest.raises(ValueError) as e:
text = "A\tB\tC\nN\tS\tN\n4\tb\ta" # C column contains non-numeric data
Expand Down Expand Up @@ -1056,7 +1052,7 @@ def test_store_comments(read_basic):
4 5 6
"""
table = read_basic(text, check_meta=True)
assert_equal(table.meta["comments"], ["header comment", "comment 2", "comment 3"])
np.testing.assert_equal(table.meta["comments"], ["header comment", "comment 2", "comment 3"])


def test_empty_quotes(read_basic):
Expand Down Expand Up @@ -1189,7 +1185,7 @@ def test_data_out_of_range(fast_reader, guess):
w[i].message
)
read_values = np.array([col[0] for col in t.itercols()])
assert_almost_equal(read_values, values, rtol=rtol, atol=1.0e-324)
np.testing.assert_allclose(read_values, values, rtol=rtol, atol=1.0e-324)

# Test some additional corner cases
fields = [
Expand Down Expand Up @@ -1221,7 +1217,7 @@ def test_data_out_of_range(fast_reader, guess):
w[i].message
)
read_values = np.array([col[0] for col in t.itercols()])
assert_almost_equal(read_values, values, rtol=rtol, atol=1.0e-324)
np.testing.assert_allclose(read_values, values, rtol=rtol, atol=1.0e-324)

# Test corner cases again with non-standard exponent_style (auto-detection)
if fast_reader and fast_reader.get("use_fast_converter"):
Expand Down Expand Up @@ -1253,7 +1249,7 @@ def test_data_out_of_range(fast_reader, guess):
else:
assert len(w) == 3
read_values = np.array([col[0] for col in t.itercols()])
assert_almost_equal(read_values, values, rtol=rtol, atol=1.0e-324)
np.testing.assert_allclose(read_values, values, rtol=rtol, atol=1.0e-324)


@pytest.mark.parametrize("guess", [True, False])
Expand Down Expand Up @@ -1297,15 +1293,15 @@ def test_data_at_range_limit(fast_reader, guess):
guess=guess,
fast_reader=fast_reader,
)
assert_almost_equal(t["col1"][0], 10.0 ** -(D + 1), rtol=rtol, atol=1.0e-324)
np.testing.assert_allclose(t["col1"][0], 10.0 ** -(D + 1), rtol=rtol, atol=1.0e-324)
for D in 99, 202, 308:
t = ascii.read(
StringIO("1" + D * "0" + ".0"),
format="no_header",
guess=guess,
fast_reader=fast_reader,
)
assert_almost_equal(t["col1"][0], 10.0**D, rtol=rtol, atol=1.0e-324)
np.testing.assert_allclose(t["col1"][0], 10.0**D, rtol=rtol, atol=1.0e-324)

# 0.0 is always exact (no Overflow warning)!
for s in "0.0", "0.0e+0", 399 * "0" + "." + 365 * "0":
Expand All @@ -1332,7 +1328,7 @@ def test_data_at_range_limit(fast_reader, guess):
"resulting in degraded precision" in str(w[0].message)
)

assert_almost_equal(t["col1"][0], 1.0e-315, rtol=1.0e-10, atol=1.0e-324)
np.testing.assert_allclose(t["col1"][0], 1.0e-315, rtol=1.0e-10, atol=1.0e-324)


@pytest.mark.parametrize("guess", [True, False])
Expand Down Expand Up @@ -1666,10 +1662,10 @@ def test_conversion_fast(fast_reader):
4 2 -12 .4 +.e1 - + six
"""
table = ascii.read(text, fast_reader=fast_reader)
assert_equal(table["A"].dtype.kind, "f")
np.testing.assert_equal(table["A"].dtype.kind, "f")
assert table["B"].dtype.kind in ("S", "U")
assert_equal(table["C"].dtype.kind, "i")
assert_equal(table["D"].dtype.kind, "f")
np.testing.assert_equal(table["C"].dtype.kind, "i")
np.testing.assert_equal(table["D"].dtype.kind, "f")
assert table["E"].dtype.kind in ("S", "U")
assert table["F"].dtype.kind in ("S", "U")
assert table["G"].dtype.kind in ("S", "U")
Expand Down Expand Up @@ -1711,7 +1707,7 @@ def test_newline_as_delimiter(delimiter, fast_reader):

if not fast_reader:
pytest.xfail("Quoted fields are not parsed correctly by BaseSplitter")
assert_equal(t1["b"].dtype.kind, "i")
np.testing.assert_equal(t1["b"].dtype.kind, "i")


@pytest.mark.parametrize("delimiter", [" ", "|", "\n", "\r"])
Expand Down
4 changes: 1 addition & 3 deletions astropy/io/ascii/tests/test_cds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from astropy.utils.data import get_pkg_data_filename
from astropy.utils.exceptions import AstropyWarning

from .common import assert_almost_equal

test_dat = [
"names e d s i",
"HD81809 1E-7 22.25608 +2 67",
Expand Down Expand Up @@ -503,7 +501,7 @@ def test_write_extra_skycoord_cols():
for a, b in zip(lines[-2:], exp_output[-2:]):
assert a[:18] == b[:18]
assert a[30:42] == b[30:42]
assert_almost_equal(
np.testing.assert_allclose(
np.fromstring(a[2:], sep=" "), np.fromstring(b[2:], sep=" ")
)

Expand Down
64 changes: 31 additions & 33 deletions astropy/io/ascii/tests/test_cds_header_from_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from astropy.io import ascii

from .common import (
assert_almost_equal,
assert_equal,
setup_function, # noqa: F401
teardown_function, # noqa: F401
)
Expand All @@ -34,21 +32,21 @@ def test_description():
data = "data/cds/description/table.dat"
for read_table in (read_table1, read_table2, read_table3):
table = read_table(readme, data)
assert_equal(len(table), 2)
assert_equal(table["Cluster"].description, "Cluster name")
assert_equal(table["Star"].description, "")
assert_equal(table["Wave"].description, "wave ? Wavelength in Angstroms")
assert_equal(table["El"].description, "a")
assert_equal(
np.testing.assert_equal(len(table), 2)
np.testing.assert_equal(table["Cluster"].description, "Cluster name")
np.testing.assert_equal(table["Star"].description, "")
np.testing.assert_equal(table["Wave"].description, "wave ? Wavelength in Angstroms")
np.testing.assert_equal(table["El"].description, "a")
np.testing.assert_equal(
table["ion"].description, "- Ionization stage (1 for neutral element)"
)
assert_equal(
np.testing.assert_equal(
table["loggf"].description,
"log10 of the gf value - logarithm base 10 of stat. weight times "
"oscillator strength",
)
assert_equal(table["EW"].description, "Equivalent width (in mA)")
assert_equal(
np.testing.assert_equal(table["EW"].description, "Equivalent width (in mA)")
np.testing.assert_equal(
table["Q"].description, "DAOSPEC quality parameter Q (large values are bad)"
)

Expand All @@ -58,25 +56,25 @@ def test_multi_header():
data = "data/cds/multi/lhs2065.dat"
for read_table in (read_table1, read_table2, read_table3):
table = read_table(readme, data)
assert_equal(len(table), 18)
assert_almost_equal(table["Lambda"][-1], 6479.32)
assert_equal(table["Fnu"][-1], "0.285937")
np.testing.assert_equal(len(table), 18)
np.testing.assert_allclose(table["Lambda"][-1], 6479.32)
np.testing.assert_equal(table["Fnu"][-1], "0.285937")
data = "data/cds/multi/lp944-20.dat"
for read_table in (read_table1, read_table2, read_table3):
table = read_table(readme, data)
assert_equal(len(table), 18)
assert_almost_equal(table["Lambda"][0], 6476.09)
assert_equal(table["Fnu"][-1], "0.489005")
np.testing.assert_equal(len(table), 18)
np.testing.assert_allclose(table["Lambda"][0], 6476.09)
np.testing.assert_equal(table["Fnu"][-1], "0.489005")


def test_glob_header():
readme = "data/cds/glob/ReadMe"
data = "data/cds/glob/lmxbrefs.dat"
for read_table in (read_table1, read_table2, read_table3):
table = read_table(readme, data)
assert_equal(len(table), 291)
assert_equal(table["Name"][-1], "J1914+0953")
assert_equal(table["BibCode"][-2], "2005A&A...432..235R")
np.testing.assert_equal(len(table), 291)
np.testing.assert_equal(table["Name"][-1], "J1914+0953")
np.testing.assert_equal(table["BibCode"][-2], "2005A&A...432..235R")


def test_header_from_readme():
Expand Down Expand Up @@ -204,7 +202,7 @@ def test_cds_function_units2(reader_cls):
assert table["vturb"].unit == u.km / u.s
assert table["[Fe/H]"].unit == u.dex(u.one)
assert table["e_[Fe/H]"].unit == u.dex(u.one)
assert_almost_equal(
np.testing.assert_allclose(
table["[Fe/H]"].to(u.one), 10.0 ** (np.array([-2.07, -1.50, -2.11, -1.64]))
)

Expand All @@ -216,9 +214,9 @@ def test_cds_ignore_nullable():
data = "data/cds/null/table.dat"
r = ascii.Cds(readme)
r.read(data)
assert_equal(r.header.cols[6].description, "Temperature class codified (10)")
assert_equal(r.header.cols[8].description, "Luminosity class codified (11)")
assert_equal(r.header.cols[5].description, "Pericenter position angle (18)")
np.testing.assert_equal(r.header.cols[6].description, "Temperature class codified (10)")
np.testing.assert_equal(r.header.cols[8].description, "Luminosity class codified (11)")
np.testing.assert_equal(r.header.cols[5].description, "Pericenter position angle (18)")


def test_cds_no_whitespace():
Expand All @@ -228,15 +226,15 @@ def test_cds_no_whitespace():
data = "data/cds/null/table.dat"
r = ascii.Cds(readme)
r.read(data)
assert_equal(r.header.cols[6].description, "Temperature class codified (10)")
assert_equal(r.header.cols[6].null, "")
assert_equal(r.header.cols[7].description, "Equivalent width (in mA)")
assert_equal(r.header.cols[7].null, "-9.9")
assert_equal(
np.testing.assert_equal(r.header.cols[6].description, "Temperature class codified (10)")
np.testing.assert_equal(r.header.cols[6].null, "")
np.testing.assert_equal(r.header.cols[7].description, "Equivalent width (in mA)")
np.testing.assert_equal(r.header.cols[7].null, "-9.9")
np.testing.assert_equal(
r.header.cols[10].description,
"DAOSPEC quality parameter Q (large values are bad)",
)
assert_equal(r.header.cols[10].null, "-9.999")
np.testing.assert_equal(r.header.cols[10].null, "-9.999")


def test_cds_order():
Expand All @@ -246,6 +244,6 @@ def test_cds_order():
data = "data/cds/null/table.dat"
r = ascii.Cds(readme)
r.read(data)
assert_equal(r.header.cols[5].description, "Catalogue Identification Number")
assert_equal(r.header.cols[8].description, "Equivalent width (in mA)")
assert_equal(r.header.cols[9].description, "Luminosity class codified (11)")
np.testing.assert_equal(r.header.cols[5].description, "Catalogue Identification Number")
np.testing.assert_equal(r.header.cols[8].description, "Equivalent width (in mA)")
np.testing.assert_equal(r.header.cols[9].description, "Luminosity class codified (11)")

0 comments on commit 0a77151

Please sign in to comment.