private void savePlotsToolStripMenuItem_Click(object sender, EventArgs e) { var sfd = new SaveFileDialog(); sfd.InitialDirectory = @"H: \Users\Jack\Source\Repos\BraitenbergSimulator\BraitenbergSimulator\BraitenbergSimulator"; sfd.Filter = "PNG files (*.png)|*.png|All files (.*)|*.*"; sfd.FilterIndex = 0; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { var pngExporter = new PngExporter { Width = 500, Height = 500, Background = OxyColors.White }; pngExporter.ExportToFile(plotView1.Model, sfd.FileName); } }
private void saveButton_Click(object sender, EventArgs e) { var sfd = new SaveFileDialog(); sfd.InitialDirectory = @"H: \Users\Jack\Source\Repos\BraitenbergSimulator\BraitenbergSimulator\BraitenbergSimulator"; sfd.Filter = "PNG files (*.png)|*.png|All files (.*)|*.*"; sfd.FilterIndex = 0; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { var pngExporter = new PngExporter { Width = 500, Height = 500, Background = OxyColors.White }; pngExporter.ExportToFile(plotView1.Model, sfd.FileName); string subName = Path.Combine(Path.GetDirectoryName(sfd.FileName), Path.GetFileNameWithoutExtension(sfd.FileName) + "pip" + Path.GetExtension(sfd.FileName)); pngExporter.ExportToFile(plotView2.Model, subName); using (var writer = File.CreateText(Path.ChangeExtension(sfd.FileName, "yaml"))) { Serializer s = new Serializer(namingConvention: new CamelCaseNamingConvention()); s.Serialize(writer, stats); } } }
private void btn_CreateResultTable_Click(object sender, EventArgs e) { OxyColor EEGColor = OxyColors.Cyan; OxyColor EDAColor = OxyColors.LightGreen; OxyColor HRColor = OxyColors.Salmon; //var tester = new List<double> //{ // 0.01 //}; //var tester2 = new List<double> //{ // 0.01, 0.01 //}; //var tester3 = new List<double> //{ // 0.01, 0.01, 0.01 //}; //MessageBox.Show(tester.FisherCombineP().ToString("0.00000000") + "\n" + tester2.FisherCombineP().ToString("0.00000000") + "\n" + tester3.FisherCombineP().ToString("0.00000000")); //var res1 = FisherCompare1(0.4, 10, 0.3, 12); //var res2 = FisherCompare2(0.4, 10, 0.3, 12); //MessageBox.Show("1)\n" + res1.Item1 + "\n" + res1.Item2 + "\n" + res1.Item3 + "\n\n2)\n" + res2.Item1 + "\n" + res2.Item2 + "\n" + res2.Item3); string corrType = "Pearson"; //string corrType = "Kendall"; //string corrType = "Spearman"; double minMilliseconds = 10000; FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { //sensor is first string var timeTable = new Dictionary<string, Dictionary<int, List<Tuple<double, double>>>>(); var stimuliTable = new Dictionary<string, Dictionary<string, List<Tuple<double, double>>>>(); var totalList = new Dictionary<string, List<Tuple<double, double>>>(); var big5List = new Dictionary<string, List<Dictionary<Big5, int>>>(); List<string> sensors = new List<string>(); List<int> times = new List<int>(); List<string> stimulis = new List<string>(); List<string> resultFiles = new List<string>(); foreach (var folder in Directory.GetDirectories(fbd.SelectedPath)) { if (folder.Contains("Stimuli high") || folder.Contains("Stimuli low") || folder.Contains("Time 0") || folder.Contains("Time 1") || folder.Contains("Time 2") || folder.Contains(".git") || folder.Split('\\').Last() == "3" || folder.Split('\\').Last() == "6" || folder.Split('\\').Last() == "13") { continue; } string subject = folder.Split('\\').Last(); var metaLines = File.ReadAllLines($"{folder}/meta.txt"); var big5 = GetBig5(metaLines); int time = int.Parse(metaLines[0].Split('=').Last()); string stimuli = metaLines[1].Split('=').Last(); stimuli = stimuli == "neu" ? "low" : "high"; if (!times.Contains(time)) times.Add(time); if (!stimulis.Contains(stimuli)) stimulis.Add(stimuli); List<string> foldersToExamine = new List<string>(); foldersToExamine.Add(fbd.SelectedPath + "\\Time " + time); if (time > 0) { foldersToExamine.Add(fbd.SelectedPath + "\\Stimuli " + stimuli); } if (!big5List.ContainsKey("time" + time)) { big5List.Add("time" + time, new List<Dictionary<Big5, int>>()); } if (!big5List.ContainsKey("stim" + stimuli)) { big5List.Add("stim" + stimuli, new List<Dictionary<Big5, int>>()); } if (!big5List.ContainsKey("total")) { big5List.Add("total", new List<Dictionary<Big5, int>>()); } if (!big5List.ContainsKey("corr")) { big5List.Add("corr", new List<Dictionary<Big5, int>>()); } if (!big5List.ContainsKey("revCorr")) { big5List.Add("revCorr", new List<Dictionary<Big5, int>>()); } big5List["time" + time].Add(big5); if (time != 0) { big5List["stim" + stimuli].Add(big5); } big5List["total"].Add(big5); foreach (var folderToExamine in foldersToExamine) { foreach (var resultFile in Directory.GetFiles(folderToExamine).Where(f => f.Split('\\').Last().StartsWith(subject) && f.Split('\\').Last().EndsWith(".txt"))) { if (resultFiles.Contains(resultFile.Split('\\').Last()) || !folderToExamine.Contains("Time") && !foldersToExamine.Contains("Stimuli") || resultFile.Contains("dtw")) { continue; } resultFiles.Add(resultFile.Split('\\').Last()); string sensor = new String(resultFile.Split('.').First().SkipWhile(x => x != '_').Skip(1).SkipWhile(x => x != '_').Skip(1).ToArray()); if (!sensors.Contains(sensor)) sensors.Add(sensor); var resultLines = File.ReadAllLines(resultFile); string correlationLine = resultLines.First(x => x.Contains("|" + corrType)); int corrId = resultLines.ToList().IndexOf(correlationLine); int sigId = corrId + 2; string significanceLine = resultLines[sigId]; string N = resultLines[sigId + 2]; double highPassThreshold = minMilliseconds / 1000; if (sensor.Contains("EEG")) { highPassThreshold *= 128; } else if (sensor.Contains("GSR")) { highPassThreshold *= 20; } else if (sensor.Contains("HR")) { highPassThreshold *= 1; } string[] Nsplit = N.Split(new char[] { '|', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (correlationLine.Contains(".a") || significanceLine.Contains(".a") || int.Parse(Nsplit[2]) < highPassThreshold) { if (int.Parse(Nsplit[2]) < highPassThreshold) { Log.LogMessage("Removing - " + Nsplit[2] + ": " + resultFile); } continue; } int splitIndex = (corrType == "Pearson") ? 3 : 4; double pearsCorrelation = double.Parse(correlationLine.Split(new char[] { '|', '*' }, StringSplitOptions.RemoveEmptyEntries)[splitIndex].Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture); double pearsSignificance = double.Parse(significanceLine.Split(new char[] { '|', '*' }, StringSplitOptions.RemoveEmptyEntries)[splitIndex].Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture); var result = Tuple.Create(pearsCorrelation, pearsSignificance); if (!timeTable.ContainsKey(sensor)) { timeTable.Add(sensor, new Dictionary<int, List<Tuple<double, double>>>()); stimuliTable.Add(sensor, new Dictionary<string, List<Tuple<double, double>>>()); totalList.Add(sensor, new List<Tuple<double, double>>()); } if (!timeTable[sensor].ContainsKey(time)) { timeTable[sensor].Add(time, new List<Tuple<double, double>>()); } if (!stimuliTable[sensor].ContainsKey(stimuli)) { stimuliTable[sensor].Add(stimuli, new List<Tuple<double, double>>()); } timeTable[sensor][time].Add(result); totalList[sensor].Add(result); if (time != 0) { stimuliTable[sensor][stimuli].Add(result); } if (pearsCorrelation > 0) { big5List["corr"].Add(big5); } else { big5List["revCorr"].Add(big5); } } } } //done gathering results List<string> totalToWrite = new List<string>(); totalToWrite.Add("Sensor&Avg Corr&Avg Sig. \\\\"); foreach (var sensor in sensors) { double avgCorrelation = totalList[sensor].Average(x => x.Item1); double stdevCorrelation = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(totalList[sensor].Select(x => x.Item1).ToArray()); double avgSignificance = totalList[sensor].Average(x => x.Item2); double stdevSignificance = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(totalList[sensor].Select(x => x.Item2).ToArray()); totalToWrite.Add($"{sensor}&{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})&{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")}) \\\\"); } Dictionary<Big5, List<string>> big5Anova = new Dictionary<Big5, List<string>>(); foreach (Big5 item in Enum.GetValues(typeof(Big5))) { big5Anova.Add(item, new List<string>()); totalToWrite.Add(item + " Mean: " + big5List["total"].Average(x => x[item]).ToString("0.00") + ", SD: " + MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(big5List["total"].Select(x => x[item]).ToArray()).ToString("0.00") + "."); big5List["time0"].ForEach(x => big5Anova[item].Add("0;" + x[item])); big5List["time1"].ForEach(x => big5Anova[item].Add("1;" + x[item])); big5List["time2"].ForEach(x => big5Anova[item].Add("2;" + x[item])); big5List["stimlow"].ForEach(x => big5Anova[item].Add("3;" + x[item])); big5List["stimhigh"].ForEach(x => big5Anova[item].Add("4;" + x[item])); } foreach (var big5group in big5Anova) { File.WriteAllLines(fbd.SelectedPath + "/" + big5group.Key + "_anova.csv", big5group.Value); } File.WriteAllLines(fbd.SelectedPath + "/" + corrType + "_totals.txt", totalToWrite); double width = 1 / (sensors.Count * 1.4); double widthTime = 0.3; var timeModel = new PlotModel() { Title = $"Time Groups Box Plot" }; var avgLineSeries = new OxyPlot.Series.LineSeries() { }; List<OxyColor> colors = new List<OxyColor>() { }; int small = sensors.Count / 3; int mid = small * 2; int stepsize = 255 / small; for (int i = 0; i < sensors.Count; i++) { byte increaser = (byte)((i % small) * stepsize); byte decreaser = (byte)(255 - increaser); if (i < small) { colors.Add(OxyColor.FromRgb(increaser, decreaser, decreaser)); } else if (i < mid) { colors.Add(OxyColor.FromRgb(decreaser, increaser, decreaser)); } else { colors.Add(OxyColor.FromRgb(decreaser, decreaser, increaser)); } } List<string> sensorsAdded = new List<string>(); foreach (var sensor in sensors) { List<string> timeAnova = new List<string>(); foreach (var time in times) { timeTable[sensor][time].ForEach(x => timeAnova.Add(time + ";" + x.Item1)); } File.WriteAllLines(fbd.SelectedPath + "/" + sensor + ".csv", timeAnova); } Dictionary<string, int[]> significantAmount = new Dictionary<string, int[]>(); Dictionary<string, int[]> significantAmountMax = new Dictionary<string, int[]>(); var significantCorr = new Dictionary<string, Tuple<double, double, double, double>[]>(); foreach (var sensor in sensors) { significantAmount.Add(sensor, new int[5]); significantAmountMax.Add(sensor, new int[5]); //significantCorr.Add(sensor, new Tuple<double, double>[5]); } significantCorr.Add("EEG", new Tuple<double, double, double, double>[5]); significantCorr.Add("EDA", new Tuple<double, double, double, double>[5]); significantCorr.Add("HR", new Tuple<double, double, double, double>[5]); //significantCorr.Add("AVG", new Tuple<double, double, double, double>[5]); Action<string, int, List<Tuple<double, double>>> AddCorrelation = (sens, id, correl) => { //old average + sd //significantCorr[sens][id] = Tuple.Create(correl.Average(x => x.Item1), correl.Select(x => x.Item1).STDEV(), correl.Average(x => x.Item2), correl.Select(x => x.Item2).STDEV()); //new fisher algorithms significantCorr[sens][id] = Tuple.Create(FisherInverse(correl.Average(x => Fisher(x.Item1))), Math.Round((double)correl.Count), correl.Select(x => x.Item2).FisherCombineP(), Math.Round((double)correl.Count)); }; List<string> amountTimeSignificant = new List<string>(); List<double> timeSignificantPoints = new List<double>(); var TimeErrorModel = new PlotModel() { Title = $"Time Error Model" }; var timeErrorSeries = new OxyPlot.Series.ErrorColumnSeries() { }; TimeErrorModel.Series.Add(timeErrorSeries); TimeErrorModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left }); var axis = new OxyPlot.Axes.CategoryAxis { Position = OxyPlot.Axes.AxisPosition.Bottom }; axis.Labels.Add("Time 0"); axis.Labels.Add("Time 1"); axis.Labels.Add("Time 2"); TimeErrorModel.Axes.Add(axis); TimeErrorModel.Annotations.Add(new OxyPlot.Annotations.LineAnnotation() { Y = 0, Type = OxyPlot.Annotations.LineAnnotationType.Horizontal }); var TimeErrorModel2 = new PlotModel() { Title = $"Time Error Model" }; TimeErrorModel2.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Left }); var axis2 = new OxyPlot.Axes.CategoryAxis { Position = OxyPlot.Axes.AxisPosition.Bottom }; axis2.Labels.Add("Time 0"); axis2.Labels.Add("Time 1"); axis2.Labels.Add("Time 2"); TimeErrorModel2.Axes.Add(axis2); List<string> AnovaIndividual = new List<string>(); List<string> AnovaAvg = new List<string>(); int anovaIndividualId = 0; int anovaAvgId = 0; List<string> AnovaIndividualLegend = new List<string>(); List<string> AnovaAvgLegend = new List<string>(); foreach (var time in times) { int sensorId = 0; List<string> timeToWrite = new List<string>(); timeToWrite.Add("Sensor&Avg Corr&Avg Sig. \\\\"); List<double> avgs = new List<double>(); List<double> sigPoints = new List<double>(); var errorSeries = new OxyPlot.Series.ErrorColumnSeries(); TimeErrorModel2.Series.Add(errorSeries); var EEGAllCorrelations = new List<Tuple<double, double>>(); var GSRAllCorrelations = timeTable["GSR"][time]; var HRAllCorrelations = timeTable["HR"][time]; foreach (var sensor in sensors) { if (sensor.Contains("EEG")) { EEGAllCorrelations.AddRange(timeTable[sensor][time]); } double avgCorrelation = timeTable[sensor][time].Average(x => x.Item1); double stdevCorrelation = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Select(x => x.Item1).ToArray()); double avgSignificance = timeTable[sensor][time].Average(x => x.Item2); double stdevSignificance = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Select(x => x.Item2).ToArray()); var orderedAll = timeTable[sensor][time].OrderBy(x => x.Item1).ToList();//timeTable[sensor][time].Where(x => x.Item2 * 100 < (int)5).OrderBy(x => x.Item1).ToList(); amountTimeSignificant.Add(time + " & " + sensor + " & " + orderedAll.Count); significantAmount[sensor][time] = orderedAll.Count; significantAmountMax[sensor][time] = timeTable[sensor][time].Count; //significantCorr[sensor][time] = Tuple.Create(orderedAll.Average(x => x.Item1), MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(orderedAll.Select(x => x.Item1).ToArray())); //var boxItem = new OxyPlot.Series.BoxPlotItem(time + sensorId * widthTime - (0.5 * widthTime * sensors.Count), orderedAll[0].Item1, orderedAll[(int)(orderedAll.Count * 0.25)].Item1, orderedAll[orderedAll.Count / 2].Item1, orderedAll[(int)(orderedAll.Count * 0.75)].Item1, orderedAll.Last().Item1); //var boxItem = new OxyPlot.Series.BoxPlotItem(sensorId + time * widthTime - (0.5 * widthTime * (times.Count - 1)), orderedAll[0].Item1, orderedAll[(int)(orderedAll.Count * 0.25)].Item1, orderedAll[orderedAll.Count / 2].Item1, orderedAll[(int)(orderedAll.Count * 0.75)].Item1, orderedAll.Last().Item1); //var boxSeries = new OxyPlot.Series.BoxPlotSeries() { }; //boxSeries.BoxWidth = widthTime; //boxSeries.WhiskerWidth = widthTime; //boxSeries.Items.Add(boxItem); //boxSeries.Fill = colors[sensorId]; //timeModel.Series.Add(boxSeries); errorSeries.Items.Add(new OxyPlot.Series.ErrorColumnItem(orderedAll.Average(x => x.Item1), MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(orderedAll.Select(x => x.Item1).ToArray()), time) { Color = colors[sensorId] }); //if (!sensorsAdded.Contains(sensor)) //{ // sensorsAdded.Add(sensor); // boxSeries.Title = sensor; //} avgs.Add(orderedAll.Average(x => x.Item1)); sigPoints.AddRange(orderedAll.Select(x => x.Item1)); if (avgSignificance * 100 < (int)5) { timeToWrite.Add($"\\textbf{{{sensor}}}&\\textbf{{{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})}}&\\textbf{{{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")})}} \\\\"); } else { timeToWrite.Add($"{sensor}&{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})&{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")}) \\\\"); } sensorId++; } double boxWidth = 0.3; //eeg EEGAllCorrelations = EEGAllCorrelations.OrderBy(x => x.Item1).ToList(); var EEGSeries = new OxyPlot.Series.BoxPlotSeries() { Fill = EEGColor, BoxWidth = boxWidth, WhiskerWidth = boxWidth }; if (time == 0) EEGSeries.Title = "EEG"; var EEGItem = CreateBoxItem(EEGAllCorrelations); EEGItem.X = time - EEGSeries.BoxWidth * 1; EEGSeries.Items.Add(EEGItem); timeModel.Series.Add(EEGSeries); AddCorrelation("EEG", time, EEGAllCorrelations); foreach (var cor in EEGAllCorrelations) { AnovaIndividual.Add(anovaIndividualId + ";" + cor.Item1); } AnovaIndividualLegend.Add(anovaIndividualId++ + "=time_" + time + "_EEG"); //gsr GSRAllCorrelations = GSRAllCorrelations.OrderBy(x => x.Item1).ToList(); var GSRSeries = new OxyPlot.Series.BoxPlotSeries() { Fill = EDAColor, BoxWidth = boxWidth, WhiskerWidth = boxWidth }; if (time == 0) GSRSeries.Title = "EDA"; var GSRItem = CreateBoxItem(GSRAllCorrelations); GSRItem.X = time; GSRSeries.Items.Add(GSRItem); timeModel.Series.Add(GSRSeries); AddCorrelation("EDA", time, GSRAllCorrelations); foreach (var cor in GSRAllCorrelations) { AnovaIndividual.Add(anovaIndividualId + ";" + cor.Item1); } AnovaIndividualLegend.Add(anovaIndividualId++ + "=time_" + time + "_GSR"); //hr HRAllCorrelations = HRAllCorrelations.OrderBy(x => x.Item1).ToList(); var HRSeries = new OxyPlot.Series.BoxPlotSeries() { Fill = HRColor, BoxWidth = boxWidth, WhiskerWidth = boxWidth }; if (time == 0) HRSeries.Title = "HR"; var HRItem = CreateBoxItem(HRAllCorrelations); HRItem.X = time + HRSeries.BoxWidth * 1; HRSeries.Items.Add(HRItem); timeModel.Series.Add(HRSeries); AddCorrelation("HR", time, HRAllCorrelations); foreach (var cor in HRAllCorrelations) { AnovaIndividual.Add(anovaIndividualId + ";" + cor.Item1); } AnovaIndividualLegend.Add(anovaIndividualId++ + "=time_" + time + "_HR"); //avg var AVGAllCorrelations = EEGAllCorrelations.Concat(GSRAllCorrelations.Concat(HRAllCorrelations)).ToList(); //AddCorrelation("AVG", time, AVGAllCorrelations); foreach (var cor in AVGAllCorrelations) { AnovaAvg.Add(anovaAvgId + ";" + cor.Item1); } AnovaAvgLegend.Add(anovaAvgId++ + "=time_" + time); double totalAvg = AVGAllCorrelations.Average(x => x.Item1); var txtAvg = new OxyPlot.Annotations.TextAnnotation() { TextPosition = new OxyPlot.DataPoint(time, -1), Text = "Avg " + totalAvg.ToString(".000").Replace(",", "."), Stroke = OxyColors.White }; timeModel.Annotations.Add(txtAvg); timeErrorSeries.Items.Add(new OxyPlot.Series.ErrorColumnItem(sigPoints.Average(), MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(sigPoints.ToArray()), time)); avgLineSeries.Points.Add(new OxyPlot.DataPoint(time, avgs.Average())); File.WriteAllLines(fbd.SelectedPath + "/" + corrType + "_time" + time + ".txt", timeToWrite); } File.WriteAllLines(fbd.SelectedPath + "/significantTime.tex", amountTimeSignificant); timeModel.LegendPlacement = LegendPlacement.Outside; timeModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 1, Minimum = -1, Title = "Pearson's r" }); timeModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Maximum = 2.5, Minimum = -0.5, MajorStep = 1, Title = "Time", MinorTickSize = 0 }); //timeModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Maximum = sensors.Count - 0.5, Minimum = -0.5, MajorStep = 1, Title = "Sensors", MinorTickSize = 0 }); //boxModel.Series.Add(avgLineSeries); PngExporter pnger = new PngExporter(); pnger.ExportToFile(timeModel, fbd.SelectedPath + "/timeBox.png"); pnger.ExportToFile(TimeErrorModel, fbd.SelectedPath + "/errorPlotTest.png"); pnger.ExportToFile(TimeErrorModel2, fbd.SelectedPath + "/errorPlotTest2.png"); /* //correlation and reverse correlation foreach (var time in times) { //Correlation List<string> correlationTimeToWrite = new List<string>(); correlationTimeToWrite.Add("Sensor&Avg Corr&Avg Sig. \\\\"); //Reverse correlation List<string> reverseCorrelationTimeToWrite = new List<string>(); reverseCorrelationTimeToWrite.Add("Sensor & Avg Corr & Avg Sig. \\\\"); foreach (var sensor in sensors) { double correlationAvgCorrelation = timeTable[sensor][time].Where(x => x.Item1 >= 0).Average(x => x.Item1); double correlationStdevCorrelation = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Where(x => x.Item1 >= 0).Select(x => x.Item1).ToArray()); double correlationAvgSignificance = timeTable[sensor][time].Where(x => x.Item1 >= 0).Average(x => x.Item2); double correlationStdevSignificance = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Where(x => x.Item1 >= 0).Select(x => x.Item2).ToArray()); double reverseCorrelationAvgCorrelation = timeTable[sensor][time].Where(x => x.Item1 < 0).Average(x => x.Item1); double reverseCorrelationStdevCorrelation = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Where(x => x.Item1 < 0).Select(x => x.Item1).ToArray()); double reverseCorrelationAvgSignificance = timeTable[sensor][time].Where(x => x.Item1 < 0).Average(x => x.Item2); double reverseCorrelationStdevSignificance = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Where(x => x.Item1 < 0).Select(x => x.Item2).ToArray()); correlationTimeToWrite.Add($"{sensor}&{correlationAvgCorrelation.ToString("0.000")}({correlationStdevCorrelation.ToString("0.000")})&{correlationAvgSignificance.ToString("0.000")}({correlationStdevSignificance.ToString("0.000")}) \\\\"); reverseCorrelationTimeToWrite.Add($"{sensor}&{reverseCorrelationAvgCorrelation.ToString("0.000")}({reverseCorrelationStdevCorrelation.ToString("0.000")})&{reverseCorrelationAvgSignificance.ToString("0.000")}({reverseCorrelationStdevSignificance.ToString("0.000")}) \\\\"); } foreach (Big5 item in Enum.GetValues(typeof(Big5))) { correlationTimeToWrite.Add(item + " Mean: " + big5List["corr"].Average(x => x[item]).ToString("0.00") + ", SD: " + MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(big5List["corr"].Select(x => x[item]).ToArray()).ToString("0.00") + "."); reverseCorrelationTimeToWrite.Add(item + " Mean: " + big5List["revCorr"].Average(x => x[item]).ToString("0.00") + ", SD: " + MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(big5List["revCorr"].Select(x => x[item]).ToArray()).ToString("0.00") + "."); } File.WriteAllLines(fbd.SelectedPath + "/correlationTime" + time + ".txt", correlationTimeToWrite); File.WriteAllLines(fbd.SelectedPath + "/reverseCorrelationTime" + time + ".txt", reverseCorrelationTimeToWrite); } */ var Big5timeBox = new PlotModel() { Title = "Big5 Time Box Plots", LegendPlacement = LegendPlacement.Outside }; Dictionary<Big5, OxyPlot.Series.BoxPlotSeries> big5timeSeries = new Dictionary<Big5, OxyPlot.Series.BoxPlotSeries>(); foreach (Big5 item in Enum.GetValues(typeof(Big5))) { big5timeSeries.Add(item, new OxyPlot.Series.BoxPlotSeries() { Fill = colors[(int)item * 2], Title = item.ToString(), BoxWidth = 0.1, WhiskerWidth = 0.1 }); Big5timeBox.Series.Add(big5timeSeries[item]); } Big5timeBox.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 50, Minimum = 10, Title = "Score" }); Big5timeBox.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Maximum = 2.5, Minimum = -0.5, MajorStep = 1, Title = "Time", MinorTickSize = 0 }); foreach (var time in times) { foreach (Big5 item in Enum.GetValues(typeof(Big5))) { var orderino = big5List["time" + time].Select(x => x[item]).OrderBy(x => x).ToList(); big5timeSeries[item].Items.Add(new OxyPlot.Series.BoxPlotItem(time - 0.25 + (int)item * 0.1, orderino[0], orderino[(int)(orderino.Count * 0.25)], orderino[orderino.Count / 2], orderino[(int)(orderino.Count * 0.75)], orderino.Last())); } } pnger.ExportToFile(Big5timeBox, fbd.SelectedPath + "/timeBoxBig5.png"); foreach (var time in times) { List<string> timeToWrite = new List<string>(); timeToWrite.Add("\\begin{table}"); timeToWrite.Add("\\centering"); timeToWrite.Add("{\\large \\textbf{Time " + time + "}}\\vspace{1pt}"); timeToWrite.Add("\\begin{tabular}{ccc}"); timeToWrite.Add("\\toprule"); timeToWrite.Add("Sensor&Avg Corr&Avg Sig. \\\\"); timeToWrite.Add("\\midrule"); foreach (var sensor in sensors) { double avgCorrelation = timeTable[sensor][time].Average(x => x.Item1); double stdevCorrelation = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Select(x => x.Item1).ToArray()); double avgSignificance = timeTable[sensor][time].Average(x => x.Item2); double stdevSignificance = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(timeTable[sensor][time].Select(x => x.Item2).ToArray()); if (avgSignificance < 0.05) { timeToWrite.Add($"\\textbf{{{sensor}}}&\\textbf{{{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})}}&\\textbf{{{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")})}} \\\\"); } else { timeToWrite.Add($"{sensor}&{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})&{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")}) \\\\"); } } timeToWrite.Add("\\bottomrule"); timeToWrite.Add("\\end{tabular}"); timeToWrite.Add("\\caption{Results from time " + time + "."); foreach (Big5 item in Enum.GetValues(typeof(Big5))) { timeToWrite.Add(item + " Mean: " + big5List["time" + time].Average(x => x[item]).ToString("0.00") + ", SD: " + MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(big5List["time" + time].Select(x => x[item]).ToArray()).ToString("0.00") + "."); } timeToWrite.Add("}"); timeToWrite.Add("\\label{[TABLE] res time" + time + "}"); timeToWrite.Add("\\end{table}"); File.WriteAllLines(fbd.SelectedPath + "/" + corrType + "_time" + time + ".txt", timeToWrite); } var stimModel = new PlotModel() { Title = "Stimuli Groups Box Plot" }; int stimId = 0; sensorsAdded.Clear(); avgLineSeries.Points.Clear(); var Big5StimBox = new PlotModel() { Title = "Big5 Stimuli Box Plots", LegendPlacement = LegendPlacement.Outside }; Dictionary<Big5, OxyPlot.Series.BoxPlotSeries> big5Series = new Dictionary<Big5, OxyPlot.Series.BoxPlotSeries>(); foreach (Big5 item in Enum.GetValues(typeof(Big5))) { big5Series.Add(item, new OxyPlot.Series.BoxPlotSeries() { Fill = colors[(int)item * 2], Title = item.ToString(), BoxWidth = 0.1, WhiskerWidth = 0.1 }); Big5StimBox.Series.Add(big5Series[item]); } Big5StimBox.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 50, Minimum = 10, Title = "Score" }); Big5StimBox.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Maximum = 1.5, Minimum = -0.5, MajorStep = 1, Title = "Category", MinorTickSize = 0 }); List<string> amountStimSignificant = new List<string>(); foreach (var stimuli in stimulis) { List<string> stimuliToWrite = new List<string>(); stimuliToWrite.Add("\\begin{table}"); stimuliToWrite.Add("\\centering"); stimuliToWrite.Add("{\\large \\textbf{Stimuli " + stimuli + "}}\\vspace{1pt}"); stimuliToWrite.Add("\\begin{tabular}{ccc}"); stimuliToWrite.Add("\\toprule"); stimuliToWrite.Add("Sensor&Avg Corr&Avg Sig. \\\\"); stimuliToWrite.Add("\\midrule"); List<double> avgs = new List<double>(); int sensorId = 0; var EEGAllCorrelations = new List<Tuple<double, double>>(); var GSRAllCorrelations = stimuliTable["GSR"][stimuli]; var HRAllCorrelations = stimuliTable["HR"][stimuli]; foreach (var sensor in sensors) { if (sensor.Contains("EEG")) { EEGAllCorrelations.AddRange(stimuliTable[sensor][stimuli]); } double avgCorrelation = stimuliTable[sensor][stimuli].Average(x => x.Item1); double stdevCorrelation = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(stimuliTable[sensor][stimuli].Select(x => x.Item1).ToArray()); double avgSignificance = stimuliTable[sensor][stimuli].Average(x => x.Item2); double stdevSignificance = MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(stimuliTable[sensor][stimuli].Select(x => x.Item2).ToArray()); var orderedAll = stimuliTable[sensor][stimuli].Where(x => x.Item2 * 100 < (int)5).OrderBy(x => x.Item1).ToList(); amountStimSignificant.Add(stimuli + " & " + sensor + " & " + orderedAll.Count); significantAmount[sensor][stimuli == "low" ? 3 : 4] = orderedAll.Count; significantAmountMax[sensor][stimuli == "low" ? 3 : 4] = stimuliTable[sensor][stimuli].Count; //significantCorr[sensor][stimuli == "low" ? 3 : 4] = Tuple.Create(orderedAll.Average(x => x.Item1), MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(orderedAll.Select(x => x.Item1).ToArray())); var boxItem = new OxyPlot.Series.BoxPlotItem(((1 + stimId) % 2) + sensorId * width - (0.5 * width * sensors.Count), orderedAll[0].Item1, orderedAll[(int)(orderedAll.Count * 0.25)].Item1, orderedAll[orderedAll.Count / 2].Item1, orderedAll[(int)(orderedAll.Count * 0.75)].Item1, orderedAll.Last().Item1); //var boxItem = new OxyPlot.Series.BoxPlotItem(sensorId + ((1 + stimId) % 2) * widthTime - (0.5 * widthTime), orderedAll[0].Item1, orderedAll[(int)(orderedAll.Count * 0.25)].Item1, orderedAll[orderedAll.Count / 2].Item1, orderedAll[(int)(orderedAll.Count * 0.75)].Item1, orderedAll.Last().Item1); var boxSeries = new OxyPlot.Series.BoxPlotSeries() { }; boxSeries.BoxWidth = width; boxSeries.WhiskerWidth = width; //boxSeries.BoxWidth = widthTime; //boxSeries.WhiskerWidth = widthTime; boxSeries.Items.Add(boxItem); boxSeries.Fill = colors[sensorId]; //stimModel.Series.Add(boxSeries); avgs.Add(orderedAll.Average(x => x.Item1)); if (!sensorsAdded.Contains(sensor)) { sensorsAdded.Add(sensor); boxSeries.Title = sensor; } if (avgSignificance < 0.05) { stimuliToWrite.Add($"\\textbf{{{sensor}}}&\\textbf{{{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})}}&\\textbf{{{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")})}} \\\\"); } else { stimuliToWrite.Add($"{sensor}&{avgCorrelation.ToString("0.000")}({stdevCorrelation.ToString("0.000")})&{avgSignificance.ToString("0.000")}({stdevSignificance.ToString("0.000")}) \\\\"); } sensorId++; } double boxWidth = 0.3; //eeg EEGAllCorrelations = EEGAllCorrelations.OrderBy(x => x.Item1).ToList(); var EEGSeries = new OxyPlot.Series.BoxPlotSeries() { Fill = EEGColor, BoxWidth = boxWidth, WhiskerWidth = boxWidth }; if (stimuli == "low") EEGSeries.Title = "EEG"; var EEGItem = CreateBoxItem(EEGAllCorrelations); EEGItem.X = (stimuli == "low" ? 0 : 1) - EEGSeries.BoxWidth * 1; EEGSeries.Items.Add(EEGItem); stimModel.Series.Add(EEGSeries); AddCorrelation("EEG", stimuli == "low" ? 3 : 4, EEGAllCorrelations); foreach (var cor in EEGAllCorrelations) { AnovaIndividual.Add(anovaIndividualId + ";" + cor.Item1); } AnovaIndividualLegend.Add(anovaIndividualId++ + "=stimuli_" + stimuli + "_EEG"); //gsr GSRAllCorrelations = GSRAllCorrelations.OrderBy(x => x.Item1).ToList(); var GSRSeries = new OxyPlot.Series.BoxPlotSeries() { Fill = EDAColor, BoxWidth = boxWidth, WhiskerWidth = boxWidth }; if (stimuli == "low") GSRSeries.Title = "EDA"; var GSRItem = CreateBoxItem(GSRAllCorrelations); GSRItem.X = (stimuli == "low" ? 0 : 1); GSRSeries.Items.Add(GSRItem); stimModel.Series.Add(GSRSeries); AddCorrelation("EDA", stimuli == "low" ? 3 : 4, GSRAllCorrelations); foreach (var cor in GSRAllCorrelations) { AnovaIndividual.Add(anovaIndividualId + ";" + cor.Item1); } AnovaIndividualLegend.Add(anovaIndividualId++ + "=stimuli_" + stimuli + "_GSR"); //hr HRAllCorrelations = HRAllCorrelations.OrderBy(x => x.Item1).ToList(); var HRSeries = new OxyPlot.Series.BoxPlotSeries() { Fill = HRColor, BoxWidth = boxWidth, WhiskerWidth = boxWidth }; if (stimuli == "low") HRSeries.Title = "HR"; var HRItem = CreateBoxItem(HRAllCorrelations); HRItem.X = (stimuli == "low" ? 0 : 1) + HRSeries.BoxWidth * 1; HRSeries.Items.Add(HRItem); stimModel.Series.Add(HRSeries); AddCorrelation("HR", stimuli == "low" ? 3 : 4, HRAllCorrelations); foreach (var cor in HRAllCorrelations) { AnovaIndividual.Add(anovaIndividualId + ";" + cor.Item1); } AnovaIndividualLegend.Add(anovaIndividualId++ + "=stimuli_" + stimuli + "_HR"); //avg var AVGAllCorrelations = EEGAllCorrelations.Concat(GSRAllCorrelations.Concat(HRAllCorrelations)).ToList(); //AddCorrelation("AVG", stimuli == "low" ? 3 : 4, AVGAllCorrelations); foreach (var cor in AVGAllCorrelations) { AnovaAvg.Add(anovaAvgId + ";" + cor.Item1); } AnovaAvgLegend.Add(anovaAvgId++ + "=stimuli_" + stimuli); avgLineSeries.Points.Add(new OxyPlot.DataPoint(0, avgs.Average())); stimuliToWrite.Add("\\bottomrule"); stimuliToWrite.Add("\\end{tabular}"); stimuliToWrite.Add("\\caption{Results from stimuli " + stimuli + "."); foreach (Big5 item in Enum.GetValues(typeof(Big5))) { stimuliToWrite.Add(item + " Mean: " + big5List["stim" + stimuli].Average(x => x[item]).ToString("0.00") + ", SD: " + MathNet.Numerics.Statistics.ArrayStatistics.PopulationStandardDeviation(big5List["stim" + stimuli].Select(x => x[item]).ToArray()).ToString("0.00") + "."); var orderino = big5List["stim" + stimuli].Select(x => x[item]).OrderBy(x => x).ToList(); big5Series[item].Items.Add(new OxyPlot.Series.BoxPlotItem(((1 + stimId) % 2) - 0.25 + (int)item * 0.1, orderino[0], orderino[(int)(orderino.Count * 0.25)], orderino[orderino.Count / 2], orderino[(int)(orderino.Count * 0.75)], orderino.Last())); } stimuliToWrite.Add("}"); stimuliToWrite.Add("\\label{[TABLE] res stimuli" + stimuli + "}"); stimuliToWrite.Add("\\end{table}"); File.WriteAllLines(fbd.SelectedPath + "/" + corrType + "_stimuli_" + stimuli + ".txt", stimuliToWrite); stimId++; } File.WriteAllLines(fbd.SelectedPath + "/significantStim.tex", amountStimSignificant); List<string> sigAmountLines = new List<string>(); foreach (var sensor in sensors) { string linerino = sensor; for (int i = 0; i < 5; i++) { linerino += $" & {significantAmount[sensor][i]}/{significantAmountMax[sensor][i]}"; } sigAmountLines.Add(linerino + "\\\\"); } File.WriteAllLines(fbd.SelectedPath + "/significantTable.tex", sigAmountLines); //File.WriteAllLines(fbd.SelectedPath + "/significantTable.tex", significantAmount.Select(x => $"{x.Key} & {x.Value[0]} & {x.Value[1]} & {x.Value[2]} & {x.Value[3]} & {x.Value[4]}").ToList()); File.WriteAllLines(fbd.SelectedPath + "/significantCorrTable.tex", significantCorr.Select(x => $"{x.Key} & {x.Value[0].Item1.ToString(".000")}({x.Value[0].Item2.ToString(".000")}) & {x.Value[1].Item1.ToString(".000")}({x.Value[1].Item2.ToString(".000")}) & {x.Value[2].Item1.ToString(".000")}({x.Value[2].Item2.ToString(".000")}) & {x.Value[3].Item1.ToString(".000")}({x.Value[3].Item2.ToString(".000")}) & {x.Value[4].Item1.ToString(".000")}({x.Value[4].Item2.ToString(".000")}) \\\\")); File.WriteAllLines(fbd.SelectedPath + "/significantCorrTableTime.tex", significantCorr.Select(x => $"{x.Key} & {x.Value[0].Item1.ToString(".000")} (SD={x.Value[0].Item2.ToString(".000")}, p={x.Value[0].Item3.ToString(".000000")}) & {x.Value[1].Item1.ToString(".000")} (SD={x.Value[1].Item2.ToString(".000")}, p={x.Value[1].Item3.ToString(".000000")}) & {x.Value[2].Item1.ToString(".000")} (SD={x.Value[2].Item2.ToString(".000")}, p={x.Value[2].Item3.ToString(".000000")}) \\\\")); File.WriteAllLines(fbd.SelectedPath + "/significantCorrTableStimuli.tex", significantCorr.Select(x => $"{x.Key} & {x.Value[3].Item1.ToString(".000")} (SD={x.Value[3].Item2.ToString(".000")}, p={x.Value[3].Item3.ToString(".000")}) & {x.Value[4].Item1.ToString(".000")} (SD={x.Value[4].Item2.ToString(".000")}, p={x.Value[4].Item3.ToString(".000")}) \\\\")); List<string> timeLines = new List<string>() { "sensor & 0 vs 1 & 1 vs 2 & 0 vs 2" }; List<string> stimLines = new List<string>() { "sensor & 0 vs Low & Low vs High & 0 vs High" }; foreach (var item in significantCorr) { var z01 = ZCalc(item.Value[0].Item1, Convert.ToInt32(item.Value[0].Item2), item.Value[1].Item1, Convert.ToInt32(item.Value[1].Item2)); var z12 = ZCalc(item.Value[1].Item1, Convert.ToInt32(item.Value[1].Item2), item.Value[2].Item1, Convert.ToInt32(item.Value[2].Item2)); var z02 = ZCalc(item.Value[0].Item1, Convert.ToInt32(item.Value[0].Item2), item.Value[2].Item1, Convert.ToInt32(item.Value[2].Item2)); var p01 = ZtoP(z01); var p12 = ZtoP(z12); var p02 = ZtoP(z02); timeLines.Add($"{item.Key} & z: {z01} | p: {p01} & z: {z12} | p: {p12} & z: {z02} | p: {p02}"); var z0Low = ZCalc(item.Value[0].Item1, Convert.ToInt32(item.Value[0].Item2), item.Value[3].Item1, Convert.ToInt32(item.Value[3].Item2)); var zLowHigh = ZCalc(item.Value[3].Item1, Convert.ToInt32(item.Value[3].Item2), item.Value[4].Item1, Convert.ToInt32(item.Value[4].Item2)); var z0High = ZCalc(item.Value[0].Item1, Convert.ToInt32(item.Value[0].Item2), item.Value[4].Item1, Convert.ToInt32(item.Value[4].Item2)); var p0Low = ZtoP(z0Low); var pLowHigh = ZtoP(zLowHigh); var p0High = ZtoP(z0High); stimLines.Add($"{item.Key} & z: {z0Low} | p: {p0Low} & z: {zLowHigh} | p: {pLowHigh} & z: {z0High} | p: {p0High}"); } File.WriteAllLines(fbd.SelectedPath + "/significantCorrCompareTime.tex", timeLines); File.WriteAllLines(fbd.SelectedPath + "/significantCorrCompareStimuli.tex", stimLines); pnger.ExportToFile(Big5StimBox, fbd.SelectedPath + "/stimBoxBig5.png"); stimModel.LegendPlacement = LegendPlacement.Outside; //index 1 = low var stimTxt0 = new OxyPlot.Annotations.TextAnnotation() { TextPosition = new OxyPlot.DataPoint(0, -1), Text = "Avg " + avgLineSeries.Points[1].Y.ToString(".000").Replace(",", "."), Stroke = OxyColors.White }; //index 0 = high var stimTxt1 = new OxyPlot.Annotations.TextAnnotation() { TextPosition = new OxyPlot.DataPoint(1, -1), Text = "Avg " + avgLineSeries.Points[0].Y.ToString(".000").Replace(",", "."), Stroke = OxyColors.White }; stimModel.Annotations.Add(stimTxt0); stimModel.Annotations.Add(stimTxt1); stimModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Left, Maximum = 1, Minimum = -1, Title = "Pearson's r" }); stimModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Maximum = 1.5, Minimum = -0.5, MajorStep = 1, Title = "Stimuli", MinorTickSize = 0 }); //stimModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Position = OxyPlot.Axes.AxisPosition.Bottom, Maximum = sensors.Count - 0.5, Minimum = -0.5, MajorStep = 1, Title = "Sensors", MinorTickSize = 0 }); pnger.ExportToFile(stimModel, fbd.SelectedPath + "/stimBox.png"); File.WriteAllLines(fbd.SelectedPath + "/anovaIndividual.csv", AnovaIndividual); File.WriteAllLines(fbd.SelectedPath + "/anovaIndividualLegend.csv", AnovaIndividualLegend); File.WriteAllLines(fbd.SelectedPath + "/anovaAvg.csv", AnovaAvg); File.WriteAllLines(fbd.SelectedPath + "/anovaAvgLegend.csv", AnovaAvgLegend); Log.LogMessage("DonnoDK"); } }
static void SavePngScatter(string path, string name, List<double> A, List<double> B, double axisMin = 0, double axisMax = 0) { PngExporter pngify = new PngExporter(); pngify.Width = 2000; pngify.Height = 2000; var model = new PlotModel() { Title = name }; var scatterSeries = new OxyPlot.Series.ScatterSeries() { MarkerSize = 0.8f, MarkerType = MarkerType.Circle, MarkerFill = OxyColors.Black }; for (int i = 0; i < A.Count && i < B.Count; i++) { scatterSeries.Points.Add(new OxyPlot.Series.ScatterPoint(A[i], B[i])); } model.Series.Add(scatterSeries); model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Minimum = axisMin, Maximum = axisMax, Position = OxyPlot.Axes.AxisPosition.Left }); model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Minimum = axisMin, Maximum = axisMax, Position = OxyPlot.Axes.AxisPosition.Bottom }); pngify.ExportToFile(model, path); }
static void SavePng(string path, string name, List<double> A, List<double> B, List<Tuple<int, int>> pairings = null) { PngExporter pngify = new PngExporter(); pngify.Width = 3200; pngify.Height = 1200; var model = new PlotModel() { Title = name }; var aSeries = new OxyPlot.Series.LineSeries() { Color = OxyColors.Blue }; var bSeries = new OxyPlot.Series.LineSeries() { Color = OxyColors.Red }; for (int i = 0; i < A.Count; i++) { aSeries.Points.Add(new OxyPlot.DataPoint(i, A[i])); } for (int i = 0; i < B.Count; i++) { bSeries.Points.Add(new OxyPlot.DataPoint(i, B[i])); } if (pairings != null) { for (int i = 0; i < pairings.Count; i += 10) { var lineSeries = new OxyPlot.Series.LineSeries() { Color = OxyColors.Gray, StrokeThickness = 0.2 }; lineSeries.Points.Add(aSeries.Points[pairings[i].Item1]); lineSeries.Points.Add(bSeries.Points[pairings[i].Item2]); model.Series.Add(lineSeries); } } model.Series.Add(aSeries); model.Series.Add(bSeries); model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Minimum = 0, Maximum = 1, Position = OxyPlot.Axes.AxisPosition.Left }); //model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Minimum = 0, Maximum = 1, Position = OxyPlot.Axes.AxisPosition.Bottom }); pngify.ExportToFile(model, path); }
private void btn_ExportPNG_Click(object sender, EventArgs e) { if (loaded.Count < 2) { Log.LogMessage("You must load all data before exporting!"); return; } int height = 0; if (!int.TryParse(txt_height.Text, out height)) { Log.LogMessage("Height must be an integer"); return; } int width = 0; if (!int.TryParse(txt_width.Text, out width)) { Log.LogMessage("Width must be an integer"); return; } int offset = 0; if (!int.TryParse(txt_PlotDataOffset.Text, out offset)) { Log.LogMessage("Offset must be an integer"); return; } double pointSize = 0; if (!double.TryParse(txt_PlotPointSize.Text.Replace(".", ","), out pointSize)) { Log.LogMessage("Point size must be a double"); return; } int from = -1; if (!int.TryParse(txt_ExportFrom.Text, out from) || txt_ExportFrom.Text == "") { Log.LogMessage("Export from not valid integer, using 0"); from = -1; } int to = -1; if (!int.TryParse(txt_ExportTo.Text, out to) || txt_ExportTo.Text == "") { Log.LogMessage("Export to not valid integer, using max"); to = -1; } SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = ".png"; sfd.Filter = "png|*.png"; if (sfd.ShowDialog() == DialogResult.OK) { PngExporter pngify = new PngExporter(); pngify.Width = width; pngify.Height = height; var xy = GetXY(from, to); var test = xy.Item1; var recall = xy.Item2; FitPlot(test, recall); var model = new PlotModel() { Title = $"Slope:{slope.ToString("0.000")} RSquared:{rsquared.ToString("0.000")}" }; var dataSeries = new OxyPlot.Series.ScatterSeries() { MarkerType = MarkerType.Circle, MarkerStroke = OxyColors.Red }; for (int i = 0; i < test.Count; i++) { dataSeries.Points.Add(new OxyPlot.Series.ScatterPoint(test[i], recall[i]) { Size = pointSize }); } var fitSeries = new OxyPlot.Series.FunctionSeries((x) => intercept + slope * x, test.Min(), test.Max(), 0.001) { Color = OxyColors.Blue }; model.Series.Add(dataSeries); model.Series.Add(fitSeries); model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Minimum = 0, Maximum = 1, Position = OxyPlot.Axes.AxisPosition.Left }); model.Axes.Add(new OxyPlot.Axes.LinearAxis() { Minimum = 0, Maximum = 1, Position = OxyPlot.Axes.AxisPosition.Bottom }); pngify.ExportToFile(model, sfd.FileName); Log.LogMessage("Saved " + sfd.FileName + "!"); } }