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

no more Ti #479

Open
wants to merge 4 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
42 changes: 21 additions & 21 deletions docs/src/UserGuide/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ nothing # hide
````julia
function weighted_seasons(ds)
# calculate weights
tempo = dims(ds, :Ti)
tempo = dims(ds, :time)
month_length = YAXArray((tempo,), daysinmonth.(tempo))
g_tempo = groupby(month_length, Ti => seasons(; start=December))
sum_days = sum.(g_tempo, dims=:Ti)
g_tempo = groupby(month_length, Dim{:time} => seasons(; start=December))
sum_days = sum.(g_tempo, dims=:time)
weights = map(./, g_tempo, sum_days)
# unweighted seasons
g_ds = groupby(ds, Ti => seasons(; start=December))
mean_g = mean.(g_ds, dims=:Ti)
mean_g = dropdims.(mean_g, dims=:Ti)
g_ds = groupby(ds, Dim{:time} => seasons(; start=December))
mean_g = mean.(g_ds, dims=:time)
mean_g = dropdims.(mean_g, dims=:time)
# weighted seasons
g_dsW = broadcast_dims.(*, weights, g_ds)
weighted_g = sum.(g_dsW, dims = :Ti);
weighted_g = dropdims.(weighted_g, dims=:Ti)
weighted_g = sum.(g_dsW, dims = :time);
weighted_g = dropdims.(weighted_g, dims=:time)
# differences
diff_g = map(.-, weighted_g, mean_g)
seasons_g = lookup(mean_g, :Ti)
seasons_g = lookup(mean_g, :time)
return mean_g, weighted_g, diff_g, seasons_g
end
````
Expand All @@ -74,29 +74,29 @@ end
Now, we continue with the `groupby` operations as usual

````@ansi compareXarray
g_ds = groupby(ds, Ti => seasons(; start=December))
g_ds = groupby(ds, Dim{:time} => seasons(; start=December))
````

And the mean per season is calculated as follows

````@ansi compareXarray
mean_g = mean.(g_ds, dims=:Ti)
mean_g = mean.(g_ds, dims=:time)
````

### dropdims

Note that now the time dimension has length one, we can use `dropdims` to remove it

````@ansi compareXarray
mean_g = dropdims.(mean_g, dims=:Ti)
mean_g = dropdims.(mean_g, dims=:time)
````

### seasons

Due to the `groupby` function we will obtain new grouping names, in this case in the time dimension:

````@example compareXarray
seasons_g = lookup(mean_g, :Ti)
seasons_g = lookup(mean_g, :time)
````

Next, we will weight this grouping by days/month in each group.
Expand All @@ -106,20 +106,20 @@ Next, we will weight this grouping by days/month in each group.
Create a `YAXArray` for the month length

````@example compareXarray
tempo = dims(ds, :Ti)
tempo = dims(ds, :time)
month_length = YAXArray((tempo,), daysinmonth.(tempo))
````

Now group it by season

````@ansi compareXarray
g_tempo = groupby(month_length, Ti => seasons(; start=December))
g_tempo = groupby(month_length, Dim{:time} => seasons(; start=December))
````

Get the number of days per season

````@ansi compareXarray
sum_days = sum.(g_tempo, dims=:Ti)
sum_days = sum.(g_tempo, dims=:time)
````

### weights
Expand All @@ -146,8 +146,8 @@ g_dsW = broadcast_dims.(*, weights, g_ds)
apply a `sum` over the time dimension and drop it

````@ansi compareXarray
weighted_g = sum.(g_dsW, dims = :Ti);
weighted_g = dropdims.(weighted_g, dims=:Ti)
weighted_g = sum.(g_dsW, dims = :time);
weighted_g = dropdims.(weighted_g, dims=:time)
````

Calculate the differences
Expand Down Expand Up @@ -181,9 +181,9 @@ with_theme(theme_ggplot2()) do
fig = Figure(; size = (850,500))
axs = [Axis(fig[i,j], aspect=DataAspect()) for i in 1:3, j in 1:4]
for (j, s) in enumerate(seasons_g)
hm_o = heatmap!(axs[1,j], mean_g[Ti=At(s)]; colorrange, lowclip, highclip, colormap)
hm_w = heatmap!(axs[2,j], weighted_g[Ti=At(s)]; colorrange, lowclip, highclip, colormap)
hm_d = heatmap!(axs[3,j], diff_g[Ti=At(s)]; colorrange=(-0.1,0.1), lowclip, highclip,
hm_o = heatmap!(axs[1,j], mean_g[time=At(s)]; colorrange, lowclip, highclip, colormap)
hm_w = heatmap!(axs[2,j], weighted_g[time=At(s)]; colorrange, lowclip, highclip, colormap)
hm_d = heatmap!(axs[3,j], diff_g[time=At(s)]; colorrange=(-0.1,0.1), lowclip, highclip,
colormap=:diverging_bwr_20_95_c54_n256)
end
Colorbar(fig[1:2,5], hm_o, label=cb_label)
Expand Down
9 changes: 1 addition & 8 deletions src/Cubes/Cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,7 @@ function Base.getindex(a::YAXArray, args::DD.Dimension...; kwargs...)
for (k,v) in kwargsdict
d = getAxis(k,a)
if d !== nothing
if d isa DD.Ti
if v isa UnitRange{Int}
v = Date(first(v))..Date(last(v),12,31)
end
d2[:Ti] = v
felixcremer marked this conversation as resolved.
Show resolved Hide resolved
else
d2[DD.name(d)] = v
end
d2[DD.name(d)] = v
else
d2[k] = v
end
Expand Down
2 changes: 1 addition & 1 deletion src/DatasetAPI/Datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function toaxis(dimname, g, offs, len)
catch
ar[:]
end
DD.Ti(tsteps[offs+1:end])
DD.rebuild(DD.name2dim(axname), tsteps[offs+1:end])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the axis that we get here a subtype of Ti?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should not do this, since it would only work with eval anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use the @dim macro from DimensionalData to define this subtyping and as far as I can see this is not using eval for that.

The point of making it a subtype is, that in the analysis packages we can use Ti to select a time series no matter what the time axis is named in a dataset.

elseif haskey(aratts, "_ARRAYVALUES")
vals = identity.(aratts["_ARRAYVALUES"])
DD.rebuild(DD.name2dim(axname),(vals))
Expand Down
6 changes: 3 additions & 3 deletions test/Datasets/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using Dates
YAXArray(axlist2, data[3], props[3]),
)
ds = Dataset(avar=c1, something=c2, smaller=c3)
# previous version will throw this error: `KeyError: key :Ti not found`
# previous version will throw this error: `KeyError: key :time not found`
f = "./temp.zarr"
@test_nowarn savedataset(ds; path=f)
rm(f, recursive=true, force=true)
Expand Down Expand Up @@ -215,11 +215,11 @@ end
ds = open_dataset("test.mock")
@test size(ds.Var1) == (10, 5, 2)
@test size(ds.Var2) == (10, 5)
@test all(in(keys(ds.axes)), (:Ti, :d2, :d3))
@test all(in(keys(ds.axes)), (:time, :d2, :d3))
ar = Cube(ds)
@test ar isa YAXArray
@test size(ar) == (10, 5, 2, 2)
@test DD.name.(ar.axes) == (:Ti, :d2, :d3, :Variable)
@test DD.name.(ar.axes) == (:time, :d2, :d3, :Variable)
@test DD.lookup(ar.axes[4]) == ["Var1", "Var3"]
end
@testset "Dataset creation" begin
Expand Down
Loading