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:
filefile or str or Path

An open file object, a string containing the filename, or a Path object. When reading from a file object it must support random access (i.e. it must have tell and seek methods).

dtype{None, str, dtype object}, 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. If None, uses a default floating point data type for the device on which the returned array is allocated.

Default: float.

countint, optional

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

Default: -1.

sepstr, 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.

Default: "".

offsetint, optional

The offset (in bytes) from the file's current position. Only permitted for binary files.

Default: 0.

device{None, string, SyclDevice, SyclQueue, Device}, optional

An array API concept of device where the output array is created. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a dpnp.tensor.Device object returned by dpnp.ndarray.device.

Default: None.

usm_type{None, "device", "shared", "host"}, optional

The type of SYCL USM allocation for the output array.

Default: "device".

sycl_queue{None, SyclQueue}, optional

A SYCL queue to use for output array allocation and copying. The sycl_queue can be passed as None (the default), which means to get the SYCL queue from device keyword if present or to use a default queue.

Default: None.

Returns:
outdpnp.ndarray

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

Limitations

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

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.

Notes

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

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')