-
Notifications
You must be signed in to change notification settings - Fork 339
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
Turn ClassicalTransport methods into attributes #705
Turn ClassicalTransport methods into attributes #705
Conversation
Codecov Report
@@ Coverage Diff @@
## master #705 +/- ##
==========================================
+ Coverage 95.04% 95.05% +<.01%
==========================================
Files 57 57
Lines 4705 4711 +6
==========================================
+ Hits 4472 4478 +6
Misses 233 233
Continue to review full report at Codecov.
|
cec22f7
to
2f173c0
Compare
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.
As a fellow chronic procrastinator, I salute thee, @namurphy 😆
I was going to say a few things about how @property
should probably be limited to look ups, not calculations, since calling a method with ()
involves an implicit user-program contract, you're expecting a computation to be performed, while a @property
is something that's either already there or can be constructed very quickly from existing class fields.
But then I checked the timings:
[ins] In [19]: %timeit ct.electron_thermal_conductivity()
12.7 ms ± 272 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
[ins] In [21]: %timeit plasmapy.formulary.ClassicalTransport(5 * u.eV, 1e23/u.m**3, 5*u.eV, 1e23/u.m**3, 'p').electron_thermal_conductivity()
44.9 ms ± 2.45 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
[ins] In [22]: %timeit plasmapy.formulary.electron_thermal_conductivity(5 * u.eV, 1e23/u.m**3, 5*u.eV, 1e23/u.m**3, 'p')
43.6 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
[ins] In [24]: %timeit plasmapy.formulary.ClassicalTransport(5 * u.eV, 1e23/u.m**3, 5*u.eV, 1e23/u.m**3, 'p')
30.7 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# for comparison, I would deem this a good @property in the strict sense
[ins] In [23]: %timeit 2 * 3
7.92 ns ± 0.125 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
But given that the method calls ARE faster than the functions, sure, why not! The syntax looks much cleaner as well.
It may even be possible to, given that a CT object is "static" in a sense (you create it and it's just there, there's no parameters than can be added to the method calls), it might be possible to look into caching the results... though that's probably a bunch of work for little gain.
I did just stumble upon this, though... https://docs.python.org/3/library/functools.html#functools.cached_property
New in version 3.8.
😞
Anyway, merging this for now! 😆
I went through the
ClassicalTransport
class and made all of the methods into attributes by using the@property
decorator. Before, we had to do:Now, we have to do:
This pull request is brought to you by procrastinating working on my poster for the APS DPP meeting.