forked from GiovineItalia/Gadfly.jl
/
statistics.jl
1122 lines (902 loc) · 31.8 KB
/
statistics.jl
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Stat
import Gadfly
import StatsBase
import Contour
using Color
using Compose
using DataArrays
using DataStructures
using Hexagons
using Loess
import Gadfly: Scale, Coord, element_aesthetics, default_scales, isconcrete,
nonzero_length, setfield!
import StatsBase: bandwidth, kde
import Distributions: Uniform
import Iterators: chain, cycle, product, partition, distinct
include("bincount.jl")
# Apply a series of statistics.
#
# Args:
# stats: Statistics to apply in order.
# scales: Scales used by the plot.
# aes: A Aesthetics instance.
#
# Returns:
# Nothing, modifies aes.
#
function apply_statistics(stats::Vector{Gadfly.StatisticElement},
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
for stat in stats
apply_statistic(stat, scales, coord, aes)
end
nothing
end
immutable Nil <: Gadfly.StatisticElement
end
const nil = Nil
immutable Identity <: Gadfly.StatisticElement
end
function apply_statistic(stat::Identity,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
nothing
end
const identity = Identity
immutable HistogramStatistic <: Gadfly.StatisticElement
minbincount::Int
maxbincount::Int
orientation::Symbol
function HistogramStatistic(; bincount=nothing,
minbincount=3,
maxbincount=150,
orientation::Symbol=:vertical)
if bincount != nothing
new(bincount, bincount, orientation)
else
new(minbincount, maxbincount, orientation)
end
end
end
element_aesthetics(::HistogramStatistic) = [:x]
const histogram = HistogramStatistic
function apply_statistic(stat::HistogramStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
if stat.orientation == :horizontal
var = :y
othervar = :x
minvar = :ymin
maxvar = :ymax
drawmaxvar = :xdrawmax
labelvar = :x_label
else
var = :x
othervar = :y
minvar = :xmin
maxvar = :xmax
drawmaxvar = :ydrawmax
labelvar = :y_label
end
Gadfly.assert_aesthetics_defined("HistogramStatistic", aes, var)
values = getfield(aes, var)
if stat.minbincount > stat.maxbincount
error("Histogram minbincount > maxbincount")
end
if isempty(getfield(aes, var))
setfield!(aes, minvar, Float64[1.0])
setfield!(aes, maxvar, Float64[1.0])
setfield!(aes, othervar, Float64[0.0])
return
end
if haskey(scales, var) && isa(scales[var], Scale.DiscreteScale)
x_min = minimum(values)
x_max = maximum(values)
d = x_max - x_min + 1
bincounts = zeros(Int, d)
for x in values
bincounts[x - x_min + 1] += 1
end
else
value_set = Set(values[Bool[Gadfly.isconcrete(v) for v in values]])
if length(value_set) / length(values) < 0.9
d, bincounts = choose_bin_count_simple(values, value_set)
else
d, bincounts = choose_bin_count_1d(values,
stat.minbincount,
stat.maxbincount)
end
end
x_min = Gadfly.concrete_minimum(values)
x_max = Gadfly.concrete_maximum(values)
binwidth = (x_max - x_min) / d
if aes.color === nothing
setfield!(aes, minvar, Array(Float64, d))
setfield!(aes, maxvar, Array(Float64, d))
setfield!(aes, othervar, Array(Float64, d))
for j in 1:d
getfield(aes, minvar)[j] = x_min + (j - 1) * binwidth
getfield(aes, maxvar)[j] = x_min + j * binwidth
getfield(aes, othervar)[j] = bincounts[j]
end
else
groups = Dict()
for (x, c) in zip(values, cycle(aes.color))
if !Gadfly.isconcrete(x)
continue
end
if !haskey(groups, c)
groups[c] = Float64[x]
else
push!(groups[c], x)
end
end
setfield!(aes, minvar, Array(Float64, d * length(groups)))
setfield!(aes, maxvar, Array(Float64, d * length(groups)))
setfield!(aes, othervar, Array(Float64, d * length(groups)))
colors = Array(ColorValue, d * length(groups))
x_min = Gadfly.concrete_minimum(values)
x_max = Gadfly.concrete_maximum(values)
stack_height = zeros(Int, d)
for (i, (c, xs)) in enumerate(groups)
fill!(bincounts, 0)
for x in xs
if !Gadfly.isconcrete(x)
continue
end
bin = max(1, min(d, int(ceil((x - x_min) / binwidth))))
bincounts[bin] += 1
end
stack_height += bincounts[1:d]
for j in 1:d
idx = (i-1)*d + j
getfield(aes, minvar)[idx] = x_min + (j - 1) * binwidth
getfield(aes, maxvar)[idx] = x_min + j * binwidth
getfield(aes, othervar)[idx] = bincounts[j]
colors[idx] = c
end
end
drawmax = float64(maximum(stack_height))
aes_drawmax = getfield(aes, drawmaxvar)
if aes_drawmax === nothing || aes_drawmax < drawmax
setfield!(aes, drawmaxvar, drawmax)
end
aes.color = PooledDataArray(colors)
end
setfield!(aes, labelvar, Scale.identity_formatter)
end
immutable DensityStatistic <: Gadfly.StatisticElement
# Number of points sampled
n::Int
function DensityStatistic(n=300)
new(n)
end
end
const density = DensityStatistic
element_aesthetics(::DensityStatistic) = [:x, :y]
function apply_statistic(stat::DensityStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
if !isdefined(:kde)
error("KDE is currently not available for your version of Julia.")
end
Gadfly.assert_aesthetics_defined("DensityStatistic", aes, :x)
if aes.color === nothing
if !isa(aes.x[1], Real)
error("Kernel density estimation only works on Real types.")
end
x_f64 = collect(Float64, aes.x)
# When will stat.n ever be <= 1? Seems pointless
# certainly its length will always be 1
window = stat.n > 1 ? bandwidth(x_f64) : 0.1
f = kde(x_f64, width=window, npoints=stat.n)
aes.x = f.x
aes.y = f.density
else
groups = Dict()
for (x, c) in zip(aes.x, cycle(aes.color))
if !haskey(groups, c)
groups[c] = Float64[x]
else
push!(groups[c], x)
end
end
colors = Array(ColorValue, 0)
aes.x = Array(Float64, 0)
aes.y = Array(Float64, 0)
for (c, xs) in groups
window = stat.n > 1 ? bandwidth(xs) : 0.1
f = kde(xs, width=window, npoints=stat.n)
append!(aes.x, f.x)
append!(aes.y, f.density)
for _ in 1:length(f.x)
push!(colors, c)
end
end
aes.color = PooledDataArray(colors)
end
aes.y_label = Gadfly.Scale.identity_formatter
end
immutable Histogram2DStatistic <: Gadfly.StatisticElement
xminbincount::Int
xmaxbincount::Int
yminbincount::Int
ymaxbincount::Int
function Histogram2DStatistic(; xbincount=nothing,
xminbincount=3,
xmaxbincount=150,
ybincount=nothing,
yminbincount=3,
ymaxbincount=150)
if xbincount != nothing
xminbincount = xbincount
xmaxbincount = xbincount
end
if ybincount != nothing
yminbincount = ybincount
ymaxbincount = ybincount
end
new(xminbincount, xmaxbincount, yminbincount, ymaxbincount)
end
end
element_aesthetics(::Histogram2DStatistic) = [:x, :y, :color]
default_scales(::Histogram2DStatistic) = [Gadfly.Scale.continuous_color()]
const histogram2d = Histogram2DStatistic
function apply_statistic(stat::Histogram2DStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
Gadfly.assert_aesthetics_defined("Histogram2DStatistic", aes, :x, :y)
x_min, x_max = Gadfly.concrete_minimum(aes.x), Gadfly.concrete_maximum(aes.x)
y_min, y_max = Gadfly.concrete_minimum(aes.y), Gadfly.concrete_maximum(aes.y)
if haskey(scales, :x) && isa(scales[:x], Scale.DiscreteScale)
x_categorial = true
xminbincount = x_max - x_min + 1
xmaxbincount = xminbincount
else
x_categorial = false
xminbincount = stat.xminbincount
xmaxbincount = stat.xmaxbincount
end
if haskey(scales, :y) && isa(scales[:y], Scale.DiscreteScale)
y_categorial = true
yminbincount = y_max - y_min + 1
ymaxbincount = yminbincount
else
y_categorial = false
yminbincount = stat.yminbincount
ymaxbincount = stat.ymaxbincount
end
dy, dx, bincounts = choose_bin_count_2d(aes.x, aes.y,
xminbincount, xmaxbincount,
yminbincount, ymaxbincount)
wx = x_categorial ? 1 : (x_max - x_min) / dx
wy = y_categorial ? 1 : (y_max - y_min) / dx
n = 0
for cnt in bincounts
if cnt > 0
n += 1
end
end
if x_categorial
aes.x = Array(Int64, n)
else
aes.xmin = Array(Float64, n)
aes.xmax = Array(Float64, n)
end
if y_categorial
aes.y = Array(Int64, n)
else
aes.ymin = Array(Float64, n)
aes.ymax = Array(Float64, n)
end
k = 1
for i in 1:dy, j in 1:dx
cnt = bincounts[i, j]
if cnt > 0
if x_categorial
aes.x[k] = x_min + (j - 1)
else
aes.xmin[k] = x_min + (j - 1) * wx
aes.xmax[k] = x_min + j * wx
end
if y_categorial
aes.y[k] = y_min + (i - 1)
else
aes.ymin[k] = y_min + (i - 1) * wy
aes.ymax[k] = y_min + i * wy
end
k += 1
end
end
@assert k - 1 == n
if !haskey(scales, :color)
error("Histogram2DStatistic requires a color scale.")
end
color_scale = scales[:color]
if !(typeof(color_scale) <: Scale.ContinuousColorScale)
error("Histogram2DStatistic requires a continuous color scale.")
end
aes.color_key_title = "Count"
data = Gadfly.Data()
data.color = Array(Int, n)
k = 1
for cnt in transpose(bincounts)
if cnt > 0
data.color[k] = cnt
k += 1
end
end
if x_categorial
aes.x = PooledDataArray(aes.x)
end
if y_categorial
aes.y = PooledDataArray(aes.y)
end
Scale.apply_scale(color_scale, [aes], data)
nothing
end
# Find reasonable places to put tick marks and grid lines.
immutable TickStatistic <: Gadfly.StatisticElement
in_vars::Vector{Symbol}
out_var::String
# fixed ticks, or nothing
ticks::Union(Nothing, AbstractArray)
end
function xticks(ticks::Union(Nothing, AbstractArray)=nothing)
TickStatistic([:x, :xmin, :xmax, :xdrawmin, :xdrawmax], "x", ticks)
end
function yticks(ticks::Union(Nothing, AbstractArray)=nothing)
TickStatistic(
[:y, :ymin, :ymax, :middle, :lower_hinge, :upper_hinge,
:lower_fence, :upper_fence, :ydrawmin, :ydrawmax], "y", ticks)
end
# Apply a tick statistic.
#
# Args:
# stat: statistic.
# aes: aesthetics.
#
# Returns:
# nothing
#
# Modifies:
# aes
#
function apply_statistic(stat::TickStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
in_group_var = symbol(string(stat.out_var, "group"))
minval, maxval = nothing, nothing
if getfield(aes, in_group_var) === nothing
in_values = {}
categorical = true
for var in stat.in_vars
vals = getfield(aes, var)
if vals != nothing && !isa(vals, PooledDataArray)
categorical = false
end
if vals != nothing
if minval == nothing
minval = first(vals)
end
if maxval == nothing
maxval = first(vals)
end
T = isempty(vals) ? eltype(vals) : typeof(first(vals))
if stat.out_var == "x"
dsize = aes.xsize === nothing ? [nothing] : aes.xsize
elseif stat.out_var == "y"
dsize = aes.ysize === nothing ? [nothing] : aes.ysize
else
dsize = [nothing]
end
size = aes.size === nothing ? [nothing] : aes.size
minval, maxval = apply_statistic_typed(minval, maxval, vals, size, dsize)
push!(in_values, vals)
end
end
if isempty(in_values)
return
end
in_values = chain(in_values...)
else
vals = getfield(aes, in_group_var)
in_values = vals
minval = Gadfly.concrete_minimum(in_values)
maxval = Gadfly.concrete_maximum(in_values)
categorical = true
end
# consider forced tick marks
if stat.ticks != nothing
minval = min(minval, minimum(stat.ticks))
maxval = max(maxval, maximum(stat.ticks))
end
# TODO: handle the outliers aesthetic
n = Gadfly.concrete_length(in_values)
# take into account a forced viewport in cartesian coordinates.
if typeof(coord) == Coord.Cartesian
if stat.out_var == "x"
if !is(coord.xmin, nothing)
minval = min(minval, coord.xmin)
end
if !is(coord.xmax, nothing)
maxval = max(maxval, coord.xmax)
end
elseif stat.out_var == "y"
if !is(coord.ymin, nothing)
minval = min(minval, coord.ymin)
end
if !is(coord.ymax, nothing)
maxval = max(maxval, coord.ymax)
end
end
end
# check the x/yviewmin/max pesudo-aesthetics
if stat.out_var == "x"
if aes.xviewmin != nothing
minval = aes.xviewmin
end
if aes.xviewmax != nothing
maxval = aes.xviewmax
end
elseif stat.out_var == "y"
if aes.yviewmin != nothing
minval = aes.yviewmin
end
if aes.yviewmax != nothing
maxval = aes.yviewmax
end
end
# all the input values in order.
if stat.ticks != nothing
grids = ticks = stat.ticks
viewmin = minval
viewmax = maxval
tickvisible = fill(true, length(ticks))
tickscale = fill(1.0, length(ticks))
elseif categorical
ticks = Set()
for val in in_values
push!(ticks, val)
end
ticks = Float64[t for t in ticks]
sort!(ticks)
grids = (ticks .- 0.5)[2:end]
viewmin = minimum(ticks)
viewmax = maximum(ticks)
tickvisible = fill(true, length(ticks))
tickscale = fill(1.0, length(ticks))
else
minval, maxval = promote(minval, maxval)
ticks, viewmin, viewmax =
Gadfly.optimize_ticks(minval, maxval, extend_ticks=true)
grids = ticks
multiticks = Gadfly.multilevel_ticks(viewmin - (viewmax - viewmin),
viewmax + (viewmax - viewmin))
tickcount = length(ticks) + sum([length(ts) for ts in values(multiticks)])
tickvisible = Array(Bool, tickcount)
tickscale = Array(Float64, tickcount)
i = 1
for t in ticks
tickscale[i] = 1.0
tickvisible[i] = viewmin <= t <= viewmax
i += 1
end
for (scale, ts) in multiticks
for t in ts
push!(ticks, t)
tickvisible[i] = false
tickscale[i] = scale
i += 1
end
end
end
# We use the first label function we find for any of the aesthetics. I'm not
# positive this is the right thing to do, or would would be.
labeler = getfield(aes, symbol(string(stat.out_var, "_label")))
setfield!(aes, symbol(string(stat.out_var, "tick")), ticks)
setfield!(aes, symbol(string(stat.out_var, "grid")), grids)
setfield!(aes, symbol(string(stat.out_var, "tick_label")), labeler)
setfield!(aes, symbol(string(stat.out_var, "tickvisible")), tickvisible)
setfield!(aes, symbol(string(stat.out_var, "tickscale")), tickscale)
viewmin_var = symbol(string(stat.out_var, "viewmin"))
if getfield(aes, viewmin_var) === nothing ||
getfield(aes, viewmin_var) > viewmin
setfield!(aes, viewmin_var, viewmin)
end
viewmax_var = symbol(string(stat.out_var, "viewmax"))
if getfield(aes, viewmax_var) === nothing ||
getfield(aes, viewmax_var) < viewmax
setfield!(aes, viewmax_var, viewmax)
end
nothing
end
function apply_statistic_typed(minval, maxval, vals, size, dsize)
# for (val, s, ds) in zip(vals, cycle(size), cycle(dsize))
lensize = length(size)
lendsize = length(dsize)
for (i, val) in enumerate(vals)
if !Gadfly.isconcrete(val) || !isfinite(val)
continue
end
s = size[mod1(i, lensize)]
ds = dsize[mod1(i, lendsize)]
minval, maxval = minvalmaxval(minval, maxval, val, s, ds)
end
minval, maxval
end
function apply_statistic_typed{T}(minval, maxval, vals::DataArray{T}, size, dsize)
# for (val, s, ds) in zip(vals, cycle(size), cycle(dsize))
lensize = length(size)
lendsize = length(dsize)
for i = 1:length(vals)
if vals.na[i]
continue
end
val::T = vals.data[i]
s = size[mod1(i, lensize)]
ds = dsize[mod1(i, lendsize)]
minval, maxval = minvalmaxval(minval, maxval, val, s, ds)
end
minval, maxval
end
function minvalmaxval{T}(minval::T, maxval::T, val, s, ds)
if val < minval || !isfinite(minval)
minval = val
end
if val > maxval || !isfinite(maxval)
maxval = val
end
if s != nothing
minval = min(minval, val - s)::T
maxval = max(maxval, val + s)::T
end
if ds != nothing
minval = min(minval, val - ds)::T
maxval = max(maxval, val + ds)::T
end
minval, maxval
end
immutable BoxplotStatistic <: Gadfly.StatisticElement
end
element_aesthetics(::BoxplotStatistic) = [:x, :y]
const boxplot = BoxplotStatistic
function apply_statistic(stat::BoxplotStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
if aes.y === nothing
Gadfly.assert_aesthetics_defined("BoxplotStatistic", aes,
:x, :lower_hinge, :upper_hinge, :lower_fence, :upper_fence)
aes_color = aes.color === nothing ? [nothing] : aes.color
groups = {}
for (x, c) in zip(aes.x, cycle(aes_color))
push!(groups, (x, c))
end
if !is(aes.color, nothing)
aes.color = PooledDataArray(ColorValue[c for (x, c) in groups],
levels(aes.color))
end
return
end
aes_x = aes.x === nothing ? [nothing] : aes.x
aes_color = aes.color === nothing ? [nothing] : aes.color
T = isempty(aes.y) ? eltype(aes.y) : typeof(aes.y[1] / 1)
groups = DefaultOrderedDict(() -> T[])
for (x, y, c) in zip(cycle(aes_x), aes.y, cycle(aes_color))
push!(groups[(x, c)], y)
end
if aes.y != nothing
m = length(groups)
aes.x = Array(eltype(aes.x), m)
aes.middle = Array(T, m)
aes.lower_hinge = Array(T, m)
aes.upper_hinge = Array(T, m)
aes.lower_fence = Array(T, m)
aes.upper_fence = Array(T, m)
aes.outliers = Vector{T}[]
for (i, ((x, c), ys)) in enumerate(groups)
sort!(ys)
aes.x[i] = x
aes.lower_hinge[i], aes.middle[i], aes.upper_hinge[i] =
quantile!(ys, [0.25, 0.5, 0.75])
iqr = aes.upper_hinge[i] - aes.lower_hinge[i]
idx = searchsortedfirst(ys, aes.lower_hinge[i] - 1.5iqr)
aes.lower_fence[i] = ys[idx]
idx = searchsortedlast(ys, aes.upper_hinge[i] + 1.5iqr)
aes.upper_fence[i] = ys[idx]
push!(aes.outliers,
filter(y -> y < aes.lower_fence[i] || y > aes.upper_fence[i], ys))
end
end
if isa(aes_x, PooledDataArray)
aes.x = PooledDataArray(aes.x, aes_x.pool)
end
if !is(aes.color, nothing)
aes.color = PooledDataArray(ColorValue[c for (x, c) in keys(groups)],
levels(aes.color))
end
nothing
end
immutable SmoothStatistic <: Gadfly.StatisticElement
method::Symbol
smoothing::Float64
function SmoothStatistic(; method::Symbol=:loess, smoothing::Float64=0.75)
new(method, smoothing)
end
end
const smooth = SmoothStatistic
element_aesthetics(::SmoothStatistic) = [:x, :y]
function apply_statistic(stat::SmoothStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
Gadfly.assert_aesthetics_defined("Stat.smooth", aes, :x, :y)
Gadfly.assert_aesthetics_equal_length("Stat.smooth", aes, :x, :y)
if !(stat.method in [:loess,:lm])
error("The only Stat.smooth methods currently supported are loess and lm.")
end
num_steps = 750
if aes.color === nothing
x_min, x_max = minimum(aes.x), maximum(aes.x)
if x_min == x_max
error("Stat.smooth requires more than one distinct x value")
end
local xs, ys
try
# work arround for 0.2
if isa(aes.x, DataArray)
xs = collect(Float64, aes.x)
else
xs = convert(Vector{Float64}, aes.x)
end
if isa(aes.y, DataArray)
ys = collect(Float64, aes.y)
else
ys = convert(Vector{Float64}, aes.y)
end
catch e
error("Stat.loess and Stat.lm require that x and y be bound to arrays of plain numbers.")
end
# loess can't predict points <x_min or >x_max. Make sure that doesn't
# happen through a floating point fluke
nudge = 1e-5 * (x_max - x_min)
aes.x = collect((x_min + nudge):((x_max - x_min) / num_steps):(x_max - nudge))
if stat.method == :loess
aes.y = predict(loess(xs, ys, span=stat.smoothing), aes.x)
elseif stat.method == :lm
lmcoeff = linreg(xs,ys)
aes.y = lmcoeff[2].*aes.x .+ lmcoeff[1]
end
else
groups = Dict()
aes_color = aes.color === nothing ? [nothing] : aes.color
for (x, y, c) in zip(aes.x, aes.y, cycle(aes_color))
if !haskey(groups, c)
groups[c] = (Float64[], Float64[])
end
try
push!(groups[c][1], x)
push!(groups[c][2], y)
catch
error("Stat.loess and Stat.lm require that x and y be bound to arrays of plain numbers.")
end
end
aes.x = Array(Float64, length(groups) * num_steps)
aes.y = Array(Float64, length(groups) * num_steps)
colors = Array(ColorValue, length(groups) * num_steps)
for (i, (c, (xs, ys))) in enumerate(groups)
x_min, x_max = minimum(xs), maximum(xs)
if x_min == x_max
error("Stat.smooth requires more than one distinct x value")
end
nudge = 1e-5 * (x_max - x_min)
steps = collect((x_min + nudge):((x_max - x_min) / num_steps):(x_max - nudge))
if stat.method == :loess
smoothys = predict(loess(xs, ys, span=stat.smoothing), steps)
elseif stat.method == :lm
lmcoeff = linreg(xs,ys)
smoothys = lmcoeff[2].*steps .+ lmcoeff[1]
end
for (j, (x, y)) in enumerate(zip(steps, smoothys))
aes.x[(i - 1) * num_steps + j] = x
aes.y[(i - 1) * num_steps + j] = y
colors[(i - 1) * num_steps + j] = c
end
end
aes.color = PooledDataArray(colors)
end
end
immutable HexBinStatistic <: Gadfly.StatisticElement
xbincount::Int
ybincount::Int
function HexBinStatistic(; xbincount=50, ybincount=50)
new(xbincount, ybincount)
end
end
const hexbin = HexBinStatistic
function apply_statistic(stat::HexBinStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
xmin, xmax = minimum(aes.x), maximum(aes.x)
ymin, ymax = minimum(aes.y), maximum(aes.y)
xspan, yspan = xmax - xmin, ymax - ymin
xsize = xspan / stat.xbincount
ysize = yspan / stat.ybincount
counts = Dict{(Any, Any), Int}()
for (x, y) in zip(aes.x, aes.y)
h = convert(HexagonOffsetOddR, pointhex(x - xmin + xspan/2,
y - ymin + yspan/2,
xsize, ysize))
idx = (h.q, h.r)
if !haskey(counts, idx)
counts[idx] = 1
else
counts[idx] += 1
end
end
N = length(counts)
aes.x = Array(Float64, N)
aes.y = Array(Float64, N)
data = Gadfly.Data()
data.color = Array(Int, N)
k = 1
for (idx, cnt) in counts
x, y = center(HexagonOffsetOddR(idx[1], idx[2]), xsize, ysize,
xmin - xspan/2, ymin - yspan/2)
aes.x[k] = x
aes.y[k] = y
data.color[k] = cnt
k += 1
end
aes.xsize = [xsize]
aes.ysize = [ysize]
color_scale = scales[:color]
if !(typeof(color_scale) <: Scale.ContinuousColorScale)
error("HexBinGeometry requires a continuous color scale.")
end
Scale.apply_scale(color_scale, [aes], data)
end
function default_scales(::HexBinStatistic)
return [Gadfly.Scale.continuous_color()]
end
immutable StepStatistic <: Gadfly.StatisticElement
direction::Symbol
function StepStatistic(; direction::Symbol=:hv)
return new(direction)
end
end
const step = StepStatistic
function element_aesthetics(::StepStatistic)
return [:x, :y]
end
function apply_statistic(stat::StepStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
Gadfly.assert_aesthetics_defined("StepStatistic", aes, :x)
Gadfly.assert_aesthetics_defined("StepStatistic", aes, :y)
Gadfly.assert_aesthetics_equal_length("StepStatistic", aes, :x, :y)
points = collect(zip(aes.x, aes.y))
sort!(points, by=first)
n = length(points)
x_step = Array(eltype(aes.x), 2n - 1)
y_step = Array(eltype(aes.y), 2n - 1)
for i in 1:(2n-1)
if isodd(i)
x_step[i] = points[div(i-1,2)+1][1]
y_step[i] = points[div(i-1,2)+1][2]
elseif stat.direction == :hv
x_step[i] = points[div(i-1,2)+2][1]
y_step[i] = points[div(i-1,2)+1][2]
else
x_step[i] = points[div(i-1,2)+1][1]
y_step[i] = points[div(i-1,2)+2][2]
end
end
aes.x = x_step
aes.y = y_step
end
immutable FunctionStatistic <: Gadfly.StatisticElement
# Number of points to evaluate the function at
num_samples::Int