forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
261 lines (204 loc) · 7.33 KB
/
utils.py
File metadata and controls
261 lines (204 loc) · 7.33 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
###########################################################################################
# Copyright ironArray SL 2021.
#
# All rights reserved.
#
# This software is the confidential and proprietary information of ironArray SL
# ("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
import os
import shutil
zarr_to_iarray_dtypes = {
"int8": np.int8,
"int16": np.int16,
"int32": np.int32,
"int64": np.int64,
"uint8": np.uint8,
"uint16": np.uint16,
"uint32": np.uint32,
"uint64": np.uint64,
"float32": np.float32,
"float64": np.float64,
"bool": np.bool_,
}
class IllegalArgumentError(ValueError):
pass
def _check_access_mode(urlpath, mode, update=False):
"""
Based on `urlpath` and `mode`, remove the possible underlying storage.
Call this function only when modifying/creating an array.
"""
supported_modes = [b"r", b"r+", b"w-", b"w", b"a"]
mode = mode.encode("utf-8") if isinstance(mode, str) else mode
supported = any(x == mode for x in supported_modes)
if not supported:
raise NotImplementedError("The mode is not supported yet.")
if mode == b"r":
raise IOError("Cannot do the requested operation with the actual mode.")
if mode == b"r+" and not update:
raise IOError("Cannot do the requested operation with the actual mode.")
if urlpath is not None:
if mode == b"w":
if not update:
ia.remove_urlpath(urlpath)
elif os.path.exists(urlpath) and mode == b"w-" and not update:
raise IOError(
f"The writing mode cannot overwrite the already existing array '{urlpath}'."
)
def cmp_arrays(a, b, success=None) -> None:
"""Quick and dirty comparison between arrays `a` and `b`.
The arrays `a` and `b` are converted internally to numpy arrays, so
this can require a lot of memory. This is mainly used for quick testing.
If success, and the string passed in `success` is not None, it is printed.
If failed, an exception will be raised.
"""
if type(a) is ia.IArray:
a = ia.iarray2numpy(a)
if type(b) is ia.IArray:
b = ia.iarray2numpy(b)
if a.dtype == np.float64 and b.dtype == np.float64:
tol = 1e-14
else:
tol = 1e-6
np.testing.assert_allclose(a, b, rtol=tol, atol=tol)
if success is not None:
print(success)
# TODO: are cfg and kwargs needed here?
def save(urlpath: str, iarr: ia.IArray, cfg: ia.Config = None, **kwargs) -> None:
"""Save an array to a binary file in ironArray `.iarr` format.
If the file already exists it overwrites it.
The default for this function is `contiguous=True`.
`cfg` and `kwargs` are the same than for :func:`IArray.copy`.
Parameters
----------
iarr : :ref:`IArray`
The array to save.
urlpath : str
The url path to save the array.
See Also
--------
load : Load an array from disk.
open : Open an array from disk.
"""
ia.remove_urlpath(urlpath)
if kwargs.get("contiguous", None) is None and (cfg is None or cfg.contiguous is None):
kwargs = dict(kwargs, contiguous=True)
iarr.copy(cfg=cfg, urlpath=urlpath, **kwargs)
def load(urlpath: str, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Open an array from a binary file in ironArray `.iarr` format and load data into memory.
The default for this function is `contiguous=False` and `mode='a'`.
`cfg` and `kwargs` are the same than for :func:`IArray.copy`.
Parameters
----------
urlpath : str
The url path to read.
Returns
-------
:ref:`IArray`
The new loaded array.
See Also
--------
save : Save an array to disk.
"""
if kwargs.get("contiguous", None) is None and (cfg is None or cfg.contiguous is None):
kwargs = dict(kwargs, contiguous=False)
if kwargs.get("mode", None) is not None:
iarr = ia.open(urlpath, mode=kwargs.get("mode", None))
elif cfg is not None:
iarr = ia.open(urlpath, mode=cfg.mode)
else:
iarr = ia.open(urlpath)
return iarr.copy(cfg=cfg, **kwargs)
def open(urlpath: str, mode="a") -> ia.IArray:
"""Open an array from a binary file in ironArray `.iarr` format.
The array data will lazily be read when necessary.
Parameters
----------
urlpath : str
The url path to read.
mode : str
The open mode. This parameter supersedes the mode in the default :class:`Config`.
Returns
-------
:ref:`IArray`
The new opened array.
See Also
--------
save : Save an array to disk.
"""
cfg = ia.get_config_defaults()
if not os.path.exists(urlpath):
raise IOError("The file does not exist.")
with ia.config(cfg=cfg, mode=mode) as cfg:
return ext.open(cfg, urlpath)
# TODO: are cfg and kwargs needed here?
def iarray2numpy(iarr: ia.IArray, cfg: ia.Config = None, **kwargs) -> np.ndarray:
"""Convert an ironArray array into a NumPy array.
`cfg` and `kwargs` are the same than for :func:`empty`.
Parameters
----------
iarr : :ref:`IArray`
The array to convert.
Returns
-------
out: `np.ndarray <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html>`_
The new NumPy array.
See Also
--------
numpy2iarray : Convert a NumPy array into an ironArray array.
"""
if cfg is None:
cfg = ia.get_config_defaults()
with ia.config(cfg=cfg, **kwargs) as cfg:
return ext.iarray2numpy(cfg, iarr)
def numpy2iarray(arr: np.ndarray, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Convert a NumPy array into an ironArray array.
`cfg` and `kwargs` are the same than for :func:`empty`.
Parameters
----------
arr : `np.ndarray <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html>`_
The array to convert.
Returns
-------
:ref:`IArray`
The new ironArray array.
See Also
--------
iarray2numpy : Convert an ironArray array into a NumPy array.
"""
if np.dtype(arr.dtype) in [np.dtype(d) for d in zarr_to_iarray_dtypes]:
dtype = arr.dtype.type
kwargs["dtype"] = dtype
else:
np_dtype = np.dtype(arr.dtype).str
kwargs["np_dtype"] = np_dtype
kwargs["dtype"] = ext.dtype_str2dtype[np_dtype]
if cfg is None:
cfg = ia.get_config_defaults()
if not arr.flags["C_CONTIGUOUS"]:
# For the conversion we need a *C* contiguous array
arr = arr.copy(order="C")
with ia.config(shape=arr.shape, cfg=cfg, **kwargs) as cfg:
dtshape = ia.DTShape(arr.shape, cfg.dtype)
return ext.numpy2iarray(cfg, arr, dtshape)
# File system utilities
def remove_urlpath(urlpath):
"""Permanently remove the file or the directory given by `urlpath`.
Parameters
----------
urlpath: String
The path of the directory or file.
Returns
-------
None
"""
if urlpath is not None:
if os.path.exists(urlpath):
if os.path.isdir(urlpath):
shutil.rmtree(urlpath)
else:
os.remove(urlpath)