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

Merged
merged 14 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 14 additions & 14 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)
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)
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 @@ -80,23 +80,23 @@ g_ds = groupby(ds, Ti => 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,7 +106,7 @@ 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))
````

Expand All @@ -119,7 +119,7 @@ g_tempo = groupby(month_length, Ti => 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
2 changes: 1 addition & 1 deletion src/Cubes/Cubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ function Base.getindex(a::YAXArray, args::DD.Dimension...; kwargs...)
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
d2[:time] = v
else
d2[DD.name(d)] = 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