func (c *collection) GetNewDistribution(args ...interface{}) ([]interface{}, error) { var err error newConfig := distribution.DefaultConfig() newConfig.Name, err = ArgToString(args, 0, newConfig.Name) if err != nil { return nil, maskAny(err) } newConfig.StaticChannels, err = ArgToFloat64Slice(args, 1, newConfig.StaticChannels) if err != nil { return nil, maskAny(err) } newConfig.Vectors, err = ArgToFloat64SliceSlice(args, 2, newConfig.Vectors) if err != nil { return nil, maskAny(err) } if len(args) > 3 { return nil, maskAnyf(tooManyArgumentsError, "expected 3 got %d", len(args)) } newDistribution, err := distribution.NewDistribution(newConfig) if err != nil { return nil, maskAny(err) } return []interface{}{newDistribution}, nil }
// NewFeature creates a new configured feature object. A feature represents a // differentiable part of a given sequence. func NewFeature(config FeatureConfig) (spec.Feature, error) { newFeature := &feature{ Distribution: nil, FeatureConfig: config, ID: id.MustNewID(), Mutex: sync.Mutex{}, Type: ObjectTypeFeature, } if len(newFeature.Positions) == 0 { return nil, maskAnyf(invalidConfigError, "positions must not be empty") } if newFeature.Sequence == "" { return nil, maskAnyf(invalidConfigError, "sequence must not be empty") } newConfig := distribution.DefaultConfig() newConfig.Name = newFeature.Sequence newConfig.Vectors = newFeature.Positions newDistribution, err := distribution.NewDistribution(newConfig) if err != nil { return nil, maskAnyf(invalidConfigError, err.Error()) } newFeature.Distribution = newDistribution return newFeature, nil }
func Test_Distribution_DifferenceDistribution(t *testing.T) { testDistribution := func(staticChannels []float64, vectors [][]float64) spec.Distribution { newConfig := distribution.DefaultConfig() newConfig.Name = "name" newConfig.StaticChannels = staticChannels newConfig.Vectors = vectors newDistribution, err := distribution.NewDistribution(newConfig) if err != nil { t.Fatal("expected", nil, "got", err) } return newDistribution } testCases := []struct { Input []interface{} Expected []interface{} ErrorMatcher func(err error) bool }{ { Input: []interface{}{ testDistribution([]float64{50, 100}, [][]float64{{11, 22}, {33, 44}}), testDistribution([]float64{50, 100}, [][]float64{{35, 48}, {13, 22}}), }, Expected: []interface{}{[]float64{0, 0}}, ErrorMatcher: nil, }, { Input: []interface{}{ testDistribution([]float64{50, 100}, [][]float64{{11, 22}, {33, 44}}), testDistribution([]float64{50, 100}, [][]float64{{57, 84}, {69, 87}}), }, Expected: []interface{}{[]float64{-2, 2}}, ErrorMatcher: nil, }, { Input: []interface{}{ testDistribution([]float64{50, 100}, [][]float64{{11, 22}, {33, 44}}), testDistribution([]float64{50, 100}, [][]float64{{57, 84}, {69, 87}}), "foo", }, Expected: nil, ErrorMatcher: IsTooManyArguments, }, { Input: []interface{}{ 81, testDistribution([]float64{50, 100}, [][]float64{{57, 84}, {69, 87}}), }, Expected: nil, ErrorMatcher: IsWrongArgumentType, }, { Input: []interface{}{ testDistribution([]float64{50, 100}, [][]float64{{57, 84}, {69, 87}}), 81, }, Expected: nil, ErrorMatcher: IsWrongArgumentType, }, { Input: []interface{}{}, Expected: nil, ErrorMatcher: IsNotEnoughArguments, }, } for i, testCase := range testCases { output, err := testMaybeNewCollection(t).DifferenceDistribution(testCase.Input...) if (err != nil && testCase.ErrorMatcher == nil) || (testCase.ErrorMatcher != nil && !testCase.ErrorMatcher(err)) { t.Fatal("case", i+1, "expected", true, "got", false) } if testCase.ErrorMatcher == nil { if !reflect.DeepEqual(output, testCase.Expected) { t.Fatal("case", i+1, "expected", testCase.Expected, "got", output) } } } }
func Test_Distribution_GetVectorsDistribution(t *testing.T) { testDistribution := func(vectors [][]float64) spec.Distribution { newConfig := distribution.DefaultConfig() newConfig.Name = "test-name" newConfig.Vectors = vectors newDistribution, err := distribution.NewDistribution(newConfig) if err != nil { t.Fatal("expected", nil, "got", err) } return newDistribution } testCases := []struct { Input []interface{} Expected []interface{} ErrorMatcher func(err error) bool }{ { Input: []interface{}{testDistribution([][]float64{{1, 1}, {2, 7}})}, Expected: []interface{}{[][]float64{{1, 1}, {2, 7}}}, ErrorMatcher: nil, }, { Input: []interface{}{testDistribution([][]float64{{3, 2}, {10, 3}})}, Expected: []interface{}{[][]float64{{3, 2}, {10, 3}}}, ErrorMatcher: nil, }, { Input: []interface{}{testDistribution([][]float64{{11, 22}, {33, 44}})}, Expected: []interface{}{[][]float64{{11, 22}, {33, 44}}}, ErrorMatcher: nil, }, { Input: []interface{}{testDistribution([][]float64{{11, 22}, {33, 44}}), "foo"}, Expected: nil, ErrorMatcher: IsTooManyArguments, }, { Input: []interface{}{8.1}, Expected: nil, ErrorMatcher: IsWrongArgumentType, }, { Input: []interface{}{}, Expected: nil, ErrorMatcher: IsNotEnoughArguments, }, } for i, testCase := range testCases { output, err := testMaybeNewCollection(t).GetVectorsDistribution(testCase.Input...) if (err != nil && testCase.ErrorMatcher == nil) || (testCase.ErrorMatcher != nil && !testCase.ErrorMatcher(err)) { t.Fatal("case", i+1, "expected", true, "got", false) } if testCase.ErrorMatcher == nil { if !reflect.DeepEqual(output, testCase.Expected) { t.Fatal("case", i+1, "expected", testCase.Expected, "got", output) } } } }