-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdemotest.d
More file actions
390 lines (332 loc) · 12.8 KB
/
demotest.d
File metadata and controls
390 lines (332 loc) · 12.8 KB
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
/* These are demos/tests for Plot2Kill. They serve both as tests
* and as examples of usage. Most testing is done here, mostly because
* objective, automatically verifiable tests of correctness are hard to come
* by for a plotting library, especially if avoiding testing implementation
* details is also a goal. It's much easier to just generate some plots
* and see if they look right.
*
* Copyright (C) 2010-2012 David Simcha
*
* License:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
version(test):
import std.conv, std.exception, std.algorithm, std.random,
std.traits, std.math, std.array, std.range, std.mathspecial, std.stdio;
import plot2kill.all, plot2kill.util;
version(dfl) {
enum string libName = "DFL";
} else {
enum string libName = "GTK";
}
void main(string[] args)
{
// Test special-case handling of line graphs with perfectly vertical or
// perfectly horizontal lines.
Subplot(2, 2).addFigure(
Figure(
LineGraph([0.0, 3.0], [1.0, 1.0]),
LineGraph([1.0, 1.0], [0.0, 2.0])
),
Figure(LineGraph([0.0, 3.0], [1.0, 1.0])),
Figure(LineGraph([1.0, 1.0], [0.0, 2.0]))
).showAsMain();
// Test dendrogram.
{
auto mat = [[3.0, 1, 4, 1, 5, 9, 2],
[8.0, 6, 7, 5, 3, 0, 9],
[2.0, 7, 1, 8, 2, 8, 1],
[7.0, 1, 2, 6, 9, 1, 3],
[4.0, 1, 8, 3, 0, 9, 3]];
auto names = ["Pi", "80s Song", "e", "Made-up 1", "Made-up 2"];
auto clusters = hierarchicalCluster(mat, ClusterBy.rows, names);
auto dend = Dendrogram(clusters);
dend.toLabeledFigure.showAsMain();
}
// Test linear fit line.
{
auto x = [8,6,7,5,4,0,9];
auto y = [3,1,4,1,5,9,2];
auto scatter = ScatterPlot(x, y);
auto linear = LinearFit(x, y);
Figure(scatter, linear)
.xTickLabels([-0.5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
.showAsMain();
// Test covariance fixes.
auto plotArr = [scatter, scatter];
auto fig2 = Figure(plotArr);
fig2.removePlot(scatter, linear);
fig2.addPlot(scatter, linear);
auto arr2 = [scatter, scatter];
fig2.removePlot(arr2);
auto arr3 = [linear, linear];
fig2.addPlot(arr3);
}
// Test hierarchical heat maps.
{
double[][] matrix = new double[][10];
foreach(ref row; matrix) row = randArray!rNorm(10, 0, 1);
auto rowLabels = to!(string[])(array(iota(10)));
auto colLabels = to!(string[])(array(iota(10)));
auto arr = [-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2];
copy(arr, transversal(matrix, 1));
copy(arr, transversal(matrix, 7));
arr[] += randArray!rNorm(10, 0, 0.5)[];
copy(arr, transversal(matrix, 3));
hierarchicalHeatMap(matrix, rowLabels, colLabels)
.colors([getColor(255, 0, 0), getColor(0, 0, 0), getColor(0, 255, 0)])
.toFigure
.xTickLabels(iota(1, 11), colLabels)
.yTickLabels(iota(1, 11), rowLabels)
.showAsMain();
}
// Test point symbols on line graphs.
{
LineGraph([8,6,7,5,3,0,9]).pointSymbol('O')
.pointColor(getColor(255, 0, 0))
.toFigure.saveToFile("foo.svg");
}
// Test stacked bar plots.
auto stacked = Figure(
stackedBar(iota(3), [[5, 3, 1], [1, 2, 3]], 0.6,
["Coffee", "Tea"]
)
).legendLocation(LegendLocation.right)
.title("Caffeine Consumption")
.xLabel("Time of Day")
.xTickLabels(iota(3), ["Morning", "Afternoon", "Evening"])
.yLabel("Beverages");
// Test removing a plot.
auto fooHist = Histogram(randArray!rNorm(100, 0, 1), 10);
stacked.addPlot(fooHist);
stacked.removePlot(fooHist);
stacked.showAsMain();
// Test grouped bar plots.
auto withoutCaffeine = [8, 6, 3];
auto withCaffeine = [5, 3, 1];
auto sleepinessPlot = groupedBar(
iota(3), [withoutCaffeine, withCaffeine], 0.6,
["W/o Caffeine", "W/ Caffeine"],
[getColor(96, 96, 255), getColor(255, 96, 96)]
);
auto sleepinessFig = Figure(sleepinessPlot)
.title("Sleepiness Survey")
.yLabel("Sleepiness Rating")
.xLabel("Activity")
.legendLocation(LegendLocation.right)
.gridIntensity(cast(ubyte) 100)
.horizontalGrid(true)
.xTickLabels(
iota(3),
["In Meeting", "On Phone", "Coding"]
);
sleepinessFig.showAsMain();
// Test box-and whisker plots.
auto boxFigNorm = BoxPlot(0.05).addData(
randArray!rNorm(100, 0, 1),
randArray!rNorm(100, 0, 0.5),
randArray!rNorm(100, 1, 2)
).legendText("Normal");
auto boxFigNonNorm = BoxPlot(0.05)
.offset(boxFigNorm.nBoxes)
.addData(
randArray!rExponential(100, 0.5),
randArray!uniform(100, -2.0, 2.0) )
.color(getColor(255, 0, 0))
.legendText("Non-Normal");
auto boxFig = Figure(boxFigNorm, boxFigNonNorm)
.rotatedXTick(true)
.legendLocation(LegendLocation.right)
.xTickLabels(iota(5), [
"Normal(0, 1)", "Normal(0, 0.5)", "Normal(1, 2)",
"Exponential(0.5)", "Uniform(-2, 2)"]
);
boxFig.showAsMain();
// Test line graphs and histograms on the same plot.
auto histRand = Histogram(
randArray!rNorm(5_000, 0, 1), 100, -5, 5, OutOfBounds.ignore);
histRand.put(
Histogram(randArray!rNorm(5_000, 0, 1), 100, -5, 5, OutOfBounds.ignore)
);
histRand.legendText = "Empirical";
auto histLine = ContinuousFunction(&stdNormal, -5, 5);
histLine.legendText = "Theoretical";
histRand.scaleDistributionFunction(histLine);
histLine.lineColor = getColor(255, 0, 0);
histLine.lineWidth = 3;
auto hist = Figure(histRand, histLine);
hist.addLines(
FigureLine(-2, 0, -2, hist.topMost, getColor(128, 0, 0), 2),
FigureLine(2, 0, 2, hist.topMost, getColor(128, 0, 0), 2)
);
hist.title = "Normal Distrib.";
hist.xLabel = "Random Variable";
hist.yLabel = "Count";
hist.saveToFile("foo" ~ libName ~ ".png");
hist.saveToFile("foo" ~ libName ~ ".bmp");
hist.showAsMain();
// Test error bars.
auto errs = [0.1, 0.2, 0.3, 0.4];
auto linesWithErrors =
LineGraph([1,2,3,4], [1,2,3,8], errs, errs);
linesWithErrors.lineColor = getColor(255, 0, 0);
auto linesWithErrorsFig = linesWithErrors.toFigure;
linesWithErrorsFig.title = "Error Bars";
linesWithErrorsFig.showAsMain();
// Plot a normal approximation of the binomial distribution superimposed
// on the exact distribution.
auto binomExact =
DiscreteFunction((int x) { return binomialPMF(x, 8, 0.5); }, 0, 8);
binomExact.legendText = "Exact";
auto binomApprox = ContinuousFunction(
(double x) { return stdNormal((x - 4) / SQRT2) / SQRT2; }, -1, 9, 100
);
binomApprox.legendText = "Approx.";
binomApprox.lineWidth = 2;
auto binom = Figure(binomExact, binomApprox);
binom.title = "Binomial";
binom.xLabel = "N Successes";
binom.yLabel = "Probability";
binom.xTickLabels(array(iota(0, 9, 1)));
binom.legendLocation = LegendLocation.top;
binom.xLim(0, 8);
binom.showAsMain();
// Test a basic scatter plot with grid lines.
auto scatter = ScatterPlot(
randArray!rNorm(100, 0, 1),
randArray!rNorm(100, 0, 1)
).legendText("Point").pointSize(10).pointColor(getColor(255, 0, 255)).toFigure;
scatter.xLim(-2, 2);
scatter.yLim(-2, 2);
scatter.verticalGrid = true;
scatter.horizontalGrid = true;
scatter.showAsMain();
// Test error bars with bar plots.
auto bars = BarPlot([1,2,3], [8,7,3], 0.5, [1,2,4], [1,2,4]);
auto barFig = bars.toFigure;
barFig.xTickLabels(bars.centers, ["Plan A", "Plan B", "Plan C"]);
barFig.title = "Useless Plans";
barFig.yLabel = "Screwedness";
barFig.rotatedXTick = true;
barFig.showAsMain();
// Test QQ plots.
auto qq = QQPlot(
randArray!rNorm(100, 0, 1),
&normalDistributionInverse
).toFigure;
qq.title = "Normal(0, 1) Theoretical vs. Actual";
qq.xLabel = "Theoretical Quantile";
qq.yLabel = "Actual Quantile";
qq.showAsMain();
// Test equal frequency histograms.
auto frqHist = FrequencyHistogram(
randArray!rNorm(100_000, 0, 1), 100).toFigure;
frqHist.xLim(-2.5, 2.5);
// Test unique histograms.
auto uniqueHist = UniqueHistogram(
randArray!uniform(10_000, 0, 8)
);
uniqueHist.histType = HistType.Probability;
uniqueHist.barColor = getColor(0, 200, 0);
auto uniqueHistFig = uniqueHist.toLabeledFigure;
uniqueHistFig.title = "Unique Histogram";
uniqueHistFig.showAsMain();
// Test heat scatter plots.
auto heatScatter = HeatScatter(100, 100, -6, 6, -5, 5);
heatScatter.boundsBehavior = OutOfBounds.Ignore;
heatScatter.colors = [getColor(0, 128, 0),
getColor(255, 255, 0), getColor(255, 0, 0), getColor(255, 255, 255)];
foreach(i; 0..500_000) {
auto num1 = rNorm(-2, 1);
auto num2 = rNorm(1, 1);
num1 += num2;
heatScatter.put(num1, num2);
}
auto a1 = randArray!rNorm(500_000, -2, 1);
auto a2 = randArray!rNorm(500_000, 1, 1);
a1[] += a2[];
heatScatter.put(
HeatScatter(a1, a2, 100, 100, -6, 6, -5, 5, OutOfBounds.Ignore)
);
auto heatScatterFig = heatScatter.toFigure
.xLim(-4, 2)
.yLim(-2, 4)
.title("2D Histogram")
.xLabel("Normal(-2, 1) + Y[i]")
.yLabel("Normal(1, 1)");
//heatScatterFig.saveToFile("bar" ~ libName ~ ".png", ".png", 640, 480);
heatScatterFig.showAsMain();
// Test subplots. Put a whole bunch of what we did on one big subplot.
enum string titleStuff = "Plot2Kill " ~ libName ~
" Demo (Programmatically saved, no longer a screenshot)";
enum string subplotY = "Pretty Rotated Text";
auto sp = Subplot().addFigure
(hist, binom, barFig, linesWithErrorsFig, scatter,
qq, frqHist, uniqueHistFig, heatScatterFig, boxFig, sleepinessFig,
stacked)
.title(titleStuff)
.yLabel(subplotY)
.xLabel("Boring X-Axis Label");
// Test saving results to a file.
version(dfl) {
} else {
sp.saveToFile("sp.pdf", 1280, 1024);
sp.saveToFile("sp.svg", 1280, 1024);
}
sp.saveToFile("sp" ~ libName ~ ".bmp", 1280, 1024);
sp.saveToFile("sp" ~ libName ~ ".png", 1280, 1024);
// Test covariance fixes.
auto sp2 = Subplot();
auto figArr = [hist, binom];
sp2.addFigure(figArr);
sp.showAsMain();
}
// Statistical functions for statistics oriented plots. These are mostly
// cut and pasted from my dstats library.
double[] randArray(alias randFun, Args...)(size_t N, auto ref Args args) {
auto ret = uninitializedArray!(double[])(N);
foreach(ref elem; ret) {
elem = randFun(args);
}
return ret;
}
double rNorm(double mean, double sd) {
immutable p = uniform(0.0, 1.0);
return normalDistributionInverse(p) * sd + mean;
}
double rExponential(double lambda) {
double p = uniform(0.0, 1.0);
return -log(p) / lambda;
}
double stdNormal(double x) {
return exp(-(x * x) / 2) / sqrt(2 * PI);
}
double binomialPMF(ulong k, ulong n, double p) {
return exp(logNcomb(n, k) + k * log(p) + (n - k) * log(1 - p));
}
double logNcomb(ulong n, ulong k) {
return logGamma(n + 1) - (logGamma(k + 1) + logGamma(n - k + 1));
}