Skip to content

Commit 65263db

Browse files
authored
Merge pull request #53 from JuliaArrays/teh/convenience
Add steps/offsets convenience constructor:
2 parents 040f115 + babb416 commit 65263db

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/core.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ immutable AxisArray{T,N,D,Ax} <: AbstractArray{T,N}
9494
AxisArray(A::AbstractArray, axes::Axis...)
9595
AxisArray(A::AbstractArray, names::Symbol...)
9696
AxisArray(A::AbstractArray, vectors::AbstractVector...)
97+
AxisArray(A::AbstractArray, (names...,), (steps...,), [(offsets...,)])
9798
```
9899
99100
### Arguments
@@ -202,6 +203,10 @@ checknames() = ()
202203
AxisArray(A::AbstractArray) = AxisArray(A, ()) # Disambiguation
203204
AxisArray(A::AbstractArray, names::Symbol...) = AxisArray(A, map((name,ind)->Axis{name}(ind), names, indices(A)))
204205
AxisArray(A::AbstractArray, vects::AbstractVector...) = AxisArray(A, ntuple(i->Axis{_defaultdimname(i)}(vects[i]), length(vects)))
206+
function AxisArray{T,N}(A::AbstractArray{T,N}, names::NTuple{N,Symbol}, steps::NTuple{N,Number}, offsets::NTuple{N,Number}=map(zero, steps))
207+
axs = ntuple(i->Axis{names[i]}(range(offsets[i], steps[i], size(A,i))), N)
208+
AxisArray(A, axs...)
209+
end
205210

206211
# Traits
207212
immutable HasAxes{B} end

test/core.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ A = AxisArray([0]', :x, :y)
117117
@test axisnames(@inferred(squeeze(A, Axis{:x,UnitRange{Int}}))) == (:y,)
118118
@test axisnames(@inferred(squeeze(A, Axis{:y}))) == (:x,)
119119
@test axisnames(@inferred(squeeze(squeeze(A, Axis{:x}), Axis{:y}))) == ()
120+
# Names, steps, and offsets
121+
B = AxisArray([1 4; 2 5; 3 6], (:x, :y), (0.2, 100))
122+
@test axisnames(B) == (:x, :y)
123+
@test axisvalues(B) == (0:0.2:0.4, 0:100:100)
124+
B = AxisArray([1 4; 2 5; 3 6], (:x, :y), (0.2, 100), (-3,14))
125+
@test axisnames(B) == (:x, :y)
126+
@test axisvalues(B) == (-3:0.2:-2.6, 14:100:114)
120127

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

0 commit comments

Comments
 (0)