Compare commits

...

8 Commits

Author SHA1 Message Date
Michael K. Borregaard
d74b8c63d8 Fixed an issue with zeros 2017-09-04 21:35:47 +02:00
Michael K. Borregaard
96a0133c12 length-> length(linearindices( 2017-09-04 16:31:21 +02:00
Michael K. Borregaard
2229172ebd length -> linearindices 2017-09-04 16:31:05 +02:00
Michael Krabbe Borregaard
80590f4647 Merge pull request #1067 from timholy/teh/generalized_arrays
A couple more generalized array fixes
2017-09-02 21:54:25 +02:00
Michael K. Borregaard
2de85d3fd3 replace sizes 2017-09-02 21:51:54 +02:00
Tim Holy
9a073c27ab More generalized indexing fixes 2017-09-02 14:09:49 -05:00
Michael K. Borregaard
55d634c17a replace 1:length with linearindices 2017-09-02 12:57:28 +02:00
Michael K. Borregaard
d25a3e006f Initial commit 2017-09-02 12:22:36 +02:00
13 changed files with 47 additions and 46 deletions

View File

@ -866,7 +866,7 @@ function extractGroupArgs(v::AVec, args...; legendEntry = string)
if n > 100
warn("You created n=$n groups... Is that intended?")
end
groupIds = Vector{Int}[filter(i -> v[i] == glab, 1:length(v)) for glab in groupLabels]
groupIds = Vector{Int}[filter(i -> v[i] == glab, linearindices(v)) for glab in groupLabels]
GroupBy(map(legendEntry, groupLabels), groupIds)
end
@ -874,7 +874,7 @@ legendEntryFromTuple(ns::Tuple) = string(("$n " for n in ns)...)
# this is when given a tuple of vectors of values to group by
function extractGroupArgs(vs::Tuple, args...)
(vs == ()) && return GroupBy([""], [1:size(args[1],1)])
(vs == ()) && return GroupBy([""], [indices(args[1],1)])
v = collect(zip(vs...))
extractGroupArgs(v, args...; legendEntry = legendEntryFromTuple)
end
@ -983,7 +983,8 @@ convertLegendValue(v::AbstractArray) = map(convertLegendValue, v)
# anything else is returned as-is
function slice_arg(v::AMat, idx::Int)
c = mod1(idx, size(v,2))
size(v,1) == 1 ? v[1,c] : v[:,c]
m,n = indices(v)
size(v,1) == 1 ? v[first(m),n[c]] : v[:,n[c]]
end
slice_arg(wrapper::InputWrapper, idx) = wrapper.obj
slice_arg(v, idx) = v

View File

@ -461,10 +461,10 @@ end
# add the discrete value for each item. return the continuous values and the indices
function discrete_value!(axis::Axis, v::AVec)
n = length(v)
cvec = zeros(n)
discrete_indices = zeros(Int, n)
for i=1:n
n = linearindices(v)
cvec = zeros(length(n))
discrete_indices = zeros(Int, length(n))
for i=n
cvec[i], discrete_indices[i] = discrete_value!(axis, v[i])
end
cvec, discrete_indices
@ -472,10 +472,10 @@ end
# add the discrete value for each item. return the continuous values and the indices
function discrete_value!(axis::Axis, v::AMat)
n,m = size(v)
cmat = zeros(n,m)
discrete_indices = zeros(Int, n, m)
for i=1:n, j=1:m
n,m = indices(v)
cmat = zeros(v)
discrete_indices = similar(Array{Int}, n, m)
@inbounds for i=n, j=m
cmat[i,j], discrete_indices[i,j] = discrete_value!(axis, v[i,j])
end
cmat, discrete_indices

View File

@ -784,7 +784,7 @@ end
function gl_bar(d, kw_args)
x, y = d[:x], d[:y]
nx, ny = length(x), length(y)
nx, ny = length(linearindices(x)), length(linearindices(y))
axis = d[:subplot][isvertical(d) ? :xaxis : :yaxis]
cv = [discrete_value!(axis, xi)[1] for xi=x]
x = if nx == ny
@ -792,7 +792,7 @@ function gl_bar(d, kw_args)
elseif nx == ny + 1
0.5diff(cv) + cv[1:end-1]
else
error("bar recipe: x must be same length as y (centers), or one more than y (edges).\n\t\tlength(x)=$(length(x)), length(y)=$(length(y))")
error("bar recipe: x must be same length as y (centers), or one more than y (edges).\n\t\tlength(x)=$(nx), length(y)=$(ny)")
end
if haskey(kw_args, :stroke_width) # stroke is inside for bars
#kw_args[:stroke_width] = -kw_args[:stroke_width]
@ -802,7 +802,7 @@ function gl_bar(d, kw_args)
hw = if bw == nothing
ignorenan_mean(diff(x))
else
Float64[_cycle(bw,i)*0.5 for i=1:length(x)]
Float64[_cycle(bw,i)*0.5 for i=linearindices(x)]
end
# make fillto a vector... default fills to 0
@ -852,11 +852,11 @@ function gl_boxplot(d, kw_args)
sx, sy = m[1,1], m[2,2]
for (i,glabel) in enumerate(glabels)
# filter y
values = y[filter(i -> _cycle(x,i) == glabel, 1:length(y))]
values = y[filter(i -> _cycle(x,i) == glabel, linearindices(y))]
# compute quantiles
q1,q2,q3,q4,q5 = quantile(values, linspace(0,1,5))
# notch
n = Plots.notch_width(q2, q4, length(values))
n = Plots.notch_width(q2, q4, length(linearindices(values)))
# warn on inverted notches?
if notch && !warning && ( (q2>(q3-n)) || (q4<(q3+n)) )
warn("Boxplot's notch went outside hinges. Set notch to false.")
@ -963,7 +963,7 @@ function scale_for_annotations!(series::Series, scaletype::Symbol = :pixels)
# with a list of custom shapes for each
msw, msh = anns.scalefactor
offsets = Array{Vec2f0}(length(anns.strs))
series[:markersize] = map(1:length(anns.strs)) do i
series[:markersize] = map(linearindices(anns.strs)) do i
str = _cycle(anns.strs, i)
# get the width and height of the string (in mm)
sw, sh = text_size(str, anns.font.pointsize)
@ -1077,7 +1077,7 @@ function _display(plt::Plot{GLVisualizeBackend}, visible = true)
kw = copy(kw_args)
fr = d[:fillrange]
ps = if all(x-> x >= 0, diff(d[:x])) # if is monotonic
vcat(points, Point2f0[(points[i][1], _cycle(fr, i)) for i=length(points):-1:1])
vcat(points, Point2f0[(points[i][1], _cycle(fr, i)) for i=reverse(linearindices(points))])
else
points
end
@ -1291,8 +1291,8 @@ function gl_surface(x,y,z, kw_args)
if isa(x, AbstractMatrix) && isa(y, AbstractMatrix)
main = map(s->map(Float32, s), (x, y, z))
elseif isa(x, AbstractVector) || isa(y, AbstractVector)
x = Float32[x[i] for i = 1:size(z,1), j = 1:size(z,2)]
y = Float32[y[j] for i = 1:size(z,1), j = 1:size(z,2)]
x = Float32[x[i] for i = indices(z,1), j = indices(z,2)]
y = Float32[y[j] for i = indices(z,1), j = indices(z,2)]
main = (x, y, map(Float32, z))
else
error("surface: combination of types not supported: $(typeof(x)) $(typeof(y)) $(typeof(z))")
@ -1301,7 +1301,7 @@ function gl_surface(x,y,z, kw_args)
points = map(Point3f0, zip(vec(x), vec(y), vec(z)))
faces = Cuint[]
idx = (i,j) -> sub2ind(size(z), i, j) - 1
for i=1:size(z,1), j=1:size(z,2)
for i=indices(z,1), j=indices(z,2)
i < size(z,1) && push!(faces, idx(i, j), idx(i+1, j))
j < size(z,2) && push!(faces, idx(i, j), idx(i, j+1))

View File

@ -306,7 +306,7 @@ end
function gr_draw_markers(series::Series, x, y, msize, mz)
shapes = series[:markershape]
if shapes != :none
for i=1:length(x)
for i=linearindices(x)
msi = _cycle(msize, i)
shape = _cycle(shapes, i)
cfunc = isa(shape, Shape) ? gr_set_fillcolor : gr_set_markercolor

View File

@ -370,7 +370,7 @@ end
function plotly_colorscale(grad::ColorGradient, α)
[[grad.values[i], rgb_string(grad.colors[i])] for i in 1:length(grad.colors)]
[[grad.values[i], rgb_string(grad.colors[i])] for i in linearindices(grad.colors)]
end
plotly_colorscale(c, α) = plotly_colorscale(cgrad(alpha=α), α)
# plotly_colorscale(c, alpha = nothing) = plotly_colorscale(cgrad(), alpha)
@ -397,7 +397,7 @@ end
# we split by NaNs and then construct/destruct the shapes to get the closed coords
function plotly_close_shapes(x, y)
xs, ys = nansplit(x), nansplit(y)
for i=1:length(xs)
for i=linearindices(xs)
shape = Shape(xs[i], ys[i])
xs[i], ys[i] = coords(shape)
end

View File

@ -244,7 +244,7 @@ end
# end
function get_locator_and_formatter(vals::AVec)
pyticker["FixedLocator"](1:length(vals)), pyticker["FixedFormatter"](vals)
pyticker["FixedLocator"](linearindices(vals)), pyticker["FixedFormatter"](vals)
end
function add_pyfixedformatter(cbar, vals::AVec)
@ -589,7 +589,7 @@ function py_add_series(plt::Plot{PyPlotBackend}, series::Series)
shapes = series[:markershape]
msc = py_markerstrokecolor(series)
lw = py_dpi_scale(plt, series[:markerstrokewidth])
for i=1:length(y)
for i=linearindices(y)
extrakw[:c] = if series[:marker_z] == nothing
py_color_fix(py_color(_cycle(series[:markercolor],i)), x)
else
@ -850,7 +850,7 @@ function py_add_series(plt::Plot{PyPlotBackend}, series::Series)
kw = KW()
if discrete_colorbar_values != nothing
locator, formatter = get_locator_and_formatter(discrete_colorbar_values)
# kw[:values] = 1:length(discrete_colorbar_values)
# kw[:values] = linearindices(discrete_colorbar_values)
kw[:values] = sp[:zaxis][:continuous_values]
kw[:ticks] = locator
kw[:format] = formatter

View File

@ -186,7 +186,7 @@ end
function scale!(shape::Shape, x::Real, y::Real = x, c = center(shape))
sx, sy = coords(shape)
cx, cy = c
for i=1:length(sx)
for i=linearindices(sx)
sx[i] = (sx[i] - cx) * x + cx
sy[i] = (sy[i] - cy) * y + cy
end
@ -201,7 +201,7 @@ end
"translate a Shape in space"
function translate!(shape::Shape, x::Real, y::Real = x)
sx, sy = coords(shape)
for i=1:length(sx)
for i=linearindices(sx)
sx[i] += x
sy[i] += y
end
@ -229,7 +229,7 @@ end
function rotate!(shape::Shape, Θ::Real, c = center(shape))
x, y = coords(shape)
cx, cy = c
for i=1:length(x)
for i=linearindices(x)
xi = rotate_x(x[i], y[i], Θ, cx, cy)
yi = rotate_y(x[i], y[i], Θ, cx, cy)
x[i], y[i] = xi, yi
@ -506,7 +506,7 @@ function series_annotations_shapes!(series::Series, scaletype::Symbol = :pixels)
push!(msize, maxscale)
baseshape = _cycle(get(anns.baseshape),i)
shape = scale(baseshape, msw*xscale/maxscale, msh*yscale/maxscale, (0,0))
end for i=1:length(anns.strs)]
end for i=linearindices(anns.strs)]
series[:markershape] = shapes
series[:markersize] = msize
end

