Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete subsetcube #326

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/UserGuide/examples_from_esdl_study_1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ vars = ["gross_primary_productivity", "air_temperature_2m", "surface_moisture"]
time_overlap = Date("2001-01-01")..Date("2020-12-31")

# So we "virtually get" the cube data virtually:
cube_subset = subsetcube(cube_handle, variable=vars, time=time_overlap)
cube_subset = cube_handle[Variable=At(vars), time=time_overlap]

# The next function estimates the median seasonal cycle. This changes the dimension of the cube, as the time domain is replaced by day of year (doy); Eq. 9 in the manuscript:
# $$
Expand Down
9 changes: 6 additions & 3 deletions docs/src/tutorials/intro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# In this tutorial we will explore the features of the YAXArrays package.

using YAXArrays, EarthDataLab, Zarr, NetCDF
using DimensionalData: Where

# ## Use data larger than RAM#
#
Expand All @@ -19,8 +20,10 @@ c = esdc(res="low")

# ## Subsets happen lazily

europe = subsetcube(c, region="Europe", time=2000:2016,
Variable=["air_temperature_2m", "net_ecosystem", "soil_moisture"])

plot(europe.time.values,europe[Variable="air_temperature_2m", lat=50, lon=11].data)
europe = c[region="Europe", time=2000:2016,
Variable=Where( x-> any(contains.((x,),["air_temperature_2m", "net_ecosystem", "moisture"])))]


plot(lookup(europe, Ti).data,europe[Variable=At("air_temperature_2m"), lat=50, lon=11].data)

124 changes: 2 additions & 122 deletions src/Cubes/Cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,12 @@ using Tables: istable, schema, columns
using DimensionalData: DimensionalData as DD, AbstractDimArray, NoName
import DimensionalData: name

export concatenatecubes, caxes, subsetcube, readcubedata, renameaxis!, YAXArray, setchunks
export concatenatecubes, caxes, readcubedata, renameaxis!, YAXArray, setchunks

"""
This function calculates a subset of a cube's data
"""
function subsetcube end

"Returns the axes of a Cube"
function caxes end

# TODO: Give Axes an own module in YAXArrays
#=
include("Axes.jl")
using .Axes:
CubeAxis,
RangeAxis,
CategoricalAxis,
findAxis,
getAxis,
axVal2Index,
axname,
axsym,
axVal2Index_lb,
axVal2Index_ub,
renameaxis,
axcopy

