/// <summary>
        /// Creates training vectors.
        /// </summary>
        /// <param name="experimentCode"></param>
        /// <param name="inputBits"></param>
        /// <returns></returns>
        private List <int[]> GetTrainingvectors(int experimentCode, int inputBits, int width)
        {
            if (experimentCode == 0)
            {
                //
                // We create here 2 vectors.
                List <int[]> inputValues = new List <int[]>();

                for (int i = 0; i < 10; i += 1)
                {
                    inputValues.Add(NeoCortexUtils.CreateVector(inputBits, i, i + width));
                }


                return(inputValues);
            }
            else if (experimentCode == 1)
            {
                // todo. create or load other test vectors/images here
                // We create here 2 vectors.
                List <int[]> inputValues = new List <int[]>();

                for (int i = 0; i < 10; i += 1)
                {
                    inputValues.Add(NeoCortexUtils.CreateVector(inputBits, i, i + width));
                }


                return(inputValues);
            }
            else
            {
                throw new ApplicationException("Invalid experimentCode");
            }
        }
示例#2
0
        private void SimilarityResult(int arr1, int arr2, Dictionary <double, int[]> sdrs, String folder)                // Function to check similarity between Inputs
        {
            List <int[, ]> arrayOvr = new List <int[, ]>();

            int h = arr1;
            int w = arr2;

            Console.WriteLine("SDR[h] = ");

            Console.WriteLine(Helpers.StringifyVector(sdrs[h]));

            Console.WriteLine("SDR[w] = ");

            Console.WriteLine(Helpers.StringifyVector(sdrs[w]));

            var Overlaparray = SdrRepresentation.OverlapArraFun(sdrs[h], sdrs[w]);

            int[,] twoDimenArray2 = ArrayUtils.Make2DArray <int>(Overlaparray, (int)Math.Sqrt(Overlaparray.Length), (int)Math.Sqrt(Overlaparray.Length));
            int[,] twoDimArray1   = ArrayUtils.Transpose(twoDimenArray2);
            NeoCortexUtils.DrawBitmap(twoDimArray1, 1024, 1024, $"{folder}\\Overlap_Union\\Overlap_{h}_{w}.png", Color.PaleGreen, Color.Red, text: $"Overlap_{h}_{w}.png");

            var unionArr = sdrs[h].Union(sdrs[w]).ToArray();

            int[,] twoDimenArray4 = ArrayUtils.Make2DArray <int>(unionArr, (int)Math.Sqrt(unionArr.Length), (int)Math.Sqrt(unionArr.Length));
            int[,] twoDimArray3   = ArrayUtils.Transpose(twoDimenArray4);

            NeoCortexUtils.DrawBitmap(twoDimArray3, 1024, 1024, $"{folder}\\Overlap_Union\\Union_{h}_{w}.png", Color.PaleGreen, Color.Green, text: $"Overlap_{h}_{w}.png");
        }
示例#3
0
        public void EncodeFullDateTimeTest(int w, double r, Object input, int[] expectedOutput)
        {
            CortexNetworkContext ctx = new CortexNetworkContext();

            var now = DateTimeOffset.Now;

            Dictionary <string, Dictionary <string, object> > encoderSettings = getFullEncoderSettings();

            var encoder = new DateTimeEncoder(encoderSettings, DateTimeEncoder.Precision.Days);

            var result = encoder.Encode(DateTimeOffset.Parse(input.ToString()));

            // This is a limitation for 2D drawing only.
            if ((result.Length % 2) != 0)
            {
                throw new ArgumentException("Only odd number of bits is allowed. Please set Offset pproperty of all encoders to odd value.");
            }

            Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result));

            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"FullDateTime_out_{input.ToString().Replace("/", "-").Replace(":", "-")}_32x32-N-{encoderSettings["DateTimeEncoder"]["N"]}-W-{encoderSettings["DateTimeEncoder"]["W"]}.png", Color.Yellow, Color.Black);

            // Assert.IsTrue(result.SequenceEqual(expectedOutput));
        }
示例#4
0
        public void ScalarEncodingExperiment()
        {
            string outFolder = nameof(ScalarEncodingExperiment);

            Directory.CreateDirectory(outFolder);

            DateTime now = DateTime.Now;

            ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
            {
                { "W", 21 },
                { "N", 1024 },
                { "Radius", -1.0 },
                { "MinVal", 0.0 },
                { "MaxVal", 100.0 },
                { "Periodic", false },
                { "Name", "scalar" },
                { "ClipInput", false },
            });

            for (double i = 0.0; i < (long)encoder.MaxVal; i += 0.1)
            {
                var result = encoder.Encode(i);

                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
                var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

                NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}.png", Color.Yellow, Color.Black, text: i.ToString());
            }
        }
示例#5
0
        private static void execFullDateTimeEncodingTest(string prefix, object input)
        {
            var now = DateTimeOffset.Now;

            Dictionary <string, Dictionary <string, object> > encoderSettings = getFullEncoderSettings();

            encoderSettings["DayOfWeekEncoder"]["ClipInput"] = false;

            var encoder = new DateTimeEncoder(encoderSettings, DateTimeEncoder.Precision.Days);

            var result = encoder.Encode(DateTimeOffset.Parse(input.ToString(), CultureInfo.InvariantCulture));

            // This is a limitation for 2D drawing only.
            if ((result.Length % 2) != 0)
            {
                throw new ArgumentException("Only odd number of bits is allowed. Please set Offset pproperty of all encoders to odd value.");
            }

            Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result));

            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{prefix}_out_{input.ToString().Replace("/", "-").Replace(":", "-")}_32x32-N-{encoderSettings["DateTimeEncoder"]["N"]}-W-{encoderSettings["DateTimeEncoder"]["W"]}.png", Color.Yellow, Color.Black);
        }
示例#6
0
        public void EncodeDateTimeTest(int w, double r, Object input, int[] expectedOutput)
        {
            CortexNetworkContext ctx = new CortexNetworkContext();

            var now = DateTimeOffset.Now;

            Dictionary <string, Dictionary <string, object> > encoderSettings = new Dictionary <string, Dictionary <string, object> >();

            encoderSettings.Add("DateTimeEncoder", new Dictionary <string, object>()
            {
                { "W", 21 },
                { "N", 1024 },
                { "MinVal", now.AddYears(-10) },
                { "MaxVal", now },
                { "Periodic", false },
                { "Name", "DateTimeEncoder" },
                { "ClipInput", false },
                { "Padding", 5 },
            });

            var encoder = new DateTimeEncoder(encoderSettings, DateTimeEncoder.Precision.Days);

            var result = encoder.Encode(DateTimeOffset.Parse(input.ToString()));

            Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(result));
            //Debug.WriteLine(NeoCortexApi.Helpers.StringifyVector(expectedOutput));

            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 32, 32);
            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"DateTime_out_{input.ToString().Replace("/", "-").Replace(":", "-")}_32x32-N-{encoderSettings["DateTimeEncoder"]["N"]}-W-{encoderSettings["DateTimeEncoder"]["W"]}.png");

            // Assert.IsTrue(result.SequenceEqual(expectedOutput));
        }
