Skip to content

Commit 42ebece

Browse files
authored
add test for NEBAnalysis.get_plot() (#3570)
1 parent e085343 commit 42ebece

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

Diff for: pymatgen/analysis/transition_state.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -166,32 +166,25 @@ def get_plot(self, normalize_rxn_coordinate: bool = True, label_barrier: bool =
166166
plt.Axes: matplotlib axes object.
167167
"""
168168
ax = pretty_plot(12, 8)
169-
scale = 1 if not normalize_rxn_coordinate else 1 / self.r[-1]
170-
x = np.arange(0, np.max(self.r), 0.01)
171-
y = self.spline(x) * 1000
169+
scale = 1 / self.r[-1] if normalize_rxn_coordinate else 1
170+
xs = np.arange(0, np.max(self.r), 0.01)
171+
ys = self.spline(xs) * 1000
172172
relative_energies = self.energies - self.energies[0]
173-
ax.plot(
174-
self.r * scale,
175-
relative_energies * 1000,
176-
"ro",
177-
x * scale,
178-
y,
179-
"k-",
180-
linewidth=2,
181-
markersize=10,
182-
)
183-
ax.set_xlabel("Reaction coordinate")
173+
ax.plot(self.r * scale, relative_energies * 1000, "ro", xs * scale, ys, "k-", linewidth=2, markersize=10)
174+
175+
ax.set_xlabel("Reaction Coordinate")
184176
ax.set_ylabel("Energy (meV)")
185-
ax.set_ylim((np.min(y) - 10, np.max(y) * 1.02 + 20))
177+
ax.set_ylim((np.min(ys) - 10, np.max(ys) * 1.02 + 20))
186178
if label_barrier:
187-
data = zip(x * scale, y)
179+
data = zip(xs * scale, ys)
188180
barrier = max(data, key=lambda d: d[1])
189-
ax.plot([0, barrier[0]], [barrier[1], barrier[1]], "k--")
181+
ax.plot([0, barrier[0]], [barrier[1], barrier[1]], "k--", linewidth=0.5)
190182
ax.annotate(
191-
f"{np.max(y) - np.min(y):.0f} meV",
183+
f"{np.max(ys) - np.min(ys):.0f} meV",
192184
xy=(barrier[0] / 2, barrier[1] * 1.02),
193185
xytext=(barrier[0] / 2, barrier[1] * 1.02),
194186
horizontalalignment="center",
187+
fontsize=18,
195188
)
196189
plt.tight_layout()
197190
return ax

Diff for: tests/analysis/test_transition_state.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44

5+
from matplotlib import pyplot as plt
56
from numpy.testing import assert_allclose
67

78
from pymatgen.analysis.transition_state import NEBAnalysis, combine_neb_plots
@@ -19,12 +20,12 @@
1920
__date__ = "2/5/16"
2021

2122

22-
test_dir = f"{TEST_FILES_DIR}/neb_analysis"
23+
TEST_DIR = f"{TEST_FILES_DIR}/neb_analysis"
2324

2425

2526
class TestNEBAnalysis(PymatgenTest):
2627
def test_run(self):
27-
neb_analysis1 = NEBAnalysis.from_dir(f"{test_dir}/neb1/neb")
28+
neb_analysis1 = NEBAnalysis.from_dir(f"{TEST_DIR}/neb1/neb")
2829
neb_analysis1_from_dict = NEBAnalysis.from_dict(neb_analysis1.as_dict())
2930
json_data = json.dumps(neb_analysis1.as_dict())
3031

@@ -47,7 +48,7 @@ def test_run(self):
4748

4849
neb_analysis1.setup_spline(spline_options={"saddle_point": "zero_slope"})
4950
assert_allclose(neb_analysis1.get_extrema()[1][0], (0.50023335723480078, 325.20003984140203))
50-
with open(f"{test_dir}/neb2/neb_analysis2.json") as f:
51+
with open(f"{TEST_DIR}/neb2/neb_analysis2.json") as f:
5152
neb_analysis2_dict = json.load(f)
5253
neb_analysis2 = NEBAnalysis.from_dict(neb_analysis2_dict)
5354
assert_allclose(neb_analysis2.get_extrema()[1][0], (0.37255257367467326, 562.40825334519991))
@@ -56,6 +57,15 @@ def test_run(self):
5657
assert_allclose(neb_analysis2.get_extrema()[1][0], (0.30371133723478794, 528.46229631648691))
5758

5859
def test_combine_neb_plots(self):
59-
neb_dir = f"{test_dir}/neb1/neb"
60+
neb_dir = f"{TEST_DIR}/neb1/neb"
6061
neb_analysis = NEBAnalysis.from_dir(neb_dir)
6162
combine_neb_plots([neb_analysis, neb_analysis])
63+
64+
def test_get_plot(self):
65+
neb_dir = f"{TEST_DIR}/neb1/neb"
66+
neb_analysis = NEBAnalysis.from_dir(neb_dir)
67+
ax = neb_analysis.get_plot()
68+
assert isinstance(ax, plt.Axes)
69+
assert ax.texts[0].get_text() == "326 meV", "Unexpected annotation text"
70+
assert ax.get_xlabel() == "Reaction Coordinate"
71+
assert ax.get_ylabel() == "Energy (meV)"

0 commit comments

Comments
 (0)