Skip to content
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

Fixes broadcasting of arrays. #697

Merged
merged 2 commits into from Mar 7, 2023
Merged

Fixes broadcasting of arrays. #697

merged 2 commits into from Mar 7, 2023

Conversation

1uc
Copy link
Collaborator

@1uc 1uc commented Mar 2, 2023

The rules for broadcasting in 2.4.x seem to be that singleton dimension can be stripped from either side. If both is possible it'll strip from the back, i.e. the dataset with shape (1, 3, 1) can be read into an array of shape (1, 3). This was probably motivated by std::vector<std::vector<double>> of shape (1, 3) is easier to deal with than one that's (3, 1).

However, the stripping from both sides only works for one-dimensional arrays. Stripping multi-dimensional arrays from either side is unstable and should be avoided anyway. Hence, we only allow stripping any 1s if the target shape is one-dimensional.

This commit achieves the above by:

  • introducing details::squeezeDimensions which strips the required amount of singleton dimesions.

  • improving details::checkDimensions by removing bugs related to (3, 1, 1) not being a valid 2D array.

  • Adds (back) tests for checking the broadcasting rules. As well as details::checkDimensions and details::squeezeDimensions.

@codecov-commenter
Copy link

codecov-commenter commented Mar 2, 2023

Codecov Report

Merging #697 (046c774) into master (20be06f) will increase coverage by 0.59%.
The diff coverage is 98.05%.

@@            Coverage Diff             @@
##           master     #697      +/-   ##
==========================================
+ Coverage   81.37%   81.97%   +0.59%     
==========================================
  Files          67       67              
  Lines        4248     4383     +135     
==========================================
+ Hits         3457     3593     +136     
+ Misses        791      790       -1     
Impacted Files Coverage Δ
include/highfive/bits/H5Slice_traits_misc.hpp 90.99% <0.00%> (+0.81%) ⬆️
include/highfive/bits/H5Converter_misc.hpp 86.11% <95.65%> (+1.30%) ⬆️
include/highfive/bits/H5Dataspace_misc.hpp 83.92% <100.00%> (+0.59%) ⬆️
include/highfive/bits/H5Utils.hpp 86.36% <100.00%> (+6.36%) ⬆️
tests/unit/tests_high_five_base.cpp 99.63% <100.00%> (+0.02%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@1uc 1uc marked this pull request as ready for review March 2, 2023 15:18
@1uc 1uc requested a review from alkino March 2, 2023 15:18
The rules for broadcasting in `2.4.x` seem to be that singleton
dimension can be stripped from either side. If both is possible
it'll strip from the back, i.e. the dataset with shape `(1, 3, 1)`
can be read into an array of shape `(1, 3)`. This was probably
motivated by `std::vector<std::vector<double>>` of shape `(1, 3)`
is easier to deal with than one that's `(3, 1)`.

However, the stripping from both sides only works for one-dimensional
arrays. Stripping multi-dimensional arrays from either side is unstable
and should be avoided anyway. Hence, we only allow stripping any `1`s
if the target shape is one-dimensional.

This commit achieves the above by:
  * introducing `details::squeezeDimensions` which strips the
    required amount of singleton dimesions.

  * improving `details::checkDimensions` by removing bugs related
    to `(3, 1, 1)` not being a valid 2D array.

  * Adds (back) tests for checking the broadcasting rules. As well as
    `details::checkDimensions` and `details::squeezeDimensions`.
matz-e
matz-e previously approved these changes Mar 3, 2023
@1uc 1uc merged commit 4638084 into master Mar 7, 2023
@1uc 1uc deleted the 1uc/fix-broadcasting branch March 7, 2023 12:26
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.

None yet

4 participants