Skip to content

Commit 40f3c1a

Browse files
Array creation from* funcs (#383)
* dpctl renaming
1 parent bd8b9f8 commit 40f3c1a

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

dpnp/dpnp_iface_arraycreation.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@
5858
"diagflat",
5959
"empty",
6060
"empty_like",
61+
"frombuffer",
62+
"fromfile",
63+
"fromfunction",
64+
"fromiter",
65+
"fromstring",
6166
"full",
6267
"full_like",
6368
"geomspace",
6469
"linspace",
70+
"loadtxt",
6571
"logspace",
6672
"meshgrid",
6773
"mgrid",
@@ -497,6 +503,88 @@ def empty_like(prototype, dtype=None, order='C', subok=False, shape=None):
497503
return numpy.empty_like(prototype, dtype, order, subok, shape)
498504

499505

506+
def frombuffer(buffer, **kwargs):
507+
"""
508+
Interpret a buffer as a 1-dimensional array.
509+
510+
For full documentation refer to :obj:`numpy.frombuffer`.
511+
512+
Limitations
513+
-----------
514+
Only float64, float32, int64, int32 types are supported.
515+
516+
"""
517+
518+
return call_origin(numpy.frombuffer, buffer, **kwargs)
519+
520+
521+
def fromfile(file, **kwargs):
522+
"""
523+
Construct an array from data in a text or binary file.
524+
525+
A highly efficient way of reading binary data with a known data-type,
526+
as well as parsing simply formatted text files. Data written using the
527+
`tofile` method can be read using this function.
528+
529+
For full documentation refer to :obj:`numpy.fromfile`.
530+
531+
Limitations
532+
-----------
533+
Only float64, float32, int64, int32 types are supported.
534+
535+
"""
536+
537+
return call_origin(numpy.fromfile, file, **kwargs)
538+
539+
540+
def fromfunction(function, shape, **kwargs):
541+
"""
542+
Construct an array by executing a function over each coordinate.
543+
544+
The resulting array therefore has a value ``fn(x, y, z)`` at
545+
coordinate ``(x, y, z)``.
546+
547+
For full documentation refer to :obj:`numpy.fromfunction`.
548+
549+
Limitations
550+
-----------
551+
Only float64, float32, int64, int32 types are supported.
552+
553+
"""
554+
555+
return call_origin(numpy.fromfunction, function, shape, **kwargs)
556+
557+
558+
def fromiter(iterable, dtype, count=-1):
559+
"""
560+
Create a new 1-dimensional array from an iterable object.
561+
562+
For full documentation refer to :obj:`numpy.fromiter`.
563+
564+
Limitations
565+
-----------
566+
Only float64, float32, int64, int32 types are supported.
567+
568+
"""
569+
570+
return call_origin(numpy.fromiter, iterable, dtype, count)
571+
572+
573+
def fromstring(string, **kwargs):
574+
"""
575+
A new 1-D array initialized from text data in a string.
576+
577+
For full documentation refer to :obj:`numpy.fromstring`.
578+
579+
Limitations
580+
-----------
581+
Only float64, float32, int64, int32 types are supported.
582+
583+
"""
584+
585+
return call_origin(numpy.fromstring, string, **kwargs)
586+
587+
500588
# numpy.full(shape, fill_value, dtype=None, order='C')
501589
def full(shape, fill_value, dtype=None, order='C'):
502590
"""
@@ -666,6 +754,32 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis
666754
return call_origin(numpy.linspace, start, stop, num, endpoint, retstep, dtype, axis)
667755

668756

757+
def loadtxt(fname, **kwargs):
758+
"""
759+
Load data from a text file.
760+
761+
Each row in the text file must have the same number of values.
762+
763+
For full documentation refer to :obj:`numpy.loadtxt`.
764+
765+
Limitations
766+
-----------
767+
Only float64, float32, int64, int32 types are supported.
768+
769+
Examples
770+
--------
771+
>>> import dpnp as np
772+
>>> from io import StringIO # StringIO behaves like a file object
773+
>>> c = StringIO("0 1\n2 3")
774+
>>> np.loadtxt(c)
775+
array([[0., 1.],
776+
[2., 3.]])
777+
778+
"""
779+
780+
return call_origin(numpy.loadtxt, fname, **kwargs)
781+
782+
669783
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0):
670784
"""
671785
Return numbers spaced evenly on a log scale.

tests/test_arraycreation.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,75 @@
44

55
import numpy
66

7+
import tempfile
8+
9+
10+
@pytest.mark.parametrize("type",
11+
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
12+
ids=['float64', 'float32', 'int64', 'int32'])
13+
def test_frombuffer(type):
14+
buffer = b'12345678'
15+
16+
np_res = numpy.frombuffer(buffer, dtype=type)
17+
dpnp_res = dpnp.frombuffer(buffer, dtype=type)
18+
19+
numpy.testing.assert_array_equal(dpnp_res, np_res)
20+
21+
22+
@pytest.mark.parametrize("type",
23+
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
24+
ids=['float64', 'float32', 'int64', 'int32'])
25+
def test_fromfile(type):
26+
with tempfile.TemporaryFile() as fh:
27+
fh.write(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08")
28+
fh.flush()
29+
30+
fh.seek(0)
31+
np_res = numpy.fromfile(fh, dtype=type)
32+
fh.seek(0)
33+
dpnp_res = dpnp.fromfile(fh, dtype=type)
34+
35+
numpy.testing.assert_array_equal(dpnp_res, np_res)
36+
37+
38+
@pytest.mark.parametrize("type",
39+
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
40+
ids=['float64', 'float32', 'int64', 'int32'])
41+
def test_fromfunction(type):
42+
def func(x, y):
43+
return x * y
44+
45+
shape = (3, 3)
46+
47+
np_res = numpy.fromfunction(func, shape=shape, dtype=type)
48+
dpnp_res = dpnp.fromfunction(func, shape=shape, dtype=type)
49+
50+
numpy.testing.assert_array_equal(dpnp_res, np_res)
51+
52+
53+
@pytest.mark.parametrize("type",
54+
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
55+
ids=['float64', 'float32', 'int64', 'int32'])
56+
def test_fromiter(type):
57+
iter = [1, 2, 3, 4]
58+
59+
np_res = numpy.fromiter(iter, dtype=type)
60+
dpnp_res = dpnp.fromiter(iter, dtype=type)
61+
62+
numpy.testing.assert_array_equal(dpnp_res, np_res)
63+
64+
65+
@pytest.mark.parametrize("type",
66+
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
67+
ids=['float64', 'float32', 'int64', 'int32'])
68+
def test_fromstring(type):
69+
string = "1 2 3 4"
70+
71+
np_res = numpy.fromstring(string, dtype=type, sep=' ')
72+
dpnp_res = dpnp.fromstring(string, dtype=type, sep=' ')
73+
74+
numpy.testing.assert_array_equal(dpnp_res, np_res)
75+
776

877
@pytest.mark.parametrize("type",
978
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
@@ -25,3 +94,19 @@ def test_geomspace(type, num, endpoint):
2594
numpy.testing.assert_allclose(dpnp_res, np_res, atol=1)
2695
else:
2796
numpy.testing.assert_allclose(dpnp_res, np_res)
97+
98+
99+
@pytest.mark.parametrize("type",
100+
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
101+
ids=['float64', 'float32', 'int64', 'int32'])
102+
def test_loadtxt(type):
103+
with tempfile.TemporaryFile() as fh:
104+
fh.write(b"1 2 3 4")
105+
fh.flush()
106+
107+
fh.seek(0)
108+
np_res = numpy.loadtxt(fh, dtype=type)
109+
fh.seek(0)
110+
dpnp_res = dpnp.loadtxt(fh, dtype=type)
111+
112+
numpy.testing.assert_array_equal(dpnp_res, np_res)

0 commit comments

Comments
 (0)