Skip to content

Commit 1826221

Browse files
add packing fraction, add more tests
1 parent 5180eeb commit 1826221

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/diffpy/utils/tools.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,30 @@ def get_package_info(package_names, metadata=None):
135135
return metadata
136136

137137

138-
def compute_mu_using_xraydb(sample, energy, density=None):
138+
def compute_mu_using_xraydb(sample_composition, energy, density=None, packing_fraction=1):
139139
"""
140-
compute mu using the XrayDB database
140+
Compute the attenuation coefficient (mu) using the XrayDB database
141141
142+
This function calculates mu based on the sample composition and energy.
143+
If density is not provided, a standard reference density (e.g., 0.987 g/cm^3 for H2O) is used.
144+
User can provide either a measured density or an estimated packing fraction (with a standard density).
145+
It is recommended to specify the density, especially for materials like ZrO2, where it can vary.
142146
Reference: https://xraypy.github.io/XrayDB/python.html#xraydb.material_mu
143147
144148
Parameters
145149
----------
146-
sample str
147-
the chemical formula or the name of the material
148-
energy float
149-
the energy in eV
150-
density float or None
151-
material density in gr/cm^3
150+
sample_composition: str
151+
The chemical formula or the name of the material
152+
energy: float
153+
The energy in eV
154+
density: float, optional, Default is None
155+
The mass density of the packed powder/sample in gr/cm^3. If None, a standard density from XrayDB is used.
156+
packing_fraction: float, optional, Default is 1
157+
The fraction of sample in the capillary (between 0 and 1)
152158
153159
Returns
154160
-------
155161
the attenuation coefficient mu in mm^{-1}
156162
"""
157-
mu = material_mu(sample, energy, density=density, kind="total") / 10
163+
mu = material_mu(sample_composition, energy, density=density, kind="total") * packing_fraction / 10
158164
return mu

tests/test_tools.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,25 @@ def test_get_package_info(monkeypatch, inputs, expected):
191191
assert actual_metadata == expected
192192

193193

194-
def test_compute_mu_using_xraydb():
195-
sample, energy, density = "ZrO2", 17445.362740959618, 1.009
196-
actual_mu = compute_mu_using_xraydb(sample, energy, density=density)
197-
assert actual_mu == pytest.approx(1.252, rel=1e-4, abs=0.1)
194+
params_mu = [
195+
# C1: user didn't specify density or packing fraction
196+
({"sample_composition": "H2O", "energy": 10000, "density": None, "packing_fraction": 1}, 0.5330),
197+
# C2: user specified packing fraction only
198+
({"sample_composition": "H2O", "energy": 10000, "density": None, "packing_fraction": 0.5}, 0.2665),
199+
# C3: user specified density only
200+
({"sample_composition": "H2O", "energy": 10000, "density": 0.997, "packing_fraction": 1}, 0.5330),
201+
({"sample_composition": "H2O", "energy": 10000, "density": 0.4985, "packing_fraction": 1}, 0.2665),
202+
# C4: user specified a standard density and a packing fraction
203+
({"sample_composition": "H2O", "energy": 10000, "density": 0.997, "packing_fraction": 0.5}, 0.2665),
204+
]
205+
206+
207+
@pytest.mark.parametrize("inputs, expected", params_mu)
208+
def test_compute_mu_using_xraydb(inputs, expected):
209+
actual_mu = compute_mu_using_xraydb(
210+
inputs["sample_composition"],
211+
inputs["energy"],
212+
density=inputs["density"],
213+
packing_fraction=inputs["packing_fraction"],
214+
)
215+
assert actual_mu == pytest.approx(expected, rel=0.01, abs=0.1)

0 commit comments

Comments
 (0)