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

Rename squeeze -> dropdims #28303

Merged
merged 2 commits into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ Deprecated or removed
`dims` keyword argument. This includes the functions `sum`, `prod`, `maximum`,
`minimum`, `all`, `any`, `findmax`, `findmin`, `mean`, `varm`, `std`, `var`, `cov`,
`cor`, `median`, `mapreducedim`, `reducedim`, `sort`, `accumulate`, `accumulate!`,
`cumsum`, `cumsum!`, `cumprod`, `cumprod!`, `flipdim`, `squeeze`, and `cat` ([#25501], [#26660], [#27100]).
`cumsum`, `cumsum!`, `cumprod`, `cumprod!`, `flipdim`, `dropdims`, and `cat` ([#25501], [#26660], [#27100]).

* `indices(a)` and `indices(a,d)` have been deprecated in favor of `axes(a)` and
`axes(a, d)` ([#25057]).
Expand Down Expand Up @@ -1321,6 +1321,8 @@ Deprecated or removed

* `ndigits(n, b, [pad])` is deprecated in favor of `ndigits(n, base=b, pad=pad)` ([#27908]).

* `squeeze` is deprecated in favor of `dropdims`.

Command-line option changes
---------------------------

Expand Down
16 changes: 8 additions & 8 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ _sub(t::Tuple, ::Tuple{}) = t
_sub(t::Tuple, s::Tuple) = _sub(tail(t), tail(s))

"""
squeeze(A; dims)
dropdims(A; dims)

Remove the dimensions specified by `dims` from array `A`.
Elements of `dims` must be unique and within the range `1:ndims(A)`.
Expand All @@ -61,20 +61,20 @@ julia> a = reshape(Vector(1:4),(2,2,1,1))
1 3
2 4

julia> squeeze(a; dims=3)
julia> dropdims(a; dims=3)
2×2×1 Array{Int64,3}:
[:, :, 1] =
1 3
2 4
```
"""
squeeze(A; dims) = _squeeze(A, dims)
function _squeeze(A::AbstractArray, dims::Dims)
dropdims(A; dims) = _dropdims(A, dims)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not strictly related to this PR, but what's the value of having dims be a keyword argument for a simple two arg function? I know they are fast now, but here it doesn't seem needed. The rename makes this a bit more jarring, since we now have dropdims(A,dims=1) which is redundant.

I believe there are other functions where I've noticed this, though can't remember which exactly. I can open an issue if we want.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

It's just for consistency --- all functions that accept a list of dimensions accept them with a dims keyword argument, so it's easier to guess or remember how to call 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.

This was done for consistency. All functions that operate on dimensions now take those as a keyword argument.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

jinx!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I see now they were all changed. I'm not a huge fan, but I suppose that ship has sailed.

function _dropdims(A::AbstractArray, dims::Dims)
for i in 1:length(dims)
1 <= dims[i] <= ndims(A) || throw(ArgumentError("squeezed dims must be in range 1:ndims(A)"))
length(axes(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
1 <= dims[i] <= ndims(A) || throw(ArgumentError("dropped dims must be in range 1:ndims(A)"))
length(axes(A, dims[i])) == 1 || throw(ArgumentError("dropped dims must all be size 1"))
for j = 1:i-1
dims[j] == dims[i] && throw(ArgumentError("squeezed dims must be unique"))
dims[j] == dims[i] && throw(ArgumentError("dropped dims must be unique"))
end
end
d = ()
Expand All @@ -85,7 +85,7 @@ function _squeeze(A::AbstractArray, dims::Dims)
end
reshape(A, d::typeof(_sub(axes(A), dims)))
end
_squeeze(A::AbstractArray, dim::Integer) = _squeeze(A, (Int(dim),))
_dropdims(A::AbstractArray, dim::Integer) = _dropdims(A, (Int(dim),))

## Unary operators ##

Expand Down
3 changes: 2 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,8 @@ export readandwrite

@deprecate flipdim(A, d) reverse(A, dims=d)

@deprecate squeeze(A, dims) squeeze(A, dims=dims)
@deprecate squeeze dropdims
@deprecate dropdims(A, dims) dropdims(A, dims=dims)

@deprecate diff(A::AbstractMatrix, dim::Integer) diff(A, dims=dim)
@deprecate unique(A::AbstractArray, dim::Int) unique(A, dims=dim)
Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export
sortperm,
sortperm!,
sortrows,
squeeze,
dropdims,
step,
stride,
strides,
Expand Down
2 changes: 1 addition & 1 deletion doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Base.parentindices
Base.selectdim
Base.reinterpret
Base.reshape
Base.squeeze
Base.dropdims
Base.vec
```

Expand Down
2 changes: 1 addition & 1 deletion stdlib/SparseArrays/src/SparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
cotd, coth, count, csc, cscd, csch, done,
exp10, exp2, findprev, findnext, floor, hash, argmin, inv,
log10, log2, next, sec, secd, sech, show,
sin, sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,
sin, sinc, sind, sinh, sinpi, dropdims, start, sum, summary, tan,
tand, tanh, trunc, abs, abs2,
broadcast, ceil, complex, conj, convert, copy, copyto!, adjoint,
exp, expm1, findall, findmax, findmin, float, getindex,
Expand Down
10 changes: 5 additions & 5 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ let
end
end

@testset "squeeze" begin
@testset "dropdims" begin
for i = 1:5
am = sprand(20, 1, 0.2)
av = squeeze(am, dims=2)
av = dropdims(am, dims=2)
@test ndims(av) == 1
@test all(av.==am)
am = sprand(1, 20, 0.2)
av = squeeze(am, dims=1)
av = dropdims(am, dims=1)
@test ndims(av) == 1
@test all(av' .== am)
end
Expand Down Expand Up @@ -1401,10 +1401,10 @@ end
@test norm(Array(D) - Array(S)) == 0.0
end

@testset "error conditions for reshape, and squeeze" begin
@testset "error conditions for reshape, and dropdims" begin
local A = sprand(Bool, 5, 5, 0.2)
@test_throws DimensionMismatch reshape(A,(20, 2))
@test_throws ArgumentError squeeze(A,dims=(1, 1))
@test_throws ArgumentError dropdims(A,dims=(1, 1))
end

@testset "float" begin
Expand Down
22 changes: 11 additions & 11 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,17 @@ end
@test vec(b) == vec(a)

a = rand(1, 1, 8, 8, 1)
@test @inferred(squeeze(a, dims=1)) == @inferred(squeeze(a, dims=(1,))) == reshape(a, (1, 8, 8, 1))
@test @inferred(squeeze(a, dims=(1, 5))) == squeeze(a, dims=(5, 1)) == reshape(a, (1, 8, 8))
@test @inferred(squeeze(a, dims=(1, 2, 5))) == squeeze(a, dims=(5, 2, 1)) == reshape(a, (8, 8))
@test_throws UndefKeywordError squeeze(a)
@test_throws ArgumentError squeeze(a, dims=0)
@test_throws ArgumentError squeeze(a, dims=(1, 1))
@test_throws ArgumentError squeeze(a, dims=(1, 2, 1))
@test_throws ArgumentError squeeze(a, dims=(1, 1, 2))
@test_throws ArgumentError squeeze(a, dims=3)
@test_throws ArgumentError squeeze(a, dims=4)
@test_throws ArgumentError squeeze(a, dims=6)
@test @inferred(dropdims(a, dims=1)) == @inferred(dropdims(a, dims=(1,))) == reshape(a, (1, 8, 8, 1))
@test @inferred(dropdims(a, dims=(1, 5))) == dropdims(a, dims=(5, 1)) == reshape(a, (1, 8, 8))
@test @inferred(dropdims(a, dims=(1, 2, 5))) == dropdims(a, dims=(5, 2, 1)) == reshape(a, (8, 8))
@test_throws UndefKeywordError dropdims(a)
@test_throws ArgumentError dropdims(a, dims=0)
@test_throws ArgumentError dropdims(a, dims=(1, 1))
@test_throws ArgumentError dropdims(a, dims=(1, 2, 1))
@test_throws ArgumentError dropdims(a, dims=(1, 1, 2))
@test_throws ArgumentError dropdims(a, dims=3)
@test_throws ArgumentError dropdims(a, dims=4)
@test_throws ArgumentError dropdims(a, dims=6)

sz = (5,8,7)
A = reshape(1:prod(sz),sz...)
Expand Down
24 changes: 12 additions & 12 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,20 @@ am = map(identity, a)
@test isa(am, OffsetArray)
@test am == a

# squeeze
# dropdims
a0 = rand(1,1,8,8,1)
a = OffsetArray(a0, (-1,2,3,4,5))
@test @inferred(squeeze(a, dims=1)) == @inferred(squeeze(a, dims=(1,))) == OffsetArray(reshape(a, (1,8,8,1)), (2,3,4,5))
@test @inferred(squeeze(a, dims=5)) == @inferred(squeeze(a, dims=(5,))) == OffsetArray(reshape(a, (1,1,8,8)), (-1,2,3,4))
@test @inferred(squeeze(a, dims=(1,5))) == squeeze(a, dims=(5,1)) == OffsetArray(reshape(a, (1,8,8)), (2,3,4))
@test @inferred(squeeze(a, dims=(1,2,5))) == squeeze(a, dims=(5,2,1)) == OffsetArray(reshape(a, (8,8)), (3,4))
@test_throws ArgumentError squeeze(a, dims=0)
@test_throws ArgumentError squeeze(a, dims=(1,1))
@test_throws ArgumentError squeeze(a, dims=(1,2,1))
@test_throws ArgumentError squeeze(a, dims=(1,1,2))
@test_throws ArgumentError squeeze(a, dims=3)
@test_throws ArgumentError squeeze(a, dims=4)
@test_throws ArgumentError squeeze(a, dims=6)
@test @inferred(dropdims(a, dims=1)) == @inferred(dropdims(a, dims=(1,))) == OffsetArray(reshape(a, (1,8,8,1)), (2,3,4,5))
@test @inferred(dropdims(a, dims=5)) == @inferred(dropdims(a, dims=(5,))) == OffsetArray(reshape(a, (1,1,8,8)), (-1,2,3,4))
@test @inferred(dropdims(a, dims=(1,5))) == dropdims(a, dims=(5,1)) == OffsetArray(reshape(a, (1,8,8)), (2,3,4))
@test @inferred(dropdims(a, dims=(1,2,5))) == dropdims(a, dims=(5,2,1)) == OffsetArray(reshape(a, (8,8)), (3,4))
@test_throws ArgumentError dropdims(a, dims=0)
@test_throws ArgumentError dropdims(a, dims=(1,1))
@test_throws ArgumentError dropdims(a, dims=(1,2,1))
@test_throws ArgumentError dropdims(a, dims=(1,1,2))
@test_throws ArgumentError dropdims(a, dims=3)
@test_throws ArgumentError dropdims(a, dims=4)
@test_throws ArgumentError dropdims(a, dims=6)

# other functions
v = OffsetArray(v0, (-3,))
Expand Down
2 changes: 1 addition & 1 deletion test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Agen_slice(A::AbstractArray, I...)
push!(sd, i)
end
end
squeeze(B, dims=sd)
dropdims(B, dims=sd)
end

_Agen(A, i1) = [A[j1] for j1 in i1]
Expand Down