Skip to content

Commit

Permalink
Merge pull request #325 from neutrinoceros/nep18_savetxt
Browse files Browse the repository at this point in the history
ENH: (NEP 18) implement numpy.savetxt
  • Loading branch information
ngoldbaum authored Nov 23, 2022
2 parents 857b0e2 + 0e377b0 commit fdb13f1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ addopts = "--ignore=benchmarks --ignore=paper --ignore=unyt/_mpl_array_converter
filterwarnings = [
"error",
"ignore:Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.:UserWarning",
"ignore:In accordance with NEP 32:DeprecationWarning"
"ignore:In accordance with NEP 32:DeprecationWarning",
"ignore:distutils Version classes are deprecated. Use packaging.version instead.:DeprecationWarning",
]

[tool.coverage.run]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ deps =
setuptools_scm
commands =
# don't do doctests on old numpy versions
pytest --cov=unyt --cov-append --basetemp={envtmpdir} -W once
pytest --cov=unyt --cov-append --basetemp={envtmpdir}
coverage report --omit='.tox/*'

[testenv:py38-dependencies]
Expand Down
16 changes: 16 additions & 0 deletions unyt/_array_functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
from packaging.version import Version

Expand Down Expand Up @@ -605,3 +607,17 @@ def linalg_eigvalsh(a, *args, **kwargs):
np.linalg.eigvalsh._implementation(a.view(np.ndarray), *args, **kwargs)
* a.units
)


@implements(np.savetxt)
def savetxt(fname, X, *args, **kwargs):
warnings.warn(
"numpy.savetxt does not preserve units, "
"and will only save the raw numerical data from the unyt_array object.\n"
"If this is the intended behaviour, call `numpy.savetxt(file, arr.d)` "
"to silence this warning.\n"
"If you want to preserve units, use `unyt.savetxt` "
"(and `unyt.loadtxt`) instead.",
stacklevel=4,
)
return np.savetxt._implementation(fname, X.view(np.ndarray), *args, **kwargs)
20 changes: 19 additions & 1 deletion unyt/tests/test_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@
np.result_type,
np.roots,
np.save,
np.savetxt,
np.savez,
np.savez_compressed,
np.searchsorted,
Expand Down Expand Up @@ -1193,3 +1192,22 @@ def test_eigvals(func):
w = func(a)
assert type(w) is unyt_array
assert w.units == cm


def test_savetxt(tmp_path):
a = [1, 2, 3] * cm
with pytest.raises(
UserWarning,
match=re.escape(
"numpy.savetxt does not preserve units, "
"and will only save the raw numerical data from the unyt_array object.\n"
"If this is the intended behaviour, call `numpy.savetxt(file, arr.d)` "
"to silence this warning.\n"
"If you want to preserve units, use `unyt.savetxt` "
"(and `unyt.loadtxt`) instead."
),
):
np.savetxt(tmp_path / "savefile.npy", a)

# check that doing what the warning says doesn't trigger any other warning
np.savetxt(tmp_path / "savefile.npy", a.d)

0 comments on commit fdb13f1

Please sign in to comment.