forked from GiovineItalia/Gadfly.jl
/
misc.jl
560 lines (458 loc) · 12.7 KB
/
misc.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
#Is this usable data?
function isconcrete{T<:Number}(x::T)
!isna(x) && isfinite(x)
end
function isconcrete(x::MathConst)
return true
end
function isconcrete(x)
!isna(x)
end
function isallconcrete(xs)
ans = true
for x in xs
if !isconcrete(x)
ans = false
break
end
end
return ans
end
function concretize(xss::AbstractVector...)
if all(map(isallconcrete, xss))
return xss
end
count = 0
for j in 1:length(xss[1])
for xs in xss
if !isconcrete(xs[j])
@goto next_j1
end
end
count += 1
@label next_j1
end
yss = Array(AbstractVector, length(xss))
for (i, xs) in enumerate(xss)
yss[i] = Array(eltype(xs), count)
end
k = 1
for j in 1:length(xss[1])
for xs in xss
if !isconcrete(xs[j])
@goto next_j2
end
end
for (i, xs) in enumerate(xss)
yss[i][k] = xs[j]
end
k += 1
@label next_j2
end
return tuple(yss...)
end
# How many concrete elements in an iterable
function concrete_length(xs)
n = 0
for x in xs
if isconcrete(x)
n += 1
end
end
n
end
function concrete_length{T}(xs::DataArray{T})
n = 0
for i = 1:length(xs)
if !xs.na[i] && isconcrete(xs.data[i]::T)
n += 1
end
end
n
end
function concrete_length(xs::Iterators.Chain)
n = 0
for obj in xs.xss
n += concrete_length(obj)
end
n
end
function concrete_minimum(xs)
if done(xs, start(xs))
error("argument must not be empty")
end
x_min = first(xs)
for x in xs
if Gadfly.isconcrete(x) && isfinite(x)
x_min = x
break
end
end
for x in xs
if Gadfly.isconcrete(x) && isfinite(x) && x < x_min
x_min = x
end
end
return x_min
end
function concrete_maximum(xs)
if done(xs, start(xs))
error("argument must not be empty")
end
x_max = first(xs)
for x in xs
if Gadfly.isconcrete(x) && isfinite(x)
x_max = x
break
end
end
for x in xs
if Gadfly.isconcrete(x) && isfinite(x) && x > x_max
x_max = x
end
end
return x_max
end
function concrete_minmax{T<:Real}(xs, xmin::T, xmax::T)
if eltype(xs) <: Base.Callable
return xmin, xmax
end
for x in xs
if isconcrete(x)
xT = convert(T, x)
if isnan(xmin) || xT < xmin
xmin = xT
end
if isnan(xmax) || xT > xmax
xmax = xT
end
end
end
xmin, xmax
end
function concrete_minmax{T<:Real, TA}(xs::DataArray{TA}, xmin::T, xmax::T)
for i = 1:length(xs)
if !xs.na[i]
x = xs.data[i]::TA
xT = convert(T, x)
if isnan(xmin) || xT < xmin
xmin = xT
end
if isnan(xmax) || xT > xmax
xmax = xT
end
end
end
xmin, xmax
end
function concrete_minmax{T}(xs, xmin::T, xmax::T)
for x in xs
if isconcrete(x)
xT = convert(T, x)
if xT < xmin
xmin = xT
end
if xT > xmax
xmax = xT
end
end
end
xmin, xmax
end
function concrete_minmax{T, TA}(xs::DataArray{TA}, xmin::T, xmax::T)
for i = 1:length(xs)
if !xs.na[i]
x = xs.data[i]::TA
xT = convert(T, x)
if xT < xmin
xmin = xT
end
if xT > xmax
xmax = xT
end
end
end
xmin, xmax
end
function concrete_minmax{T<:Real}(xs::Iterators.Chain, xmin::T, xmax::T)
for obj in xs.xss
xmin, xmax = concrete_minmax(obj, xmin, xmax)
end
xmin, xmax
end
function nonzero_length(xs)
n = 0
for x in xs
if x != 0
n += 1
end
end
n
end
# Create a new object of type T from a with missing values (i.e., those set to
# nothing) inherited from b.
function inherit{T}(a::T, b::T)
c = copy(a)
inherit!(c, b)
c
end
function inherit!{T}(a::T, b::T)
for field in fieldnames(T)
aval = getfield(a, field)
bval = getfield(b, field)
# TODO: this is a hack to let non-default labelers overide the defaults
if aval === nothing || aval === string || aval == showoff
setfield!(a, field, bval)
elseif typeof(aval) <: Dict && typeof(bval) <: Dict
merge!(aval, getfield(b, field))
end
end
nothing
end
isnothing(u) = is(u, nothing)
issomething(u) = !isnothing(u)
negate(f) = x -> !f(x)
function has{T,N}(xs::AbstractArray{T,N}, y::T)
for x in xs
if x == y
return true
end
end
return false
end
Maybe(T) = Union(T, Nothing)
function lerp(x::Float64, a::Float64, b::Float64)
a + (b - a) * max(min(x, 1.0), 0.0)
end
# Generate a unique id, primarily for assigning IDs to SVG elements.
let next_unique_svg_id_num = 0
global unique_svg_id
function unique_svg_id()
uid = @sprintf("id%d", next_unique_svg_id_num)
next_unique_svg_id_num += 1
uid
end
end
# Remove any markup or whitespace from a string.
function escape_id(s::String)
s = replace(s, r"<[^>]*>", "")
s = replace(s, r"\s", "_")
s
end
# Can a numerical value be treated as an integer
is_int_compatable(::Integer) = true
is_int_compatable{T <: FloatingPoint}(x::T) = abs(x) < maxintfloat(T) && float(int(x)) == x
is_int_compatable(::Any) = false
## Return a DataFrame with x, y column suitable for plotting a function.
#
# Args:
# f: Function/Expression to be evaluated.
# a: Lower bound.
# b: Upper bound.
# n: Number of points to evaluate the function at.
#
# Returns:
# A data frame with "x" and "f(x)" columns.
#
function evalfunc(f::Function, a, b, n)
@assert n > 1
step = (b - a) / (n - 1)
xs = Array(typeof(a + step), n)
for i in 1:n
xs[i] = a + (i-1) * step
end
df = DataFrame(xs, map(f, xs))
# NOTE: 'colnames!' is the older deprecated name. 'names!' was also defined
# but threw an error.
try
names!(df, [:x, :f_x])
catch
colnames!(df, ["x", "f_x"])
end
df
end
# Construct a jscall to store arbitrary data in the element
function jsdata(key::String, value::String, arg::Vector{Measure}=Measure[])
return jscall(
"""
data("$(escape_string(key))", $(value))
""", arg)
end
# Construct jscall properties to store arbitrary data in plotroot elements.
function jsplotdata(key::String, value::String, arg::Vector{Measure}=Measure[])
return jscall(
"""
plotroot().data("$(escape_string(key))", $(value))
""", arg)
end
function svg_color_class_from_label(label::String)
return @sprintf("color_%s", escape_id(label))
end
# TODO: Delete when 0.3 compatibility is dropped
if VERSION < v"0.4-dev"
using Dates
function Showoff.showoff{T <: Union(Date, DateTime)}(ds::AbstractArray{T}, style=:none)
years = Set()
months = Set()
days = Set()
hours = Set()
minutes = Set()
seconds = Set()
for d in ds
push!(years, Dates.year(d))
push!(months, Dates.month(d))
push!(days, Dates.day(d))
push!(hours, Dates.hour(d))
push!(minutes, Dates.minute(d))
push!(seconds, Dates.second(d))
end
all_same_year = length(years) == 1
all_one_month = length(months) == 1 && 1 in months
all_one_day = length(days) == 1 && 1 in days
all_zero_hour = length(hours) == 1 && 0 in hours
all_zero_minute = length(minutes) == 1 && 0 in minutes
all_zero_seconds = length(minutes) == 1 && 0 in minutes
all_zero_milliseconds = length(minutes) == 1 && 0 in minutes
# first label format
label_months = false
label_days = false
f1 = "u d, yyyy"
f2 = ""
if !all_zero_seconds
f2 = "HH:MM:SS.sss"
elseif !all_zero_seconds
f2 = "HH:MM:SS"
elseif !all_zero_hour || !all_zero_minute
f2 = "HH:MM"
else
if !all_one_day
first_label_format = "u d yyyy"
elseif !all_one_month
first_label_format = "u yyyy"
elseif !all_one_day
first_label_format = "yyyy"
end
end
if f2 != ""
first_label_format = string(f1, " ", f2)
else
first_label_format = f1
end
labels = Array(String, length(ds))
labels[1] = Dates.format(ds[1], first_label_format)
d_last = ds[1]
for (i, d) in enumerate(ds[2:end])
if Dates.year(d) != Dates.year(d_last)
if all_one_day && all_one_month
f1 = "yyyy"
elseif all_one_day && !all_one_month
f1 = "u yyyy"
else
f1 = "u d, yyyy"
end
elseif Dates.month(d) != Dates.month(d_last)
f1 = all_one_day ? "u" : "u d"
elseif Dates.day(d) != Dates.day(d_last)
f1 = "d"
else
f1 = ""
end
if f2 != ""
f = string(f1, " ", f2)
elseif f1 != ""
f = f1
else
f = first_label_format
end
labels[i+1] = Dates.format(d, f)
d_last = d
end
return labels
end
else
using Base.Dates
end
# TODO: This is a clusterfuck. I should really just wrap Date types to force
# them to behave how I want.
if !method_exists(/, (Dates.Day, Dates.Day))
/(a::Dates.Day, b::Dates.Day) = a.value / b.value
end
if VERSION < v"0.4.0-dev"
Base.convert(::Type{Dates.Millisecond}, d::Dates.Day) =
Dates.Millisecond(24 * 60 * 60 * 1000 * Dates.value(d))
end
if !method_exists(/, (Dates.Day, Real))
/(a::Dates.Day, b::Real) = Dates.Day(round(Integer, (a.value / b)))
end
/(a::Dates.Day, b::FloatingPoint) = convert(Dates.Millisecond, a) / b
if !method_exists(/, (Dates.Millisecond, Dates.Millisecond))
/(a::Dates.Millisecond, b::Dates.Millisecond) = a.value / b.value
end
if !method_exists(/, (Dates.Millisecond, Real))
/(a::Dates.Millisecond, b::Real) = Dates.Millisecond(round(Integer, (a.value / b)))
end
/(a::Dates.Millisecond, b::FloatingPoint) = Dates.Millisecond(round(Integer, (a.value / b)))
if !method_exists(-, (Dates.Date, Dates.DateTime))
-(a::Dates.Date, b::Dates.DateTime) = convert(Dates.DateTime, a) - b
end
+(a::Dates.Date, b::Dates.Millisecond) = convert(Dates.DateTime, a) + b
if !method_exists(-, (Dates.DateTime, Dates.Date))
-(a::Dates.DateTime, b::Dates.Date) = a - convert(Dates.DateTime, b)
end
if !method_exists(/, (Dates.Day, Dates.Millisecond))
/(a::Dates.Day, b::Dates.Millisecond) = convert(Dates.Millisecond, a) / b
end
for T in [Dates.Hour, Dates.Minute, Dates.Second, Dates.Millisecond]
if !method_exists(-, (Dates.Date, T))
@eval begin
-(a::Dates.Date, b::$(T)) = convert(Dates.DateTime, a) - b
end
end
end
#if !method_exists(*, (FloatingPoint, Dates.Day))
*(a::FloatingPoint, b::Dates.Day) = Dates.Day(round(Integer, (a * b.value)))
*(a::Dates.Day, b::FloatingPoint) = b * a
*(a::FloatingPoint, b::Dates.Millisecond) = Dates.Millisecond(round(Integer, (a * b.value)))
*(a::Dates.Millisecond, b::FloatingPoint) = b * a
#end
# Arbitrarily order colors
function color_isless(a::ColorValue, b::ColorValue)
return color_isless(convert(RGB{Float32}, a), convert(RGB{Float32}, b))
end
function color_isless(a::AlphaColorValue, b::AlphaColorValue)
return color_isless(convert(RGBA{Float32}, a), convert(RGBA{Float32}, b))
end
function color_isless(a::RGB{Float32}, b::RGB{Float32})
if a.r < b.r
return true
elseif a.r == b.r
if a.g < b.g
return true
elseif a.g == b.g
return a.b < b.b
else
return false
end
else
return false
end
end
function color_isless(a::RGBA{Float32}, b::RGBA{Float32})
if a.c < b.c
return true
elseif a.c == b.c
return a.alpha < b.alpha
else
return false
end
end
function group_color_isless{S, T <: ColorValue}(a::(@compat Tuple{S, T}),
b::(@compat Tuple{S, T}))
if a[1] < b[1]
return true
elseif a[1] == b[1]
return color_isless(a[2], b[2])
else
return false
end
end