-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
164 lines (124 loc) · 4.31 KB
/
Copy pathutils.py
File metadata and controls
164 lines (124 loc) · 4.31 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
###########################################################################################
# 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
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 ``.iarray`` format.
`cfg` and `kwargs` are the same than for :func:`empty`.
Parameters
----------
iarr : 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.
"""
iarr.copy(view=False, cfg=cfg, urlpath=urlpath, **kwargs)
def load(filename: str, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Open an array from a binary file in ironArray ``.iarray`` format and load data into memory.
`cfg` and `kwargs` are the same than for :func:`empty`.
Parameters
----------
filename : str
The file name to read.
Returns
-------
IArray
The new loaded array.
See Also
--------
save : Save an array to disk.
"""
with ia.config(cfg=cfg, **kwargs) as cfg:
return ext.load(cfg, filename)
def open(filename: str, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Open an array from a binary file in ironArray ``.iarray`` format. The array data will lazily
be read when necessary.
`cfg` and `kwargs` are the same than for :func:`empty`.
Parameters
----------
filename : str
The file name to read.
Returns
-------
IArray
The new opened array.
See Also
--------
save : Save an array to disk.
"""
with ia.config(cfg=cfg, **kwargs) as cfg:
return ext.open(cfg, filename)
# 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 : IArray
The array to convert.
Returns
-------
np.ndarray
The new NumPy array.
See Also
--------
numpy2iarray : Convert a NumPy array into an ironArray array.
"""
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
The array to convert.
Returns
-------
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")
dtshape = ia.DTShape(arr.shape, dtype)
with ia.config(dtshape=dtshape, cfg=cfg, **kwargs) as cfg:
return ext.numpy2iarray(cfg, arr, dtshape)