Exemplo n.º 1
0
        public void TryGetPartTest()
        {
            // Part identifier: "false"
            //      indexes: 0, 10
            //
            // Part identifier: "true"
            //      indexes: 5, 15
            //
            var falsePart = IndexCollection.FromArray(new int[2] {
                0, 10
            });
            var truePart = IndexCollection.FromArray(new int[2] {
                5, 15
            });

            var target = new IndexPartition <string>
            {
                partIndetifiers = new List <string>(2)
                {
                    "false",
                    "true"
                },

                parts = new Dictionary <string, IndexCollection>(2)
                {
                    { "false", falsePart },
                    { "true", truePart }
                }
            };

            bool partFound;

            partFound = target.TryGetPart("unknown", out IndexCollection part);
            Assert.AreEqual(expected: false, actual: partFound);
            Assert.IsNull(part);

            partFound = target.TryGetPart("false", out part);
            Assert.AreEqual(expected: true, actual: partFound);
            IndexCollectionAssert.AreEqual(expected: falsePart, actual: part);

            partFound = target.TryGetPart("true", out part);
            Assert.AreEqual(expected: true, actual: partFound);
            IndexCollectionAssert.AreEqual(expected: truePart, actual: part);
        }
Exemplo n.º 2
0
        public void IndexerGetTest()
        {
            // Create an array of strings.
            var elements = new string[6] {
                "one",
                "two",
                "one",
                "one",
                "three",
                "three"
            };

            // Partition the array positions by their contents.
            var target = IndexPartition.Create(elements);

            // The partition contains three parts, identified, respectively,
            // by the strings "one", "two", and "three".

            // Expected:
            //
            // Part identifier: one
            //      indexes: 0, 2, 3
            //
            // Part identifier: three
            //      indexes: 4, 5
            //
            // Part identifier: two
            //      indexes: 1

            // partIdentifier is null
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var part = target[(string)null];
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "partIdentifier");
            }

            // partIdentifier is not a key
            {
                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    var part = target["four"];
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage: ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_IS_NOT_A_PART_IDENTIFIER"),
                    expectedParameterName: "partIdentifier");
            }

            // Valid partIdentifier
            {
                var actual = target["one"];

                var expected = IndexCollection.FromArray(new int[3] {
                    0, 2, 3
                });

                IndexCollectionAssert.AreEqual(expected, actual);
            }
        }
Exemplo n.º 3
0
        public void ExplainTest()
        {
            // data is null
            {
                string parameterName = "data";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: null,
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 2);;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // data is null
            {
                string parameterName = "partition";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: null,
                        numberOfExplanatoryFeatures: 2);;
                },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage:
                    ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is zero
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 0);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is negative
            {
                var STR_EXCEPT_PAR_MUST_BE_POSITIVE =
                    ImplementationServices.GetResourceString(
                        "STR_EXCEPT_PAR_MUST_BE_POSITIVE");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: -1);
                },
                    expectedType: typeof(ArgumentOutOfRangeException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_POSITIVE,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is equal to the number of columns in data
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS"),
                        "numberOfExplanatoryFeatures",
                        "data");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 5);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS,
                    expectedParameterName: parameterName);
            }

            // numberOfExplanatoryFeatures is greater than the number of columns in data
            {
                var STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS =
                    string.Format(
                        ImplementationServices.GetResourceString(
                            "STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS"),
                        "numberOfExplanatoryFeatures",
                        "data");

                string parameterName = "numberOfExplanatoryFeatures";

                ArgumentExceptionAssert.Throw(
                    () =>
                {
                    Clusters.Explain(
                        data: DoubleMatrix.Dense(10, 5),
                        partition: IndexPartition.Create(
                            DoubleMatrix.Dense(10, 1)),
                        numberOfExplanatoryFeatures: 6);
                },
                    expectedType: typeof(ArgumentException),
                    expectedPartialMessage:
                    STR_EXCEPT_PAR_MUST_BE_LESS_THAN_OTHER_COLUMNS,
                    expectedParameterName: parameterName);
            }

            // Valid input
            {
                const int numberOfItems = 12;

                var source = DoubleMatrix.Dense(numberOfItems, 1,
                                                new double[numberOfItems]
                {
                    0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
                });

                var partition = IndexPartition.Create(source);
                var data      = DoubleMatrix.Dense(numberOfItems, 7);

                // features 0 to 4
                var g = new GaussianDistribution(mu: 0, sigma: .1);
                for (int j = 0; j < 5; j++)
                {
                    data[":", j] = g.Sample(sampleSize: numberOfItems);
                }

                var partIdentifiers = partition.Identifiers;

                // feature 5 to 6
                double mu = 1.0;
                for (int i = 0; i < partIdentifiers.Count; i++)
                {
                    var part     = partition[partIdentifiers[i]];
                    int partSize = part.Count;
                    g.Mu          = mu;
                    data[part, 5] = g.Sample(sampleSize: partSize);
                    mu           += 2.0;
                    g.Mu          = mu;
                    data[part, 6] = g.Sample(sampleSize: partSize);
                    mu           += 2.0;
                }

                IndexCollection actualFeatureIndexes =
                    Clusters.Explain(
                        data: data,
                        partition: partition,
                        numberOfExplanatoryFeatures: 2);

                IndexCollectionAssert.AreEqual(
                    expected: IndexCollection.Range(5, 6),
                    actual: actualFeatureIndexes);
            }
        }