Skip to content

Commit 6cc01b6

Browse files
committed
Implement interface for Named Tuples
1 parent e8113b9 commit 6cc01b6

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/YAXArrayBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using DataStructures: OrderedDict
44

55
include("datasets/datasetinterface.jl")
66
include("axisarrays/axisinterface.jl")
7+
include("axisarrays/namedtuple.jl")
78

89
defaultfillval(T::Type{<:AbstractFloat}) = convert(T,1e32)
910
defaultfillval(::Type{Float16}) = Float16(3.2e4)

src/axisarrays/axisinterface.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ iscompressed(x) = false
4343
4444
Returns a tuple of dimension names for al dimensions of x.
4545
"""
46-
dimnames(x) = ntuple(i->dimname(x,i), ndims(x))
46+
dimnames(x) = ntuple(i->dimname(x,i), ndims(getdata(x)))
4747

4848
"""
4949
getattributes(x)
@@ -77,7 +77,8 @@ valfromaxis(x) = x
7777
Converts an AbstractArray x that implements the interface to type T.
7878
"""
7979
function yaxconvert(T::Type{<:Any},x)
80-
yaxcreate(T, getdata(x), dimnames(x),dimvals.(Ref(x),1:ndims(x)),getattributes(x))
80+
data = getdata(x)
81+
yaxcreate(T, data, dimnames(x),dimvals.(Ref(x),1:ndims(data)),getattributes(x))
8182
end
8283

8384
"""

src/axisarrays/namedtuple.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
dimvals(x::NamedTuple,i) = x.axes[i]
2+
3+
_dname(::NamedTuple{N},i) where N = N[i]
4+
_dname(::Tuple,i) = Symbol("Dim_",i)
5+
dimname(x::NamedTuple, i) = _dname(x.axes,i)
6+
7+
#Special method to get all dim names
8+
dimnames(x::NamedTuple) = _dnames(x.axes)
9+
_dnames(::NamedTuple{N}) where N = N
10+
_dnames(x) = ntuple(i->Symbol("Dim_",i), length(x))
11+
12+
getdata(x::NamedTuple) = x.values
13+
14+
function yaxcreate(::Type{<:NamedTuple},data,dnames,dvals,atts)
15+
axes = NamedTuple{dnames}(dvals)
16+
(axes = axes, values = data)
17+
end

0 commit comments

Comments
 (0)