Skip to content

Commit 9e4bc0d

Browse files
refine docstring and test comments
1 parent e54f9bb commit 9e4bc0d

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

src/diffpy/utils/tools.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,29 @@ def get_package_info(package_names, metadata=None):
136136

137137

138138
def compute_mu_using_xraydb(sample_composition, energy, density=None, packing_fraction=1):
139-
"""
140-
Compute the attenuation coefficient (mu) using the XrayDB database
139+
"""Compute the attenuation coefficient (mu) using the XrayDB database.
141140
142-
This function calculates mu based on the sample composition and energy.
141+
Computes mu based on the sample composition and energy.
143142
If density is not provided, a standard reference density (e.g., 0.987 g/cm^3 for H2O) is used.
144143
User can provide either a measured density or an estimated packing fraction (with a standard density).
145144
It is recommended to specify the density, especially for materials like ZrO2, where it can vary.
146-
Reference: https://xraypy.github.io/XrayDB/python.html#xraydb.material_mu
145+
Reference: https://xraypy.github.io/XrayDB/python.html#xraydb.material_mu.
147146
148147
Parameters
149148
----------
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
149+
sample_composition : str
150+
The chemical formula or the name of the material.
151+
energy : float
152+
The energy in eV.
153+
density : float, optional, Default is None
155154
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)
155+
packing_fraction : float, optional, Default is 1
156+
The fraction of sample in the capillary (between 0 and 1).
158157
159158
Returns
160159
-------
161-
the attenuation coefficient mu in mm^{-1}
160+
mu : float
161+
The attenuation coefficient mu in mm^{-1}.
162162
"""
163163
mu = material_mu(sample_composition, energy, density=density, kind="total") * packing_fraction / 10
164164
return mu

tests/test_tools.py

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

193193

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):
194+
@pytest.mark.parametrize(
195+
"inputs, expected_mu",
196+
[
197+
# Test whether the function returns the correct mu
198+
( # C1: No density or packing fraction provided, expect to compute mu based on standard density
199+
{
200+
"sample_composition": "H2O",
201+
"energy": 10000,
202+
"density": None,
203+
"packing_fraction": 1,
204+
},
205+
0.5330,
206+
),
207+
( # C2: Packing fraction (=0.5) provided only, expect to return half of mu based on standard density
208+
{
209+
"sample_composition": "H2O",
210+
"energy": 10000,
211+
"density": None,
212+
"packing_fraction": 0.5,
213+
},
214+
0.2665,
215+
),
216+
( # C3: Density provided only, expect to compute mu based on density
217+
# 1. Standard density provided, expect to return the same mu as C1
218+
{
219+
"sample_composition": "H2O",
220+
"energy": 10000,
221+
"density": 0.997,
222+
"packing_fraction": 1,
223+
},
224+
0.5330,
225+
),
226+
( # 2. Lower density for H2O (half of standard), expect to return half of mu based on standard density
227+
{
228+
"sample_composition": "H2O",
229+
"energy": 10000,
230+
"density": 0.4985,
231+
"packing_fraction": 1,
232+
},
233+
0.2665,
234+
),
235+
( # C4: Both standard density and packing fraction are provided, expect to compute the same mu as C2
236+
{
237+
"sample_composition": "H2O",
238+
"energy": 10000,
239+
"density": 0.997,
240+
"packing_fraction": 0.5,
241+
},
242+
0.2665,
243+
),
244+
],
245+
)
246+
def test_compute_mu_using_xraydb(inputs, expected_mu):
209247
actual_mu = compute_mu_using_xraydb(
210248
inputs["sample_composition"],
211249
inputs["energy"],
212250
density=inputs["density"],
213251
packing_fraction=inputs["packing_fraction"],
214252
)
215-
assert actual_mu == pytest.approx(expected, rel=0.01, abs=0.1)
253+
assert actual_mu == pytest.approx(expected_mu, rel=0.01, abs=0.1)

0 commit comments

Comments
 (0)