Skip to content

Commit 36f0cf8

Browse files
committedJan 31, 2025·
feat: raise error when both wavelength and anode type are specified
1 parent 95fee4d commit 36f0cf8

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed
 

Diff for: ‎src/diffpy/labpdfproc/labpdfprocapp.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ def define_arguments():
3030
"in a file with name file_list.txt. "
3131
"If one or more directory is provided, all valid "
3232
"data-files in that directory will be processed. "
33-
"Examples of valid "
34-
"inputs are 'file.xy', 'data/file.xy', "
33+
"Examples of valid inputs are 'file.xy', 'data/file.xy', "
3534
"'file.xy, data/file.xy', "
3635
"'.' (load everything in the current directory), "
3736
"'data' (load everything in the folder ./data), "
@@ -48,18 +47,16 @@ def define_arguments():
4847
"name": ["-a", "--anode-type"],
4948
"help": (
5049
f"The type of the x-ray source. "
51-
f"Allowed values are {*[known_sources], }. "
50+
f"Allowed values are {*known_sources, }. "
5251
f"Either specify a known x-ray source or specify wavelength."
5352
),
54-
"default": "Mo",
53+
"default": None,
5554
},
5655
{
5756
"name": ["-w", "--wavelength"],
5857
"help": (
5958
"X-ray source wavelength in angstroms. "
6059
"Not needed if the anode-type is specified. "
61-
"This wavelength will override the anode wavelength "
62-
"if both are specified."
6360
),
6461
"type": float,
6562
},

Diff for: ‎src/diffpy/labpdfproc/tools.py

+30-16
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def set_input_lists(args):
160160

161161

162162
def set_wavelength(args):
163-
"""Set the wavelength based on the given anode_type.
164-
If a wavelength is provided,
165-
it will be used, and the anode_type argument will be removed.
163+
"""Set the wavelength based on the given anode_type or wavelength.
164+
First checks from args. If neither is provided,
165+
it attempts to load from local and then global config file.
166166
167167
Parameters
168168
----------
@@ -172,15 +172,31 @@ def set_wavelength(args):
172172
Raises
173173
------
174174
ValueError
175-
Raised when input wavelength is non-positive
176-
or if input anode_type is not one of the known sources.
175+
Raised if:
176+
(1) neither is provided, and either mu*D needs to be looked up or
177+
xtype is not the two-theta grid,
178+
(2) both are provided,
179+
(3) anode_type is not one of the known sources,
180+
(4) wavelength is non-positive.
177181
178182
Returns
179183
-------
180184
args : argparse.Namespace
181185
The updated arguments with the wavelength.
182186
"""
183-
if args.wavelength is None:
187+
# first load values from config file
188+
if args.wavelength is None and args.anode_type is None:
189+
if not (args.mud is not None and args.xtype in ANGLEQUANTITIES):
190+
raise ValueError(
191+
f"Please provide a wavelength or anode type. "
192+
f"Allowed anode types are {*known_sources, }."
193+
)
194+
elif args.wavelength is not None and args.anode_type is not None:
195+
raise ValueError(
196+
f"Please provide either a wavelength or an anode type, not both. "
197+
f"Allowed anode types are {*known_sources, }."
198+
)
199+
elif args.anode_type is not None:
184200
matched_anode_type = next(
185201
(
186202
key
@@ -197,15 +213,12 @@ def set_wavelength(args):
197213
)
198214
args.anode_type = matched_anode_type
199215
args.wavelength = WAVELENGTHS[args.anode_type]
200-
else:
201-
if args.wavelength <= 0:
202-
raise ValueError(
203-
"No valid wavelength. "
204-
"Please rerun specifying a known anode_type "
205-
"or a positive wavelength."
206-
)
207-
else:
208-
delattr(args, "anode_type")
216+
elif args.wavelength is not None and args.wavelength <= 0:
217+
raise ValueError(
218+
"No valid wavelength. "
219+
"Please rerun specifying a known anode_type "
220+
"or a positive wavelength."
221+
)
209222
return args
210223

211224

@@ -362,7 +375,8 @@ def load_package_info(args):
362375
def preprocessing_args(args):
363376
"""Perform preprocessing on the provided args.
364377
The process includes loading package and user information,
365-
setting input, output, wavelength, xtype, mu*D, and loading user metadata.
378+
setting input, output, wavelength, anode type, xtype, mu*D,
379+
and loading user metadata.
366380
367381
Parameters
368382
----------

