numpy / numpy/numpy

ENH: Deprecate ``np.squeeze()`` with unspecified ``axis``

Open
#27,968 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Proposed new feature or change:

Deprecate calls unspecified axis in calls to np.squeeze(), and/or issue a single runtime warning.

Rationale

squeeze() without an axis argument is the cause of many subtle bugs. Example:

x = images[:batch_size]

B, C, H, W = x.shape

# Incorrect attempt to remove "grayscale" channel dimension.
gray = x.squeeze()

This causes hard-to-debug issues if:

  • B == 1 (batch size is 1)
  • H == 1 (height is 1)
  • W == 1 (width is 1)

The above should have been written:

gray = x.squeeze(1)
Examples in the wild

Many users of this library are not "experts" at code, and frequently produce and publish programs containing this bug.

Roughly 486000 bugs here:
https://github.com/search?q=language%3APython+squeeze%28%29&type=code
I challenge the reader to find an example which is not a (potential) bug!

YOLOX (10k stars):

        conf_mask = (image_pred[:, 4] * class_conf.squeeze() >= conf_thre).squeeze()

The correct code would have been:

        conf_mask = image_pred[:, 4] * class_conf.squeeze(1) >= conf_thre

The above bug actually inspired the creation of this ENH proposal.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the np.squeeze() entry point and locate its implementation and related tests; no specific files or tests are named in the issue. Decide whether unspecified axis should be deprecated or produce a single runtime warning, then add coverage showing the intended behavior for the documented shape cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.