Skip to content

Commit

Permalink
Merge pull request #69 from rafaqz/cleanup_dims
Browse files Browse the repository at this point in the history
clean up dim names and test use of abstract types in primitives
  • Loading branch information
rafaqz authored Feb 9, 2020
2 parents eea0fc0 + 4aa9df3 commit 44bde9d
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 97 deletions.
11 changes: 5 additions & 6 deletions src/DimensionalData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ and selecting data from dimension values instead of using indices directly.
## Dimensions
Dimensions are simply types that wrap values. They both store dimension values
and are used for dimension lookup or indices, ranges or dimension number.
`X`, `Y`, `Z` and `Time` are the unexported defaults, add this line to use them:
```julia
using DimensionalData: X, Y, Z, Time
```
Dimensions are simply types that wrap values. They store the dimension index
and define details about the grid and other metadata, and are also used
to index into the array. `X`, `Y`, `Z` and `Ti` are the exported defaults.
A generalised [`Dim`](@ref) type is available to use arbitrary symbols to name dimensions.
Custom dimensions can be defined using the [`@dim`](@ref) macro.
Expand Down Expand Up @@ -83,6 +80,8 @@ export AbstractDimensionalArray, DimensionalArray
export data, dims, refdims, metadata, name, shortname,
val, label, units, order, bounds, locus, grid, <|

export dimnum, hasdim, setdim

export @dim

