Skip to content

BUG: avoid mutation of shape attibutes (modeling) - #19129

Merged
pllim merged 2 commits into
astropy:mainfrom
neutrinoceros:modelling/19126
Jan 13, 2026
Merged

pllim merged 2 commits into
astropy:mainfrom
neutrinoceros:modelling/19126

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

ref #19126

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

github-actions Bot commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@neutrinoceros neutrinoceros changed the title BUG: avoid mutation of shape attibutes (modelling) BUG: avoid mutation of shape attibutes (modeling) Jan 8, 2026
@neutrinoceros neutrinoceros added this to the v7.2.1 milestone Jan 8, 2026
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

docs failures are unrelated #19134

@neutrinoceros
neutrinoceros marked this pull request as ready for review January 8, 2026 13:57
@neutrinoceros
neutrinoceros requested a review from a team as a code owner January 8, 2026 13:57
@pllim

pllim commented Jan 8, 2026

Copy link
Copy Markdown
Member

Any subtlety wrt copy vs no copy?

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Well, the copy argument was only introduced in numpy 2.1 so the only consistent behavior we can get across all supported versions is the default copy=None

@pllim
pllim requested a review from mhvk January 8, 2026 15:46
@pllim

pllim commented Jan 8, 2026

Copy link
Copy Markdown
Member

I think if we can get approval on any of the set of 3, we can merge them all as all the diff are similar. Perhaps they could even have been one PR.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is fine, but I would suggest to do just a single reshape and then assign to x, y[, z] - see in-line.

Comment thread astropy/modeling/rotations.py Outdated
result = np.dot(_create_matrix(angles[0], self.axes_order), inarr)
x, y, z = result[0], result[1], result[2]
x.shape = y.shape = z.shape = orig_shape
x, y, z = [np.reshape(a, orig_shape) for a in result]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is better as x, y, z = np.reshape(result, (3,) + orig_shape)

Comment thread astropy/modeling/rotations.py Outdated
result = np.dot(cls._compute_matrix(angle), inarr)
x, y = result[0], result[1]
x.shape = y.shape = orig_shape
x = np.reshape(result[0], orig_shape)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similarly, here I'd do x, y = np.reshape(result, (2,) + orig_shape)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried a slight variant of this (np.reshape(result, (2, *shape))) which I think is exactly equivalent (?) to your suggestion, but it also breaks tests. Maybe I'm missing something ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Huh?? I'll need to check, that doesn't seem to make sense! (I assume you did do (2, *orig_shape)...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I assume you did do (2, *orig_shape)

yes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

for the record I can confirm that using your exact suggestion leads to the exact same problem (confirming that my variant is semantically identical).

Comment thread astropy/modeling/projections.py Outdated
result = np.dot(augmented_matrix, inarr)
x, y = result[0], result[1]
x.shape = y.shape = shape
x = np.reshape(result[0], shape)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See other comments.

@pllim

pllim commented Jan 13, 2026

Copy link
Copy Markdown
Member

Can we please prioritize this one? I see modeling code triggers the most downstream failures on my end. Thanks!

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

I don't think there's anything left to do. As I said, Marten's suggestion isn't working (though I admit I don't know why). I rebased the branch so docs could rebuild cleanly, and anyone should be able to merge this without @pllim's super powers.

@mhvk

mhvk commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

I'd like to have a look myself, but also don't want to hold this up. How about if I don't say otherwise by end of today (EST), then just go ahead and merge?

@mhvk

mhvk commented Jan 13, 2026

Copy link
Copy Markdown
Contributor

OK, I found it. In the projections, one is actually projecting, i.e., ignoring a third coordinate. Will push fix.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like all tests passed with my change, so let me approve.

One final thing: I went with your suggestion of np.reshape(result, (3, *shape)) instead of my original np.reshape(result, (3,)+shape), but it turns out this is the slower option:

In [6]: %timeit tuple((3,)+shape)
50.3 ns ± 0.925 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In [7]: %timeit tuple((3, *shape))
105 ns ± 1.13 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

In the end I don't think it is worth changing, though, since the reshape is 1.5 us.

(Conversely, doing the reshape separately for each coordinate is quite a bit slower, so we are close to optimal now.)

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, all! I don't see modeling in the devdeps failures anymore, so let's get this in.

@pllim
pllim merged commit dc7afdd into astropy:main Jan 13, 2026
32 of 33 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Jan 13, 2026
@neutrinoceros
neutrinoceros deleted the modelling/19126 branch January 14, 2026 01:00
pllim added a commit that referenced this pull request Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants