-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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 bugs in unwrap function #16977
fix bugs in unwrap function #16977
Conversation
See also #14877, where some questions were raised about the correctness of this function. |
@eric-wieser, thank you for your prompt reply. The following code draws two normalized phase response, one is calculated in radian and another is in degree. Code (Python ver 3.7.7):
The correct result is obtained in degree by using the pull request code. @scimax also mentioned about the problem in the case discont=180 in #14877. |
I withdraw about L1543. It was wrong. I am sorry to bother you. |
I have to say I took a quick look yesterday and was very confused about the factor 1.5, but as I had no time to think this through I didn't want to ask yet. |
@@ -1537,10 +1537,10 @@ def unwrap(p, discont=pi, axis=-1): | |||
slice1 = [slice(None, None)]*nd # full slices | |||
slice1[axis] = slice(1, None) | |||
slice1 = tuple(slice1) | |||
ddmod = mod(dd + pi, 2*pi) - pi | |||
_nx.copyto(ddmod, pi, where=(ddmod == -pi) & (dd > 0)) | |||
ddmod = mod(dd + discont, 2 * discont) - discont |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This 2*pi
change is definitely incorrect - the docs refer specifically to comparing discont
to pi
and 2*pi
, but after this change the function does not mention pi at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, absolutely true, the meaning of discont
was misused here. But the logic is correct. I will correct this. I would propose a name like ´interval_sizesince
min_valand
max_val` from #14877 don't make sense at all. It only corrects differences, so it will do the same correction for
unwrap([ 1, 2, 3, 4, 5, 6, 1, 2, 3], min_val=1, max_val=7)
as for
unwrap([ 1, 2, 3, 4, 5, 6, 1, 2, 3], min_val=0, max_val=6)
. Only the difference max_val - min_val
matters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since
min_val
andmax_val
from #14877 don't make sense at all.
I don't agree with that - they makes sense to me, and should satisfy unwrap(arr, min=-a, max=a) + b== unwrap(arr + b, min=-a + b, max=a + b)
I compared it to my solution, and, from a semantics point of view I came up with the same solution. But I couldn't solve the issue with integers returning floats, which are mentioned in #14877 . @eric-wieser , the tests you mentioned in #14877 which failed, are working with this. It's just a mistake in L1540. |
I just realized, I can't propose corrections. Neither here nor in #14877 . Shall I open another PR for that? |
I misunderstood that unwrap() could also be used in degree by changing discont to 180. Also, I confirmed #16987. |
Thanks for the thorough summary @tatsuoka999, reports with graphs make a nice change :) |
Fixed the following two bugs.
Bug1: Returned wrong result when the parameter "discont" is not pi.
Bug2: Wrapped when the diff value is a little larger than pi or discont.
Changes:
L1540, L1541 for Bug1.
L1543 for Bug2.
Example for Bug2: