Skip to content

A helper function that removes singleton dimensions #974

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

Closed
alex-s-gardner opened this issue Apr 17, 2025 · 11 comments
Closed

A helper function that removes singleton dimensions #974

alex-s-gardner opened this issue Apr 17, 2025 · 11 comments

Comments

@alex-s-gardner
Copy link
Contributor

I could be useful to have a helper function that strips singleton dimensions from a DimArray... something akin to Matlab's squeeze:
https://www.mathworks.com/help/matlab/ref/squeeze.html

@felixcremer
Copy link
Collaborator

You mean like dropdims?

@alex-s-gardner
Copy link
Contributor Author

alex-s-gardner commented Apr 17, 2025

A wrapper around dropdims that would drop all singleton dimensions without needing to specify kwarg dims.

@alex-s-gardner
Copy link
Contributor Author

alex-s-gardner commented Apr 17, 2025

maybe dropsingletondims or dropsingleton

@asinghvi17
Copy link
Collaborator

asinghvi17 commented Apr 17, 2025

Would that just be:

dropsingletondims(x) = dropdims(x; dims = filter(d -> length(d) == 1, dims(x)))

?

It seems to work on simple cases

@alex-s-gardner
Copy link
Contributor Author

alex-s-gardner commented Apr 17, 2025

Yup... that's seems to do the job... the question is would others find it helpful... dropsingletondims(x) / dropsingleton(x) is much easier to remember than dropdims(x; dims = filter(d -> length(d) == 1, dims(x)))

@rafaqz
Copy link
Owner

rafaqz commented Apr 17, 2025

Seems like something generally missing in Julia more than just missing in DD?

Which makes me cautious to add it

@alex-s-gardner
Copy link
Contributor Author

Fair enough. There is a good discussion here.

I am wondering if DD is a bit unique in the need to remove singleton dims... I find myself often needing to remove singleton dims after using reducer function as the singleton dim often has no valuable meaning ... like when taking the mean of data grouped by month (I understand that the dim is technically correct). If this is something others do often then a dropsingleton(x) could be valuable.

@rafaqz
Copy link
Owner

rafaqz commented Apr 17, 2025

I think I agree with Tim Holys take in the thread. It encourages something slow and potentially buggy to be frequently used.

I know the alternative is annoying boilerplate, but our quest for perfect syntax will inevitably end somewhere short of perfect ;)

@asinghvi17
Copy link
Collaborator

Yeah, the way this basically nukes array dimension inference means you really don't want this in package code (but user level might be fine if you aren't precompiling)

@felixcremer
Copy link
Collaborator

There is also this julia issue where it came up JuliaLang/julia#28906 and where Tim Holy also advised against it.

@alex-s-gardner
Copy link
Contributor Author

alex-s-gardner commented Apr 17, 2025

Good discussion ... closing issue as this functionality is not best practice

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

No branches or pull requests

4 participants