From e834c2e45095ef4e26b0abbcf3154a32ba02b161 Mon Sep 17 00:00:00 2001 From: naik-aakash Date: Mon, 16 Sep 2024 14:33:57 +0200 Subject: [PATCH] add tests for core, utils and batch --- tests/featurize/test_batch.py | 88 +++++++++++++++++++++++++++++++++++ tests/featurize/test_core.py | 56 ++++++++++++++++++++++ tests/featurize/test_utils.py | 33 ++++++++++++- 3 files changed, 176 insertions(+), 1 deletion(-) diff --git a/tests/featurize/test_batch.py b/tests/featurize/test_batch.py index bdb8fceb..e687851f 100644 --- a/tests/featurize/test_batch.py +++ b/tests/featurize/test_batch.py @@ -425,6 +425,94 @@ def test_summary_featurize_with_json_antibonding(self): assert sorted(df.columns) == sorted(expected_cols) + def test_summary_featurize_unique_bonds_df(self): + summary_featurize = BatchSummaryFeaturizer( + path_to_lobster_calcs=TestDir / "test_data/Featurizer_test_data/Lobster_calcs", + bonds="all", + n_jobs=3, + cutoff_icohp=0.05, + ) + + df = summary_featurize.get_unique_bonds_df() + + expected_index = ["mp-1000", "mp-2176", "mp-463"] + + expected_cols = [ + "Ba-Te_icohp_mean", + "Ba-Te_icohp_min", + "Ba-Te_icohp_max", + "Ba-Te_icohp_std", + "Ba-Te_icohp_sum", + "Ba-Ba_icohp_mean", + "Ba-Ba_icohp_min", + "Ba-Ba_icohp_max", + "Ba-Ba_icohp_std", + "Ba-Ba_icohp_sum", + "Zn-Te_icohp_mean", + "Zn-Te_icohp_min", + "Zn-Te_icohp_max", + "Zn-Te_icohp_std", + "Zn-Te_icohp_sum", + "K-K_icohp_mean", + "K-K_icohp_min", + "K-K_icohp_max", + "K-K_icohp_std", + "K-K_icohp_sum", + "K-F_icohp_mean", + "K-F_icohp_min", + "K-F_icohp_max", + "K-F_icohp_std", + "K-F_icohp_sum", + ] + + assert isinstance(df, pd.DataFrame) + assert sorted(df.columns) == sorted(expected_cols) + assert sorted(df.index) == sorted(expected_index) + + summary_featurize_rm = BatchSummaryFeaturizer( + path_to_lobster_calcs=TestDir / "test_data/Featurizer_test_data/Lobster_calcs", + bonds="all", + n_jobs=3, + rm_weighted_icohps=True, + cutoff_icohp=0.05, + ) + + df_rm = summary_featurize_rm.get_unique_bonds_df() + + rm_expected_index = ["mp-1000", "mp-2176", "mp-463"] + + rm_expected_cols = [ + "Ba-Te_rm_icohp_mean", + "Ba-Te_rm_icohp_min", + "Ba-Te_rm_icohp_max", + "Ba-Te_rm_icohp_std", + "Ba-Te_rm_icohp_sum", + "Ba-Ba_rm_icohp_mean", + "Ba-Ba_rm_icohp_min", + "Ba-Ba_rm_icohp_max", + "Ba-Ba_rm_icohp_std", + "Ba-Ba_rm_icohp_sum", + "Zn-Te_rm_icohp_mean", + "Zn-Te_rm_icohp_min", + "Zn-Te_rm_icohp_max", + "Zn-Te_rm_icohp_std", + "Zn-Te_rm_icohp_sum", + "K-K_rm_icohp_mean", + "K-K_rm_icohp_min", + "K-K_rm_icohp_max", + "K-K_rm_icohp_std", + "K-K_rm_icohp_sum", + "K-F_rm_icohp_mean", + "K-F_rm_icohp_min", + "K-F_rm_icohp_max", + "K-F_rm_icohp_std", + "K-F_rm_icohp_sum", + ] + + assert isinstance(df_rm, pd.DataFrame) + assert sorted(df_rm.columns) == sorted(rm_expected_cols) + assert sorted(df_rm.index) == sorted(rm_expected_index) + class TestBatchCoxxFingerprint: def test_fp_cohp_overall(self): diff --git a/tests/featurize/test_core.py b/tests/featurize/test_core.py index 79b2136e..ef3dc7e0 100644 --- a/tests/featurize/test_core.py +++ b/tests/featurize/test_core.py @@ -246,6 +246,62 @@ def test_featurize_mp463(self): assert df.loc["mp-463", "antibonding_orb_perc_max"] == pytest.approx(0.580000, abs=1e-05) assert df.loc["mp-463", "antibonding_orb_perc_std"] == pytest.approx(0.169999, abs=1e-05) + def test_featurize_unique_bonds_mp463(self): + path_to_lobster_calc = TestDir / "test_data/Featurizer_test_data/Lobster_calcs/mp-463" + df = FeaturizeLobsterpy.get_unique_bonds_df(ids=None, bonds="all", path_to_lobster_calc=path_to_lobster_calc) + + expected_cols = [ + "K-K_icohp_mean", + "K-K_icohp_min", + "K-K_icohp_max", + "K-K_icohp_std", + "K-K_icohp_sum", + "K-F_icohp_mean", + "K-F_icohp_min", + "K-F_icohp_max", + "K-F_icohp_std", + "K-F_icohp_sum", + ] + + assert sorted(df.columns) == sorted(expected_cols) + assert df.index[0] == "mp-463" + + # Test columns values + assert df.loc["mp-463", "K-K_icohp_min"] == pytest.approx(-0.29358, abs=1e-05) + assert df.loc["mp-463", "K-K_icohp_max"] == pytest.approx(-0.29354, abs=1e-05) + assert df.loc["mp-463", "K-K_icohp_mean"] == pytest.approx(-0.29356, abs=1e-05) + assert df.loc["mp-463", "K-K_icohp_sum"] == pytest.approx(-3.52272, abs=1e-05) + assert df.loc["mp-463", "K-K_icohp_std"] == pytest.approx(0.00002, abs=1e-05) + assert df.loc["mp-463", "K-F_icohp_min"] == pytest.approx(-0.39612, abs=1e-05) + assert df.loc["mp-463", "K-F_icohp_max"] == pytest.approx(-0.39612, abs=1e-05) + assert df.loc["mp-463", "K-F_icohp_mean"] == pytest.approx(-0.39612, abs=1e-05) + assert df.loc["mp-463", "K-F_icohp_sum"] == pytest.approx(-4.75344, abs=1e-05) + assert df.loc["mp-463", "K-F_icohp_std"] == pytest.approx(5.551115e-17, abs=1e-05) + + def test_featurize_unique_bonds_mp2176(self): + path_to_lobster_calc = TestDir / "test_data/Featurizer_test_data/Lobster_calcs/mp-2176" + df = FeaturizeLobsterpy.get_unique_bonds_df( + ids="mp-2176", bonds="all", rm_weighted_icohps=True, path_to_lobster_calc=path_to_lobster_calc + ) + + expected_cols = [ + "Zn-Te_rm_icohp_mean", + "Zn-Te_rm_icohp_min", + "Zn-Te_rm_icohp_max", + "Zn-Te_rm_icohp_std", + "Zn-Te_rm_icohp_sum", + ] + + assert sorted(df.columns) == sorted(expected_cols) + assert df.index[0] == "mp-2176" + + # Test columns values + assert df.loc["mp-2176", "Zn-Te_rm_icohp_mean"] == pytest.approx(-0.024767, abs=1e-05) + assert df.loc["mp-2176", "Zn-Te_rm_icohp_min"] == pytest.approx(-0.024767, abs=1e-05) + assert df.loc["mp-2176", "Zn-Te_rm_icohp_max"] == pytest.approx(-0.024767, abs=1e-05) + assert df.loc["mp-2176", "Zn-Te_rm_icohp_std"] == pytest.approx(0.000000, abs=1e-05) + assert df.loc["mp-2176", "Zn-Te_rm_icohp_sum"] == pytest.approx(-0.198137, abs=1e-05) + # Tests for new jsons from atomate2 def test_featurize_mp66_json(self): featurize_mp66_json = FeaturizeLobsterpy( diff --git a/tests/featurize/test_utils.py b/tests/featurize/test_utils.py index ccd769d4..83e7876e 100644 --- a/tests/featurize/test_utils.py +++ b/tests/featurize/test_utils.py @@ -2,7 +2,14 @@ import shutil from pathlib import Path -from lobsterpy.featurize import get_file_paths, get_structure_path +import pytest +from lobsterpy.featurize import ( + get_electronegativities, + get_file_paths, + get_reduced_mass, + get_structure_path, + sort_dict_by_value, +) from pymatgen.core import Structure CurrentDir = Path(__file__).absolute().parent @@ -55,3 +62,27 @@ def test_get_file_paths(tmp_path): for key, value in file_paths_unzipped.items(): assert isinstance(key, str) assert isinstance(value, Path) + + +def test_get_reduced_mass(): + """ + Tests that reduced mass is computed correctly. + """ + assert get_reduced_mass(["H", "Pt"]) == pytest.approx(1.002818, abs=1e-05) + assert get_reduced_mass(["Na", "Cl"]) == pytest.approx(13.945765, abs=1e-05) + + +def test_get_electronegativities(): + """ + Tests that electronegativities are computed correctly. + """ + assert get_electronegativities(["H", "Pt"]) == pytest.approx([13.61, 10.16], abs=1e-05) + assert get_electronegativities(["Na", "Cl"]) == pytest.approx([5.14, 16.97], abs=1e-05) + + +def test_sort_dict_by_value(): + """ + Tests that dictionary is sorted by values. + """ + assert sort_dict_by_value(input_dict={"Na-Cl": -1.6, "H-Pt": -3.4}) == {"H-Pt": -3.4, "Na-Cl": -1.6} + assert sort_dict_by_value(input_dict={"a": 1, "b": 0, "c": 2}) == {"b": 0, "a": 1, "c": 2}