Skip to content

feat: initial commit on getmud module #175

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions news/getmud.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Functionalities to estimate sample mass density or capillary diameter from muD and related chemical info.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
74 changes: 74 additions & 0 deletions src/diffpy/labpdfproc/getmud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import numpy as np
from scipy.optimize import newton

from diffpy.utils.tools import compute_mu_using_xraydb


def estimate_mass_density(mud, diameter, sample_composition, energy):
"""Estimate sample mass density (g/cm^3) from mu*D
using capillary diameter, sample composition, and energy.

Parameters
----------
mud : float
The given product of attenuation coefficient mu
in mm^{-1} and capillary diameter in mm.
diameter : float
The given diameter of the sample capillary in mm.
sample_composition : str
The chemical formula of the material.
energy : float
The energy of the incident x-rays in keV.

Returns
-------
estimated_density : float
The estimated mass density of the packed powder/sample in g/cm^3.
"""
mu = mud / diameter

def residual_density(density):
return np.abs(
compute_mu_using_xraydb(
sample_composition, energy, sample_mass_density=density
)
- mu
)

estimated_density = newton(residual_density, x0=1.0, x1=10.0)
return estimated_density


def estimate_diameter(
mud,
sample_composition,
energy,
sample_mass_density=None,
packing_fraction=None,
):
"""Estimate capillary diameter (mm) from mu*D and mu.

Parameters
----------
mud : float
The given product of attenuation coefficient mu
in mm^{-1} and capillary diameter in mm.
sample_composition : str
Copy link
Contributor

Choose a reason for hiding this comment

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

These are not needed presumably. If I know mud and I know mu then nothing else matters.

The chemical formula of the material.
energy : float
The energy of the incident x-rays in keV.
sample_mass_density : float
The mass density of the packed powder/sample in g/cm^3.
packing_fraction : float, optional, Default is None
The fraction of sample in the capillary (between 0 and 1).
Specify either sample_mass_density or packing_fraction but not both.

Returns
-------
diameter : float
The given diameter of the sample capillary in mm.
"""
mu = compute_mu_using_xraydb(
Copy link
Contributor

Choose a reason for hiding this comment

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

comment out and replace with pass. Let's just work on tests to begin with, until we learn what we want the function to do.

sample_composition, energy, sample_mass_density, packing_fraction
)
return mud / mu
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't mud/ mu = d?

59 changes: 59 additions & 0 deletions tests/test_getmud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest

from diffpy.labpdfproc.getmud import estimate_diameter, estimate_mass_density


@pytest.mark.parametrize(
"inputs, expected_mass_density",
[
(
{
"mud": 2.0,
"diameter": 1.5,
"sample_composition": "ZrO2",
"energy": 20,
Copy link
Contributor

Choose a reason for hiding this comment

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

it is probably a better test if we use reasonable energies. These would be Cu Kalpha, Mo Kalpha, Ag Kalpha. They don't have to be exact but ballpark,

},
0.25,
),
],
)
def test_estimate_mass_density(inputs, expected_mass_density):
actual_mass_density = estimate_mass_density(
inputs["mud"],
inputs["diameter"],
inputs["sample_composition"],
inputs["energy"],
)
assert actual_mass_density == pytest.approx(
expected_mass_density, rel=0.01, abs=0.1
)


@pytest.mark.parametrize(
"inputs, expected_diameter",
[
( # C1: user specifies a sample mass density
{
"mud": 2.0,
"sample_composition": "ZrO2",
"energy": 20,
"sample_mass_density": 0.25,
},
1.5,
),
# ( # C2: user specifies a packing fraction
# {
# "mud": 2.0,
# "sample_composition": "ZrO2",
# "energy": 20,
# "packing_fraction": 0.3
# },
# 1.5
# ),
],
)
def test_estimate_diameter(inputs, expected_diameter):
actual_diameter = estimate_diameter(**inputs)
assert actual_diameter == pytest.approx(
expected_diameter, rel=0.01, abs=0.1
)
Loading