@@ -32,8 +32,6 @@ def _set_active_options(self, chunks={}, chunk_limits=True):
32
32
self ._active_chunks = chunks
33
33
self ._chunk_limits = chunk_limits
34
34
35
-
36
-
37
35
# Holds all Active routines.
38
36
class ActiveChunk :
39
37
@@ -43,23 +41,26 @@ def _post_process_data(self, data):
43
41
# Perform any post-processing steps on the data here
44
42
return data
45
43
46
- def _standard_mean (self , axes = None , skipna = None , ** kwargs ):
44
+ def _standard_sum (self , axes = None , skipna = None , ** kwargs ):
47
45
"""
48
46
Standard Mean routine matches the normal routine for dask, required at this
49
47
stage if Active mean not available.
50
48
"""
51
- size = 1
52
- for i in axes :
53
- size *= self .shape [i ]
54
49
55
50
arr = np .array (self )
56
51
if skipna :
57
- total = np .nanmean (arr , axis = axes , ** kwargs ) * size
52
+ total = np .nansum (arr , axis = axes , ** kwargs )
58
53
else :
59
- total = np .mean (arr , axis = axes , ** kwargs ) * size
60
- return {'n' : self ._numel (axes = axes ), 'total' : total }
54
+ total = np .sum (arr , axis = axes , ** kwargs )
55
+ return total
56
+
57
+ def _standard_max (self , axes = None , skipna = None , ** kwargs ):
58
+ return np .max (self , axis = axes )
61
59
62
- def _numel (self , axes = None ):
60
+ def _standard_min (self , axes = None , skipna = None , ** kwargs ):
61
+ return np .min (self , axis = axes )
62
+
63
+ def _numel (self , method , axes = None ):
63
64
if not axes :
64
65
return self .size
65
66
@@ -72,7 +73,7 @@ def _numel(self, axes=None):
72
73
73
74
return np .full (newshape , size )
74
75
75
- def active_mean (self , axis = None , skipna = None , ** kwargs ):
76
+ def active_method (self , method , axis = None , skipna = None , ** kwargs ):
76
77
"""
77
78
Use PyActiveStorage package functionality to perform mean of this Fragment.
78
79
@@ -83,47 +84,66 @@ def active_mean(self, axis=None, skipna=None, **kwargs):
83
84
:returns: A ``duck array`` (numpy-like) with the reduced array or scalar value,
84
85
as specified by the axes parameter.
85
86
"""
87
+
88
+ standard_methods = {
89
+ 'mean' : self ._standard_sum ,
90
+ 'sum' : self ._standard_sum ,
91
+ 'max' : self ._standard_max ,
92
+ 'min' : self ._standard_min
93
+ }
94
+ ret = None
95
+ n = self ._numel (method , axes = axis )
96
+
86
97
try :
87
98
from activestorage .active import Active
88
99
except ImportError :
89
100
# Unable to import Active package. Default to using normal mean.
90
101
print ("ActiveWarning: Unable to import active module - defaulting to standard method." )
91
- return self ._standard_mean (axes = axis , skipna = skipna , ** kwargs )
92
-
93
- active = Active (self .filename , self .address )
94
- active .method = "mean"
95
- extent = tuple (self .get_extent ())
96
- data = active [extent ]
97
-
98
- if axis == None :
99
- axis = tuple ([i for i in range (self .ndim )])
100
-
101
- n = self ._numel (axes = axis )
102
-
103
- if len (axis ) == self .ndim :
104
-
105
- t = self ._post_process_data (data ) * n
102
+ ret = {
103
+ 'n' : n ,
104
+ 'total' : standard_methods [method ](axes = axis , skipna = skipna , ** kwargs )
105
+ }
106
106
107
- r = {
107
+ if not ret :
108
+
109
+ active = Active (self .filename , self .address )
110
+ active .method = method
111
+ extent = tuple (self .get_extent ())
112
+
113
+ if axis == None :
114
+ axis = tuple ([i for i in range (self .ndim )])
115
+
116
+ n = self ._numel (method , axes = axis )
117
+
118
+ if len (axis ) == self .ndim :
119
+ data = active [extent ]
120
+ t = self ._post_process_data (data ) * n
121
+
122
+ ret = {
123
+ 'n' : n ,
124
+ 'total' : t
125
+ }
126
+
127
+ if not ret :
128
+ # Experimental Recursive requesting to get each 1D column along the axes being requested.
129
+ range_recursives = []
130
+ for dim in range (self .ndim ):
131
+ if dim not in axis :
132
+ range_recursives .append (range (extent [dim ].start , extent [dim ].stop ))
133
+ else :
134
+ range_recursives .append (extent [dim ])
135
+ results = np .array (self ._get_elements (active , range_recursives , hyperslab = []))
136
+
137
+ t = self ._post_process_data (results ) * n
138
+ ret = {
108
139
'n' : n ,
109
140
'total' : t
110
141
}
111
- return r
112
-
113
- # Experimental Recursive requesting to get each 1D column along the axes being requested.
114
- range_recursives = []
115
- for dim in range (self .ndim ):
116
- if dim not in axis :
117
- range_recursives .append (range (extent [dim ].start , extent [dim ].stop ))
118
- else :
119
- range_recursives .append (extent [dim ])
120
- results = np .array (self ._get_elements (active , range_recursives , hyperslab = []))
121
-
122
- t = self ._post_process_data (results ) * n
123
- return {
124
- 'n' : n ,
125
- 'total' : t
126
- }
142
+
143
+ if method == 'mean' :
144
+ return ret
145
+ else :
146
+ return ret ['total' ]/ ret ['n' ]
127
147
128
148
def _get_elements (self , active , recursives , hyperslab = []):
129
149
dimarray = []
@@ -144,26 +164,3 @@ def _get_elements(self, active, recursives, hyperslab=[]):
144
164
dimarray .append (self ._get_elements (active , recursives [1 :], hyperslab = newslab ))
145
165
146
166
return dimarray
147
-
148
- def _determine_chunk_space (chunks , shape , dims , chunk_limits = True ):
149
-
150
- if not chunks :
151
- return None
152
-
153
- chunk_space = [1 for i in range (len (shape ))]
154
-
155
- max_chunks = np .prod (shape )
156
- if chunk_limits :
157
- max_chunks = int (max_chunks / 2e6 )
158
-
159
- for x , d in enumerate (dims ):
160
- if d not in chunks :
161
- continue
162
-
163
- chunks_in_dim = chunks [d ]
164
- if chunks_in_dim > max_chunks :
165
- chunks_in_dim = max_chunks
166
-
167
- chunk_space [x ] = int (shape [x ]/ chunks [d ])
168
-
169
- return tuple (chunk_space )
0 commit comments