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

Add optional utilities for conversion to a DataFrame and for plotting with Gadfly #5

Merged
merged 2 commits into from
Feb 20, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
julia 0.4-
Requires
3 changes: 3 additions & 0 deletions src/AxisArrays.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module AxisArrays

using Requires

export AxisArray, Axis, Interval, axisnames, axisdim, axes

include("core.jl")
include("intervals.jl")
include("indexing.jl")
include("utils.jl")

end
67 changes: 67 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -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