Reference
A
property
readonly
¶
Return the mass number A for all nuclei in the table as a numpy array.
binding_energy
property
readonly
¶
Return binding energies instead of mass excesses
count: int
property
readonly
¶
Return the total number of nuclei in the table
Examples:
>>> Table('AME2012').count
2438
It is also possible to do:
>>> len(Table('AME2012'))
2438
ds2n
property
readonly
¶
Calculates the derivative of the neutron separation energies:
ds2n(Z,A) = s2n(Z,A) - s2n(Z,A+2)
ds2p
property
readonly
¶
Calculates the derivative of the neutron separation energies:
ds2n(Z,A) = s2n(Z,A) - s2n(Z,A+2)
even_even
property
readonly
¶
Selects even-even nuclei from the table
even_odd
property
readonly
¶
Selects even-odd nuclei from the table
N
property
readonly
¶
Return the neutron number N for all nuclei in the table as a numpy array.
odd_even: Table
property
readonly
¶
Selects odd-even nuclei from the table
odd_odd
property
readonly
¶
Selects odd-odd nuclei from the table:
>>> Table('FRDM95').odd_odd
Z N
9 9 1.21
11 0.10
13 3.08
15 9.32
...
q_alpha
property
readonly
¶
Return Q_alpha
q_beta
property
readonly
¶
Return Q_beta
s1n: Table
property
readonly
¶
Return 1 neutron separation energy
s1p
property
readonly
¶
Return 1 proton separation energy
s2n
property
readonly
¶
Return 2 neutron separation energy
s2p
property
readonly
¶
Return 2 proton separation energy
Z
property
readonly
¶
Return the proton number Z for all nuclei in the table as a numpy array.
__getattr__(self, attr)
special
¶
Pass properties and method calls to the DataFrame object
Source code in masstable/masstable.py
def __getattr__(self, attr):
# TODO: Pythonize
"Pass properties and method calls to the DataFrame object"
instance_method = getattr(self.df, attr)
if callable(instance_method):
def fn(*args, **kwargs):
result = instance_method(
*args, **kwargs
) # ()->call the instance method
if isinstance(result, (pd.DataFrame, pd.Series)):
try:
name = result.name
except AttributeError:
name = None
return Table(name=name, df=result) # wrap in Table class
return fn
else:
return instance_method
__getitem__(self, index)
special
¶
Access [] operator
Examples¶
>>> Table('DUZU')[82, 126:127]
DUZU
Z N
82 126 -22.29
127 -17.87
>>> Table('AME2012all')[118, :]
AME2012all
Z N
118 173 198.93
174 199.27
175 201.43
Source code in masstable/masstable.py
def __getitem__(self, index):
"""Access [] operator
Examples
--------
>>> Table('DUZU')[82, 126:127]
DUZU
Z N
82 126 -22.29
127 -17.87
>>> Table('AME2012all')[118, :]
AME2012all
Z N
118 173 198.93
174 199.27
175 201.43
"""
if isinstance(index, tuple) and len(index) == 2:
# can probably be simplified with pd.IndexSlice
if isinstance(index[0], int): # single N: "[82, :]"
startZ, stopZ = index[0], index[0]
if isinstance(index[1], int):
startN, stopN = index[1], index[1] # single N: "[:, 126]"
if isinstance(index[0], slice): # Z slice: "[:, 126]"
startZ, stopZ, stepZ = index[0].start, index[0].stop, index[0].step
if isinstance(index[1], slice): # N slice: "[:, 126]"
startN, stopN, stepN = index[1].start, index[1].stop, index[1].step
if not startZ:
startZ = self.Z.min() # might be optimized
if not stopZ:
stopZ = self.Z.max()
if not startN:
startN = self.N.min()
if not stopN:
stopN = self.N.max()
x = self.df.reset_index()
x = x.loc[
(x.Z >= startZ) & (x.Z <= stopZ) & (x.N >= startN) & (x.N <= stopN)
]
df = x.set_index(["Z", "N"]).sort_index(0)
return Table(df=df[df.columns[0]], name=self.name)
if isinstance(index, list):
return self.at(index)
if isinstance(index, Callable):
return self.select(index)
__init__(self, name='', df=None)
special
¶
Init from a Series/Dataframe (df) of a file (name)
Source code in masstable/masstable.py
def __init__(self, name: str = "", df: pd.DataFrame = None):
"Init from a Series/Dataframe (df) of a file (name)"
if df is not None: # init from dataframe
self.df = df
self.name = name
elif name in self._names: # init from name
self.name = name
self.df = self.from_name(name).df
# self.df.name = name
else:
print("Error: Invalid table name. Valid names are:")
print(" ".join(Table.names))
return None
__len__(self)
special
¶
Return the total number of nuclei
Example¶
>>> len(Table('AME2012'))
2438
Source code in masstable/masstable.py
def __len__(self):
"""Return the total number of nuclei
Example
-------
>>> len(Table('AME2012'))
2438
"""
return len(self.df)
at(self, nuclei)
¶
Return a selection of the Table at positions given by nuclei
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nuclei |
List[Tuple[int, int]] |
list of tuples A list where each element is tuple of the form (Z,N) |
required |
Example¶
Return binding energies at magic nuclei:
>>> magic_nuclei = [(20,28), (50,50), (50,82), (82,126)]
>>> Table('AME2012').binding_energy.at(magic_nuclei)
Z N
20 28 416.014215
50 50 825.325172
82 1102.876416
82 126 1636.486450
Source code in masstable/masstable.py
def at(self, nuclei: List[Tuple[int, int]]) -> Table:
"""Return a selection of the Table at positions given by ``nuclei``
Parameters:
nuclei: list of tuples
A list where each element is tuple of the form (Z,N)
Example
-------
Return binding energies at magic nuclei:
>>> magic_nuclei = [(20,28), (50,50), (50,82), (82,126)]
>>> Table('AME2012').binding_energy.at(magic_nuclei)
Z N
20 28 416.014215
50 50 825.325172
82 1102.876416
82 126 1636.486450
"""
index = pd.MultiIndex.from_tuples(nuclei, names=["Z", "N"])
return Table(df=self.df.loc[index], name=self.name)
chart_plot(self, ax=None, cmap='RdBu', xlabel='N', ylabel='Z', grid_on=True, colorbar=True, save_path=None)
¶
Plot a nuclear chart with (N,Z) as axis and the values of the Table as a color scale
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ax |
|
optional matplotlib axes defaults to current axes |
None |
cmap |
str |
a matplotlib colormap default: 'RdBu' |
'RdBu' |
xlabel |
str |
string representing the label of the x axis default: 'N' |
'N' |
ylabel |
str |
string, default: 'Z' the label of the x axis |
'Z' |
grid_on |
bool |
(boolean), default: True, whether to draw the axes grid or not |
True |
colorbar |
bool |
boolean, default: True whether to draw a colorbar or not |
True |
Returns:
Type | Description |
---|---|
ax |
a matplotlib axes object |
Example¶
Plot the theoretical deviation for the Möller's model::
>>> Table('FRDM95').error().chart_plot()
Source code in masstable/masstable.py
def chart_plot(
self,
ax=None,
cmap: str = "RdBu",
xlabel: str = "N",
ylabel: str = "Z",
grid_on: bool = True,
colorbar: bool = True,
save_path: str = None,
):
"""Plot a nuclear chart with (N,Z) as axis and the values
of the Table as a color scale
Parameters:
ax: optional matplotlib axes
defaults to current axes
cmap: a matplotlib colormap
default: 'RdBu'
xlabel: string representing the label of the x axis
default: 'N'
ylabel: string, default: 'Z'
the label of the x axis
grid_on: (boolean), default: True,
whether to draw the axes grid or not
colorbar: boolean, default: True
whether to draw a colorbar or not
Returns:
ax: a matplotlib axes object
Example
-------
Plot the theoretical deviation for the Möller's model::
>>> Table('FRDM95').error().chart_plot()
"""
from scipy.interpolate import griddata
from numpy import linspace, meshgrid
import matplotlib.pyplot as plt
# extract the 1D arrays to be plotted
x = self.dropna().N
y = self.dropna().Z
z = self.dropna().values
# convert to matplotlibs grid format
xi = linspace(min(x), max(x), max(x) - min(x) + 1)
yi = linspace(min(y), max(y), max(y) - min(y) + 1)
X, Y = meshgrid(xi, yi)
Z = griddata((x, y), z, (X, Y), method="linear")
# create and customize plot
if ax is None:
ax = plt.gca()
chart = ax.pcolormesh(X, Y, Z, cmap=cmap, shading="auto")
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.grid(grid_on)
ax.set_aspect("equal")
if colorbar:
plt.colorbar(chart)
if save_path is not None:
fig = plt.gcf()
fig.savefig(save_path)
return ax
derived(self, name, relative_coords, formula)
¶
Helper function for derived quantities
Source code in masstable/masstable.py
def derived(self, name: str, relative_coords: Tuple[int, int], formula: Callable):
"""Helper function for derived quantities"""
dZ, dN = relative_coords
daughter_idx = [(Z + dZ, N + dN) for Z, N in self.df.index]
idx = self.df.index.intersection(daughter_idx)
values = formula(self.df.values, self.df.reindex(daughter_idx).values)
return Table(
df=pd.Series(values, index=self.df.index, name=name + "(" + self.name + ")")
)
error(self, relative_to='AME2003')
¶
Calculate error difference
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relative_to |
str |
a valid mass table name |
'AME2003' |
Example¶
>>> Table('DUZU').error(relative_to='AME2003').dropna()
Z N
8 8 0.667001
9 0.138813
10 -0.598478
11 -0.684870
12 -1.167462
Source code in masstable/masstable.py
def error(self, relative_to: str = "AME2003") -> Table:
"""
Calculate error difference
Parameters:
relative_to: a valid mass table name
Example
-------
>>> Table('DUZU').error(relative_to='AME2003').dropna()
Z N
8 8 0.667001
9 0.138813
10 -0.598478
11 -0.684870
12 -1.167462
"""
df = self.df - Table(relative_to).df
return Table(df=df)
from_file(filename, name='')
classmethod
¶
Imports a mass table from a file
Source code in masstable/masstable.py
@classmethod
def from_file(cls, filename: str, name: str = ""):
"Imports a mass table from a file"
df = pd.read_csv(filename, header=0, delim_whitespace=True, index_col=[0, 1])[
"M"
]
df.name = name
return cls(df=df, name=name)
from_name(name)
classmethod
¶
Imports a mass table from a file
Source code in masstable/masstable.py
@classmethod
def from_name(cls, name: str):
"Imports a mass table from a file"
filename = os.path.join(package_dir, "data", name + ".txt")
return cls.from_file(filename, name)
from_ZNM(Z, N, M, name='')
classmethod
¶
Creates a table from arrays Z, N and M
Examples:
>>> Z = [82, 82, 83]
>>> N = [126, 127, 130]
>>> M = [-21.34, -18.0, -14.45]
>>> Table.from_ZNM(Z, N, M, name='Custom Table')
Z N
82 126 -21.34
127 -18.00
83 130 -14.45
Name: Custom Table, dtype: float64
Source code in masstable/masstable.py
@classmethod
def from_ZNM(cls, Z, N, M, name=""):
"""
Creates a table from arrays Z, N and M
Example:
________
>>> Z = [82, 82, 83]
>>> N = [126, 127, 130]
>>> M = [-21.34, -18.0, -14.45]
>>> Table.from_ZNM(Z, N, M, name='Custom Table')
Z N
82 126 -21.34
127 -18.00
83 130 -14.45
Name: Custom Table, dtype: float64
"""
df = pd.DataFrame.from_dict({"Z": Z, "N": N, "M": M}).set_index(["Z", "N"])["M"]
df.name = name
return cls(df=df, name=name)
intersection(self, table)
¶
Select nuclei which also belong to table
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
Table |
a Table object |
required |
Example¶
>>> Table('AME2003').intersection(Table('AME1995'))
Source code in masstable/masstable.py
def intersection(self, table: Table) -> Table:
"""
Select nuclei which also belong to ``table``
Parameters:
table: a Table object
Example
-------
>>> Table('AME2003').intersection(Table('AME1995'))
"""
idx = self.df.index.intersection(table.df.index)
return Table(df=self.df[idx], name=self.name)
names()
classmethod
¶
Return a list of the names of all supported mass models
Examples:
>>> Table.names()
['AME2003', 'AME2003all', 'AME2012', 'AME2012all', 'AME1995',
'AME1995all', 'DUZU', 'FRDM95', 'KTUY05', 'ETFSI12', 'HFB14',
'HFB26', 'TCSM12', 'TCSM13', 'BR2013', 'MAJA88', 'GK88', 'WS32010', 'WS32011',
'SVM13']
Source code in masstable/masstable.py
@classmethod
def names(cls):
"""Return a list of the names of all supported mass models
Example:
>>> Table.names()
['AME2003', 'AME2003all', 'AME2012', 'AME2012all', 'AME1995',
'AME1995all', 'DUZU', 'FRDM95', 'KTUY05', 'ETFSI12', 'HFB14',
'HFB26', 'TCSM12', 'TCSM13', 'BR2013', 'MAJA88', 'GK88', 'WS32010', 'WS32011',
'SVM13']
"""
return cls._names
not_in(self, table)
¶
Select nuclei not in table
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
Table |
Table Table object from where nuclei should be removed |
required |
Example¶
Find the new nuclei in AME2003 with Z,N >= 8:
>>> Table('AME2003').not_in(Table('AME1995'))[8:,8:].count
389
Source code in masstable/masstable.py
def not_in(self, table: Table) -> Table:
"""
Select nuclei not in table
Parameters:
table: Table
Table object from where nuclei should be removed
Example
-------
Find the new nuclei in AME2003 with Z,N >= 8:
>>> Table('AME2003').not_in(Table('AME1995'))[8:,8:].count
389
"""
idx = self.df.index.difference(table.df.index)
return Table(df=self.df[idx], name=self.name)
rmse(self, relative_to='AME2003')
¶
Calculate root mean squared error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
relative_to |
str |
a valid mass table name. |
'AME2003' |
Example
>>> template = '{0:10}|{1:^6.2f}|{2:^6.2f}|{3:^6.2f}'
>>> print('Model ', 'AME95 ', 'AME03 ', 'AME12 ') # Table header
... for name in Table.names:
... print(template.format(name, Table(name).rmse(relative_to='AME1995'),
... Table(name).rmse(relative_to='AME2003'),
... Table(name).rmse(relative_to='AME2012')))
Model AME95 AME03 AME12
AME2003 | 0.13 | 0.00 | 0.13
AME2003all| 0.42 | 0.40 | 0.71
AME2012 | 0.16 | 0.13 | 0.00
AME2012all| 0.43 | 0.43 | 0.69
AME1995 | 0.00 | 0.13 | 0.16
AME1995all| 0.00 | 0.17 | 0.21
DUZU | 0.52 | 0.52 | 0.76
FRDM95 | 0.79 | 0.78 | 0.95
KTUY05 | 0.78 | 0.77 | 1.03
ETFSI12 | 0.84 | 0.84 | 1.04
HFB14 | 0.84 | 0.83 | 1.02
Source code in masstable/masstable.py
def rmse(self, relative_to: str = "AME2003"):
"""Calculate root mean squared error
Parameters:
relative_to: a valid mass table name.
Example
>>> template = '{0:10}|{1:^6.2f}|{2:^6.2f}|{3:^6.2f}'
>>> print('Model ', 'AME95 ', 'AME03 ', 'AME12 ') # Table header
... for name in Table.names:
... print(template.format(name, Table(name).rmse(relative_to='AME1995'),
... Table(name).rmse(relative_to='AME2003'),
... Table(name).rmse(relative_to='AME2012')))
Model AME95 AME03 AME12
AME2003 | 0.13 | 0.00 | 0.13
AME2003all| 0.42 | 0.40 | 0.71
AME2012 | 0.16 | 0.13 | 0.00
AME2012all| 0.43 | 0.43 | 0.69
AME1995 | 0.00 | 0.13 | 0.16
AME1995all| 0.00 | 0.17 | 0.21
DUZU | 0.52 | 0.52 | 0.76
FRDM95 | 0.79 | 0.78 | 0.95
KTUY05 | 0.78 | 0.77 | 1.03
ETFSI12 | 0.84 | 0.84 | 1.04
HFB14 | 0.84 | 0.83 | 1.02
"""
error = self.error(relative_to=relative_to)
return math.sqrt((error.df ** 2).mean())
select(self, condition, name='')
¶
Selects nuclei according to a condition on Z,N or M
Parameters:
Name | Type | Description | Default |
---|---|---|---|
condition |
Callable |
Can have one of the signatures f(M), f(Z,N) or f(Z, N, M) must return a boolean value |
required |
name |
str |
optional name for the resulting Table |
'' |
Examples:
Select all nuclei with A > 160:
>>> A_gt_160 = lambda Z,N: Z + N > 160
>>> Table('AME2003').select(A_gt_160)
Source code in masstable/masstable.py
def select(self, condition: Callable, name: str = "") -> Table:
"""
Selects nuclei according to a condition on Z,N or M
Parameters:
condition:
Can have one of the signatures f(M), f(Z,N) or f(Z, N, M)
must return a boolean value
name:
optional name for the resulting Table
Example:
Select all nuclei with A > 160:
>>> A_gt_160 = lambda Z,N: Z + N > 160
>>> Table('AME2003').select(A_gt_160)
"""
if condition.__code__.co_argcount == 1:
idx = [(Z, N) for (Z, N), M in self if condition(M)]
if condition.__code__.co_argcount == 2:
idx = [(Z, N) for (Z, N) in self.index if condition(Z, N)]
if condition.__code__.co_argcount == 3:
idx = [(Z, N) for (Z, N), M in self if condition(Z, N, M)]
index = pd.MultiIndex.from_tuples(idx, names=["Z", "N"])
return Table(df=self.df.loc[index], name=name)
to_file(self, path)
¶
Export the contents to a file as comma separated values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str |
File path where the data should be saved to |
required |
Examples:
Export the last ten elements of AME2012 to a new file:
>>> Table('AME2012').tail(10).to_file('last_ten.txt')
Source code in masstable/masstable.py
def to_file(self, path: str):
"""Export the contents to a file as comma separated values.
Parameters:
path : File path where the data should be saved to
Example:
Export the last ten elements of AME2012 to a new file:
>>> Table('AME2012').tail(10).to_file('last_ten.txt')
"""
with open(path, "w") as f:
f.write("Z N M\n")
self.df.to_csv(path, sep="\t", mode="a")