Skip to content

Commit 68e0005

Browse files
Raise a clear ValueError when writing NaN or inf values to MRT (#20723)
* TST: add a regression test for gh-17840 * BUG: fix a useless error message when attempting to export a table containing NaNs or infs to MRT
1 parent 5666012 commit 68e0005

4 files changed

Lines changed: 78 additions & 3 deletions

File tree

‎astropy/io/ascii/mrt.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,29 @@ def write_byte_by_byte(self):
265265
names=["Bytes", "Format", "Units", "Label", "Explanations"], dtype=[str] * 5
266266
)
267267

268-
# Iterate over the columns to write Byte-By-Byte rows.
269-
for i, col in enumerate(self.cols):
268+
# Invalidate non MRT-compliant values early
269+
have_nonfinite: list[str] = []
270+
for col in self.cols:
270271
# Check if column is MaskedColumn
271-
col.has_null = isinstance(col, MaskedColumn)
272+
if isinstance(col, MaskedColumn):
273+
col.has_null = True
274+
col_data = col.data.data[~col.mask]
275+
else:
276+
col.has_null = False
277+
col_data = col.data
278+
279+
if col.dtype.kind in "fc" and not np.all(np.isfinite(col_data)):
280+
have_nonfinite.append(col.name)
272281

282+
if have_nonfinite:
283+
raise ValueError(
284+
"The following columns contain NaNs or infs, "
285+
f"which are not allowed in MRT: {', '.join(have_nonfinite)}\n"
286+
"Mask these values to write them as null (blank) entries."
287+
)
288+
289+
# Iterate over the columns to write Byte-By-Byte rows.
290+
for i, col in enumerate(self.cols):
273291
if col.format is not None:
274292
col.formatted_width = max(len(sval) for sval in col.str_vals)
275293

‎astropy/io/ascii/tests/test_cds.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,54 @@ def test_write_null_data_values():
153153
assert lines == exp_output
154154

155155

156+
@pytest.mark.parametrize(
157+
"val",
158+
[float("nan"), np.nan, float("+inf"), np.inf, float("-inf"), -np.inf],
159+
)
160+
def test_write_non_finite_values_to_mrt(val):
161+
# see https://github.com/astropy/astropy/issues/17840
162+
out = StringIO()
163+
data = {"A": [0, val], "B": [val, 1]}
164+
t = Table(data)
165+
expected = (
166+
r"^The following columns contain NaNs or infs, "
167+
r"which are not allowed in MRT\: {columns}\n"
168+
r"Mask these values to write them as null \(blank\) entries\.$"
169+
)
170+
with pytest.raises(ValueError, match=expected.format(columns="A, B")):
171+
t.write(out, format="ascii.mrt")
172+
173+
t_ma_a = Table(
174+
data={
175+
"A": MaskedColumn(data["A"], mask=[False, True]),
176+
"B": data["B"],
177+
}
178+
)
179+
with pytest.raises(ValueError, match=expected.format(columns="B")):
180+
t_ma_a.write(out, format="ascii.mrt")
181+
182+
t_ma_b = Table(
183+
data={
184+
"A": data["A"],
185+
"B": MaskedColumn(data["B"], mask=[True, False]),
186+
}
187+
)
188+
with pytest.raises(ValueError, match=expected.format(columns="A")):
189+
t_ma_b.write(out, format="ascii.mrt")
190+
191+
t_ma = Table(
192+
data={
193+
"A": MaskedColumn(data["A"], mask=[False, True]),
194+
"B": MaskedColumn(data["B"], mask=[True, False]),
195+
}
196+
)
197+
t_ma.write(out, format="ascii.mrt")
198+
assert out.getvalue().splitlines()[-2:] == [
199+
"0.0 ",
200+
" 1.0",
201+
]
202+
203+
156204
def test_write_byte_by_byte_for_masked_column():
157205
"""
158206
This test differs from the ``test_write_null_data_values``
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Writing a table to MRT now raises a ``ValueError`` if any float column
2+
contains NaN or infinite values, since these are not allowed by the
3+
MRT format. Previously such tables were written whenever the first
4+
value of the column was finite, producing non-compliant ``nan``
5+
entries. To write missing values, use a masked column; masked entries
6+
are written as MRT null (blank) values.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed an unhelpful ``ValueError`` or ``OverflowError`` raised when
2+
writing a table to MRT where the first value of a float column was NaN
3+
or infinite.

0 commit comments

Comments
 (0)