예제 #1
0
        public void TestSerializeParameters()
        {
            Parameters p = GetParameters();

            SerialConfig config = new SerialConfig("testSerializeParameters", SerialConfig.SERIAL_TEST_DIR);

            IPersistenceAPI api = Persistence.Get(config);

            // 1. serialize
            byte[] data = api.Write(p, "testSerializeParameters");

            // 2. deserialize
            Parameters serialized = api.Read <Parameters>(data);

            Assert.IsTrue(p.Keys().Count == serialized.Keys().Count);
            Assert.IsTrue(p.IsDeepEqual(serialized));
            foreach (Parameters.KEY k in p.Keys())
            {
                DeepCompare(serialized.GetParameterByKey(k), p.GetParameterByKey(k));
            }

            // 3. reify from file
            /////////////////////////////////////
            //  SHOW RETRIEVAL USING FILENAME  //
            /////////////////////////////////////
            Parameters fromFile = api.Read <Parameters>("testSerializeParameters");

            Assert.IsTrue(p.Keys().Count == fromFile.Keys().Count);
            Assert.IsTrue(p.IsDeepEqual(fromFile));
            foreach (Parameters.KEY k in p.Keys())
            {
                DeepCompare(fromFile.GetParameterByKey(k), p.GetParameterByKey(k));
            }
        }
예제 #2
0
 public static IPersistenceAPI Get(SerialConfig config)
 {
     if (access == null)
     {
         access = new PersistenceAccess(config);
     }
     return(access);
 }
예제 #3
0
        public void TestSerializeAnomalyLikelihoodForUpdates()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.LIKELIHOOD);

            AnomalyLikelihood an = (AnomalyLikelihood)Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeAnomalyLikelihood", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(an);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            AnomalyLikelihood serializedAn = api.Read <AnomalyLikelihood>(bytes);

            Assert.IsNotNull(serializedAn);

            //----------------------------------------
            // Step 1. Generate an initial estimate using fake distribution of anomaly scores.
            List <Sample>            data1    = AnomalyLikelihoodTest.GenerateSampleData(0.2, 0.2, 0.2, 0.2).Take(1000).ToList();
            AnomalyLikelihoodMetrics metrics1 = serializedAn.EstimateAnomalyLikelihoods(data1, 5, 0);

            //----------------------------------------
            // Step 2. Generate some new data with a higher average anomaly
            // score. Using the estimator from step 1, to compute likelihoods. Now we
            // should see a lot more anomalies.
            List <Sample>            data2    = AnomalyLikelihoodTest.GenerateSampleData(0.6, 0.2, 0.2, 0.2).Take(300).ToList();
            AnomalyLikelihoodMetrics metrics2 = serializedAn.UpdateAnomalyLikelihoods(data2, metrics1.GetParams());

            // Serialize the Metrics too just to be sure everything can be serialized
            SerialConfig metricsConfig = new SerialConfig("testSerializeMetrics", SerialConfig.SERIAL_TEST_DIR);

            api = Persistence.Get(metricsConfig);
            api.Write(metrics2);

            // Deserialize the Metrics
            AnomalyLikelihoodMetrics serializedMetrics = api.Read <AnomalyLikelihoodMetrics>();

            Assert.IsNotNull(serializedMetrics);

            Assert.AreEqual(serializedMetrics.GetLikelihoods().Length, data2.Count);
            Assert.AreEqual(serializedMetrics.GetAvgRecordList().Count, data2.Count);
            Assert.IsTrue(serializedAn.IsValidEstimatorParams(serializedMetrics.GetParams()));

            // The new running total should be different
            Assert.IsFalse(metrics1.GetAvgRecordList().Total == serializedMetrics.GetAvgRecordList().Total);

            // We should have many more samples where likelihood is < 0.01, but not all
            int conditionCount = ArrayUtils.Where(serializedMetrics.GetLikelihoods(), d => d < 0.1).Length;

            Assert.IsTrue(conditionCount >= 25);
            Assert.IsTrue(conditionCount <= 250);
        }
