Skip to content

Commit

Permalink
refactor test and some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jokasimr committed Sep 24, 2024
1 parent 44bf714 commit 7b74c61
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions tests/absorption/correction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
from scippneutron.absorption import Cylinder, Material, compute_transmission_map


def transmission_fraction_case1(effective_attenuation_factor):
'''Transmission fraction at a point on the z-axis
infinitely far away when the sample is a cylinder
symmetrical around y, with 1mm radius and 1mm height.
'''
return (
2
/ np.pi
* quad(
lambda x: (
np.sqrt(1 - x**2)
* np.exp(-2 * effective_attenuation_factor * np.sqrt(1 - x**2))
),
-1,
1,
)[0]
)


@pytest.mark.parametrize('scattering_cross_section', [0.1, 0.5, 1.0])
def test_compute_transmission_map(scattering_cross_section):
material = Material(
Expand All @@ -21,20 +40,6 @@ def test_compute_transmission_map(scattering_cross_section):
sc.scalar(1.0, unit='mm'),
)

def transmission_fraction(effective_attenuation_factor):
return (
2
/ np.pi
* quad(
lambda x: (
np.sqrt(1 - x**2)
* np.exp(-2 * effective_attenuation_factor * np.sqrt(1 - x**2))
),
-1,
1,
)[0]
)

tm = compute_transmission_map(
cylinder,
material,
Expand All @@ -45,14 +50,15 @@ def transmission_fraction(effective_attenuation_factor):
)
assert_allclose(
tm['wavelength', 0]['x', 0].data,
sc.scalar(transmission_fraction(scattering_cross_section)),
sc.scalar(transmission_fraction_case1(scattering_cross_section)),
rtol=sc.scalar(1e-3),
)


@pytest.mark.parametrize('scattering_cross_section', [0.1, 0.5, 1.0])
def test_compute_transmission_map_wavelength_dependent(scattering_cross_section):
material = Material(
# Modify the absorption cross section
sc.scalar(scattering_cross_section / 5, unit='mm**2'),
sc.scalar(0, unit='mm**2'),
sc.scalar(1.0, unit='1/mm**3'),
Expand All @@ -64,32 +70,24 @@ def test_compute_transmission_map_wavelength_dependent(scattering_cross_section)
sc.scalar(1.0, unit='mm'),
)

def transmission_fraction(effective_attenuation_factor):
return (
2
/ np.pi
* quad(
lambda x: (
np.sqrt(1 - x**2)
* np.exp(-2 * effective_attenuation_factor * np.sqrt(1 - x**2))
),
-1,
1,
)[0]
)

tm = compute_transmission_map(
cylinder,
material,
beam_direction=sc.vector([0, 0, 1]),
wavelength=sc.linspace(
'wavelength', 5 * 1.7982, 5 * 1.7982, 1, unit='angstrom'
# Set wavelength so that it cancels the
# modified absorption cross section
'wavelength',
5 * 1.7982,
5 * 1.7982,
1,
unit='angstrom',
),
detector_position=sc.vectors(dims='x', values=[[0, 0, 1]], unit='m'),
quadrature_kind='expensive',
)
assert_allclose(
tm['wavelength', 0]['x', 0].data,
sc.scalar(transmission_fraction(scattering_cross_section)),
sc.scalar(transmission_fraction_case1(scattering_cross_section)),
rtol=sc.scalar(1e-3),
)

0 comments on commit 7b74c61

Please sign in to comment.