Skip to content

Commit

Permalink
Array creation from* funcs (#383)
Browse files Browse the repository at this point in the history
* dpctl renaming
  • Loading branch information
Alexander-Makaryev authored Dec 8, 2020
1 parent bd8b9f8 commit 40f3c1a
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
114 changes: 114 additions & 0 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@
"diagflat",
"empty",
"empty_like",
"frombuffer",
"fromfile",
"fromfunction",
"fromiter",
"fromstring",
"full",
"full_like",
"geomspace",
"linspace",
"loadtxt",
"logspace",
"meshgrid",
"mgrid",
Expand Down Expand Up @@ -497,6 +503,88 @@ def empty_like(prototype, dtype=None, order='C', subok=False, shape=None):
return numpy.empty_like(prototype, dtype, order, subok, shape)


def frombuffer(buffer, **kwargs):
"""
Interpret a buffer as a 1-dimensional array.
For full documentation refer to :obj:`numpy.frombuffer`.
Limitations
-----------
Only float64, float32, int64, int32 types are supported.
"""

return call_origin(numpy.frombuffer, buffer, **kwargs)


def fromfile(file, **kwargs):
"""
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 :obj:`numpy.fromfile`.
Limitations
-----------
Only float64, float32, int64, int32 types are supported.
"""

return call_origin(numpy.fromfile, file, **kwargs)


def fromfunction(function, shape, **kwargs):
"""
Construct an array by executing a function over each coordinate.
The resulting array therefore has a value ``fn(x, y, z)`` at
coordinate ``(x, y, z)``.
For full documentation refer to :obj:`numpy.fromfunction`.
Limitations
-----------
Only float64, float32, int64, int32 types are supported.
"""

return call_origin(numpy.fromfunction, function, shape, **kwargs)


def fromiter(iterable, dtype, count=-1):
"""
Create a new 1-dimensional array from an iterable object.
For full documentation refer to :obj:`numpy.fromiter`.
Limitations
-----------
Only float64, float32, int64, int32 types are supported.
"""

return call_origin(numpy.fromiter, iterable, dtype, count)


def fromstring(string, **kwargs):
"""
A new 1-D array initialized from text data in a string.
For full documentation refer to :obj:`numpy.fromstring`.
Limitations
-----------
Only float64, float32, int64, int32 types are supported.
"""

return call_origin(numpy.fromstring, string, **kwargs)


# numpy.full(shape, fill_value, dtype=None, order='C')
def full(shape, fill_value, dtype=None, order='C'):
"""
Expand Down Expand Up @@ -666,6 +754,32 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis
return call_origin(numpy.linspace, start, stop, num, endpoint, retstep, dtype, axis)


def loadtxt(fname, **kwargs):
"""
Load data from a text file.
Each row in the text file must have the same number of values.
For full documentation refer to :obj:`numpy.loadtxt`.
Limitations
-----------
Only float64, float32, int64, int32 types are supported.
Examples
--------
>>> import dpnp as np
>>> from io import StringIO # StringIO behaves like a file object
>>> c = StringIO("0 1\n2 3")
>>> np.loadtxt(c)
array([[0., 1.],
[2., 3.]])
"""

return call_origin(numpy.loadtxt, fname, **kwargs)


def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
"""
Return numbers spaced evenly on a log scale.
Expand Down
85 changes: 85 additions & 0 deletions tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,75 @@

import numpy

import tempfile


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_frombuffer(type):
buffer = b'12345678'

np_res = numpy.frombuffer(buffer, dtype=type)
dpnp_res = dpnp.frombuffer(buffer, dtype=type)

numpy.testing.assert_array_equal(dpnp_res, np_res)


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_fromfile(type):
with tempfile.TemporaryFile() as fh:
fh.write(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08")
fh.flush()

fh.seek(0)
np_res = numpy.fromfile(fh, dtype=type)
fh.seek(0)
dpnp_res = dpnp.fromfile(fh, dtype=type)

numpy.testing.assert_array_equal(dpnp_res, np_res)


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_fromfunction(type):
def func(x, y):
return x * y

shape = (3, 3)

np_res = numpy.fromfunction(func, shape=shape, dtype=type)
dpnp_res = dpnp.fromfunction(func, shape=shape, dtype=type)

numpy.testing.assert_array_equal(dpnp_res, np_res)


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_fromiter(type):
iter = [1, 2, 3, 4]

np_res = numpy.fromiter(iter, dtype=type)
dpnp_res = dpnp.fromiter(iter, dtype=type)

numpy.testing.assert_array_equal(dpnp_res, np_res)


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_fromstring(type):
string = "1 2 3 4"

np_res = numpy.fromstring(string, dtype=type, sep=' ')
dpnp_res = dpnp.fromstring(string, dtype=type, sep=' ')

numpy.testing.assert_array_equal(dpnp_res, np_res)


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
Expand All @@ -25,3 +94,19 @@ def test_geomspace(type, num, endpoint):
numpy.testing.assert_allclose(dpnp_res, np_res, atol=1)
else:
numpy.testing.assert_allclose(dpnp_res, np_res)


@pytest.mark.parametrize("type",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=['float64', 'float32', 'int64', 'int32'])
def test_loadtxt(type):
with tempfile.TemporaryFile() as fh:
fh.write(b"1 2 3 4")
fh.flush()

fh.seek(0)
np_res = numpy.loadtxt(fh, dtype=type)
fh.seek(0)
dpnp_res = dpnp.loadtxt(fh, dtype=type)

numpy.testing.assert_array_equal(dpnp_res, np_res)

0 comments on commit 40f3c1a

Please sign in to comment.