Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
* Enabled support of Python 3.14 [#2631](https://github.com/IntelPython/dpnp/pull/2631)
* Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652)
* Added implementation of `dpnp.frexp` [#2635](https://github.com/IntelPython/dpnp/pull/2635)
* Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635)

### Changed

Expand Down
34 changes: 33 additions & 1 deletion dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2013,7 +2013,39 @@ def to_device(self, device, /, *, stream=None):
return dpnp_array._create_from_usm_ndarray(usm_res)

# 'tobytes',
# 'tofile',

def tofile(self, fid, sep="", format=""):
"""
Writes the array to a file as text or binary (default).

For full documentation refer to :obj:`numpy.ndarray.tofile`.

Parameters
----------
fid : {file. str, path}
An open file object, or a string containing a filename.
sep : str, optional
Separator between array items for text output. If ``""`` (empty),
a binary file is written.

Default: ``""``.
format : str, optional
Format string for text file output (when non-empty `sep` is passed).
Each entry in the array is formatted to text by first converting it
to the closest Python type, and then using ``format % item``. If
``""`` (empty), no formatting is used while converting to the
string.

Default: ``""``.

See Also
--------
:obj:`dpnp.fromfile` : Construct an array from data in a text or binary
file.

"""

self.asnumpy().tofile(fid, sep=sep, format=format)

def tolist(self):
"""
Expand Down
59 changes: 59 additions & 0 deletions dpnp/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dpnp

from .helper import (
generate_random_numpy_array,
get_abs_array,
get_all_dtypes,
get_complex_dtypes,
Expand Down Expand Up @@ -107,6 +108,64 @@ def test_strides(self):
assert xp.full_like(a, fill_value=6) not in a


class TestToFile:
def _create_data(self):
x = generate_random_numpy_array((2, 4, 3), dtype=complex)
x[0, :, 1] = [numpy.nan, numpy.inf, -numpy.inf, numpy.nan]
return dpnp.array(x)

@pytest.fixture(params=["string", "path_obj"])
def tmp_filename(self, tmp_path, request):
# This fixture covers two cases:
# one where the filename is a string and
# another where it is a pathlib object
filename = tmp_path / "file"
if request.param == "string":
filename = str(filename)
yield filename

def test_roundtrip_file(self, tmp_filename):
a = self._create_data()

with open(tmp_filename, "wb") as f:
a.tofile(f)

# reconstruct the array back from the file
with open(tmp_filename, "rb") as f:
b = dpnp.fromfile(f, dtype=a.dtype)
assert_array_equal(b, a.asnumpy().flat)

def test_roundtrip(self, tmp_filename):
a = self._create_data()

a.tofile(tmp_filename)
b = dpnp.fromfile(tmp_filename, dtype=a.dtype)
assert_array_equal(b, a.asnumpy().flat)

def test_sep(self, tmp_filename):
a = dpnp.array([1.51, 2, 3.51, 4])

with open(tmp_filename, "w") as f:
a.tofile(f, sep=",")

# reconstruct the array
with open(tmp_filename, "r") as f:
s = f.read()
b = dpnp.array([float(p) for p in s.split(",")], dtype=a.dtype)
assert_array_equal(a, b.asnumpy())

def test_format(self, tmp_filename):
a = dpnp.array([1.51, 2, 3.51, 4])

with open(tmp_filename, "w") as f:
a.tofile(f, sep=",", format="%.2f")

# reconstruct the array as a string
with open(tmp_filename, "r") as f:
s = f.read()
assert_equal(s, "1.51,2.00,3.51,4.00")


class TestToList:
@pytest.mark.parametrize(
"data", [[1, 2], [[1, 2], [3, 4]]], ids=["1d", "2d"]
Expand Down
Loading