Skip to content

feat: options for estimating muD theoretically #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def _define_arguments():
"The filename(s) or folder(s) of the datafile(s) to load. "
"Required.\n"
"Supply a space-separated list of files or directories. "
"If a filename contains whitespace, enclose it in quotes. "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe mention that, as a general matter, avoid filenames with whietspaces in, but if it does, then enclose in quotes

"Long lists can be supplied, one per line, "
"in a file with name file_list.txt. "
"If one or more directory is provided, all valid "
"data-files in that directory will be processed. "
"Examples of valid inputs are 'file.xy', 'data/file.xy', "
"'file.xy, data/file.xy', "
"'file.xy data/file.xy', "
"'.' (load everything in the current directory), "
"'data' (load everything in the folder ./data), "
"'data/file_list.txt' (load the list of files "
Expand Down Expand Up @@ -177,29 +178,27 @@ def _add_mud_selection_group(p, is_gui=False):
**({"widget": "FileChooser"} if is_gui else {}),
)
g.add_argument(
"-td",
"-d",
"--theoretical-from-density",
help=(
"Estimate mu*D theoretically using sample mass density. "
"Specify the sample composition (chemical formula), "
"incident x-ray energy in keV, "
"and sample mass density in g/cm^3 "
"in that exact order "
"and separated by commas with no whitespaces "
"(e.g., 'ZrO2,2,1.2')."
"Specify the chemical formula, incident x-ray energy (in keV), "
"and sample mass density (in g/cm^3), in that exact order, "
"separated by commas (e.g., ZrO2,20,1.5). "
"If you add whitespaces, "
"enclose it in quotes (e.g., 'ZrO2, 20, 1.5'). "
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow whitespace - edits in help message

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the parser do if there is whitespace but no quotes. Did you test that?

),
)
g.add_argument(
"-tp",
"-p",
"--theoretical-from-packing",
help=(
"Estimate mu*D theoretically using packing fraction. "
"Specify the sample composition (chemical formula), "
"incident x-ray energy in keV, "
"and packing fraction (0 to 1) "
"in that exact order "
"and separated by commas with no whitespaces "
"(e.g., 'ZrO2,2,0.5')."
"Specify the chemical formula, incident x-ray energy (in keV), "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this seems to be used 2x, define it in a variable and call the variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbillinge ready for another review. I edited the help messages according to the comments above. When there's whitespace but no quotes the CLI will raise an error saying that it doesn't recognize the command

"and packing fraction (0 to 1), in that exact order, "
"separated by commas (e.g., ZrO2,20,0.5). "
"If you add whitespaces, "
"enclose it in quotes (e.g., 'ZrO2, 20, 0.5'). "
),
)
return p
Expand Down
4 changes: 2 additions & 2 deletions src/diffpy/labpdfproc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,13 @@ def _set_mud_from_zscan(args):

def _parse_theoretical_input(input_str):
"""Helper function to parse and validate the input string."""
parts = input_str.split(",")
parts = [part.strip() for part in input_str.split(",")]
if len(parts) != 3:
raise ValueError(
f"Invalid mu*D input '{input_str}'. "
"Expected format is 'sample composition, energy, "
"sample mass density or packing fraction' "
"with no whitespaces (e.g., 'ZrO2,2,0.8').",
"(e.g., 'ZrO2,20,0.8').",
)
sample_composition = parts[0]
energy = float(parts[1])
Expand Down
27 changes: 17 additions & 10 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,14 @@ def test_set_xtype_bad():
# C2: user provides a z-scan file, expect to estimate through the file
(["--z-scan-file", "test_dir/testfile.xy"], 3),
# C3: user specifies sample composition, energy,
# and sample mass density, expect to estimate theoretically
# and sample mass density,
# both with and without whitespaces, expect to estimate theoretically
(["--theoretical-from-density", "ZrO2,17.45,1.2"], 1.49),
(["--theoretical-from-density", "ZrO2, 17.45, 1.2"], 1.49),
# C4: user specifies sample composition, energy, and packing fraction
# expect to estimate theoretically
# both with and without whitespaces, expect to estimate theoretically
# (["--theoretical-from-packing", "ZrO2,17.45,0.3"], 1.49),
# (["--theoretical-from-packing", "ZrO2, 17.45, 0.3"], 1.49),
],
)
def test_set_mud(user_filesystem, inputs, expected_mud):
Expand All @@ -489,7 +492,8 @@ def test_set_mud(user_filesystem, inputs, expected_mud):
"Cannot find invalid file. Please specify a valid file path.",
],
),
# C2.1: user provides fewer than three input values
# C2.1: (sample mass density option)
# user provides fewer than three input values
# expect ValueError with a message indicating the correct format
(
["--theoretical-from-density", "ZrO2,0.5"],
Expand All @@ -498,10 +502,11 @@ def test_set_mud(user_filesystem, inputs, expected_mud):
"Invalid mu*D input 'ZrO2,0.5'. "
"Expected format is 'sample composition, energy, "
"sample mass density or packing fraction' "
"with no whitespaces (e.g., 'ZrO2,2,0.8').",
"(e.g., 'ZrO2,20,0.8').",
],
),
# C2.1: user provides fewer than three input values
# C2.2: (packing fraction option)
# user provides fewer than three input values
# expect ValueError with a message indicating the correct format
(
["--theoretical-from-packing", "ZrO2,0.5"],
Expand All @@ -510,10 +515,11 @@ def test_set_mud(user_filesystem, inputs, expected_mud):
"Invalid mu*D input 'ZrO2,0.5'. "
"Expected format is 'sample composition, energy, "
"sample mass density or packing fraction' "
"with no whitespaces (e.g., 'ZrO2,2,0.8').",
"(e.g., 'ZrO2,20,0.8').",
],
),
# C3.1: user provides more than 3 input values
# C3.1: (sample mass density option)
# user provides more than 3 input values
# expect ValueError with a message indicating the correct format
(
["--theoretical-from-density", "ZrO2,1.5,1.5,0.5"],
Expand All @@ -522,10 +528,11 @@ def test_set_mud(user_filesystem, inputs, expected_mud):
"Invalid mu*D input 'ZrO2,1.5,1.5,0.5'. "
"Expected format is 'sample composition, energy, "
"sample mass density or packing fraction' "
"with no whitespaces (e.g., 'ZrO2,2,0.8').",
"(e.g., 'ZrO2,20,0.8').",
],
),
# C3.2: user provides more than 3 input values
# C3.2: (packing fraction option)
# user provides more than 3 input values
# expect ValueError with a message indicating the correct format
(
["--theoretical-from-packing", "ZrO2,1.5,1.5,0.5"],
Expand All @@ -534,7 +541,7 @@ def test_set_mud(user_filesystem, inputs, expected_mud):
"Invalid mu*D input 'ZrO2,1.5,1.5,0.5'. "
"Expected format is 'sample composition, energy, "
"sample mass density or packing fraction' "
"with no whitespaces (e.g., 'ZrO2,2,0.8').",
"(e.g., 'ZrO2,20,0.8').",
],
),
],
Expand Down
Loading