示例#7
0
        // <summary>
        /// Calculate all required results.
        /// 1. Overlap and Union of the Spatial Pooler SDRs of two Images as Input
        ///    It cross compares the 1st SDR with it self and all the Tranings Images.
        /// 2. Creates bitmaps of the overlaping and non-overlaping regions of the Comparing SDRs.
        /// 3. Also generate HeatMaps of the SDRs during Spatial Pooler learning Phase.
        /// </summary>
        /// <param name="sdrs"></param>
        private void CalculateResult(Dictionary <string, int[]> sdrs, Dictionary <string, int[]> inputVectors, int numOfCols, int[] activeCols, string outFolder, string trainingImage, int[] inputVector)
        {
            int[] CompareArray = new int[numOfCols];
            int[] ActiveArray  = new int[numOfCols];

            ActiveArray = SdrRepresentation.GetIntArray(activeCols, 4096);

            sdrs.Add(trainingImage, activeCols);
            inputVectors.Add(trainingImage, inputVector);
            int[] FirstSDRArray = new int[81];
            if (sdrs.First().Key == null)
            {
                FirstSDRArray = new int[sdrs.First().Value.Length];
            }

            FirstSDRArray = sdrs.First().Value;

            CompareArray = SdrRepresentation.GetIntArray(FirstSDRArray, 4096);

            var Array = SdrRepresentation.OverlapArraFun(ActiveArray, CompareArray);

            int[,] twoDimenArray2 = ArrayUtils.Make2DArray <int>(Array, (int)Math.Sqrt(Array.Length), (int)Math.Sqrt(Array.Length));
            int[,] twoDimArray1   = ArrayUtils.Transpose(twoDimenArray2);
            NeoCortexUtils.DrawBitmap(twoDimArray1, 1024, 1024, $"{outFolder}\\Overlap_{sdrs.Count}.png", Color.PaleGreen, Color.Red, text: $"Overlap.png");

            Array = ActiveArray.Union(CompareArray).ToArray();
            int[,] twoDimenArray4 = ArrayUtils.Make2DArray <int>(Array, (int)Math.Sqrt(Array.Length), (int)Math.Sqrt(Array.Length));
            int[,] twoDimArray3   = ArrayUtils.Transpose(twoDimenArray4);
            NeoCortexUtils.DrawBitmap(twoDimArray3, 1024, 1024, $"{outFolder}\\Union_{sdrs.Count}.png", Color.PaleGreen, Color.Green, text: $"Union.png");

            // Bitmap Intersection Image of two bit arrays selected for comparison
            SdrRepresentation.DrawIntersections(twoDimArray3, twoDimArray1, 10, $"{outFolder}\\Intersection_{sdrs.Count}.png", Color.Black, Color.Gray, text: $"Intersection.png");

            return;
        }
        public void TestHashDictionary()
        {
            double minOctOverlapCycles = 1.0;
            int    inputBits           = 100;
            int    numColumns          = 2048;
            double maxBoost            = 5.0;

            Parameters p = Parameters.getAllDefaultParameters();

            p.Set(KEY.RANDOM, new ThreadSafeRandom(42));
            p.Set(KEY.INPUT_DIMENSIONS, new int[] { inputBits });
            p.Set(KEY.CELLS_PER_COLUMN, 10);
            p.Set(KEY.COLUMN_DIMENSIONS, new int[] { numColumns });

            p.Set(KEY.MAX_BOOST, maxBoost);
            p.Set(KEY.DUTY_CYCLE_PERIOD, 100);
            p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, minOctOverlapCycles);

            var mem = new Connections();

            List <int[]> inputs  = new List <int[]>();
            List <int[]> outputs = new List <int[]>();

            // Create random vectors with 2% sparsity
            for (int i = 0; i < 10; i++)
            {
                var inp  = NeoCortexUtils.CreateRandomVector(inputBits, 20);
                var outp = NeoCortexUtils.CreateRandomVector(numColumns, 40);

                inputs.Add(inp);
                outputs.Add(outp);
            }

            bool isBoostOff = true;

            int learningCycles = 100;

            HomeostaticPlasticityController hpa = new HomeostaticPlasticityController(mem, inputs.Count * learningCycles,
                                                                                      (isStable, numPatterns, actColAvg, seenInputs) => { });

            for (int cycle = 0; cycle < 1000; cycle++)
            {
                for (int i = 0; i < inputs.Count; i++)
                {
                    isBoostOff = hpa.Compute(inputs[i], outputs[i]);

                    if (isBoostOff)
                    {
                        Assert.IsTrue(cycle >= learningCycles);
                    }
                }
            }

            Assert.IsTrue(isBoostOff);
        }
        private static void DrawImages(HtmConfig cfg, string inputKey, int[] input, int[] activeColumns)
        {
            List <int[, ]> twoDimArrays = new List <int[, ]>();

            int[,] twoDimInpArray           = ArrayUtils.Make2DArray <int>(input, (int)(Math.Sqrt(input.Length) + 0.5), (int)(Math.Sqrt(input.Length) + 0.5));
            twoDimArrays.Add(twoDimInpArray = ArrayUtils.Transpose(twoDimInpArray));
            int[,] twoDimOutArray           = ArrayUtils.Make2DArray <int>(activeColumns, (int)(Math.Sqrt(cfg.NumColumns) + 0.5), (int)(Math.Sqrt(cfg.NumColumns) + 0.5));
            twoDimArrays.Add(twoDimInpArray = ArrayUtils.Transpose(twoDimOutArray));

            NeoCortexUtils.DrawBitmaps(twoDimArrays, $"{inputKey}.png", Color.Yellow, Color.Gray, 1024, 1024);
        }
        public void MonthEncoderTest(string input)
        {
            ScalarEncoder monthEncoder = DateTimeEncoders.FetchMonthEncoder();
            DateTime      dateTime     = DateTime.Parse(input);
            int           month        = dateTime.Month;

            var result = monthEncoder.Encode(month);

            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 100, 100);
            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{dateTime.Day}.png", null);
        }
        public void DayOfWeekEncoderTest(string input)
        {
            ScalarEncoder dayEncoder = DateTimeEncoders.FetchDayEncoder();
            DateTime      dateTime   = DateTime.Parse(input);
            int           dayOfWeek  = (int)dateTime.DayOfWeek;

            var result = dayEncoder.Encode(dayOfWeek);

            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 100, 100);
            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{dateTime.Day}.png", null);
        }
        public void SegmentEncoderTest(string input)
        {
            ScalarEncoder segmentEncoder = DateTimeEncoders.FetchSegmentEncoder();
            DateTime      dateTime       = DateTime.Parse(input);
            var           time           = dateTime.ToString("HH:mm");
            Slot          slot           = CSVPRocessingMethods.GetSlot(time);

            var result = segmentEncoder.Encode(slot.Segment);

            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 100, 100);
            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{dateTime.Day}.png", null);
        }
示例#13
0
        /// <summary>
        /// Returns the input vectors as array of integers
        /// </summary>
        /// <param name="inputSequence">An array of double, consisting the starting indexes for each input vector</param>
        /// <param name="min">Minimum index in the input vector plane</param>
        /// <param name="max">Maximum index in the input vector plane</param>
        /// <param name="inputs">
        /// </param>
        /// <param name="outFolder">The path where the input vectors are to be generated</param>
        /// <returns></returns>
        static List <int[]> GetEncodedSequence(double[] inputSequence, double min, double max, InputParameters inputs, string outFolder)
        {
            List <int[]> sdrList = new List <int[]>();

            DateTime now = DateTime.Now;

            ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
            {
                { "W", inputs.getWidth() },
                { "N", 1024 },
                { "Radius", inputs.getRadius() },
                { "MinVal", min },
                { "MaxVal", max },
                { "Periodic", false },
                { "Name", "scalar" },
                { "ClipInput", false },
            });

            foreach (var i in inputSequence)
            {
                var result = encoder.Encode(i);

                sdrList.Add(result);

                int[,] twoDimenArray = ArrayUtils.Make2DArray(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
                var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

                int counter = 0;
                if (File.Exists(outFolder + @"\\" + i + @".png"))
                {
                    counter = 1;
                    while (File.Exists(outFolder + @"\\" + i + @"-" + counter.ToString() + @".png"))
                    {
                        counter++;
                    }
                }

                if (counter == 0)
                {
                    NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}.png", Color.Yellow, Color.Black, text: i.ToString());
                }
                else
                {
                    NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}-{counter}.png", Color.Yellow, Color.Black, text: i.ToString());
                }
            }

            return(sdrList);
        }
示例#14
0
        private void ScalarEncoderTest(int[] inputs)
        {
            var outFolder = @"..\..\..\TestFiles\ScalarEncoderResults";

            ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
            {
                { "W", 3 },       // 2% Approx
                { "N", 100 },
                { "MinVal", (double)0 },
                { "MaxVal", (double)99 },
                { "Periodic", true },
                { "Name", "Scalar Sequence" },
                { "ClipInput", true },
            });
            Dictionary <double, int[]> sdrs = new Dictionary <double, int[]>();


            foreach (double input in inputs)
            {
                int[] result = encoder.Encode(input);

                Console.WriteLine($"Input = {input}");
                Console.WriteLine($"SDRs Generated = {NeoCortexApi.Helpers.StringifyVector(result)}");
                Console.WriteLine($"SDR As Text = {NeoCortexApi.Helpers.StringifyVector(ArrayUtils.IndexWhere(result, k => k == 1))}");


                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
                int[,] twoDimArray   = ArrayUtils.Transpose(twoDimenArray);
                NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{input}.png", Color.PaleGreen, Color.Blue, text: input.ToString());

                sdrs.Add(input, result);
            }


            // <summary>
            /// Calculate all required results.
            /// 1. Overlap and Union of the Binary arrays of two scalar values
            ///    It cross compares the binary arrays  of any of the two scalar values User enters.
            /// 2. Creates bitmaps of the overlaping and non-overlaping regions of the two binary arrays selected by the User.
            /// </summary>
            Console.WriteLine("Encoder Binary array Created");
            Console.WriteLine("Enter the two elements you want to Compare");
            String a = Console.ReadLine();
            String b = Console.ReadLine();

            SimilarityResult(Convert.ToInt32(a), Convert.ToInt32(b), sdrs, outFolder);
        }
示例#15
0
        public void CreateSdrAsBitmapTest()
        {
            Object[] d1 = new Object[] { "05/02/2020 22:58:06", "06/04/2020 01:28:07", "07/09/2019 21:15:07", "08/01/2017 11:27:07" };

            this.DateTimeEncoderTest(d1);


            Object[] inputs = { "05/02/2020 22:58:07", "06/04/2020 01:28:07", "07/09/2019 21:15:07", "08/01/2018 11:27:07" };



            foreach (var input in inputs)
            {
                var outFolder = @"EncoderOutputImages\DateTimeEncoderOutput";
                Directory.CreateDirectory(outFolder);

                var now = DateTimeOffset.Now;

                Dictionary <string, Dictionary <string, object> > encoderSettings = new Dictionary <string, Dictionary <string, object> >();
                encoderSettings.Add("DateTimeEncoder", new Dictionary <string, object>()
                {
                    { "W", 21 },
                    { "N", 1024 },
                    { "MinVal", now.AddYears(-10) },
                    { "MaxVal", now },
                    { "Periodic", false },
                    { "Name", "DateTimeEncoder" },
                    { "ClipInput", false },
                    { "Padding", 5 },
                });

                var encoder = new DateTimeEncoder(encoderSettings, DateTimeEncoder.Precision.Days);
                var result  = encoder.Encode(DateTimeOffset.Parse(input.ToString()));

                Debug.WriteLine($"Input = {input}");
                Debug.WriteLine($"SDRs Generated = {NeoCortexApi.Helpers.StringifyVector(result)}");
                Debug.WriteLine($"SDR As Indices = {NeoCortexApi.Helpers.StringifyVector(ArrayUtils.IndexWhere(result, k => k == 1))}");

                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, 32, 32);
                var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

                NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{input.ToString().Replace("/", "-").Replace(":", "-")}_32x32-N-{encoderSettings["DateTimeEncoder"]["N"]}-W-{encoderSettings["DateTimeEncoder"]["W"]}.png");
            }
        }
