forked from jautschbach/dynpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisotopes.py
189 lines (158 loc) · 6.13 KB
/
isotopes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
# Copyright (c) 2015-2022, Exa Analytics Development Team
# Distributed under the terms of the Apache License 2.0
"""
Periodic Table of Elements and Isotopes
########################################
This module provides a database of the atomic elements and their isotopes.
Visualization parameters are also provided. Data is provided and maintained
by `NIST`_. The full api is given in the code example below. Note that not
all attributes that are present on isotopes are present on elements (and vice
versa).
.. code-block:: python
from exatomic.exa.util import isotopes
isotopes.H # Hydrogen element
isotopes.H[2] # Hydrogen isotopes 2 (deuterium)
isotopes.H.cov_radius # Empirical covalent radius (a.u. - Bohr)
isotopes.H.van_radius # Empirical Van der Waals radius (a.u. - Bohr)
isotopes.H.af # Abundance fraction
isotopes.H.afu # Abundance fraction uncertainty
isotopes.H.mass # Atomic mass (g/mol)
isotopes.H.massu # Atomic mass uncertainty (g/mol)
isotopes.H.eneg # Electronegativity (Pauling scale)
isotopes.H[2].quad # Nuclear quadrupole moment (eb - electron-barn - e10^(-28)m^2)
isotopes.H[2].A # Atomic mass number (g/mol)
isotopes.H.Z # Proton number
isotopes.H[2].g # Nuclear g-factor (dimensionless magnetic moment)
isotopes.H[2].spin # Nuclear spin
isotopes.H.color # Traditional atomic color (HTML)
isotopes.H.name # Full element name
Warning:
Isotopes are provided as part of the static data directory.
.. _NIST: https://www.nist.gov/
"""
import os as _os
import sys as _sys
import bz2 as _bz2
from pandas import read_json as _rj
#from exatomic.exa.static import resource as _resource
if not hasattr(_bz2, "open"):
_bz2.open = _bz2.BZ2File
class Element(object):
"""
An element from Mendeleev's periodic table.
.. code-block:: python
from exatomic.exa.util import isotopes
H = isotopes.H # Hydrogen element (isotope averaged)
D = isotopes.H['2'] # Deuterium (2H, a specific isotope)
isotopes.H.isotopes # List of available isotopes
"""
@property
def isotopes(self):
return [v for k, v in vars(self).items() if k.startswith("_")]
@property
def radius(self):
return self.cov_radius
def __init__(self, symbol, name, mass, znum, cov_radius, van_radius, color):
self.symbol = symbol
self.name = name
self.mass = mass
self.Z = znum
self.cov_radius = cov_radius
self.van_radius = van_radius
self.color = color
def __getitem__(self, key):
if isinstance(key, str):
return getattr(self, "_"+key)
return getattr(self, key)
def __repr__(self):
return self.symbol
class Isotope(object):
"""
A specific atomic isotope (the physical manifestation of an element).
.. code-block:: python
from exatomic.exa.util import isotopes
isotopes.U['235'].mass # Mass of 235-U
"""
@property
def radius(self):
return self.cov_radius
def __init__(self, anum, znum, af, afu, cov_radius, van_radius, g, mass, massu, name, eneg, quad, spin, symbol, color):
self.A = anum
self.Z = znum
self.af = af
self.afu = afu
self.cov_radius = cov_radius
self.van_radius = van_radius
self.g = g
self.mass = mass
self.massu = massu
self.name = name
self.eneg = eneg
self.quad = quad
self.spin = spin
self.symbol = symbol
self.color = color
@property
def radius(self):
return self.cov_radius
def __repr__(self):
return str(self.A) + self.symbol
def _create():
"""Globally called function for creating the isotope/element API."""
def creator(group):
"""Helper function applied to each symbol group of the raw isotope table."""
symbol = group['symbol'].values[0]
try: # Ghosts and custom atoms don't necessarily have an abundance fraction
mass = (group['mass']*group['af']).sum()
afm = group['af'].sum()
if afm > 0.0:
mass /= afm
except ZeroDivisionError:
mass = group['mass'].mean()
znum = group['Z'].max()
cov_radius = group['cov_radius'].mean()
van_radius = group['van_radius'].mean()
# try:
# color = group.loc[group['af'].idxmax(), 'color']
# except (TypeError, KeyError):
color = group['color'].values[0]
name = group['name'].values[0]
ele = Element(symbol, name, mass, znum, cov_radius, van_radius, color)
# Attached isotopes
for tope in group.apply(lambda s: Isotope(*s.tolist()), axis=1):
setattr(ele, "_"+str(tope.A), tope)
return ele
iso = _rj(_path)
iso.columns = _columns
setattr(_this, "iso", iso)
for element in iso.groupby("symbol").apply(creator):
setattr(_this, element.symbol, element)
def as_df():
"""Return a dataframe of isotopes."""
return _this.iso
def staticdir():
"""Return the location of the static data directory."""
root = _os.path.abspath(_os.path.dirname(__file__))
return _os.path.join(root, "static")
def resource(name):
"""
Return the full path of a named resource in the static directory.
If multiple files with the same name exist, **name** should contain
the first directory as well.
.. code-block:: python
resource("myfile")
resource("test01/test.txt")
resource("test02/test.txt")
"""
for path, _, files in _os.walk(staticdir()):
if name in files:
return _os.path.abspath(_os.path.join(path, name))
# Data order of isotopic (nuclear) properties:
resource = resource("isotopes.json")
_columns = ("A", "Z", "af", "afu", "cov_radius", "van_radius", "g", "mass", "massu", "name",
"eneg", "quad", "spin", "symbol", "color")
_this = _sys.modules[__name__] # Reference to this module
_path = _os.path.abspath(_os.path.join(_os.path.abspath(__file__), resource))
if not hasattr(_this, "H"):
_create()