public void ThrowsFileNotFoundException()
        {
            string fileName = Path.GetTempFileName();

            File.Delete(fileName);
            BSDataObject doesntMatter = BSRepository.DataObjectFromFile(fileName);
        }
 protected BSDataObject ProcessUsingFourierDirection(BSDataObject iObject, FourierTransform.Direction iDir)
 {
     Complex[] complexDataArray = (from tmp in iObject.DataArray select new Complex(tmp, 0)).ToArray();
     FourierTransform.DFT(complexDataArray, iDir);
     double[] resultingArray = (from tmp in complexDataArray select Math.Sqrt(tmp.Re * tmp.Re + tmp.Im * tmp.Im)).ToArray();
     return(new BSDataObject(resultingArray, iObject.ObjName + "_Fouirer"));
 }
Exemple #3
0
        static void Main(string[] args)
        {
            Random rnd = new Random();
            //Creating an array with values, corresponding to linear function with added white noise
            const int n = 360;

            double[] array = new double[n];
            for (int i = 0; i < n; i++)
            {
                double noise = 0;
                noise = (double)rnd.Next(Int32.MinValue, Int32.MaxValue) / (Int32.MaxValue / rnd.Next(1, 30));
                //if (i % 10 == 0) noise = 100; //Noise for tukey filter testing

                array[i] = (double)i + noise;
            }
            BSDataObject source     = new BSDataObject(array, "SourceData");
            IVisualizer  visualizer = new SimpleVisualizationHandler();

            visualizer.AddToGraph(source);
            visualizer.AddToGraph(new MedianPreprocessor().Process(source));
            using (var ms = new MemoryStream(n * (sizeof(double) + sizeof(char))))
            {
                StreamWriter sw = new StreamWriter(ms);
                for (int i = 0; i < n; i++)
                {
                    array[i] = 80 * Math.Sin((double)i * Math.PI / 180.0);
                    sw.WriteLine(array[i]);
                }
                sw.Flush();
                ms.Position = 0;
                BSDataObject target = DataObjectSupplier.GetDataObjectForStream(ms, "Sine from memory stream");
                visualizer.AddToGraph(target);
            }
            (visualizer as SimpleVisualizationHandler)._TestShow();
        }
        /// <summary>
        /// A test for GenerateInputAndOutput
        ///</summary>
        //[TestMethod()]
        public void GenerateInputAndOutputTest()
        {
            uint iLag = 1;
            uint iWindowSize = 5;
            int pointsNum = 360;
            IInputProvider target = new WindowingInputProvider(iLag, iWindowSize);
            Random rnd = new Random();
            double[] iTimeSeries = new double[pointsNum];
            for (int i = 0; i < pointsNum; i++)
            {
                iTimeSeries[i] = 1 * Math.Sin(2 * i * Math.PI / 180.0) + (Convert.ToDouble(rnd.Next(-2, 2)) / 5) * ((i % 3 == 0) ? 1 : 0);
            }
            double[][] oInput = null;
            double[][] oOutput = null;
            target.GenerateInputAndOutput(iTimeSeries, out oInput, out oOutput);
            IForecastingModelBuilder modelBuilder = new ANNmodelBuilder();
            IForecastingModel model = modelBuilder.TrainNewModel(oInput, oOutput);

            double[] forecast = new double[oInput.LongLength], actual = new double[oInput.LongLength];
            for (int i = 0; i < oInput.LongLength; ++i)
            {
                forecast[i] = model.CalculateOutput(oInput[i])[0]; actual[i] = oOutput[i][0];
            }

            BSDataObject forecastDO = new BSDataObject(forecast, "Forecast"), actualDO = new BSDataObject(actual, "Actual");

            SimpleVisualizationHandler handler = new SimpleVisualizationHandler();

            handler.AddToGraph(actualDO);
            handler.AddToGraph(forecastDO);
            handler._TestShow();
        }
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        /// A test for GenerateInputAndOutput
        ///</summary>
        //[TestMethod()]
        public void GenerateInputAndOutputTest()
        {
            uint           iLag        = 1;
            uint           iWindowSize = 5;
            int            pointsNum   = 360;
            IInputProvider target      = new WindowingInputProvider(iLag, iWindowSize);
            Random         rnd         = new Random();

            double[] iTimeSeries = new double[pointsNum];
            for (int i = 0; i < pointsNum; i++)
            {
                iTimeSeries[i] = 1 * Math.Sin(2 * i * Math.PI / 180.0) + (Convert.ToDouble(rnd.Next(-2, 2)) / 5) * ((i % 3 == 0) ? 1 : 0);
            }
            double[][] oInput  = null;
            double[][] oOutput = null;
            target.GenerateInputAndOutput(iTimeSeries, out oInput, out oOutput);
            IForecastingModelBuilder modelBuilder = new ANNmodelBuilder();
            IForecastingModel        model        = modelBuilder.TrainNewModel(oInput, oOutput);

            double[] forecast = new double[oInput.LongLength], actual = new double[oInput.LongLength];
            for (int i = 0; i < oInput.LongLength; ++i)
            {
                forecast[i] = model.CalculateOutput(oInput[i])[0]; actual[i] = oOutput[i][0];
            }

            BSDataObject forecastDO = new BSDataObject(forecast, "Forecast"), actualDO = new BSDataObject(actual, "Actual");

            SimpleVisualizationHandler handler = new SimpleVisualizationHandler();


            handler.AddToGraph(actualDO);
            handler.AddToGraph(forecastDO);
            handler._TestShow();
        }
 protected BSDataObject ProcessUsingFourierDirection(BSDataObject iObject, FourierTransform.Direction iDir)
 {
     Complex[] complexDataArray = (from tmp in iObject.DataArray select new Complex(tmp, 0)).ToArray();
     FourierTransform.DFT(complexDataArray, iDir);
     double[] resultingArray = (from tmp in complexDataArray select Math.Sqrt(tmp.Re * tmp.Re + tmp.Im * tmp.Im)).ToArray();
     return new BSDataObject(resultingArray, iObject.ObjName + "_Fouirer");
 }
        static void Main(string[] args)
        {
            Random rnd = new Random();
            //Creating an array with values, corresponding to linear function with added white noise
            const int n = 360;
            double[] array = new double[n];
            for (int i = 0; i < n; i++)
            {
                double noise = 0;
                noise = (double)rnd.Next(Int32.MinValue, Int32.MaxValue) / (Int32.MaxValue / rnd.Next(1, 30));
                //if (i % 10 == 0) noise = 100; //Noise for tukey filter testing

                array[i] = (double)i + noise;
            }
            BSDataObject source = new BSDataObject(array, "SourceData");
            IVisualizer visualizer = new SimpleVisualizationHandler();
            visualizer.AddToGraph(source);
            visualizer.AddToGraph(new MedianPreprocessor().Process(source));
            using (var ms = new MemoryStream(n * (sizeof(double) + sizeof(char))))
            {
                StreamWriter sw = new StreamWriter(ms);
                for (int i = 0; i < n; i++)
                {
                    array[i] = 80 * Math.Sin((double)i * Math.PI / 180.0);
                    sw.WriteLine(array[i]);
                }
                sw.Flush();
                ms.Position = 0;
                BSDataObject target = DataObjectSupplier.GetDataObjectForStream(ms, "Sine from memory stream");
                visualizer.AddToGraph(target);
            }
            (visualizer as SimpleVisualizationHandler)._TestShow();
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="iObj">See IVisualizer interface doc string</param>
 /// <param name="iColor">A Console color for the future graph</param>
 public void AddToGraph(BSDataObject iObj, ConsoleColor iColor)
 {
     if (dataObjectsAndColors.ContainsKey(iObj))
     {
         return;
     }
     AddToDictAndGraphForm(iObj, iColor);
 }
 public BSDataObject Process(BSDataObject iObj)
 {
     double[] iArray = iObj.DataArray;
     int count = iArray.Count();
     double[] result = new double[count - 1];
     for (int i = 0; i < count - 1; i++)
         result[i] = iArray[i + 1] - iArray[i];
     return new BSDataObject(result, iObj.ObjName + "_Derivate");
 }
 public static void SaveDataObjectToFile(BSDataObject iObj, string iFilePath)
 {
     if (null == iObj || null == iFilePath)
         throw new ArgumentNullException();
     using (StreamWriter sw = new StreamWriter(iFilePath))
     {
         foreach (double number in iObj.DataArray)
             sw.WriteLine(number);
     }
 }
Exemple #11
0
        public void ProcessMedianForEvenWindowSizeTest()
        {
            MedianPreprocessor target = new MedianPreprocessor(4);

            double[]     iArray   = { 4, 1, 2, 5, 6, 7, 10, 3, 5, 2 };
            double[]     expected = { 2, 2, 5, 6, 6, 5, 3 };
            BSDataObject actual   = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
        }
 /// <summary>
 ///A test for Visualize
 ///</summary>
 //[TestMethod()]
 public void VisualizeTest()
 {
     SimpleVisualizationHandler target = new SimpleVisualizationHandler();
     double[] iArray = {1.2, 3.4, 5.6, 2.2, 7.7};
     BSDataObject obj = new BSDataObject(iArray, "asdasd");
     target.AddToGraph(obj);
     iArray = new double[] { 2.6, 7.8, 12.2, 10, 2.2, 3.3, 4.4, 5.5, 3.3, -5, 30};
     target.AddToGraph(new BSDataObject(iArray, "anotherGraph"));
     target._TestShow();
 }
        /// <summary>
        /// Creates a new DataObject using data in file at <paramref name="iFilePath"/> path.
        /// </summary>
        /// <param name="iFilePath">A path to the file with data - relative or absolute.</param>
        /// <exception cref="System.ArgumentNullException"/>
        /// <exception cref="System.UnauthorizedAccessException"/>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.IO.FileNotFoundException"/>
        /// <exception cref="System.IO.PathTooLongException"/>
        /// <exception cref="System.FormatException"/>
        /// <returns>New Dataobject.</returns>
        public static BSDataObject DataObjectFromFile(string iFilePath)
        {
            BSDataObject result = null;

            using (FileStream sr = new FileStream(iFilePath, FileMode.Open))
            {
                result = DataObjectSupplier.GetDataObjectForStream(sr);
            }
            return(result);
        }
 public void DataObjectConstructorTest()
 {
     double[] iDataArray = {1, 2, 3, 4, 5};
     string iName = "someStr";
     BSDataObject target = new BSDataObject(iDataArray, iName);
     Assert.AreEqual<string>(iName, target.ObjName);
     CollectionAssert.AreEqual(iDataArray, target.DataArray);
     iDataArray[2] = 2.2;
     CollectionAssert.AreEqual(iDataArray, target.DataArray);
 }
Exemple #15
0
        public void DataObjectConstructorTest()
        {
            double[]     iDataArray = { 1, 2, 3, 4, 5 };
            string       iName      = "someStr";
            BSDataObject target     = new BSDataObject(iDataArray, iName);

            Assert.AreEqual <string>(iName, target.ObjName);
            CollectionAssert.AreEqual(iDataArray, target.DataArray);
            iDataArray[2] = 2.2;
            CollectionAssert.AreEqual(iDataArray, target.DataArray);
        }
            public bool Equals(BSDataObject iObj)
            {
                const double delta = 0.0000000001;

                if (iObj.DataArray.Length != this.DataArray.Length)
                    return false;

                for (int i = 0; i < this.DataArray.Length; i++)
                    if (Math.Abs(this.DataArray[i] - iObj.DataArray[i]) > delta)
                        return false;
                return this.ObjName.Equals(iObj.ObjName);
            }
Exemple #17
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Visualize
        ///</summary>
        //[TestMethod()]
        public void VisualizeTest()
        {
            SimpleVisualizationHandler target = new SimpleVisualizationHandler();

            double[]     iArray = { 1.2, 3.4, 5.6, 2.2, 7.7 };
            BSDataObject obj    = new BSDataObject(iArray, "asdasd");

            target.AddToGraph(obj);
            iArray = new double[] { 2.6, 7.8, 12.2, 10, 2.2, 3.3, 4.4, 5.5, 3.3, -5, 30 };
            target.AddToGraph(new BSDataObject(iArray, "anotherGraph"));
            target._TestShow();
        }
        public BSDataObject Process(BSDataObject iObj)
        {
            double[] iArray = iObj.DataArray;
            int      count  = iArray.Count();

            double[] result = new double[count - 1];
            for (int i = 0; i < count - 1; i++)
            {
                result[i] = iArray[i + 1] - iArray[i];
            }
            return(new BSDataObject(result, iObj.ObjName + "_Derivate"));
        }
        public void ProcessUnordinaryWindowSizesTest()
        {
            MeanPreprocessor target = new MeanPreprocessor(6);

            double[]     iArray   = { 1, 2, 3, 4, 5, 6 };
            double[]     expected = { 3.5 };
            BSDataObject actual   = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
            target = new MeanPreprocessor(5);
            iArray = new double[] { 1, 2, 3, 4 };
            actual = target.Process(new BSDataObject(iArray));
            Assert.AreEqual <int>(0, actual.DataArray.Length);
        }
 public static void SaveDataObjectToFile(BSDataObject iObj, string iFilePath)
 {
     if (null == iObj || null == iFilePath)
     {
         throw new ArgumentNullException();
     }
     using (StreamWriter sw = new StreamWriter(iFilePath))
     {
         foreach (double number in iObj.DataArray)
         {
             sw.WriteLine(number);
         }
     }
 }
            /// <summary>
            /// If iObj was already added to the visualization list, function will do nothing.
            /// If number of already added objects exceed the number of allowed colors (14), than the next
            /// object will receive already assigned color (so, avoid this type of situations).
            /// </summary>
            /// <param name="iObj">See IVisualizer interface doc string</param>
            public void AddToGraph(BSDataObject iObj)
            {
                if (dataObjectsAndColors.ContainsKey(iObj))
                    return;
                while ((ConsoleColor)colorIndex == ConsoleColor.White || (ConsoleColor)colorIndex == ConsoleColor.DarkYellow) //White and DarkYellow colors are invisible on white graph
                    colorIndex++;
                ConsoleColor value = (ConsoleColor)colorIndex;

                if (colorIndex == (int)ConsoleColor.Yellow) //We exceeded maximum number of console colors
                    colorIndex = (int)ConsoleColor.Black; //So, start from begining
                else
                    colorIndex++;
                AddToDictAndGraphForm(iObj, value);
            }
        public void ProcessTest()
        {
            MeanPreprocessor target = new MeanPreprocessor(5);

            double[]     iArray   = { 1, 2, 3, 10, 5, 7, 4, 2, 12, 3, 10 };
            double[]     expected = { 4.2, 5.4, 5.8, 5.6, 6, 5.6, 6.2 };
            BSDataObject actual   = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
            target   = new MeanPreprocessor(4);
            iArray   = new double[] { 1, 2, 3, 4, 5 };
            expected = new double[] { 2.5, 3.5 };
            actual   = target.Process(new BSDataObject(iArray));
            CheckTwoOrderedArrays(actual.DataArray, expected);
        }
        private static BSDataObject ProcessWithoutArraySizeShrinkage(BSDataObject iObj, WindowBasedPreprocessor iProcessor)
        {
            double[]      iArray = iObj.DataArray;
            double[]      result = iProcessor.Process(iObj).DataArray;
            List <double> aList  = new List <double>();

            FieldInfo[] info = aList.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); //this stuff is here to make list from array without copying its elements
            info[0].SetValue(aList, result);                                                              //_items private field
            info[1].SetValue(aList, result.Count());                                                      //_size private field
            aList.Capacity = result.Count() + iProcessor.WindowSize - 1;
            aList.InsertRange(0, iArray.Take((iProcessor.WindowSize - 1) / 2));
            aList.AddRange(iArray.Skip(iArray.Count() - (iProcessor.WindowSize - 1) / 2));
            result = (double[])info[0].GetValue(aList);
            return(new BSDataObject(result, iObj.ObjName));
        }