示例#16
0
        /// <summary>
        /// Create a List of encoded data as an array of 0 and 1
        /// <br>Scalar Encoder initiation,</br>
        /// <br>logging the encoded data information to CSV file</br>
        /// <br>draw encoded data to bitmaps</br>
        /// </summary>
        /// <param name="inputDoubleArray"></param>
        /// <param name="inputDimSize">how many elements the ouput has</param>
        /// <returns></returns>
        internal List <int[]> ScalarEncoderDoubleArray(
            double[] inputDoubleArray,
            int inputDimSize,
            String testFolder)
        {
            List <int[]>                arrays          = new List <int[]>();
            int                         encodeDim       = inputDimSize;
            CortexNetworkContext        ctx             = new CortexNetworkContext();
            Dictionary <string, object> encoderSettings = new Dictionary <string, object>
            {
                { "W", 25 },
                { "N", encodeDim *encodeDim },
                { "MinVal", inputDoubleArray.Min() - 0.01 },
                { "MaxVal", inputDoubleArray.Max() + 0.01 },
                { "Radius", (double)2.5 },
                { "Resolution", (double)0 },
                { "Periodic", (bool)false },
                { "ClipInput", (bool)true },
                { "Name", "TestScalarEncoder" },
                { "IsRealCortexModel", true }
            };

            ScalarEncoder encoder = new ScalarEncoder(encoderSettings);

            for (int i = 0; i < inputDoubleArray.Length; i++)
            {
                Debug.WriteLine($"Output Dimension of the Scalar Encoder : {encoder.N}");
                var result = encoder.Encode(inputDoubleArray[i]);
                Debug.WriteLine($"the input value is {inputDoubleArray[i]}");
                Debug.WriteLine($"resulting SDR: {NeoCortexApi.Helpers.StringifyVector(result)}");
                arrays.Add(result);
                int[,] arrayToDraw = ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(result, encodeDim, encodeDim));
                NeoCortexUtils.DrawBitmap(
                    arrayToDraw,
                    OutImgSize, OutImgSize,
                    $"{outputFolder}\\{testFolder}\\{encoderOutputFolder}\\encodedValnumber_{i}.png",
                    Color.FromArgb(44, 44, 44),
                    Color.FromArgb(200, 200, 250),
                    $"encodedVal of {inputDoubleArray[i]}");
            }
            LogToCSV(inputDoubleArray, arrays, $"{testFolder}\\{logOutputFolder}\\scalarEncoderOutput.csv");
            return(arrays);
        }
示例#17
0
        public void CreateSdrAsTextTest()
        {
            var outFolder = @"EncoderOutputImages\ScalerEncoderOutput";

            int[] d = new int[] { 1, 4, 5, 7, 8, 9 };
            this.ScalarEncoderTest(d);

            Directory.CreateDirectory(outFolder);

            Console.WriteLine("SDR Representation using ScalarEncoder");


            for (int input = 1; input < (int)6; input++)
            {
                //double input = 1.10;


                ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
                {
                    { "W", 25 },
                    { "N", (int)0 },
                    { "Radius", (double)2.5 },
                    { "MinVal", (double)1 },
                    { "MaxVal", (double)50 },
                    { "Periodic", false },
                    { "Name", "Scalar Encoder" },
                    { "ClipInput", false },
                });

                var result = encoder.Encode(input);
                Debug.WriteLine($"Input = {input}");
                Debug.WriteLine($"SDRs Generated = {NeoCortexApi.Helpers.StringifyVector(result)}");
                Debug.WriteLine($"SDR As Indices = {NeoCortexApi.Helpers.StringifyVector(ArrayUtils.IndexWhere(result, k => k == 1))}");

                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
                var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

                NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{input}.png", Color.Yellow, Color.Black, text: input.ToString());
            }
        }
        // [TestCategory("Prod")]
        public void TestHeatmapCreation(int threshold)
        {
            List <double[, ]> bostArrays = new List <double[, ]>();

            bostArrays.Add(new double[64, 64]);
            bostArrays.Add(new double[64, 64]);

            double v = 0;

            for (int i = 0; i < 64; i++)
            {
                for (int j = 0; j < 64; j++)
                {
                    bostArrays[0][i, j] = v;
                    bostArrays[1][j, i] = v;
                }

                v += 1;
            }

            NeoCortexUtils.DrawHeatmaps(bostArrays, $"tessheat_{threshold}.png", 1024, 1024, 60, threshold, 10);
        }
示例#19
0
        public void SimilarityResult1(int[] arr1, int[] arr2, Dictionary <Object, int[]> sdrs, String folder, Object input0, Object input1)                // Function to check similarity between Inputs
        {
            List <int[, ]> arrayOvr = new List <int[, ]>();

            Object h = input0;
            Object w = input1;

            var Overlaparray = SdrRepresentation.OverlapArraFun(arr1, arr2);

            int[,] twoDimenArray2 = ArrayUtils.Make2DArray <int>(Overlaparray, 32, 32);

            var twoDimArray1 = ArrayUtils.Transpose(twoDimenArray2);

            NeoCortexUtils.DrawBitmap(twoDimArray1, 1024, 1024, $"{folder}\\Overlap_Union\\Overlap_{h.ToString().Replace("/", "-").Replace(":", "-")}_{w.ToString().Replace("/", "-").Replace(":", "-")}.png", Color.PaleGreen, Color.Red, text: $"Overlap_{h}_{w}.png");

            var unionArr = arr1.Union(arr2).ToArray();

            int[,] twoDimenArray4 = ArrayUtils.Make2DArray <int>(unionArr, 32, 32);
            int[,] twoDimArray3   = ArrayUtils.Transpose(twoDimenArray4);

            NeoCortexUtils.DrawBitmap(twoDimArray3, 1024, 1024, $"{folder}\\Overlap_Union\\Union{h.ToString().Replace("/", "-").Replace(":", "-")}_{w.ToString().Replace("/", "-").Replace(":", "-")}.png", Color.PaleGreen, Color.Green, text: $"Overlap_{h}_{w}.png");
        }
        public void ExperimentTest(string inputBinarizedFile)
        {
            var parameters = SetupParameters(32, 64, 4096, true);

            parameters.Set(KEY.DUTY_CYCLE_PERIOD, 100000);
            parameters.Set(KEY.MAX_BOOST, 1.0);
            parameters.Set(KEY.IS_BUMPUP_WEAKCOLUMNS_DISABLED, true);

            var sp  = new SpatialPooler();
            var mem = new Connections();

            int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinarizedFile).ToArray();
            int[] activeArray = new int[4096];
            parameters.apply(mem);
            sp.Init(mem);

            for (int i = 0; i < 1000; i++)
            {
                sp.compute(inputVector, activeArray, true);
                var activeCols = activeArray.IndexWhere((el) => el == 1);
                var str        = Helpers.StringifyVector(activeCols);
                Debug.WriteLine(str);
            }
        }
