Base NaN-compliant functions on NaNMath

replace _min and _max with NaNMath versions
This commit is contained in:
Michael K. Borregaard 2017-05-30 23:44:51 +02:00
parent 75a56d5a12
commit 16ebc2b05b
9 changed files with 32 additions and 31 deletions

View File

@ -106,9 +106,10 @@ export
# --------------------------------------------------------- # ---------------------------------------------------------
import NaNMath
# define functions (e.g. `_extrema`, that ignores NaNs, when the type is applicable. To overcome the destructive effects of https://github.com/JuliaLang/julia/pull/12563 # define functions (e.g. `_extrema`, that ignores NaNs, when the type is applicable. To overcome the destructive effects of https://github.com/JuliaLang/julia/pull/12563
for fun in (:extrema, :minimum, :maximum) for fun in (:extrema, :minimum, :maximum)
@eval $(Symbol(string("_",fun))){F<:AbstractFloat}(x::AbstractArray{F}) = Base.$(fun)(filter(!isnan,x)) @eval $(Symbol(string("_",fun))){F<:AbstractFloat}(x::AbstractArray{F}) = NaNMath.$(fun)(x)
@eval $(Symbol(string("_",fun)))(x) = Base.$(fun)(x) @eval $(Symbol(string("_",fun)))(x) = Base.$(fun)(x)
end end

View File

@ -260,8 +260,8 @@ end
function expand_extrema!(ex::Extrema, v::Number) function expand_extrema!(ex::Extrema, v::Number)
ex.emin = _min(v, ex.emin) ex.emin = NaNMath.min(v, ex.emin)
ex.emax = _max(v, ex.emax) ex.emax = NaNMath.max(v, ex.emax)
ex ex
end end
@ -276,8 +276,8 @@ expand_extrema!(axis::Axis, ::Bool) = axis[:extrema]
function expand_extrema!{MIN<:Number,MAX<:Number}(axis::Axis, v::Tuple{MIN,MAX}) function expand_extrema!{MIN<:Number,MAX<:Number}(axis::Axis, v::Tuple{MIN,MAX})
ex = axis[:extrema] ex = axis[:extrema]
ex.emin = _min(v[1], ex.emin) ex.emin = NaNMath.min(v[1], ex.emin)
ex.emax = _max(v[2], ex.emax) ex.emax = NaNMath.max(v[2], ex.emax)
ex ex
end end
function expand_extrema!{N<:Number}(axis::Axis, v::AVec{N}) function expand_extrema!{N<:Number}(axis::Axis, v::AVec{N})
@ -368,8 +368,8 @@ end
# push the limits out slightly # push the limits out slightly
function widen(lmin, lmax) function widen(lmin, lmax)
span = lmax - lmin span = lmax - lmin
# eps = _max(1e-16, min(1e-2span, 1e-10)) # eps = NaNMath.max(1e-16, min(1e-2span, 1e-10))
eps = _max(1e-16, 0.03span) eps = NaNMath.max(1e-16, 0.03span)
lmin-eps, lmax+eps lmin-eps, lmax+eps
end end
@ -425,7 +425,7 @@ function discrete_value!(axis::Axis, dv)
# @show axis[:discrete_map], axis[:discrete_values], dv # @show axis[:discrete_map], axis[:discrete_values], dv
if cv_idx == -1 if cv_idx == -1
ex = axis[:extrema] ex = axis[:extrema]
cv = _max(0.5, ex.emax + 1.0) cv = NaNMath.max(0.5, ex.emax + 1.0)
expand_extrema!(axis, cv) expand_extrema!(axis, cv)
push!(axis[:discrete_values], dv) push!(axis[:discrete_values], dv)
push!(axis[:continuous_values], cv) push!(axis[:continuous_values], cv)

View File

@ -367,14 +367,14 @@ end
dist(a, b) = abs(a-b) dist(a, b) = abs(a-b)
mindist(x, a, b) = _min(dist(a, x), dist(b, x)) mindist(x, a, b) = NaNMath.min(dist(a, x), dist(b, x))
function gappy(x, ps) function gappy(x, ps)
n = length(ps) n = length(ps)
x <= first(ps) && return first(ps) - x x <= first(ps) && return first(ps) - x
for j=1:(n-1) for j=1:(n-1)
p0 = ps[j] p0 = ps[j]
p1 = ps[_min(j+1, n)] p1 = ps[NaNMath.min(j+1, n)]
if p0 <= x && p1 >= x if p0 <= x && p1 >= x
return mindist(x, p0, p1) * (isodd(j) ? 1 : -1) return mindist(x, p0, p1) * (isodd(j) ? 1 : -1)
end end

View File

