public void OnDataChanged()
        {
            if (DrawCost && DrawDistance)
            {
                throw new Exception("Only one of the values can be drawn at once, 'cost' or 'distance'.");
            }

            double[][] matrixValues = null;
            if (DrawCost)
            {
                matrixValues = Dtw.GetCostMatrix();
            }
            if (DrawDistance)
            {
                matrixValues = Dtw.GetDistanceMatrix();
            }

            var dtwPath        = Dtw.GetPath();
            var xLength        = Dtw.XLength;
            var yLength        = Dtw.YLength;
            var cost           = Dtw.GetCost();
            var costNormalized = Dtw.GetCost() / Math.Sqrt(xLength * xLength + yLength * yLength);

            var plotModel = new PlotModel(String.Format("Dtw norm by length: {0:0.00}, total: {1:0.00}", costNormalized, cost))
            {
                LegendTextColor = DrawCost || DrawDistance ? OxyColors.White : OxyColors.Black,
            };

            if (matrixValues != null)
            {
                var maxMatrixValue = 0.0;
                for (int i = 0; i < xLength; i++)
                {
                    for (int j = 0; j < yLength; j++)
                    {
                        maxMatrixValue = Math.Max(maxMatrixValue, Double.IsPositiveInfinity(matrixValues[i][j]) ? 0 : matrixValues[i][j]);
                    }
                }

                for (int i = 0; i < xLength; i++)
                {
                    for (int j = 0; j < yLength; j++)
                    {
                        var value = matrixValues[i][j];
                        var isValuePositiveInfinity = Double.IsPositiveInfinity(value);

                        var intensityBytes = isValuePositiveInfinity ? new byte[] { 0, 0, 0 } : GetFauxColourRgbIntensity(value, 0, maxMatrixValue);
                        //var intensityByte = (byte)(255 - Math.Floor(255 * intensity));
                        plotModel.Annotations.Add(new PolygonAnnotation
                        {
                            Points =
                                new[]
                            {
                                new DataPoint(i - 0.5, j - 0.5), new DataPoint(i + 0.5, j - 0.5),
                                new DataPoint(i + 0.5, j + 0.5), new DataPoint(i - 0.5, j + 0.5),
                            },
                            StrokeThickness = 0,
                            Selectable      = false,
                            Layer           = AnnotationLayer.BelowAxes,
                            Fill            = OxyColor.FromArgb(255, intensityBytes[0], intensityBytes[1], intensityBytes[2]),
                        });
                    }
                }

                for (int i = 0; i < 30; i++)
                {
                    var intensityBytes = GetFauxColourRgbIntensity(i, 0, 29);

                    plotModel.Annotations.Add(new RectangleAnnotation
                    {
                        MinimumX   = -39,
                        MaximumX   = -25,
                        MinimumY   = -i - 6,
                        MaximumY   = -i - 5,
                        Selectable = false,
                        Fill       = OxyColor.FromArgb(255, intensityBytes[0], intensityBytes[1], intensityBytes[2])
                    });
                }

                plotModel.Annotations.Add(new TextAnnotation
                {
                    Position            = new DataPoint(-24, -5),
                    HorizontalAlignment = HorizontalTextAlign.Left,
                    VerticalAlignment   = VerticalTextAlign.Middle,
                    StrokeThickness     = 0,
                    Text = "0"
                });

                plotModel.Annotations.Add(new TextAnnotation
                {
                    Position            = new DataPoint(-24, -34),
                    HorizontalAlignment = HorizontalTextAlign.Left,
                    VerticalAlignment   = VerticalTextAlign.Middle,
                    StrokeThickness     = 0,
                    Text = String.Format("{0:0.00}", maxMatrixValue),
                });
            }

            var matrixPathSeries = new LineSeries("Path")
            {
                StrokeThickness = 1,
                Color           = OxyColors.Red,
            };

            for (int i = 0; i < dtwPath.Length; i++)
            {
                matrixPathSeries.Points.Add(new DataPoint(dtwPath[i].Item1, dtwPath[i].Item2));
            }

            plotModel.Series.Add(matrixPathSeries);

            var seriesMatrixScale = (xLength + yLength) * 0.05;

            for (int variableIndex = 0; variableIndex < Dtw.SeriesVariables.Length; variableIndex++)
            {
                var variableA       = Dtw.SeriesVariables[variableIndex];
                var variableASeries = variableA.OriginalXSeries;
                var variableB       = Dtw.SeriesVariables[variableIndex];
                var variableBSeries = variableB.OriginalYSeries;

                var minSeriesA        = variableASeries.Min();
                var maxSeriesA        = variableASeries.Max();
                var normalizedSeriesA = variableASeries.Select(x => (x - minSeriesA) / (maxSeriesA - minSeriesA)).ToList();
                var matrixSeriesA     = new LineSeries(variableA.VariableName);

                for (int i = 0; i < normalizedSeriesA.Count; i++)
                {
                    matrixSeriesA.Points.Add(new DataPoint(i, (-1 + normalizedSeriesA[i]) * seriesMatrixScale - 1 - seriesMatrixScale * (variableIndex + 1)));
                }

                plotModel.Series.Add(matrixSeriesA);

                var minSeriesB        = variableBSeries.Min();
                var maxSeriesB        = variableBSeries.Max();
                var normalizedSeriesB = variableBSeries.Select(x => (x - minSeriesB) / (maxSeriesB - minSeriesB)).ToList();
                var matrixSeriesB     = new LineSeries(variableB.VariableName);

                for (int i = 0; i < normalizedSeriesB.Count; i++)
                {
                    matrixSeriesB.Points.Add(new DataPoint(-normalizedSeriesB[i] * seriesMatrixScale - 1 - seriesMatrixScale * (variableIndex + 1), i));
                }

                plotModel.Series.Add(matrixSeriesB);
            }

            plotModel.Axes.Add(new LinearAxis(AxisPosition.Bottom, "           Series A")
            {
                Maximum = Math.Max(xLength, yLength), PositionAtZeroCrossing = true
            });
            plotModel.Axes.Add(new LinearAxis(AxisPosition.Left, "                  Series B")
            {
                Maximum = Math.Max(xLength, yLength), PositionAtZeroCrossing = true
            });

            MatrixPlot.Model = plotModel;
        }
