forked from airbusgeo/godal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.go
110 lines (94 loc) · 3.19 KB
/
histogram.go
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
// Copyright 2021 Airbus Defence and Space
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package godal
// Histogram is a band's histogram.
type Histogram struct {
min, max float64
counts []uint64
}
// Bucket is a histogram entry. It spans [Min,Max] and contains Count entries.
type Bucket struct {
Min, Max float64
Count uint64
}
//Len returns the number of buckets contained in the histogram
func (h Histogram) Len() int {
return len(h.counts)
}
//Bucket returns the i'th bucket in the histogram. i must be between 0 and Len()-1.
func (h Histogram) Bucket(i int) Bucket {
width := (h.max - h.min) / float64(len(h.counts))
return Bucket{
Min: h.min + width*float64(i),
Max: h.min + width*float64(i+1),
Count: h.counts[i],
}
}
type histogramOpts struct {
approx int
includeOutside int
min, max float64
buckets int32
errorHandler ErrorHandler
}
// HistogramOption is an option that can be passed to Band.Histogram()
//
// Available HistogramOptions are:
// - Approximate() to allow the algorithm to operate on a subset of the full resolution data
// - Intervals(count int, min,max float64) to compute a histogram with count buckets, spanning [min,max].
// Each bucket will be (max-min)/count wide. If not provided, the default histogram will be returned.
// - IncludeOutOfRange() to populate the first and last bucket with values under/over the specified min/max
// when used in conjuntion with Intervals()
// - ErrLogger
type HistogramOption interface {
setHistogramOpt(ho *histogramOpts)
}
type includeOutsideOpt struct{}
func (ioo includeOutsideOpt) setHistogramOpt(ho *histogramOpts) {
ho.includeOutside = 1
}
// IncludeOutOfRange populates the first and last bucket with values under/over the specified min/max
// when used in conjuntion with Intervals()
func IncludeOutOfRange() interface {
HistogramOption
} {
return includeOutsideOpt{}
}
type approximateOkOption struct{}
func (aoo approximateOkOption) setHistogramOpt(ho *histogramOpts) {
ho.approx = 1
}
// Approximate allows the histogram algorithm to operate on a subset of the full resolution data
func Approximate() interface {
HistogramOption
StatisticsOption
} {
return approximateOkOption{}
}
type intervalsOption struct {
min, max float64
buckets int32
}
func (io intervalsOption) setHistogramOpt(ho *histogramOpts) {
ho.min = io.min
ho.max = io.max
ho.buckets = io.buckets
}
// Intervals computes a histogram with count buckets, spanning [min,max].
// Each bucket will be (max-min)/count wide. If not provided, the default histogram will be returned.
func Intervals(count int, min, max float64) interface {
HistogramOption
} {
return intervalsOption{min: min, max: max, buckets: int32(count)}
}