@ -341,7 +341,7 @@ end
function gr_set_line(lw, style, c) #, a) function gr_set_line(lw, style, c) #, a)
GR.setlinetype(gr_linetype[style]) GR.setlinetype(gr_linetype[style])
w, h = gr_plot_size w, h = gr_plot_size
GR.setlinewidth(_max(0, lw / ((w + h) * 0.001))) GR.setlinewidth(NaNMath.max(0, lw / ((w + h) * 0.001)))
gr_set_linecolor(c) #, a) gr_set_linecolor(c) #, a)
end end
@ -394,7 +394,7 @@ function gr_viewport_from_bbox(sp::Subplot{GRBackend}, bb::BoundingBox, w, h, vi
viewport[4] = viewport_canvas[4] * (1.0 - top(bb) / h) viewport[4] = viewport_canvas[4] * (1.0 - top(bb) / h)
if is3d(sp) if is3d(sp)
vp = viewport[:] vp = viewport[:]
extent = _min(vp[2] - vp[1], vp[4] - vp[3]) extent = NaNMath.min(vp[2] - vp[1], vp[4] - vp[3])
viewport[1] = 0.5 * (vp[1] + vp[2] - extent) viewport[1] = 0.5 * (vp[1] + vp[2] - extent)
viewport[2] = 0.5 * (vp[1] + vp[2] + extent) viewport[2] = 0.5 * (vp[1] + vp[2] + extent)
viewport[3] = 0.5 * (vp[3] + vp[4] - extent) viewport[3] = 0.5 * (vp[3] + vp[4] - extent)
@ -428,7 +428,7 @@ function gr_set_viewport_polar()
ymax -= 0.05 * (xmax - xmin) ymax -= 0.05 * (xmax - xmin)
xcenter = 0.5 * (xmin + xmax) xcenter = 0.5 * (xmin + xmax)
ycenter = 0.5 * (ymin + ymax) ycenter = 0.5 * (ymin + ymax)
r = 0.5 * _min(xmax - xmin, ymax - ymin) r = 0.5 * NaNMath.min(xmax - xmin, ymax - ymin)
GR.setviewport(xcenter -r, xcenter + r, ycenter - r, ycenter + r) GR.setviewport(xcenter -r, xcenter + r, ycenter - r, ycenter + r)
GR.setwindow(-1, 1, -1, 1) GR.setwindow(-1, 1, -1, 1)
r r

View File

@ -349,7 +349,7 @@ function _inspectdr_setupsubplot(sp::Subplot{InspectDRBackend})
ymin, ymax = axis_limits(yaxis) ymin, ymax = axis_limits(yaxis)
if ispolar(sp) if ispolar(sp)
#Plots.jl appears to give (xmin,xmax) ≜ (Θmin,Θmax) & (ymin,ymax) ≜ (rmin,rmax) #Plots.jl appears to give (xmin,xmax) ≜ (Θmin,Θmax) & (ymin,ymax) ≜ (rmin,rmax)
rmax = _max(abs(ymin), abs(ymax)) rmax = NaNMath.max(abs(ymin), abs(ymax))
xmin, xmax = -rmax, rmax xmin, xmax = -rmax, rmax
ymin, ymax = -rmax, rmax ymin, ymax = -rmax, rmax
end end

View File

@ -926,14 +926,14 @@ function py_compute_axis_minval(axis::Axis)
for series in series_list(sp) for series in series_list(sp)
v = series.d[axis[:letter]] v = series.d[axis[:letter]]
if !isempty(v) if !isempty(v)
minval = _min(minval, _minimum(abs(v))) minval = NaNMath.min(minval, _minimum(abs(v)))
end end
end end
end end
# now if the axis limits go to a smaller abs value, use that instead # now if the axis limits go to a smaller abs value, use that instead
vmin, vmax = axis_limits(axis) vmin, vmax = axis_limits(axis)
minval = _min(minval, abs(vmin), abs(vmax)) minval = NaNMath.min(minval, abs(vmin), abs(vmax))
minval minval
end end
@ -954,7 +954,7 @@ function py_set_scale(ax, axis::Axis)
elseif scale == :log10 elseif scale == :log10
10 10
end end
kw[Symbol(:linthresh,letter)] = _max(1e-16, py_compute_axis_minval(axis)) kw[Symbol(:linthresh,letter)] = NaNMath.max(1e-16, py_compute_axis_minval(axis))
"symlog" "symlog"
end end
func(arg; kw...) func(arg; kw...)
@ -1096,10 +1096,10 @@ function _update_min_padding!(sp::Subplot{PyPlotBackend})
bottompad = 0mm bottompad = 0mm
for bb in (py_bbox_axis(ax, "x"), py_bbox_axis(ax, "y"), py_bbox_title(ax)) for bb in (py_bbox_axis(ax, "x"), py_bbox_axis(ax, "y"), py_bbox_title(ax))
if ispositive(width(bb)) && ispositive(height(bb)) if ispositive(width(bb)) && ispositive(height(bb))
leftpad = _max(leftpad, left(plotbb) - left(bb)) leftpad = NaNMath.max(leftpad, left(plotbb) - left(bb))
toppad = _max(toppad, top(plotbb) - top(bb)) toppad = NaNMath.max(toppad, top(plotbb) - top(bb))
rightpad = _max(rightpad, right(bb) - right(plotbb)) rightpad = NaNMath.max(rightpad, right(bb) - right(plotbb))
bottompad = _max(bottompad, bottom(bb) - bottom(plotbb)) bottompad = NaNMath.max(bottompad, bottom(bb) - bottom(plotbb))
end end
end end

View File

@ -55,10 +55,10 @@ function Base.:+(bb1::BoundingBox, bb2::BoundingBox)
ispositive(width(bb2)) || return bb1 ispositive(width(bb2)) || return bb1
ispositive(height(bb2)) || return bb1 ispositive(height(bb2)) || return bb1
l = _min(left(bb1), left(bb2)) l = NaNMath.min(left(bb1), left(bb2))
t = _min(top(bb1), top(bb2)) t = NaNMath.min(top(bb1), top(bb2))
r = _max(right(bb1), right(bb2)) r = NaNMath.max(right(bb1), right(bb2))
b = _max(bottom(bb1), bottom(bb2)) b = NaNMath.max(bottom(bb1), bottom(bb2))
BoundingBox(l, t, r-l, b-t) BoundingBox(l, t, r-l, b-t)
end end

View File

@ -225,7 +225,7 @@ end
fr = if yaxis[:scale] == :identity fr = if yaxis[:scale] == :identity
0.0 0.0
else else
_min(axis_limits(yaxis)[1], _minimum(y)) NaNMath.min(axis_limits(yaxis)[1], _minimum(y))
end end
end end
newx, newy = zeros(3n), zeros(3n) newx, newy = zeros(3n), zeros(3n)
@ -548,7 +548,7 @@ Plots.@deps stepbins path
function _auto_binning_nbins{N}(vs::NTuple{N,AbstractVector}, dim::Integer; mode::Symbol = :auto) function _auto_binning_nbins{N}(vs::NTuple{N,AbstractVector}, dim::Integer; mode::Symbol = :auto)
_cl(x) = _max(ceil(Int, x), 1) _cl(x) = NaNMath.max(ceil(Int, x), 1)
_iqr(v) = quantile(v, 0.75) - quantile(v, 0.25) _iqr(v) = quantile(v, 0.75) - quantile(v, 0.25)
_span(v) = _maximum(v) - _minimum(v) _span(v) = _maximum(v) - _minimum(v)

View File

@ -6,7 +6,7 @@ function binData(data, nbins)
lo, hi = _extrema(data) lo, hi = _extrema(data)
edges = collect(linspace(lo, hi, nbins+1)) edges = collect(linspace(lo, hi, nbins+1))
midpoints = calcMidpoints(edges) midpoints = calcMidpoints(edges)
buckets = Int[_max(2, _min(searchsortedfirst(edges, x), length(edges)))-1 for x in data] buckets = Int[NaNMath.max(2, NaNMath.min(searchsortedfirst(edges, x), length(edges)))-1 for x in data]
counts = zeros(Int, length(midpoints)) counts = zeros(Int, length(midpoints))
for b in buckets for b in buckets
counts[b] += 1 counts[b] += 1
@ -284,8 +284,8 @@ unzip{T}(xyuv::FixedSizeArrays.Vec{4,T}) = T[xyuv[1]], T[xyuv[2]], T[xyuv[
function _expand_limits(lims, x) function _expand_limits(lims, x)
try try
e1, e2 = _extrema(x) e1, e2 = _extrema(x)
lims[1] = _min(lims[1], e1) lims[1] = NaNMath.min(lims[1], e1)
lims[2] = _max(lims[2], e2) lims[2] = NaNMath.max(lims[2], e2)
# catch err # catch err
# warn(err) # warn(err)
end end
@ -343,7 +343,7 @@ end
function calc_r_extrema(x, y) function calc_r_extrema(x, y)
xmin, xmax = _extrema(x) xmin, xmax = _extrema(x)
ymin, ymax = _extrema(y) ymin, ymax = _extrema(y)
r = 0.5 * _min(xmax - xmin, ymax - ymin) r = 0.5 * NaNMath.min(xmax - xmin, ymax - ymin)
_extrema(r) _extrema(r)
end end