Skip to content

compute mu #278

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 15 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/mu.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* function to compute x-ray attenuation coefficient (mu) using XrayDB

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
1 change: 1 addition & 0 deletions requirements/conda.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
numpy
xraydb
1 change: 1 addition & 0 deletions requirements/pip.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
numpy
xraydb
24 changes: 24 additions & 0 deletions src/diffpy/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from copy import copy
from pathlib import Path

from xraydb import material_mu


def _stringify(obj):
"""
Expand Down Expand Up @@ -131,3 +133,25 @@ def get_package_info(package_names, metadata=None):
pkg_info.update({package: importlib.metadata.version(package)})
metadata["package_info"] = pkg_info
return metadata


def compute_mu_using_xraydb(sample, energy, density=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

let's change sample to composition or maybe sample_composition.

I think we probably need packing_fraction as an optional arg.

"""Compute mu using the XrayDB database.

Reference: https://xraypy.github.io/XrayDB/python.html#xraydb.material_mu

Parameters
----------
sample str
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 we would normally do tihs as:

    sample: str
       The chemical formula or the name of the material

i.e., colon and sentencecse

the chemical formula or the name of the material
energy float
the energy in eV
density float or None
Copy link
Contributor

Choose a reason for hiding this comment

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

density: float, optional, Default is None

material density in gr/cm^3
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be "sample mass density" or even better, "mass density of the packed powder/sample" to be clear. the material desnsity is something quite different (mass of atoms in the unit cell divided by unit cell volume)


Returns
-------
the attenuation coefficient mu in mm^{-1}
"""
mu = material_mu(sample, energy, density=density, kind="total") / 10
return mu
8 changes: 7 additions & 1 deletion tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from diffpy.utils.tools import get_package_info, get_user_info
from diffpy.utils.tools import compute_mu_using_xraydb, get_package_info, get_user_info

# def _setup_dirs(monkeypatch, user_filesystem):
# home_dir, cwd_dir = user_filesystem.home_dir, user_filesystem.cwd_dir
Expand Down Expand Up @@ -189,3 +189,9 @@ def test_get_package_info(monkeypatch, inputs, expected):
)
actual_metadata = get_package_info(inputs[0], metadata=inputs[1])
assert actual_metadata == expected


def test_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.

we need a test for no density passed in. Also for packing fraction passed in and also for both passed in. Think about what behavior you want in each of these cases and write a "case" (look elsewhere in test_tools for how we are handling this now)

sample, energy, density = "ZrO2", 17445.362740959618, 1.009
actual_mu = compute_mu_using_xraydb(sample, energy, density=density)
assert actual_mu == pytest.approx(1.252, rel=1e-4, abs=0.1)
Loading