/
array.py
229 lines (167 loc) · 3.76 KB
/
array.py
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
from builtins import (str, super) #(object, str)
from future.utils import with_metaclass
import abc
from ...abstract import Container
class Array(with_metaclass(abc.ABCMeta, Container)):
'''Abstract base class for a container of an array.
The form of the array is defined by the initialization parameters of a
subclass.
See `cfdm.core.NumpyArray` for an example implementation.
.. versionadded:: 1.7.0
'''
def __init__(self, **kwargs):
'''**Initialization**
:Parameters:
kwargs: *optional*
Named parameters and their values that define the array.
'''
super().__init__()
for key, value in kwargs.items():
self._set_component(key, value, copy=False)
#--- End: def
def __deepcopy__(self, memo):
'''Called by the `copy.deepcopy` function.
x.__deepcopy__() <==> copy.deepcopy(x)
Copy-on-write is employed. Therefore, after copying, care must be
taken when making in-place modifications to attributes of either the
original or the new copy.
.. versionadded:: 1.7.0
**Examples:**
>>> import copy
>>> y = copy.deepcopy(x)
'''
return self.copy()
#--- End: def
# ----------------------------------------------------------------
# Attributes
# ----------------------------------------------------------------
@property
@abc.abstractmethod
def dtype(self):
'''Data-type of the data elements.
.. versionadded:: 1.7.0
**Examples:**
>>> a.dtype
dtype('float64')
>>> print(type(a.dtype))
<type 'numpy.dtype'>
'''
raise NotImplementedError() # pragma: no cover
#--- End: def
@property
@abc.abstractmethod
def ndim(self):
'''Number of array dimensions
.. versionadded:: 1.7.0
**Examples:**
>>> a.shape
(73, 96)
>>> a.ndim
2
>>> a.size
7008
>>> a.shape
(1, 1, 1)
>>> a.ndim
3
>>> a.size
1
>>> a.shape
()
>>> a.ndim
0
>>> a.size
1
'''
raise NotImplementedError() # pragma: no cover
#--- End: def
@property
@abc.abstractmethod
def shape(self):
'''Tuple of array dimension sizes.
.. versionadded:: 1.7.0
**Examples:**
>>> a.shape
(73, 96)
>>> a.ndim
2
>>> a.size
7008
>>> a.shape
(1, 1, 1)
>>> a.ndim
3
>>> a.size
1
>>> a.shape
()
>>> a.ndim
0
>>> a.size
1
'''
raise NotImplementedError() # pragma: no cover
#--- End: def
@property
@abc.abstractmethod
def size(self):
'''Number of elements in the array.
.. versionadded:: 1.7.0
**Examples:**
>>> a.shape
(73, 96)
>>> a.size
7008
>>> a.ndim
2
>>> a.shape
(1, 1, 1)
>>> a.ndim
3
>>> a.size
1
>>> a.shape
()
>>> a.ndim
0
>>> a.size
1
'''
raise NotImplementedError() # pragma: no cover
#--- End: def
@property
@abc.abstractmethod
def array(self):
'''Return an independent numpy array containing the data.
.. versionadded:: 1.7.0
:Returns:
`numpy.ndarray`
An independent numpy array of the data.
**Examples:**
>>> n = a.array
>>> isinstance(n, numpy.ndarray)
True
'''
raise NotImplementedError() # pragma: no cover
#--- End: def
# ----------------------------------------------------------------
# Methods
# ----------------------------------------------------------------
def copy(self):
'''Return a deep copy of the array.
``a.copy() is equivalent to ``copy.deepcopy(a)``.
Copy-on-write is employed. Therefore, after copying, care must be
taken when making in-place modifications to attributes of either the
original or the new copy.
.. versionadded:: 1.7.0
:Returns:
The deep copy.
**Examples:**
>>> b = a.copy()
'''
klass = self.__class__
new = klass.__new__(klass)
new.__dict__ = self.__dict__.copy()
return new
#--- End: def
#--- End: class