SplitApplyCombine.jl provides high-level, generic tools for manipulating data -
particularly focussing on data in nested containers. An emphasis is placed on ensuring
split-apply-combine strategies are easy to apply, and work reliably for arbitrary iterables
and in an optimized way with the data structures included in Julia's standard library.
The tools come in the form of high-level functions that operate on iterable or indexable
containers in an intuitive and simple way, extending Julia's in-built map, reduce and
filter commands to a wider range of operations. Just like these Base functions, the
functions here like invert, group and innerjoin are able to be overloaded and
optimized by users and the maintainers of other packages for their own, custom data
containers.
One side goal is to provide sufficient functionality to satisfy the need to manipulate
"relational" data (meaning tables and dataframes) with basic in-built Julia data containers
like Vectors of NamedTuples and higher-level functions in a "standard" Julia style.
Pay particular to the invert family of functions, which effectively allows you to switch
between a "struct-of-arrays" and an "array-of-structs" interpretation of your data. I am
exploring the idea of using arrays of named tuples for a fast table package in another
package under development called
MinimumViableTables), which adds
acceleration indexes but otherwise attempts to use a generic "native Julia" interface.
Quick start
You can install the package by typing
Pkg.add("SplitApplyCombine") at the REPL.
Below are some simple examples of how a select subset of the tools can be used to split,
manipulate, and combine data. A complete API reference is included at the end of this
README.
julia>using SplitApplyCombine
julia>only([3]) # return the one-and-only element of the input (included in Julia 1.4)3
julia>splitdims([123; 456]) # create nested arrays3-element Array{Array{Int64,1},1}:
[1, 4]
[2, 5]
[3, 6]
julia>combinedims([[1, 4], [2, 5], [3, 6]]) # flatten nested arrays2×3 Array{Int64,2}:123456
julia>invert([[1,2,3],[4,5,6]]) # invert the order of nesting3-element Array{Array{Int64,1},1}:
[1, 4]
[2, 5]
[3, 6]
julia>group(iseven, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # split elements into groups2-element Dictionary{Bool,Array{Int64,1}}
false │ [1, 3, 5, 7, 9]
true │ [2, 4, 6, 8, 10]
julia>groupreduce(iseven, +, 1:10) # like above, but performing reduction2-element Dictionary{Bool,Int64}
false │ 25true │ 30
julia>innerjoin(iseven, iseven, tuple, [1,2,3,4], [0,1,2]) # combine two datasets - related to SQL `inner join`6-element Array{Tuple{Int64,Int64},1}:
(1, 1)
(2, 0)
(2, 2)
(3, 1)
(4, 0)
(4, 2)
julia>leftgroupjoin(iseven, iseven, tuple, [1,2,3,4], [0,1,2]) # efficient groupings from two datasets
Dict{Bool,Array{Tuple{Int64,Int64},1}} with 2 entries:false=> Tuple{Int64,Int64}[(1, 1), (3, 1)]
true=> Tuple{Int64,Int64}[(2, 0), (2, 2), (4, 0), (4, 2)]
Tabular data
The primary interface for manipulating tabular data is the relational algebra. A
relation is typically defined as an (unordered) collection of (unique) (named) tuples.
If relations are collections of rows, and tables are to be viewed as relations, then I
suggest that tables should be viewed as collections of rows (and in particular they should
iterate rows and return an entire row from getindex, if defined).
While simple, this already allows quite a bit of relational algebra to occur. One can then
filter rows of a table, map rows of a table (to project, rename or create columns), and
use zip and product iterables for more complex operations. The goal below will be to
discuss functions which work well for general iterables and will be useful for a table
that iterates rows. As a prototype to keep in mind for this work, I consider an
AbstractVector{<:NamedTuple} to be a good model of (strongly-typed) a table/dataframe.
Specialized packages may provide convenient macro-based DSLs, a greater range of functions,
and implementations that focus on things such as out-of-core or distributed computing, more
flexible acceleration indexing, etc. Here I'm only considering the basic, bare-bones API
that may be extended and built upon by other packages.
Notes
This package recently switched from using the dictionaries in Base to those in the
Dictionaries.jl package, particularly
for the group family of functions.
API reference
The package currently implements and exports only, splitdims, splitdimsview,
combinedims, combinedimsview, mapview, filterview, mapmany, flatten, group, groupinds, groupview,
groupreduce, innerjoin and leftgroupjoin, as well as the @_ macro. Expect this list
to grow.
Generic operations on collections
only(iter)
Returns the single, one-and-only element of the collection iter. If it contains zero
elements or more than one element, an error is thrown.
Example:
julia>only([3])
3
julia>only([])
ERROR: ArgumentError: Collection must have exactly one element (input was empty)
Stacktrace:
[1] only(::Array{Any,1}) at /home/ferris/.julia/v0.7/SAC/src/only.jl:4
julia>single([3, 10])
ERROR: ArgumentError: Collection must have exactly one element (input contained more than one element)
Stacktrace:
[1] only(::Array{Int64,1}) at /home/ferris/.julia/v0.7/SAC/src/only.jl:10
splitdims(array, [dims])
Split a multidimensional array into nested arrays of arrays, splitting the specified
dimensions dims to the "outer" array, leaving the remaining dimension in the "inner"
array. By default, the last dimension is split into the outer array.
Like splitdimsview(array, dims) except creating a lazy view of the nested struture.
combinedims(array)
The inverse operation of splitdims - this will take a nested array of arrays, where
each sub-array has the same dimensions, and combine them into a single, flattened array.
Like combinedims(array) except creating a lazy view of the flattened struture.
invert(a)
Take a nested container a and return a container where the nesting is reversed, such that
invert(a)[i][j] === a[j][i].
Currently implemented for combinations of AbstractArray, Tuple and NamedTuple. It is
planned to add AbstractDict in the future.
Examples:
julia>invert([[1,2,3],[4,5,6]]) # invert the order of nesting3-element Array{Array{Int64,1},1}:
[1, 4]
[2, 5]
[3, 6]
julia>invert((a = [1, 2, 3], b = [2.0, 4.0, 6.0])) # Works between different data types3-element Array{NamedTuple{(:a, :b),Tuple{Int64,Float64}},1}:
(a =1, b =2.0)
(a =2, b =4.0)
(a =3, b =6.0)
invert!(out, a)
A mutating version of invert, which stores the result in out.
mapview(f, iter)
Like map, but presents a view of the data contained in iter. The result may be wrapped in an
lazily-computed output container (generally attempting to preserve arrays as AbstractArray, and
so-on).
For immutable collections (like Tuple and NamedTuple), the operation may be performed eagerly.
Example:
julia> a = [1,2,3];
julia> b =mapview(iseven, a)
3-element MappedArray{Bool,1,typeof(iseven),Array{Int64,1}}:falsetruefalse
julia> a[1] =2;
julia> b
3-element MappedArray{Bool,1,typeof(iseven),Array{Int64,1}}:truetruefalse
filterview(f, arr)
Like filter, but presents a view of the data contained in arr. Data in arr is not copied, and modifications to the resulting view are propagated to the original array.
Example:
julia> a = [1, 2, 3];
julia> b =filterview(isodd, a)
2-element view(::Vector{Int64}, [1, 3]) with eltype Int64:13
julia> b[2] =10;
julia> a
3-element Vector{Int64}:1210
julia> b
2-element view(::Vector{Int64}, [1, 3]) with eltype Int64:110
mapmany(f, iters...)
Like map, but f(x...) for each x ∈ zip(iters...) may return an arbitrary number of
values to insert into the output.
(Note that, semantically, filter could be thought of as a special case of mapmany.)
flatten(a)
Takes a collection of collections a and returns a collection containing all the elements
of the subcollecitons of a. Equivalent to mapmany(identity, a).
These operations help you split the elements of a collection according to an arbitrary
function which maps each element to a group key.
group([by = identity], [f = identity], iter)
Group the elements x of the iterable iter into groups labeled by by(x), transforming
each element . The default implementation creates a Dictionaries.Dictionary of
Vectors, but of course a table/dataframe package might extend this to return a suitable
(nested) structure of tables/dataframes.
Also a group(by, f, iters...) method exists for the case where multiple iterables of the
same length are provided.
Applies a mapreduce-like operation on the groupings labeled by passing the elements of
iter through by. Mostly equivalent to map(g -> reduce(op, g; init=init), group(by, f, iter)),
but designed to be more efficient. If multiple collections (of the same length) are
provided, the transformations by and f occur elementwise.
We also export groupcount, groupsum and groupprod as special cases of the above, to determine
the number of elements per group, their sum, and their product, respectively.
请发表评论