Beispiel #1
0
        public void Z_Score_Test()
        {
            StatisticCalculator StatCalc = new StatisticCalculator();
            var finalZSCore = StatCalc.ZScore(a, arrayA);

            Assert.AreEqual(0.77, finalZSCore);
        }
Beispiel #2
0
        public void Variance_Test()
        {
            StatisticCalculator StatCalc = new StatisticCalculator();
            var finalVariance            = StatCalc.Variance(arrayA);

            Assert.AreEqual(6.8, finalVariance);
        }
Beispiel #3
0
        public void StandardDev_Test()
        {
            StatisticCalculator StatCalc = new StatisticCalculator();
            var finalStandardDev         = StatCalc.StandardDeviation(arrayA);

            Assert.AreEqual(2.61, finalStandardDev);
        }
Beispiel #4
0
        public void Mean_AverageTest()
        {
            StatisticCalculator StatCalc = new StatisticCalculator();
            var finalMean = StatCalc.Mean_Average(arrayX);

            Assert.AreEqual(6.57, finalMean);
        }
Beispiel #5
0
 /// <summary>
 /// Returns true or false depending on if a string is detected in a array list of arrayLists. used by GetSelectedData().
 /// </summary>
 /// <param name="arrayListOfArrayLists"></param>
 /// <returns></returns>
 public bool StringDetection(ArrayList[] arrayListOfArrayLists)
 {
     try
     {
         //If a string is detected in a selectedColumn. Set bool stringDetected to true
         for (int columnIndex = 0; columnIndex < arrayListOfArrayLists.Length; columnIndex++)
         {
             try
             {
                 if (StatisticCalculator.ConvertToList(arrayListOfArrayLists[columnIndex])[0] == -1)//-1 == string
                 {
                     stringArrayIndex = columnIndex;
                     return(true);
                 }
             }
             catch (ArgumentOutOfRangeException)
             {
                 return(false);
             }
         }
     }
     catch (NullReferenceException nRe)
     {
         throw nRe;
     }
     return(false);
 }
Beispiel #6
0
        /// <summary>
        /// Add points to the plotter
        /// </summary>
        /// <param name="points"></param>
        public void AddPoints(ArrayList[] points)
        {
            this.points = points;
            this.Series[serieOne].Points.Clear();
            this.Series[serieTwo].Points.Clear();
            //Should be two arraylist which are both the same size and not empty.
            if (points.Length < 3 && points.Length > 0 && points[0].Count > 0 && points[1].Count > 0 && points[0].Count == points[1].Count)
            {
                //if noth columns are numbers.
                try
                {
                    for (int i = 0; i < points[0].Count; i++)
                    {
                        this.Series[serieOne].Points.AddXY(Convert.ToInt32(points[0][i]), Convert.ToInt32(points[1][i]));
                    }
                }
                //if column is a string.
                catch (Exception)
                {
                    this.Series[serieOne].Points.Clear();
                    for (int i = 0; i < points[0].Count; i++)
                    {
                        this.Series[serieOne].Points.AddXY(points[0][i], points[1][i]);
                    }
                }
            }

            //set gradientline.
            gradient = Math.Round(StatisticCalculator.getGradient(points), 3);
            int max = getMax(points[0]);
            int min = getMin(points[0]);

            this.Series[serieTwo].Points.AddXY(min, (min * gradient));
            this.Series[serieTwo].Points.AddXY(max, (max * gradient));
        }
        public void MeanTest()
        {
            StatisticCalculator statsCal = new StatisticCalculator();

            int[] values = { 1, 2 };
            var   mean   = statsCal.Mean(values);

            Assert.AreEqual(1.5, mean);
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            var statisticCalculator   = new StatisticCalculator(TextFileName);
            IStatisticPrinter printer = new ConsolePrinter();

            Statistic statistic = statisticCalculator.CalculateUsingSingleThread();

            printer.Print(statistic);

            statistic = statisticCalculator.CalculateUsingAllThreads();
            printer.Print(statistic);
        }
Beispiel #9
0
        /// <summary>
        /// Create a staticModel from a given array list. used by GetSelectedData().
        /// </summary>
        /// <param name="arrayList"></param>
        public StatisticModel CreateStatisticModelFromArrayList(ArrayList arrayList)
        {
            //Calculate data
            double average           = Math.Round(StatisticCalculator.Average(arrayList), 2);
            double standardDeviation = Math.Round(StatisticCalculator.StandardDeviation(arrayList), 2);
            int    lowerWhisker      = StatisticCalculator.SmallestValue(arrayList);
            double lowerBox          = StatisticCalculator.FirstQuartile(arrayList);
            double median            = StatisticCalculator.Median(arrayList);
            double upperBox          = StatisticCalculator.ThirdQuartile(arrayList);
            int    upperWhisker      = StatisticCalculator.BiggestValue(arrayList);

            //Create statisticsModel and return it.
            return(new StatisticModel(average, standardDeviation, lowerWhisker, lowerBox, median, upperBox, upperWhisker));
        }