Exemple #24
0
        public void ProcessTest()
        {
            WeightedAveragePreprocessor target = new WeightedAveragePreprocessor(4, new double[] { 1, 2, 3, 4 });

            double[]     iArray   = { 1, 3, 8, 5, 2, 2, 9, 4, 6 };
            double[]     expected = { 5.1, 4.2, 3.2, 5.1, 4.9, 5.6 };
            BSDataObject actual   = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
            target   = new WeightedAveragePreprocessor();
            iArray   = new double[] { 1, 2, 3, 4, 5, 6, 7 };
            expected = new double[] { 3, 4, 5 };
            actual   = target.Process(new BSDataObject(iArray));
            CheckTwoOrderedArrays(actual.DataArray, expected);
        }
Exemple #25
0
        public void ProcessTest()
        {
            MedianPreprocessor target = new MedianPreprocessor(5);

            double[]     iArray   = { 1, 2, 3, 10, 5, 7, 4, 2, 12, 3, 10 };
            double[]     expected = { 3, 5, 5, 5, 5, 4, 4 };
            BSDataObject actual   = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
            target   = new MedianPreprocessor(3);
            iArray   = new double[] { 1, 2, 3, 4, 5 };
            expected = new double[] { 2, 3, 4 };
            actual   = target.Process(new BSDataObject(iArray));
            CheckTwoOrderedArrays(actual.DataArray, expected);
        }
