Example #1
0
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
}
Example #2
0
// 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
}
Example #3
0
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)
			}
		}
	}
}
Example #4
0
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)
			}
		}
	}
}
Example #5
0
func Test_Distribution_GetNewDistribution(t *testing.T) {
	testCases := []struct {
		// Note that this input is used to validate the output. We only want to
		// ensure that the distribution that is dynmaically created within the CLG
		// is properly configured. Thus the input configuration should equal the
		// output configuration.
		Input        []interface{}
		ErrorMatcher func(err error) bool
	}{
		{
			Input:        []interface{}{"name", DefaultArg{}, [][]float64{{1, 2}, {3, 4}, {5, 6}}},
			ErrorMatcher: nil,
		},
		{
			Input:        []interface{}{"foo", DefaultArg{}, [][]float64{{4, 5}, {6, 7}, {8, 9}}},
			ErrorMatcher: nil,
		},
		{
			Input:        []interface{}{true, DefaultArg{}, [][]float64{{4, 5}, {6, 7}, {8, 9}}},
			ErrorMatcher: IsWrongArgumentType,
		},
		{
			Input:        []interface{}{"foo", 3.4, [][]float64{{4, 5}, {6, 7}, {8, 9}}},
			ErrorMatcher: IsWrongArgumentType,
		},
		{
			Input:        []interface{}{"foo", DefaultArg{}, []int{1, 2, 3}},
			ErrorMatcher: IsWrongArgumentType,
		},
		{
			Input:        []interface{}{"foo", DefaultArg{}, [][]float64{{4, 5}, {6, 7}, {8, 9}}, "foo"},
			ErrorMatcher: IsTooManyArguments,
		},
		{
			Input:        []interface{}{"foo", DefaultArg{}},
			ErrorMatcher: distribution.IsInvalidConfig,
		},
		{
			Input:        []interface{}{},
			ErrorMatcher: distribution.IsInvalidConfig,
		},
		{
			Input:        []interface{}{"foo", 3.4, [][]float64{}},
			ErrorMatcher: distribution.IsInvalidConfig,
		},
	}

	for i, testCase := range testCases {
		if i != 7 {
			continue
		}
		// Test.
		output, err := testMaybeNewCollection(t).GetNewDistribution(testCase.Input...)
		if (err != nil && testCase.ErrorMatcher == nil) || (testCase.ErrorMatcher != nil && !testCase.ErrorMatcher(err)) {
			t.Fatal("case", i+1, "expected", true, "got", false)
		}
		// Convert.
		if testCase.ErrorMatcher == nil {
			if len(output) > 1 {
				t.Fatal("case", i+1, "expected", 1, "got", len(output))
			}
			d, err := ArgToDistribution(output, 0)
			testMaybeFatalCase(t, i, err)
			defaultConfig := distribution.DefaultConfig()

			// Assert. Note that we don't test the hash map here, since this is not a
			// configuration getter.
			name, err := ArgToString(testCase.Input, 1, defaultConfig.Name)
			testMaybeFatalCase(t, i, err)
			if d.GetName() != name {
				t.Fatal("case", i+1, "expected", name, "got", d.GetName())
			}
			staticChannels, err := ArgToFloat64Slice(testCase.Input, 2, defaultConfig.StaticChannels)
			testMaybeFatalCase(t, i, err)
			if !reflect.DeepEqual(d.GetStaticChannels(), staticChannels) {
				t.Fatal("case", i+1, "expected", staticChannels, "got", d.GetStaticChannels())
			}
			vectors, err := ArgToFloat64SliceSlice(testCase.Input, 3, defaultConfig.Vectors)
			testMaybeFatalCase(t, i, err)
			if !reflect.DeepEqual(d.GetVectors(), vectors) {
				t.Fatal("case", i+1, "expected", vectors, "got", d.GetVectors())
			}
		}
	}
}