Skip to content

Commit bcff387

Browse files
add functions to compute muD from xraydb database
1 parent c537fe4 commit bcff387

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-9
lines changed

src/diffpy/labpdfproc/tools.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import copy
22
from pathlib import Path
33

4+
from xraydb import material_mu
5+
46
from diffpy.labpdfproc.mud_calculator import compute_mud
57
from diffpy.utils.scattering_objects.diffraction_objects import QQUANTITIES, XQUANTITIES
68
from diffpy.utils.tools import get_package_info, get_user_info
@@ -158,9 +160,9 @@ def set_xtype(args):
158160
return args
159161

160162

161-
def set_mud(args):
163+
def set_mud_from_zscan_file(args):
162164
"""
163-
Set the mud based on the given input arguments
165+
Set the mud based on the given z-scan file
164166
165167
Parameters
166168
----------
@@ -180,6 +182,49 @@ def set_mud(args):
180182
return args
181183

182184

185+
def set_mud_using_xraydb(args):
186+
"""
187+
Set the mud using xraydb, prompting for parameters if not provided by the user
188+
189+
The function works in this way:
190+
(1) if mu*D is provided, no further input is required, and any additional inputs are written to metadata.
191+
(2) if mu*D is missing but mu is provided, prompt for diameter if missing and compute muD = mu * d.
192+
(3) if neither is provided, prompt for sample name, energy, density, and diameter as needed, then compute muD.
193+
Reference for the xraydb database: https://xraypy.github.io/XrayDB/python.html#xraydb.material_mu.
194+
195+
Parameters
196+
----------
197+
args argparse.Namespace
198+
the arguments from the parser
199+
sample str
200+
the chemical formula or name of material
201+
energy float
202+
the energy in eV
203+
density float or None
204+
material density in gr/cm^3
205+
diameter float
206+
the capillary diameter in mm
207+
208+
Returns
209+
-------
210+
the updated args argparse.Namespace with mu*D
211+
"""
212+
213+
if args.mud:
214+
return args
215+
if args.mu:
216+
args.diameter = args.diameter or float(input("Please enter the capillary diameter (mm): ").strip())
217+
args.mud = args.mu * args.diameter
218+
return args
219+
args.sample = args.sample or input("Please enter the chemical formula or name of material: ").strip()
220+
args.energy = args.energy or float(input("Please enter the energy (eV): ").strip())
221+
args.density = args.density or float(input("Please enter material density (gr/cm^3): ").strip())
222+
args.diameter = args.diameter or float(input("Please enter the capillary diameter (mm): ").strip())
223+
args.mu = material_mu(args.sample, args.energy, density=args.density, kind="total") / 10
224+
args.mud = args.mu * args.diameter
225+
return args
226+
227+
183228
def _load_key_value_pair(s):
184229
items = s.split("=")
185230
key = items[0].strip()
@@ -281,7 +326,8 @@ def preprocessing_args(args):
281326
args.output_directory = set_output_directory(args)
282327
args = set_wavelength(args)
283328
args = set_xtype(args)
284-
args = set_mud(args)
329+
args = set_mud_from_zscan_file(args)
330+
args = set_mud_using_xraydb(args)
285331
args = load_user_metadata(args)
286332
return args
287333

tests/test_tools.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
load_user_metadata,
1414
preprocessing_args,
1515
set_input_lists,
16-
set_mud,
16+
set_mud_from_zscan_file,
17+
set_mud_using_xraydb,
1718
set_output_directory,
1819
set_wavelength,
1920
set_xtype,
@@ -216,10 +217,10 @@ def test_set_xtype_bad():
216217
actual_args = set_xtype(actual_args)
217218

218219

219-
def test_set_mud(user_filesystem):
220+
def test_set_mud_from_zscan_file(user_filesystem):
220221
cli_inputs = ["2.5", "data.xy"]
221222
actual_args = get_args(cli_inputs)
222-
actual_args = set_mud(actual_args)
223+
actual_args = set_mud_from_zscan_file(actual_args)
223224
assert actual_args.mud == pytest.approx(2.5, rel=1e-4, abs=0.1)
224225
assert actual_args.z_scan_file is None
225226

@@ -230,16 +231,41 @@ def test_set_mud(user_filesystem):
230231
expected = [3, str(test_dir / "testfile.xy")]
231232
cli_inputs = ["2.5", "data.xy"] + inputs
232233
actual_args = get_args(cli_inputs)
233-
actual_args = set_mud(actual_args)
234+
actual_args = set_mud_from_zscan_file(actual_args)
234235
assert actual_args.mud == pytest.approx(expected[0], rel=1e-4, abs=0.1)
235236
assert actual_args.z_scan_file == expected[1]
236237

237238

238-
def test_set_mud_bad():
239+
def test_set_mud_from_zscan_file_bad():
239240
cli_inputs = ["2.5", "data.xy", "--z-scan-file", "invalid file"]
240241
actual_args = get_args(cli_inputs)
241242
with pytest.raises(FileNotFoundError, match="Cannot find invalid file. Please specify a valid file path."):
242-
actual_args = set_mud(actual_args)
243+
actual_args = set_mud_from_zscan_file(actual_args)
244+
245+
246+
params_mud = [
247+
# user specified mud
248+
(["0.7952", "data.xy"], [""]),
249+
# user specified mu but not mud
250+
(["data.xy", "--mu", "1.2522", "--diameter", "0.635"], [""]),
251+
(["data.xy", "--mu", "1.2522"], ["0.635"]),
252+
# user did not specify mud or mu
253+
(["data.xy"], ["ZrO2", "17445", "1.009", "0.635"]),
254+
(["data.xy", "--energy", "17445", "--density", "1.009", "--diameter", "0.635"], ["ZrO2"]),
255+
(["data.xy", "--sample", "ZrO2", "--density", "1.009", "--diameter", "0.635"], ["17445.362740959618"]),
256+
(["data.xy", "--sample", "ZrO2", "--energy", "17445", "--diameter", "0.635"], ["1.009"]),
257+
(["data.xy", "--sample", "ZrO2", "--energy", "17445", "--density", "1.009"], ["0.635"]),
258+
(["data.xy", "--sample", "ZrO2", "--energy", "17445", "--density", "1.009", "--diameter", "0.635"], [""]),
259+
]
260+
261+
262+
@pytest.mark.parametrize("cli_inputs, user_inputs", params_mud)
263+
def test_set_mud_using_xraydb(cli_inputs, user_inputs, monkeypatch):
264+
inp_iter = iter(user_inputs)
265+
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
266+
actual_args = get_args(cli_inputs)
267+
actual_args = set_mud_using_xraydb(actual_args)
268+
assert actual_args.mud == pytest.approx(0.7952, rel=1e-4, abs=0.1)
243269

244270

245271
params5 = [

0 commit comments

Comments
 (0)