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
218 lines (166 loc) · 5.89 KB
/
Copy pathutils.py
File metadata and controls
218 lines (166 loc) · 5.89 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
###########################################################################################
# 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
import os
import shutil
# TODO: complete support for all possible values of mode
def _check_path_mode(urlpath, mode):
"""
Based on `urlpath` and `mode`, remove the possible underlying storage.
"""
if urlpath is not None and mode == b"w" and os.path.exists(urlpath):
ia.remove_urlpath(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`.
`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)
iarr = ia.open(urlpath)
return iarr.copy(cfg=cfg, **kwargs)
def open(urlpath: str) -> 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.
Returns
-------
:ref:`IArray`
The new opened array.
See Also
--------
save : Save an array to disk.
"""
cfg = ia.get_config_defaults()
with ia.config(cfg=cfg) 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 arr.dtype == np.float64:
dtype = np.float64
elif arr.dtype == np.float32:
dtype = np.float32
else:
raise NotImplementedError("Only float32 and float64 types are supported for now")
kwargs["dtype"] = 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)