Skip to content

Commit

Permalink
dont pirate convert: fix mauro3/Parameters.jl#73 (#20)
Browse files Browse the repository at this point in the history
* dont pirate convert

* add helper functions

* docs

* drop shiftedabstractarray and generalize
  • Loading branch information
Pietro Vertechi committed Sep 5, 2018
1 parent eb5b8ad commit bfb6622
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,19 @@ 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`.

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.
20 changes: 17 additions & 3 deletions src/offset.jl
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/shiftedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bfb6622

Please sign in to comment.