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

Create custom list for particles #1017

Merged
merged 46 commits into from
Feb 24, 2021
Merged

Conversation

namurphy
Copy link
Member

This pull request creates ParticleList, which inherits from collections.UserList. The purpose of ParticleList is to simplify getting Quantity arrays for things like particles.mass and particles.charge, and lists like particles.symbols. The purpose of this pull request is to establish the essential architecture, rather than make this list fully featured. In particular, I'm adding the features that I expect to need in IonizationState, IonizationStateCollection, and PlasmaPy-NEI more generally. I'll probably use ParticleList a lot in those places, in particular to get rid of some code that I keep having to rewrite.

I'd also like to enable the formulary to be used like:

>>> plasmapy.formulary.gyrofrequency(B=5 * u.T, particle=["p+", "D+", "T+", "alpha"])

and then get an array of gyrofrequencies back.

Later on, I'd like to add subclasses for different categories of particles. For example, an IonicLevels list would contain attributes shared by all ions. It'd be really helpful to have an easy way to create a list of all of the ionic levels of a particular element or isotope...like IonicLevels("He-4"). Or alternatively, all isotopes (common, stable, or unstable) of a particular element.

Closes #948. Related to #687.

@namurphy namurphy added the plasmapy.particles Related to the plasmapy.particles subpackage label Feb 16, 2021
@namurphy
Copy link
Member Author

As an attempt at computational efficiency, I'm trying to store retrieved attributes so that they can be used later, so long as the contents of the list does not change. This could help with computationally intensive things where there are lots of particles, and the arrays of masses or charges get used repeatedly.

@namurphy
Copy link
Member Author

I've been starting to think that caching this could lead to some weird and unexpected behaviors later on, so I'm going to drop the caching part here. The issues are:

  1. ParticleList is a mutable type
  2. ParticleList can contain CustomParticle instances, which are also mutable.

Suppose one element of ParticleList is a CustomParticle. Then suppose someone modifies the mass of that CustomParticle directly, without modifying the identities of the particles within the list. Then the cached values of mass would no longer be valid. This will probably not be a common issue, but it could cause unexpected and difficult to track down problems. It'll be safer and cleaner to avoid caching. We can revisit caching if/when there's a situation where it ends up being needed.

@StanczakDominik
Copy link
Member

True, though people changing particle masses after creation seems like a rare use case. Dusty plasmas, maybe... But for e.g. impurity studies I'd just want a bunch of completely static particle types. Then we can take advantage of caching without issues. But keeping PRs small is also a great idea 👍

Suppose one element of ParticleList is a CustomParticle. Then suppose
someone modifies the mass of that CustomParticle directly, without
modifying the identities of the particles within the list. Then the
cached values of mass would no longer be valid. This will probably not
be a common issue, but it could cause unexpected and difficult to track
down problems. It'll be safer and cleaner to avoid caching. We can
revisit caching if/when there's a situation where it ends up being needed.
The tests still need to be cleaned up and expanded more.
@codecov
Copy link

codecov bot commented Feb 18, 2021

Codecov Report

Attention: Patch coverage is 99.17355% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.86%. Comparing base (1503043) to head (260da34).

Files Patch % Lines
plasmapy/particles/particle_collections.py 99.02% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1017      +/-   ##
==========================================
+ Coverage   96.82%   96.86%   +0.04%     
==========================================
  Files          69       70       +1     
  Lines        6737     6856     +119     
==========================================
+ Hits         6523     6641     +118     
- Misses        214      215       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@namurphy namurphy requested a review from a team February 19, 2021 03:49
@namurphy namurphy marked this pull request as ready for review February 19, 2021 03:49
…ergy

I believe this idea came from Stuart Mumford several years ago.
@namurphy
Copy link
Member Author

namurphy commented Feb 19, 2021

Ahhhhhhh scope creep on this pull request 🙀 but check this out 🐍

>>> α = Particle("alpha")
>>> D = Particle("D+")
>>> T = Particle("T+")
>>> n = Particle("n")
>>> D + T > n + α  # gets nuclear reaction energy
<Quantity 2.81810898e-12 J>

I've been meaning to implement this for bleeping years. I think the idea originally came from @Cadair. I hereby dedicate the use of unicode characters to @rocco8773. 😬

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.

Good stuff! I have mostly minor comments about testing, and one more important note about documentation for the nuclear reaction energy feature.

Oh yeah, and that last one should go into the changelog!

plasmapy/particles/tests/test_nuclear.py Outdated Show resolved Hide resolved
plasmapy/particles/tests/test_nuclear.py Outdated Show resolved Hide resolved
plasmapy/particles/tests/test_particle_collections.py Outdated Show resolved Hide resolved
plasmapy/particles/particle_collections.py Outdated Show resolved Hide resolved
Comment on lines +88 to +89
except (InvalidParticleError, TypeError):
raise InvalidParticleError(f"Cannot cast {other} into a ParticleList")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing tests here for this edge case (and a couple such edge cases below)!

plasmapy/particles/particle_collections.py Show resolved Hide resolved
plasmapy/particles/particle_class.py Outdated Show resolved Hide resolved
Co-authored-by: Dominik Stańczak <[email protected]>
Examples
--------
A `ParticleList` can be created by calling it with a `list`, `tuple`,
or other iterable that provides `~plasmapy.particles.ParticleLike`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm guessing the doc test glitches out on this reference, because it can't find ParticleLike. Do we have it in particles/__init__.py?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out we do. Note, however, that...

/home/runner/work/PlasmaPy/PlasmaPy/docs/particles/index.rst.rst:64:autosummary: stub file not found 'plasmapy.particles.ParticleLike'. Check your autosummary_generate setting.

double .rst extension for some reason?

Copy link
Member

@StanczakDominik StanczakDominik Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something with https://github.com/namurphy/PlasmaPy/tree/particle-collection/docs/api_static (which I still don't quite get?) ? @rocco8773 do you see anything concerning here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the current problem was related to me setting ParticleLike.__name__ for some reason. I just removed that line and I'm getting the tests to pass. I'm going to raise a different issue about the docs for ParticleLike, since I started going beyond the scope of the current PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is pretty wack, indeed. Looks ready to merge, then!

I'm wondering if this might be causing doc build errors related to finding
`ParticleLike`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plasmapy.particles Related to the plasmapy.particles subpackage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a ParticleCollection class
2 participants