Exemple #26
0
        public void RegressionTests()
        {
            //Test case #1
            Tukey53HPreprocessor target = new Tukey53HPreprocessor(0.2);

            double[]     iArray = { 538608907.0, 1614349569.0,   46527135.0,  271407561.0, 1224014298.0,  109214588.0, 1401750087.0, 1772709690.0, 1987789339.0,
                                    774552815.0,     1226448635.0, 1418861964.0, 2008047439.0, 1719056947.0, 2091971495.0, 2013473641.0, 1857880694.0,
                                    986804018.0,      623007992.0, 1047211489.0 };
            double[]     expected = { 538608907.0,  292568021.0,  942878565.0,  271407561.0,  190311074.5, 1312882192.5, 1401750087.0, 1694769713.0,
                                      1273631252.5,    1607118987.0, 1096707389.5, 1418861964.0, 1568959455.5, 2050009467.0, 2091971495.0, 2013473641.0,
                                      1500138829.5,    1240444343.0, 1017007753.5, 1047211489.0 };
            BSDataObject actual = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
        }
        protected BSDataObject ProcessUsingWindowingFunc(BSDataObject iObj)
        {
            double[] iArray = iObj.DataArray;
            int      count  = iArray.Count();

            if (count < WindowSize)
            {
                return(new BSDataObject(new double[0], iObj.ObjName + "_" + this.GetType().Name));
            }
            double[] result = new double[count - WindowSize + 1];
            for (int i = 0; i < count - WindowSize + 1; i++)
            {
                result[i] = windowProcessingFunc(iArray.Skip(i).Take(WindowSize));
            }
            return(new BSDataObject(result, (WindowSize - 1) / 2, iObj.ObjName + "_" + this.GetType().Name.Replace("Preprocessor", "")));
        }
 public void ProcessTest()
 {
     Random rnd = new Random();
     FourierPreprocessor target = new FourierPreprocessor();
     const int n = 360, amplitude = 80;
     int freq = freq = rnd.Next(1, 5);
     double[] array = new double[n];
     for (int i = 0; i < n; i++)
     {
         array[i] = amplitude * Math.Sin((double)freq * i * Math.PI / 180.0);
     }
     BSDataObject iObject = new BSDataObject(array, "SourceData");
     BSDataObject expected = new BSDataObject((from i in Enumerable.Range(0, n) select (i == freq || i ==  n - freq) ? amplitude / 2.0 : 0).ToArray());
     BSDataObject actual = target.Process(iObject);
     BSDataObject inversed = new InverseFourierPreprocessor().Process(expected);
     CheckTwoOrderedArrays(expected.DataArray, actual.DataArray, delta: 0.000001);
 }
            public bool Equals(BSDataObject iObj)
            {
                const double delta = 0.0000000001;

                if (iObj.DataArray.Length != this.DataArray.Length)
                {
                    return(false);
                }

                for (int i = 0; i < this.DataArray.Length; i++)
                {
                    if (Math.Abs(this.DataArray[i] - iObj.DataArray[i]) > delta)
                    {
                        return(false);
                    }
                }
                return(this.ObjName.Equals(iObj.ObjName));
            }
        public void ProcessTest()
        {
            Random rnd = new Random();
            FourierPreprocessor target = new FourierPreprocessor();
            const int           n = 360, amplitude = 80;
            int freq = freq = rnd.Next(1, 5);

            double[] array = new double[n];
            for (int i = 0; i < n; i++)
            {
                array[i] = amplitude * Math.Sin((double)freq * i * Math.PI / 180.0);
            }
            BSDataObject iObject  = new BSDataObject(array, "SourceData");
            BSDataObject expected = new BSDataObject((from i in Enumerable.Range(0, n) select(i == freq || i == n - freq) ? amplitude / 2.0 : 0).ToArray());
            BSDataObject actual   = target.Process(iObject);
            BSDataObject inversed = new InverseFourierPreprocessor().Process(expected);

            CheckTwoOrderedArrays(expected.DataArray, actual.DataArray, delta: 0.000001);
        }
