-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
utilities.jl
323 lines (275 loc) · 9.93 KB
/
utilities.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
function to_image(image::AbstractMatrix{<: AbstractFloat}, colormap::AbstractVector{<: Colorant}, colorrange)
return interpolated_getindex.((to_value(colormap),), image, (to_value(colorrange),))
end
"""
resample(A::AbstractVector, len::Integer)
Resample a vector with linear interpolation to have length `len`
"""
function resample(A::AbstractVector, len::Integer)
length(A) == len && return A
return interpolated_getindex.((A,), range(0.0, stop=1.0, length=len))
end
"""
resample_cmap(cmap, ncolors::Integer; alpha=1.0)
* cmap: anything that `to_colormap` accepts
* ncolors: number of desired colors
* alpha: additional alpha applied to each color. Can also be an array, matching `colors`, or a tuple giving a start + stop alpha value.
"""
function resample_cmap(cmap, ncolors::Integer; alpha=1.0)
cols = to_colormap(cmap)
r = range(0.0, stop=1.0, length=ncolors)
if alpha isa Tuple{<:Number, <:Number}
alphas = LinRange(alpha..., ncolors)
else
alphas = alpha
end
return broadcast(r, alphas) do i, a
c = interpolated_getindex(cols, i)
return RGBAf(Colors.color(c), Colors.alpha(c) * a)
end
end
"""
resampled_colors(attributes::Attributes, levels::Integer)
Resample the color attribute from `attributes`. Resamples `:colormap` if present,
or repeats `:color`.
"""
function resampled_colors(attributes, levels::Integer)
cols = if haskey(attributes, :color)
c = get_attribute(attributes, :color)
c isa AbstractVector ? resample(c, levels) : repeated(c, levels)
else
c = get_attribute(attributes, :colormap)
resample(c, levels)
end
end
"""
Like `get!(f, dict, key)` but also calls `f` and replaces `key` when the corresponding
value is nothing
"""
function replace_automatic!(f, dict, key)
haskey(dict, key) || return (dict[key] = f())
val = dict[key]
to_value(val) == automatic && return (dict[key] = f())
val
end
is_unitrange(x) = (false, 0:0)
is_unitrange(x::AbstractRange) = (true, x)
function is_unitrange(x::AbstractVector)
length(x) < 2 && return false, 0:0
diff = x[2] - x[1]
length(x) < 3 && return true, x[1]:x[2]
last = x[3]
for elem in drop(x, 3)
diff2 = elem - last
diff2 != diff && return false, 0:0
end
return true, range(first(x), diff, length(x))
end
function ngrid(x::AbstractVector, y::AbstractVector)
xgrid = [Float32(x[i]) for i = 1:length(x), j = 1:length(y)]
ygrid = [Float32(y[j]) for i = 1:length(x), j = 1:length(y)]
xgrid, ygrid
end
function nan_extrema(array)
mini, maxi = (Inf, -Inf)
for elem in array
isnan(elem) && continue
mini = min(mini, elem)
maxi = max(maxi, elem)
end
Vec2f(mini, maxi)
end
function extract_expr(extract_func, dictlike, args)
if args.head != :tuple
error("Usage: args need to be a tuple. Found: $args")
end
expr = Expr(:block)
for elem in args.args
push!(expr.args, :($(esc(elem)) = $(extract_func)($(esc(dictlike)), $(QuoteNode(elem)))))
end
push!(expr.args, esc(args))
expr
end
"""
usage @exctract scene (a, b, c, d)
"""
macro extract(scene, args)
extract_expr(getindex, scene, args)
end
"""
@get_attribute scene (a, b, c, d)
This will extract attribute `a`, `b`, `c`, `d` from `scene` and apply the correct attribute
conversions + will extract the value if it's a signal.
It will make those attributes available as variables and return them as a tuple.
So the above is equal to:
will become:
```julia
begin
a = get_attribute(scene, :a)
b = get_attribute(scene, :b)
c = get_attribute(scene, :c)
(a, b, c)
end
```
"""
macro get_attribute(scene, args)
extract_expr(get_attribute, scene, args)
end
@inline getindex_value(x::Union{Dict,Attributes,AbstractPlot}, key::Symbol) = to_value(x[key])
@inline getindex_value(x, key::Symbol) = to_value(getfield(x, key))
"""
usage @extractvalue scene (a, b, c, d)
will become:
```julia
begin
a = to_value(scene[:a])
b = to_value(scene[:b])
c = to_value(scene[:c])
(a, b, c)
end
```
"""
macro extractvalue(scene, args)
extract_expr(getindex_value, scene, args)
end
attr_broadcast_length(x::NativeFont) = 1 # these are our rules, and for what we do, Vecs are usually scalars
attr_broadcast_length(x::VecTypes) = 1 # these are our rules, and for what we do, Vecs are usually scalars
attr_broadcast_length(x::AbstractArray) = length(x)
attr_broadcast_length(x) = 1
attr_broadcast_length(x::ScalarOrVector) = x.sv isa Vector ? length(x.sv) : 1
attr_broadcast_getindex(x::NativeFont, i) = x # these are our rules, and for what we do, Vecs are usually scalars
attr_broadcast_getindex(x::VecTypes, i) = x # these are our rules, and for what we do, Vecs are usually scalars
attr_broadcast_getindex(x::AbstractArray, i) = x[i]
attr_broadcast_getindex(x, i) = x
attr_broadcast_getindex(x::ScalarOrVector, i) = x.sv isa Vector ? x.sv[i] : x.sv
is_vector_attribute(x::AbstractArray) = true
is_vector_attribute(x::NativeFont) = false
is_vector_attribute(x::VecTypes) = false
is_vector_attribute(x) = false
is_scalar_attribute(x) = !is_vector_attribute(x)
"""
broadcast_foreach(f, args...)
Like broadcast but for foreach. Doesn't care about shape and treats Tuples && StaticVectors as scalars.
This method is meant for broadcasting across attributes that can either have scalar or vector / array form.
An example would be a collection of scatter markers that have different sizes but a single color.
The length of an attribute is determined with `attr_broadcast_length` and elements are accessed with
`attr_broadcast_getindex`.
"""
function broadcast_foreach(f, args...)
lengths = attr_broadcast_length.(args)
maxlen = maximum(lengths)
# all non scalars should have same length
if any(x -> !(x in (0, 1, maxlen)), lengths)
error("All non scalars need same length, Found lengths for each argument: $lengths, $(typeof.(args))")
end
# skip if there's a zero length element (like an empty annotations collection, etc)
# this differs from standard broadcasting logic in which all non-scalar shapes have to match
0 in lengths && return
for i in 1:maxlen
f(attr_broadcast_getindex.(args, i)...)
end
return
end
"""
from_dict(::Type{T}, dict)
Creates the type `T` from the fields in dict.
Automatically converts to the correct types.
"""
function from_dict(::Type{T}, dict) where T
T(map(fieldnames(T)) do name
convert(fieldtype(T, name), dict[name])
end...)
end
same_length_array(array, value::NativeFont) = repeated(value, length(array))
same_length_array(array, value) = repeated(value, length(array))
function same_length_array(arr, value::Vector)
if length(arr) != length(value)
error("Array lengths do not match. Found: $(length(arr)) of $(eltype(arr)) but $(length(value)) $(eltype(value))")
end
value
end
same_length_array(arr, value, key) = same_length_array(arr, convert_attribute(value, key))
function to_ndim(T::Type{<: VecTypes{N,ET}}, vec::VecTypes{N2}, fillval) where {N,ET,N2}
T(ntuple(Val(N)) do i
i > N2 && return ET(fillval)
@inbounds return vec[i]
end)
end
dim3(x) = ntuple(i -> x, Val(3))
dim3(x::NTuple{3,Any}) = x
dim2(x) = ntuple(i -> x, Val(2))
dim2(x::NTuple{2,Any}) = x
lerp(a::T, b::T, val::AbstractFloat) where {T} = (a .+ (val * (b .- a)))
function merged_get!(defaults::Function, key, scene, input::Vector{Any})
return merged_get!(defaults, key, scene, Attributes(input))
end
function merged_get!(defaults::Function, key, scene::SceneLike, input::Attributes)
d = defaults()
if haskey(theme(scene), key)
# we need to merge theme(scene) with the defaults, because it might be an incomplete theme
# TODO have a mark that says "theme uncomplete" and only then get the defaults
d = merge!(to_value(theme(scene, key)), d)
end
return merge!(input, d)
end
to_vector(x::AbstractVector, len, T) = convert(Vector{T}, x)
function to_vector(x::AbstractArray, len, T)
if length(x) in size(x) # assert that just one dim != 1
to_vector(vec(x), len, T)
else
error("Can't convert to a Vector. Please supply a range/vector/interval")
end
end
function to_vector(x::ClosedInterval, len, T)
a, b = T.(extrema(x))
range(a, stop=b, length=len)
end
"""
A colorsampler maps numnber values from a certain range to values of a colormap
```
x = ColorSampler(colormap, (0.0, 1.0))
x[0.5] # returns color at half point of colormap
```
"""
struct ColorSampler{Data <: AbstractArray}
colormap::Data
color_range::Tuple{Float64,Float64}
end
function Base.getindex(cs::ColorSampler, value::Number)
return interpolated_getindex(cs.colormap, value, cs.color_range)
end
# This function was copied from GR.jl,
# written by Josef Heinen.
"""
peaks([n=49])
Return a nonlinear function on a grid. Useful for test cases.
"""
function peaks(n=49)
x = LinRange(-3, 3, n)
y = LinRange(-3, 3, n)
3 * (1 .- x').^2 .* exp.(-(x'.^2) .- (y .+ 1).^2) .- 10 * (x' / 5 .- x'.^3 .- y.^5) .* exp.(-x'.^2 .- y.^2) .- 1 / 3 * exp.(-(x' .+ 1).^2 .- y.^2)
end
get_dim(x, ind, dim, size) = get_dim(LinRange(extrema(x)..., size[dim]), ind, dim, size)
get_dim(x::AbstractVector, ind, dim, size) = x[Tuple(ind)[dim]]
get_dim(x::AbstractMatrix, ind, dim, size) = x[ind]
"""
surface_normals(x, y, z)
Normals for a surface defined on the grid xy
"""
function surface_normals(x, y, z)
function normal(i)
i1, imax = CartesianIndex(1, 1), CartesianIndex(size(z))
ci(x, y) = min(max(i + CartesianIndex(x, y), i1), imax)
of = (ci(-1, -1), ci(1, -1), ci(-1, 1), ci(1, 1))
function offsets(off)
s = size(z)
return Vec3f(get_dim(x, off, 1, s), get_dim(y, off, 2, s), z[off])
end
return normalize(mapreduce(offsets, +, init=Vec3f(0), of))
end
return vec(map(normal, CartesianIndices(z)))
end
function attribute_names(PlotType)
# TODO, have all plot types store their attribute names
return keys(default_theme(nothing, PlotType))
end