Пример #1
0
        public void ClassifyChoosesMinimumDistance(ImageDistanceCalculator <int> calculator)
        {
            var data       = new[] { -1, 0, 789 };
            var classifier = new BasicImageClassifier(calculator);

            var observations = new[]
            {
                new ImageObservation <int>("One", new[] { 0, 0, 0 }),
                new ImageObservation <int>("Two", new[] { 789, 0, -1 }),
                new ImageObservation <int>("Three", new[] { 1, 2, 3 }),
                new ImageObservation <int>("Four", new[] { 999, 888, 777 }),
                new ImageObservation <int>("Five", new[] { 1, 0, -789 }),
                new ImageObservation <int>("Six", new[] { 12, 8, 765 })
            };

            var minIndex = 0;
            var value    = 0d;
            var minValue = Double.MaxValue;

            for (var index = 0; index < observations.Length; ++index)
            {
                value = calculator(data, observations[index].Pixels);

                if (value < minValue)
                {
                    minIndex = index;
                    minValue = value;
                }
            }

            classifier.Train(observations);
            classifier.Classify(data).Should().Be(observations[minIndex].Label, $"because classifying should choose the image that is least different from the given data with { calculator.Method.Name } distance");
        }
Пример #2
0
        public void CalculatorDisallowsMismatchedPixelSizes(ImageDistanceCalculator <int> calculator,
                                                            IEnumerable <int> pixels,
                                                            IEnumerable <int> otherPixels)
        {
            Action actionUnderTest = () => calculator(pixels, otherPixels);

            actionUnderTest.Should().Throw <ArgumentException>($"because the two sets of pixels did not have the same size when calling { calculator.Method.Name }");
        }
Пример #3
0
        public void ClassifyIsSuccessfulWithMatchingSingleSet(ImageDistanceCalculator <int> calculator)
        {
            var observations = new[] { new ImageObservation <int>("Test", new[] { 1, 2, 3 }) };
            var classifier   = new BasicImageClassifier(calculator);

            classifier.Train(observations);
            classifier.Classify(observations[0].Pixels).Should().Be(observations[0].Label, $"because the same single set was used for training and classifying with { calculator.Method.Name } distance");
        }
Пример #4
0
        public void CalculatorRequiresPixels(ImageDistanceCalculator <int> calculator,
                                             IEnumerable <int> pixels,
                                             IEnumerable <int> otherPixels)
        {
            var nullArg = (pixels == null) ? nameof(pixels) : nameof(otherPixels);

            Action actionUnderTest = () => calculator(pixels, otherPixels);

            actionUnderTest.Should().Throw <ArgumentNullException>($"because { nullArg } was null when calling { calculator.Method.Name }");
        }
Пример #5
0
        public void ClassifyIsSuccessfulWithSameSetPresent(ImageDistanceCalculator <int> calculator)
        {
            var data       = new[] { 7, 8, 9 };
            var classifier = new BasicImageClassifier(calculator);

            var observations = new[]
            {
                new ImageObservation <int>("Test", new[] { 1, 2, 3 }),
                new ImageObservation <int>("Expected", data)
            };


            classifier.Train(observations);
            classifier.Classify(data).Should().Be(observations[1].Label, $"because a matching set was present in the training with { calculator.Method.Name } distance");
        }
Пример #6
0
        public void ImageCalculatorDetectsTheSameReference(ImageDistanceCalculator <int> calculator,
                                                           IEnumerable <int> pixels,
                                                           IEnumerable <int> otherPixels,
                                                           bool isSameReference)
        {
            var result = calculator(pixels, otherPixels);

            if (isSameReference)
            {
                result.Should().Be(0, $"because the same instance of pixels was passed when calling { calculator.Method.Name }");
            }
            else
            {
                result.Should().NotBe(0, $"because the a different instance of pixels with different values was passed when calling { calculator.Method.Name }");
            }
        }
Пример #7
0
 public void ImageCalculatorDetectsDifferentSets(ImageDistanceCalculator <int> calculator,
                                                 IEnumerable <int> pixels,
                                                 IEnumerable <int> otherPixels)
 {
     calculator(pixels, otherPixels).Should().NotBe(0, $"because the sets were not equal when calling { calculator.Method.Name }");
 }
Пример #8
0
 public BasicImageClassifier(ImageDistanceCalculator <int> calculator) =>
 this.distanceCalculator = calculator ?? throw new ArgumentNullException(nameof(calculator));