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

Closed
wants to merge 2 commits into from
Closed
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 target muD and related chemical info.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
75 changes: 75 additions & 0 deletions src/diffpy/labpdfproc/getmud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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
muD, capillary diameter, sample composition, and energy.

Parameters
----------
mud : float
The given product of attenuation coefficient mu
Copy link
Contributor

Choose a reason for hiding this comment

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

"The given muD of the sample"

The rest you wrote just makes it more confusing I thank.

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would give a few example to illustreate what form it must be in to be valid. I think we will use that help text in multiple places, so maybe define it in a variable and reuse? We may have already written it somewere in a docstring or the docs, so just copy paste from there if possible....

energy : float
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe call this xray_energy?

The energy of the incident x-rays in keV.

Returns
-------
estimated_density : float
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the density is "estimated", it is calculated, given the other parameters

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
muD, sample composition, energy, and mass density/packing fraction.

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": 17.45, # Mo K_alpha source
},
1.0751,
),
],
)
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": 17.45,
"sample_mass_density": 1.20,
},
1.3439,
),
# ( # C2: user specifies a packing fraction
# {
# "mud": 2.0,
# "sample_composition": "ZrO2",
# "energy": 17.45,
# "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