-
Notifications
You must be signed in to change notification settings - Fork 11
feat: getmud UC1 (get_diameter) #184
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
**Added:** | ||
|
||
* New ``getmuD`` module with the ``compute_diameter`` function to calculate capillary diameter. | ||
* CLI now supports ``compute_diameter`` function for target muD via a subcommand. | ||
|
||
**Changed:** | ||
|
||
* <news item> | ||
|
||
**Deprecated:** | ||
|
||
* <news item> | ||
|
||
**Removed:** | ||
|
||
* <news item> | ||
|
||
**Fixed:** | ||
|
||
* <news item> | ||
|
||
**Security:** | ||
|
||
* <news item> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from diffpy.utils.tools import compute_mu_using_xraydb | ||
|
||
|
||
def compute_diameter( | ||
mud, | ||
sample_composition, | ||
xray_energy, | ||
sample_mass_density=None, | ||
packing_fraction=None, | ||
): | ||
""" | ||
Compute capillary diameter (mm) from muD, sample composition, energy, | ||
and either sample mass density or packing fraction. | ||
|
||
Parameters | ||
---------- | ||
mud : float | ||
The given muD of the sample. | ||
sample_composition : str | ||
The chemical formula of the material (e.g. "ZrO2"). | ||
xray_energy : float | ||
The energy of the incident x-rays in keV. | ||
sample_mass_density : float, optional | ||
The mass density of the packed sample in g/cm^3. | ||
packing_fraction : float, optional | ||
The packing fraction of the sample (0–1). | ||
|
||
Returns | ||
------- | ||
diameter : float | ||
Computed capillary diameter in mm. | ||
""" | ||
mu = compute_mu_using_xraydb( | ||
sample_composition, | ||
xray_energy, | ||
sample_mass_density, | ||
packing_fraction, | ||
) | ||
return mud / mu |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from gooey import Gooey, GooeyParser | ||
|
||
from diffpy.labpdfproc.functions import CVE_METHODS, apply_corr, compute_cve | ||
from diffpy.labpdfproc.getmud import compute_diameter | ||
from diffpy.labpdfproc.tools import ( | ||
known_sources, | ||
load_metadata, | ||
|
@@ -207,7 +208,43 @@ def _add_mud_selection_group(p, is_gui=False): | |
return p | ||
|
||
|
||
def _add_subparsers(p): | ||
"""Add subcommands for getmud module.""" | ||
subparsers = p.add_subparsers(dest="subcommand") | ||
get_d_parser = subparsers.add_parser( | ||
"get-diameter", help="Compute capillary diameter." | ||
) | ||
get_d_parser.add_argument( | ||
"mud", type=float, help="The target muD of the sample." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will write a better help message with an example here |
||
) | ||
get_d_parser.add_argument( | ||
"composition", | ||
help="The chemical formula of the material (e.g., ZrO2).", | ||
) | ||
get_d_parser.add_argument( | ||
"energy", type=float, help="The X-ray energy in keV." | ||
) | ||
get_d_parser.add_argument( | ||
"mass_density", | ||
type=float, | ||
help="The sample mass density in g/cm^3.", | ||
default=None, | ||
) | ||
get_d_parser.set_defaults(func=compute_diameter) | ||
return p | ||
|
||
|
||
def get_args(override_cli_inputs=None): | ||
"""Parse CLI arguments for main processing or subcommand mode.""" | ||
# User enters one of the subcommands | ||
cli_args = override_cli_inputs or sys.argv[1:] | ||
p = ArgumentParser() | ||
p = _add_subparsers(p) | ||
subcommands = ["get-diameter"] | ||
if cli_args and cli_args[0] in subcommands: | ||
return p.parse_args(override_cli_inputs) | ||
|
||
# User does not enter subcommands | ||
p = ArgumentParser() | ||
p = _add_mud_selection_group(p, is_gui=False) | ||
for arg in _define_arguments(): | ||
|
@@ -238,6 +275,16 @@ def main(): | |
if len(sys.argv) == 1 or "--gui" in sys.argv | ||
else get_args() | ||
) | ||
if getattr(args, "subcommand", None) == "get-diameter": | ||
diameter = args.func( | ||
mud=args.mud, | ||
sample_composition=args.composition, | ||
xray_energy=args.energy, | ||
sample_mass_density=args.mass_density, | ||
) | ||
print(f"Capillary Diameter: {diameter:.2f} mm") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic is that if the user enters |
||
return | ||
|
||
args = preprocessing_args(args) | ||
|
||
for filepath in args.input_paths: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
|
||
from diffpy.labpdfproc.getmud import compute_diameter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"inputs, expected_diameter", | ||
[ | ||
( | ||
{ | ||
"mud": 2.0, | ||
"sample_composition": "ZrO2", | ||
"xray_energy": 17.45, | ||
"sample_mass_density": 1.20, | ||
}, | ||
1.3439, | ||
), | ||
], | ||
) | ||
def test_compute_diameter(inputs, expected_diameter): | ||
actual_diameter = compute_diameter(**inputs) | ||
assert actual_diameter == pytest.approx( | ||
expected_diameter, rel=0.01, abs=0.1 | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function should just be
with docstrings etc. of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think previously we discussed that user enters chemical info related to mu, but not mu itself (See #175 (review))? Maybe a better approach here is to write
return mud/compute_mu(composition, energy, density)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function that computes mu Is much more reusable. It should probably be in didn't. Utils though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sbillinge Sounds good, I edited the function and it's ready for another review. I removed the CLI here and created a new issue for it (#186).