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

Modernize unwrap API and prepare for #198 #201

Merged
merged 1 commit into from
Jun 1, 2018
Merged

Conversation

martinholters
Copy link
Member

Make the API of unwrap/unwrap! similar to the one of accumulate/accumulate! and friends and deprecate the case of unspecified dims to make room for proper multi-dimensional unwrapping
(#198), CC @platawiec.

Also switch the implementation to use accumulate! for simpler and more efficient code.

I've realized that unwrap!(y, x) could actually use default dims=1:ndims(x) right now (well, once the functionality of #198 is in), but that would be very inconsistent with unwrap(x) defaulting to dims=ndims(x) (with a deprecation warning). So I suggest we provide no default dims for unwrap! until we remove that deprecation and then use the same default in both.

@martinholters
Copy link
Member Author

Can you give this a look, @platawiec and @ssfrr?

src/util.jl Outdated
@@ -29,46 +30,58 @@ export unwrap!,
shiftin!

"""
unwrap!(m, dim=ndims(m); range=2pi)
unwrap!(y, m; dims=nothing, range=2pi)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the value of separating the src and dest arrays, but is there a reason we can't also keep the 1-arg in-place version? We could define a fallback like:

unwrap!(m::AbstractArray; kwargs...) = unwrap!(m, m; kwargs...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can easily keep them.

src/util.jl Outdated
@deprecate(unwrap!(m::Array{T}; range::Number=2pi) where T<:AbstractFloat,
unwrap!(m, m, dims=ndims(m), range=range))
@deprecate(unwrap!(m::Array{T}, dim::Integer; range::Number=2pi) where T<:AbstractFloat,
unwrap!(m, m, dims=dim, range=range))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be AbstractVector / AbstractArray, I think. Technically this is more general than the original definition that's being deprecated, but if someone calls this with some AbstractArray subtype I think the deprecation warning will be more useful than a MethodError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably won't make much of a difference in practice, but yeah, I'll widen the signatures.

src/util.jl Outdated
# println("slice_tuple: ", slice_tuple)
# println("m[slice_tuple...]: ", m[slice_tuple...])
m[slice_tuple...] = m[slice_tuple...] - offset
if dims isa Integer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's cleaner to use dispatch to handle the Vector / dims logic, though I guess to do that would require an internal _unwrap! function that passed dims as a positional arg rather than a keyword arg. So maybe not worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my thinking. Might be reconsidered as part of #198.

src/util.jl Outdated
unwrap_kernel(range=2π) = (x, y) -> y - round((y - x) / range) * range

@deprecate(unwrap!(m::Vector{T}; range::Number=2pi) where T<:AbstractFloat,
unwrap!(m, m, range=range))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this deprecation if you end up going with the in-place fallback suggestion above.

@ssfrr
Copy link
Contributor

ssfrr commented May 30, 2018

Sounds good. I'm 👍 to merge with the tweaks above

@martinholters
Copy link
Member Author

Ok, I think I have addressed all your comments. I've widened the type signatures to just AbstractArray everywhere. Leaving out the T <: AbstractFloat allows e.g.:

julia> unwrap([1,2,3,14,15,6], range=10)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

Not that this seems like a particularly useful feature, but why disallow it?
On the down side, the error here is maybe a little unintuitive now:

julia> unwrap([1,2,3,14,15,6])
ERROR: InexactError: Int64(Int64, 1.4336293856408275)

We could make unwrap call accumulate directly, as this works:

accumulate(DSP.Util.unwrap_kernel(), [1,2,3,14,15,6], dims=1)

It returns an Array{Float64}. However, just like that, it would also return an Array{Float64} even if range is an Int. But we don't have to sort that out in this PR, I'd say.

src/util.jl Outdated
unwrap!(copy(m), args...; kwargs...)
function unwrap(m::AbstractArray{<:Any,N}; dims=nothing, range=2pi) where {N}
if dims === nothing && N != 1
Base.depwarn("`unwrap(m):AbstractArray` is deprecated, use `unwrap(m, dims=ndims(m))` instead", :unwrap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, I missed this last time - should this be unwrap(m::AbstractArray) rather than unwrap(m):AbstractArray?

Other than that I think this is good to merge.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I probably had too little coffee before reworking the deprecation messages for the widened type signatures. Update incoming...

Make the API of `unwrap`/`unwrap!` similar to the one of
`accumulate`/`accumulate!` and friends and deprecate the case of
unspecified `dims` to make room for proper multi-dimensional unwrapping
(#198).

Also switch the implementation to use `accumulate!` for simpler and more
efficient code.
@martinholters
Copy link
Member Author

Ok, one (hopefully) last look?

@ssfrr ssfrr merged commit f92dcd7 into master Jun 1, 2018
@ssfrr ssfrr deleted the mh/modern_unwrap branch June 1, 2018 14:19
@ssfrr
Copy link
Contributor

ssfrr commented Jun 1, 2018

Something weird is going on with coverage - most of the lines it says are uncovered seem to be unrelated to this PR. I don't have time to look into it now but hopefully it'll resolve itself.

@martinholters
Copy link
Member Author

Thanks, having a second pair of eyes here definitely was worth it.

I think there is something fishy with to coverage data on 0.7, but I haven't looked at the details, yet.

platawiec pushed a commit to platawiec/DSP.jl that referenced this pull request Jun 8, 2018
Added multi-dimensional unwrap when calling dims=1:N, added relevant
keyword arguments, added and fixed tests, fixed issue where range
keyword was incorrectly typed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants