Skip to content

Commit

Permalink
idea: add naive binning fallback (#101)
Browse files Browse the repository at this point in the history
Co-authored-by: pieterlukasse <pieterlukasse@gmail.com>
  • Loading branch information
tianj7 and pieterlukasse committed May 2, 2024
1 parent 0e68f9d commit 1534186
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions utils/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ type HistogramColumn struct {
NumberOfPeople int `json:"personCount"`
}

const MAX_NUM_BINS = 1000

func GenerateHistogramData(conceptValues []float64) []HistogramColumn {

if len(conceptValues) == 0 {
return nil
}
numBins, width := GetBinsAndWidthUsingFreedmanDiaconisAndSortValues(conceptValues) //conceptValues will get sorted as a side-effect, which is useful in this case
numBins, width := GetBinsAndWidthAndSortValues(conceptValues) //conceptValues will get sorted as a side-effect, which is useful in this case
startValue := conceptValues[0]
binIndexToHistogramColumn := make(map[int]HistogramColumn)

Expand Down Expand Up @@ -50,7 +52,7 @@ func GenerateHistogramData(conceptValues []float64) []HistogramColumn {
}

// Sorts the given values, and returns the number of bins, the width of the bins using FreedmanDiaconis
func GetBinsAndWidthUsingFreedmanDiaconisAndSortValues(values []float64) (int, float64) {
func GetBinsAndWidthAndSortValues(values []float64) (int, float64) {

width := FreedmanDiaconis(values) // values will get sorted as a side-effect, which is useful in this case
startValue := values[0]
Expand All @@ -63,6 +65,11 @@ func GetBinsAndWidthUsingFreedmanDiaconisAndSortValues(values []float64) (int, f
numBins = 1
width = endValue + 1 - startValue
}
// check if numBins is acceptable:
if numBins > MAX_NUM_BINS {
numBins = MAX_NUM_BINS
width = (endValue - startValue) / MAX_NUM_BINS
}
log.Printf("num bins %v", numBins)

return numBins, width
Expand Down

0 comments on commit 1534186

Please sign in to comment.