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 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
29 changes: 19 additions & 10 deletions src/diffpy/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ def get_package_info(package_names, metadata=None):
return metadata


def get_density_from_cloud(sample_composition, mp_token=""):
"""Function to get material density from the MP database.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

add new function - not implemented

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to also go to the COD database (doesn't require a token).


It is not implemented yet.
"""
raise NotImplementedError


def compute_mu_using_xraydb(sample_composition, energy, sample_mass_density=None, packing_fraction=None):
"""Compute the attenuation coefficient (mu) using the XrayDB database.

Expand Down Expand Up @@ -242,14 +250,15 @@ def compute_mu_using_xraydb(sample_composition, energy, sample_mass_density=None
"You must specify either sample_mass_density or packing_fraction, but not both. "
"Please rerun specifying only one."
)
if sample_mass_density is not None:
mu = material_mu(sample_composition, energy * 1000, density=sample_mass_density, kind="total") / 10
else:
warnings.warn(
"Warning: Density is set to None if a packing fraction is specified, "
"which may cause errors for some materials. "
"We recommend specifying sample mass density for now. "
"Auto-density calculation is coming soon."
)
mu = material_mu(sample_composition, energy * 1000, density=None, kind="total") * packing_fraction / 10
if packing_fraction is None:
packing_fraction = 1
try:
sample_mass_density = get_density_from_cloud(sample_composition) * packing_fraction
except NotImplementedError:
warnings.warn(
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 needs to be an error not a warning, no? We need to interrupt execution.

Also, let's apologise... "So sorry, Density computation from composition is not implemented right now. We hope to have this implemented in the next release. Please rerun specifying a sample mass density.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! I will add the COD database and change the error message

"Density computation is not implemented right now. "
"Please rerun specifying a sample mass density."
)
energy_eV = energy * 1000
mu = material_mu(sample_composition, energy_eV, density=sample_mass_density, kind="total") / 10
return mu
9 changes: 0 additions & 9 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,6 @@ def test_get_package_info(monkeypatch, inputs, expected):
},
1.2522,
),
( # C2: Composition, energy, and packing fraction provided, expect to get mu based on packing fraction
# Reuse pattern from C1.1 here
{
"sample_composition": "quartz",
"energy": 10,
"packing_fraction": 0.5,
},
2.5184,
),
],
)
def test_compute_mu_using_xraydb(inputs, expected_mu):
Expand Down
Loading