public void ReadRowsInSecondScanDoNotUseOriginalReaderForEmptyRowsArray()
        {
            // arrange
            const int    rows    = 0;
            const int    columns = 0;
            const double density = 0.01;

            var originalVectors            = SparseVectorHelper.GenerateSparseVectors(rows, columns, density, () => SparseVectorHelper.RandomInInterval(-100, 100, 2)).ToArray();
            var originalReader             = new InMemorySparseMatrixReader(originalVectors);
            var originalReaderWithCounters = new SparseMatrixReaderWithMemberInvocationCounters <double>(originalReader);

            using (var cachedReader = new CachedMatrixMarketReader <double>(originalReaderWithCounters))
            {
                // act
                foreach (var row in cachedReader.ReadRows())
                {
                }
                foreach (var row in cachedReader.ReadRows())
                {
                }

                // assert
                Assert.AreEqual(1, originalReaderWithCounters.ColumnsCountInvocations);
                Assert.AreEqual(1, originalReaderWithCounters.RowsCountInvocations);
                Assert.AreEqual(1, originalReaderWithCounters.ElementsCountInvocations);
                Assert.AreEqual(1, originalReaderWithCounters.ReadRowsInvocations);
            }
        }
        public void ConstructorThrowsArgumentNullExceptionForNullReaderArgument()
        {
            // arrange

            // act
            using (var cachedReader = new CachedMatrixMarketReader <double>(null))
            {
            }

            // assert
            Assert.Fail();
        }
        public void InitializedCachedMatrixMarketReaderReturnsTheSamePropertiesValuesForEmptyRowsArray()
        {
            // arrange
            var reader = new InMemorySparseMatrixReader();

            using (var cachedReader = new CachedMatrixMarketReader <double>(reader))
            {
                // act

                // assert
                Assert.AreEqual(reader.ColumnsCount, cachedReader.ColumnsCount);
                Assert.AreEqual(reader.RowsCount, cachedReader.RowsCount);
                Assert.AreEqual(reader.ElementsCount, cachedReader.ElementsCount);
            }
        }
        public void ReadRowsReturnSameVectorsInFirstScanForEmptyRowsArray()
        {
            // arrange
            const int    rows    = 0;
            const int    columns = 0;
            const double density = 0.01;

            var originalVectors = SparseVectorHelper.GenerateSparseVectors(rows, columns, density, () => SparseVectorHelper.RandomInInterval(-100, 100, 2)).ToArray();
            var originalReader  = new InMemorySparseMatrixReader(originalVectors);

            using (var cachedReader = new CachedMatrixMarketReader <double>(originalReader))
            {
                // act
                var firstScanResults = originalReader.ReadRows().ZipFull(cachedReader.ReadRows(), (originalVector, cachedVector) => originalVector == cachedVector).All(b => b);

                // assert
                Assert.IsTrue(firstScanResults);
            }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            const string matrixFileName = @"c:\temp\matrix.txt";
            const int    rows           = 10000;
            const int    columns        = 1000;
            const double density        = 0.01;

            const int featuresCount   = 10;
            const int iterationsCount = 30;

            Console.WriteLine("Generating matrix {0}x{1} with avg. {2} non-zero elements ({3}%)...", rows, columns, (long)((double)rows * columns * density), density * 100.0);
            using (var file = new FileStream(matrixFileName, FileMode.Create))
                using (var mmwriter = new MatrixMarketWriter <double>(file))
                {
                    var vectors = SparseVectorHelper.GenerateSparseVectors(rows, columns, density, () => SparseVectorHelper.RandomInInterval(0.01, 100, 2));
                    mmwriter.Write(vectors);
                }

            Console.WriteLine("Matrix factorization (features=" + featuresCount + ", iterations=" + iterationsCount + ")...");
            using (var file = new FileStream(matrixFileName, FileMode.Open))
                using (var mmreader = new MatrixMarketReader <double>(file))
                    using (var cachedReader = new CachedMatrixMarketReader <double>(mmreader))
                    {
                        var nmf = new NMF(cachedReader);

                        // act
                        var sw = Stopwatch.StartNew();
                        using (var factorization = nmf.Factorize(featuresCount, iterationsCount))
                        {
                            sw.Stop();
                            Console.WriteLine("Factorization time: " + sw.ElapsedMilliseconds + " ms.");

                            Console.WriteLine("Euclidean distance calculation...");
                            sw = Stopwatch.StartNew();
                            var euclideanDistance = nmf.GetEuclideanDistance(factorization);
                            sw.Stop();
                            Console.WriteLine("Euclidean distance calculation time: " + sw.ElapsedMilliseconds + " ms.");
                            Console.WriteLine("Factorization Euclidean distance: " + euclideanDistance);
                        }
                    }
        }
        static void Main(string[] args)
        {
            const string matrixFileName = @"c:\temp\matrix.txt";
            const int rows = 10000;
            const int columns = 1000;
            const double density = 0.01;

            const int featuresCount = 10;
            const int iterationsCount = 30;

            Console.WriteLine("Generating matrix {0}x{1} with avg. {2} non-zero elements ({3}%)...", rows, columns, (long)((double)rows * columns * density), density * 100.0);
            using (var file = new FileStream(matrixFileName, FileMode.Create))
            using (var mmwriter = new MatrixMarketWriter<double>(file))
            {
                var vectors = SparseVectorHelper.GenerateSparseVectors(rows, columns, density, () => SparseVectorHelper.RandomInInterval(0.01, 100, 2));
                mmwriter.Write(vectors);
            }

            Console.WriteLine("Matrix factorization (features=" + featuresCount + ", iterations=" + iterationsCount + ")...");
            using (var file = new FileStream(matrixFileName, FileMode.Open))
            using (var mmreader = new MatrixMarketReader<double>(file))
            using (var cachedReader = new CachedMatrixMarketReader<double>(mmreader))
            {
                var nmf = new NMF(cachedReader);

                // act
                var sw = Stopwatch.StartNew();
                using (var factorization = nmf.Factorize(featuresCount, iterationsCount))
                {
                    sw.Stop();                    
                    Console.WriteLine("Factorization time: " + sw.ElapsedMilliseconds + " ms.");

                    Console.WriteLine("Euclidean distance calculation...");
                    sw = Stopwatch.StartNew();
                    var euclideanDistance = nmf.GetEuclideanDistance(factorization);
                    sw.Stop();
                    Console.WriteLine("Euclidean distance calculation time: " + sw.ElapsedMilliseconds + " ms.");                   
                    Console.WriteLine("Factorization Euclidean distance: " + euclideanDistance);                    
                }
            }
        }
        public void InitializedCachedMatrixMarketReaderReturnsTheSamePropertiesValues()
        {
            // arrange
            var reader = new InMemorySparseMatrixReader(
                new SparseVector <double> {
                { 0, 22.0 }, { 1, 28.0 }
            },
                new SparseVector <double> {
                { 0, 49.0 }, { 1, 64.0 }
            });

            using (var cachedReader = new CachedMatrixMarketReader <double>(reader))
            {
                // act

                // assert
                Assert.AreEqual(reader.ColumnsCount, cachedReader.ColumnsCount);
                Assert.AreEqual(reader.RowsCount, cachedReader.RowsCount);
                Assert.AreEqual(reader.ElementsCount, cachedReader.ElementsCount);
            }
        }