@@ -59,8 +59,8 @@ def _weighted_stdev(
59
59
values : NDArray [numpy .floating ],
60
60
weights : NDArray [numpy .floating ],
61
61
) -> float :
62
- average = numpy .average (values , weights = weights )
63
- variance = numpy .average ((values - average ) ** 2 , weights = weights )
62
+ average = numpy .ma . average (values , weights = weights )
63
+ variance = numpy .ma . average ((values - average ) ** 2 , weights = weights )
64
64
return float (math .sqrt (variance ))
65
65
66
66
@@ -174,35 +174,44 @@ def get_array_statistics(
174
174
_weighted_quantiles (data_comp , masked_coverage .compressed (), pp / 100.0 )
175
175
for pp in percentiles
176
176
]
177
- else :
178
- percentiles_values = [numpy .nan ] * len (percentiles_names )
179
-
180
- if valid_pixels :
181
177
majority = float (keys [counts .tolist ().index (counts .max ())].tolist ())
182
178
minority = float (keys [counts .tolist ().index (counts .min ())].tolist ())
179
+ std = _weighted_stdev (data_comp , masked_coverage .compressed ())
180
+ med = _weighted_quantiles (data_comp , masked_coverage .compressed ())
181
+ _min = float (data [b ].min ())
182
+ _max = float (data [b ].max ())
183
+ _mean = float (data_cov .sum () / masked_coverage .sum ())
184
+ _count = float (masked_coverage .sum ())
185
+ _sum = float (data_cov .sum ())
186
+
183
187
else :
188
+ percentiles_values = [numpy .nan ] * len (percentiles_names )
184
189
majority = numpy .nan
185
190
minority = numpy .nan
186
-
187
- _count = masked_coverage .sum ()
188
- _sum = data_cov .sum ()
191
+ std = numpy .nan
192
+ med = numpy .nan
193
+ _min = numpy .nan
194
+ _max = numpy .nan
195
+ _count = 0
196
+ _sum = 0
197
+ _mean = numpy .nan
189
198
190
199
output .append (
191
200
{
192
201
# Minimum value, not taking coverage fractions into account.
193
- "min" : float ( data [ b ]. min ()) ,
202
+ "min" : _min ,
194
203
# Maximum value, not taking coverage fractions into account.
195
- "max" : float ( data [ b ]. max ()) ,
204
+ "max" : _max ,
196
205
# Mean value, weighted by the percent of each cell that is covered.
197
- "mean" : float ( _sum / _count ) ,
206
+ "mean" : _mean ,
198
207
# Sum of all non-masked cell coverage fractions.
199
- "count" : float ( _count ) ,
208
+ "count" : _count ,
200
209
# Sum of values, weighted by their coverage fractions.
201
- "sum" : float ( _sum ) ,
210
+ "sum" : _sum ,
202
211
# Population standard deviation of cell values, taking into account coverage fraction.
203
- "std" : _weighted_stdev ( data_comp , masked_coverage . compressed ()) ,
212
+ "std" : std ,
204
213
# Median value of cells, weighted by the percent of each cell that is covered.
205
- "median" : _weighted_quantiles ( data_comp , masked_coverage . compressed ()) ,
214
+ "median" : med ,
206
215
# The value occupying the greatest number of cells.
207
216
"majority" : majority ,
208
217
# The value occupying the least number of cells.
0 commit comments