Skip to content

Commit

Permalink
add more tests for cli and plotter
Browse files Browse the repository at this point in the history
  • Loading branch information
naik-aakash committed Aug 18, 2023
1 parent 1e83714 commit 8d607cb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.

Large diffs are not rendered by default.

58 changes: 56 additions & 2 deletions lobsterpy/plotting/test/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
from pathlib import Path
from plotly.io import read_json
from pymatgen.electronic_structure.cohp import Cohp
from lobsterpy.cohp.analyze import Analysis
from lobsterpy.cohp.describe import Description
from lobsterpy.plotting import PlainCohpPlotter, InteractiveCohpPlotter
Expand All @@ -13,7 +14,7 @@
TestDir = CurrentDir / "../../"


class InteractiveCohpPlotterTest(unittest.TestCase):
class PlainInteractiveCohpPlotterTest(unittest.TestCase):
def setUp(self):
self.analyse_NaCl = Analysis(
path_to_poscar=TestDir / "TestData/NaCl/POSCAR",
Expand All @@ -25,6 +26,18 @@ def setUp(self):
summed_spins=False,
)

self.analyse_NaCl_cobi = Analysis(
path_to_poscar=TestDir / "TestData/NaCl_comp_range/POSCAR.gz",
path_to_cohpcar=TestDir / "TestData/NaCl_comp_range/COBICAR.lobster.gz",
path_to_icohplist=TestDir / "TestData/NaCl_comp_range/ICOBILIST.lobster.gz",
path_to_charge=TestDir / "TestData/NaCl_comp_range/CHARGE.lobster.gz",
whichbonds="cation-anion",
cutoff_icohp=0.1,
summed_spins=False,
noise_cutoff=0.001,
are_cobis=True,
)

self.analyse_NaSi = Analysis(
path_to_poscar=TestDir / "TestData/NaSi/POSCAR",
path_to_cohpcar=TestDir / "TestData/NaSi/COHPCAR.lobster",
Expand Down Expand Up @@ -66,7 +79,6 @@ def test_add_all_relevant_cohps_NaCl(self):
self.iplotter.add_all_relevant_cohps(
analyse=self.analyse_NaCl, label_resolved=False, suffix=""
)
# self.assertIn("Please select COHP label here", self.iplotter._cohps)
self.assertIn("All", self.iplotter._cohps)
self.assertEqual(len(self.iplotter._cohps), 1)

Expand All @@ -90,6 +102,35 @@ def test_add_all_relevant_cohps_NaCl(self):
self.assertEqual(og_trace.line, ref_trace.line)
self.assertEqual(og_trace.visible, ref_trace.visible)

def test_add_all_relevant_cohps_NaCl_cobi(self):
self.iplotter = InteractiveCohpPlotter(zero_at_efermi=False, are_cobis=True)

self.iplotter.add_all_relevant_cohps(
analyse=self.analyse_NaCl_cobi, label_resolved=False, suffix=""
)
self.assertIn("All", self.iplotter._cohps)
self.assertEqual(len(self.iplotter._cohps), 1)

fig = self.iplotter.get_plot()
ref_fig = read_json(
TestDir / "TestData/interactive_plotter_ref/analyse_NaCl_cobi.json",
engine="json",
)
self.assertEqual(len(fig.data), len(ref_fig.data))
self.assertEqual(fig.layout, ref_fig.layout)
for og_trace in fig.data:
if og_trace in ref_fig.data:
ref_trace = ref_fig.data[ref_fig.data.index(og_trace)]
for og_x, og_y, ref_x, ref_y in zip(
og_trace.x, og_trace.y, ref_trace.x, ref_trace.y
):
self.assertAlmostEqual(ref_x, og_x, delta=0.0001)
self.assertAlmostEqual(ref_y, og_y, delta=0.0001)
self.assertEqual(og_trace.name, ref_trace.name)
self.assertEqual(og_trace.line, ref_trace.line)
self.assertEqual(og_trace.line, ref_trace.line)
self.assertEqual(og_trace.visible, ref_trace.visible)

def test_add_all_relevant_cohps_K3Sb(self):
self.iplotter = InteractiveCohpPlotter()

Expand Down Expand Up @@ -259,6 +300,19 @@ def test_plot_labels(self):

self.assertEqual(fig.layout.xaxis["title"]["text"], "−COHP")

def test_plaincohp_plotter_options(self):
self.plotter = PlainCohpPlotter(zero_at_efermi=False)

for label, cohp in self.lobsterpy_plot_data.items():
cohp_obj = Cohp.from_dict(cohp)
self.plotter.add_cohp(label=label, cohp=cohp_obj)

fig = self.plotter.get_plot(
integrated=True, xlim=(-5, 2), ylim=(-4, 4), invert_axes=False
).gca()
self.assertEqual(fig.get_ylabel(), "$-$ICOHP (eV)")
self.assertEqual(fig.get_xlabel(), "$E$ (eV)")


class TestPlotterExceptions(unittest.TestCase):
def test_plotter_exception(self):
Expand Down
37 changes: 37 additions & 0 deletions lobsterpy/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ def test_iaplot_saved(self, tmp_path, inject_mocks, clean_plot):
run(test)
self.assert_is_finite_file(plot_path)

def test_cli_interactive_plotter_cobi(self):
os.chdir(TestDir / "TestData/NaCl_comp_range")
# tests skip showing plots generated using automatic interactive plotter
args = ["automatic-plot-ia", "--hideplot", "--cobis"]
test = get_parser().parse_args(args)
run(test)

def test_cli_interactive_plotter_coops(self):
os.chdir(TestDir / "TestData/CdF_comp_range")
# tests skip showing plots generated using automatic interactive plotter
args = [
"auto-plot-ia",
"--hideplot",
"--coops",
"--allbonds",
]
test = get_parser().parse_args(args)
run(test)

def test_lobsterin_generation(self, tmp_path):
os.chdir(TestDir / "TestData/Test_Input_Generation_Empty")
lobsterinpath = tmp_path / "lobsterin.lobsterpy"
Expand Down Expand Up @@ -226,6 +245,24 @@ def test_lobsterin_generation_error(self, tmp_path):
run(test)
os.chdir(TestDir / "TestData/NaCl")

def test_cli_automatic_analysis_error(self):
os.chdir(TestDir / "TestData/NaCl")
args1 = [
"description",
"--cobis",
]
test1 = get_parser().parse_args(args1)
with pytest.raises(ValueError):
run(test1)

args2 = [
"description",
"--coops",
]
test2 = get_parser().parse_args(args2)
with pytest.raises(ValueError):
run(test2)

def test_lobsterin_generation_error_userbasis(self, tmp_path):
# This is a test for the user-defined basis set.
os.chdir(TestDir / "TestData/Test_Input_Generation_Empty")
Expand Down

0 comments on commit 8d607cb

Please sign in to comment.