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

Fix DataView reading and writing #414

Merged
merged 14 commits into from
Oct 14, 2019
Merged

Conversation

achilleas-k
Copy link
Member

This PR fixes the read and write methods for DataView and adds tests for all sorts of weird cases and fancy slicing.

The rules for slicing and slice combinations were made to match Python slicing behaviour, adapted for the case of the DataView. Most of the work is done in the _transform_coordinates() method. An important change from the previous way the indices were calculated is that now we rely on slice objects and the slice.indices() method to calculate the start and stop values for fancy slicing cases, like negative indexing and slicing beyond the end of an array, both of which are now closer to the way indexing works in Python and NumPy.

New behaviour:

  • Better handling of negative slices: dataview[-N] is legal only for N < len(dataview). The previous conversion simply calculated len(dataview) - N and then passed the result to the underlying h5py function, which raised the error when necessary.
  • Slicing beyond the end of the array is now legal and returns the values up to and including the last one in the array. For DataView objects, this stops at the internal stop value of the DataView.
  • Negative step values are not allowed because HDF5 doesn't allow them for hyperslabs. We catch this early to make stacktraces shorter.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 89.253% when pulling f438d33 on achilleas-k:fix/data-view-write into dbf2a24 on G-Node:master.

@coveralls
Copy link

coveralls commented Sep 25, 2019

Coverage Status

Coverage increased (+1.0%) to 89.906% when pulling 3f3da07 on achilleas-k:fix/data-view-write into dbf2a24 on G-Node:master.

@achilleas-k
Copy link
Member Author

Ok, now I'm done for real.

- Dimension count check (on top of bounds checks) in DataView
constructor: DataView dimensionality must match array dims exactly.
- Simplify incoming slices using the 'slice.indices()' function:
Converts potential negative or None 'start' or 'stop' to positive
equivalents.
- Pluralise '_slices' attribute to represent that it's a collection
(tuple) of slice objects.
- Checks for single integer index and adds it to DataView 'start'
(offset).
- If it's a slice, simplifies using slice.indices() and transforms.
- Performs out of bounds checks for the transformed slice or index.
- Use ._transform_coordinates() for _read_data()
- Convert single slicing or single index to tuple when transforming
When the user supplies slices and indices that are fewer than the
dimensionality of the DataView, a full-slice (slice(None)) is implied
for the remaining dimensions.
When requested slice dimensions don't match DataArray dimensionality,
raise IncompatibleDimensions error instead of IndexError.

Added redundant test to DataView test file as well.
Copy link
Member

@jgrewe jgrewe left a comment

Choose a reason for hiding this comment

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

very nice

@jgrewe jgrewe merged commit d6df55e into G-Node:master Oct 14, 2019
@achilleas-k achilleas-k deleted the fix/data-view-write branch October 15, 2019 14:38
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.

3 participants