示例#21
0
        public List <int[]> GetEncodedSequence(double[] inputSequence, double min, double max)
        {
            List <int[]> sdrList = new List <int[]>();

            string outFolder = nameof(NoiseUnitTests);

            Directory.CreateDirectory(outFolder);

            DateTime now = DateTime.Now;

            ScalarEncoder encoder = new ScalarEncoder(new Dictionary <string, object>()
            {
                { "W", 21 },
                { "N", 1024 },
                { "Radius", -1.0 },
                { "MinVal", min },
                { "MaxVal", max },
                { "Periodic", false },
                { "Name", "scalar" },
                { "ClipInput", false },
            });

            foreach (var i in inputSequence)
            {
                var result = encoder.Encode(i);

                sdrList.Add(result);

                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(result, (int)Math.Sqrt(result.Length), (int)Math.Sqrt(result.Length));
                var twoDimArray = ArrayUtils.Transpose(twoDimenArray);

                NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{i}.png", Color.Yellow, Color.Black, text: i.ToString());
            }

            return(sdrList);
        }
        public void RunGaussianNoiseExperiment()
        {
            const int E_outBits     = 423;
            const int columnsNumber = 2048;

            int[]        SP_Result           = null;
            int[]        SP_NoisyResult      = null;
            List <int[]> SP_Result_List      = new List <int[]>();
            List <int[]> SP_NoisyResult_List = new List <int[]>();

            double[] ham_total = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            double[] ham_avg   = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            double   ham_upper;
            double   ham_lower;
            int      number_training_set = 41;  // Integer numbers range from -20 to 20 with step of 1.
            int      number_testing_set  = 401; // Decimal numbers range from -20 to 20 with step of 0.1.

            string experimentFolder = nameof(GaussianNoiseExperiment);

            // string SP_noisyinFolder = nameof(GoussianNoiseExperiment);
            Directory.CreateDirectory(experimentFolder);
            // Directory.CreateDirectory(SP_noisyinFolder);
            string SP_inFile      = $"{experimentFolder}\\MyEncoderOut.csv";
            string SP_noisyinFile = $"{experimentFolder}\\MyNoisyEncoderOut.csv";
            //string SP_outFolder = "MySPOutput";
            //string SP_noisyoutFolder = "MyNoisySPOutput";
            //Directory.CreateDirectory(SP_outFolder);
            //Directory.CreateDirectory(SP_noisyoutFolder);
            string SP_outFile      = $"{experimentFolder}\\MySPOut.csv";
            string SP_noisyoutFile = $"{experimentFolder}\\MyNoisySPOut.csv";

            //string testFolder = "MyDraftFoler";
            //Directory.CreateDirectory(testFolder);

            //-------------------------------------------------------
            //|                     PARAMETERS                      |
            //-------------------------------------------------------
            Parameters p = Parameters.getAllDefaultParameters();

            p.Set(KEY.RANDOM, new ThreadSafeRandom(42));

            //------------------SPATIAL POOLER PARAMETERS-----------------
            p.Set(KEY.INPUT_DIMENSIONS, new int[] { E_outBits });
            p.Set(KEY.POTENTIAL_RADIUS, -1);
            p.Set(KEY.POTENTIAL_PCT, 0.75);
            p.Set(KEY.GLOBAL_INHIBITION, true);
            p.Set(KEY.INHIBITION_RADIUS, 15);
            p.Set(KEY.LOCAL_AREA_DENSITY, -1.0);
            p.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * columnsNumber);
            p.Set(KEY.STIMULUS_THRESHOLD, 5);
            p.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008);
            p.Set(KEY.SYN_PERM_ACTIVE_INC, 0.05);
            p.Set(KEY.SYN_PERM_CONNECTED, 0.10);
            p.Set(KEY.SYN_PERM_BELOW_STIMULUS_INC, 0.01);
            p.Set(KEY.SYN_PERM_TRIM_THRESHOLD, 0.05);

            p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 1);
            p.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001);
            p.Set(KEY.DUTY_CYCLE_PERIOD, 100);

            // These values activate powerfull boosting.
            p.Set(KEY.MAX_BOOST, 5);
            p.Set(KEY.DUTY_CYCLE_PERIOD, 100);
            p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 1);

            p.Set(KEY.MAX_BOOST, 10);
            p.Set(KEY.WRAP_AROUND, true);
            p.Set(KEY.LEARN, true);

            //-------------------TEMPORAL MEMORY PARAMETERS----------------
            p.Set(KEY.COLUMN_DIMENSIONS, new int[] { columnsNumber });
            p.Set(KEY.CELLS_PER_COLUMN, 32);
            p.Set(KEY.ACTIVATION_THRESHOLD, 10);
            p.Set(KEY.LEARNING_RADIUS, 10);
            p.Set(KEY.MIN_THRESHOLD, 9);
            p.Set(KEY.MAX_NEW_SYNAPSE_COUNT, 20);
            p.Set(KEY.MAX_SYNAPSES_PER_SEGMENT, 225);
            p.Set(KEY.MAX_SEGMENTS_PER_CELL, 225);
            p.Set(KEY.INITIAL_PERMANENCE, 0.21);
            p.Set(KEY.CONNECTED_PERMANENCE, 0.1);
            p.Set(KEY.PERMANENCE_INCREMENT, 0.10);
            p.Set(KEY.PERMANENCE_DECREMENT, 0.10);
            p.Set(KEY.PREDICTED_SEGMENT_DECREMENT, 0.1);
            p.Set(KEY.LEARN, true);

            //Initiating components of a Cortex Layer
            SpatialPoolerMT sp1 = new SpatialPoolerMT();
            TemporalMemory  tm1 = new TemporalMemory();
            var             mem = new Connections();

            p.apply(mem);
            sp1.Init(mem, UnitTestHelpers.GetMemory());
            tm1.Init(mem);

            HtmClassifier <double, ComputeCycle> cls = new HtmClassifier <double, ComputeCycle>();

            Encoding(E_outBits);

            // Can adjust the number of SP learning cycles below
            for (int cycle = 0; cycle < 320; cycle++)
            {
                if (cycle >= 300)
                {
                    // These activates ew-born effect which switch offs the boosting.
                    //mem.setMaxBoost(0.0);
                    mem.HtmConfig.MaxBoost = 0.0;
                    //mem.updateMinPctOverlapDutyCycles(0.0);
                    mem.HtmConfig.MinPctOverlapDutyCycles = 0.0;
                }

                using (StreamReader sr = new StreamReader(SP_inFile))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] tokens   = line.Split(",");
                        int[]    SP_input = new int[E_outBits];
                        for (int i = 0; i < E_outBits; i++)
                        {
                            if (tokens[i + 1] == "0")
                            {
                                SP_input[i] = 0;
                            }
                            else
                            {
                                SP_input[i] = 1;
                            }
                        }
                        SP_Result = sp1.Compute(SP_input, true);
                    }
                }
            }

            using (StreamReader sr = new StreamReader(SP_inFile))
            {
                string line;
                int    lineNumber = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] tokens   = line.Split(",");
                    int[]    SP_input = new int[E_outBits];
                    for (int i = 0; i < E_outBits; i++)
                    {
                        if (tokens[i + 1] == "0")
                        {
                            SP_input[i] = 0;
                        }
                        else
                        {
                            SP_input[i] = 1;
                        }
                    }
                    SP_Result = sp1.Compute(SP_input, false, false);
                    SP_Result_List.Add(SP_Result);
                    int[,] SP_twoDimenArray = ArrayUtils.Make2DArray(SP_Result, 32, 64);
                    var SP_twoDimArray = ArrayUtils.Transpose(SP_twoDimenArray);
                    NeoCortexUtils.DrawBitmap(SP_twoDimArray, 1024, 1024, $"{experimentFolder}\\{lineNumber}.png", Color.DimGray, Color.LawnGreen, text: tokens[0]);
                    lineNumber++;
                }
            }

            using (StreamReader sr = new StreamReader(SP_noisyinFile))
            {
                string line;
                int    lineNumber = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] tokens   = line.Split(",");
                    int[]    SP_input = new int[E_outBits];
                    for (int i = 0; i < E_outBits; i++)
                    {
                        if (tokens[i + 1] == "0")
                        {
                            SP_input[i] = 0;
                        }
                        else
                        {
                            SP_input[i] = 1;
                        }
                    }
                    SP_NoisyResult = sp1.Compute(SP_input, false, false);
                    SP_NoisyResult_List.Add(SP_NoisyResult);
                    var ham = MathHelpers.GetHammingDistance(SP_Result_List[lineNumber], SP_NoisyResult_List[lineNumber], true);
                    Debug.WriteLine($"Noisy input: {tokens[0]} - Hamming NonZ: {ham}");
                    ham = MathHelpers.GetHammingDistance(SP_Result_List[lineNumber], SP_NoisyResult_List[lineNumber], false);
                    Debug.WriteLine($"Noisy input: {tokens[0]} - Hamming All: {ham}");
                    int[,] SP_twoDimenArray = ArrayUtils.Make2DArray(SP_NoisyResult, 32, 64);
                    var SP_twoDimArray = ArrayUtils.Transpose(SP_twoDimenArray);
                    NeoCortexUtils.DrawBitmap(SP_twoDimArray, 1024, 1024, $"{experimentFolder}\\{lineNumber}.png", Color.DimGray, Color.LawnGreen, text: tokens[0]);
                    lineNumber++;
                }
            }

            for (int i = 0; i < number_testing_set - 1; i += 10)
            {
                int count = 1;
                for (int j = i + 1; j < i + 1 + 9; j++)
                {
                    if (i != 0 && i != number_testing_set - 1)
                    {
                        ham_upper             = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[j], true);
                        ham_lower             = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[i - count], true);
                        ham_total[count - 1] += ham_upper + ham_lower;
                        count++;
                    }
                    else if (i == 0)
                    {
                        ham_upper             = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[j], true);
                        ham_total[count - 1] += ham_upper;
                        count++;
                    }
                    else
                    {
                        ham_lower             = MathHelpers.GetHammingDistance(SP_NoisyResult_List[i], SP_NoisyResult_List[i - count], true);
                        ham_total[count - 1] += ham_lower;
                        count++;
                    }
                }
            }
            for (int i = 0; i < 9; i++)
            {
                ham_avg[i] = ham_total[i] / (number_training_set * 2 - 2);
                Debug.WriteLine($"0.{i + 1} step avg hamming distance: {ham_avg[i]}");
            }
        }
        /// <summary>
        /// Creating CSV file containing encoded input for the SP
        /// </summary>
        /// <param name="E_outBits"></param>
        private void Encoding(int local_E_outBits)
        {
            string E_inFile         = "TestFiles\\sequence.csv";
            string E_outFolder      = "MyEncoderOutput";
            string E_noisyinFile    = "TestFiles\\noisy_sequence.csv";
            string E_noisyoutFolder = "MyNoisyEncoderOutput";

            Directory.CreateDirectory(E_outFolder);
            Directory.CreateDirectory(E_noisyoutFolder);
            string E_outFile      = $"{E_outFolder}\\MyEncoderOut.csv";
            string E_noisyoutFile = $"{E_noisyoutFolder}\\MyNoisyEncoderOut.csv";

            Dictionary <string, object> scalarEncoderSettings = getScalarEncoderDefaultSettings(local_E_outBits);
            ScalarEncoder encoder      = new ScalarEncoder(scalarEncoderSettings);
            ScalarEncoder noisyencoder = new ScalarEncoder(scalarEncoderSettings);

            using (StreamReader sr = new StreamReader(E_inFile))
            {
                string line;
                using (StreamWriter sw = new StreamWriter(E_outFile))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        List <int> E_output = new List <int>();
                        string[]   tokens   = line.Split(",");
                        var        E_result = encoder.Encode(tokens[1]);
                        E_output.AddRange(E_result);
                        E_output.AddRange(new int[local_E_outBits - E_output.Count]);
                        var outArr = E_output.ToArray();
                        Debug.WriteLine($"-------------- {tokens[1]} --------------");
                        int[,] E_twoDimenArray = ArrayUtils.Make2DArray(outArr, 9, 47);
                        var E_twoDimArray = ArrayUtils.Transpose(E_twoDimenArray);
                        NeoCortexUtils.DrawBitmap(E_twoDimArray, 1024, 1024, $"{E_outFolder}\\{tokens[0].Replace("/", "-").Replace(":", "-")}.png", Color.Yellow, Color.Black, text: tokens[1]);

                        sw.Write($"{tokens[1]},");
                        for (int i = 0; i < outArr.Length; i++)
                        {
                            sw.Write(outArr[i]);
                            if (i < outArr.Length - 1)
                            {
                                sw.Write(",");
                            }
                        }
                        sw.WriteLine();
                    }
                }
            }

            using (StreamReader sr = new StreamReader(E_noisyinFile))
            {
                string line;
                using (StreamWriter sw = new StreamWriter(E_noisyoutFile))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        List <int> E_output = new List <int>();
                        string[]   tokens   = line.Split(",");
                        var        E_result = noisyencoder.Encode(tokens[1]);
                        E_output.AddRange(E_result);
                        E_output.AddRange(new int[local_E_outBits - E_output.Count]);
                        var outArr = E_output.ToArray();
                        Debug.WriteLine($"-------------- {tokens[1]} --------------");
                        int[,] E_twoDimenArray = ArrayUtils.Make2DArray(outArr, 9, 47);
                        var E_twoDimArray = ArrayUtils.Transpose(E_twoDimenArray);
                        NeoCortexUtils.DrawBitmap(E_twoDimArray, 1024, 1024, $"{E_noisyoutFolder}\\{tokens[0].Replace("/", "-").Replace(":", "-")}.png", Color.Yellow, Color.Black, text: tokens[1]);

                        sw.Write($"{tokens[1]},");
                        for (int i = 0; i < outArr.Length; i++)
                        {
                            sw.Write(outArr[i]);
                            if (i < outArr.Length - 1)
                            {
                                sw.Write(",");
                            }
                        }
                        sw.WriteLine();
                    }
                }
            }
        }