Exemple #2
0
        /// <summary>
        /// Generate average of supplied series.
        /// </summary>
        public static double[] Average(List <double[]> series, int maxIterations = 100)
        {
            if (series == null || series.Count == 0)
            {
                throw new Exception("series null or empty");
            }
            if (series.Count == 1)
            {
                return(series[0]);
            }
            if (series.Select(x => x.Length).Distinct().Count() != 1)
            {
                throw new Exception("Series must be of equal length");
            }

            int length = series[0].Length;

            //initializing the average to a simple mean of every point, without DTW
            //double[] average = new double[length];
            //for (int i = 0; i < series[0].Length; i++)
            //{
            //    average[i] = series.Average(x => x[i]);
            //}

            //initialize to a random series from the input
            //var r = new Random();
            //double[] average = series[r.Next(0, series.Count - 1)];

            //initialize to series closest to median min/max after detrending
            List <double[]> tempSeries     = series.Select(Detrend).ToList();
            List <int>      maxIndexes     = tempSeries.Select(x => x.IndexOfMax()).ToList();
            List <int>      minIndexes     = tempSeries.Select(x => x.IndexOfMin()).ToList();
            double          medianMaxIndex = maxIndexes.Median();
            double          medianMinIndex = minIndexes.Median();
            var             distances      = maxIndexes.Select((x, i) => Math.Pow(x - medianMaxIndex, 2) + Math.Pow(minIndexes[i] - medianMinIndex, 2)).ToList();
            int             selectedSeries = distances.IndexOfMin();

            double[] average = series[selectedSeries];

            //this list will hold the values of each aligned point,
            //later used to construct the aligned average
            List <double>[] points = new List <double> [length];
            for (int i = 0; i < length; i++)
            {
                points[i] = new List <double>();
            }

            double prevTotalDist = -1;
            double totalDist     = -2;

            //sometimes the process gets "stuck" in a loop between two different states
            //so we have to set a hard limit to end the loop
            int count = 0;

            //get the path between each series and the average
            while (totalDist != prevTotalDist && count < maxIterations)
            {
                prevTotalDist = totalDist;

                //clear the points from the last calculation
                foreach (var list in points)
                {
                    list.Clear();
                }

                //here we do the alignment for every series
                foreach (double[] ts in series)
                {
                    var dtw = new Dtw(new[] { new SeriesVariable(ts, average) });
                    Tuple <int, int>[] path = dtw.GetPath();

                    //use the path to distribute the points according to the warping
                    Array.ForEach(path, x => points[x.Item2].Add(ts[x.Item1]));
                }

                //Then simply construct the new average series by taking the mean of every List in points.
                average = points.Select(x => x.Average()).ToArray();

                //calculate Euclidean distance to stop the loop if no further improvement can be made
                double[] average1 = average;
                totalDist = series.Sum(x => x.Select((y, i) => Math.Pow(y - average1[i], 2)).Sum()); //we get convergence even though there's still work to be done
                count++;
            }

            return(average);
        }
