Skip to content

Commit

Permalink
Add steps/offsets convenience constructor:
Browse files Browse the repository at this point in the history
    AxisArray(A::AbstractArray, (names...,), (steps...,), [(offsets...,)])
  • Loading branch information
timholy committed Jan 5, 2017
1 parent 040f115 commit babb416
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core.jl
Expand Up @@ -94,6 +94,7 @@ immutable AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
AxisArray(A::AbstractArray, axes::Axis...)
AxisArray(A::AbstractArray, names::Symbol...)
AxisArray(A::AbstractArray, vectors::AbstractVector...)
AxisArray(A::AbstractArray, (names...,), (steps...,), [(offsets...,)])
```
### Arguments
Expand Down Expand Up @@ -202,6 +203,10 @@ checknames() = ()
AxisArray(A::AbstractArray) = AxisArray(A, ()) # Disambiguation
AxisArray(A::AbstractArray, names::Symbol...) = AxisArray(A, map((name,ind)->Axis{name}(ind), names, indices(A)))
AxisArray(A::AbstractArray, vects::AbstractVector...) = AxisArray(A, ntuple(i->Axis{_defaultdimname(i)}(vects[i]), length(vects)))
function AxisArray{T,N}(A::AbstractArray{T,N}, names::NTuple{N,Symbol}, steps::NTuple{N,Number}, offsets::NTuple{N,Number}=map(zero, steps))
axs = ntuple(i->Axis{names[i]}(range(offsets[i], steps[i], size(A,i))), N)
AxisArray(A, axs...)
end

# Traits
immutable HasAxes{B} end
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Expand Up @@ -117,6 +117,13 @@ A = AxisArray([0]', :x, :y)
@test axisnames(@inferred(squeeze(A, Axis{:x,UnitRange{Int}}))) == (:y,)
@test axisnames(@inferred(squeeze(A, Axis{:y}))) == (:x,)
@test axisnames(@inferred(squeeze(squeeze(A, Axis{:x}), Axis{:y}))) == ()
# Names, steps, and offsets
B = AxisArray([1 4; 2 5; 3 6], (:x, :y), (0.2, 100))
@test axisnames(B) == (:x, :y)
@test axisvalues(B) == (0:0.2:0.4, 0:100:100)
B = AxisArray([1 4; 2 5; 3 6], (:x, :y), (0.2, 100), (-3,14))
@test axisnames(B) == (:x, :y)
@test axisvalues(B) == (-3:0.2:-2.6, 14:100:114)

@test AxisArrays.HasAxes(A) == AxisArrays.HasAxes{true}()
@test AxisArrays.HasAxes([1]) == AxisArrays.HasAxes{false}()
Expand Down

0 comments on commit babb416

Please sign in to comment.