Skip to content

Commit 03e0975

Browse files
authored
Add implementation of dpnp.ndarray.tofile method (#2653)
The PR adds implementation of `dpnp.ndarray.tofile` method converting the array to `numpy.ndarray` and calling `.tofile(...)` for the result.
1 parent b7d1572 commit 03e0975

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
2020
* Enabled support of Python 3.14 [#2631](https://github.com/IntelPython/dpnp/pull/2631)
2121
* Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652)
2222
* Added implementation of `dpnp.frexp` [#2635](https://github.com/IntelPython/dpnp/pull/2635)
23+
* Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635)
2324

2425
### Changed
2526

dpnp/dpnp_array.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,39 @@ def to_device(self, device, /, *, stream=None):
20132013
return dpnp_array._create_from_usm_ndarray(usm_res)
20142014

20152015
# 'tobytes',
2016-
# 'tofile',
2016+
2017+
def tofile(self, fid, sep="", format=""):
2018+
"""
2019+
Writes the array to a file as text or binary (default).
2020+
2021+
For full documentation refer to :obj:`numpy.ndarray.tofile`.
2022+
2023+
Parameters
2024+
----------
2025+
fid : {file. str, path}
2026+
An open file object, or a string containing a filename.
2027+
sep : str, optional
2028+
Separator between array items for text output. If ``""`` (empty),
2029+
a binary file is written.
2030+
2031+
Default: ``""``.
2032+
format : str, optional
2033+
Format string for text file output (when non-empty `sep` is passed).
2034+
Each entry in the array is formatted to text by first converting it
2035+
to the closest Python type, and then using ``format % item``. If
2036+
``""`` (empty), no formatting is used while converting to the
2037+
string.
2038+
2039+
Default: ``""``.
2040+
2041+
See Also
2042+
--------
2043+
:obj:`dpnp.fromfile` : Construct an array from data in a text or binary
2044+
file.
2045+
2046+
"""
2047+
2048+
self.asnumpy().tofile(fid, sep=sep, format=format)
20172049

20182050
def tolist(self):
20192051
"""

dpnp/tests/test_ndarray.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import dpnp
1212

1313
from .helper import (
14+
generate_random_numpy_array,
1415
get_abs_array,
1516
get_all_dtypes,
1617
get_complex_dtypes,
@@ -107,6 +108,64 @@ def test_strides(self):
107108
assert xp.full_like(a, fill_value=6) not in a
108109

109110

111+
class TestToFile:
112+
def _create_data(self):
113+
x = generate_random_numpy_array((2, 4, 3), dtype=complex)
114+
x[0, :, 1] = [numpy.nan, numpy.inf, -numpy.inf, numpy.nan]
115+
return dpnp.array(x)
116+
117+
@pytest.fixture(params=["string", "path_obj"])
118+
def tmp_filename(self, tmp_path, request):
119+
# This fixture covers two cases:
120+
# one where the filename is a string and
121+
# another where it is a pathlib object
122+
filename = tmp_path / "file"
123+
if request.param == "string":
124+
filename = str(filename)
125+
yield filename
126+
127+
def test_roundtrip_file(self, tmp_filename):
128+
a = self._create_data()
129+
130+
with open(tmp_filename, "wb") as f:
131+
a.tofile(f)
132+
133+
# reconstruct the array back from the file
134+
with open(tmp_filename, "rb") as f:
135+
b = dpnp.fromfile(f, dtype=a.dtype)
136+
assert_array_equal(b, a.asnumpy().flat)
137+
138+
def test_roundtrip(self, tmp_filename):
139+
a = self._create_data()
140+
141+
a.tofile(tmp_filename)
142+
b = dpnp.fromfile(tmp_filename, dtype=a.dtype)
143+
assert_array_equal(b, a.asnumpy().flat)
144+
145+
def test_sep(self, tmp_filename):
146+
a = dpnp.array([1.51, 2, 3.51, 4])
147+
148+
with open(tmp_filename, "w") as f:
149+
a.tofile(f, sep=",")
150+
151+
# reconstruct the array
152+
with open(tmp_filename, "r") as f:
153+
s = f.read()
154+
b = dpnp.array([float(p) for p in s.split(",")], dtype=a.dtype)
155+
assert_array_equal(a, b.asnumpy())
156+
157+
def test_format(self, tmp_filename):
158+
a = dpnp.array([1.51, 2, 3.51, 4])
159+
160+
with open(tmp_filename, "w") as f:
161+
a.tofile(f, sep=",", format="%.2f")
162+
163+
# reconstruct the array as a string
164+
with open(tmp_filename, "r") as f:
165+
s = f.read()
166+
assert_equal(s, "1.51,2.00,3.51,4.00")
167+
168+
110169
class TestToList:
111170
@pytest.mark.parametrize(
112171
"data", [[1, 2], [[1, 2], [3, 4]]], ids=["1d", "2d"]

0 commit comments

Comments
 (0)