forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.py
More file actions
210 lines (161 loc) · 5.45 KB
/
Copy pathconstructors.py
File metadata and controls
210 lines (161 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
###########################################################################################
# Copyright INAOS GmbH, Thalwil, 2018.
# Copyright Francesc Alted, 2018.
#
# All rights reserved.
#
# This software is the confidential and proprietary information of INAOS GmbH
# and Francesc Alted ("Confidential Information"). You shall not disclose such Confidential
# Information and shall use it only in accordance with the terms of the license agreement.
###########################################################################################
import numpy as np
import iarray as ia
from iarray import iarray_ext as ext
from dataclasses import dataclass, field
from typing import Sequence
@dataclass
class DTShape:
"""Shape and data type dataclass.
Parameters
----------
shape: list, tuple
The shape of the array.
dtype: np.float32, np.float64
The data type of the elements in the array. The default is np.float64.
"""
shape: Sequence
dtype: (np.float32, np.float64) = np.float64
def __post_init__(self):
if self.shape is None:
raise ValueError("shape must be non-empty")
def empty(shape: Sequence, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Return an empty array.
An empty array has no data and needs to be filled via a write iterator.
Parameters
----------
shape : tuple, list
The shape of the array to be created.
cfg : Config
The configuration for running the expression.
If None (default), global defaults are used.
kwargs : dict
A dictionary for setting some or all of the fields in the Config
dataclass that should override the current configuration.
Returns
-------
IArray
The new array.
See Also
--------
IArray.iter_write_block : Iterator for filling an empty array.
"""
if cfg is None:
cfg = ia.get_config()
with ia.config(shape=shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(shape, cfg.dtype)
return ext.empty(cfg, dtshape)
def arange(
shape: Sequence, start=None, stop=None, step=None, cfg: ia.Config = None, **kwargs
) -> ia.IArray:
"""Return evenly spaced values within a given interval.
`shape`, `cfg` and `kwargs` are the same than for :func:`empty`.
`start`, `stop`, `step` are the same as in np.arange.
Returns
-------
IArray
The new array.
See Also
--------
empty : Create an empty array.
"""
if (start, stop, step) == (None, None, None):
stop = np.prod(shape)
start = 0
step = 1
elif (stop, step) == (None, None):
stop = start
start = 0
step = 1
elif step is None:
stop = stop
start = start
if shape is None:
step = 1
else:
step = (stop - start) / np.prod(shape)
slice_ = slice(start, stop, step)
if cfg is None:
cfg = ia.get_config()
with ia.config(shape=shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(shape, cfg.dtype)
return ext.arange(cfg, slice_, dtshape)
def linspace(
shape: Sequence, start: float, stop: float, cfg: ia.Config = None, **kwargs
) -> ia.IArray:
"""Return evenly spaced numbers over a specified interval.
`shape`, `cfg` and `kwargs` are the same than for :func:`empty`.
`start`, `stop` are the same as in np.linspace.
Returns
-------
IArray
The new array.
See Also
--------
empty : Create an empty array.
"""
if cfg is None:
cfg = ia.get_config()
with ia.config(shape=shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(shape, cfg.dtype)
return ext.linspace(cfg, start, stop, dtshape)
def zeros(shape: Sequence, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Return a new array of given shape and type, filled with zeros.
`shape`, `cfg` and `kwargs` are the same than for :func:`empty`.
Returns
-------
IArray
The new array.
See Also
--------
empty : Create an empty array.
ones : Create an array filled with ones.
"""
if cfg is None:
cfg = ia.get_config()
with ia.config(shape=shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(shape, cfg.dtype)
return ext.zeros(cfg, dtshape)
def ones(shape: Sequence, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Return a new array of given shape and type, filled with ones.
`shape`, `cfg` and `kwargs` are the same than for :func:`empty`.
Returns
-------
IArray
The new array.
See Also
--------
empty : Create an empty array.
zeros : Create an array filled with zeros.
"""
if cfg is None:
cfg = ia.get_config()
with ia.config(shape=shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(shape, cfg.dtype)
return ext.ones(cfg, dtshape)
def full(shape: Sequence, fill_value: float, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Return a new array of given shape and type, filled with `fill_value`.
`shape`, `cfg` and `kwargs` are the same than for :func:`empty`.
Returns
-------
IArray
The new array.
See Also
--------
empty : Create an empty array.
zeros : Create an array filled with zeros.
"""
if cfg is None:
cfg = ia.get_config()
with ia.config(shape=shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(shape, cfg.dtype)
return ext.full(cfg, fill_value, dtshape)