Skip to content

don't coerce to ndarray when initializing/expanding arrays #13

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 4 commits into from
Apr 4, 2025

Conversation

wpbonelli
Copy link
Member

@wpbonelli wpbonelli commented Mar 26, 2025

might fix #9 ?

@wpbonelli wpbonelli requested a review from deltamarnix March 26, 2025 17:26
Comment on lines +320 to +323
try:
shp = value.shape # type: ignore
except AttributeError:
return np.full(shape, value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

exceptions can be expensive. Perhaps check with hasattr instead?

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 point, I found this

https://stackoverflow.com/a/903238

Sounds like if value has a shape then try/except is faster but if not, it's slower. I'm not sure what proportion of arrays vs scalars we can expect. The difference isn't huge in either case but maybe worth our own benchmark at some point.

return np.full(shape, value.item()) # type: ignore
if shp != shape:
raise ValueError(f"Shape mismatch, got {shp}, expected {shape}")
# any way to avoid the cast?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Casting gets optimized away by python and doesn't do anything at all. It's a pass-through function.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, I just wonder if there's any way to write this so mypy is happy without the cast.

@wpbonelli wpbonelli merged commit 3b6848d into modflowpy:develop Apr 4, 2025
11 checks passed
@wpbonelli wpbonelli deleted the chexpand branch April 4, 2025 10:56
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.

Don't create new numpy arrays when inserting in the DataTree
2 participants