-
Notifications
You must be signed in to change notification settings - Fork 95
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
.to().value -> .to_value speedup #428
Conversation
Signed-off-by: Nathaniel Starkman <nstarkman@protonmail.com>
Codecov Report
@@ Coverage Diff @@
## master #428 +/- ##
=======================================
Coverage 99.82% 99.82%
=======================================
Files 168 168
Lines 25332 25332
=======================================
Hits 25287 25287
Misses 45 45
Continue to review full report at Codecov.
|
Thanks! Yeah, I didn't know about There's a test that fails, haven't looked why. I'm currently in the process of merging a rename of some files in |
Ok. The failures are a bit strange. I'll have to look into that. I can just do a merge from master when the other PR is done. |
Okay, I've merged it now. Note that I'm also planning on centralizing how quantities are parsed by just defining parsers for different units in |
Fair enough. I just resolved conflicts. I'll hold off further testing until the unit parsing PR is done. |
I don't know when it was added. I just looked at their docs and it seems to be there from at least 2.0 on, maybe earlier. That could be a backport though. It should be there for python 2.7. |
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.
Okay, thanks, so it seems like to_value
has probably existed for a long time and I just didn't know about it (happens a lot, I didn't know numpy.geomspace
existed until a few months ago...).
I'm not about to do the parse PR, so we could just merge this in the mean time if the tests pass.
Signed-off-by: Nathaniel Starkman <nstarkman@protonmail.com>
Thanks! I didn't know about that function. looks useful! |
The tests aren't passing and the errors are super strange. The one I looked at more deeply seems to have problems with |
…modified in-place
Okay, I think I fixed the issue, which seems to stem from the fact that running
but
This seems like unexpected behavior to me, because once I think I'm dealing with floats, I would think that they would no longer communicate with the original Quantity array, especially because
So now I'm a little reluctant to switch to using |
I see. I’m looking at the documentation for `to_value` and I see that it returns a view object if the unit to which to convert is the same as the current unit. This is definitely a speedup, but does have the drawback you mentioned.
… On Aug 24, 2020, at 11:59, Jo Bovy ***@***.***> wrote:
Okay, I think I fixed the issue, which seems to stem from the fact that running to_value on an array somehow creates a reference to the original array, such that if you later change the unitless array, it also modifies the original array. E.g.
def check(r):
r= r.to_value(u.kpc)
r*= -1.
print(r)
r= 4*u.kpc
check(r)
# -4.0
print(r)
# <Quantity 4. kpc>, as expected
but
r= 4*u.kpc*numpy.ones(3)
check(r)
# [-4. -4. -4.]
print(r)
# <Quantity [-4., -4., -4.] kpc>, changed from original
This seems like unexpected behavior to me, because once I think I'm dealing with floats, I would think that they would no longer communicate with the original Quantity array, especially because
isinstance(r.to_value(u.kpc),u.Quantity)
# False
So now I'm a little reluctant to switch to using to_value, because this may lead to unexpected behavior in the future.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#428 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACEI7EKBZG7EQLDJH2NFHM3SCKE47ANCNFSM4QI36TWA>.
|
I guess one could use
but I don't know whether that defeats the speed-up purpose of using |
That would work, but I do think it is effectively |
Why does the codecov look so terrible? I'm pouring over all the diffs and there doesn't seem to be any coverage change. |
The Codecov stats were off, because a few of the jobs didn't report to Codecov (the reporting is quite flaky, often at least one job will just fail to run the reporting script). I re-ran them and now all looks good. There isn't really any internal use of |
I made the switch to using a centralized set of Quantity parsers as much as possible in #430. With that in place and the danger of using |
I agree. |
Okay, I'll close this one then. But thanks for giving me the push to simplify the Quantity parsing! |
.to().value
is slower thanto_value()
since.to
makes a copy and then returns a view of the object's value, whileto_value
performs the optimized set of operations, which is situation dependent, to accomplish the same thing. At worst they are the same speed.