예제 #4
0
        public void TestSerializeCLAClassifier()
        {
            CLAClassifier        classifier     = new CLAClassifier(new int[] { 1 }, 0.1, 0.1, 0);
            int                  recordNum      = 0;
            Map <String, Object> classification = new Map <string, object>();

            classification.Add("bucketIdx", 4);
            classification.Add("actValue", 34.7);
            Classification <double> result = classifier.Compute <double>(recordNum, classification, new int[] { 1, 5, 9 }, true, true);

            recordNum += 1;

            classification.Add("bucketIdx", 5);
            classification.Add("actValue", 41.7);
            result     = classifier.Compute <double>(recordNum, classification, new int[] { 0, 6, 9, 11 }, true, true);
            recordNum += 1;

            classification.Add("bucketIdx", 5);
            classification.Add("actValue", 44.9);
            result     = classifier.Compute <double>(recordNum, classification, new int[] { 6, 9 }, true, true);
            recordNum += 1;

            classification.Add("bucketIdx", 4);
            classification.Add("actValue", 42.9);
            result     = classifier.Compute <double>(recordNum, classification, new int[] { 1, 5, 9 }, true, true);
            recordNum += 1;

            // Serialize the Metrics too just to be sure everything can be serialized
            SerialConfig    config = new SerialConfig("testSerializeCLAClassifier", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            api.Write(classifier);

            // Deserialize the Metrics
            CLAClassifier serializedClassifier = api.Read <CLAClassifier>();

            Assert.IsNotNull(serializedClassifier);

            //Using the deserialized classifier, continue test
            classification.Add("bucketIdx", 4);
            classification.Add("actValue", 34.7);
            result     = serializedClassifier.Compute <double>(recordNum, classification, new int[] { 1, 5, 9 }, true, true);
            recordNum += 1;

            Assert.IsTrue(Arrays.AreEqual(new int[] { 1 }, result.StepSet()));
            Assert.AreEqual(35.520000457763672, result.GetActualValue(4), 0.00001);
            Assert.AreEqual(42.020000457763672, result.GetActualValue(5), 0.00001);
            Assert.AreEqual(6, result.GetStatCount(1));
            Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001);
            Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001);
            Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001);
            Assert.AreEqual(0.0, result.GetStat(1, 3), 0.00001);
            Assert.AreEqual(0.12300123, result.GetStat(1, 4), 0.00001);
            Assert.AreEqual(0.87699877, result.GetStat(1, 5), 0.00001);
        }
예제 #5
0
        public void TestSerialization()
        {
            _classifier = new CLAClassifier(new[] { 1 }, 0.1, 0.1, 0);
            int recordNum = 0;
            Map <string, object> classification = new Map <string, object>();

            classification.Add("bucketIdx", 4);
            classification.Add("actValue", 34.7);
            Classification <double> result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true);

            recordNum += 1;

            classification.Add("bucketIdx", 5);
            classification.Add("actValue", 41.7);
            result     = _classifier.Compute <double>(recordNum, classification, new[] { 0, 6, 9, 11 }, true, true);
            recordNum += 1;

            classification.Add("bucketIdx", 5);
            classification.Add("actValue", 44.9);
            result     = _classifier.Compute <double>(recordNum, classification, new[] { 6, 9 }, true, true);
            recordNum += 1;

            classification.Add("bucketIdx", 4);
            classification.Add("actValue", 42.9);
            result     = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true);
            recordNum += 1;

            // Configure serializer
            SerialConfig    config = new SerialConfig("testSerializerClassifier", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            // 1. Serialize
            byte[] data = api.Write(_classifier, "testSerializeClassifier");

            // 2. Deserialize
            CLAClassifier serialized = api.Read <CLAClassifier>(data);

            // Using the deserialized classifier, continue test
            classification.Add("bucketIdx", 4);
            classification.Add("actValue", 34.7);
            result     = serialized.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true);
            recordNum += 1;

            Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet()));
            Assert.AreEqual(35.520000457763672, result.GetActualValue(4), 0.00001);
            Assert.AreEqual(42.020000457763672, result.GetActualValue(5), 0.00001);
            Assert.AreEqual(6, result.GetStatCount(1));
            Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001);
            Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001);
            Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001);
            Assert.AreEqual(0.0, result.GetStat(1, 3), 0.00001);
            Assert.AreEqual(0.12300123, result.GetStat(1, 4), 0.00001);
            Assert.AreEqual(0.87699877, result.GetStat(1, 5), 0.00001);
        }
