File tree Expand file tree Collapse file tree 2 files changed +43
-5
lines changed
Expand file tree Collapse file tree 2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change @@ -146,13 +146,20 @@ def _push_stats(self, data):
146146 if data .size == 0 :
147147 return
148148
149+ # Filter data by histogram range.
150+ if self .hist_range :
151+ stats_data = data [data >= self .hist_range [0 ]]
152+ stats_data = stats_data [stats_data <= self .hist_range [1 ]]
153+ else :
154+ stats_data = data
155+
149156 # Compute incremental statistics
150- self ._stats_t0 += data .size
151- self ._stats_t1 += numpy .sum (data )
152- self ._stats_t2 += numpy .sum (numpy .square (data ))
157+ self ._stats_t0 += stats_data .size
158+ self ._stats_t1 += numpy .sum (stats_data )
159+ self ._stats_t2 += numpy .sum (numpy .square (stats_data ))
153160
154- tile_max = numpy .max (data )
155- tile_min = numpy .min (data )
161+ tile_max = numpy .max (stats_data )
162+ tile_min = numpy .min (stats_data )
156163
157164 if self ._stats_max_value is None :
158165 self ._stats_max_value = tile_max
Original file line number Diff line number Diff line change @@ -283,6 +283,37 @@ def test_histogram_range(self):
283283 '(40.0, 50.0)' : 0 ,
284284 }
285285 )
286+ # Limit histogram range.
287+ agg = Aggregator (
288+ layer_dict = {'a' : self .rasterlayer .id },
289+ formula = 'a' ,
290+ grouping = 'continuous' ,
291+ hist_range = (0.5 , 8 )
292+ )
293+ # Compute value count.
294+ vals = agg .value_count ()
295+ self .assertDictEqual (
296+ vals ,
297+ {
298+ '(0.5, 1.25)' : 695 ,
299+ '(1.25, 2.0)' : 0 ,
300+ '(2.0, 2.75)' : 56 ,
301+ '(2.75, 3.5)' : 4131 ,
302+ '(3.5, 4.25)' : 31490 ,
303+ '(4.25, 5.0)' : 0 ,
304+ '(5.0, 5.75)' : 0 ,
305+ '(5.75, 6.5)' : 0 ,
306+ '(6.5, 7.25)' : 0 ,
307+ '(7.25, 8.0)' : 1350
308+ }
309+ )
310+ # Statistics are clipped to range.
311+ self .assertEqual (agg ._stats_min_value , 1 )
312+ self .assertEqual (agg ._stats_max_value , 8 )
313+ self .assertEqual (agg ._stats_t0 , 37722 )
314+ self .assertEqual (sum (vals .values ()), agg ._stats_t0 )
315+ self .assertEqual (agg ._stats_t1 , 149960 )
316+ self .assertEqual (agg ._stats_t2 , 628338 )
286317
287318 def test_memory_efficient (self ):
288319 agg = Aggregator (
You can’t perform that action at this time.
0 commit comments