示例#24
0
        //[DataRow("Box")]
        //[DataRow("Horizontal")]
        public void ImageSimilarityExperiment(string inputPrefix)
        {
            //int stableStateCnt = 100;
            double minOctOverlapCycles = 1.0;
            double maxBoost            = 10.0;
            //int inputBits = 100;
            var colDims   = new int[] { 64, 64 };
            int numOfCols = 64 * 64;
            //int numColumns = colDims[0];

            string trainingFolder = "Similarity\\TestFiles";
            int    imgSize        = 28;
            //var colDims = new int[] { 64, 64 };
            //int numOfActCols = colDims[0] * colDims[1];

            string TestOutputFolder = $"Output-{nameof(ImageSimilarityExperiment)}";

            var trainingImages = Directory.GetFiles(trainingFolder, $"{inputPrefix}*.png");

            Directory.CreateDirectory($"{nameof(ImageSimilarityExperiment)}");

            int counter = 0;
            //var parameters = GetDefaultParams();
            ////parameters.Set(KEY.DUTY_CYCLE_PERIOD, 20);
            ////parameters.Set(KEY.MAX_BOOST, 1);
            ////parameters.setInputDimensions(new int[] { imageSize[imSizeIndx], imageSize[imSizeIndx] });
            ////parameters.setColumnDimensions(new int[] { topologies[topologyIndx], topologies[topologyIndx] });
            ////parameters.setNumActiveColumnsPerInhArea(0.02 * numOfActCols);
            //parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.06 * 4096); // TODO. Experiment with different sizes
            //parameters.Set(KEY.POTENTIAL_RADIUS, imgSize * imgSize);
            //parameters.Set(KEY.POTENTIAL_PCT, 1.0);
            //parameters.Set(KEY.GLOBAL_INHIBITION, true); // TODO: Experiment with local inhibition too. Note also the execution time of the experiment.

            //// Num of active synapces in order to activate the column.
            //parameters.Set(KEY.STIMULUS_THRESHOLD, 50.0);
            //parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008);
            //parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.05);

            //parameters.Set(KEY.INHIBITION_RADIUS, (int)0.02 * imgSize * imgSize); // TODO. check if this has influence in a case of the global inhibition. ALso check how this parameter influences the similarity of SDR.

            //parameters.Set(KEY.SYN_PERM_CONNECTED, 0.2);
            //parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001);
            //parameters.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001);
            //parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000);
            //parameters.Set(KEY.MAX_BOOST, 100);
            //parameters.Set(KEY.WRAP_AROUND, true);
            //parameters.Set(KEY.SEED, 1969);
            //parameters.setInputDimensions(new int[] { imgSize, imgSize });
            //parameters.setColumnDimensions(colDims);

            Parameters p = Parameters.getAllDefaultParameters();

            p.Set(KEY.RANDOM, new ThreadSafeRandom(42));
            p.Set(KEY.INPUT_DIMENSIONS, new int[] { imgSize, imgSize });
            p.Set(KEY.COLUMN_DIMENSIONS, colDims);
            p.Set(KEY.CELLS_PER_COLUMN, 10);

            p.Set(KEY.MAX_BOOST, maxBoost);
            p.Set(KEY.DUTY_CYCLE_PERIOD, 50);
            p.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, minOctOverlapCycles);

            // Global inhibition
            // N of 40 (40= 0.02*2048 columns) active cells required to activate the segment.
            p.Set(KEY.GLOBAL_INHIBITION, true);
            p.setNumActiveColumnsPerInhArea(0.02 * numOfCols);
            p.Set(KEY.POTENTIAL_RADIUS, (int)(0.8 * imgSize * imgSize));
            p.Set(KEY.LOCAL_AREA_DENSITY, -1); // In a case of global inhibition.
            //p.setInhibitionRadius( Automatically set on the columns pace in a case of global inhibition.);

            // Activation threshold is 10 active cells of 40 cells in inhibition area.
            p.setActivationThreshold(10);

            // Max number of synapses on the segment.
            p.setMaxNewSynapsesPerSegmentCount((int)(0.02 * numOfCols));

            bool isInStableState = false;

            var mem = new Connections();

            p.apply(mem);

            HomeostaticPlasticityController hpa = new HomeostaticPlasticityController(mem, trainingImages.Length * 50, (isStable, numPatterns, actColAvg, seenInputs) =>
            {
                // Event should only be fired when entering the stable state.
                // Ideal SP should never enter unstable state after stable state.
                Assert.IsTrue(isStable);
                Assert.IsTrue(numPatterns == trainingImages.Length);
                isInStableState = true;
                Debug.WriteLine($"Entered STABLE state: Patterns: {numPatterns}, Inputs: {seenInputs}, iteration: {seenInputs / numPatterns}");
            }, requiredSimilarityThreshold: 0.975);

            SpatialPooler sp = new SpatialPoolerMT(hpa);

            sp.Init(mem, UnitTestHelpers.GetMemory());

            string outFolder = $"{TestOutputFolder}\\{inputPrefix}";

            Directory.CreateDirectory(outFolder);

            string outputHamDistFile = $"{outFolder}\\digit{inputPrefix}_hamming.txt";

            string outputActColFile = $"{outFolder}\\digit{inputPrefix}_activeCol.txt";

            using (StreamWriter swHam = new StreamWriter(outputHamDistFile))
            {
                using (StreamWriter swActCol = new StreamWriter(outputActColFile))
                {
                    int cycle = 0;

                    Dictionary <string, int[]> sdrs         = new Dictionary <string, int[]>();
                    Dictionary <string, int[]> inputVectors = new Dictionary <string, int[]>();

                    while (true)
                    {
                        foreach (var trainingImage in trainingImages)
                        {
                            int[] activeArray = new int[numOfCols];

                            FileInfo fI = new FileInfo(trainingImage);

                            string outputImage = $"{outFolder}\\{inputPrefix}_cycle_{counter}_{fI.Name}";

                            string testName = $"{outFolder}\\{inputPrefix}_{fI.Name}";

                            string inputBinaryImageFile = NeoCortexUtils.BinarizeImage($"{trainingImage}", imgSize, testName);

                            // Read input csv file into array
                            int[] inputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray();

                            int[]             oldArray      = new int[activeArray.Length];
                            List <double[, ]> overlapArrays = new List <double[, ]>();
                            List <double[, ]> bostArrays    = new List <double[, ]>();

                            sp.compute(inputVector, activeArray, true);

                            var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

                            Debug.WriteLine($"Cycle: {cycle++} - Input: {trainingImage}");
                            Debug.WriteLine($"{Helpers.StringifyVector(activeCols)}\n");

                            if (isInStableState)
                            {
                                if (sdrs.Count == trainingImages.Length)
                                {
                                    CalculateSimilarity(sdrs, inputVectors);
                                    return;
                                }

                                var distance = MathHelpers.GetHammingDistance(oldArray, activeArray, true);
                                //var similarity = MathHelpers.CalcArraySimilarity(oldArray, activeArray, true);
                                sdrs.Add(trainingImage, activeCols);
                                inputVectors.Add(trainingImage, inputVector);

                                swHam.WriteLine($"{counter++}|{distance} ");

                                oldArray = new int[numOfCols];
                                activeArray.CopyTo(oldArray, 0);

                                overlapArrays.Add(ArrayUtils.Make2DArray <double>(ArrayUtils.ToDoubleArray(mem.Overlaps), colDims[0], colDims[1]));
                                bostArrays.Add(ArrayUtils.Make2DArray <double>(mem.BoostedOverlaps, colDims[0], colDims[1]));

                                var activeStr = Helpers.StringifyVector(activeArray);
                                swActCol.WriteLine("Active Array: " + activeStr);

                                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, colDims[0], colDims[1]);
                                twoDimenArray        = ArrayUtils.Transpose(twoDimenArray);
                                List <int[, ]> arrays = new List <int[, ]>();
                                arrays.Add(twoDimenArray);
                                arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(inputVector, (int)Math.Sqrt(inputVector.Length), (int)Math.Sqrt(inputVector.Length))));

                                NeoCortexUtils.DrawBitmaps(arrays, outputImage, Color.Yellow, Color.Gray, OutImgSize, OutImgSize);
                                NeoCortexUtils.DrawHeatmaps(overlapArrays, $"{outputImage}_overlap.png", 1024, 1024, 150, 50, 5);
                                NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5);
                            }
                        }
                    }
                }
            }
        }