Diff for: ‎tests/test_tools.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ def test_set_output_directory_bad(user_filesystem):
198198
@pytest.mark.parametrize(
199199
"inputs, expected",
200200
[
201-
# C1: nothing passed in, expect default is Mo
202-
([], {"wavelength": 0.71073, "anode_type": "Mo"}),
203-
# C2: only a valid anode type was entered (case independent),
201+
# C1: only a valid anode type was entered (case independent),
204202
# expect to match the corresponding wavelength
205203
# and preserve the correct case anode type
206204
(["--anode-type", "Mo"], {"wavelength": 0.71073, "anode_type": "Mo"}),
@@ -239,45 +237,51 @@ def test_set_output_directory_bad(user_filesystem):
239237
["--anode-type", "cuka1"],
240238
{"wavelength": 1.54056, "anode_type": "CuKa1"},
241239
),
242-
# C3: only a valid wavelength was entered,
240+
# C2: a valid wavelength was entered,
243241
# expect to include the wavelength only and anode type is None
244242
(["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}),
245-
# C4: both valid anode type and wavelength were entered,
246-
# expect to remove the anode type and preserve wavelength only
247-
(
248-
["--wavelength", "0.25", "--anode-type", "Ag"],
249-
{"wavelength": 0.25, "anode_type": None},
250-
),
243+
# C3: nothing passed in, but mu*D was provided and xtype is on tth
244+
# expect wavelength and anode type to be None
245+
# and program proceeds without error
246+
([], {"wavelength": None, "anode_type": None}),
251247
],
252248
)
253249
def test_set_wavelength(inputs, expected):
254250
cli_inputs = ["2.5", "data.xy"] + inputs
255251
actual_args = get_args(cli_inputs)
256252
actual_args = set_wavelength(actual_args)
257253
assert actual_args.wavelength == expected["wavelength"]
258-
assert getattr(actual_args, "anode_type", None) == expected["anode_type"]
254+
assert actual_args.anode_type == expected["anode_type"]
259255

260256

261257
@pytest.mark.parametrize(
262258
"inputs, expected_error_msg",
263259
[
264-
(
260+
( # C1.1: nothing passed in, xtype is not on tth
261+
# expect error asking for either wavelength or anode type
262+
["--xtype", "q"],
263+
f"Please provide a wavelength or anode type. "
264+
f"Allowed anode types are {*known_sources, }.",
265+
),
266+
# C1.2: nothing passed in, need to look up mu*D
267+
# expect error asking for either wavelength or anode type
268+
( # C2: both wavelength and anode type were specified
269+
# expect error asking not to specify both
270+
["--wavelength", "0.7", "--anode-type", "Mo"],
271+
f"Please provide either a wavelength or an anode type, not both. "
272+
f"Allowed anode types are {*known_sources, }.",
273+
),
274+
( # C3: invalid anode type
265275
["--anode-type", "invalid"],
266276
f"Anode type not recognized. "
267277
f"Please rerun specifying an anode_type from {*known_sources, }.",
268278
),
269-
(
279+
( # C4: invalid wavelength
270280
["--wavelength", "0"],
271281
"No valid wavelength. "
272282
"Please rerun specifying a known anode_type "
273283
"or a positive wavelength.",
274284
),
275-
(
276-
["--wavelength", "-1", "--anode-type", "Mo"],
277-
"No valid wavelength. "
278-
"Please rerun specifying a known anode_type "
279-
"or a positive wavelength.",
280-
),
281285
],
282286
)
283287
def test_set_wavelength_bad(inputs, expected_error_msg):
@@ -515,6 +519,8 @@ def test_load_metadata(mocker, user_filesystem):
515519
cli_inputs = [
516520
"2.5",
517521
".",
522+
"--anode-type",
523+
"Mo",
518524
"--user-metadata",
519525
"key=value",
520526
"--username",

0 commit comments

Comments
 (0)
Please sign in to comment.