Exemple #31
0
        public void GetBSDataObjectForStreamTest()
        {
            const int numberOfPoints = 360;

            double[] expectedArray = new double[numberOfPoints];
            string   fileName      = Path.GetTempFileName();
            Random   rnd           = new Random();

            //Create a file with random values in it
            using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                var streamWriter = new StreamWriter(fileStream);
                for (int i = 0; i < numberOfPoints; i++)
                {
                    expectedArray[i] = rnd.Next();
                    streamWriter.WriteLine(expectedArray[i]);
                }
                streamWriter.Flush();
            }
            //Get BSDataObject from this file
            using (var fileStream = new FileStream(fileName, FileMode.Open))
            {
                BSDataObject target = DataObjectSupplier.GetDataObjectForStream(fileStream);
                CollectionAssert.AreEqual(expectedArray, target.DataArray);
            }
            File.Delete(fileName);

            //The same for memory stream
            using (var memoryStream = new MemoryStream(numberOfPoints * (sizeof(double) + sizeof(char))))
            {
                StreamWriter streamWriter = new StreamWriter(memoryStream);
                for (int i = 0; i < numberOfPoints; i++)
                {
                    expectedArray[i] = Math.Sin((double)i * Math.PI / 180.0);
                    streamWriter.WriteLine(expectedArray[i]);
                }
                streamWriter.Flush();
                memoryStream.Position = 0;
                BSDataObject target = DataObjectSupplier.GetDataObjectForStream(memoryStream);
                CheckTwoOrderedArrays(expectedArray, target.DataArray);
            }
        }
        public void General()
        {
            string    fileName      = Path.GetTempFileName();
            Random    rnd           = new Random();
            const int numbersAmount = 100;

            double[] expected = new double[numbersAmount];
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                for (int i = 0; i < expected.Count(); ++i)
                {
                    expected[i] = rnd.Next(int.MinValue, int.MaxValue);
                    sw.WriteLine(expected[i]);
                }
            }
            BSDataObject result = BSRepository.DataObjectFromFile(fileName);

            File.Delete(fileName);
            CollectionAssert.AreEqual(expected, result.DataArray);
        }
            /// <summary>
            /// If iObj was already added to the visualization list, function will do nothing.
            /// If number of already added objects exceed the number of allowed colors (14), than the next
            /// object will receive already assigned color (so, avoid this type of situations).
            /// </summary>
            /// <param name="iObj">See IVisualizer interface doc string</param>
            public void AddToGraph(BSDataObject iObj)
            {
                if (dataObjectsAndColors.ContainsKey(iObj))
                {
                    return;
                }
                while ((ConsoleColor)colorIndex == ConsoleColor.White || (ConsoleColor)colorIndex == ConsoleColor.DarkYellow) //White and DarkYellow colors are invisible on white graph
                {
                    colorIndex++;
                }
                ConsoleColor value = (ConsoleColor)colorIndex;

                if (colorIndex == (int)ConsoleColor.Yellow) //We exceeded maximum number of console colors
                {
                    colorIndex = (int)ConsoleColor.Black;   //So, start from begining
                }
                else
                {
                    colorIndex++;
                }
                AddToDictAndGraphForm(iObj, value);
            }