예제 #6
0
        public void TestRegionSerialisation()
        {
            Net.Network.Network n = new Net.Network.Network("network", Parameters.Empty());
            Region r = new Region("myRegion", n);

            IPersistenceAPI pa = Persistence.Get(new SerialConfig(null, SerialConfig.SERIAL_TEST_DIR));

            byte[] bytes = pa.Serializer().Serialize(r);
            Region r2    = pa.Serializer().Deserialize <Region>(bytes);

            Assert.AreEqual(r, r2);
        }
예제 #7
0
 public void TestHierarchicalNetwork()
 {
     Net.Network.Network network = GetLoadedHotGymHierarchy();
     try
     {
         SerialConfig    config = new SerialConfig("testSerializeHierarchy", SerialConfig.SERIAL_TEST_DIR);
         IPersistenceAPI api    = Persistence.Get(config);
         api.Store(network);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         Assert.Fail();
     }
 }
예제 #8
0
        public void TestSerializeConnections()
        {
            Parameters  p   = GetParameters();
            Connections con = new Connections();

            p.Apply(con);

            TemporalMemory.Init(con);

            SerialConfig    config = new SerialConfig("testSerializeConnections", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            // 1. serialize
            byte[] data = api.Write(con);

            // 2. deserialize
            Connections serialized = api.Read <Connections>(data);

            Assert.IsTrue(con.IsDeepEqual(serialized));

            serialized.PrintParameters();
            int cellCount = con.GetCellsPerColumn();

            for (int i = 0; i < con.GetNumColumns(); i++)
            {
                DeepCompare(con.GetColumn(i), serialized.GetColumn(i));
                for (int j = 0; j < cellCount; j++)
                {
                    Cell cell = serialized.GetCell(i * cellCount + j);
                    DeepCompare(con.GetCell(i * cellCount + j), cell);
                }
            }

            // 3. reify from file
            Connections fromFile = api.Read <Connections>(data);

            Assert.IsTrue(con.IsDeepEqual(fromFile));
            for (int i = 0; i < con.GetNumColumns(); i++)
            {
                DeepCompare(con.GetColumn(i), fromFile.GetColumn(i));
                for (int j = 0; j < cellCount; j++)
                {
                    Cell cell = fromFile.GetCell(i * cellCount + j);
                    DeepCompare(con.GetCell(i * cellCount + j), cell);
                }
            }
        }
예제 #9
0
        public void TestEnsurePathExists()
        {
            SerialConfig    config  = new SerialConfig("testEnsurePathExists", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI persist = Persistence.Get();

            persist.SetConfig(config);

            try
            {
                ((Persistence.PersistenceAccess)persist).EnsurePathExists(config);
            }
            catch (Exception e) { Assert.Fail(); }

            FileInfo f1 = new FileInfo(Environment.CurrentDirectory + "\\" + config.GetFileDir() + "\\" + "testEnsurePathExists");

            Assert.IsTrue(f1.Exists);
        }
예제 #10
0
        public void TestSerializeAnomalyLikelihood()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.LIKELIHOOD);

            AnomalyLikelihood an = (AnomalyLikelihood)Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeAnomalyLikelihood", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(an);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            Anomaly serializedAn = api.Read <Anomaly>(bytes);

            Assert.IsNotNull(serializedAn);
        }
