Skip to content

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

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 24 additions & 0 deletions news/getmud.rst
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>
39 changes: 39 additions & 0 deletions src/diffpy/labpdfproc/getmud.py
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(
Copy link
Contributor

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

def get_diameter(mud, mu)
    return mud/mu

with docstrings etc. of course.

Copy link
Collaborator Author

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)?

Copy link
Contributor

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

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 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).

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
47 changes: 47 additions & 0 deletions src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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():
Expand Down Expand Up @@ -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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The current logic is that if the user enters get-diameter, it will load the compute_diameter function from getmud module. I'm not sure if printing the result is the best approach here. The CLI will look something like labpdfproc get-diameter 2 ZrO2 17.45 1.6.

return

args = preprocessing_args(args)

for filepath in args.input_paths:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_getmud.py
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
)
Loading