View File

@ -258,7 +258,7 @@ function addToGadflyLegend(plt::Plot, d::KW)
foundit = false
# extend the label if we found this color
for i in 1:length(guide.colors)
for i in linearindices(guide.colors)
if RGB(c) == guide.colors[i]
guide.labels[i] *= ", " * d[:label]
foundit = true
@ -333,9 +333,9 @@ end
# # this is super weird, but... oh well... for some reason this creates n separate line segments...
# # create a list of vertices that go: [x1,x2,x2,x3,x3, ... ,xi,xi, ... xn,xn] (same for y)
# # then the vector passed to the "color" keyword should be a vector: [1,1,2,2,3,3,4,4, ..., i,i, ... , n,n]
# csindices = Int[mod1(i,length(cscheme.v)) for i in 1:length(d[:y])]
# csindices = Int[mod1(i,length(cscheme.v)) for i in linearindices(d[:y])]
# cs = collect(repmat(csindices', 2, 1))[1:end-1]
# grp = collect(repmat((1:length(d[:y]))', 2, 1))[1:end-1]
# grp = collect(repmat((linearindices(d[:y]))', 2, 1))[1:end-1]
# d[:x], d[:y] = map(createSegments, (d[:x], d[:y]))
# colorgroup = [(:linecolor, cs), (:group, grp)]

