diff options
author | Sergey Grebenshchikov <sgreben@gmail.com> | 2018-03-30 21:10:19 +0200 |
---|---|---|
committer | Sergey Grebenshchikov <sgreben@gmail.com> | 2018-03-30 21:10:19 +0200 |
commit | 0f9e84d2a4754c51cba79615820bcd53b0d9dd48 (patch) | |
tree | b5c944dd43682c09dd6543831f43450a667a1ac1 /pkg/data/histogram.go | |
parent | df8bf8e12216702a3291e214624825304c3ac886 (diff) |
Add heatmap plots1.1.8
Diffstat (limited to 'pkg/data/histogram.go')
-rw-r--r-- | pkg/data/histogram.go | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/pkg/data/histogram.go b/pkg/data/histogram.go index e16d000..be72142 100644 --- a/pkg/data/histogram.go +++ b/pkg/data/histogram.go @@ -23,6 +23,7 @@ type Bin struct { Right float64 RightInclusive bool Count uint64 + CountNorm float64 } func (b *Bin) String() string { @@ -38,16 +39,16 @@ type Bins struct { numPoints int } -func (b *Bins) ChooseSqrt() { - b.Number = int(math.Sqrt(float64(b.numPoints))) +func BinsSqrt(numPoints int) int { + return int(math.Sqrt(float64(numPoints))) } -func (b *Bins) ChooseSturges() { - b.Number = int(math.Ceil(math.Log2(float64(b.numPoints))) + 1) +func BinsSturges(numPoints int) int { + return int(math.Ceil(math.Log2(float64(numPoints))) + 1) } -func (b *Bins) ChooseRice() { - b.Number = int(math.Ceil(2 * math.Pow(float64(b.numPoints), 1.0/3.0))) +func BinsRice(numPoints int) int { + return int(math.Ceil(2 * math.Pow(float64(numPoints), 1.0/3.0))) } func NewBins(points []float64) *Bins { @@ -106,3 +107,42 @@ func Histogram(points []float64, bins *Bins) (out []Bin) { } return } + +type Bins2D struct { + X *Bins + Y *Bins +} + +func NewBins2D(points [][2]float64) *Bins2D { + bins := new(Bins2D) + xs := make([]float64, len(points)) + ys := make([]float64, len(points)) + for i := range points { + xs[i] = points[i][0] + ys[i] = points[i][1] + } + bins.X = NewBins(xs) + bins.Y = NewBins(ys) + return bins +} + +func Histogram2D(points [][2]float64, bins *Bins2D) (x, y []Bin, z [][]uint64) { + x = bins.X.All() + y = bins.Y.All() + z = make([][]uint64, len(y)) + for _, b := range x { + b.Count = 0 + } + for i, b := range y { + z[i] = make([]uint64, len(x)) + b.Count = 0 + } + for _, p := range points { + i := bins.X.Point(p[0]) + j := bins.Y.Point(p[1]) + x[i].Count++ + y[j].Count++ + z[i][j]++ + } + return +} |