-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlottingHelper.cs
756 lines (683 loc) · 31.4 KB
/
PlottingHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//
// PlottingHelpers.cs
//
// Author:
// Tom Diethe <tom.diethe@bristol.ac.uk>
//
// Copyright (c) 2016 University of Bristol
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace InferNetPlotting
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MicrosoftResearch.Infer.Distributions;
using PythonPlotter;
using InferHelpers;
using Vector = MathNet.Numerics.LinearAlgebra.Vector<double>;
using Matrix = MathNet.Numerics.LinearAlgebra.Matrix<double>;
public static class PlottingHelper
{
public static string PythonPath { get; set; }
public static string ScriptPath { get; set; }
public static string FigurePath { get; set; }
/// <summary>
/// Plots the results.
/// </summary>
/// <param name="x">The x values.</param>
/// <param name="y">The y values.</param>
/// <param name="title">The plot title.</param>
/// <param name="subTitle">Sub title.</param>
/// <param name="xlabel">x-axis label.</param>
/// <param name="ylabel">y-axis label.</param>
/// <param name="show">Whether to show the plot.</param>
public static void Plot(IEnumerable<double> x, IEnumerable<double> y, string title, string subTitle,
string xlabel, string ylabel, bool show = false)
{
var series = (ISeries) (new LineSeries { X = x, Y = y });
var plotter = new Plotter
{
Title = title + (string.IsNullOrEmpty(subTitle) ? string.Empty : " " + subTitle),
XLabel = xlabel,
YLabel = ylabel,
Series = new[] { series },
ScriptName = Path.Combine(ScriptPath, title.Replace(" ", "_") + ".py"),
FigureName = Path.Combine(FigurePath, title.Replace(" ", "_") + ".pdf"),
Python = PythonPath,
Show = show,
Tight = true
};
plotter.Plot();
}
public static void TwinTwinPlot(
Dictionary<string, IEnumerable<double>> y1,
Dictionary<string, IEnumerable<double>> y2,
string title,
string xlabel,
string y1Label,
string y2Label,
bool show = false)
{
var series1 = y1.Select(ia => (ISeries) (new LineSeries {Label = ia.Key, X = ia.Value})).ToArray();
var series2 = y2.Select(ia => (ISeries) (new LineSeries {Label = ia.Key, X = ia.Value})).ToArray();
// Turn on color cycling for both series
series1[0].Color = "next(palette)";
series2[0].Color = "next(palette)";
// Here we build the plotting script for the second plot (without the pre/postamble),
// so we can append it to the script for the first plot
var plotter2 = new Plotter { XLabel = xlabel, YLabel = y2Label, Series = series2, TwinX = true };
plotter2.BuildScript();
// TODO: http://matplotlib.org/examples/api/two_scales.html
var plotter1 = new Plotter
{
Title = title,
XLabel = xlabel,
YLabel = y1Label,
Series = series1,
Python = PythonPath,
Show = show,
Tight = true
};
plotter1.Plot(plotter2.Script);
}
/// <summary>
/// Plots the results.
/// </summary>
/// <param name="y">The values.</param>
/// <param name="filename">The file name.</param>
/// <param name="xlabel">x-axis label.</param>
/// <param name="ylabel">y-axis label.</param>
/// <param name="show">Whether to show the plot.</param>
public static void Plot(Dictionary<string, IEnumerable<double>> y, string filename, string xlabel,
string ylabel, bool show = false)
{
var series = y.Select(ia => (ISeries) (new LineSeries {Label = ia.Key, X = ia.Value})).ToArray();
var plotter = new Plotter
{
XLabel = xlabel,
YLabel = ylabel,
Series = series,
ScriptName = Path.Combine(ScriptPath, filename.Replace(" ", "_") + ".py"),
FigureName = Path.Combine(FigurePath, filename.Replace(" ", "_") + ".pdf"),
Python = PythonPath,
Show = show,
Tight = true
};
plotter.Plot();
}
/// <summary>
/// Plots the results.
/// </summary>
/// <param name="y">The values.</param>
/// <param name="title">The plot title.</param>
/// <param name="subTitle">Sub title.</param>
/// <param name="xlabel">x-axis label.</param>
/// <param name="ylabel">y-axis label.</param>
/// <param name="show">Whether to show the plot.</param>
public static void Plot(Dictionary<string, IEnumerable<double>> y, string title, string subTitle, string xlabel,
string ylabel, bool show = false)
{
var series = y.Select(ia => (ISeries) (new LineSeries {Label = ia.Key, X = ia.Value})).ToArray();
var plotter = new Plotter
{
Title = title + (string.IsNullOrEmpty(subTitle) ? string.Empty : " " + subTitle),
XLabel = xlabel,
YLabel = ylabel,
Series = series,
ScriptName = Path.Combine(ScriptPath, title.Replace(" ", "_") + ".py"),
FigureName = Path.Combine(FigurePath, title.Replace(" ", "_") + ".pdf"),
Python = PythonPath,
Show = show,
Tight = true
};
plotter.Plot();
}
public static void SparsityPlot(Gaussian[][] coefficients, string title, string filename, bool show = false)
{
var values = coefficients.GetMeans<Gaussian>(); // .To2D().Transpose().ToJagged();
var plotter = new Plotter
{
// Title = title,
XLabel = "bases",
YLabel = "signals",
ScriptName = Path.Combine(ScriptPath, $"{filename.Replace(" ", "_")}.py"),
FigureName = Path.Combine(FigurePath, $"{filename.Replace(" ", "_")}.pdf"),
Python = PythonPath,
Series = new ISeries[] { new HintonSeries { Values = values } },
Grid = false,
Show = show,
Tight = true
};
plotter.Plot();
}
/// <summary>
/// Plots the functions.
/// </summary>
/// <param name="functions">The functions to plot.</param>
/// <param name="title">The plot title..</param>
/// <param name="subTitle">Sub title.</param>
/// <param name="options">The plotting options.</param>
public static void PlotFunctions(Matrix functions, string title, string subTitle, PlotOptions options)
{
if (options.IsImage)
{
PlotImages(functions, title, options.Images);
return;
}
var series = CreateSeries(functions.ToRowArrays(), null, options.Signals);
var plotter = new Plotter
{
Title = title + (string.IsNullOrEmpty(subTitle) ? string.Empty : " " + subTitle),
XLabel = "x", YLabel = "y",
Series = series, Subplots = options.Signals.Subplots,
ScriptName = Path.Combine(ScriptPath, title + ".py"),
FigureName = Path.Combine(FigurePath, title + ".pdf"),
Python = PythonPath
};
plotter.Plot();
}
public static void ScatterPlot(Matrix data, int[] labels, string title, string subTitle)
{
var series = (ISeries) (new ScatterSeries
{
X = data.Column(0).ToArray(),
Y = data.Column(1).ToArray(),
Color = "[" + string.Join(", ", labels.Select(ia => $"{ia}")) + "]"
});
var plotter = new Plotter
{
Title = title + (string.IsNullOrEmpty(subTitle) ? string.Empty : " " + subTitle),
XLabel = "x",
YLabel = "y",
Series = new[] { series },
ScriptName = Path.Combine(ScriptPath, title + ".py"),
FigureName = Path.Combine(FigurePath, title + ".pdf"),
Python = PythonPath
};
plotter.Plot();
}
/// <summary>
/// Plots the results.
/// </summary>
/// <returns>The results.</returns>
/// <param name="numBases">Number of bases.</param>
/// <param name="signalWidth">The signal width.</param>
/// <param name="dictionary">Dictionary.</param>
/// <param name="coefficients">Coefficients.</param>
/// <param name="st">Sub title.</param>
/// <param name="plotOptions">The plotting options.</param>
public static void PlotResults(
int numBases, int signalWidth,
Gaussian[][] dictionary, Gaussian[][] coefficients,
string st, PlotOptions plotOptions)
{
var subplots = new Subplots
{
ShareX = true,
ShareY = true,
Rows = numBases < 16 ? numBases : 4,
Columns = numBases < 16 ? 1 : 4
};
plotOptions.Dictionary.Subplots = subplots;
plotOptions.Dictionary.NumToShow = plotOptions.IsImage ? 1 : Math.Min(numBases, 16);
if (plotOptions.IsImage)
{
PlotImages(dictionary, signalWidth, "Dictionary", plotOptions.Dictionary);
}
else
{
PlotPosteriors(dictionary, "Dictionary", st, plotOptions.Dictionary);
}
if (coefficients == null)
return;
subplots.Rows = 3;
subplots.Columns = 2;
plotOptions.Coefficients.NumToShow = 6;
plotOptions.Coefficients.Subplots = subplots;
PlotPosteriors(coefficients, "Coefficients", st, plotOptions.Coefficients);
}
/// <summary>
/// Plots the results.
/// </summary>
/// <returns>The results.</returns>
/// <param name="numBases">Number of bases.</param>
/// <param name="signalWidth">The signal width.</param>
/// <param name="dictionary">Dictionary.</param>
/// <param name="coefficients">Coefficients.</param>
/// <param name="st">Sub title.</param>
/// <param name="plotOptions">The plotting options.</param>
/// <param name="labels">Class labels.</param>
public static void PlotResults(
int numBases, int signalWidth,
Gaussian[][][] dictionary, Gaussian[][][] coefficients,
string st, PlotOptions plotOptions, IList<string> labels)
{
var subplots = new Subplots
{
ShareX = true,
ShareY = true,
Rows = numBases < 16 ? numBases : 4,
Columns = numBases < 16 ? 1 : 4,
};
plotOptions.Dictionary.Subplots = subplots;
plotOptions.Dictionary.NumToShow = plotOptions.IsImage ? 1 : Math.Min(numBases, 16);
for (var i = 0; i < dictionary.Length; i++)
{
string label = "Dictionary_class=" + (labels == null ? $"{i}" : labels[i]);
if (plotOptions.IsImage)
{
PlotImages(dictionary[i], signalWidth, label, plotOptions.Dictionary);
}
else
{
PlotPosteriors(dictionary[i], label, st, plotOptions.Dictionary);
}
}
if (coefficients == null)
return;
subplots.Rows = 3;
subplots.Columns = 2;
plotOptions.Coefficients.NumToShow = 6;
plotOptions.Coefficients.Subplots = subplots;
for (var i = 0; i < coefficients.Length; i++)
{
var coefficient = coefficients[i];
string label = "Coefficients_class={label}" + (labels == null ? $"{i}" : labels[i]);
PlotPosteriors(coefficient, label, st, plotOptions.Coefficients);
}
}
/// <summary>
/// Plots the results.
/// </summary>
/// <returns>The results.</returns>
/// <param name="numBases">Number of bases.</param>
/// <param name="signalWidth">The signal width.</param>
/// <param name="dictionary">Dictionary.</param>
/// <param name="coefficients">Coefficients.</param>
/// <param name="st">Sub title.</param>
/// <param name="plotOptions">Plotting options.</param>
public static void PlotResults(
int numBases, int signalWidth,
VectorGaussian[] dictionary, VectorGaussian[] coefficients,
string st, PlotOptions plotOptions)
{
// Note the dictionary is transposed in this case
var dict = dictionary.Select(DistributionHelpers.IndependentApproximation).ToArray().Transpose();
var coef = coefficients.Select(DistributionHelpers.IndependentApproximation).ToArray();
PlotResults(numBases, signalWidth, dict, coef, st, plotOptions);
}
/// <summary>
/// Plots the results.
/// </summary>
/// <returns>The results.</returns>
/// <param name="numBases">Number of bases.</param>
/// <param name="signalWidth">The signal width.</param>
/// <param name="dictionary">Dictionary.</param>
/// <param name="coefficients">Coefficients.</param>
/// <param name="st">Sub title.</param>
/// <param name="plotOptions">Plotting options.</param>
public static void PlotResults(
int numBases, int signalWidth,
Gaussian[][] dictionary, VectorGaussian[] coefficients,
string st, PlotOptions plotOptions)
{
var coef = coefficients.Select(DistributionHelpers.IndependentApproximation).ToArray();
PlotResults(numBases, signalWidth, dictionary, coef, st, plotOptions);
}
/// <summary>
/// Plots the reconstructions.
/// </summary>
/// <returns>The reconstructions.</returns>
/// <param name="reconstructions">Reconstructions.</param>
/// <param name="averageError">The average reconstruction error.</param>
/// <param name="subTitle">Sub title.</param>
/// <param name="normalised">Whether these are normalised reconstructions.</param>
/// <param name="plotOptions">Plotting options.</param>
public static void PlotReconstructions(Reconstruction[] reconstructions, double averageError, string subTitle,
bool normalised, PlotOptions plotOptions)
{
if (plotOptions.IsImage)
{
PlotImageReconstructions(reconstructions, averageError, subTitle, normalised, plotOptions);
return;
}
var series1 = reconstructions.Take(plotOptions.Reconstructions.NumToShow).Select(
(ia, i) => (ISeries)(new LineSeries
{
Label = "signal",
X = ia.Signal,
Row = i/plotOptions.Reconstructions.Subplots.Columns,
Column = i%plotOptions.Reconstructions.Subplots.Columns
})).ToArray();
var series2 = reconstructions.Take(plotOptions.Reconstructions.NumToShow).Select(
(ia, i) => (ISeries)(new ErrorLineSeries
{
Label = "reconstruction",
ErrorLabel = "$\\pm$s.d.",
X = ia.Estimate.GetMeans(),
ErrorValues = ia.Estimate.GetStandardDeviations(),
Row = i/plotOptions.Reconstructions.Subplots.Columns,
Column = i%plotOptions.Reconstructions.Subplots.Columns
})).ToArray();
IList<ISeries> series = series1.Concat(series2).ToArray();
string n = normalised ? " (normalised)" : string.Empty;
// var series = new[] { new LineSeries { X = x1, Row = 0 }, new LineSeries { X = x2, Row = 1 } };
string sub = string.IsNullOrEmpty(subTitle) ? string.Empty : $"_{subTitle.Replace(" ", "_")}";
var plotter = new Plotter
{
Title = $"Reconstructions{n}, RMSE={averageError:N4}",
XLabel = "x",
YLabel = "y",
Series = series,
Subplots = plotOptions.Reconstructions.Subplots,
ScriptName = Path.Combine(ScriptPath, $"Reconstructions_{n}{sub}.py"),
FigureName = Path.Combine(FigurePath, $"Reconstructions_{n}{sub}.pdf"),
Python = PythonPath,
Show = plotOptions.Reconstructions.Show
};
plotter.Plot();
}
/// <summary>
/// Plots the image reconstructions. Note that we assume the images are square
/// </summary>
/// <returns>The reconstructions.</returns>
/// <param name="reconstructions">Reconstructions.</param>
/// <param name="averageError">The average reconstruction error.</param>
/// <param name="subTitle">Sub title.</param>
/// <param name="normalised">Whether these are normalised reconstructions.</param>
/// <param name="plotOptions">Plotting options.</param>
public static void PlotImageReconstructions(Reconstruction[] reconstructions, double averageError,
string subTitle, bool normalised, PlotOptions plotOptions)
{
var series1 = reconstructions.Take(plotOptions.Reconstructions.NumToShow).Select(
(ia, i) => (ISeries) new HintonSeries
{
Label = "signal",
Values = Reshape(Vector.Build.Dense(ia.Signal)),
Row = i,
Column = 0
}).ToArray();
var series2 = reconstructions.Take(plotOptions.Reconstructions.NumToShow).Select(
(ia, i) => (ISeries) new HintonSeries
{
Label = "reconstruction",
// ErrorLabel = "$\\pm$s.d.",
Values = Reshape(Vector.Build.Dense(ia.Estimate.GetMeans())),
// ErrorValues = ia.Estimate.GetStandardDeviations(),
Row = i,
Column = 1
}).ToArray();
IList<ISeries> series = series1.Concat(series2).ToArray();
string n = normalised ? "(normalised)" : string.Empty;
var subplots = new Subplots {Rows = plotOptions.Reconstructions.NumToShow, Columns = 2, ShareX = false, ShareY = false};
string sub1 = string.IsNullOrEmpty(subTitle) ? string.Empty : $" {subTitle.Replace("_", " ")}";
string sub2 = string.IsNullOrEmpty(subTitle) ? string.Empty : $"_{subTitle.Replace(" ", "_")}";
string message = $"Reconstructions {n}{sub1}, avg. error={averageError:N4}";
Console.WriteLine(message);
var plotter = new Plotter
{
Title = message,
XLabel = "x",
YLabel = "y",
Grid = false,
Series = series,
Subplots = subplots,
ScriptName = Path.Combine(ScriptPath, $"Reconstructions_{n}{sub2}.py"),
FigureName = Path.Combine(FigurePath, $"Reconstructions_{n}{sub2}.pdf"),
Python = PythonPath,
Show = plotOptions.Reconstructions.Show
};
plotter.Plot();
}
// /// <summary>
// /// Plots the reconstructions.
// /// </summary>
// /// <returns>The reconstructions.</returns>
// /// <param name="signals">Signals.</param>
// /// <param name="reconstructions">Reconstructions.</param>
// /// <param name="title">Title.</param>
// /// <param name="numToShow">Number to show.</param>
// /// <param name="rows">Rows.</param>
// /// <param name="cols">Cols.</param>
// /// <param name="subTitle">Sub title.</param>
// public static void PlotReconstructions(double[][] signals, double[][] reconstructions, string title, int numToShow, int rows, int cols, string subTitle = null)
// {
// var r = signals.Zip(reconstructions, (s, e) => new Reconstruction { Signal = s, Estimate = e.Select(Gaussian.PointMass).ToArray() }).ToArray();
// PlotReconstructions(r, averageError, title, numToShow, rows, cols, subTitle);
// }
/// <summary>
/// Plots the posteriors.
/// </summary>
/// <returns>The posteriors.</returns>
/// <param name="posteriors">Posteriors.</param>
/// <param name="title">Title.</param>
/// <param name="subTitle">Sub title.</param>
/// <param name="options">Plot options.</param>
public static void PlotPosteriors<T>(
T[][] posteriors,
string title,
string subTitle,
BaseOptions options)
where T : IDistribution<double>, CanGetMean<double>, CanGetVariance<double>
{
var series = CreateSeries(posteriors, null, options);
// var series = new[] { new LineSeries { X = x1, Row = 0 }, new LineSeries { X = x2, Row = 1 } };
string sub = string.IsNullOrEmpty(subTitle) ? string.Empty : $"_{subTitle.Replace(" ", "_")}";
string sel = $"{options.Skip}-{options.Skip + options.NumToShow}";
var plotter = new Plotter
{
Title = $"{title} {sel}",
XLabel = "x", YLabel = "y", Series = series, Subplots = options.Subplots,
Python = PythonPath,
ScriptName = Path.Combine(ScriptPath, $"{title}{sub}_{sel}.py"),
FigureName = Path.Combine(FigurePath, $"{title}{sub}_{sel}.pdf"),
Show = options.Show
};
plotter.Plot();
}
/// <summary>
/// Plot errors with evidence on twinx
/// </summary>
public static void PlotErrorsWithEvidence(IList<double> bases, IList<double> errors, IList<double> evidence, bool show = false)
{
// Here we're going to customise the Plotter.TwinPlot function
const string title = "Effect of number of bases";
const string xlabel = "#bases";
const string y1Label = "Reconstruction error";
const string y2Label = "Log Evidence";
var series1 = new ISeries[] { new LineSeries { X = bases, Y = errors, Color = "next(palette)", Label = "Reconstruction error" } };
var series2 = new ISeries[] { new LineSeries { X = bases, Y = evidence, Color = "next(palette)", Label = "Evidence" } };
// Here we build the plotting script for the second plot (without the pre/postamble),
// so we can append it to the script for the first plot
var plotter2 = new Plotter { XLabel = xlabel, YLabel = y2Label, Series = series2, TwinX = true };
plotter2.BuildScript();
// TODO: http://matplotlib.org/examples/api/two_scales.html
var plotter1 = new Plotter
{
Title = title,
XLabel = xlabel,
YLabel = y1Label,
Series = series1,
Python = PythonPath,
ScriptName = Path.Combine(ScriptPath, "EffectOfBases"),
FigureName = Path.Combine(FigurePath, "EffectOfBases"),
Show = show
};
plotter1.Plot(plotter2.Script);
}
/// <summary>
/// Plots the image.
/// </summary>
/// <param name="imageFlat">Image flat.</param>
/// <param name="show">Whether to show the plot.</param>
public static void PlotImage(Vector imageFlat, bool show = false)
{
// DenseOfColumnMajor(rows, columns, m.Row(0));
// Plotter.Hinton(image);
var plotter = new Plotter
{
Series = new ISeries[] { new MatrixSeries { Values = Reshape(imageFlat) } },
Grid = false,
Python = PythonPath,
ScriptName = Path.Combine(ScriptPath, "EffectOfBases"),
FigureName = Path.Combine(FigurePath, "EffectOfBases"),
Show = show
};
plotter.Plot();
}
/// <summary>
/// Plots the images.
/// </summary>
/// <param name="imagesFlat">Images.</param>
/// <param name="title">The title.</param>
/// <param name="options">Plotting options.</param>
public static void PlotImages(Matrix imagesFlat, string title, BaseOptions options)
{
var series =
(from t in imagesFlat.EnumerateRowsIndexed()
let index = t.Item1
let image = Reshape(t.Item2)
select
new MatrixSeries
{
Values = image,
Row = index/options.Subplots.Columns,
Column = index%options.Subplots.Columns
}
).Cast<ISeries>().ToList();
var plotter = new Plotter
{
Title = title,
Series = series,
Grid = false,
Subplots = options.Subplots,
Python = PythonPath,
ScriptName = Path.Combine(ScriptPath, "EffectOfBases"),
FigureName = Path.Combine(FigurePath, "EffectOfBases"),
Show = options.Show
};
plotter.Plot();
}
public static void PlotImages(Gaussian[][] images, int imageWidth, string title, BaseOptions options)
{
var dictionary = Matrix.Build.DenseOfRowArrays(images.GetMeans<Gaussian>());
PlotImages(dictionary.SubMatrix(0, options.Subplots.Rows * options.Subplots.Columns, 0, imageWidth), title, options);
}
public static ISeries[] CreateSeries(double[][] data, string[] labels, BaseOptions options)
{
ISeries[] series;
switch (options.PlotType)
{
case PlotType.Line:
series = data.Skip(options.Skip).Take(options.NumToShow).Select(
(ia, i) => (ISeries) (new ErrorLineSeries
{
Label = labels?[i],
X = ia,
Row = i/options.Subplots.Columns,
Column = i%options.Subplots.Columns
})).ToArray();
break;
case PlotType.Bar:
series = data.Skip(options.Skip).Take(options.NumToShow).Select(
(ia, i) => (ISeries) (new BarSeries<string>
{
Label = labels?[i],
DependentValues = ia,
Row = i/options.Subplots.Columns,
Column = i%options.Subplots.Columns
})).ToArray();
break;
default:
throw new ArgumentException("Unknonw plot type", nameof(options.PlotType));
}
return series;
}
public static
ISeries[] CreateSeries<T>(T[][] data, string[] labels, BaseOptions options)
where T : IDistribution<double>, CanGetMean<double>, CanGetVariance<double>
{
ISeries[] series;
switch (options.PlotType)
{
case PlotType.ErrorLine:
series = data.Skip(options.Skip).Take(options.NumToShow).Select(
(ia, i) => (ISeries)(new ErrorLineSeries
{
Label = labels?[i],
X = ia.Select(x => x.GetMean()).ToArray(),
ErrorValues = ia.GetStandardDeviations(),
Row = i / options.Subplots.Columns,
Column = i % options.Subplots.Columns
})).ToArray();
break;
case PlotType.Bar:
series = data.Skip(options.Skip).Take(options.NumToShow).Select(
(ia, i) => (ISeries)(new BarSeries<string>
{
Label = labels?[i],
DependentValues = ia.GetMeans(),
Row = i / options.Subplots.Columns,
Column = i % options.Subplots.Columns
})).ToArray();
break;
case PlotType.ErrorBar:
series = data.Skip(options.Skip).Take(options.NumToShow).Select(
(ia, i) => (ISeries)(new BarSeries<string>
{
Label = labels?[i],
DependentValues = ia.GetMeans(),
ErrorValues = ia.GetStandardDeviations(),
Row = i / options.Subplots.Columns,
Column = i % options.Subplots.Columns
})).ToArray();
break;
case PlotType.Line:
series = data.Skip(options.Skip).Take(options.NumToShow).Select(
(ia, i) => (ISeries)(new LineSeries
{
Label = labels?[i],
X = ia.GetMeans(),
Row = i / options.Subplots.Columns,
Column = i % options.Subplots.Columns
})).ToArray();
break;
default:
throw new ArgumentException("Unknonw plot type", nameof(options.PlotType));
}
return series;
}
/// <summary>
/// Reshape the specified flat image.
/// </summary>
/// <param name="imageFlat">Image flat.</param>
public static double[][] Reshape(Vector imageFlat)
{
var sz = (int)Math.Sqrt(imageFlat.Count);
var image = new double[sz][];
for (int i = 0; i < sz; i++)
{
image[i] = imageFlat.SubVector(i * sz, sz).ToArray();
}
return image;
}
}
}