From bfb6622395a525af2463d5cda5db6b2b41749433 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Wed, 5 Sep 2018 13:57:06 +0100 Subject: [PATCH] dont pirate convert: fix https://github.com/mauro3/Parameters.jl/issues/73 (#20) * dont pirate convert * add helper functions * docs * drop shiftedabstractarray and generalize --- NEWS.md | 7 +++++++ README.md | 6 +++--- src/offset.jl | 20 +++++++++++++++++--- src/reduce.jl | 4 ++-- src/shiftedarray.jl | 2 -- test/runtests.jl | 4 ++-- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3c35596..abda459 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +## ShiftedArrays 0.4 release notes + +### Breaking changes + +- Now conversion of `AbstractArray{<:ShiftedArray}` to `Array` or `OffsetArray` is done via `ShiftedArray.to_array` and `ShiftedArray.to_offsetarray` respectively rather than `Base.convert` to avoid type piracy. + + ## ShiftedArrays 0.3.1 release notes ### New features diff --git a/README.md b/README.md index 1120aee..9015dda 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ mapreduce_vec(f, g, s, range, filter = isfinite) To collect a `Vector` of `ShiftedArrays` into a normal `Array`, simply: ```julia -convert(Array, s, inds...) +ShiftedArray.to_array(s, inds...) ``` where you need as many `inds` as the dimensions of your `ShiftedArrays`. The output `Array` first few dimensions will be indexed by `inds` (though starting from `1`) and the last one will correspond to the index of the `ShiftedArray` within the `Array` of `ShiftedArrays`. @@ -254,11 +254,11 @@ where you need as many `inds` as the dimensions of your `ShiftedArrays`. The out Similarly, to collect a `Vector` of `ShiftedArrays` into an `OffseyArray` (if you want to preserve the `inds` as offset indices), simply: ```julia -convert(OffsetArray, s, inds...) +ShiftedArray.to_offsetarray(s, inds...) ``` The output `OffsetArray` first few dimensions will be indexed by `inds` and the last one will correspond to the index of the `ShiftedArray` within the `Array` of `ShiftedArrays`. ## Warning -This package uses `Missings` for missing data. `Missings` are known to be inefficient in Julia 0.6 and still have some problems when used in combination with `map`, `broadcast` or `collect` in the development version of Julia 0.7 (see [#25553](https://github.com/JuliaLang/julia/pull/25553) for a potential fix). +This package uses `Missings` for missing data. `Missings` are known to be inefficient in Julia 0.6, but should work better in Julia 1.0. diff --git a/src/offset.jl b/src/offset.jl index fa539f1..715e334 100644 --- a/src/offset.jl +++ b/src/offset.jl @@ -1,9 +1,23 @@ -function Base.convert(::Type{<:Array}, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N +""" +`to_array(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N` + +Collect a `AbstractArray` of `AbstractArrays` into a normal `Array` selecting indices `I` (can take negative values if inner `AbstractArrays` allow it). +The output `Array` first few dimensions will be indexed by `I` (though starting from `1`) +and the last one will correspond to the index of the inner `AbstractArray` within the `AbstractArray` of `AbstractArrays`. +""" +function to_array(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N v = VectorOfArray([view(s, I...) for s in ss]) Array(v) end -function Base.convert(::Type{<:OffsetArray}, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N - m = convert(Array, ss, I...) +""" +`to_offsetarray(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N` + +Collect a `AbstractArray` of `ShiftedArrays` into an `OffsetArray` selecting indices `I` (can take negative values if inner `AbstractArrays` allow it). +The output `OffsetArray` first few dimensions will be indexed by `I` +and the last one will correspond to the index of the inner `AbstractArray` within the `AbstractArray` of `AbstractArrays`. +""" +function to_offsetarray(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N + m = to_array(ss, I...) OffsetArray(m, I..., last(Compat.axes(m))) end diff --git a/src/reduce.jl b/src/reduce.jl index bcb0054..fac9b59 100644 --- a/src/reduce.jl +++ b/src/reduce.jl @@ -157,13 +157,13 @@ for (_mapreduce, mapreduce, reduce) in [(:_mapreduce, :mapreduce, :reduce), (:_m end # align the shifted arrays and apply _mapreduce - function ($mapreduce)(g, f, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N} + function ($mapreduce)(g, f, ss::AbstractArray{<:ShiftedArray{<:Any, <:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N} inds = Base.product(args...) [($_mapreduce)(g, f, (s[CartesianIndex(i)] for s in ss); kwargs...) for i in inds] end # define corresponding reduce methods - ($reduce)(op, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N} = + ($reduce)(op, ss::AbstractArray{<:ShiftedArray{<:Any, <:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N} = ($mapreduce)(identity, op, ss, args...; kwargs...) end end diff --git a/src/shiftedarray.jl b/src/shiftedarray.jl index 1b4b2b1..eb0b497 100644 --- a/src/shiftedarray.jl +++ b/src/shiftedarray.jl @@ -59,8 +59,6 @@ struct ShiftedArray{T, M, N, S<:AbstractArray} <: AbstractArray{Union{T, M}, N} default::M end -const ShiftedAbstractArray{T, N} = ShiftedArray{T, <:Any, N, <:Any} - ShiftedArray(v::AbstractArray{T, N}, n = Tuple(0 for i in 1:N); default::M = missing) where {T, N, M} = ShiftedArray{T, M, N, typeof(v)}(v, _padded_tuple(v, n), default) diff --git a/test/runtests.jl b/test/runtests.jl index 476889d..6ca57a1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -115,6 +115,6 @@ end @testset "offset" begin v = [1, 3, 5, 6, 7, 8, 9, 11] ss = ShiftedArray.((v,), [-1, -3, -6]) - @test isequal(convert(Array, ss, -1:2), [missing 3 7; 1 5 8; 3 6 9; 5 7 11]) - @test isequal(convert(OffsetArray, ss, -1:2), OffsetArray([missing 3 7; 1 5 8; 3 6 9; 5 7 11], -1:2, 1:3)) + @test isequal(ShiftedArrays.to_array(ss, -1:2), [missing 3 7; 1 5 8; 3 6 9; 5 7 11]) + @test isequal(ShiftedArrays.to_offsetarray(ss, -1:2), OffsetArray([missing 3 7; 1 5 8; 3 6 9; 5 7 11], -1:2, 1:3)) end