histogram

package module
v0.0.0-...-fbb17fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2024 License: MIT Imports: 7 Imported by: 0

README

histogram

This package provides histogramming functionality. It is based on https://github.com/aybabtme/uniplot, but has some enhancements that allow callers to supply their own binning and indexing implementations.

Examples

See the tests for more examples, but the basic usage is as follows:

vs := []float64{0.1, 0.15, 0.1, 0.2, 0.7, 0.4, 0.71}
bs := BSArbitrarySpan(0, 1, 10)(vs)
h, err := Hist(vs, bs, DefaultBucketer)
if err != nil {
    // do something
}
Fprint(os.Stdout, hist, Linear(5))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BSArbitrarySpan

func BSArbitrarySpan(start float64, stop float64, nbins int) func([]float64) ([]Bucket, error)

BSArbitrarySpan returns Buckets conforming to start, stop, and nbins.

func BSExactSpan

func BSExactSpan(nb int) func([]float64) ([]Bucket, error)

BSExactSpan returns Buckets that span the input array from the smallest input to the largest input exactly.

func DefaultBucketer

func DefaultBucketer(val float64, bs []Bucket) int

DefaultBucketer uses BinearySearchFunc to bucket the supplied value.

func Fprint

func Fprint(w io.Writer, h *Histogram, s ScaleFunc) error

Fprint prints a unicode histogram on the io.Writer, using scale s. This code:

hist := Hist(9, data)
err := Fprint(os.Stdout, hist, Linear(5))

... yields the graph:

0.1-0.2  5%   ▋1
0.2-0.3  25%  ██▋5
0.3-0.4  0%   ▏
0.4-0.5  5%   ▋1
0.5-0.6  50%  █████▏10
0.6-0.7  0%   ▏
0.7-0.8  0%   ▏
0.8-0.9  5%   ▋1
0.9-1    10%  █▏2

func Fprintf

func Fprintf(w io.Writer, h *Histogram, s ScaleFunc, f FormatFunc) error

Fprintf is the same as Fprint, but applies f to the axis labels.

Types

type Bucket

type Bucket struct {
	// Count is the number of values represented in the bucket.
	Count int
	// Min is the low, inclusive bound of the bucket.
	Min float64
	// Max is the high, exclusive bound of the bucket.
	Max float64
}

Bucket counts a partion of values.

func Linspace

func Linspace(start float64, stop float64, nb int) ([]Bucket, error)

type Bucketer

type Bucketer func(val float64, bs []Bucket) int

Bucketer is a function that takes a value and returns the bucket index in the supplied slice of Buckets. Implementations should return -1 if the value does not fit in a bucket. Callers MUST ensure that bs is sorted.

type FormatFunc

type FormatFunc func(v float64) string

FormatFunc formats a float into the proper string form. Used to print meaningful axe labels.

type Histogram

type Histogram struct {
	// Min is the size of the smallest bucket.
	Min int
	// Max is the size of the biggest bucket.
	Max int
	// Count is the total size of all buckets.
	Count int
	// Buckets over which values are partionned.
	Buckets []Bucket
}

Histogram holds a count of values partionned over buckets.

func Hist

func Hist(input []float64, buckets []Bucket, bf Bucketer) (*Histogram, error)

func (Histogram) Scale

func (h Histogram) Scale(s ScaleFunc, idx int) float64

Scale gives the scaled count of the bucket at idx, using the provided scale func.

type ScaleFunc

type ScaleFunc func(min, max, value int) float64

ScaleFunc is the type to implement to scale an histogram.

func Linear

func Linear(width int) ScaleFunc

Linear builds a ScaleFunc that will linearly scale the values of an histogram so that they do not exceed width.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL