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

Turn ClassicalTransport methods into attributes #705

Merged

Conversation

namurphy
Copy link
Member

@namurphy namurphy commented Oct 19, 2019

I went through the ClassicalTransport class and made all of the methods into attributes by using the @property decorator. Before, we had to do:

>>> classical_transport_instance.resistivity()

Now, we have to do:

>>> classical_transport_instance.resistivity

This pull request is brought to you by procrastinating working on my poster for the APS DPP meeting.

@namurphy namurphy added plasmapy.formulary Related to the plasmapy.formulary subpackage transport Related to plasma transport phenomena labels Oct 19, 2019
@namurphy namurphy added this to the v0.3.0 milestone Oct 19, 2019
@codecov
Copy link

codecov bot commented Oct 19, 2019

Codecov Report

Merging #705 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #705      +/-   ##
==========================================
+ Coverage   95.04%   95.05%   +<.01%     
==========================================
  Files          57       57              
  Lines        4705     4711       +6     
==========================================
+ Hits         4472     4478       +6     
  Misses        233      233
Impacted Files Coverage Δ
plasmapy/formulary/braginskii.py 99.71% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b1e4ea2...2f173c0. Read the comment docs.

@namurphy namurphy force-pushed the add-property-decorator-to-transport branch from cec22f7 to 2f173c0 Compare October 19, 2019 20:03
@namurphy namurphy marked this pull request as ready for review October 19, 2019 20:31
@namurphy namurphy added the breaking change For breaking changes, excluding deprecations and planned removals label Oct 19, 2019
Copy link
Member

@StanczakDominik StanczakDominik left a 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! 😆

@StanczakDominik StanczakDominik merged commit b10c41d into PlasmaPy:master Oct 20, 2019
@namurphy namurphy deleted the add-property-decorator-to-transport branch August 14, 2021 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change For breaking changes, excluding deprecations and planned removals plasmapy.formulary Related to the plasmapy.formulary subpackage transport Related to plasma transport phenomena
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants