-
Notifications
You must be signed in to change notification settings - Fork 19
/
compressedsubarray.py
136 lines (95 loc) · 3.07 KB
/
compressedsubarray.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
import abc
from functools import reduce
from operator import mul
from ...functions import inspect as cf_inspect
class CompressedSubarray(abc.ABC):
"""Abstract base class for a compressed sub-array container."""
def __init__(self, array, shape, compression):
"""**Initialization**
:Parameters:
array:
shape: `tuple`
The shape of the uncompressed array
compression: `dict`
"""
# DO NOT CHANGE IN PLACE
self.array = array
# DO NOT CHANGE IN PLACE
self.compression = compression
# DO NOT CHANGE IN PLACE
self.shape = tuple(shape)
# DO NOT CHANGE IN PLACE
self.ndim = len(shape)
# DO NOT CHANGE IN PLACE
self.size = reduce(mul, shape, 1)
@abc.abstractmethod
def __getitem__(self, indices):
"""x.__getitem__(indices) <==> x[indices]
Returns a numpy array.
"""
raise NotImplementedError() # pragma: no cover
def __repr__(self):
"""x.__repr__() <==> repr(x)"""
array = self.array
shape = str(array.shape)
shape = shape.replace(",)", ")")
return f"<CF {self.__class__.__name__}{shape}: {array}>"
# ----------------------------------------------------------------
# Dask attributes
# ----------------------------------------------------------------
@property
def dask_lock(self):
"""TODODASK."""
return self.array.dask_lock
@property
def dask_asarray(self):
"""TODODASK."""
return False
# ----------------------------------------------------------------
# Attributes
# ----------------------------------------------------------------
@property
def dtype(self):
return self.array.dtype
@property
def file(self):
"""The file on disk which contains the compressed array, or
`None` of the array is in memory.
**Examples:**
>>> self.file
'/home/foo/bar.nc'
"""
return getattr(self.array, "file", None)
# ----------------------------------------------------------------
# Methods
# ----------------------------------------------------------------
def close(self):
"""Close all referenced open files.
:Returns:
`None`
**Examples:**
>>> f.close()
"""
if self.on_disk():
self.array.close()
def copy(self):
"""Replace the abstract base class with a deep copy."""
C = self.__class__
new = C.__new__(C)
new.__dict__ = self.__dict__.copy()
return new
def inspect(self):
"""Inspect the object for debugging.
.. seealso:: `cf.inspect`
:Returns:
`None`
"""
print(cf_inspect(self))
def on_disk(self):
"""True if and only if the compressed array is on disk as
opposed to in memory.
**Examples:**
>>> a.on_disk()
True
"""
return not hasattr(self.array, "__array_interface__")