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

Reorganize and refactor atomic subpackage #122

Merged
merged 21 commits into from
Sep 29, 2017

Conversation

namurphy
Copy link
Member

This pull request makes the following changes to the atomic subpackage:

  • Rename element_symbol to atomic_symbol
  • Rename energy_from_nuclear_reaction to nuclear_reaction_energy
  • Move nuclear_reaction_energy and nuclear_binding_energy to new file called nuclear.py
  • Clean up and reorganize docstrings in atomic.py
  • Improve variable name clarity and consistency in atomic.py
  • Change atomic_symbols_list to a dictionary called atomic_symbols in elements.py
  • Use warnings.warn when there is a UserWarning instead of raise
  • Disallow neutrons in atomic_symbol and element_name, but continue to allow them in isotope_symbol, isotope_mass, and mass_number because neutrons do count as nuclides
  • Add helper functions to identify atomic special cases, including:
    • __is_hydrogen
    • __is_positron
    • __is_electron
    • __is_antiproton
    • __is_neutron
    • __is_alpha

This commit enforces the following naming conventions:

    element = element_symbol(argument)
    isotope = isotope_symbol(argument)

Using `symbol` leaves it ambiguous whether it refers to an element,
isotope, or ion.
The new atomic_symbols dictionary in elements.py has atomic numbers as
keys and the atomic symbols as items.  This results in the same
functionality, but will not break if we remove neutrons from index 0.
New functions include: __is_hydrogen, __is_positron, __is_electron,
__is_antiproton, __is_neutron, and __is_alpha.  These functions check
whether or not a particle string corresponds to any of the cases with
special names.  These functions are designed to make functions like
atomic_symbol more readable.  This is still a work in progress.
@namurphy namurphy added the plasmapy.particles Related to the plasmapy.particles subpackage label Sep 26, 2017
@namurphy namurphy added this to the v0.1 milestone Sep 26, 2017
@pep8speaks
Copy link

pep8speaks commented Sep 26, 2017

Hello @namurphy! Thanks for updating the PR.

Line 844:26: E127 continuation line over-indented for visual indent

Comment last updated on September 29, 2017 at 21:52 Hours UTC

This fixes a bug where __extract_charge_state('H-1-') did not return
('H-1', -1).
Up until now, atomic symbols were case-insensitive (e.g., 'd' and 'D'
both stand for deuterium; but 'p' means proton and 'P' means
phosphorus).  The problem with case-insensitivity is that it allows
for sloppier and less consistent code.
Because: "coverage/coveralls — Coverage decreased (-0.01%) to 98.955%"
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.

Found some mostly minor text fixes to be done. :)

.gitignore Outdated
Untitled*
untitled*
C\:*
Copy link
Member

Choose a reason for hiding this comment

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

... Wait, what? C:?

Copy link
Member Author

@namurphy namurphy Sep 27, 2017

Choose a reason for hiding this comment

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

For some reason on my workstation running CentOS 6.8 I occasionally get files named C:\\nppdf32Log\\debuglog.txt for some unbeknownst reason. Hm...maybe *debuglog* would be a better choice, in case this has any adverse impact on Windows.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, that's weird.

Here's something I found out about recently that might be helpful, though: https://help.github.com/articles/ignoring-files/#create-a-global-gitignore

-------
energy: Quantity
The change in nuclear binding energy, which will be positive
if the reaction releases and negative if the reaction is
Copy link
Member

Choose a reason for hiding this comment

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

...releases energy and...

if item == 'n':
symbol = 'n'
else:
symbol = isotope_symbol(item)
Copy link
Member

Choose a reason for hiding this comment

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

I'm still not sure what's worse, the weird feeling I get seeing how this has to be handled separately, or calling a neutron an isotope.

Copy link
Member Author

@namurphy namurphy Sep 27, 2017

Choose a reason for hiding this comment

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

Good catch. Since isotope_symbol works with neutrons, we can shorten these four lines down to just symbol = isotope_symbol(item). I may change symbol to isotope for clarity and consistency as well.

if __is_neutron(argument) and (mass_numb is None or mass_numb == 1):
return 'n'
elif argument == 0 and mass_numb == 1:
return 'n'
Copy link
Member

Choose a reason for hiding this comment

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

Oh, so you did add it! One of my previous comments may have just been invalidated, then!

else:
warn("Redundant mass number information in isotope_symbol from"
" inputs: (" + str(argument)+", " + str(mass_numb) + ")",
UserWarning)
Copy link
Member

Choose a reason for hiding this comment

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

Excuse me, do you have a moment to talk about our typing savior the f-string?

f"Redundant mass number information in isotope_symbol from inputs: ({argument}, {mass_numb})",

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, great idea! I think I had some bad experiences using Fortran formatting (i.e., I used Fortran formatting) and so have been reluctant to learn how to do it in Python.

@@ -434,24 +444,25 @@ def half_life(argument, mass_numb=None):
half_life_sec = Isotopes[isotope]['half_life']
except Exception:
half_life_sec = None
raise UserWarning("The half-life for isotope " + isotope +
" is not available; returning None.")
warn("The half-life for isotope " + isotope + " is not available; "
Copy link
Member

Choose a reason for hiding this comment

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

Once more, a proper place for the glory of f-strings!


Returns
-------
mass_number : integer
An integer representing the mass number of the isotope.
The total number of protons plus neutrons in a nuclide.
Copy link
Member

Choose a reason for hiding this comment

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

👌

uses hydrogen's standard atomic weight based on terrestrial values. To
get the mass of a proton, use ion_mass('p').
Calling ion_mass('H') does not return the mass of a proton but
instea uses hydrogen's standard atomic weight based on terrestrial
Copy link
Member

Choose a reason for hiding this comment

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

typo, instead

A string or integer representing an atomic number or element, or a
string represnting an isotope.
A string or integer representing an atomic number or element,
or a string represnting an isotope.

unstable_instead: boolean
Copy link
Member

Choose a reason for hiding this comment

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

Actually, how about just unstable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've been going back and forth about how best to name this. The possibilities I've been wondering about are:

  1. stable_isotopes('Fe', unstable_instead=True) (status quo)
  2. stable_isotopes('Fe', unstable=True) (your suggestion)
  3. stable_isotopes('Fe', invert=True)
  4. unstable_isotopes('Fe') (create a new function)
  5. Not provide this functionality and let people do a list comprehension like:
[isotope for isotope in known_isotopes('H') if isotope not in stable_isotopes('H')]

Are any of these better than the others? The most readable is unstable_isotopes('Fe'), though I'm wondering if we need to create a function for it.


return Z


def electric_charge(argument):
Copy link
Member

Choose a reason for hiding this comment

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

heck yeah!

@StanczakDominik
Copy link
Member

StanczakDominik commented Sep 27, 2017 via email

@namurphy
Copy link
Member Author

I'm thinking about eventually creating a decorator that will take the arguments for these various functions and parse them into the standardized form expected later on. That will probably simplify a lot of this code. I think I may save that for a future pull request though.

@namurphy namurphy merged commit 7f06dd7 into PlasmaPy:master Sep 29, 2017
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
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants