Skip to content

Commit bfb6622

Browse files
author
Pietro Vertechi
authored
dont pirate convert: fix mauro3/Parameters.jl#73 (#20)
* dont pirate convert * add helper functions * docs * drop shiftedabstractarray and generalize
1 parent eb5b8ad commit bfb6622

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## ShiftedArrays 0.4 release notes
2+
3+
### Breaking changes
4+
5+
- 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.
6+
7+
18
## ShiftedArrays 0.3.1 release notes
29

310
### New features

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,19 @@ mapreduce_vec(f, g, s, range, filter = isfinite)
246246
To collect a `Vector` of `ShiftedArrays` into a normal `Array`, simply:
247247

248248
```julia
249-
convert(Array, s, inds...)
249+
ShiftedArray.to_array(s, inds...)
250250
```
251251

252252
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`.
253253

254254
Similarly, to collect a `Vector` of `ShiftedArrays` into an `OffseyArray` (if you want to preserve the `inds` as offset indices), simply:
255255

256256
```julia
257-
convert(OffsetArray, s, inds...)
257+
ShiftedArray.to_offsetarray(s, inds...)
258258
```
259259

260260
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`.
261261

262262
## Warning
263263

264-
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).
264+
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.

src/offset.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
function Base.convert(::Type{<:Array}, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N
1+
"""
2+
`to_array(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N`
3+
4+
Collect a `AbstractArray` of `AbstractArrays` into a normal `Array` selecting indices `I` (can take negative values if inner `AbstractArrays` allow it).
5+
The output `Array` first few dimensions will be indexed by `I` (though starting from `1`)
6+
and the last one will correspond to the index of the inner `AbstractArray` within the `AbstractArray` of `AbstractArrays`.
7+
"""
8+
function to_array(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N
29
v = VectorOfArray([view(s, I...) for s in ss])
310
Array(v)
411
end
512

6-
function Base.convert(::Type{<:OffsetArray}, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N
7-
m = convert(Array, ss, I...)
13+
"""
14+
`to_offsetarray(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N`
15+
16+
Collect a `AbstractArray` of `ShiftedArrays` into an `OffsetArray` selecting indices `I` (can take negative values if inner `AbstractArrays` allow it).
17+
The output `OffsetArray` first few dimensions will be indexed by `I`
18+
and the last one will correspond to the index of the inner `AbstractArray` within the `AbstractArray` of `AbstractArrays`.
19+
"""
20+
function to_offsetarray(ss::AbstractArray{<:AbstractArray{<:Any, N}}, I::Vararg{<:AbstractArray, N}) where N
21+
m = to_array(ss, I...)
822
OffsetArray(m, I..., last(Compat.axes(m)))
923
end

src/reduce.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ for (_mapreduce, mapreduce, reduce) in [(:_mapreduce, :mapreduce, :reduce), (:_m
157157
end
158158

159159
# align the shifted arrays and apply _mapreduce
160-
function ($mapreduce)(g, f, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N}
160+
function ($mapreduce)(g, f, ss::AbstractArray{<:ShiftedArray{<:Any, <:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N}
161161
inds = Base.product(args...)
162162
[($_mapreduce)(g, f, (s[CartesianIndex(i)] for s in ss); kwargs...) for i in inds]
163163
end
164164

165165
# define corresponding reduce methods
166-
($reduce)(op, ss::AbstractArray{<:ShiftedAbstractArray{<:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N} =
166+
($reduce)(op, ss::AbstractArray{<:ShiftedArray{<:Any, <:Any, N}}, args::Vararg{<:AbstractArray, N}; kwargs...) where{N} =
167167
($mapreduce)(identity, op, ss, args...; kwargs...)
168168
end
169169
end

src/shiftedarray.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ struct ShiftedArray{T, M, N, S<:AbstractArray} <: AbstractArray{Union{T, M}, N}
5959
default::M
6060
end
6161

62-
const ShiftedAbstractArray{T, N} = ShiftedArray{T, <:Any, N, <:Any}
63-
6462
ShiftedArray(v::AbstractArray{T, N}, n = Tuple(0 for i in 1:N); default::M = missing) where {T, N, M} =
6563
ShiftedArray{T, M, N, typeof(v)}(v, _padded_tuple(v, n), default)
6664

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ end
115115
@testset "offset" begin
116116
v = [1, 3, 5, 6, 7, 8, 9, 11]
117117
ss = ShiftedArray.((v,), [-1, -3, -6])
118-
@test isequal(convert(Array, ss, -1:2), [missing 3 7; 1 5 8; 3 6 9; 5 7 11])
119-
@test isequal(convert(OffsetArray, ss, -1:2), OffsetArray([missing 3 7; 1 5 8; 3 6 9; 5 7 11], -1:2, 1:3))
118+
@test isequal(ShiftedArrays.to_array(ss, -1:2), [missing 3 7; 1 5 8; 3 6 9; 5 7 11])
119+
@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))
120120
end

0 commit comments

Comments
 (0)