-
Notifications
You must be signed in to change notification settings - Fork 18
/
dciterators.jl
208 lines (190 loc) · 6.49 KB
/
dciterators.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import YAXArrays.DAT: DATConfig
import YAXArrays.YAXTools: PickAxisArray
#using YAXArrays.Cubes.Axes: axcopy
using DiskArrays: GridChunks, AbstractDiskArray
using Tables: Tables, Schema, AbstractColumns
struct CubeIterator{R,LAX,S<:Schema} <: AbstractVector{Any}
dc::DATConfig
r::R
loopaxes::LAX
schema::S
end
Tables.schema(t::CubeIterator) = t.schema
Base.length(ci::CubeIterator) = length(ci.r)
Base.size(ci::CubeIterator) = (length(ci.r),)
Base.eltype(ci::Type{<:CubeIterator{<:R,<:LAX}}) where {R,LAX} = YAXTableChunk{ci, LAX, eltype(R)}
Base.eltype(ci::CubeIterator) = eltype(typeof(ci))
function Base.getindex(t::CubeIterator, i::Int)
rnow = t.r[i]
laxsmall = map(t.loopaxes, rnow) do ax,ir
DD.rebuild(ax,ax.val[ir])
end
cols = Union{Nothing,YAXColumn}[nothing for i in t.schema.names]
return YAXTableChunk(t,laxsmall,rnow,cols)
end
function Base.show(io::IO, ci::CubeIterator)
print(
io,
"Datacube iterator with ",
length(ci.r),
" subtables with fields: ",
ci.schema.names,
)
end
function Base.show(io::IO, ::MIME"text/plain", X::CubeIterator)
show(io,X)
end
"""
YAXColumn
A struct representing a single column of a YAXArray partitioned Table
# Fields
$(FIELDS)
"""
struct YAXColumn{T,A,IT} <: AbstractVector{T}
inarBC::A
inds::IT
end
Base.getindex(a::YAXColumn,i) = a.inarBC[a.inds[i]]
Base.length(a::YAXColumn) = length(a.inds)
Base.size(a::YAXColumn) = (length(a.inds),)
struct YAXTableChunk{CI<:CubeIterator, LAX, IC} <: AbstractColumns
ci::CI
loopaxes::LAX
ichunk::IC
cols::Vector{Union{Nothing,YAXColumn}}
end
function Tables.getcolumn(t::YAXTableChunk, i::Int)
cols = getfield(t,:cols)
if cols[i] === nothing
cols[i] = YAXColumn(t,i)
end
cols[i]
end
function Tables.getcolumn(t::YAXTableChunk, s::Symbol)
n = Tables.schema(t).names
i = findfirst(==(s), n)
if i === nothing
error("Could not find $s in table with columns $n")
end
Tables.getcolumn(t,i)
end
Tables.columnnames(t::YAXTableChunk) = getfield(t,:ci).schema.names
Tables.schema(t::YAXTableChunk) = getfield(t,:ci).schema
function Base.show(io::IO, X::YAXTableChunk)
println(io,"Table chunk with schema:")
print(io,getfield(X,:ci).schema)
end
Base.show(io::IO, ::MIME"text/plain", X::YAXTableChunk) = show(io,X)
function YAXColumn(t::YAXTableChunk,ivar)
ci = getfield(t,:ci)
rnow = getfield(t,:ichunk)
if ivar > length(ci.dc.incubes)
iax = ivar-length(ci.dc.incubes)
axvals = getfield(t,:loopaxes)[iax].val
allax = map(_->false, getfield(t,:loopaxes))
allax = Base.setindex(allax, true, iax)
inarbc = PickAxisArray(axvals, allax)
inds = CartesianIndices(Base.OneTo.(length.(rnow)))
return YAXColumn{eltype(inarbc),typeof(inarbc), typeof(inds)}(inarbc, inds)
else
ic = ci.dc.incubes[ivar]
buf = allocatecachebuf(ic, ci.dc.loopcachesize)
updatear(:read, rnow, ic.cube, geticolon(ic), ic.loopinds, buf)
allax = ntuple(_->false, length(ci.dc.LoopAxes))
for il in ic.loopinds
allax = Base.setindex(allax,true,il)
end
for il in ic.icolon
i_insert = findfirst(==(il),cumsum(allax))
allax = if i_insert === nothing
(allax...,Colon())
else
(allax[1:i_insert]...,Colon(),allax[i_insert+1:end]...)
end
end
inarbc = if ic.colonperm === nothing
pa = PickAxisArray(buf, allax)
else
pa = PickAxisArray(buf, allax, ic.colonperm)
end
inds = CartesianIndices(Base.OneTo.(length.(rnow)))
return YAXColumn{eltype(inarbc),typeof(inarbc), typeof(inds)}(inarbc, inds)
end
end
export CubeTable
"""
CubeTable()
Function to turn a DataCube object into an iterable table. Takes a list of as arguments,
specified as a `name=cube` expression. For example
`CubeTable(data=cube1,country=cube2)` would generate a Table with the entries `data` and `country`,
where `data` contains the values of `cube1` and `country` the values of `cube2`. The cubes
are matched and broadcasted along their axes like in `mapCube`.
"""
function CubeTable(; expandaxes = (), cubes...)
c = (map((k, v) -> v, keys(cubes), values(cubes))...,)
all(i -> isa(i, Union{YAXArray,AbstractArray}), c) ||
throw(ArgumentError("All inputs must be DataCubes"))
varnames = map(string, keys(cubes))
expandaxes = isa(expandaxes, Tuple) ? expandaxes : (expandaxes,)
inaxnames = Set{String}()
indims = if isempty(expandaxes)
map(i -> InDims(), c)
else
map(c) do i
axn = filter(collect(expandaxes)) do ax
findAxis(ax, i) !== nothing
end
foreach(j -> push!(inaxnames, string(DD.name(getAxis(j, i)))), axn)
InDims(axn...)
end
end
axnames = map(i -> string.(DD.name.(caxes(i))), c)
foreach(1:length(axnames)) do i
otheraxes = axnames[[1:i-1;i+1:length(axnames)]]
if !isempty(otheraxes) && isempty(intersect(axnames[i], union(otheraxes...)))
@warn "Input cube $i with axes $(axnames[i]) does not share any axis with other cubes from the iterator, please check the axis names"
end
end
allvars = union(axnames...)
allnums = collect(1:length(allvars))
configiter =
mapCube(identity, c, debug = true, indims = indims, outdims = (), ispar = false)
# if inax !== nothing
# linax = length(inax)
# foreach(configiter.incubes) do ic1
# if !isempty(ic1.axesSmall)
# empty!(ic1.axesSmall)
# map!(i->i+1,ic1.loopinds,ic1.loopinds)
# pushfirst!(ic1.loopinds,1)
# else
# map!(i->i+1,ic1.loopinds,ic1.loopinds)
# end
# end
# end
r = GridChunks(
getloopchunks(configiter)...
)
ci = CubeIterator(configiter, r, varnames = varnames)
end
function CubeIterator(
dc,
r;
varnames::Tuple = ntuple(i -> Symbol("x$i"), length(dc.incubes)),
)
loopaxes = (dc.LoopAxes...,)
length(varnames) == length(dc.incubes) ||
error("Supplied $(length(varnames)) varnames and $(length(dc.incubes)) cubes.")
et = map(dc.incubes) do ic
eltype(ic.cube)
end
et = (et..., map(i->eltype(i.val), loopaxes)...)
axnames = DD.name.(loopaxes)
colnames = (map(Symbol, varnames)..., axnames...)
CubeIterator(
dc,
r,
loopaxes,
Tables.Schema(colnames,et)
)
end
import Tables