View File

@ -379,7 +379,7 @@ function test_examples(pkgname::Symbol; debug = false, disp = true, sleep = noth
skip = [], only = nothing)
Plots._debugMode.on = debug
plts = Dict()
for i in 1:length(_examples)
for i in linearindices(_examples)
only != nothing && !(i in only) && continue
i in skip && continue
try

View File

@ -291,7 +291,7 @@ end
hw = if bw == nothing
0.5ignorenan_mean(diff(procx))
else
Float64[0.5_cycle(bw,i) for i=1:length(procx)]
Float64[0.5_cycle(bw,i) for i=linearindices(procx)]
end
# make fillto a vector... default fills to 0
@ -872,7 +872,7 @@ function get_xy(o::OHLC, x, xdiff)
end
# get the joined vector
function get_xy(v::AVec{OHLC}, x = 1:length(v))
function get_xy(v::AVec{OHLC}, x = linearindices(v))
xdiff = 0.3ignorenan_mean(abs.(diff(x)))
x_out, y_out = zeros(0), zeros(0)
for (i,ohlc) in enumerate(v)

View File

@ -69,13 +69,13 @@ end
# TODO: can we avoid the copy here? one error that crops up is that mapping functions over the same array
# result in that array being shared. push!, etc will add too many items to that array
compute_x(x::Void, y::Void, z) = 1:size(z,1)
compute_x(x::Void, y, z) = 1:size(y,1)
compute_x(x::Void, y::Void, z) = indices(z,1)
compute_x(x::Void, y, z) = indices(y,1)
compute_x(x::Function, y, z) = map(x, y)
compute_x(x, y, z) = copy(x)
# compute_y(x::Void, y::Function, z) = error()
compute_y(x::Void, y::Void, z) = 1:size(z,2)
compute_y(x::Void, y::Void, z) = indices(z,2)
compute_y(x, y::Function, z) = map(y, x)
compute_y(x, y, z) = copy(y)
@ -272,9 +272,9 @@ end
# return a surface if this is a 3d plot, otherwise let it be sliced up
@recipe function f{T<:Union{Integer,AbstractFloat}}(mat::AMat{T})
if all3D(d)
n,m = size(mat)
inds = indices(mat)
wrap_surfaces(d)
SliceIt, 1:m, 1:n, Surface(mat)
SliceIt, inds[2], inds[1], Surface(mat)
else
SliceIt, nothing, mat, nothing
end