示例#25
0
        /// <summary>
        /// Processes the test cases for Noise Test taking the necessary parameters.
        /// It's the parent method which calls other utility methods to create the input vectors and the outputs.
        /// </summary>
        /// <param name="inputSequence">An array of double, consisting the starting indexes for each input vector</param>
        /// <param name="inputs">
        /// A parameter of the class "InputParameters", which contains all the input parameters needed as properties which can be set in a test case
        /// </param>
        /// <returns>Returns nothing</returns>
        public void ProcessTestCase(List <double[]> inputSequences, InputParameters inputs)
        {
            string path      = Directory.GetCurrentDirectory();
            string parentDir = path.Substring(0, path.IndexOf("bin") - 1);
            string resultDir = parentDir.Substring(0, parentDir.LastIndexOf(@"\"));

            string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            string outFolder = resultDir + @"\SpatialPooler_Results\" + timeStamp + @"\Output\";
            string inFolder  = resultDir + @"\SpatialPooler_Results\" + timeStamp + @"\InputVectors";

            if (!Directory.Exists(outFolder))
            {
                Directory.CreateDirectory(outFolder);
            }

            if (!Directory.Exists(inFolder + @"\"))
            {
                Directory.CreateDirectory(inFolder);
            }

            //int radius = 0;

            var parameters = GetDefaultParams();

            parameters.Set(KEY.POTENTIAL_RADIUS, 64 * 64);
            parameters.Set(KEY.POTENTIAL_PCT, 1.0);
            parameters.Set(KEY.GLOBAL_INHIBITION, false);
            parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5);
            parameters.Set(KEY.INHIBITION_RADIUS, (int)0.25 * 64 * 64);
            parameters.Set(KEY.LOCAL_AREA_DENSITY, -1);
            parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.1 * 64 * 64);
            parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000000);
            parameters.Set(KEY.MAX_BOOST, 5);
            Console.WriteLine("Fetched all default parameters\n");

            parameters.setInputDimensions(new int[] { 32, 32 });
            parameters.setColumnDimensions(new int[] { 64, 64 });
            parameters.setNumActiveColumnsPerInhArea(0.02 * 64 * 64);
            var sp  = new SpatialPoolerMT();
            var mem = new Connections();

            parameters.apply(mem);
            Console.WriteLine("\nConfiguring the Inputs...\n");
            sp.Init(mem, GetInMemoryDictionary());
            int outFolderCount = 0;

            int compareIndex = Convert.ToInt32(inputs.getCompareNumber());

            double[][] recordOutput    = null;
            double[]   hammingDistance = null;

            foreach (double[] inputSequence in inputSequences)
            {
                outFolderCount++;
                double minVal = 0.0;
                for (int i = 0; i < inputSequence.Length; i++)
                {
                    if (i == 0)
                    {
                        minVal = inputSequence[i];
                    }
                    else if (inputSequence[i] < minVal)
                    {
                        minVal = inputSequence[i];
                    }
                }
                minVal -= 1.0;

                Console.WriteLine("\nGetting the Input Vectors...\n");
                var inputVectors = GetEncodedSequence(inputSequence, minVal, inputs.getMaxIndex(), inputs, inFolder);

                int count = 1;
                //string output = String.Empty;
                int max = 0;
                for (int i = 0; i < inputVectors.Count; i++)
                {
                    hammingDistance = null;
                    //output = String.Empty;
                    Console.WriteLine("Computing the Output for the vector no: " + count.ToString() + "...\n");
                    var activeArray = sp.Compute(inputVectors[i], true) as int[];

                    for (int j = 0; j < activeArray.Length; j++)
                    {
                        if (activeArray[j] > max)
                        {
                            max = activeArray[j];
                        }
                    }

                    //var str = Helpers.StringifyVector(activeArray);

                    int rows    = Convert.ToInt32(Math.Ceiling(Math.Sqrt(Convert.ToDouble(max))));
                    int counter = 0;
                    int index   = 0;
                    int[,] outTwoDArray = new int[rows, rows];

                    for (int j = 0; j < rows; j++)
                    {
                        for (int k = 0; k < rows; k++)
                        {
                            outTwoDArray[j, k] = 0;
                        }
                    }

                    for (int j = 0; j < rows; j++)
                    {
                        for (int k = 0; k < rows; k++)
                        {
                            counter++;
                            if (index < activeArray.Length && activeArray[index] == counter)
                            {
                                index++;
                                outTwoDArray[j, k] = 1;
                            }
                        }
                    }

                    double[][] comparingArray = new double[rows][];
                    for (int j = 0; j < rows; j++)
                    {
                        comparingArray[j] = new double[rows];
                        for (int k = 0; k < rows; k++)
                        {
                            comparingArray[j][k] = Convert.ToDouble(outTwoDArray[j, k]);
                        }
                    }

                    int[,] record2Darray = null;
                    if (inputSequence[i] == compareIndex)
                    {
                        if (recordOutput != null)
                        {
                            hammingDistance = MathHelpers.GetHammingDistance(recordOutput, comparingArray, false);
                            record2Darray   = new int[recordOutput.Length, recordOutput.Length];
                            for (int j = 0; j < recordOutput.Length; j++)
                            {
                                for (int k = 0; k < recordOutput.Length; k++)
                                {
                                    record2Darray[j, k] = Convert.ToInt32(recordOutput[j][k]);
                                }
                            }
                        }

                        recordOutput = new double[rows][];

                        for (int j = 0; j < rows; j++)
                        {
                            recordOutput[j] = new double[rows];
                            for (int k = 0; k < rows; k++)
                            {
                                recordOutput[j][k] = comparingArray[j][k];
                            }
                        }
                    }

                    if (hammingDistance != null)
                    {
                        int rowHam = Convert.ToInt32(Math.Ceiling(Math.Sqrt(hammingDistance.Length)));
                        int[,] hammingArray = new int[rowHam, rowHam];
                        int limit = 0;

                        for (int j = 0; j < rowHam; j++)
                        {
                            for (int k = 0; k < rowHam; k++)
                            {
                                if (limit < hammingDistance.Length)
                                {
                                    //hj
                                    hammingArray[j, k] = Convert.ToInt32(hammingDistance[limit]);
                                    limit++;
                                }
                            }
                        }

                        int compare_no = 1;
                        if (!File.Exists($"{outFolder}\\Compare_{compareIndex}.png"))
                        {
                            DrawBitmapHamming(hammingArray, 1024, 1024, $"{outFolder}\\Compare_{compareIndex}.png", $"Compare_{compareIndex}");
                            DrawBitmapOverlap(record2Darray, outTwoDArray, 1024, 1024, $"{outFolder}\\Overlap_{compareIndex}.png", $"Overlap_{compareIndex}");
                        }
                        else
                        {
                            while (File.Exists($"{outFolder}\\Compare_{compareIndex}_{compare_no}.png"))
                            {
                                compare_no++;
                            }
                            DrawBitmapHamming(hammingArray, 1024, 1024, $"{outFolder}\\Compare_{compareIndex}_{compare_no}.png", $"Compare_{compareIndex}");
                            compare_no = 1;
                            while (File.Exists($"{outFolder}\\Overlap_{compareIndex}_{compare_no}.png"))
                            {
                                compare_no++;
                            }
                            DrawBitmapOverlap(record2Darray, outTwoDArray, 1024, 1024, $"{outFolder}\\Overlap_{compareIndex}_{compare_no}.png", $"Overlap_{compareIndex}");
                        }
                    }

                    if (!Directory.Exists(outFolder + @"\\" + outFolderCount.ToString()))
                    {
                        Directory.CreateDirectory(outFolder + @"\\" + outFolderCount.ToString());
                    }

                    int[,] out2dimArray = ArrayUtils.Transpose(outTwoDArray);
                    NeoCortexUtils.DrawBitmap(out2dimArray, 1024, 1024, $"{outFolder}\\{outFolderCount}\\{count}.png", Color.Black, Color.Green, text: inputSequence[i].ToString());

                    //File.WriteAllLines(outFolder + count.ToString() + ".txt", new string[] { str });
                    Console.WriteLine("Output is recorded in the path: " + outFolder + count.ToString() + ".txt\n");
                    count++;
                }
            }
        }
        private float Train(int inpBits, IHtmModule <object, object> network, HtmClassifier <double, ComputeCycle> cls, bool isNewBornMode = true)
        {
            float accurracy;

            string outFolder = nameof(RunPowerPredictionExperiment);

            Directory.CreateDirectory(outFolder);

            CortexNetworkContext ctx = new CortexNetworkContext();

            Dictionary <string, object> scalarEncoderSettings = getScalarEncoderDefaultSettings(inpBits);
            //var dateTimeEncoderSettings = getFullDateTimeEncoderSettings();

            ScalarEncoder scalarEncoder = new ScalarEncoder(scalarEncoderSettings);
            //DateTimeEncoder dtEncoder = new DateTimeEncoder(dateTimeEncoderSettings, DateTimeEncoder.Precision.Hours);

            string fileName = "TestFiles\\rec-center-hourly-short.csv";

            using (StreamReader sr = new StreamReader(fileName))
            {
                string line;
                int    cnt     = 0;
                int    matches = 0;

                using (StreamWriter sw = new StreamWriter("out.csv"))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        cnt++;

                        //if (isNewBornMode && cnt > 100) break;

                        bool x = false;
                        if (x)
                        {
                            break;
                        }
                        List <int> output = new List <int>();

                        string[] tokens = line.Split(",");

                        // Encode scalar value
                        var result = scalarEncoder.Encode(tokens[1]);

                        output.AddRange(result);

                        // This part adds datetime components to the input vector.
                        //output.AddRange(new int[scalarEncoder.Offset]);
                        //DateTime dt = DateTime.Parse(tokens[0], CultureInfo.InvariantCulture);
                        // Encode date/time/hour.
                        //result = dtEncoder.Encode(new DateTimeOffset(dt, TimeSpan.FromMilliseconds(0)));
                        //output.AddRange(result);

                        // This performs a padding to the inputBits = 10404 = 102*102.
                        output.AddRange(new int[inpBits - output.Count]);

                        var outArr = output.ToArray();

                        Debug.WriteLine($"-------------- {tokens[1]} --------------");

                        if (isNewBornMode)
                        {
                            for (int j = 0; j < 10; j++)
                            {
                                // Output here are active cells.
                                var res = network.Compute(output.ToArray(), true);

                                Debug.WriteLine(Helpers.StringifyVector(((int[])res)));
                            }
                        }
                        else
                        {
                            var lyrOut = network.Compute(output.ToArray(), true) as ComputeCycle;;

                            double input = Convert.ToDouble(tokens[1], CultureInfo.InvariantCulture);

                            if (input == lastPredictedValue)
                            {
                                matches++;
                                Debug.WriteLine($"Match {input}");
                            }
                            else
                            {
                                Debug.WriteLine($"Missmatch Actual value: {input} - Predicted value: {lastPredictedValue}");
                            }

                            cls.Learn(input, lyrOut.ActiveCells.ToArray());

                            lastPredictedValue = cls.GetPredictedInputValue(lyrOut.PredictiveCells.ToArray());

                            sw.WriteLine($"{tokens[0]};{input.ToString(CultureInfo.InvariantCulture)};{lastPredictedValue.ToString(CultureInfo.InvariantCulture)}");

                            Debug.WriteLine($"W: {Helpers.StringifyVector(lyrOut.WinnerCells.Select(c => c.Index).ToArray())}");
                            Debug.WriteLine($"P: {Helpers.StringifyVector(lyrOut.PredictiveCells.Select(c => c.Index).ToArray())}");
                            Debug.WriteLine($"Current input: {input} Predicted Input: {lastPredictedValue}");

                            int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(outArr, (int)Math.Sqrt(outArr.Length), (int)Math.Sqrt(output.Count));
                            var twoDimArray = ArrayUtils.Transpose(twoDimenArray);
                            NeoCortexUtils.DrawBitmap(twoDimArray, 1024, 1024, $"{outFolder}\\{tokens[0].Replace("/", "-").Replace(":", "-")}.png", Color.Yellow, Color.Black, text: input.ToString());
                        }

                        Debug.WriteLine($"NewBorn stage: {isNewBornMode} - record: {cnt}");
                    }
                }

                accurracy = (float)matches / (float)cnt * (float)100.0;
            }

            return(accurracy);
        }
        /// <summary>
        /// This function train the input image and write result to text files in folder @"/OutputDutyCycle"
        /// The result text files include speed comparison between global inhibition and local inhibition,
        /// the stable of the out put array (by comparing hamming distance arrays).
        /// Finally this method draw an image of active column as .png file.
        /// This training method is used for testing speed of training with different value of max boost and duty cycle
        /// </summary>
        /// <param name="inputBinarizedFile">input image after binarized</param>
        /// <param name="hammingFile">Path to hamming distance output file</param>
        /// <param name="outputSpeedFile">Path to speed comparison output file</param>
        /// <param name="outputImage">Path to active column after training output file (as .png image file)</param>
        /// <param name="parameters">Parameter setup</param>
        private static void Training(string inputBinarizedFile, string hammingFile, string outputSpeedFile, string outputImage, Parameters parameters)
        {
            int outputImageSize = 1024;
            int topology        = parameters.Get <int[]>(KEY.COLUMN_DIMENSIONS)[0];
            int activeColumn    = topology * topology;
            var stopwatch       = new Stopwatch();

            using (StreamWriter swHamming = new StreamWriter(hammingFile))
            {
                using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true))
                {
                    var sp  = new SpatialPooler();
                    var mem = new Connections();

                    parameters.apply(mem);

                    stopwatch.Start();
                    sp.Init(mem);
                    stopwatch.Stop();

                    int   actiColumnLength = activeColumn;
                    int[] activeArray      = new int[actiColumnLength];

                    // Read input csv file into array
                    int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinarizedFile).ToArray();

                    stopwatch.Restart();

                    int   iterations = 1000;
                    int[] oldArray   = new int[activeArray.Length];

                    for (int k = 0; k < iterations; k++)
                    {
                        sp.compute(inputVector, activeArray, true);

                        var activeCols = activeArray.IndexWhere((el) => el == 1);
                        var distance   = MathHelpers.GetHammingDistance(oldArray, activeArray);
                        var similarity = MathHelpers.CalcArraySimilarity(oldArray, activeArray);
                        swHamming.WriteLine($"{distance} | {similarity}");
                        var str = Helpers.StringifyVector(activeCols);
                        Debug.WriteLine(str);
                        oldArray = new int[actiColumnLength];
                        activeArray.CopyTo(oldArray, 0);
                    }

                    var activeArrayString = Helpers.StringifyVector(activeArray);

                    stopwatch.Stop();

                    Debug.WriteLine("Active Array: " + activeArrayString);

                    int    potentialRadius    = parameters.Get <int>(KEY.POTENTIAL_RADIUS);
                    bool   isGlobalInhibition = parameters.Get <bool>(KEY.GLOBAL_INHIBITION);
                    string inhibition         = isGlobalInhibition ? "Global" : "Local";
                    double milliseconds       = stopwatch.ElapsedMilliseconds;
                    double seconds            = milliseconds / 1000;
                    swSpeed.WriteLine($"Column dimension: {topology.ToString().PadRight(5)} |Potential Radius: {potentialRadius}| Inhibition type: {inhibition.PadRight(7)} | Total time: {milliseconds:N0} milliseconds ({seconds:N2} seconds).");

                    int[,] twoDimenArray = ArrayUtils.Make2DArray(activeArray, topology, topology);
                    twoDimenArray        = ArrayUtils.Transpose(twoDimenArray);

                    NeoCortexUtils.DrawBitmap(twoDimenArray, outputImageSize, outputImageSize, outputImage);
                }
            }
        }
        /// <summary>
        /// This function train the input image and write result to text files in folder @"/Output"
        /// The result text files include speed comparison between global inhibition and local inhibition,
        /// the stable of the out put array (by comparing hamming distance arrays).
        /// Finally this method draw an image of active column as .png file.
        /// </summary>
        /// <param name="imageSize">Size of the image (image has same width and height)</param>
        /// <param name="columnDimension">List of sparse space size.(with same width and height)</param>
        /// <param name="inputBinarizedFile">input image after binarized</param>
        /// <param name="hammingFile">Path to hamming distance output file </param>
        /// <param name="outputSpeedFile">Path to speed comparison output file</param>
        /// <param name="activeColumnFile">Path to active column after training output file (as array text)</param>
        /// <param name="outputImage">Path to active column after training output file (as .png image file)</param>
        /// <param name="isGlobalInhibition">is using Global inhibition algorithms or not (if false using local inhibition)</param>
        private static void Training(int imageSize, int columnDimension, string inputBinarizedFile, string hammingFile, string outputSpeedFile, string activeColumnFile, string outputImage, bool isGlobalInhibition)
        {
            int outputImageSize = 1024;
            int activeColumn    = columnDimension * columnDimension;
            var stopwatch       = new Stopwatch();

            using (StreamWriter swHamming = new StreamWriter(hammingFile))
            {
                using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true))
                {
                    using (StreamWriter swActiveColumn = new StreamWriter(activeColumnFile))
                    {
                        var parameters = SetupParameters(imageSize, columnDimension, isGlobalInhibition);
                        var sp         = new SpatialPooler();
                        var mem        = new Connections();

                        parameters.apply(mem);

                        stopwatch.Start();
                        sp.Init(mem);
                        stopwatch.Stop();

                        int   actiColumnLength = activeColumn;
                        int[] activeArray      = new int[actiColumnLength];

                        // Read input csv file into array
                        int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinarizedFile).ToArray();

                        stopwatch.Restart();

                        int   iterations = 300;
                        int[] oldArray   = new int[activeArray.Length];

                        for (int k = 0; k < iterations; k++)
                        {
                            sp.compute(inputVector, activeArray, true);

                            var activeCols = activeArray.IndexWhere((el) => el == 1);
                            var distance   = MathHelpers.GetHammingDistance(oldArray, activeArray);
                            var similarity = MathHelpers.CalcArraySimilarity(oldArray, activeArray);
                            swHamming.WriteLine($"{distance} | {similarity}");
                            var str = Helpers.StringifyVector(activeCols);
                            Debug.WriteLine(str);
                            oldArray = new int[actiColumnLength];
                            activeArray.CopyTo(oldArray, 0);
                        }

                        stopwatch.Stop();
                        var activeArrayString = Helpers.StringifyVector(activeArray);
                        swActiveColumn.WriteLine("Active Array: " + activeArrayString);

                        string inhibition   = isGlobalInhibition ? "Global" : "Local";
                        double milliseconds = stopwatch.ElapsedMilliseconds;
                        double seconds      = milliseconds / 1000;
                        swSpeed.WriteLine($"Topology: {columnDimension.ToString().PadRight(5)} | Inhibition type: {inhibition.PadRight(7)} | Total time: {milliseconds:N0} milliseconds ({seconds:N2} seconds).");

                        int[,] twoDimenArray = ArrayUtils.Make2DArray(activeArray, columnDimension, columnDimension);
                        twoDimenArray        = ArrayUtils.Transpose(twoDimenArray);

                        NeoCortexUtils.DrawBitmap(twoDimenArray, outputImageSize, outputImageSize, outputImage);
                    }
                }
            }
        }
        // [DataRow("MnistTestImages\\digit7.png", 128, 30)]
        public void LearningimageDoubleShiftStble(string mnistImage, string shiftedImage, int[] imageSize, int[] topologies)
        {
            var path        = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName, mnistImage);
            var pathShifted = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName, shiftedImage);

            Console.WriteLine("Test started");
            Console.WriteLine(mnistImage);
            Console.WriteLine(shiftedImage);
            const int OutImgSize = 1024;

            int    index1 = mnistImage.IndexOf("\\") + 1;
            int    index2 = mnistImage.IndexOf(".");
            string sub1   = mnistImage.Substring(0, index2);
            string sub2   = mnistImage.Substring(0, index1);
            string name   = mnistImage.Substring(index1, sub1.Length - sub2.Length);

            int    index1Shift = shiftedImage.IndexOf("\\") + 1;
            int    index2Shift = shiftedImage.IndexOf(".");
            string sub1Shift   = shiftedImage.Substring(0, index2Shift);
            string sub2Shift   = shiftedImage.Substring(0, index1Shift);
            string nameShift   = shiftedImage.Substring(index1Shift, sub1Shift.Length - sub2Shift.Length);

            for (int imSizeIndx = 0; imSizeIndx < imageSize.Length; imSizeIndx++)
            {
                Console.WriteLine(String.Format("Image Size: \t{0}", imageSize[imSizeIndx]));

                string testName        = $"{name}_{imageSize[imSizeIndx]}";
                string outputSpeedFile = $"Output\\{testName}_speed.txt";

                string testNameShifted        = $"{nameShift}_{imageSize[imSizeIndx]}";
                string outputSpeedFileShifted = $"Output\\{testNameShifted}_speed.txt";

                string inputBinaryImageFile        = BinarizeImage(path, imageSize[imSizeIndx], testName);
                string inputBinaryImageShiftedFile = BinarizeImage(pathShifted, imageSize[imSizeIndx], testNameShifted);

                //string inputBinaryImageFile = BinarizeImage("Output\\" + mnistImage, imageSize[imSizeIndx], testName);
                for (int topologyIndx = 0; topologyIndx < topologies.Length; topologyIndx++)
                {
                    Console.WriteLine(String.Format("Topology: \t{0}", topologies[topologyIndx]));

                    string finalName         = $"{testName}_{topologies[topologyIndx]}";
                    string outputHamDistFile = $"Output\\{finalName}_hamming.txt";
                    string outputActColFile  = $"Output\\{finalName}_activeCol.txt";

                    string outputImage = $"Output\\{finalName}.png";

                    string finalNameShifted         = $"{testNameShifted}_{topologies[topologyIndx]}";
                    string outputHamDistFileShifted = $"Output\\{finalNameShifted}_hamming.txt";
                    string outputActColFileShifted  = $"Output\\{finalNameShifted}_activeCol.txt";

                    string outputImageShifted = $"Output\\{finalNameShifted}.png";


                    //File.Create(finalName);
                    //Directory.CreateDirectory("Output");
                    //File.Create(outputHamDistFile);
                    //File.Create(outputActColFile);

                    int numOfActCols = 0;
                    var sw           = new Stopwatch();
                    using (StreamWriter swHam = new StreamWriter(outputHamDistFile))
                    {
                        using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true))
                        {
                            using (StreamWriter swActCol = new StreamWriter(outputActColFile))
                            {
                                numOfActCols = topologies[topologyIndx] * topologies[topologyIndx];
                                var parameters = GetDefaultParams();
                                parameters.setInputDimensions(new int[] { imageSize[imSizeIndx], imageSize[imSizeIndx] });
                                parameters.setColumnDimensions(new int[] { topologies[topologyIndx], topologies[topologyIndx] });
                                parameters.setNumActiveColumnsPerInhArea(0.02 * numOfActCols);

                                var sp  = new SpatialPooler();
                                var mem = new Connections();

                                parameters.apply(mem);
                                sw.Start();

                                sp.Init(mem);

                                sw.Stop();
                                swSpeed.WriteLine($"{topologies[topologyIndx]}|{(double)sw.ElapsedMilliseconds / (double)1000}");

                                int   actiColLen  = numOfActCols;
                                int[] activeArray = new int[actiColLen];

                                //Read input csv file into array
                                int[] inputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray();

                                var inputOverlap  = new List <double>();
                                var outputOverlap = new List <double>();

                                int[]      newActiveArray       = new int[topologies[topologyIndx] * topologies[topologyIndx]];
                                double[][] newActiveArrayDouble = new double[1][];
                                newActiveArrayDouble[0] = new double[newActiveArray.Length];

                                //Training
                                sp.compute(inputVector, activeArray, true);

                                var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);

                                double[][] oldActiveArray = new double[1][];
                                oldActiveArray[0] = new double[activeArray.Length];
                                for (int a = 0; a < activeArray.Length; a++)
                                {
                                    oldActiveArray[0][a] = activeArray[a];
                                }

                                int isTrained = 0;

                                while (isTrained == 0)
                                {
                                    sp.compute(inputVector, newActiveArray, true);

                                    activeCols = ArrayUtils.IndexWhere(newActiveArray, (el) => el == 1);



                                    for (int a = 0; a < newActiveArray.Length; a++)
                                    {
                                        newActiveArrayDouble[0][a] = newActiveArray[a];
                                    }

                                    if (MathHelpers.GetHammingDistance(oldActiveArray, newActiveArrayDouble, true)[0] == 100)
                                    {
                                        isTrained = 1;
                                    }
                                    else
                                    {
                                        isTrained      = 0;
                                        oldActiveArray = newActiveArrayDouble;
                                    }
                                }

                                var str = Helpers.StringifyVector(activeCols);


                                int[] oldInputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray();

                                int[] inputVectorShifted = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageShiftedFile).ToArray();

                                int[]      activeArrayShifted       = new int[topologies[topologyIndx] * topologies[topologyIndx]];
                                double[][] activeArrayShiftedDouble = new double[1][];
                                activeArrayShiftedDouble[0] = new double[activeArrayShifted.Length];

                                sw.Restart();

                                //Prediction
                                sp.compute(inputVectorShifted, activeArrayShifted, false);

                                var resActiveColsPercent = ArrayUtils.IndexWhere(activeArrayShifted, (el) => el == 1);

                                var resStrPercent = Helpers.StringifyVector(activeArrayShifted);


                                for (int a = 0; a < activeArrayShifted.Length; a++)
                                {
                                    activeArrayShiftedDouble[0][a] = activeArrayShifted[a];
                                }



                                var distance    = MathHelpers.GetHammingDistance(newActiveArrayDouble, activeArrayShiftedDouble, false)[0];
                                var distPercent = ((100 - distance) * 100) / (topologies[topologyIndx] * topologies[topologyIndx]);
                                swHam.WriteLine(distance + "\t" + (100 - distance) + "\t" + distPercent + "\t" + (1 - (distPercent / 100.0)) + "\n");

                                outputOverlap.Add(1 - (distPercent / 100.0));

                                swActCol.WriteLine(String.Format(@"Active Cols: {0}", Helpers.StringifyVector(resActiveColsPercent)));

                                Console.WriteLine(resStrPercent);
                                swActCol.WriteLine("Active Array: " + resStrPercent);

                                sw.Stop();

                                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArrayShifted, topologies[topologyIndx], topologies[topologyIndx]);
                                twoDimenArray        = ArrayUtils.Transpose(twoDimenArray);

                                NeoCortexUtils.DrawBitmap(twoDimenArray, OutImgSize, OutImgSize, outputImage);
                                //NeoCortexUtils.DrawBitmap(twoDimenArray, OutImgSize, OutImgSize, "D:\\Project_Latest\\FromGit\\se-dystsys-2018-2019-softwareengineering\\outputs\\eight");


                                swActCol.WriteLine("inputOverlaps: " + Helpers.StringifyVector(inputOverlap.ToArray()));

                                swActCol.WriteLine("outputOverlaps: " + Helpers.StringifyVector(outputOverlap.ToArray()));
                            }
                        }
                    }
                }
            }
        }
        //[DataRow("MnistTestImages\\digit7.png", 128, 30)]
        public void CalculateSpeedOfLearningTest(string mnistImage, int[] imageSize, int[] topologies)
        {
            int    index1 = mnistImage.IndexOf("\\") + 1;
            int    index2 = mnistImage.IndexOf(".");
            string sub1   = mnistImage.Substring(0, index2);
            string sub2   = mnistImage.Substring(0, index1);
            string name   = mnistImage.Substring(index1, sub1.Length - sub2.Length);

            for (int imSizeIndx = 0; imSizeIndx < imageSize.Length; imSizeIndx++)
            {
                string testName             = $"{name}_{imageSize[imSizeIndx]}";
                string outputSpeedFile      = $"Output\\{testName}_speed.txt";
                string inputBinaryImageFile = BinarizeImage("Output\\" + mnistImage, imageSize[imSizeIndx], testName);
                for (int topologyIndx = 0; topologyIndx < topologies.Length; topologyIndx++)
                {
                    string finalName         = $"{testName}_{topologies[topologyIndx]}";
                    string outputHamDistFile = $"Output\\{finalName}_hamming.txt";
                    string outputActColFile  = $"Output\\{finalName}_activeCol.txt";

                    string outputImage = $"Output\\{finalName}.png";

                    int numOfActCols = 0;
                    var sw           = new Stopwatch();
                    using (StreamWriter swHam = new StreamWriter(outputHamDistFile))
                    {
                        using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true))
                        {
                            using (StreamWriter swActCol = new StreamWriter(outputActColFile))
                            {
                                numOfActCols = topologies[topologyIndx] * topologies[topologyIndx];
                                var parameters = GetDefaultParams();
                                parameters.setInputDimensions(new int[] { imageSize[imSizeIndx], imageSize[imSizeIndx] });
                                parameters.setColumnDimensions(new int[] { topologies[topologyIndx], topologies[topologyIndx] });
                                parameters.setNumActiveColumnsPerInhArea(0.02 * numOfActCols);

                                var sp  = new SpatialPooler();
                                var mem = new Connections();

                                parameters.apply(mem);
                                sw.Start();

                                sp.Init(mem);

                                sw.Stop();
                                swSpeed.WriteLine($"{topologies[topologyIndx]}|{(double)sw.ElapsedMilliseconds / (double)1000}");

                                int   actiColLen  = numOfActCols;
                                int[] activeArray = new int[actiColLen];

                                //Read input csv file into array
                                int[] inputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray();
                                sw.Restart();

                                int   iterations = 2;
                                int[] oldArray   = new int[activeArray.Length];
                                for (int k = 0; k < iterations; k++)
                                {
                                    sp.compute(inputVector, activeArray, true);

                                    var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1);
                                    var distance   = MathHelpers.GetHammingDistance(oldArray, activeArray);
                                    swHam.WriteLine(distance + "\n");
                                    var str = Helpers.StringifyVector(activeCols);

                                    oldArray = new int[actiColLen];
                                    activeArray.CopyTo(oldArray, 0);
                                }

                                var activeStr = Helpers.StringifyVector(activeArray);
                                swActCol.WriteLine("Active Array: " + activeStr);

                                sw.Stop();

                                int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, topologies[topologyIndx], topologies[topologyIndx]);
                                twoDimenArray        = ArrayUtils.Transpose(twoDimenArray);

                                NeoCortexUtils.DrawBitmap(twoDimenArray, OutImgSize, OutImgSize, outputImage);
                            }
                        }
                    }
                }
            }
        }