Skip to content

Commit

Permalink
Remove pint dependency (#12)
Browse files Browse the repository at this point in the history
Units are noted in the DataFrame column names for now, e.g., "Energy
Level (MeV)".
  • Loading branch information
markbandstra committed Apr 19, 2017
1 parent 796a446 commit 84e43c3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 55 deletions.
67 changes: 32 additions & 35 deletions becquerel/tools/nndc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np
import pandas as pd
from uncertainties import ufloat
from .units import units


WALLET_DECAY_MODE = {
Expand Down Expand Up @@ -343,7 +342,7 @@ def perform(self):
for col in ['A', 'Z', 'N', 'M']:
if col in self.keys():
self._convert_column(col, int)
# add units and combine uncertainties
# combine uncertainty columns and add unit labels
self._add_units_uncertainties()
# add some more columns
self._add_columns_energy_levels()
Expand Down Expand Up @@ -382,12 +381,12 @@ def _add_columns_energy_levels(self):
for a, z in A_Z:
isotope = (self['A'] == a) & (self['Z'] == z)
e_levels = []
for e_level in self['Energy Level'][isotope]:
for e_level in self['Energy Level (MeV)'][isotope]:
if e_level not in e_levels:
e_levels.append(e_level)
e_levels = sorted(e_levels, key=lambda x: x.magnitude)
e_levels = sorted(e_levels)
for M, e_level in enumerate(e_levels):
isomer = isotope & (self['Energy Level'] == e_level)
isomer = isotope & (self['Energy Level (MeV)'] == e_level)
self._df['M'][isomer] = M
if M > 0:
if len(e_levels) > 2:
Expand All @@ -398,57 +397,58 @@ def _add_columns_energy_levels(self):
def _add_units_uncertainties(self):
"""Add units and uncertainties with some columns as applicable."""
if 'Energy Level Unc.' in self.keys():
self._convert_column_uncertainty('Energy Level', units.MeV)
self._convert_column_uncertainty('Energy Level')
else:
self._convert_column('Energy Level', float, units.MeV)
self._convert_column('Energy Level', float)
self._df.rename(
columns={'Energy Level': 'Energy Level (MeV)'}, inplace=True)

if 'Mass Excess' in self.keys():
self._convert_column_uncertainty('Mass Excess', units.MeV)
self._convert_column_uncertainty('Mass Excess')
self._df.rename(
columns={'Mass Excess': 'Mass Excess (MeV)'}, inplace=True)

self._convert_column('T1/2 (s)', float, units.s)
self._df['T1/2'] = self._df['T1/2 (s)']
del self._df['T1/2 (s)']
self._convert_column('T1/2 (s)', float)

if 'Abundance (%)' in self.keys():
self._convert_column_uncertainty('Abundance (%)', units.percent)
self._df['Abundance'] = self._df['Abundance (%)']
del self._df['Abundance (%)']
self._convert_column_uncertainty('Abundance (%)')

if 'Branching (%)' in self.keys():
self._convert_column(
'Branching (%)',
lambda x: _parse_float_uncertainty(x, ''), units.percent)
self._df['Branching'] = self._df['Branching (%)']
del self._df['Branching (%)']
lambda x: _parse_float_uncertainty(x, ''))

if 'Radiation Energy' in self.keys():
self._convert_column_uncertainty('Radiation Energy', units.keV)
self._convert_column_uncertainty('Radiation Energy')
self._df.rename(
columns={'Radiation Energy': 'Radiation Energy (keV)'},
inplace=True)

if 'Endpoint Energy' in self.keys():
self._convert_column_uncertainty('Endpoint Energy', units.keV)
self._convert_column_uncertainty('Endpoint Energy')
self._df.rename(
columns={'Endpoint Energy': 'Endpoint Energy (keV)'},
inplace=True)

if 'Radiation Intensity (%)' in self.keys():
self._convert_column_uncertainty(
'Radiation Intensity (%)', units.percent)
self._convert_column_uncertainty('Radiation Intensity (%)')

if 'Dose' in self.keys():
self._convert_column_uncertainty(
'Dose', units.parse_expression('MeV / (Bq * s)'))
self._convert_column_uncertainty('Dose')
self._df.rename(
columns={'Dose': 'Dose (MeV / Bq / s)'}, inplace=True)

def _convert_column(self, col, function, unit=None, null=None):
def _convert_column(self, col, function, null=None):
"""Convert column from string to another type."""
col_new = []
for x in self[col]:
if x == '':
col_new.append(null)
else:
if unit is None:
col_new.append(function(x))
else:
col_new.append(function(x) * unit)
col_new.append(function(x))
self._df[col] = col_new

def _convert_column_uncertainty(self, col, unit=None):
def _convert_column_uncertainty(self, col):
"""Combine column and its uncertainty into one column."""
new_col = []
for x, dx in zip(self[col], self[col + ' Unc.']):
Expand All @@ -460,20 +460,17 @@ def _convert_column_uncertainty(self, col, unit=None):
new_col.append(None)
else:
x2 = _parse_float_uncertainty(x, dx)
if unit is None:
new_col.append(x2)
else:
new_col.append(x2 * unit)
new_col.append(x2)
self._df[col] = new_col
del self._df[col + ' Unc.']

def _sort_columns(self):
"""Sort columns."""
preferred_order = [
'Z', 'Element', 'A', 'N', 'M', 'm', 'JPi', 'T1/2',
'Energy Level', 'Decay Mode', 'Branching',
'Energy Level (MeV)', 'Decay Mode', 'Branching (%)',
'Radiation', 'Radiation subtype',
'Radiation Energy', 'Radiation Intensity',
'Radiation Energy (keV)', 'Radiation Intensity (%)',
]
new_cols = []
for col in preferred_order:
Expand Down
40 changes: 20 additions & 20 deletions examples/nndc_chart_of_nuclides.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@
from __future__ import print_function
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from becquerel.tools import nndc, units
from becquerel.tools import nndc


def colorscale_half_life(half_life):
"""Color scale to mimic NNDC's nuclear chart color scale."""
if half_life is None:
return '#E1E1E1'
elif half_life < 1e-15 * units.s:
elif half_life < 1e-15:
return '#FF9473'
elif half_life < 1e-7 * units.s:
elif half_life < 1e-7:
return '#F7BDDD'
elif half_life < 1e-6 * units.s:
elif half_life < 1e-6:
return '#FFC6A5'
elif half_life < 1e-5 * units.s:
elif half_life < 1e-5:
return '#FFE7C6'
elif half_life < 1e-4 * units.s:
elif half_life < 1e-4:
return '#FFFF9B'
elif half_life < 1e-3 * units.s:
elif half_life < 1e-3:
return '#FFFF0C'
elif half_life < 1e-2 * units.s:
elif half_life < 1e-2:
return '#E7F684'
elif half_life < 1e-1 * units.s:
elif half_life < 1e-1:
return '#D6EF38'
elif half_life < 1e0 * units.s:
elif half_life < 1e0:
return '#ADDE63'
elif half_life < 1e1 * units.s:
elif half_life < 1e1:
return '#53B552'
elif half_life < 1e2 * units.s:
elif half_life < 1e2:
return '#64BDB5'
elif half_life < 1e3 * units.s:
elif half_life < 1e3:
return '#63C6DE'
elif half_life < 1e4 * units.s:
elif half_life < 1e4:
return '#03A5C6'
elif half_life < 1e5 * units.s:
elif half_life < 1e5:
return '#0A9A94'
elif half_life < 1e7 * units.s:
elif half_life < 1e7:
return '#0284A5'
elif half_life < 1e10 * units.s:
elif half_life < 1e10:
return '#3152A5'
elif half_life < 1e15 * units.s:
elif half_life < 1e15:
return '#29016B'
elif half_life > 1e15 * units.s:
elif half_life > 1e15:
return 'black'


Expand All @@ -62,7 +62,7 @@ def colorscale_half_life(half_life):
print('-' * 70)
print(z, n)
isotope = (data['Z'] == z) & (data['N'] == n) & (data['M'] == 0)
half_life = data[isotope]['T1/2']
half_life = data[isotope]['T1/2 (s)']
if len(half_life) == 0:
continue
print('half_life:', half_life)
Expand Down

0 comments on commit 84e43c3

Please sign in to comment.