mirror of
https://github.com/lightninglabs/faraday.git
synced 2026-08-20 13:28:33 +02:00
dataset: add get threshold function to dataset
This commit is contained in:
parent
d4c7b7d156
commit
bc779d8c79
2 changed files with 93 additions and 0 deletions
|
|
@ -180,3 +180,29 @@ func (d Dataset) GetOutliers(outlierMultiplier float64) (map[string]*OutlierResu
|
|||
|
||||
return outliers, nil
|
||||
}
|
||||
|
||||
// GetThreshold returns the set of values in a dataset <= or > a given
|
||||
// threshold. The below bool is used to toggle whether we identify values
|
||||
// above or below the threshold.
|
||||
func (d Dataset) GetThreshold(thresholdValue float64, below bool) map[string]bool {
|
||||
threshold := make(map[string]bool, len(d.rawValues()))
|
||||
|
||||
for label, value := range d {
|
||||
// If we are looking for values below the threshold, check the
|
||||
// current value then move on to the next one.
|
||||
if below {
|
||||
// If the value is below or equal to the threshold, we
|
||||
// set the label's value in the map to true. Otherwise
|
||||
// we set it to false.
|
||||
threshold[label] = value <= thresholdValue
|
||||
continue
|
||||
}
|
||||
|
||||
// We are looking for values above the threshold. Set the
|
||||
// label's value to true if the value is greater than the
|
||||
// threshold.
|
||||
threshold[label] = value > thresholdValue
|
||||
}
|
||||
|
||||
return threshold
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package dataset
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -242,3 +243,69 @@ func TestIsOutlier(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetThreshold tests getting thresholds for a dataset.
|
||||
func TestGetThreshold(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dataset Dataset
|
||||
threshold float64
|
||||
below bool
|
||||
expectedValues map[string]bool
|
||||
}{
|
||||
{
|
||||
name: "no values",
|
||||
dataset: make(map[string]float64),
|
||||
threshold: 1,
|
||||
below: false,
|
||||
expectedValues: make(map[string]bool),
|
||||
},
|
||||
{
|
||||
name: "below and equal",
|
||||
dataset: map[string]float64{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"c": 3,
|
||||
},
|
||||
threshold: 2,
|
||||
below: true,
|
||||
expectedValues: map[string]bool{
|
||||
"a": true,
|
||||
"b": true,
|
||||
"c": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "above and equal",
|
||||
dataset: map[string]float64{
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"c": 3,
|
||||
},
|
||||
threshold: 2,
|
||||
below: false,
|
||||
expectedValues: map[string]bool{
|
||||
"a": false,
|
||||
"b": false,
|
||||
"c": true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
values := test.dataset.GetThreshold(
|
||||
test.threshold, test.below,
|
||||
)
|
||||
|
||||
if !reflect.DeepEqual(test.expectedValues, values) {
|
||||
t.Fatalf("expected: %v, got: %v",
|
||||
test.expectedValues, values)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue