Skip to content

Commit

Permalink
remove double valued energies at edges and adding test for interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsteve committed Sep 3, 2020
1 parent 54cbcc1 commit 3995ed1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions roentgen/absorption/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ def __init__(self, material):
self.energy = u.Quantity(data[:, 0] * 1000, "keV")
self.data = u.Quantity(data[:, 1], "cm^2/g")

self._remove_double_vals_from_data()

data_energy_kev = np.log10(self.energy.value)
data_attenuation_coeff = np.log10(self.data.value)
self._f = interpolate.interp1d(
Expand All @@ -318,3 +320,14 @@ def __init__(self, material):
self.func = lambda x: u.Quantity(
10 ** self._f(np.log10(x.to("keV").value)), "cm^2/g"
)

def _remove_double_vals_from_data(self):
"""Remove double-values energy values. Edges are represented with
the same energy index and at the bottom and top value of the edge. This
must be removed to enable correct interpolation."""
uniq, count = np.unique(self.energy, return_counts=True)
duplicates = uniq[count > 1]
for this_dup in duplicates:
ind = (self.energy == this_dup).nonzero()
# shift the first instance of the energy, the bottom of the edge
self.energy[ind[0][0]] -= 1e-3 * u.eV
18 changes: 18 additions & 0 deletions roentgen/tests/test_massatten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from numpy.testing import assert_allclose

import astropy.units as u

from roentgen.absorption.material import MassAttenuationCoefficient


def test_interpolate_matches_at_data():
for this_element in ['Te', 'Si', 'Ge', 'cdte']:
te = MassAttenuationCoefficient(this_element)
assert_allclose(te.data, te.func(te.energy))

# now change one point and make sure it no longer works
te.energy[0] = 10 * u.keV
with pytest.raises(AssertionError):
assert_allclose(te.data, te.func(te.energy))

1 comment on commit 3995ed1

@rschwartz70
Copy link

Choose a reason for hiding this comment

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

Did the numpy interp1d not work as is? If it did, I think that's a better way to handle this than to make a discreet shift in energy at the bottom of the edge.

Please sign in to comment.