Skip to content
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

added ValueError for unknown densities #2775

Merged
merged 3 commits into from Aug 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions hyperspy/misc/material.py
Expand Up @@ -218,19 +218,23 @@ def _density_of_mixture(weight_percent,
[elements_db[element]['Physical_properties']['density (g/cm^3)']
for element in elements])
sum_densities = np.zeros_like(weight_percent, dtype='float')
if mean == 'harmonic':
for i, weight in enumerate(weight_percent):
sum_densities[i] = weight / densities[i]
sum_densities = sum_densities.sum(axis=0)
density = np.sum(weight_percent, axis=0) / sum_densities
return np.where(sum_densities == 0.0, 0.0, density)
elif mean == 'weighted':
for i, weight in enumerate(weight_percent):
sum_densities[i] = weight * densities[i]
sum_densities = sum_densities.sum(axis=0)
sum_weight = np.sum(weight_percent, axis=0)
density = sum_densities / sum_weight
return np.where(sum_weight == 0.0, 0.0, density)
try :
if mean == 'harmonic':
for i, weight in enumerate(weight_percent):
sum_densities[i] = weight / densities[i]
sum_densities = sum_densities.sum(axis=0)
density = np.sum(weight_percent, axis=0) / sum_densities
return np.where(sum_densities == 0.0, 0.0, density)
elif mean == 'weighted':
for i, weight in enumerate(weight_percent):
sum_densities[i] = weight * densities[i]
sum_densities = sum_densities.sum(axis=0)
sum_weight = np.sum(weight_percent, axis=0)
density = sum_densities / sum_weight
return np.where(sum_weight == 0.0, 0.0, density)
except TypeError :
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

A test is needed to cover this case, you can simply add your example from #2775 in

def test_density_of_mixture():
.

'The density of one of the elements is unknown (Probably At or Fr).')


def density_of_mixture(weight_percent,
Expand Down