-
Notifications
You must be signed in to change notification settings - Fork 876
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
bugfix: chargemol runtime directory and os.path.exists
fix
#3778
Open
chiang-yuan
wants to merge
14
commits into
materialsproject:master
Choose a base branch
from
chiang-yuan:fix-chargemol-path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fb3dce4
bugfix: allow custom chgcar path
chiang-yuan 637501a
atomic_densities is a direcotry, not file
chiang-yuan ba0f1a9
refactor TestChargemolAnalysis
janosh 7a29026
prefer os.path over pathlib
janosh 76324b3
fix default atomic_densities_path (set to None, so if == "" doesn't a…
janosh 1832070
fix possibly unbound var in get_charge_transfer
janosh a05635e
refactor _from_data_dir
janosh 01f0283
test_missing_exe_error
janosh 3f3a438
remove response
chiang-yuan e4ccad6
Merge branch 'master' into pr/chiang-yuan/3778
janosh 8be153a
Merge branch 'master' into fix-chargemol-path
janosh 2d9ffbc
Merge branch 'master' of https://github.com/materialsproject/pymatgen…
chiang-yuan 43b2385
add missing `command_line`
chiang-yuan 6973b7a
try to use chargemol precompiled binary
chiang-yuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,74 @@ | ||
from __future__ import annotations | ||
|
||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from pymatgen.command_line.chargemol_caller import ChargemolAnalysis | ||
from pymatgen.core import Element | ||
from pymatgen.core import Element, Structure | ||
from pymatgen.util.testing import TEST_FILES_DIR | ||
|
||
|
||
class TestChargemolAnalysis: | ||
def test_parse_chargemol(self): | ||
test_dir = f"{TEST_FILES_DIR}/chargemol/spin_unpolarized" | ||
ca = ChargemolAnalysis(path=test_dir, run_chargemol=False) | ||
assert ca.ddec_charges == [0.8432, -0.8432] | ||
assert ca.get_partial_charge(0) == 0.8432 | ||
assert ca.get_partial_charge(0, charge_type="cm5") == 0.420172 | ||
assert ca.get_charge_transfer(0) == -0.8432 | ||
assert ca.get_charge_transfer(0, charge_type="cm5") == -0.420172 | ||
assert ca.get_charge(0, nelect=1) == 1 - 0.8432 | ||
assert ca.get_charge(0, nelect=1, charge_type="cm5") == 1 - 0.420172 | ||
assert ca.dipoles == [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] | ||
assert ca.ddec_spin_moments is None | ||
assert ca.bond_order_sums == [0.53992, 0.901058] | ||
assert ca.ddec_rsquared_moments == [8.261378, 34.237274] | ||
assert ca.ddec_rcubed_moments == [14.496002, 88.169236] | ||
assert ca.ddec_rfourth_moments == [37.648248, 277.371929] | ||
assert ca.cm5_charges == [0.420172, -0.420172] | ||
assert ca.summary["ddec"]["partial_charges"] == ca.ddec_charges | ||
assert ca.summary["ddec"]["dipoles"] == ca.dipoles | ||
assert ca.summary["ddec"]["bond_order_sums"] == ca.bond_order_sums | ||
assert ca.summary["ddec"]["rsquared_moments"] == ca.ddec_rsquared_moments | ||
assert ca.summary["ddec"]["rcubed_moments"] == ca.ddec_rcubed_moments | ||
assert ca.summary["ddec"]["rfourth_moments"] == ca.ddec_rfourth_moments | ||
assert ca.summary["cm5"]["partial_charges"] == ca.cm5_charges | ||
assert ca.summary["ddec"]["bond_order_dict"] == ca.bond_order_dict | ||
assert ca.summary["ddec"].get("spin_moments") is None | ||
assert ca.natoms == [1, 1] | ||
assert ca.structure is not None | ||
assert len(ca.bond_order_dict) == 2 | ||
assert ca.bond_order_dict[0]["bonded_to"][0] == { | ||
chg_mol = ChargemolAnalysis(path=test_dir, run_chargemol=False) | ||
assert chg_mol.ddec_charges == [0.8432, -0.8432] | ||
assert chg_mol.get_partial_charge(0) == 0.8432 | ||
assert chg_mol.get_partial_charge(0, charge_type="cm5") == 0.420172 | ||
assert chg_mol.get_charge_transfer(0) == -0.8432 | ||
assert chg_mol.get_charge_transfer(0, charge_type="cm5") == -0.420172 | ||
assert chg_mol.get_charge(0, nelect=1) == 1 - 0.8432 | ||
assert chg_mol.get_charge(0, nelect=1, charge_type="cm5") == 1 - 0.420172 | ||
assert chg_mol.dipoles == [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] | ||
assert chg_mol.ddec_spin_moments is None | ||
assert chg_mol.bond_order_sums == [0.53992, 0.901058] | ||
assert chg_mol.ddec_rsquared_moments == [8.261378, 34.237274] | ||
assert chg_mol.ddec_rcubed_moments == [14.496002, 88.169236] | ||
assert chg_mol.ddec_rfourth_moments == [37.648248, 277.371929] | ||
assert chg_mol.cm5_charges == [0.420172, -0.420172] | ||
assert chg_mol.summary["ddec"]["partial_charges"] == chg_mol.ddec_charges | ||
assert chg_mol.summary["ddec"]["dipoles"] == chg_mol.dipoles | ||
assert chg_mol.summary["ddec"]["bond_order_sums"] == chg_mol.bond_order_sums | ||
assert chg_mol.summary["ddec"]["rsquared_moments"] == chg_mol.ddec_rsquared_moments | ||
assert chg_mol.summary["ddec"]["rcubed_moments"] == chg_mol.ddec_rcubed_moments | ||
assert chg_mol.summary["ddec"]["rfourth_moments"] == chg_mol.ddec_rfourth_moments | ||
assert chg_mol.summary["cm5"]["partial_charges"] == chg_mol.cm5_charges | ||
assert chg_mol.summary["ddec"]["bond_order_dict"] == chg_mol.bond_order_dict | ||
assert chg_mol.summary["ddec"].get("spin_moments") is None | ||
assert chg_mol.natoms == [1, 1] | ||
assert isinstance(chg_mol.structure, Structure) | ||
assert len(chg_mol.structure) == 2 | ||
assert chg_mol.structure.formula == "Na1 Cl1" | ||
assert len(chg_mol.bond_order_dict) == 2 | ||
assert chg_mol.bond_order_dict[0]["bonded_to"][0] == { | ||
"spin_polarization": 0.0, | ||
"index": 1, | ||
"direction": (-1, -1, 0), | ||
"bond_order": 0.0882, | ||
"element": Element("Cl"), | ||
} | ||
assert ca.bond_order_dict[1]["bonded_to"][-1]["direction"] == (-1, 0, 0) | ||
assert ca.get_property_decorated_structure().site_properties["partial_charge_ddec6"] == ca.ddec_charges | ||
assert chg_mol.bond_order_dict[1]["bonded_to"][-1]["direction"] == (-1, 0, 0) | ||
# check that partial charges are written to structure site properties | ||
charge_decorated_struct = chg_mol.get_property_decorated_structure() | ||
struct_partial_charge = charge_decorated_struct.site_properties["partial_charge_ddec6"] | ||
assert struct_partial_charge == chg_mol.ddec_charges | ||
|
||
def test_parse_chargemol2(self): | ||
test_dir = f"{TEST_FILES_DIR}/chargemol/spin_polarized" | ||
ca = ChargemolAnalysis(path=test_dir, run_chargemol=False) | ||
assert ca.ddec_spin_moments == [0.201595, 0.399203, 0.399203] | ||
assert ca.summary["ddec"]["bond_order_dict"][0]["bonded_to"][0]["spin_polarization"] == 0.0490 | ||
assert ca.summary["ddec"]["spin_moments"] == ca.ddec_spin_moments | ||
assert ca.natoms is None | ||
assert ca.structure is None | ||
chg_mol = ChargemolAnalysis(path=test_dir, run_chargemol=False) | ||
assert chg_mol.ddec_spin_moments == [0.201595, 0.399203, 0.399203] | ||
assert chg_mol.summary["ddec"]["bond_order_dict"][0]["bonded_to"][0]["spin_polarization"] == 0.0490 | ||
assert chg_mol.summary["ddec"]["spin_moments"] == chg_mol.ddec_spin_moments | ||
assert chg_mol.natoms is None | ||
assert chg_mol.structure is None | ||
|
||
def test_missing_exe_error(self): | ||
# monkeypatch CHARGEMOL_EXE to raise in ChargemolAnalysis.__init__ | ||
patch.dict("os.environ", {"CHARGEMOL_EXE": "non_existent"}) | ||
|
||
test_dir = f"{TEST_FILES_DIR}/chargemol/spin_unpolarized" | ||
with pytest.raises( | ||
OSError, match="ChargemolAnalysis requires the Chargemol executable to be in PATH. Please download" | ||
): | ||
ChargemolAnalysis(path=test_dir, run_chargemol=True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do these two lines serve a purpose? i don't think these raise when
response.returncode != 0
? i think we need a test for the previously raisedRuntimeError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can just use
check=True
? We don't even need to capture response/stdout here as it is not used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, let's use
check=True
. it's a different error typeCalledProcessError
which I think is better thanRuntimeError
anyway but we should still add a test for this behavior.