Skip to content

Commit

Permalink
make generate_entry_label private static method
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Dec 24, 2024
1 parent d714101 commit b2d464b
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/pymatgen/analysis/pourbaix_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,14 @@ def as_dict(self) -> dict[Literal["ion", "energy", "name"], Any]:


def ion_or_solid_comp_object(formula: str) -> Composition | Ion:
"""Get an Ion or Composition object given a formula.
"""Get an Ion or Composition from a formula.
Args:
formula (str): Formula. E.g. of ion: NaOH(aq), Na[+];
E.g. of solid: Fe2O3(s), Fe(s), Na2O
formula (str): Formula. E.g. of ion: NaOH(aq), Na[+], Na+;
E.g. of solid: Fe2O3(s), Fe(s), Na2O.
Returns:
Composition/Ion object
Composition/Ion object.
"""
# Formula for ion
if formula.endswith("(aq)") or re.search(r"\[.*\]$", formula) or "-" in formula or "+" in formula:
Expand Down Expand Up @@ -970,6 +970,25 @@ def __init__(self, pourbaix_diagram: PourbaixDiagram) -> None:
"""
self._pbx = pourbaix_diagram

@staticmethod
def _generate_entry_label(entry: PourbaixEntry | MultiEntry) -> str:
"""
Generates a label for the Pourbaix plotter.
Args:
entry (PourbaixEntry or MultiEntry): entry to get a label for
"""
if isinstance(entry, MultiEntry):
return " + ".join(entry.name for entry in entry.entry_list)

# TODO - a more elegant solution could be added later to Stringify
# for example, the pattern re.sub(r"([-+][\d\.]*)", r"$^{\1}$", )
# will convert B(OH)4- to B(OH)$_4^-$.
# for this to work, the ion's charge always must be written AFTER
# the sign (e.g., Fe+2 not Fe2+)
string = entry.to_latex_string()
return re.sub(r"()\[([^)]*)\]", r"\1$^{\2}$", string)

def show(self, *args, **kwargs) -> None:
"""Show the Pourbaix plot.
Expand Down Expand Up @@ -1040,7 +1059,7 @@ def get_pourbaix_plot(

if label_domains:
ax.annotate(
generate_entry_label(entry),
self._generate_entry_label(entry),
center,
ha="center",
va="center",
Expand Down Expand Up @@ -1093,7 +1112,7 @@ def plot_entry_stability(
# Plot stability map
cax = ax.pcolor(pH, V, stability, cmap=cmap, vmin=0, vmax=e_hull_max)
cbar = ax.figure.colorbar(cax)
cbar.set_label(f"Stability of {generate_entry_label(entry)} (eV/atom)")
cbar.set_label(f"Stability of {self._generate_entry_label(entry)} (eV/atom)")

# Set ticklabels
# ticklabels = [t.get_text() for t in cbar.ax.get_yticklabels()]
Expand All @@ -1112,22 +1131,3 @@ def domain_vertices(self, entry) -> list:
list of vertices
"""
return self._pbx._stable_domain_vertices[entry]


def generate_entry_label(entry: PourbaixEntry | MultiEntry) -> str:
"""
Generates a label for the Pourbaix plotter.
Args:
entry (PourbaixEntry or MultiEntry): entry to get a label for
"""
if isinstance(entry, MultiEntry):
return " + ".join(entry.name for entry in entry.entry_list)

# TODO - a more elegant solution could be added later to Stringify
# for example, the pattern re.sub(r"([-+][\d\.]*)", r"$^{\1}$", )
# will convert B(OH)4- to B(OH)$_4^-$.
# for this to work, the ion's charge always must be written AFTER
# the sign (e.g., Fe+2 not Fe2+)
string = entry.to_latex_string()
return re.sub(r"()\[([^)]*)\]", r"\1$^{\2}$", string)

0 comments on commit b2d464b

Please sign in to comment.