"""
The `Axes` module handles the Axes of a data cube.
It provides the following exports:

$(EXPORTS)
"""
Axes
=#
"""
mutable struct CleanMe

Expand Down Expand Up @@ -259,16 +230,6 @@ cubechunks(c) = approx_chunksize(eachchunk(c))
DiskArrays.eachchunk(c::YAXArray) = c.chunks
getindex_all(a) = getindex(a, ntuple(_ -> Colon(), ndims(a))...).data

#=
function Base.getindex(x::YAXArray, i...)
if length(i)==1 && istable(first(i))
batchextract(x,first(i))
else
getdata(x)[i...]
end
end
=#


function batchextract(x,i)
# This function should be documented and moved to DimensionalData
Expand Down Expand Up @@ -375,88 +336,11 @@ _iscompressed(c::DiskArrays.PermutedDiskArray) = _iscompressed(c.a.parent)
_iscompressed(c::DiskArrays.SubDiskArray) = _iscompressed(c.v.parent)
_iscompressed(c) = YAXArrayBase.iscompressed(c)

# lift renameaxis functionality from Axes.jl to YAXArrays
# lift renameaxis functionality from DimensionalData to YAXArrays
renameaxis!(c::YAXArray, p::Pair) = DD.set(c, Symbol(first(p)) => last(p))

#=
function renameaxis!(c::YAXArray, p::Pair)
#This needs to be deleted, because DimensionalData cannot update the axlist
# Because this is a tuple instead of a vector
axlist = caxes(c)
i = findAxis(p[1], axlist)
axlist[i] = renameaxis(axlist[i], p[2])
c
end
function renameaxis!(c::YAXArray, p::Pair{<:Any,<:CubeAxis})
i = findAxis(p[1], caxes(c))
i === nothing && throw(ArgumentError("$(p[1]) Axis not found"))
length(caxes(c)[i].values) == length(p[2].values) ||
throw(ArgumentError("Length of replacement axis must equal length of old axis"))
caxes(c)[i] = p[2]
c
end
=#
function _subsetcube end

function subsetcube(z::YAXArray{T}; kwargs...) where {T}
newaxes, substuple = _subsetcube(z, collect(Any, map(Base.OneTo, size(z))); kwargs...)
newdata = view(getdata(z), substuple...)
YAXArray(newaxes, newdata, z.properties, cleaner=z.cleaner)
end

sorted(x, y) = x < y ? (x, y) : (y, x)

#TODO move everything that is subset-related to its own file or to axes.jl
#=
interpretsubset(subexpr::Union{CartesianIndices{1},LinearIndices{1}}, ax) =
subexpr.indices[1]
interpretsubset(subexpr::CartesianIndex{1}, ax) = subexpr.I[1]
interpretsubset(subexpr, ax) = axVal2Index(ax, subexpr, fuzzy=true)
function interpretsubset(subexpr::NTuple{2,Any}, ax)
x, y = sorted(subexpr...)
Colon()(sorted(axVal2Index_lb(ax, x), axVal2Index_ub(ax, y))...)
end
interpretsubset(subexpr::NTuple{2,Int}, ax::RangeAxis{T}) where {T<:TimeType} =
interpretsubset(map(T, subexpr), ax)
interpretsubset(subexpr::UnitRange{<:Integer}, ax::RangeAxis{T}) where {T<:TimeType} =
interpretsubset(T(first(subexpr)) .. T(last(subexpr) + 1), ax)
interpretsubset(subexpr::Interval, ax) = interpretsubset((subexpr.left, subexpr.right), ax)
interpretsubset(subexpr::AbstractVector, ax::CategoricalAxis) =
axVal2Index.(Ref(ax), subexpr, fuzzy=true)
=#

function _subsetcube(z, subs; kwargs...)
kwargs = Dict{Any,Any}(kwargs)
for f in YAXDefaults.subsetextensions
f(kwargs)
end
newaxes = deepcopy(collect(DD.Dimension, caxes(z)))
foreach(kwargs) do kw
axdes, subexpr = kw
axdes = string(axdes)
iax = findAxis(axdes, caxes(z))
if isa(iax, Nothing)
throw(ArgumentError("Axis $axdes not found in cube"))
else
oldax = newaxes[iax]
subinds = interpretsubset(subexpr, oldax)
subs2 = subs[iax][subinds]
subs[iax] = subs2
if !isa(subinds, AbstractVector) && !isa(subinds, AbstractRange)
newaxes[iax] = axcopy(oldax, oldax.values[subinds:subinds])
else
newaxes[iax] = axcopy(oldax, oldax.values[subinds])
end
end
end
substuple = ntuple(i -> subs[i], length(subs))
inewaxes = findall(i -> isa(i, AbstractVector), substuple)
newaxes = newaxes[inewaxes]
@assert length.(newaxes) ==
map(length, filter(i -> isa(i, AbstractVector), collect(substuple)))
newaxes, substuple
end


function Base.getindex(a::YAXArray, args::DD.Dimension...; kwargs...)
kwargsdict = Dict(kwargs...)
Expand Down Expand Up @@ -496,10 +380,6 @@ end
cubesize(c::YAXArray{T}) where {T} = (sizeof(T)) * prod(map(length, caxes(c)))
cubesize(::YAXArray{T,0}) where {T} = sizeof(T)

getCubeDes(::DD.Dimension) = "Cube axis"
getCubeDes(::YAXArray) = "YAXArray"
getCubeDes(::Type{T}) where {T} = string(T)

function DD.show_after(io::IO,mime, c::YAXArray)
foreach(getattributes(c)) do p
if p[1] in ("labels", "name", "units")
Expand Down
3 changes: 1 addition & 2 deletions src/Cubes/Slices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ function getdata(x::YAXSlice)
PickAxisArray(x.c.data, m)
end

getCubeDes(s::YAXSlice) =
string(join(axname.(s.sliceaxes[2]), " x "), " slices over an ", getCubeDes(s.c))

cubesize(s::YAXSlice) = cubesize(s.c)
Base.ndims(::YAXSlice{<:Any,N}) where {N} = N

Expand Down
2 changes: 0 additions & 2 deletions src/DAT/DAT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ using Distributed:
import ..Cubes: cubechunks, iscompressed, chunkoffset, YAXArray, caxes, YAXSlice
import ..Cubes: cubechunks, iscompressed, chunkoffset, YAXArray, caxes, YAXSlice
import ..YAXArrays: findAxis, getOutAxis, getAxis
#import ..Cubes.Axes:
# AxisDescriptor, axname, ByInference, axsym, getOutAxis, getAxis, findAxis, match_axis
import ..Datasets: Dataset, createdataset
using ..YAXArrays: ByInference, YAXArrays
import ...YAXArrays.workdir
Expand Down
1 change: 0 additions & 1 deletion src/DAT/dciterators.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import YAXArrays.DAT: DATConfig
import YAXArrays.YAXTools: PickAxisArray
#using YAXArrays.Cubes.Axes: axcopy
using DiskArrays: GridChunks, AbstractDiskArray
using Tables: Tables, Schema, AbstractColumns

Expand Down
1 change: 0 additions & 1 deletion src/DAT/registration.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export InDims, OutDims, MovingWindow
#using ..Cubes.Axes: get_descriptor, findAxis, Axes
import ..YAXArrays: get_descriptor, findAxis, AxisDescriptor
using ..YAXArrays: YAXDefaults
using YAXArrayBase: yaxcreate
Expand Down
1 change: 0 additions & 1 deletion src/DAT/tablestats.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import OnlineStats: OnlineStat, Extrema, fit!, value, HistogramStat, Ash
#import ...Cubes.Axes: CategoricalAxis, RangeAxis
import IterTools
using WeightedOnlineStats
using Distributed: nworkers
Expand Down
4 changes: 1 addition & 3 deletions src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Datasets
#import ..Cubes.Axes: axsym, axname, CubeAxis, findAxis, CategoricalAxis, RangeAxis, caxes
import ..Cubes: Cubes, YAXArray, concatenatecubes, CleanMe, subsetcube, copy_diskarray, setchunks, caxes
import ..Cubes: Cubes, YAXArray, concatenatecubes, CleanMe, copy_diskarray, setchunks, caxes
using ...YAXArrays: YAXArrays, YAXDefaults, findAxis
using DataStructures: OrderedDict, counter
using Dates: Day, Hour, Minute, Second, Month, Year, Date, DateTime, TimeType, AbstractDateTime
Expand Down Expand Up @@ -327,7 +326,6 @@ function open_dataset(g; driver = :all)
sdimlist = Dict(Symbol(k) => v.ax for (k, v) in dimlist)
Dataset(allcubes, sdimlist,gatts)
end
#Base.getindex(x::Dataset; kwargs...) = subsetcube(x; kwargs...)
YAXDataset(; kwargs...) = Dataset(YAXArrays.YAXDefaults.cubedir[]; kwargs...)


Expand Down
108 changes: 0 additions & 108 deletions test/Cubes/axes.jl

This file was deleted.

25 changes: 1 addition & 24 deletions test/Cubes/cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,5 @@ using DimensionalData
@test a2.properties == Dict("att" => 5)
@test YAXArrayBase.iscompressed(a) == false
end
#=
@testset "Subsets" begin
s = YAXArrays.Cubes.subsetcube(a, X = 1.5..3.5)
@test s.data == [2 6 10 14 18; 3 7 11 15 19]
@test s.axes[1] == X(2.0:3.0)
@test s.axes[2] == Y([1, 2, 3, 4, 5])
ax = a.axes[1]
@test YAXArrays.Cubes.interpretsubset(CartesianIndices((1:2,)), ax) == 1:2
@test YAXArrays.Cubes.interpretsubset(CartesianIndex((2,)), ax) == 2
@test YAXArrays.Cubes.interpretsubset(2.1, ax) == 2
@test YAXArrays.Cubes.interpretsubset((3.5, 1.5), ax) == 2:3
@test YAXArrays.Cubes.interpretsubset(0.8..2.2, ax) == 1:2
tax = RangeAxis("ADate", Date(2001):Day(1):Date(2003, 2, 28))
@test YAXArrays.Cubes.interpretsubset((Date(2001, 1, 2), Date(2001, 1, 5)), tax) ==
2:4
@test YAXArrays.Cubes.interpretsubset(2001:2002, tax) == 1:730
@test YAXArrays.Cubes.interpretsubset([1, 3, 5], a.axes[2]) == [1, 3, 5]

s2 = a[X = 0.5..3.5, Y = [1, 5, 4]]
@test s2.data == [1 17 13; 2 18 14; 3 19 15]
@test s2.axes[1] == X(1.0:3.0)
@test s2.axes[2] == Y([1, 5, 4])
end
=#
end
end
Loading