Skip to content

Commit 5effa77

Browse files
format files
1 parent e8e6463 commit 5effa77

File tree

2 files changed

+102
-70
lines changed

2 files changed

+102
-70
lines changed

src/ticks.jl

Lines changed: 101 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ function bounding_order_of_magnitude(xspan::T) where {T}
1111

1212
a = step = 1
1313
while xspan < base^a * one_dt
14-
function bounding_order_of_magnitude(xspan::DT) where DT
15-
one_dt = oneunit(DT)
16-
17-
a = 1
18-
step = 1
19-
while xspan < 10.0^a * one_dt
2014
a -= step
2115
end
2216

@@ -48,7 +42,7 @@ struct Ticks{T} <: AbstractRange{T}
4842
q::Int
4943
z::Int
5044
step::Float64
51-
Ticks{T}(u,q,z) where T = new(u, q, z, q * 10.0^z)
45+
Ticks{T}(u, q, z) where {T} = new(u, q, z, q * 10.0^z)
5246
end
5347

5448
fallback_ticks(x_min::T, x_max::T, k_min, k_max) where {T} = (
@@ -60,33 +54,32 @@ fallback_ticks(x_min::T, x_max::T, k_min, k_max) where {T} = (
6054
)
6155

6256
Base.size(t::Ticks) = size(t.u)
63-
Base.step(t::Ticks{T}) where T = t.step * oneunit(T)
64-
Base.getindex(t::Ticks{T}, i::Integer) where T = round(t.u[i]*t.step; digits=-t.z) * oneunit(T)
57+
Base.step(t::Ticks{T}) where {T} = t.step * oneunit(T)
58+
Base.getindex(t::Ticks{T}, i::Integer) where {T} =
59+
round(t.u[i] * t.step; digits = -t.z) * oneunit(T)
6560
_ticks_str(t::Ticks) = "($(t.q*t.u))*10^$(t.z)"
6661
Base.show(io::IO, t::Ticks{<:AbstractFloat}) = print(io, _ticks_str(t))
67-
Base.show(io::IO, t::Ticks{T}) where T = print(io, _ticks_str(t), " * ", oneunit(T))
68-
62+
Base.show(io::IO, t::Ticks{T}) where {T} = print(io, _ticks_str(t), " * ", oneunit(T))
6963

70-
function restrict_ticks(t::Ticks{T}, from, to) where T
64+
function restrict_ticks(t::Ticks{T}, from, to) where {T}
7165
tickspan = step(t)
7266
u_start = max(first(t.u), ceil(Int64, from / tickspan))
7367
u_end = min(last(t.u), floor(Int64, to / tickspan))
74-
t = Ticks{T}(u_start:u_end,t.q,t.z)
68+
t = Ticks{T}(u_start:u_end, t.q, t.z)
7569

7670
# Fix possible floating-point errors (may occur in division above, or due
7771
# rounding in (::Ticks)[::Int] when endpoints are near a round number)
7872
while u_start <= u_end && t[1] < from
7973
u_start += 1
80-
t = Ticks{T}(u_start:u_end,t.q,t.z)
74+
t = Ticks{T}(u_start:u_end, t.q, t.z)
8175
end
8276
while u_start <= u_end && t[end] > to
8377
u_end -= 1
84-
t = Ticks{T}(u_start:u_end,t.q,t.z)
78+
t = Ticks{T}(u_start:u_end, t.q, t.z)
8579
end
8680
t
8781
end
8882

89-
9083
# Empty catchall
9184
optimize_ticks() = Any[]
9285

@@ -179,27 +172,56 @@ and the variables here are:
179172
* `i`: index of `q` in `Q`.
180173
* `v`: 1 if label range includes 0, 0 otherwise.
181174
"""
182-
function optimize_ticks(x_min::T, x_max::T; extend_ticks::Bool=false,
183-
Q=[(10,1.0), (50, 0.9), (20, 0.7), (25, 0.5), (30, 0.2)],
184-
k_min::Int=2, k_max::Int=10, k_ideal::Int=5,
185-
granularity_weight::Float64=1/4, simplicity_weight::Float64=1/6,
186-
coverage_weight::Float64=1/3, niceness_weight::Float64=1/4,
187-
strict_span=true, span_buffer = nothing) where T
188-
175+
function optimize_ticks(
176+
x_min::T,
177+
x_max::T;
178+
extend_ticks::Bool = false,
179+
Q = [(10, 1.0), (50, 0.9), (20, 0.7), (25, 0.5), (30, 0.2)],
180+
k_min::Int = 2,
181+
k_max::Int = 10,
182+
k_ideal::Int = 5,
183+
granularity_weight::Float64 = 1 / 4,
184+
simplicity_weight::Float64 = 1 / 6,
185+
coverage_weight::Float64 = 1 / 3,
186+
niceness_weight::Float64 = 1 / 4,
187+
strict_span = true,
188+
span_buffer = nothing,
189+
) where {T}
189190
Qv = [(Int(q[1]), Float64(q[2])) for q in Q]
190-
optimize_ticks_typed(x_min, x_max, extend_ticks, Qv, k_min, k_max, k_ideal,
191-
granularity_weight, simplicity_weight,
192-
coverage_weight, niceness_weight, strict_span, span_buffer)
191+
optimize_ticks_typed(
192+
x_min,
193+
x_max,
194+
extend_ticks,
195+
Qv,
196+
k_min,
197+
k_max,
198+
k_ideal,
199+
granularity_weight,
200+
simplicity_weight,
201+
coverage_weight,
202+
niceness_weight,
203+
strict_span,
204+
span_buffer,
205+
)
193206
end
194207

195-
function optimize_ticks_typed(x_min::T, x_max::T, extend_ticks,
196-
Q::Vector{Tuple{Int,Float64}}, k_min,
197-
k_max, k_ideal,
198-
granularity_weight::Float64, simplicity_weight::Float64,
199-
coverage_weight::Float64, niceness_weight::Float64,
200-
strict_span, span_buffer) where T
208+
function optimize_ticks_typed(
209+
x_min::T,
210+
x_max::T,
211+
extend_ticks,
212+
Q::Vector{Tuple{Int,Float64}},
213+
k_min,
214+
k_max,
215+
k_ideal,
216+
granularity_weight::Float64,
217+
simplicity_weight::Float64,
218+
coverage_weight::Float64,
219+
niceness_weight::Float64,
220+
strict_span,
221+
span_buffer,
222+
) where {T}
201223
one_t = oneunit(T)
202-
if x_max - x_min < eps()*one_t
224+
if x_max - x_min < eps() * one_t
203225
R = typeof(1.0 * one_t)
204226
return R[x_min], x_min - one_t, x_min + one_t
205227
end
@@ -233,11 +255,11 @@ function optimize_ticks_typed(
233255
high_score = -Inf
234256
best_ticks = nothing
235257

236-
max_q_exponent = ceil(Int,log10(maximum(q[1] for q in Q)))
237-
while 2k_max * 10.0^(z+max_q_exponent) * one_t > xspan
238-
for (ik, k) in enumerate(k_min:2k_max)
258+
max_q_exponent = ceil(Int, log10(maximum(q[1] for q in Q)))
259+
while 2k_max * 10.0^(z + max_q_exponent) * one_t > xspan
260+
for (ik, k) in enumerate(k_min:(2k_max))
239261
for (q, qscore) in Q
240-
stp = q*10.0^z
262+
stp = q * 10.0^z
241263
if stp < eps()
242264
continue
243265
end
@@ -250,51 +272,52 @@ function optimize_ticks_typed(
250272

251273
r = ceil(Int64, (x_max - span) / tickspan)
252274

253-
while r*tickspan <= x_min
254-
u = extend_ticks ? (r-k:r+2k-1) : (r:r+k-1)
275+
while r * tickspan <= x_min
276+
u = extend_ticks ? ((r - k):(r + 2k - 1)) : (r:(r + k - 1))
255277
ticks = Ticks{T}(u, q, z)
256278

257279
if strict_span
258-
viewmin = max(r*tickspan, x_min)
259-
viewmax = min((r+k-1)*tickspan, x_max)
280+
viewmin = max(r * tickspan, x_min)
281+
viewmax = min((r + k - 1) * tickspan, x_max)
260282
buf = something(span_buffer, 0) * (viewmax - viewmin)
261283

262-
ticks = restrict_ticks(ticks,viewmin-buf,viewmax+buf)
284+
ticks = restrict_ticks(ticks, viewmin - buf, viewmax + buf)
263285
if length(ticks) < k_min
264286
r += 1
265287
continue
266288
end
267289
end
268290
nticks = length(ticks)
269291

270-
# evaluate quality of ticks
271-
has_zero = r <= 0 && abs(r) < k
292+
# evaluate quality of ticks
293+
has_zero = r <= 0 && abs(r) < k
272294

273-
# simplicity
274-
s = has_zero && nice_scale ? 1 : 0
295+
# simplicity
296+
s = has_zero && nice_scale ? 1 : 0
275297

276-
# granularity
277-
g = 0 < len < 2k_ideal ? 1 - abs(len - k_ideal) / k_ideal : F(0)
298+
# granularity
299+
g = 0 < len < 2k_ideal ? 1 - abs(len - k_ideal) / k_ideal : F(0)
278300

279301
# granularity
280302
g = 0 < nticks < 2k_ideal ? 1 - abs(nticks - k_ideal) / k_ideal : 0.0
281303

282304
# coverage
283-
effective_span = (nticks-1) * tickspan
284-
c = 1.5 * xspan/effective_span
285-
286-
score = granularity_weight * g +
287-
simplicity_weight * s +
288-
coverage_weight * c +
289-
niceness_weight * qscore
290-
291-
# strict limits on coverage
292-
if strict_span && span > xspan
293-
score -= 10000
294-
end
295-
if span >= 2xspan
296-
score -= 1000
297-
end
305+
effective_span = (nticks - 1) * tickspan
306+
c = 1.5 * xspan / effective_span
307+
308+
score =
309+
granularity_weight * g +
310+
simplicity_weight * s +
311+
coverage_weight * c +
312+
niceness_weight * qscore
313+
314+
# strict limits on coverage
315+
if strict_span && span > xspan
316+
score -= 10000
317+
end
318+
if span >= 2xspan
319+
score -= 1000
320+
end
298321

299322
if score > high_score && (k_min <= nticks <= k_max)
300323
best_ticks = ticks
@@ -309,12 +332,21 @@ function optimize_ticks_typed(
309332
if best_ticks === nothing
310333
if strict_span
311334
@warn("No strict ticks found")
312-
return optimize_ticks_typed(x_min, x_max, extend_ticks,
313-
Q, k_min,
314-
k_max, k_ideal,
315-
granularity_weight, simplicity_weight,
316-
coverage_weight, niceness_weight,
317-
false, span_buffer)
335+
return optimize_ticks_typed(
336+
x_min,
337+
x_max,
338+
extend_ticks,
339+
Q,
340+
k_min,
341+
k_max,
342+
k_ideal,
343+
granularity_weight,
344+
simplicity_weight,
345+
coverage_weight,
346+
niceness_weight,
347+
false,
348+
span_buffer,
349+
)
318350
else
319351
R = typeof(1.0 * one_t)
320352
return R[x_min], x_min - one_t, x_min + one_t

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ end
114114
ticks, = optimize_ticks(x, y)
115115
@test issorted(ticks)
116116
@test allunique(ticks)
117-
if (x,y) [(1.0,1.0+eps()), (1.0-eps(),1.0)] # known failures
117+
if (x, y) [(1.0, 1.0 + eps()), (1.0 - eps(), 1.0)] # known failures
118118
@test_broken all(x .<= ticks .<= y)
119119
else
120120
@test all(x .<= ticks .<= y)

0 commit comments

Comments
 (0)