Exemple #34
0
        public void ProcessTest()
        {
            //Test case #1
            Tukey53HPreprocessor target = new Tukey53HPreprocessor();

            double[]     iArray   = { 1, 2, 3, 4, 5, 6, 7, 8 };
            double[]     expected = { 1, 2, 3, 4, 5, 6, 7, 8 };
            BSDataObject actual   = target.Process(new BSDataObject(iArray));

            CheckTwoOrderedArrays(actual.DataArray, expected);
            //Test case #2
            target   = new Tukey53HPreprocessor(1.5);
            iArray   = new double[] { 1, 2, 3, 10, 7, 1, 12, 4, 3, 2, 1 };
            expected = new double[] { 1, 2, 3, 5, 7, 1, 2.5, 4, 3, 2, 1 };
            actual   = target.Process(new BSDataObject(iArray));
            CheckTwoOrderedArrays(actual.DataArray, expected);
            //Test case #3
            target   = new Tukey53HPreprocessor(100);
            iArray   = new double[] { 1, 100, 2, 3, 100, 200, 5, 6, 200, 7, 8 };
            expected = iArray;
            actual   = target.Process(new BSDataObject(iArray));
            CheckTwoOrderedArrays(actual.DataArray, expected);
        }
        public BSDataObject Process(BSDataObject iObj)
        {
            double[]     iArray = iObj.DataArray;
            int          count  = iArray.Count();
            double       mean   = iArray.Sum() / count;
            double       std    = Math.Sqrt(iArray.Sum(value => (value - mean) * (value - mean)) / count);
            BSDataObject seq1   = ProcessWithoutArraySizeShrinkage(iObj, new MedianPreprocessor(5));
            BSDataObject seq2   = ProcessWithoutArraySizeShrinkage(seq1, new MedianPreprocessor(3));
            BSDataObject seq3   = ProcessWithoutArraySizeShrinkage(seq2, new WeightedAveragePreprocessor(3, new double[] { 1, 2, 1 }));

            double[] result = new double[count];
            for (int i = 0; i < iArray.Count(); i++)
            {
                if (Math.Abs(iArray[i] - seq3.DataArray[i]) <= k * std)
                {
                    result[i] = iArray[i];
                }
                else
                {
                    result[i] = (iArray[i - 1] + iArray[i + 1]) / 2;
                }
            }
            return(new BSDataObject(result, iObj.ObjName + "_Tukey"));
        }
 public virtual BSDataObject Process(BSDataObject iObj)
 {
     return ProcessUsingWindowingFunc(iObj);
 }
 private void AddToDictAndGraphForm(BSDataObject iObj, ConsoleColor iColor)
 {
     dataObjectsAndColors.Add(iObj, iColor);
     gForm.AddGraph(iObj.DataArray, iColor, iOffset: iObj.Offset, iName: iObj.ObjName);
 }
 public void ThrowsArgumentNullExceptionForNullArgument()
 {
     BSDataObject doesntMatter = BSRepository.DataObjectFromFile(null);
 }
 public BSDataObject Process(BSDataObject iObj)
 {
     double[] iArray = iObj.DataArray;
     int count = iArray.Count();
     double mean = iArray.Sum() / count;
     double std = Math.Sqrt(iArray.Sum(value => (value - mean) * (value - mean)) / count);
     BSDataObject seq1 = ProcessWithoutArraySizeShrinkage(iObj, new MedianPreprocessor(5));
     BSDataObject seq2 = ProcessWithoutArraySizeShrinkage(seq1, new MedianPreprocessor(3));
     BSDataObject seq3 = ProcessWithoutArraySizeShrinkage(seq2, new WeightedAveragePreprocessor(3, new double[] { 1, 2, 1 }));
     double[] result = new double[count];
     for (int i = 0; i < iArray.Count(); i++)
     {
         if (Math.Abs(iArray[i] - seq3.DataArray[i]) <= k * std)
             result[i] = iArray[i];
         else
             result[i] = (iArray[i-1] + iArray[i+1]) / 2;
     }
     return new BSDataObject(result, iObj.ObjName + "_Tukey");
 }
 /// <summary>
 /// Processes data using inverse DFT.
 /// </summary>
 /// <param name="iObject">A BSDataObject to process.</param>
 /// <returns>New BSDataObject, where each value in the DataArray is function value for the given time.</returns>
 public override BSDataObject Process(BSDataObject iObject)
 {
     return ProcessUsingFourierDirection(iObject, FourierTransform.Direction.Backward);
 }
 private static BSDataObject ProcessWithoutArraySizeShrinkage(BSDataObject iObj, WindowBasedPreprocessor iProcessor)
 {
     double[] iArray = iObj.DataArray;
     double[] result = iProcessor.Process(iObj).DataArray;
     List<double> aList = new List<double>();
     FieldInfo[] info = aList.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); //this stuff is here to make list from array without copying its elements
     info[0].SetValue(aList, result); //_items private field
     info[1].SetValue(aList, result.Count()); //_size private field
     aList.Capacity = result.Count() + iProcessor.WindowSize - 1;
     aList.InsertRange(0, iArray.Take((iProcessor.WindowSize - 1) / 2));
     aList.AddRange(iArray.Skip(iArray.Count() - (iProcessor.WindowSize - 1) / 2));
     result = (double[])info[0].GetValue(aList);
     return new BSDataObject(result, iObj.ObjName);
 }
 public virtual BSDataObject Process(BSDataObject iObj)
 {
     return(ProcessUsingWindowingFunc(iObj));
 }
 public abstract BSDataObject Process(BSDataObject iObject);
 public abstract BSDataObject Process(BSDataObject iObject);
 /// <summary>
 /// Processes data using inverse DFT.
 /// </summary>
 /// <param name="iObject">A BSDataObject to process.</param>
 /// <returns>New BSDataObject, where each value in the DataArray is function value for the given time.</returns>
 public override BSDataObject Process(BSDataObject iObject)
 {
     return(ProcessUsingFourierDirection(iObject, FourierTransform.Direction.Backward));
 }
 protected BSDataObject ProcessUsingWindowingFunc(BSDataObject iObj)
 {
     double[] iArray = iObj.DataArray;
     int count = iArray.Count();
     if (count < WindowSize)
         return new BSDataObject(new double[0], iObj.ObjName + "_" + this.GetType().Name);
     double[] result = new double[count - WindowSize + 1];
     for (int i = 0; i < count - WindowSize + 1; i++)
     {
         result[i] = windowProcessingFunc(iArray.Skip(i).Take(WindowSize));
     }
     return new BSDataObject(result, (WindowSize - 1) / 2, iObj.ObjName + "_" + this.GetType().Name.Replace("Preprocessor", ""));
 }
 private void AddToDictAndGraphForm(BSDataObject iObj, ConsoleColor iColor)
 {
     dataObjectsAndColors.Add(iObj, iColor);
     gForm.AddGraph(iObj.DataArray, iColor, iOffset: iObj.Offset, iName: iObj.ObjName);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="iObj">See IVisualizer interface doc string</param>
 /// <param name="iColor">A Console color for the future graph</param>
 public void AddToGraph(BSDataObject iObj, ConsoleColor iColor)
 {
     if (dataObjectsAndColors.ContainsKey(iObj))
         return;
     AddToDictAndGraphForm(iObj, iColor);
 }