include("interface.jl")
Expand Down
5 changes: 2 additions & 3 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ Example:
```
using Dates, DimensionalData
using DimensionalData: Time, X
timespan = DateTime(2001):Month(1):DateTime(2001,12)
A = DimensionalArray(rand(12,10), (Time(timespan), X(10:10:100)))
A[X<|Near([12, 35]), Time<|At(DateTime(2001,5))]
A = DimensionalArray(rand(12,10), (Ti(timespan), X(10:10:100)))
A[X<|Near([12, 35]), Ti<|At(DateTime(2001,5))]
A[Near(DateTime(2001, 5, 4)), Between(20, 50)]
```
"""
Expand Down
2 changes: 1 addition & 1 deletion src/dimension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ end

dimmacro(typ, supertype, name=string(typ), shortname=string(typ)) =
esc(quote
struct $typ{T,G,M} <: AbstractDimension{T,G,M}
struct $typ{T,G,M} <: $supertype{T,G,M}
val::T
grid::G
metadata::M
Expand Down
4 changes: 2 additions & 2 deletions src/plotrecipes.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@recipe function f(ga::AbstractDimensionalArray{T,2,<:Tuple{<:AbstractDimension,<:Time}}) where T
@recipe function f(ga::AbstractDimensionalArray{T,2,<:Tuple{<:AbstractDimension,<:Ti}}) where T
ylabel --> label(ga)
xlabel --> label(dims(ga)[1])
legendtitle --> label(dims(ga)[1])
title --> label(refdims(ga))
parent(ga)
end

@recipe function f(ga::AbstractDimensionalArray{T,2,<:Tuple{<:Time,<:AbstractDimension}}) where T
@recipe function f(ga::AbstractDimensionalArray{T,2,<:Tuple{<:Ti,<:AbstractDimension}}) where T
permutedims(ga)
end

Expand Down
6 changes: 3 additions & 3 deletions test/array.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DimensionalData, Test, Unitful
using DimensionalData: X, Y, Z, Time, Start
using DimensionalData: Start

a = [1 2; 3 4]
dimz = (X((143, 145)), Y((-38, -36)))
Expand Down Expand Up @@ -131,12 +131,12 @@ end
end

@testset "broadcast" begin
da = DimensionalArray(ones(Int, 5, 2, 4), (Y((10, 20)), Time(10:11), X(1:4)))
da = DimensionalArray(ones(Int, 5, 2, 4), (Y((10, 20)), Ti(10:11), X(1:4)))
da2 = da .* 2.0
@test da2 == fill(2.0, 5, 2, 4)
@test eltype(da2) <: Float64
@test dims(da2) == (Y(LinRange(10, 20, 5); grid=RegularGrid(;step=2.5)),
Time(10:11; grid=RegularGrid(;step=1)),
Ti(10:11; grid=RegularGrid(;step=1)),
X(1:4; grid=RegularGrid(;step=1)))
da2 = da .+ fill(10, 5, 2, 4)
@test da2 == fill(11, 5, 2, 4)
Expand Down
4 changes: 2 additions & 2 deletions test/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ println("Dims with UnitRange")
@btime d3($g);

a = rand(5, 4, 3);
da = DimensionalArray(a, (Y((1u"m", 5u"m")), X(1:4), Time(1:3)))
da = DimensionalArray(a, (Y((1u"m", 5u"m")), X(1:4), Ti(1:3)))
dimz = dims(da)

if VERSION > v"1.1-"
Expand All @@ -81,7 +81,7 @@ println("\n\nmean: normal, numbers + rebuild, dims + rebuild")
println("permutedims: normal, numbers + rebuild, dims + rebuild")
@btime permutedims($a, (2, 1, 3))
@btime permutedims($da, (2, 1, 3))
@btime permutedims($da, (Y(), X(), Time()))
@btime permutedims($da, (Y(), X(), Ti()))
println("reverse: normal, numbers + rebuild, dims + rebuild")
@btime reverse($a; dims=1)
@btime reverse($da; dims=1)
Expand Down
6 changes: 3 additions & 3 deletions test/dimension.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DimensionalData, Test, Unitful
using DimensionalData: X, Y, Z, Time, Forward, @dim, slicedims, dimnum, hasdim
using DimensionalData: Forward, slicedims

@dim TestDim "Test dimension"

Expand Down Expand Up @@ -38,7 +38,7 @@ dimz = dims(da)
@test units(dimz) == (nothing, nothing)
@test label(dimz) == ("X, Y")

a = [1 2 3 4
a = [1 2 3 4
2 3 4 5
3 4 5 6]
dimz = X((143, 145)), Y((-38, -35))
Expand All @@ -48,7 +48,7 @@ da = DimensionalArray(a, dimz)
@test dims(dimz) === dimz
@test dims(dimz, X) === dimz[1]
@test dims(dimz, Y) === dimz[2]
@test_throws ArgumentError dims(dimz, Time)
@test_throws ArgumentError dims(dimz, Ti)
@test typeof(dims(da)) == Tuple{X{LinRange{Float64},RegularGrid{Ordered{Forward,Forward,Forward},Start,UnknownSampling,Float64},Nothing},
Y{LinRange{Float64},RegularGrid{Ordered{Forward,Forward,Forward},Start,UnknownSampling,Float64},Nothing}}
end
Expand Down
2 changes: 1 addition & 1 deletion test/grid.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DimensionalData, Test, Unitful
using DimensionalData: X, Y, Z, Time, Forward, Reverse,
using DimensionalData: Forward, Reverse,
reversearray, reverseindex, slicebounds, slicegrid, identify, orderof,
indexorder, arrayorder, relationorder

Expand Down
42 changes: 21 additions & 21 deletions test/methods.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using DimensionalData, Statistics, Test, Unitful, SparseArrays, Dates

using DimensionalData: X, Y, Z, Time, EmptyDim
using DimensionalData: EmptyDim

using LinearAlgebra: Transpose

@testset "*" begin
timespan = DateTime(2001):Month(1):DateTime(2001,12)
A1 = DimensionalArray(rand(12), (Time(timespan),))
A2 = DimensionalArray(rand(12, 1), (Time(timespan), X(10:10:10)))
A1 = DimensionalArray(rand(12), (Ti(timespan),))
A2 = DimensionalArray(rand(12, 1), (Ti(timespan), X(10:10:10)))

@test length.(dims(A1)) == size(A1)
@test dims(data(A1) * permutedims(A1)) isa Tuple{<:Time,<:Time}
@test dims(data(A1) * permutedims(A1)) isa Tuple{<:Ti,<:Ti}
@test data(A1) * permutedims(A1) == data(A1) * permutedims(data(A1))
@test dims(permutedims(A1) * data(A1)) isa Tuple{<:EmptyDim}
@test permutedims(A1) * data(A1) == permutedims(data(A1)) * data(A1)
Expand Down Expand Up @@ -126,19 +126,19 @@ if VERSION > v"1.1-"
3 4 5 6
5 6 7 8]
# eachslice
da = DimensionalArray(a, (Y((10, 30)), Time(1:4)))
@test [mean(s) for s in eachslice(da; dims=Time)] == [3.0, 4.0, 5.0, 6.0]
da = DimensionalArray(a, (Y((10, 30)), Ti(1:4)))
@test [mean(s) for s in eachslice(da; dims=Ti)] == [3.0, 4.0, 5.0, 6.0]
@test [mean(s) for s in eachslice(da; dims=2)] == [3.0, 4.0, 5.0, 6.0]
slices = [s .* 2 for s in eachslice(da; dims=Y)]
@test map(sin, da) == map(sin, a)
@test slices[1] == [2, 4, 6, 8]
@test slices[2] == [6, 8, 10, 12]
@test slices[3] == [10, 12, 14, 16]
dims(slices[1]) == (Time(1.0:1.0:4.0),)
slices = [s .* 2 for s in eachslice(da; dims=Time)]
dims(slices[1]) == (Ti(1.0:1.0:4.0),)
slices = [s .* 2 for s in eachslice(da; dims=Ti)]
@test slices[1] == [2, 6, 10]
dims(slices[1]) == (Y(10.0:10.0:30.0),)
@test_throws ArgumentError [s .* 2 for s in eachslice(da; dims=(Y, Time))]
@test_throws ArgumentError [s .* 2 for s in eachslice(da; dims=(Y, Ti))]
end
end

Expand Down Expand Up @@ -171,16 +171,16 @@ end


@testset "dimension reordering methods with specified permutation" begin
da = DimensionalArray(ones(5, 2, 4), (Y((10, 20)), Time(10:11), X(1:4)))
da = DimensionalArray(ones(5, 2, 4), (Y((10, 20)), Ti(10:11), X(1:4)))
dsp = permutedims(da, [3, 1, 2])

@test permutedims(da, [X, Y, Time]) == permutedims(da, (X, Y, Time))
@test permutedims(da, [X(), Y(), Time()]) == permutedims(da, (X(), Y(), Time()))
dsp = permutedims(da, (X(), Y(), Time()))
@test permutedims(da, [X, Y, Ti]) == permutedims(da, (X, Y, Ti))
@test permutedims(da, [X(), Y(), Ti()]) == permutedims(da, (X(), Y(), Ti()))
dsp = permutedims(da, (X(), Y(), Ti()))
@test dsp == permutedims(data(da), (3, 1, 2))
@test dims(dsp) == (X(1:4; grid=RegularGrid(;step=1)),
Y(LinRange(10.0, 20.0, 5); grid=RegularGrid(;step=2.5)),
Time(10:11; grid=RegularGrid(;step=1)))
Ti(10:11; grid=RegularGrid(;step=1)))
dsp = PermutedDimsArray(da, (3, 1, 2))
@test dsp == PermutedDimsArray(data(da), (3, 1, 2))
@test typeof(dsp) <: DimensionalArray
Expand All @@ -207,13 +207,13 @@ end
a = [1 2 3 4
3 4 5 6
5 6 7 8]
da = DimensionalArray(a, (Y((10, 30)), Time(1:4)))
da = DimensionalArray(a, (Y((10, 30)), Ti(1:4)))
ms = mapslices(sum, da; dims=Y)
@test ms == [9 12 15 18]
@test typeof(dims(ms)) == typeof((Y([10.0]; grid=RegularGrid(; step=30.0, sampling=IntervalSampling())),
Time(1:4; grid=RegularGrid(; step=1))))
Ti(1:4; grid=RegularGrid(; step=1))))
@test refdims(ms) == ()
ms = mapslices(sum, da; dims=Time)
ms = mapslices(sum, da; dims=Ti)
@test data(ms) == [10 18 26]'
end

Expand Down Expand Up @@ -244,10 +244,10 @@ end
@test grid.(dims(cat(da, db; dims=X()))) == grid.(testdims)
@test cat(da, db; dims=Y()) == [1 2 3 7 8 9; 4 5 6 10 11 12]
@test cat(da, db; dims=Z(1:2)) == cat(a, b; dims=3)
@test cat(da, db; dims=(Z(1:1), Time(1:2))) == cat(a, b; dims=4)
@test cat(da, db; dims=(X(), Time(1:2))) == cat(a, b; dims=3)
dx = cat(da, db; dims=(X(), Time(1:2)))
@test dims(dx) == DimensionalData.formatdims(dx, (X(1:2), Y(1:3), Time(1:2)))
@test cat(da, db; dims=(Z(1:1), Ti(1:2))) == cat(a, b; dims=4)
@test cat(da, db; dims=(X(), Ti(1:2))) == cat(a, b; dims=3)
dx = cat(da, db; dims=(X(), Ti(1:2)))
@test dims(dx) == DimensionalData.formatdims(dx, (X(1:2), Y(1:3), Ti(1:2)))
end

@testset "unique" begin
Expand Down
3 changes: 1 addition & 2 deletions test/prettyprinting.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using DimensionalData, Test
using DimensionalData: Time, Z, @dim
using Dates: DateTime, Month

# define dims with both long name and Type name
@dim Lon "Longitude" "lon"
@dim Lat "Latitude" "lat"

timespan = DateTime(2001):Month(1):DateTime(2001,12)
t = Time(timespan)
t = Ti(timespan)
x = Lon(Vector(0.5:1.0:359.5))
y = Lat(Vector{Union{Float32, Missing}}(-89.5:1.0:89.5))
z = Z('a':'z')
Expand Down
26 changes: 17 additions & 9 deletions test/primitives.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using DimensionalData, Test

using DimensionalData: val, basetypeof, slicedims, dims2indices, formatdims, hasdim, setdim,
@dim, reducedims, dimnum, X, Y, Z, Time, Forward
@dim, reducedims, dimnum, XDim, YDim, ZDim, Forward

dimz = (X(), Y())

Expand Down Expand Up @@ -36,7 +36,7 @@ end
@test dims2indices(dimz, (Y(),), emptyval) == (Colon(), Colon())
@test dims2indices(dimz, (Y(1),), emptyval) == (Colon(), 1)
# Time is just ignored if it's not in dims. Should this be an error?
@test dims2indices(dimz, (Time(4), X(2))) == (2, Colon())
@test dims2indices(dimz, (Ti(4), X(2))) == (2, Colon())
@test dims2indices(dimz, (Y(2), X(3:7)), emptyval) == (3:7, 2)
@test dims2indices(dimz, (X(2), Y([1, 3, 4])), emptyval) == (2, [1, 3, 4])
@test dims2indices(da, (X(2), Y([1, 3, 4])), emptyval) == (2, [1, 3, 4])
Expand All @@ -46,16 +46,16 @@ end
@test dims2indices(da, X, emptyval) == (Colon(), ())
@test dims2indices(da, (1:3, [1, 2, 3]), emptyval) == (1:3, [1, 2, 3])
@test dims2indices(da, 1, emptyval) == (1, )
tdimz = Dim{:trans1}(nothing; grid=TransformedGrid(X())), Dim{:trans2}(nothing, grid=TransformedGrid(Y())), Time(1:1)
@test dims2indices(tdimz, (X(1), Y(2), Time())) == (1, 2, Colon())
@test dims2indices(tdimz, (Dim{:trans1}(1), Dim{:trans2}(2), Time())) == (1, 2, Colon())
tdimz = Dim{:trans1}(nothing; grid=TransformedGrid(X())), Dim{:trans2}(nothing, grid=TransformedGrid(Y())), Ti(1:1)
@test dims2indices(tdimz, (X(1), Y(2), Ti())) == (1, 2, Colon())
@test dims2indices(tdimz, (Dim{:trans1}(1), Dim{:trans2}(2), Ti())) == (1, 2, Colon())
end

@testset "dimnum" begin
@test dimnum(da, X) == 1
@test dimnum(da, Y()) == 2
@test dimnum(da, (Y, X())) == (2, 1)
@test_throws ArgumentError dimnum(da, Time) == (2, 1)
@test_throws ArgumentError dimnum(da, Ti) == (2, 1)
end

@testset "reducedims" begin
Expand Down Expand Up @@ -90,17 +90,25 @@ end
@test dims(dims(da), (2, 1)) isa Tuple{<:Y,<:X}
@test dims(dims(da), (2, Y)) isa Tuple{<:Y,<:Y}
@test dims(da, ()) == ()
@test_throws ArgumentError dims(da, Time)
@test_throws ArgumentError dims(da, Ti)
x = dims(da, X)
@test dims(x) == x
end

@testset "hasdims" begin
@test hasdim(da, X) == true
@test hasdim(da, Time) == false
@test hasdim(da, Ti) == false
@test hasdim(dims(da), Y) == true
@test hasdim(dims(da), (X, Y)) == (true, true)
@test hasdim(dims(da), (X, Time)) == (true, false)
@test hasdim(dims(da), (X, Ti)) == (true, false)

# Abstract
@test hasdim(dims(da), (XDim, YDim)) == (true, true)
# TODO : should this actually be (true, false) ?
# Do we remove the second one for hasdim as well?
@test hasdim(dims(da), (XDim, XDim)) == (true, true)
@test hasdim(dims(da), (ZDim, YDim)) == (false, true)
@test hasdim(dims(da), (ZDim, ZDim)) == (false, false)
end

@testset "setdim" begin
Expand Down
Loading

0 comments on commit 44bde9d

Please sign in to comment.