Exemple #3
0
        static void DTWCsv(string[] files)
        {
            for (int fileId = 0; fileId < files.Length; fileId++)
            {
                if (File.Exists(files[fileId].Split('\\').Last() + "_pearsonDataCsv.txt"))
                {
                    File.Copy(files[fileId].Split('\\').Last() + "_pearsonDataCsv.txt", files[fileId] + "_pearsonDataCsv.txt", true);
                    //Console.WriteLine("Copied " + files[fileId].Split('\\').Last() + "_pearsonDataCsv.txt" + " to " + files[fileId] + "_pearsonDataCsv.txt");

                    if (File.Exists(files[fileId].Split('\\').Last() + "dtwCostInfo.txt"))
                    {
                        File.Copy(files[fileId].Split('\\').Last() + "_pearsonDataCsv.txt", files[fileId] + "dtwCostInfo.txt", true);
                    }
                    Console.WriteLine(files[fileId].Split('\\').Last() + " is already done, skipping.");
                    continue;
                }
                var freed = GC.GetTotalMemory(false);
                GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
                Console.WriteLine("Garbage Collection completed - memory:" + ((double)GC.GetTotalMemory(false) / 1024 / 1024 / 1024).ToString("0.0") + " GB (freed " + (freed / 1024 / 1024) + " MB)");
                Console.WriteLine("Performing DTW on csv data " + fileId + " of " + files.Length + "..");
                var      watch = System.Diagnostics.Stopwatch.StartNew();
                string[] data  = File.ReadAllLines(files[fileId]);
                Console.WriteLine("Data points: " + data.Length);

                List <double> testDataPoints   = new List <double>();
                List <double> recallDataPoints = new List <double>();
                foreach (var line in data)
                {
                    var    split  = line.Replace(',', '.').Split(';');
                    double test   = double.Parse(split[0], System.Globalization.CultureInfo.InvariantCulture);
                    double recall = double.Parse(split[1], System.Globalization.CultureInfo.InvariantCulture);

                    testDataPoints.Add(test);
                    recallDataPoints.Add(recall);
                }

                //                Dtw dtw = new Dtw(testDataPoints.ToArray(), recallDataPoints.ToArray(), DistanceMeasure.Euclidean, true, true, null, null, 700);
                Dtw dtw = new Dtw(testDataPoints.ToArray(), recallDataPoints.ToArray(), DistanceMeasure.Euclidean, true, true, slopeStepSizeDiagonal: 2, slopeStepSizeAside: 1);

                var path = dtw.GetPath();
                var cost = dtw.GetCost();
                //var distanceMatrix = dtw.GetDistanceMatrix();
                //var costMatrix = dtw.GetCostMatrix();

                File.WriteAllText(files[fileId].Split('\\').Last() + "dtwCostInfo.txt",
                                  "cost=" + cost +
                                  "\nbefore_length=" + data.Length +
                                  "\nbefore_cost=" + (cost / data.Length) +
                                  "\nafter_length=" + path.Length +
                                  "\nafter_cost=" + (cost / path.Length)
                                  );

                //PngExporter pngify = new PngExporter();
                //pngify.Width = 36000;
                //pngify.Height = 4000;

                //var model = new PlotModel() { Title = "Red = test, blue = recall" };

                //var aSeries = new OxyPlot.Series.LineSeries() { Color = OxyColors.Blue, MarkerSize = 10 };
                //var bSeries = new OxyPlot.Series.LineSeries() { Color = OxyColors.Red, MarkerSize = 10 };

                //for (int i = 0; i < testDataPoints.Count; i++)
                //{
                //    aSeries.Points.Add(new DataPoint(i, testDataPoints[i]));
                //}

                //for (int i = 0; i < recallDataPoints.Count; i++)
                //{
                //    bSeries.Points.Add(new DataPoint(i, recallDataPoints[i]));
                //}

                //List<string> pearsonData = new List<string>();
                //foreach (var pairing in path)
                //{
                //    var lineSeries = new OxyPlot.Series.LineSeries() { Color = OxyColors.Gray, MarkerSize = 0.05 };

                //    lineSeries.Points.Add(new DataPoint(pairing.Item1, testDataPoints[pairing.Item1]));
                //    lineSeries.Points.Add(new DataPoint(pairing.Item2, recallDataPoints[pairing.Item2]));

                //    model.Series.Add(lineSeries);

                //    pearsonData.Add(testDataPoints[pairing.Item1].ToString().Replace(',', '.') + ";" + recallDataPoints[pairing.Item2].ToString().Replace(',', '.'));
                //}

                //var pears = MathNet.Numerics.Statistics.Correlation.Pearson(path.Select(x => testDataPoints[x.Item1]).ToList(), path.Select(x => recallDataPoints[x.Item2]).ToList());
                //Console.WriteLine("Pearson for " + files[fileId] + ":");
                //Console.WriteLine(pears.ToString());

                //File.WriteAllLines(files[fileId].Split('\\').Last() + "_pearsonDataCsv.txt", pearsonData);

                //model.Series.Add(aSeries);
                //model.Series.Add(bSeries);

                //pngify.ExportToFile(model, files[fileId].Split('\\').Last() + "_csv.png");
                watch.Stop();
                Console.WriteLine("Done in " + watch.Elapsed);
                Console.WriteLine("");
            }
        }