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

Updated Windows base Computations to be Safer #1297

Merged
merged 1 commit into from Jun 18, 2023

Conversation

LazaroHurtado
Copy link
Contributor

Please refer to #1249 for context and discussions, specifically this comment.

The assertion verifying a window's size dimension or axis' stride is non-zero has been removed and outsourced to Slice, which will error when:

  1. end > ax_desc.len, which can only happen when wsz == 0
  2. step == 0, which can only happen when an axis' stride is zero

Thus, we maintain the same functionality as before

@nilgoyette
Copy link
Collaborator

Here we'll have panics (debug mode) due to wraparound immediately if the user passes in any array with inverted axis (negative stride)

I don't want to ask too much of a generous contributor, but if you can, could you please add a test that would have failed before the fix in this PR? (a test with negative strides)

@LazaroHurtado
Copy link
Contributor Author

could you please add a test that would have failed before the fix in this PR? (a test with negative strides)

No worries! But I am not totally sure if this is possible, windows_with_stride requires stride to implement IntoDimension<Dim = D> which already restricts each axis to be non-negative (type Ix = usize;). If I'm overlooking something please let me know

@nilgoyette
Copy link
Collaborator

If it's impossible to pass an array with negative strides, then I don't understand the point of this PR.

Can't you call windows on a transposed matrix?

m.t().windows(...)

If I understand bluss comment correctly, it should create negative strides.

@LazaroHurtado
Copy link
Contributor Author

Ah sorry there are two strides here, the parameter and the offset, I was thinking about the parameter. I believe there's already a windows test that creates a matrix with negative strides by inverting it here but I can create a new test for both windows and windows_with_stride that acts on a transposed matrix as you suggested

@bluss
Copy link
Member

bluss commented Jun 18, 2023

If I understand bluss comment correctly, it should create negative strides.

Transpose just changes order of axes, it doesn't modify their lengths or strides. "Axis 0 and 1 become axis 1 and 0".

Use instead ndarray's method .invert_axis(Axis(0)), that will reverse the matrix along that axis. (*)

Example on matrix with dimensions (n1, n2) and strides (s1, s2):

  • (n1, n2) and (s1, s2) -- transpose -> (n2, n1) and (s2, s1)
  • (n1, n2) and (s1, s2) -- invert axis 0 -> (n1, n2) and (-s1, s2)

(*) slicing with negative step is equivalent to using invert axis. As seen in that test code: array.slice(s![.., ..;-1])

@bluss
Copy link
Member

bluss commented Jun 18, 2023

I don't want to ask too much of a generous contributor, but if you can, could you please add a test that would have failed before the fix in this PR? (a test with negative strides)

I agree with this one.

@LazaroHurtado We need a negative axis input array. The window strides (your feature) still just needs to be anything that's not just (1, 1, 1), for example something with a stride of 2 on the same axis that is inverted. I agree no negative window strides should be possible to be used.

@bluss bluss changed the title [FIX] Updated Windows base Computations to be Safer Updated Windows base Computations to be Safer Jun 18, 2023
@LazaroHurtado
Copy link
Contributor Author

Thank you for adding some more context! I included a test that asserts the proper strided (the parameter) windows are returned when a single and all axis are inverted. I also verified that this test failed before the work introduced in this PR.

@nilgoyette
Copy link
Collaborator

Excellent, thank you @LazaroHurtado for the test, and @bluss for the technical details. Now I feel much more confident merging this branch!

I tested with numpy and I get the same results.

@nilgoyette nilgoyette merged commit 9447328 into rust-ndarray:master Jun 18, 2023
7 checks passed
@LazaroHurtado LazaroHurtado deleted the fix/window_stride branch June 18, 2023 18:15
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

3 participants