Repository navigation
Expand file tree
/
Copy pathrows_test.go
More file actions
226 lines (207 loc) · 6.27 KB
/
Copy pathrows_test.go
File metadata and controls
226 lines (207 loc) · 6.27 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
package figure_test
// Row identity, end to end: a hit can name the source row behind the mark it
// landed on, on every geom that has one — through decimation, across a gap,
// and out of the cut faceting makes.
import (
"math"
"testing"
"github.com/timzifer/figure"
"github.com/timzifer/figure/facet"
"github.com/timzifer/figure/geom"
"github.com/timzifer/figure/internal/irtest"
"github.com/timzifer/figure/ir"
"github.com/timzifer/figure/scale"
)
func TestRowsDecimated(t *testing.T) {
const n = 200_000
x := make([]float64, n)
y := make([]float64, n)
for i := range n {
x[i] = float64(i)
y[i] = math.Sin(float64(i) / 500)
}
y[n/3] = 5
src := figure.Float64Columns(map[string][]float64{"x": x, "y": y})
p := figure.New(figure.Size(800, 400))
p.Add(geom.Line(src, geom.X("x"), geom.Y("y")))
rec := irtest.New()
live, _ := p.Live(rec.Target())
defer live.Close()
live.TrackRows(true)
if err := live.Draw(); err != nil {
t.Fatal(err)
}
ix := live.Index()
t.Logf("rows tracked: %d of %d source rows", ix.RowCount(), n)
panel := ix.Panels()[0]
// The spike survives the reduction; the row it reports must be the source
// row, not an index into whatever survived.
at := ir.Point{X: panel.X.Map(float64(n / 3)), Y: panel.Y.Map(5)}
hit, ok := ix.At(at, 4)
if !ok {
t.Fatal("no hit on the spike")
}
if hit.Row != n/3 {
t.Errorf("the spike reports row %d, want the source row %d", hit.Row, n/3)
}
}
func TestRowsWithGaps(t *testing.T) {
nan := math.NaN()
src := figure.Float64Columns(map[string][]float64{
"x": {0, 1, 2, 3, 4, 5},
"y": {1, 2, nan, 4, 5, 6},
})
p := figure.New(figure.Size(400, 300))
p.Add(geom.Line(src, geom.X("x"), geom.Y("y")))
rec := irtest.New()
live, _ := p.Live(rec.Target())
defer live.Close()
live.TrackRows(true)
if err := live.Draw(); err != nil {
t.Fatal(err)
}
ix := live.Index()
panel := ix.Panels()[0]
// Row 4 is in the second segment, which starts at source row 3.
hit, ok := ix.At(ir.Point{X: panel.X.Map(4), Y: panel.Y.Map(5)}, 4)
if !ok {
t.Fatal("no hit")
}
if hit.Row != 4 {
t.Errorf("the mark after the gap reports row %d, want 4", hit.Row)
}
}
func TestRowsInterpolated(t *testing.T) {
nan := math.NaN()
src := figure.Float64Columns(map[string][]float64{
"x": {0, 1, 2, 3, 4},
"y": {1, 2, nan, 4, 5},
})
p := figure.New(figure.Size(400, 300))
p.Add(geom.Line(src, geom.X("x"), geom.Y("y"), geom.OnMissing(geom.Interpolate)))
rec := irtest.New()
live, _ := p.Live(rec.Target())
defer live.Close()
live.TrackRows(true)
if err := live.Draw(); err != nil {
t.Fatal(err)
}
ix := live.Index()
panel := ix.Panels()[0]
if hit, ok := ix.At(ir.Point{X: panel.X.Map(3), Y: panel.Y.Map(4)}, 4); !ok || hit.Row != 3 {
t.Errorf("a measured row reports %d, want 3 (ok=%v)", hit.Row, ok)
}
// The invented point at x=2 has no row, and must not borrow a neighbour's.
if hit, ok := ix.At(ir.Point{X: panel.X.Map(2), Y: panel.Y.Map(3)}, 3); ok && hit.Row != -1 {
t.Errorf("the interpolated point reports row %d, want none", hit.Row)
}
}
func TestRowsFaceted(t *testing.T) {
tbl := figure.NewTable().
Float64("x", []float64{0, 1, 2, 3}).
Float64("y", []float64{10, 20, 30, 40}).
String("g", []string{"a", "b", "a", "b"})
p := figure.New(figure.Size(600, 300))
p.Add(geom.Scatter(tbl, geom.X("x"), geom.Y("y")))
p.Facet(facet.Wrap("g"))
rec := irtest.New()
live, _ := p.Live(rec.Target())
defer live.Close()
live.TrackRows(true)
if err := live.Draw(); err != nil {
t.Fatal(err)
}
ix := live.Index()
if len(ix.Panels()) != 2 {
t.Fatalf("panels = %d", len(ix.Panels()))
}
// Panel "b" holds source rows 1 and 3. Its second point must report 3,
// not 1 — the row of the table that was handed in, not of the cut figure
// made to build the panel.
p1 := ix.Panels()[1]
hit, ok := ix.At(ir.Point{X: p1.X.Map(3), Y: p1.Y.Map(40)}, 4)
if !ok {
t.Fatal("no hit in the second panel")
}
if hit.Row != 3 {
t.Errorf("the faceted mark reports row %d, want the table's row 3", hit.Row)
}
}
func TestRowsOffByDefault(t *testing.T) {
src := figure.Float64Columns(map[string][]float64{"x": {0, 1}, "y": {1, 2}})
p := figure.New(figure.Size(400, 300))
p.Add(geom.Scatter(src, geom.X("x"), geom.Y("y")))
rec := irtest.New()
live, _ := p.Live(rec.Target())
defer live.Close()
if err := live.Draw(); err != nil {
t.Fatal(err)
}
ix := live.Index()
if ix.RowCount() != 0 {
t.Errorf("row tracking is on by default: %d rows recorded", ix.RowCount())
}
panel := ix.Panels()[0]
hit, ok := ix.At(ir.Point{X: panel.X.Map(1), Y: panel.Y.Map(2)}, 0)
if !ok {
t.Fatal("no hit")
}
if hit.Row != -1 {
t.Errorf("Row = %d with tracking off, want -1", hit.Row)
}
}
func TestRowsBoxplotHasNone(t *testing.T) {
tbl := figure.NewTable().
String("g", []string{"a", "a", "a", "a", "b", "b", "b", "b"}).
Float64("v", []float64{1, 2, 3, 40, 5, 6, 7, 8})
p := figure.New(figure.Size(400, 300))
p.X(scale.Ordinal())
p.Add(geom.Boxplot(tbl, geom.X("g"), geom.Y("v")))
rec := irtest.New()
live, _ := p.Live(rec.Target())
defer live.Close()
live.TrackRows(true)
if err := live.Draw(); err != nil {
t.Fatal(err)
}
ix := live.Index()
if ix.RowCount() != 0 {
t.Errorf("a boxplot reported %d rows; a box aggregates and has none", ix.RowCount())
}
}
// benchmarkRows measures what row tracking costs a frame that is already being
// watched: the marks are indexed either way, and this is the position and row
// number kept on top of them.
func benchmarkRows(b *testing.B, track bool) {
onOnePGate(b)
const n = 100_000
x := make([]float64, n)
y := make([]float64, n)
for i := range n {
x[i] = float64(i)
y[i] = math.Sin(float64(i) / 500)
}
src := figure.Float64Columns(map[string][]float64{"x": x, "y": y})
p := figure.New(figure.Size(800, 400))
p.Add(geom.Line(src, geom.X("x"), geom.Y("y")))
live, err := p.Live(irtest.NullTarget())
if err != nil {
b.Fatal(err)
}
defer live.Close()
live.TrackRows(track)
if err := live.Draw(); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; b.Loop(); i++ {
// Move a point, so that every frame is a frame rather than a skip.
y[i%n] = float64(i%7) / 10
if err := live.Draw(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkWatchedFrame(b *testing.B) { benchmarkRows(b, false) }
func BenchmarkWatchedFrameRows(b *testing.B) { benchmarkRows(b, true) }