forked from tdunning/anomaly-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigures.r
95 lines (78 loc) · 2.82 KB
/
figures.r
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
figure = 1
ylim = c(-180, 180)
if (!exists('xtrace')) {
xtrace = read.delim("trace.tsv", header=F)
dict = read.delim("dict.tsv", header=F)
}
# plots three graphs in a single figure. These graphs contain
# 1) the original signal
# 2) the reconstructed signal
# 3) the error
# The offset argument indicates how many samples the plots should be offset into the entire trace
threePlot = function (offset) {
startPdf()
xm = matrix(c(0,1,2,3,0),ncol=3, nrow=5)
xm[,1] = 0
xm[,3] = 0
layout(xm, heights=c(0.2, 1, 1, 1.5, 0.02), widths = c(0.1, 1, 0.1))
par(mar=c(1,1,0,0.2))
plot(xtrace$V1[offset + 1:1000], type='l', ylim=ylim, ylab='', xaxt='n', yaxt='n')
axis(side=2, at=c(-100, 0, 100))
plot(xtrace$V2[offset + 1:1000], type='l', ylim=ylim, ylab='', xaxt='n', yaxt='n')
axis(side=2, at=c(-100, 0, 100))
par(mar=c(5,1,0,0.2))
plot((offset + 1:1000)/200, xtrace$V1[offset + 1:1000] - xtrace$V2[offset + 1:1000], type='l', ylim=ylim, ylab='', yaxt='n',
xlab="Time (s)")
axis(side=2, at=c(-100, 0, 100))
dev.off()
}
errorChart = function(offset) {
startPdf()
plot((offset + 1:1000)/200, xtrace$V1[offset + 1:1000] - xtrace$V2[offset + 1:1000], type='l', xlab="Time (s)", ylab="mV", ylim=ylim,
main=paste(offset/200, " to ", (offset+1000)/200, "seconds"), yaxt='n')
axis(side=2, at=c(-100, 0, 100))
dev.off()
}
originalChart = function(offset, size=1000) {
startPdf()
plot((offset + 1:size)/200, xtrace$V1[offset + 1:size], type='l', xlab="Time (s)", ylab="mV", ylim=ylim,
main=paste(offset/200, " to ", (offset+size)/200, "seconds"), yaxt='n')
axis(side=2, at=c(-100, 0, 100))
dev.off()
}
startPdf = function () {
pdf(sprintf("figure-%02d.pdf", figure), width=6, height=4, pointsize=11)
assign("figure", figure + 1, envir = .GlobalEnv)
}
# these show normal behavior
threePlot(0)
errorChart(0)
errorChart(1000)
errorChart(2000)
# reconstruction error is clearly not normally distributed
error = (xtrace$V1[1:20000]-xtrace$V2[1:20000])
startPdf()
qqnorm(error, cex=0.3, main="Reconstruction error is not normally distributed")
dev.off()
# but it is very tightly constrained
startPdf()
hist(error, breaks=70, main = "Reconstruction Error", xlab="Error (mV)", freq=F)
text(-55, 0.09, bquote(sigma == .(sprintf("%.1f", sd(error)))), adj=c(0,0))
text(-55, 0.08, sprintf("P(error > %.0f) = %.1f%%", 3*sd(error), 100*mean(abs(error) > 3 * sd(error))), adj=c(0,0))
text(-55, 0.07, sprintf("P(error > 100) < %.0f ppm", 1e6/20000), adj=c(0,0))
dev.off()
# at 100 seconds in, we see an anomaly
errorChart(20000)
threePlot(20000)
originalChart(20200, 200)
errorChart(48000)
threePlot(48000)
startPdf()
layout(matrix(1:64, ncol=8))
par(mar=c(0,0,0,0))
ix = order(-table(xtrace$V3))
for (i in ix[1:64]) {
ymax = max(abs(dict[i,]))
plot(t(dict[i,]), type='l', xaxt='n', yaxt='n', ylim=c(-ymax,ymax))
}
dev.off()