Beispiel #10
0
        /// <summary>
        /// Returns a Array with arraylists ith the content of the selected columns
        /// </summary>
        /// <param name="dataGrid">The dataGrid with the selected data.</param>
        /// <returns></returns>
        public ArrayList[] getResultSelection(CustomDataGridView dataGrid)
        {
            ArrayList[] lijsten = new ArrayList[2];
            lijsten[0] = new ArrayList();
            lijsten[1] = new ArrayList();

            //Get number of unique columns.
            try
            {
                List <int> columns = new List <int>();
                foreach (DataGridViewTextBoxCell item in dataView.SelectedCells)
                {
                    columns.Add(item.ColumnIndex);
                }
                var        unique_items  = new HashSet <int>(columns);
                List <int> uniekeColumns = new List <int>();
                foreach (int i in unique_items)
                {
                    uniekeColumns.Add(i);
                }

                //if 1 of 2 colomns are selected.
                if (uniekeColumns.Count < 3 && uniekeColumns.Count > 0)
                {
                    int       column1     = uniekeColumns[0];
                    int       column2     = uniekeColumns.Count == 2 ? uniekeColumns[1] : 0;
                    ArrayList listColomn1 = new ArrayList();
                    ArrayList listColomn2 = new ArrayList();

                    //add to column resultList.
                    foreach (DataGridViewTextBoxCell item in dataView.SelectedCells)
                    {
                        if (item.ColumnIndex == column1)
                        {
                            listColomn1.Add(item.Value.ToString());
                        }
                        if (item.ColumnIndex == column2)
                        {
                            listColomn2.Add(item.Value.ToString());
                        }
                    }

                    //if both resultList are the same size or list2 is empty, set the lists to the arrayListOfArrayLists.
                    if (listColomn1.Count == listColomn2.Count || listColomn2.Count == 0)
                    {
                        listColomn1.Reverse();
                        listColomn2.Reverse();
                        lijsten[0] = (listColomn1);
                        lijsten[1] = (listColomn2);

                        foreach (ArrayList a in lijsten)
                        {
                            foreach (String s in a)
                            {
                                Console.WriteLine(s);
                            }
                            Console.WriteLine();
                        }
                        List <int> lijst1 = StatisticCalculator.ConvertToList(lijsten[0]);
                        List <int> lijst2 = new List <int>();
                        if (listColomn2.Count != 0)
                        {
                            lijst2 = StatisticCalculator.ConvertToList(lijsten[1]);
                        }

                        //If the selection is only a string or string + string give a message
                        if (lijst1[0] == -1 && lijst2.Count == 0 || (lijst1[0] == -1 && lijst2[0] == -1))
                        {
                            MessageBox.Show("Selecteer a.u.b. ook getallen.", "Selectie bevat geen getallen.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            mainController.statisticView.ClearData();
                            //mainController.mainViewController.SelectTab(1);
                            lijsten = null;
                            return(lijsten);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Selecteer twee evenlange kolommen a.u.b.", "Kolommen zijn niet even lang.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        mainController.statisticView.ClearData();
                    }
                }
                else
                {
                    MessageBox.Show("Selecteer a.u.b. eerst één of twee kolommen in de resultaten tap.", "Er kan geen scatter plot van uw selectie gemaakt worden.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    mainController.statisticView.ClearData();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Sellecteer alleen kolom met tekst a.u.b.", "U mag geen knopjes selecteren.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                mainController.statisticView.ClearData();
            }

            return(lijsten);
        }
        public async Task <IActionResult> AnalyzeDataAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "analytics/plotly/{networkId}/{interval}")] HttpRequest req,
            string networkId,
            int interval,
            ILogger log)
        {
            string body = await new StreamReader(req.Body).ReadToEndAsync();
            Dictionary <string, int> continueAtIteration;

            try
            {
                continueAtIteration = JsonConvert.DeserializeObject <Dictionary <string, int> >(body);
            }
            catch (Exception)
            {
                return(new BadRequestResult());
            }

            if (continueAtIteration == null)
            {
                continueAtIteration = new Dictionary <string, int>();
            }

            var query = _logContainer
                        .GetItemLinqQueryable <LogEntryStack>(allowSynchronousQueryExecution: true, requestOptions: new QueryRequestOptions {
                PartitionKey = new PartitionKey(networkId)
            })
                        .Where(o => o.Timestamp >= DateTime.UtcNow.AddSeconds(-interval));

            var iterator = query.ToFeedIterator();

            var documents = new List <LogEntryStack>();

            while (iterator.HasMoreResults)
            {
                documents.AddRange((await iterator.ReadNextAsync()).ToList());
            }

            if (documents.Count <= 0)
            {
                return(new OkObjectResult(new { data = new List <PlotyTrace>(), lastIteration = continueAtIteration, statistics = new List <PlotyTrace>() }));
            }

            var logEntries = documents
                             .SelectMany(o => o.LogEntries)
                             .Where(o => o.Iteration > (continueAtIteration.ContainsKey(o.NodeId) ? continueAtIteration[o.NodeId] : -1))
                             .ToList();

            var traces = logEntries
                         .OrderBy(o => o.Iteration)
                         .GroupBy(o => o.NodeId)
                         .Select(o => new PlotyTrace {
                name = o.Key, x = o.ToList().Select(k => (double)k.Iteration).ToList(), y = o.ToList().Select(s => s.State).ToList(), line = new PlotTraceLine {
                    width = 1
                }, type = "scatter"
            })
                         .ToList();

            var maxIterations = continueAtIteration;

            traces
            .ForEach(t => maxIterations[t.name] = (int)t.x.Max());

            var statistics = documents
                             .SelectMany(o => o.LogEntries)
                             .Where(o => o.Iteration <= maxIterations.Where(t => t.Key != "average").Min(t => t.Value))
                             .Where(o => o.Iteration > (continueAtIteration.ContainsKey("average") ? continueAtIteration["average"] : -1))
                             .GroupBy(o => o.Iteration)
                             .OrderBy(o => o.Key)
                             .Select(o =>
            {
                var avgRef   = o.ToList().Select(s => s.ReferenceSignal).ToList().Average();
                var avgState = o.ToList().Select(s => s.State).ToList().Average();
                var maxState = o.ToList().Max(d => d.State);
                var minState = o.ToList().Min(d => d.State);
                var convergenceIterations = documents
                                            .SelectMany(c => c.LogEntries)
                                            .Where(c => c.Iteration >= o.Key - 2 && c.Iteration <= o.Key + 1)                                              // range for required states
                                            .GroupBy(c => c.NodeId)
                                            .Where(c => c.ToList().Count == 4)                                                                             // enough items in to calculate
                                            .Where(c => Math.Abs(c.ToList().Max(z => z.ReferenceSignal) - c.ToList().Min(z => z.ReferenceSignal)) < 0.001) // noch change in ref
                                            .Select(c => new
                {
                    NodeID          = c.Key,
                    ConvergenceRate = StatisticCalculator.CalculateConvergenceRate(
                        c.ToList()
                        .OrderBy(x => x.Iteration)
                        .Select(x => x.State)
                        .ToArray())
                })
                                            .ToList();
                return(new
                {
                    Iteration = o.Key,
                    Average = avgRef,
                    Error = StatisticCalculator.CalculatePercentError(avgState, avgRef),
                    Difference = Math.Abs(maxState) + Math.Abs(minState) > 0 ? StatisticCalculator.CalculatePercentDifferencePeak(o.ToList().Select(r => r.State).ToArray()) : 0,
                    ConvergenceRate = convergenceIterations.Count > 0 ? convergenceIterations.Average(c => c.ConvergenceRate) : double.NaN
                });
            })
                             .ToList();

            if (statistics.Count > 0)
            {
                traces.Insert(0, new PlotyTrace
                {
                    name = "average",
                    x    = statistics.Select(o => (double)o.Iteration).ToList(),
                    y    = statistics.Select(o => o.Average).ToList()
                });
            }

            var lastIterations = continueAtIteration;

            traces.ForEach(o => lastIterations[o.name] = (int)o.x.Max());


            var statisticTraces = new List <PlotyTrace>();

            statisticTraces.Add(new PlotyTrace
            {
                name = "%Error",
                x    = statistics.Where(s => s.Error.HasValue).Select(s => (double)s.Iteration).ToList(),
                y    = statistics.Where(s => s.Error.HasValue).Select(s => s.Error.Value).ToList()
            });

            statisticTraces.Add(new PlotyTrace
            {
                name = "%Difference",
                x    = statistics.Select(s => (double)s.Iteration).ToList(),
                y    = statistics.Select(s => s.Difference).ToList()
            });

            statisticTraces.Add(new PlotyTrace
            {
                name = "Convergence",
                x    = statistics.Where(s => !double.IsNaN(s.ConvergenceRate)).Select(s => (double)s.Iteration).ToList(),
                y    = statistics.Where(s => !double.IsNaN(s.ConvergenceRate)).Select(s => s.ConvergenceRate).ToList()
            });


            var result = new
            {
                data           = traces,
                statistics     = statisticTraces,
                lastIterations = lastIterations
            };

            return(new OkObjectResult(result));
        }