View File

@ -1,5 +1,5 @@
calcMidpoints(edges::AbstractVector) = Float64[0.5 * (edges[i] + edges[i+1]) for i in 1:length(edges)-1]
calcMidpoints(edges::AbstractVector) = Float64[0.5 * (edges[i] + edges[i+1]) for i in linearindices(edges)-1]
"Make histogram-like bins of data"
function binData(data, nbins)
@ -43,7 +43,7 @@ function barHack(; kw...)
# estimate the edges
dists = diff(midpoints) * 0.5
edges = zeros(length(midpoints)+1)
for i in 1:length(edges)
for i in linearindices(edges)
if i == 1
edge = midpoints[1] - dists[1]
elseif i == length(edges)
@ -56,7 +56,7 @@ function barHack(; kw...)
x = Float64[]
y = Float64[]
for i in 1:length(heights)
for i in linearindices(heights)
e1, e2 = edges[i:i+1]
append!(x, [e1, e1, e2, e2])
append!(y, [fillrange, heights[i], heights[i], fillrange])

View File

@ -92,7 +92,7 @@ function image_comparison_facts(pkg::Symbol;
debug = false, # print debug information?
sigma = [1,1], # number of pixels to "blur"
eps = 1e-2) # acceptable error (percent)
for i in 1:length(Plots._examples)
for i in linearindices(Plots._examples)
i in skip && continue
if only == nothing || i in only
@test image_comparison_tests(pkg, i, debug=debug, sigma=sigma, eps=eps) |> success == true