예제 #11
0
        public void TestSerializeAnomaly()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            Anomaly anomalyComputer = Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeAnomaly1", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(anomalyComputer);

            double score = anomalyComputer.Compute(new int[0], new int[0], 0, 0);

            score = anomalyComputer.Compute(new int[0], new int[0], 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = anomalyComputer.Compute(new int[0], new int[] { 3, 5 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = anomalyComputer.Compute(new int[] { 3, 5, 7 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = anomalyComputer.Compute(new int[] { 2, 3, 6 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(2.0 / 3.0, score, 0);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            Anomaly serializedAnomalyComputer = api.Read <Anomaly>(bytes);

            score = serializedAnomalyComputer.Compute(new int[0], new int[0], 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = serializedAnomalyComputer.Compute(new int[0], new int[] { 3, 5 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = serializedAnomalyComputer.Compute(new int[] { 3, 5, 7 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(0.0, score, 0);

            score = serializedAnomalyComputer.Compute(new int[] { 2, 3, 6 }, new int[] { 3, 5, 7 }, 0, 0);
            Assert.AreEqual(2.0 / 3.0, score, 0);
        }
예제 #12
0
        public void TestSerializeCumulativeAnomaly()
        {
            Parameters @params = Parameters.Empty();

            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_MODE, Anomaly.Mode.PURE);
            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_WINDOW_SIZE, 3);
            @params.SetParameterByKey(Parameters.KEY.ANOMALY_KEY_USE_MOVING_AVG, true);

            Anomaly anomalyComputer = Anomaly.Create(@params);

            // Serialize the Anomaly Computer without errors
            SerialConfig    config = new SerialConfig("testSerializeCumulativeAnomaly", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(anomalyComputer);

            // Deserialize the Anomaly Computer and make sure its usable (same tests as AnomalyTest.java)
            Anomaly serializedAnomalyComputer = api.Read <Anomaly>(bytes);

            Assert.IsNotNull(serializedAnomalyComputer);

            Object[] predicted =
            {
                new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 },
                new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 },
                new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }, new int[] { 1, 2, 6 }
            };
            Object[] actual =
            {
                new int[] {  1,  2,  6 }, new int[] {  1,  2,  6 }, new int[] {  1,  4,  6 },
                new int[] { 10, 11,  6 }, new int[] { 10, 11, 12 }, new int[] { 10, 11, 12 },
                new int[] { 10, 11, 12 }, new int[] {  1,  2,  6 }, new int[] {  1,  2,  6 }
            };

            double[] anomalyExpected = { 0.0, 0.0, 1.0 / 9.0, 3.0 / 9.0, 2.0 / 3.0, 8.0 / 9.0, 1.0, 2.0 / 3.0, 1.0 / 3.0 };
            for (int i = 0; i < 9; i++)
            {
                double score = serializedAnomalyComputer.Compute((int[])actual[i], (int[])predicted[i], 0, 0);
                Assert.AreEqual(anomalyExpected[i], score, 0.01);
            }
        }
예제 #13
0
        public void TestSerializeObservableSensor()
        {
            PublisherSupplier supplier = PublisherSupplier.GetBuilder()
                                         .AddHeader("dayOfWeek")
                                         .AddHeader("darr")
                                         .AddHeader("B").Build();

            ObservableSensor <string[]> oSensor = new ObservableSensor <string[]>(
                SensorParams.Create(SensorParams.Keys.Obs, new Object[] { "name", supplier }));

            SerialConfig    config = new SerialConfig("testSerializeObservableSensor", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(oSensor);
            ObservableSensor <string[]> serializedOSensor = api.Read <ObservableSensor <string[]> >(bytes);

            bool b = serializedOSensor.IsDeepEqual(oSensor);

            DeepCompare(serializedOSensor, oSensor);
            Assert.IsTrue(b);
        }
예제 #14
0
        public void TestHTMSensor_HotGym()
        {
            Object[]             n      = { "some name", ResourceLocator.Path("rec-center-hourly-small.csv") };
            HTMSensor <FileInfo> sensor = (HTMSensor <FileInfo>) Sensor <FileInfo> .Create(
                FileSensor.Create, SensorParams.Create(SensorParams.Keys.Path, n));

            sensor.InitEncoder(GetTestEncoderParams());

            SerialConfig    config = new SerialConfig("testHTMSensor_HotGym");
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(sensor);
            Assert.IsNotNull(bytes);
            Assert.IsTrue(bytes.Length > 0);
            HTMSensor <FileInfo> serializedSensor = api.Read <HTMSensor <FileInfo> >(bytes);

            bool b = serializedSensor.IsDeepEqual(sensor);

            DeepCompare(serializedSensor, sensor);
            Assert.IsTrue(b);
        }
예제 #15
0
        public void TestHTMSensor_DaysOfWeek()
        {
            Object[]             n      = { "some name", ResourceLocator.Path("days-of-week.csv") };
            HTMSensor <FileInfo> sensor = (HTMSensor <FileInfo>) Sensor <FileInfo> .Create(
                FileSensor.Create, SensorParams.Create(SensorParams.Keys.Path, n));

            Parameters p = GetParameters();

            p = p.Union(NetworkTestHarness.GetDayDemoTestEncoderParams());
            sensor.InitEncoder(p);

            SerialConfig    config = new SerialConfig("testHTMSensor_DaysOfWeek", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(sensor);
            HTMSensor <FileInfo> serializedSensor = api.Read <HTMSensor <FileInfo> >(bytes);

            bool b = serializedSensor.IsDeepEqual(sensor);

            DeepCompare(serializedSensor, sensor);
            Assert.IsTrue(b);
        }
예제 #16
0
        public void TestSearchAndListPreviousCheckPoint()
        {
            Parameters p = NetworkTestHarness.GetParameters();

            Net.Network.Network network = Net.Network.Network.Create("test network", p)
                                          .Add(Net.Network.Network.CreateRegion("r1")
                                               .Add(Net.Network.Network.CreateLayer("1", p)
                                                    .Add(Anomaly.Create())
                                                    .Add(new TemporalMemory())
                                                    .Add(new SpatialPooler())));

            IPersistenceAPI pa = Persistence.Get(new SerialConfig(null, SerialConfig.SERIAL_TEST_DIR));

            ArrayUtils.Range(0, 5).ToList().ForEach(i =>
                                                    ((Persistence.PersistenceAccess)pa).GetCheckPointFunction <Net.Network.Network>(network)(network));

            Dictionary <string, DateTime> checkPointFiles = pa.ListCheckPointFiles();

            Assert.IsTrue(checkPointFiles.Count > 4);

            Assert.AreEqual(checkPointFiles.ElementAt(checkPointFiles.Count - 2).Key,
                            pa.GetPreviousCheckPoint(checkPointFiles.ElementAt(checkPointFiles.Count - 1)));
        }
예제 #17
0
        public void TestThreadedPublisher_TemporalMemoryNetwork()
        {
            Net.Network.Network network = CreateAndRunTestTemporalMemoryNetwork();
            ILayer      l  = network.Lookup("r1").Lookup("1");
            Connections cn = l.GetConnections();

            SerialConfig    config = new SerialConfig("testThreadedPublisher_TemporalMemoryNetwork", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[]      bytes = api.Write(cn);
            Connections serializedConnections = api.Read <Connections>(bytes);

            Net.Network.Network network2 = CreateAndRunTestTemporalMemoryNetwork();
            ILayer      l2      = network2.Lookup("r1").Lookup("1");
            Connections newCons = l2.GetConnections();

            newCons.ShouldDeepEqual(serializedConnections);

            bool b = newCons.IsDeepEqual(serializedConnections);

            DeepCompare(newCons, serializedConnections);
            Assert.IsTrue(b);
        }
예제 #18
0
        public void TestThreadedPublisher_SpatialPoolerNetwork()
        {
            Net.Network.Network network = CreateAndRunTestSpatialPoolerNetwork(0, 6);
            ILayer      l  = network.Lookup("r1").Lookup("1");
            Connections cn = l.GetConnections();

            SerialConfig    config = new SerialConfig("testThreadedPublisher_SpatialPoolerNetwork", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            byte[] bytes = api.Write(cn);
            //Serialize above Connections for comparison with same run but unserialized below...
            Connections serializedConnections = api.Read <Connections>(bytes);

            Net.Network.Network network2 = CreateAndRunTestSpatialPoolerNetwork(0, 6);
            ILayer      l2      = network2.Lookup("r1").Lookup("1");
            Connections newCons = l2.GetConnections();

            //Compare the two Connections (both serialized and regular runs) - should be equal
            bool b = newCons.IsDeepEqual(serializedConnections);

            DeepCompare(newCons, serializedConnections);
            Assert.IsTrue(b);
        }
예제 #19
0
        public void TestSerializedUnStartedNetworkRuns()
        {
            const int NUM_CYCLES        = 600;
            const int INPUT_GROUP_COUNT = 7; // Days of Week

            Net.Network.Network network = GetLoadedDayOfWeekNetwork();

            SerialConfig    config = new SerialConfig("testSerializedUnStartedNetworkRuns", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            api.Store(network);

            //Serialize above Connections for comparison with same run but unserialized below...
            Net.Network.Network serializedNetwork = api.Load();
            Assert.AreEqual(serializedNetwork, network);
            DeepCompare(network, serializedNetwork);

            int cellsPerCol = (int)serializedNetwork.GetParameters().GetParameterByKey(Parameters.KEY.CELLS_PER_COLUMN);

            serializedNetwork.Observe().Subscribe(
                inf => { dayOfWeekPrintout(inf, cellsPerCol); },
                e => { Console.WriteLine(e); },
                () => { });
            //            new Observer<Inference>() {
            //        @Override public void onCompleted() { }
            //    @Override public void onError(Throwable e) { e.printStackTrace(); }
            //    @Override
            //        public void onNext(Inference inf)
            //    {
            //        /** see {@link #createDayOfWeekInferencePrintout()} */
            //        dayOfWeekPrintout.apply(inf, cellsPerCol);
            //    }
            //});

            Publisher pub = serializedNetwork.GetPublisher();

            serializedNetwork.Start();

            int cycleCount = 0;

            for (; cycleCount < NUM_CYCLES; cycleCount++)
            {
                for (double j = 0; j < INPUT_GROUP_COUNT; j++)
                {
                    pub.OnNext("" + j);
                }

                serializedNetwork.Reset();

                if (cycleCount == 284)
                {
                    break;
                }
            }

            pub.OnComplete();

            try
            {
                Region r1 = serializedNetwork.Lookup("r1");
                r1.Lookup("1").GetLayerThread().Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Assert.Fail();
            }
        }
예제 #20
0
        public void TestSerializeLayer()
        {
            Parameters p = NetworkTestHarness.GetParameters().Copy();

            p.SetParameterByKey(Parameters.KEY.RANDOM, new MersenneTwister(42));
            Map <String, Map <String, Object> > settings = NetworkTestHarness.SetupMap(
                null,                     // map
                8,                        // n
                0,                        // w
                0,                        // min
                0,                        // max
                0,                        // radius
                0,                        // resolution
                null,                     // periodic
                null,                     // clip
                true,                     // forced
                "dayOfWeek",              // fieldName
                "darr",                   // fieldType (dense array as opposed to sparse array or "sarr")
                "SDRPassThroughEncoder"); // encoderType

            p.SetParameterByKey(Parameters.KEY.FIELD_ENCODING_MAP, settings);

            Sensor <ObservableSensor <string[]> > sensor = Sensor <ObservableSensor <string[]> > .Create(
                ObservableSensor <string[]> .Create, SensorParams.Create(SensorParams.Keys.Obs, new Object[] { "name",
                                                                                                               PublisherSupplier.GetBuilder()
                                                                                                               .AddHeader("dayOfWeek")
                                                                                                               .AddHeader("darr")
                                                                                                               .AddHeader("B").Build() }));

            ILayer layer = Net.Network.Network.CreateLayer("1", p)
                           .AlterParameter(Parameters.KEY.AUTO_CLASSIFY, true)
                           .Add(new SpatialPooler())
                           .Add(sensor);

            //        Observer obs = new Observer<IInference>() {
            //        @Override public void onCompleted() { }
            //    @Override public void onError(Throwable e) { e.printStackTrace(); }
            //    @Override
            //        public void onNext(Inference spatialPoolerOutput)
            //    {
            //        System.out.println("in onNext()");
            //    }
            //};

            var obs = Observer.Create <IInference>(
                spatialPoolerOutput => { Console.WriteLine("in onNext()"); },
                e => Console.WriteLine(e),
                () => { });

            layer.Subscribe(obs);
            layer.Close();

            SerialConfig    config = new SerialConfig("testSerializeLayer", SerialConfig.SERIAL_TEST_DIR);
            IPersistenceAPI api    = Persistence.Get(config);

            api.Write(layer);

            //Serialize above Connections for comparison with same run but unserialized below...
            ILayer serializedLayer = api.Read <ILayer>();

            Assert.AreEqual(serializedLayer, layer);
            DeepCompare(layer, serializedLayer);

            // Now change one attribute and see that they are not equal
            serializedLayer.ResetRecordNum();
            Assert.AreNotEqual(serializedLayer, layer);
        }