Skip to content

Commit

Permalink
added axes1d
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerHeintzmann committed Sep 3, 2021
1 parent dc0b3fe commit d5dde0e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,33 @@ This is a wrapper for
`disc(eltype(arr), size(arr), disc_radius; scaling=scaling, offset=offset)`.
"""
disc

"""
axes1d(size::NTuple{N, Int}; offset=CtrFT, scale=ScaUnit, dims=ntuple(+, N), accumulator=sum, weight=1) where {N}
returns a tuple of axes, all oriented along x.
This is very useful for plotting (see example below).
The optional argument `keepdims` can be used to obtain oriented single-dimensional vectors
which can be used in analogy to `meshgrid` operations but supporting broadcasting and generator performance.
See `rr2` for a description of other options.
```jldoctest
julia> using Plots, IndexFunArrays
julia> sz = (200,100);
julia> heatmap(axes1d(sz)..., rr(sz)')
julia> x,y = axes1d((10,11), keepdims=true)
"hi" = "hi"
Base.Generator{Tuple{Int64, Int64}, IndexFunArrays.var"#675#677"{Float64, Tuple{Int64, Int64}, Tuple{Int64, Int64}, Tuple{Int64, Int64}}}(IndexFunArrays.var"#675#677"{Float64, Tuple{Int64, Int64}, Tuple{Int64, Int64}, Tuple{Int64, Int64}}((6, 6), (1, 1), (10, 11)), (1, 2))
julia> size(y)
(1, 11)
```
---
axes1d(arr::AbstractArray{T, N}; offset=CtrFT, scale=ScaUnit, dims=ntuple(+, N), accumulator=sum, weight=1) where {N}
This is a wrapper for
`axes1d(size(arr); offset=CtrFT, scale=ScaUnit, dims=ntuple(+, N), accumulator=sum, weight=1) where {N}`
"""
axes1d
21 changes: 21 additions & 0 deletions src/scalar_ifas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,25 @@ for F in generate_functions_expr()
@eval export $(F[1])
end

export axes1d

function axes1d(::Type{T}, size::NTuple{N, Int}; offset=CtrFT, scale=ScaUnit, dims=ntuple(+, N), keepdims=false) where {T,N}
offset = get_offset(size, offset)
scale_init = get_scale(size, scale)
scale = apply_dims(scale_init, dims, N) # replaces scale entries not in dims with zeros
if keepdims
@show offset
(ramp(T, d, size[d]; offset=offset[d],scale=scale[d]) for d in dims)
else
(xx(T, (size[d],); offset=offset[d], scale=scale[d]) for d in dims)
end
end

function axes1d(size::NTuple{N, Int}; offset=CtrFT, scale=ScaUnit, dims=ntuple(+, N), keepdims=false) where {N}
T = DEFAULT_T
axes1d(T, size; offset=offset, scale=scale, dims=dims, keepdims=keepdims)
end

function axes1d(arr::AbstractArray{T, N}; offset=CtrFT, scale=ScaUnit, dims=ntuple(+, N), keepdims=false) where {T, N}
axes1d(size(arr); offset=offset, scale=scale, dims=dims, keepdims=keepdims)
end
15 changes: 14 additions & 1 deletion test/concrete_generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,20 @@ end
@test disc(sz,disc_rad, offset=CtrCorner) (rr(sz, offset=CtrCorner) .<= disc_rad)
@test disc(zeros(sz),disc_rad, offset=CtrCorner) (rr(sz, offset=CtrCorner) .<= disc_rad)
end

@testset "axes1d" begin
sz = (2,3,4)
x,y,z = axes1d(sz, offset=(0,0,0))
@test x == 1:sz[1]
@test y == 1:sz[2]
@test z == 1:sz[3]
x,y,z = axes1d(sz, offset=CtrCorner, keepdims=true)
@test size(x) == (sz[1],)
@test size(y) == (1,sz[2])
@test size(z) == (1,1,sz[3])
@test x[:] == 0:sz[1]-1
@test y[:] == 0:sz[2]-1
@test z[:] == 0:sz[3]-1
end
end


Expand Down

0 comments on commit d5dde0e

Please sign in to comment.