From 21f2156bcd49c526bd1d016ec13265ead0856054 Mon Sep 17 00:00:00 2001 From: Tom Short Date: Fri, 20 Feb 2015 09:57:08 -0500 Subject: [PATCH] Add optional utilities for conversion to a DataFrame and for plotting with Gadfly. Note that this adds a dependency to the Requires package. --- REQUIRE | 2 ++ src/AxisArrays.jl | 3 +++ src/utils.jl | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 REQUIRE create mode 100644 src/utils.jl diff --git a/REQUIRE b/REQUIRE new file mode 100644 index 0000000..85663ec --- /dev/null +++ b/REQUIRE @@ -0,0 +1,2 @@ +julia 0.4- +Requires \ No newline at end of file diff --git a/src/AxisArrays.jl b/src/AxisArrays.jl index 1eaef79..4d5938b 100644 --- a/src/AxisArrays.jl +++ b/src/AxisArrays.jl @@ -1,9 +1,12 @@ module AxisArrays +using Requires + export AxisArray, Axis, Interval, axisnames include("core.jl") include("intervals.jl") include("indexing.jl") +include("utils.jl") end diff --git a/src/utils.jl b/src/utils.jl new file mode 100644 index 0000000..0dbe084 --- /dev/null +++ b/src/utils.jl @@ -0,0 +1,67 @@ +@require DataFrames begin + + """ + Convert an AxisArray into a long-format DataFrame. + + ```julia + DataFrame(a::AxisArray) + ``` + + ### Arguments + + * `a::AxisArray` + + ### Returns + + * `::DataFrame` : a DataFrame view into the AxisArray; columns are + added for each axis plus one column for the data (named `:data`). + + """ + function DataFrames.DataFrame(A::AxisArray) + colnames = Symbol[axisnames(A)..., :data] + sz = [1; size(A)...; 1] + columns = Any[DataFrames.RepeatedVector(a, prod(sz[1:i]), prod(sz[i+2:end])) for (i,a) in enumerate(axes(A))] + push!(columns, sub(A.data, 1:prod(sz))) + DataFrames.DataFrame(columns, colnames) + end + +end + + +@require Gadfly begin + + import DataArrays + ## Low-level code patching + function Gadfly.Scale.discretize_make_pda(values::DataFrames.RepeatedVector, levels=nothing) + if levels == nothing + return DataArrays.PooledDataArray(values) + else + return DataArrays.PooledDataArray(values[:], levels) + end + end + + """ + Plot an AxisArray using Gadfly. + + ```julia + Gadfly.plot(A::AxisArray, args...; kargs...) + ``` + + ### Arguments + + * `A::AxisArray` + + All other arguments are passed to Gadfly.plot. + + ### Examples + + ```julia + using Gadfly + A = AxisArray(reshape([1:24], 12,2), (.1:.1:1.2, [:a,:b])) + plot(A, x = :row, y = :data, color = :col) + ``` + + """ + Gadfly.plot(A::AxisArray, args...; kwargs...) = Gadfly.plot(DataFrames.DataFrame(A), args...; kwargs...) + +end