dpnp.fromfile

dpnp.fromfile(file, dtype=<class 'float'>, count=-1, sep='', offset=0, *, like=None, device=None, usm_type='device', sycl_queue=None)[source]

Construct an array from data in a text or binary file.

A highly efficient way of reading binary data with a known data-type, as well as parsing simply formatted text files. Data written using the tofile method can be read using this function.

For full documentation refer to numpy.fromfile.

Parameters:
  • file (file or str or Path) -- Open file object or filename.

  • dtype (data-type, optional) -- Data type of the returned array. For binary files, it is used to determine the size and byte-order of the items in the file. Default is the default floating point data type for the device where the returned array is allocated.

  • count (int, optional) -- Number of items to read. -1 means all items (i.e., the complete file).

  • sep (str, optional) -- Separator between items if file is a text file. Empty ("") separator means the file should be treated as binary. Spaces (" ") in the separator match zero or more whitespace characters. A separator consisting only of spaces must match at least one whitespace.

  • offset (int, optional) -- The offset (in bytes) from the file's current position. Defaults to 0. Only permitted for binary files.

  • device ({None, string, SyclDevice, SyclQueue}, optional) -- An array API concept of device where the output array is created. The device can be None (the default), an OneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a Device object returned by dpnp.dpnp_array.dpnp_array.device property.

  • usm_type ({None, "device", "shared", "host"}, optional) -- The type of SYCL USM allocation for the output array. Default is "device".

  • sycl_queue ({None, SyclQueue}, optional) -- A SYCL queue to use for output array allocation and copying.

Returns:

out -- A 1-dimensional array created from data in a text or binary file.

Return type:

dpnp.ndarray

Limitations

Parameter like is supported only with default value None. Otherwise, the function raises NotImplementedError exception.

Notes

This uses numpy.fromfile and coerces the result to a DPNP array.

See also

dpnp.frombuffer

Construct array from the buffer data.

dpnp.fromiter

Construct array from an iterable object.

dpnp.fromstring

Construct array from the text data in a string.

dpnp.load

Load arrays or pickled objects from files.

dpnp.save

Save an array to a binary file.

dpnp.ndarray.tofile

Write array to a file as text or binary.

dpnp.loadtxt

More flexible way of loading data from a text file.

Examples

Save the data to a temporary file:

>>> import tempfile
>>> fh = tempfile.TemporaryFile()
>>> fh.write(b"\x00\x01\x02\x03\x04")
>>> fh.flush()
>>> fh.seek(0)

Construct an array:

>>> import dpnp as np
>>> np.fromfile(fh, dtype="u1")
array([0, 1, 2, 3, 4], dtype=uint8)

Creating an array on a different device or with a specified usm_type

>>> fh.seek(0)
>>> x = np.fromfile(fh, dtype="u1") # default case
>>> x.device, x.usm_type
(Device(level_zero:gpu:0), 'device')
>>> fh.seek(0)
>>> y = np.fromfile(fh, dtype="u1", device='cpu')
>>> y.device, y.usm_type
(Device(opencl:cpu:0), 'device')
>>> fh.seek(0)
>>> z = np.fromfile(fh, dtype="u1", usm_type="host")
>>> z.device, z.usm_type
(Device(level_zero:gpu:0), 'host')