Skip to content

Add support for negative axis in specify_broadcastable #710

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

Merged
merged 16 commits into from
Apr 19, 2024

Conversation

Dhruvanshu-Joshi
Copy link
Member

@Dhruvanshu-Joshi Dhruvanshu-Joshi commented Apr 12, 2024

Description

Normalized the axes argument in specify_broadcastable using normalize_axis_tuple from numpy. Also added tensor/shape.py to mypy-failing-list as import statements cause expected failure.

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

@@ -25,6 +25,7 @@ pytensor/tensor/random/basic.py
pytensor/tensor/random/op.py
pytensor/tensor/random/utils.py
pytensor/tensor/rewriting/basic.py
pytensor/tensor/shape.py
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be done. Over time we want less files, not more. What's mypy complaining about?

Copy link
Member Author

Choose a reason for hiding this comment

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

Mypy complains about the line from numpy.core.numeric import normalize_axis_tuple. The message is that numpy.core.numeric does not have normalize_axis_tuple.
This already occurs in other places as well and are ignored in the mypy-failing-list. For eg:

from numpy.core.numeric import normalize_axis_tuple

Copy link
Member

@ricardoV94 ricardoV94 Apr 13, 2024

Choose a reason for hiding this comment

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

You can do the same. Better to ignore a single line than a whole file.

Going forward: In numpy 2.0 at least they moved this to a user facing location so it should be legal to import then. I don't know if it's already there in the current releases.

Copy link
Member

Choose a reason for hiding this comment

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

It already exists. Can you check if it's fine to import from here:
https://numpy.org/devdocs/reference/generated/numpy.lib.array_utils.normalize_axis_tuple.html#:~:text=Normalizes%20an%20axis%20argument%20into,from%20being%20specified%20multiple%20times.

If so we should import like this in the other places as remove the type ignore

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll just try this out 👍

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

So I checked I am currently using numpy version 1.26.4 and I get the error AttributeError: module 'numpy.lib' has no attribute 'array_utils'. I checked the site-packages and it seems like this feature is introduced in numpy 2.0 and greater. Here's the reference numpy.lib _init_.py that matches with what I have locally:

https://github.com/numpy/numpy/blob/ee81e365a87963b695b688d1b38a357599fa61db/numpy/lib/__init__.py#L1-L92

We are yet to switch to numpy2.0 right?

Copy link
Member

Choose a reason for hiding this comment

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

Strange because the docs say it's new from v1.13.

Anyway that answers it for now. No we're not switching to numpy 2.0 for a while, so let's keep importing and telling mypy to ignore it

@Dhruvanshu-Joshi
Copy link
Member Author

Hey is there something left to do here in this PR?

@ricardoV94
Copy link
Member

Hey is there something left to do here in this PR?

Revert the mypy failing the whole file and just add type: ignore on the imports

@Dhruvanshu-Joshi Dhruvanshu-Joshi changed the base branch from main to upstream_script April 19, 2024 02:13
@Dhruvanshu-Joshi Dhruvanshu-Joshi changed the base branch from upstream_script to main April 19, 2024 02:13
Copy link

codecov bot commented Apr 19, 2024

Codecov Report

Attention: Patch coverage is 81.08108% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 80.76%. Comparing base (76a6c2e) to head (f2ec5a4).
Report is 8 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #710   +/-   ##
=======================================
  Coverage   80.76%   80.76%           
=======================================
  Files         162      162           
  Lines       46698    46707    +9     
  Branches    11421    11422    +1     
=======================================
+ Hits        37715    37723    +8     
- Misses       6734     6735    +1     
  Partials     2249     2249           
Files Coverage Δ
pytensor/tensor/basic.py 88.36% <100.00%> (+0.03%) ⬆️
pytensor/tensor/shape.py 92.94% <100.00%> (ø)
pytensor/tensor/variable.py 87.45% <100.00%> (+0.07%) ⬆️
pytensor/tensor/rewriting/linalg.py 86.98% <75.00%> (-0.18%) ⬇️
pytensor/configdefaults.py 65.56% <50.00%> (-0.37%) ⬇️
pytensor/link/c/cmodule.py 56.88% <82.35%> (+0.11%) ⬆️

@ricardoV94 ricardoV94 changed the title Add support for negative axis in specify_broadcastable Add support for negative axis in specify_broadcastable Apr 19, 2024
@ricardoV94 ricardoV94 merged commit 14651fb into pymc-devs:main Apr 19, 2024
54 of 55 checks passed
@ricardoV94
Copy link
Member

@Dhruvanshu-Joshi pro tip: next time you can try and squash all your commits locally and force-push.

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.

pt.specify_broadcastable does not work with negative axis values
2 participants