Skip to content

Commit

Permalink
Merge pull request #137 from rafaqz/fix_plots
Browse files Browse the repository at this point in the history
Fix plots
  • Loading branch information
rafaqz authored Jun 3, 2020
2 parents 881cf47 + bcc2386 commit 7fccc4c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Base.size(A::AbDimArray) = size(data(A))
Base.axes(A::AbDimArray) = axes(data(A))
Base.iterate(A::AbDimArray, args...) = iterate(data(A), args...)
Base.IndexStyle(A::AbstractDimensionalArray) = Base.IndexStyle(data(A))
Base.parent(A::AbDimArray) = data(A)

Base.@propagate_inbounds Base.getindex(A::AbDimArray, I::StandardIndices...) =
rebuildsliced(A, getindex(data(A), I...), I)
Expand Down Expand Up @@ -152,6 +153,11 @@ julia> A[Near(DateTime(2001, 5, 4)), Between(20, 50)];
"""
DimensionalArray(A::AbstractArray, dims, name::String=""; refdims=(), metadata=nothing) =
DimensionalArray(A, formatdims(A, _to_tuple(dims)), refdims, name, metadata)
DimensionalArray(A::AbstractDimensionalArray; dims=dims(A), refdims=refdims(A), name=name(A), metadata=metadata(A)) =
DimensionalArray(A, formatdims(data(A), _to_tuple(dims)), refdims, name, metadata)
DimensionalArray(; data, dims, refdims=(), name="", metadata=nothing) =
DimensionalArray(A, formatdims(A, _to_tuple(dims)), refdims, name, metadata)

_to_tuple(t::T where T <: Tuple) = t
_to_tuple(t) = tuple(t)

Expand Down
3 changes: 2 additions & 1 deletion src/mode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ _orderof(index::AbstractArray) = begin
end
sorted ? Ordered(index=indord) : Unordered()
end

_indexorder(index::AbstractArray) =
first(index) <= last(index) ? Forward() : Reverse()
Expand All @@ -546,7 +547,7 @@ identify(span::Regular, dimtype::Type, index::AbstractArray) =
identify(span::Regular{AutoStep}, dimtype::Type, index::AbstractRange) =
Regular(step(index))
identify(span::Regular, dimtype::Type, index::AbstractRange) = begin
step(span) step(index) || throw(ArgumentError("mode step $(step(span)) does not match index step $(step(index))"))
step(span) isa Number && !(step(span) step(index)) && throw(ArgumentError("mode step $(step(span)) does not match index step $(step(index))"))
span
end
identify(span::Irregular{Nothing}, dimtype, index) =
Expand Down
20 changes: 15 additions & 5 deletions src/plotrecipes.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
struct HeatMapLike end
struct ImageLike end
struct WireframeLike end
struct SeriesLike end
struct HistogramLike end
struct ViolinLike end

@recipe function f(A::AbstractDimensionalArray)
struct DimensionalPlot end

@recipe function f(A::AbstractDimensionalArray)
DimensionalPlot(), A
end

@recipe function f(::DimensionalPlot, A::AbstractArray)
Afwd = forwardorder(A)
sertype = get(plotattributes, :seriestype, :none)
if !(sertype in [:marginalhist])
:title --> refdims_title(Afwd)
end
if sertype in [:heatmap, :contour, :volume, :marginalhist, :image,
if sertype in [:heatmap, :contour, :volume, :marginalhist,
:surface, :contour3d, :wireframe, :scatter3d]
HeatMapLike(), Afwd
elseif sertype in [:histogram, :stephist, :density, :barhist, :scatterhist, :ea_histogram]
Expand All @@ -31,7 +38,7 @@ struct ViolinLike end
end
end

@recipe function f(::SeriesLike, A::AbstractDimensionalArray{T,1}) where T
@recipe function f(::SeriesLike, A::AbstractArray{T,1}) where T
dim = dims(A, 1)
:ylabel --> label(A)
:xlabel --> label(dim)
Expand Down Expand Up @@ -90,7 +97,11 @@ end
:ylabel --> label(y)
:zlabel --> label(A)
:colorbar_title --> label(A)
val(x), val(y), data(A)
data(A)
end

@recipe function f(::ImageLike, A::AbstractArray{T,2}) where T
data(A)
end

maybe_permute(A, dims) = all(hasdim(A, dims)) ? permutedims(A, dims) : A
Expand All @@ -102,7 +113,6 @@ refdims_title(A::AbstractArray) = join(map(refdims_title, refdims(A)), ", ")
refdims_title(dim::Dimension) = string(name(dim), ": ", refdims_title(mode(dim), dim))
refdims_title(mode::AbstractSampled, dim::Dimension) = begin
start, stop = map(string, bounds(dim))
println("bounds: ", bounds(dim))
if start == stop
start
else
Expand Down
14 changes: 9 additions & 5 deletions test/plotrecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sticks(da1)
histogram(da1)
stephist(da1)
barhist(da1)
heatmap(da1)
scatterhist(da1)
histogram2d(da1)
hline(da1)
Expand All @@ -35,6 +36,8 @@ da2 = DimensionalArray(A2, (X(1:10:400), Y(1:5:100)), "Normal")
# Plots
plot(da2)
bar(da2)
violin(da2)
boxplot(da2)
sticks(da2)
histogram(da2)
stephist(da2)
Expand All @@ -44,10 +47,11 @@ histogram2d(data(da2))
histogram2d(da2)
hline(da2)
vline(da2)
plot(da2; seriestype=:line)
# Don't display dims currently
heatmap(da2)
contour(da2)
wireframe(da2)
plot(da2; seriestype=:line)

# StatsPlots
density(da2)
Expand All @@ -57,7 +61,7 @@ violin(da2)
ea_histogram(da2)

# Not sure how recipes work for this
andrewsplot(da2)
# andrewsplot(da2)

# TODO handle everything

Expand All @@ -76,8 +80,8 @@ andrewsplot(da2)
# plot(data(da2); seriestype=:barbins)
# plot(data(da2); seriestype=:contour3d)
# pie(da2)

#
# Crashes GR for some reason
# im2 = RGB24.(rand(10, 10))
# da_im2 = DimensionalArray(im2, (X(1:10), Y(1:10)), "Image")
# plot(da_im2; seriestype=:image)
# da_im2 = DimensionalArray(im2, (X(10:10:100), Y(10:10:100)), "Image")
# da_im2 |> plot
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ include("methods.jl")
include("prettyprinting.jl")
if !Sys.iswindows()
include("plotrecipes.jl")
end

# Test documentation
docsetup = quote
using DimensionalData, Random
Random.seed!(1234)
# Test documentation
docsetup = quote
using DimensionalData, Random
Random.seed!(1234)
end
DocMeta.setdocmeta!(DimensionalData, :DocTestSetup, docsetup; recursive=true)
doctest(DimensionalData)
end
DocMeta.setdocmeta!(DimensionalData, :DocTestSetup, docsetup; recursive=true)
doctest(DimensionalData)
7 changes: 3 additions & 4 deletions test/selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ a = [1 2 3 4
5 6 7 8
9 10 11 12]

using DimensionalData
dims_ = X(10:10:20; mode=Sampled(sampling=Intervals())),
Y(5:7; mode=Sampled(sampling=Intervals()))
A = DimensionalArray([1 2 3; 4 5 6], dims_)
Expand Down Expand Up @@ -376,10 +375,10 @@ end
for idx in indices
from2d = view(da, idx)
@test from2d == view(data(da), idx)
@test !(parent(from2d) isa AbstractDimensionalArray)
from1d = view(da[Y <| At(10)], idx)
@test from2d isa SubArray
from1d = view(da[Y(At(10))], idx)
@test from1d == view(data(da)[1, :], idx)
@test parent(from1d) isa AbstractDimensionalArray
@test from1d isa AbstractDimensionalArray
end
end

Expand Down

0 comments on commit 7fccc4c

Please sign in to comment.