Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rafaqz/DimensionalData.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Jan 25, 2020
2 parents 749b8a6 + 3189afa commit 0817075
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 33 deletions.
15 changes: 8 additions & 7 deletions src/DimensionalData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ and selecting data from dimension values instead of using indices directly.
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:
`X`, `Y`, `Z` and `Time` are the unexported defaults, add this line to use them:
```julia
using DimensionalData: X, Y, Z, Time
```
A generalised [`Dim`](@ref) type is available to use arbitrary symbols to name dimensions.
A generalised [`Dim`](@ref) type is available to use arbitrary symbols to name dimensions.
Custom dimensions can be defined using the [`@dim`](@ref) macro.
We can use dim wrappers for indexing, so that the dimension order in the underlying array
We can use dim wrappers for indexing, so that the dimension order in the underlying array
does not need to be known:
```
Expand All @@ -40,7 +40,7 @@ usin Unitful
a[Near(23u"s"), Between(10.5u"m", 50.5u"m")]
```
Dim types or objects can be used instead of a dimension number in many
Dim types or objects can be used instead of a dimension number in many
Base and Statistics methods:
```julia
Expand All @@ -65,17 +65,17 @@ export Sampling, SingleSample, MultiSample, UnknownSampling

export Order, Ordered, Unordered

export Grid, UnknownGrid
export Grid, UnknownGrid

export AbstractCategoricalGrid, CategoricalGrid
export AbstractCategoricalGrid, CategoricalGrid

export IndependentGrid, AbstractAlignedGrid, AlignedGrid, BoundedGrid, RegularGrid

export DependentGrid, TransformedGrid

export AbstractDimensionalArray, DimensionalArray

export data, dims, refdims, metadata, name, shortname,
export data, dims, refdims, metadata, name, shortname,
val, label, units, order, bounds, locus, grid, <|

include("interface.jl")
Expand All @@ -87,5 +87,6 @@ include("methods.jl")
include("primitives.jl")
include("utils.jl")
include("plotrecipes.jl")
include("prettyprint.jl")

end
21 changes: 6 additions & 15 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ dims(A::AbDimArray) = A.dims

Base.size(A::AbDimArray) = size(data(A))
Base.iterate(A::AbDimArray, args...) = iterate(data(A), args...)
Base.show(io::IO, A::AbDimArray) = begin
printstyled(io, label(A), ": "; color=:red)
show(io, typeof(A))
show(io, data(A))
printstyled(io, "\ndims: "; color=:magenta)
show(io, dims(A))
show(io, refdims(A))
printstyled(io, "\nmetadata: "; color=:cyan)
end

Base.@propagate_inbounds Base.getindex(A::AbDimArray, I::Vararg{<:Integer}) =
getindex(data(A), I...)
Expand All @@ -41,7 +32,7 @@ Base.BroadcastStyle(::Type{<:AbDimArray}) = Broadcast.ArrayStyle{AbDimArray}()

Base.similar(A::AbDimArray) = rebuild(A, similar(data(A)))
Base.similar(A::AbDimArray, ::Type{T}) where T = rebuild(A, similar(data(A), T))
Base.similar(A::AbDimArray, ::Type{T}, I::Tuple{Int64,Vararg{Int64}}) where T =
Base.similar(A::AbDimArray, ::Type{T}, I::Tuple{Int64,Vararg{Int64}}) where T =
rebuild(A, similar(data(A), T, I))
Base.similar(A::AbDimArray, ::Type{T}, I::Tuple{Union{Integer,AbstractRange},Vararg{Union{Integer,AbstractRange},N}}) where {T,N} =
rebuildsliced(A, similar(data(A), T, I...), I)
Expand All @@ -68,7 +59,7 @@ end
# Concrete implementation ######################################################

"""
DimensionalArray(A::AbstractArray, dims::Tuple, refdims::Tuple)
DimensionalArray(A::AbstractArray, dims::Tuple, refdims::Tuple)
A basic DimensionalArray type.
Expand All @@ -80,7 +71,7 @@ struct DimensionalArray{T,N,D<:Tuple,R<:Tuple,A<:AbstractArray{T,N}} <: Abstract
refdims::R
end
"""
DimensionalArray(A::AbstractArray, dims::Tuple; refdims=())
DimensionalArray(A::AbstractArray, dims::Tuple; refdims=())
Constructor with optional `refdims` keyword.
Example:
Expand All @@ -89,20 +80,20 @@ 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 = DimensionalArray(rand(12,10), (Time(timespan), X(10:10:100)))
A[X<|Near([12, 35]), Time<|At(DateTime(2001,5))]
A[Near(DateTime(2001, 5, 4)), Between(20, 50)]
```
"""
DimensionalArray(A::AbstractArray, dims; refdims=()) =
DimensionalArray(A::AbstractArray, dims; refdims=()) =
DimensionalArray(A, formatdims(A, dims), refdims)

# Getters
refdims(A::DimensionalArray) = A.refdims
data(A::DimensionalArray) = A.data

# DimensionalArray interface
@inline rebuild(A::DimensionalArray, data, dims, refdims) =
@inline rebuild(A::DimensionalArray, data, dims, refdims) =
DimensionalArray(data, dims, refdims)

# Array interface (AbstractDimensionalArray takes care of everything else)
Expand Down
11 changes: 0 additions & 11 deletions src/dimension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ Base.:(==)(dim1::AbDim, dim2::AbDim) =
grid(dim1) == grid(dim2) &&
metadata(dim1) == metadata(dim2)

Base.show(io::IO, dim::AbDim) = begin
printstyled(io, "\n", name(dim), ": "; color=:red)
show(io, typeof(dim))
printstyled(io, "\nval: "; color=:green)
show(io, val(dim))
printstyled(io, "\ngrid: "; color=:yellow)
show(io, grid(dim))
printstyled(io, "\nmetadata: "; color=:blue)
show(io, metadata(dim))
end

# AbstractArray methods where dims are the dispatch argument

@inline rebuildsliced(A, data, I) = rebuild(A, data, slicedims(A, I)...)
Expand Down
48 changes: 48 additions & 0 deletions src/prettyprint.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Credit to Sebastian Pfitzner for `printlimited`
function printlimited(io, x; Δx = 0)
sz = displaysize(io)
ctx = IOContext(io, :limit => true, :compact => true,
:displaysize => (1, sz[2]-Δx))
Base.print(ctx, x)
end

function Base.show(io::IO, A::AbDimArray)
l = label(A) != "" ? label(A) : nameof(typeof(A))
printstyled(io, l; color=:magenta)
print(io, " with dimensions:\n")
for d in dims(A)
print(io, " ", d, "\n")
end
if !isempty(refdims(A))
print(io, "and referenced dimensions:\n")
for d in refdims(A)
print(io, " ", d, "\n")
end
end
print(io, "and")
printstyled(io, " data: "; color=:green)
show(IOContext(io, :compact => true), MIME("text/plain"), data(A))
end

Base.show(io::IO, ::MIME"text/plain", A::AbDimArray) = show(io, A)

# Full printing version for dimensions
function Base.show(io::IO, ::MIME"text/plain", dim::AbDim)
print(io, "dimension ")
printstyled(io, name(dim); color=:red)
printstyled(io, "\nval: "; color=:green)
printlimited(io, val(dim); Δx = 5)
printstyled(io, "\ngrid: "; color=:yellow)
show(io, grid(dim))
printstyled(io, "\nmetadata: "; color=:blue)
show(io, metadata(dim))
printstyled(io, "\ntype: "; color=:cyan)
show(io, typeof(dim))
end

# short printing version for dimensions
function Base.show(io::IO, dim::AbDim)
printstyled(io, name(dim), ": "; color=:red)
Δx = length(string(nameof(typeof(dim)))) + 2
printlimited(io, val(dim); Δx = Δx)
end
15 changes: 15 additions & 0 deletions test/prettyprinting.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Dates, DimensionalData
using DimensionalData: Time, X
timespan = DateTime(2001):Month(1):DateTime(2001,12)
t = Time(timespan)
x = X(Vector(10:10:500))
A = DimensionalArray(rand(12,length(x)), (t, x))

s1 = sprint(show, A)
s2 = sprint(show, x)
s3 = sprint(show, MIME("text/plain"), x)

@test occursin("DimensionalArray with dimensions:", s1)
@test occursin("X", s1)
@test occursin("X:", s2)
@test occursin("dimension X", s3)
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ include("grid.jl")
include("selector.jl")
include("methods.jl")
include("benchmarks.jl")
include("prettyprinting.jl")

0 comments on commit 0817075

Please sign in to comment.