From c5c2da95207623015ac7caa9a1d7769afbd01de1 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Sun, 26 Jan 2025 20:57:08 -0500 Subject: [PATCH 1/4] docs: add news --- news/wavelength.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/wavelength.rst diff --git a/news/wavelength.rst b/news/wavelength.rst new file mode 100644 index 0000000..17fff49 --- /dev/null +++ b/news/wavelength.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* Increased the number of significant figures for wavelength and separated values for Ka1 and Ka2. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From 703b16a94647f7a546fb2e87c66a1bf4527f3ace Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Sun, 26 Jan 2025 21:00:47 -0500 Subject: [PATCH 2/4] fix: fix wavelength values --- src/diffpy/labpdfproc/tools.py | 15 ++++++++++++++- tests/test_tools.py | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/diffpy/labpdfproc/tools.py b/src/diffpy/labpdfproc/tools.py index 32cfaa2..e47bdb8 100644 --- a/src/diffpy/labpdfproc/tools.py +++ b/src/diffpy/labpdfproc/tools.py @@ -4,7 +4,20 @@ from diffpy.utils.diffraction_objects import ANGLEQUANTITIES, QQUANTITIES, XQUANTITIES from diffpy.utils.tools import check_and_build_global_config, compute_mud, get_package_info, get_user_info -WAVELENGTHS = {"Mo": 0.71073, "Ag": 0.59, "Cu": 1.5406} +# Reference values are taken from https://x-server.gmca.aps.anl.gov/cgi/www_dbli.exe?x0hdb=waves +# Ka1Ka2 values are calculated as: (Ka1 * 2 + Ka2) / 3 +# For CuKa1Ka2: (1.54056 * 2 + 1.544398) / 3 = 1.54184 +WAVELENGTHS = { + "Mo": 0.71073, + "MoKa1": 0.70930, + "MoKa1Ka2": 0.71073, + "Ag": 0.56087, + "AgKa1": 0.55941, + "AgKa1Ka2": 0.56087, + "Cu": 1.54184, + "CuKa1": 1.54056, + "CuKa1Ka2": 1.54184, +} known_sources = [key for key in WAVELENGTHS.keys()] # Exclude wavelength from metadata to prevent duplication, diff --git a/tests/test_tools.py b/tests/test_tools.py index 778492b..929b444 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -168,7 +168,7 @@ def test_set_output_directory_bad(user_filesystem): "inputs, expected", [ ([], {"wavelength": 0.71073, "anode_type": "Mo"}), - (["--anode-type", "Ag"], {"wavelength": 0.59, "anode_type": "Ag"}), + (["--anode-type", "Ag"], {"wavelength": 0.56087, "anode_type": "Ag"}), (["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}), (["--wavelength", "0.25", "--anode-type", "Ag"], {"wavelength": 0.25, "anode_type": None}), ], From 64e7c4da6e596380ad91100dea1a95a51c75b6cc Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Mon, 27 Jan 2025 11:30:27 -0500 Subject: [PATCH 3/4] fix: make anode type case insensitive, add related tests --- src/diffpy/labpdfproc/tools.py | 19 ++++++++++--------- tests/test_tools.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/diffpy/labpdfproc/tools.py b/src/diffpy/labpdfproc/tools.py index e47bdb8..4b52148 100644 --- a/src/diffpy/labpdfproc/tools.py +++ b/src/diffpy/labpdfproc/tools.py @@ -150,17 +150,18 @@ def set_wavelength(args): raise ValueError( "No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength." ) - if not args.wavelength and args.anode_type and args.anode_type not in WAVELENGTHS: - raise ValueError( - f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }." - ) - - if args.wavelength: - delattr(args, "anode_type") - elif args.anode_type: + elif not args.wavelength and args.anode_type: + matched_anode_type = next((key for key in WAVELENGTHS if key.lower() == args.anode_type.lower()), None) + if matched_anode_type is None: + raise ValueError( + f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }." + ) + args.anode_type = matched_anode_type args.wavelength = WAVELENGTHS[args.anode_type] - else: + elif not args.wavelength: args.wavelength = WAVELENGTHS["Mo"] + else: + delattr(args, "anode_type") return args diff --git a/tests/test_tools.py b/tests/test_tools.py index 929b444..77d649e 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -167,9 +167,26 @@ def test_set_output_directory_bad(user_filesystem): @pytest.mark.parametrize( "inputs, expected", [ + # C1: nothing passed in, expect default is Mo ([], {"wavelength": 0.71073, "anode_type": "Mo"}), + # C2: only a valid anode type was entered (case independent), + # expect to match the corresponding wavelength and preserve the correct case anode type + (["--anode-type", "Mo"], {"wavelength": 0.71073, "anode_type": "Mo"}), + (["--anode-type", "MoKa1"], {"wavelength": 0.70930, "anode_type": "MoKa1"}), + (["--anode-type", "MoKa1Ka2"], {"wavelength": 0.71073, "anode_type": "MoKa1Ka2"}), (["--anode-type", "Ag"], {"wavelength": 0.56087, "anode_type": "Ag"}), + (["--anode-type", "AgKa1"], {"wavelength": 0.55941, "anode_type": "AgKa1"}), + (["--anode-type", "AgKa1Ka2"], {"wavelength": 0.56087, "anode_type": "AgKa1Ka2"}), + (["--anode-type", "Cu"], {"wavelength": 1.54184, "anode_type": "Cu"}), + (["--anode-type", "CuKa1"], {"wavelength": 1.54056, "anode_type": "CuKa1"}), + (["--anode-type", "CuKa1Ka2"], {"wavelength": 1.54184, "anode_type": "CuKa1Ka2"}), + (["--anode-type", "moKa1Ka2"], {"wavelength": 0.71073, "anode_type": "MoKa1Ka2"}), + (["--anode-type", "ag"], {"wavelength": 0.56087, "anode_type": "Ag"}), + (["--anode-type", "cuka1"], {"wavelength": 1.54056, "anode_type": "CuKa1"}), + # C3: only a valid wavelength was entered, expect to include the wavelength only and anode type is None (["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}), + # C4: both valid anode type and wavelength were entered, + # expect to remove the anode type and preserve wavelength only (["--wavelength", "0.25", "--anode-type", "Ag"], {"wavelength": 0.25, "anode_type": None}), ], ) From 89a907a9979de99ae184fc01b430f6330538adcf Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Mon, 27 Jan 2025 21:14:23 -0500 Subject: [PATCH 4/4] fix: put WAVELENGTHS into a json file, simplified logic for set_wavelength function --- .../labpdfproc/data/wavelengths_config.json | 11 +++++++ src/diffpy/labpdfproc/tools.py | 31 +++++++------------ 2 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 src/diffpy/labpdfproc/data/wavelengths_config.json diff --git a/src/diffpy/labpdfproc/data/wavelengths_config.json b/src/diffpy/labpdfproc/data/wavelengths_config.json new file mode 100644 index 0000000..fb840e4 --- /dev/null +++ b/src/diffpy/labpdfproc/data/wavelengths_config.json @@ -0,0 +1,11 @@ +{ + "Mo": 0.71073, + "MoKa1": 0.70930, + "MoKa1Ka2": 0.71073, + "Ag": 0.56087, + "AgKa1": 0.55941, + "AgKa1Ka2": 0.56087, + "Cu": 1.54184, + "CuKa1": 1.54056, + "CuKa1Ka2": 1.54184 +} diff --git a/src/diffpy/labpdfproc/tools.py b/src/diffpy/labpdfproc/tools.py index 4b52148..b72b7f2 100644 --- a/src/diffpy/labpdfproc/tools.py +++ b/src/diffpy/labpdfproc/tools.py @@ -1,4 +1,5 @@ import copy +import json from pathlib import Path from diffpy.utils.diffraction_objects import ANGLEQUANTITIES, QQUANTITIES, XQUANTITIES @@ -7,17 +8,10 @@ # Reference values are taken from https://x-server.gmca.aps.anl.gov/cgi/www_dbli.exe?x0hdb=waves # Ka1Ka2 values are calculated as: (Ka1 * 2 + Ka2) / 3 # For CuKa1Ka2: (1.54056 * 2 + 1.544398) / 3 = 1.54184 -WAVELENGTHS = { - "Mo": 0.71073, - "MoKa1": 0.70930, - "MoKa1Ka2": 0.71073, - "Ag": 0.56087, - "AgKa1": 0.55941, - "AgKa1Ka2": 0.56087, - "Cu": 1.54184, - "CuKa1": 1.54056, - "CuKa1Ka2": 1.54184, -} +CWD = Path(__file__).parent.resolve() +WAVELENGTH_FILE_PATH = CWD / "data" / "wavelengths_config.json" +with open(WAVELENGTH_FILE_PATH, "r") as file: + WAVELENGTHS = json.load(file) known_sources = [key for key in WAVELENGTHS.keys()] # Exclude wavelength from metadata to prevent duplication, @@ -146,11 +140,7 @@ def set_wavelength(args): args : argparse.Namespace The updated arguments with the wavelength. """ - if args.wavelength is not None and args.wavelength <= 0: - raise ValueError( - "No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength." - ) - elif not args.wavelength and args.anode_type: + if args.wavelength is None: matched_anode_type = next((key for key in WAVELENGTHS if key.lower() == args.anode_type.lower()), None) if matched_anode_type is None: raise ValueError( @@ -158,10 +148,13 @@ def set_wavelength(args): ) args.anode_type = matched_anode_type args.wavelength = WAVELENGTHS[args.anode_type] - elif not args.wavelength: - args.wavelength = WAVELENGTHS["Mo"] else: - delattr(args, "anode_type") + if args.wavelength <= 0: + raise ValueError( + "No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength." + ) + else: + delattr(args, "anode_type") return args