Skip to content
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

Remove deprecated analysis.interface, rename classes to PascalCase and rename with_* to from_* #3725

Merged
merged 15 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pymatgen/analysis/bond_dissociation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(
self.expected_charges = [final_charge - 2, final_charge - 1, final_charge, final_charge + 1]

# Build principle molecule graph
self.mol_graph = MoleculeGraph.with_local_env_strategy(
self.mol_graph = MoleculeGraph.from_local_env_strategy(
Molecule.from_dict(molecule_entry["final_molecule"]), OpenBabelNN()
)
# Loop through bonds, aka graph edges, and fragment and process:
Expand Down Expand Up @@ -287,10 +287,10 @@ def filter_fragment_entries(self, fragment_entries: list) -> None:
raise RuntimeError(err_msg.replace("[[placeholder]]", "a different"))

# Build initial and final molgraphs:
entry["initial_molgraph"] = MoleculeGraph.with_local_env_strategy(
entry["initial_molgraph"] = MoleculeGraph.from_local_env_strategy(
Molecule.from_dict(entry["initial_molecule"]), OpenBabelNN()
)
entry["final_molgraph"] = MoleculeGraph.with_local_env_strategy(
entry["final_molgraph"] = MoleculeGraph.from_local_env_strategy(
Molecule.from_dict(entry["final_molecule"]), OpenBabelNN()
)
# Classify any potential structural change that occurred during optimization:
Expand Down
2 changes: 1 addition & 1 deletion pymatgen/analysis/dimensionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def zero_d_graph_to_molecule_graph(bonded_structure, graph):
sorted_sites = np.array(sites, dtype=object)[indices_ordering]
sorted_graph = nx.convert_node_labels_to_integers(graph, ordering="sorted")
mol = Molecule([s.specie for s in sorted_sites], [s.coords for s in sorted_sites])
return MoleculeGraph.with_edges(mol, nx.Graph(sorted_graph).edges())
return MoleculeGraph.from_edges(mol, nx.Graph(sorted_graph).edges())


def get_dimensionality_cheon(
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/analysis/fragmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(
self.opt_steps = opt_steps

if edges is None:
self.mol_graph = MoleculeGraph.with_local_env_strategy(molecule, OpenBabelNN())
self.mol_graph = MoleculeGraph.from_local_env_strategy(molecule, OpenBabelNN())
else:
_edges: dict[tuple[int, int], dict | None] = {(edge[0], edge[1]): None for edge in edges}
self.mol_graph = MoleculeGraph.with_edges(molecule, _edges)
Expand Down Expand Up @@ -307,4 +307,4 @@ def open_ring(mol_graph: MoleculeGraph, bond: list, opt_steps: int) -> MoleculeG
ob_mol.remove_bond(bond[0][0] + 1, bond[0][1] + 1)
ob_mol.localopt(steps=opt_steps, forcefield="uff")

return MoleculeGraph.with_local_env_strategy(ob_mol.pymatgen_mol, OpenBabelNN())
return MoleculeGraph.from_local_env_strategy(ob_mol.pymatgen_mol, OpenBabelNN())
2 changes: 1 addition & 1 deletion pymatgen/analysis/functional_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(self, molecule, optimize=False):
raise ValueError("Input to FunctionalGroupExtractor must be str, Molecule, or MoleculeGraph.")

if self.molgraph is None:
self.molgraph = MoleculeGraph.with_local_env_strategy(self.molecule, OpenBabelNN())
self.molgraph = MoleculeGraph.from_local_env_strategy(self.molecule, OpenBabelNN())

# Assign a specie and coordinates to each node in the graph,
# corresponding to the Site in the Molecule object
Expand Down
81 changes: 65 additions & 16 deletions pymatgen/analysis/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import networkx as nx
import networkx.algorithms.isomorphism as iso
import numpy as np
from monty.dev import deprecated
from monty.json import MSONable
from networkx.drawing.nx_agraph import write_dot
from networkx.readwrite import json_graph
Expand Down Expand Up @@ -115,8 +116,8 @@ class StructureGraph(MSONable):

def __init__(self, structure: Structure, graph_data: dict | None = None) -> None:
"""
If constructing this class manually, use the with_empty_graph method or
with_local_env_strategy method (using an algorithm provided by the local_env
If constructing this class manually, use the from_empty_graph method or
from_local_env_strategy method (using an algorithm provided by the local_env
module, such as O'Keeffe).
This class that contains connection information: relationships between sites
represented by a Graph structure, and an associated structure object.
Expand Down Expand Up @@ -153,7 +154,7 @@ def __init__(self, structure: Structure, graph_data: dict | None = None) -> None
data["from_jimage"] = tuple(from_img)

@classmethod
def with_empty_graph(
def from_empty_graph(
cls,
structure: Structure,
name: str = "bonds",
Expand Down Expand Up @@ -196,8 +197,16 @@ def with_empty_graph(

return cls(structure, graph_data=graph_data)

@deprecated(
from_empty_graph,
"Deprecated on 2024-03-29, to be removed on 2025-03-20.",
)
@classmethod
def with_edges(cls, structure: Structure, edges: dict) -> Self:
def with_empty_graph(cls, *args, **kwargs):
return cls.from_empty_graph(*args, **kwargs)

@classmethod
def from_edges(cls, structure: Structure, edges: dict) -> Self:
"""
Constructor for MoleculeGraph, using pre-existing or pre-defined edges
with optional edge parameters.
Expand All @@ -213,7 +222,7 @@ def with_edges(cls, structure: Structure, edges: dict) -> Self:
Returns:
sg, a StructureGraph
"""
struct_graph = cls.with_empty_graph(structure, name="bonds", edge_weight_name="weight", edge_weight_units="")
struct_graph = cls.from_empty_graph(structure, name="bonds", edge_weight_name="weight", edge_weight_units="")

for edge, props in edges.items():
try:
Expand Down Expand Up @@ -250,8 +259,16 @@ def with_edges(cls, structure: Structure, edges: dict) -> Self:
struct_graph.set_node_attributes()
return struct_graph

@deprecated(
from_edges,
"Deprecated on 2024-03-29, to be removed on 2025-03-20.",
)
@classmethod
def with_edges(cls, *args, **kwargs):
return cls.from_edges(*args, **kwargs)

@classmethod
def with_local_env_strategy(
def from_local_env_strategy(
cls, structure: Structure, strategy: NearNeighbors, weights: bool = False, edge_properties: bool = False
) -> Self:
"""
Expand All @@ -267,7 +284,7 @@ def with_local_env_strategy(
if not strategy.structures_allowed:
raise ValueError("Chosen strategy is not designed for use with structures! Please choose another strategy.")

struct_graph = cls.with_empty_graph(structure, name="bonds")
struct_graph = cls.from_empty_graph(structure, name="bonds")

for idx, neighbors in enumerate(strategy.get_all_nn_info(structure)):
for neighbor in neighbors:
Expand All @@ -287,6 +304,14 @@ def with_local_env_strategy(

return struct_graph

@deprecated(
from_local_env_strategy,
"Deprecated on 2024-03-29, to be removed on 2025-03-20.",
)
@classmethod
def with_local_env_strategy(cls, *args, **kwargs):
return cls.from_local_env_strategy(*args, **kwargs)

@property
def name(self) -> str:
"""Name of graph"""
Expand Down Expand Up @@ -1536,8 +1561,8 @@ class MoleculeGraph(MSONable):

def __init__(self, molecule, graph_data=None):
"""
If constructing this class manually, use the `with_empty_graph`
method or `with_local_env_strategy` method (using an algorithm
If constructing this class manually, use the `from_empty_graph`
method or `from_local_env_strategy` method (using an algorithm
provided by the `local_env` module, such as O'Keeffe).

This class that contains connection information:
Expand Down Expand Up @@ -1581,7 +1606,7 @@ def __init__(self, molecule, graph_data=None):
self.set_node_attributes()

@classmethod
def with_empty_graph(cls, molecule, name="bonds", edge_weight_name=None, edge_weight_units=None) -> Self:
def from_empty_graph(cls, molecule, name="bonds", edge_weight_name=None, edge_weight_units=None) -> Self:
"""
Constructor for MoleculeGraph, returns a MoleculeGraph
object with an empty graph (no edges, only nodes defined
Expand Down Expand Up @@ -1620,8 +1645,16 @@ def with_empty_graph(cls, molecule, name="bonds", edge_weight_name=None, edge_we

return cls(molecule, graph_data=graph_data)

@deprecated(
from_empty_graph,
"Deprecated on 2024-03-29, to be removed on 2025-03-20.",
)
@classmethod
def with_edges(cls, molecule: Molecule, edges: dict[tuple[int, int], None | dict]) -> Self:
def with_empty_graph(cls, *args, **kwargs):
return cls.from_empty_graph(*args, **kwargs)

@classmethod
def from_edges(cls, molecule: Molecule, edges: dict[tuple[int, int], None | dict]) -> Self:
"""
Constructor for MoleculeGraph, using pre-existing or pre-defined edges
with optional edge parameters.
Expand All @@ -1636,7 +1669,7 @@ def with_edges(cls, molecule: Molecule, edges: dict[tuple[int, int], None | dict
Returns:
A MoleculeGraph
"""
mg = cls.with_empty_graph(molecule, name="bonds", edge_weight_name="weight", edge_weight_units="")
mg = cls.from_empty_graph(molecule, name="bonds", edge_weight_name="weight", edge_weight_units="")

for edge, props in edges.items():
try:
Expand Down Expand Up @@ -1664,8 +1697,16 @@ def with_edges(cls, molecule: Molecule, edges: dict[tuple[int, int], None | dict
mg.set_node_attributes()
return mg

@deprecated(
from_edges,
"Deprecated on 2024-03-29, to be removed on 2025-03-20.",
)
@classmethod
def with_edges(cls, *args, **kwargs):
return cls.from_edges(*args, **kwargs)

@classmethod
def with_local_env_strategy(cls, molecule, strategy) -> Self:
def from_local_env_strategy(cls, molecule, strategy) -> Self:
"""
Constructor for MoleculeGraph, using a strategy
from pymatgen.analysis.local_env.
Expand All @@ -1681,7 +1722,7 @@ def with_local_env_strategy(cls, molecule, strategy) -> Self:
raise ValueError(f"{strategy=} is not designed for use with molecules! Choose another strategy.")
extend_structure = strategy.extend_structure_molecules

mg = cls.with_empty_graph(molecule, name="bonds", edge_weight_name="weight", edge_weight_units="")
mg = cls.from_empty_graph(molecule, name="bonds", edge_weight_name="weight", edge_weight_units="")

# NearNeighbor classes only (generally) work with structures
# molecules have to be boxed first
Expand Down Expand Up @@ -1729,6 +1770,14 @@ def with_local_env_strategy(cls, molecule, strategy) -> Self:
mg.set_node_attributes()
return mg

@deprecated(
from_local_env_strategy,
"Deprecated on 2024-03-29, to be removed on 2025-03-20.",
)
@classmethod
def with_local_env_strategy(cls, *args, **kwargs):
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
return cls.from_local_env_strategy(*args, **kwargs)

@property
def name(self):
"""Name of graph"""
Expand Down Expand Up @@ -2132,7 +2181,7 @@ def build_unique_fragments(self):
edges[(from_index, to_index)] = edge_props

unique_mol_graph_list.append(
self.with_edges(
self.from_edges(
Molecule(species=species, coords=coords, charge=self.molecule.charge),
edges,
)
Expand Down Expand Up @@ -2240,7 +2289,7 @@ def map_indices(grp):
)

else:
graph = self.with_local_env_strategy(func_grp, strategy(**(strategy_params or {})))
graph = self.from_local_env_strategy(func_grp, strategy(**(strategy_params or {})))

for u, v in list(graph.graph.edges()):
edge_props = graph.graph.get_edge_data(u, v)[0]
Expand Down
23 changes: 0 additions & 23 deletions pymatgen/analysis/interface.py

This file was deleted.

38 changes: 31 additions & 7 deletions pymatgen/analysis/local_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import TYPE_CHECKING, Any, Literal, get_args

import numpy as np
from monty.dev import requires
from monty.dev import deprecated, requires
from monty.serialization import loadfn
from ruamel.yaml import YAML
from scipy.spatial import Voronoi
Expand Down Expand Up @@ -625,7 +625,7 @@ def get_bonded_structure(
order_parameters = [self.get_local_order_parameters(structure, n) for n in range(len(structure))]
structure.add_site_property("order_parameters", order_parameters)

struct_graph = StructureGraph.with_local_env_strategy(
struct_graph = StructureGraph.from_local_env_strategy(
structure, self, weights=weights, edge_properties=edge_properties
)

Expand Down Expand Up @@ -1524,7 +1524,7 @@ def get_bonded_structure(self, structure: Structure, decorate: bool = False) ->
order_parameters = [self.get_local_order_parameters(structure, n) for n in range(len(structure))]
structure.add_site_property("order_parameters", order_parameters)

return MoleculeGraph.with_local_env_strategy(structure, self)
return MoleculeGraph.from_local_env_strategy(structure, self)

def get_nn_shell_info(self, structure: Structure, site_idx, shell):
"""Get a certain nearest neighbor shell for a certain site.
Expand Down Expand Up @@ -1671,7 +1671,7 @@ def get_bonded_structure(self, structure: Structure, decorate: bool = False) ->
order_parameters = [self.get_local_order_parameters(structure, n) for n in range(len(structure))]
structure.add_site_property("order_parameters", order_parameters)

return MoleculeGraph.with_local_env_strategy(structure, self)
return MoleculeGraph.from_local_env_strategy(structure, self)

def get_nn_shell_info(self, structure: Structure, site_idx, shell):
"""Get a certain nearest neighbor shell for a certain site.
Expand Down Expand Up @@ -3370,7 +3370,7 @@ def get_order_parameters(
return ops


class BrunnerNN_reciprocal(NearNeighbors):
class BrunnerNNReciprocal(NearNeighbors):
"""
Determine coordination number using Brunner's algorithm which counts the
atoms that are within the largest gap in differences in real space
Expand Down Expand Up @@ -3441,7 +3441,15 @@ def get_nn_info(self, structure: Structure, n: int):
return siw


class BrunnerNN_relative(NearNeighbors):
@deprecated(
BrunnerNNReciprocal,
"Deprecated on 2024-03-29, to be removed on 2025-03-29.",
)
class BrunnerNN_reciprocal(BrunnerNNReciprocal):
pass


class BrunnerNNRelative(NearNeighbors):
"""
Determine coordination number using Brunner's algorithm which counts the
atoms that are within the largest gap in differences in real space
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -3513,7 +3521,15 @@ def get_nn_info(self, structure: Structure, n: int):
return siw


class BrunnerNN_real(NearNeighbors):
@deprecated(
BrunnerNNRelative,
"Deprecated on 2024-03-29, to be removed on 2025-03-29.",
)
class BrunnerNN_relative(BrunnerNNRelative):
pass


class BrunnerNNReal(NearNeighbors):
"""
Determine coordination number using Brunner's algorithm which counts the
atoms that are within the largest gap in differences in real space
Expand Down Expand Up @@ -3585,6 +3601,14 @@ def get_nn_info(self, structure: Structure, n: int):
return siw


@deprecated(
BrunnerNNReal,
"Deprecated on 2024-03-29, to be removed on 2025-03-29.",
)
class BrunnerNN_real(BrunnerNNReal):
pass


class EconNN(NearNeighbors):
"""
Determines the average effective coordination number for each cation in a
Expand Down
4 changes: 2 additions & 2 deletions pymatgen/analysis/magnetism/heisenberg.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _get_graphs(cutoff, ordered_structures):
strategy = MinimumDistanceNN(cutoff=cutoff, get_all_sites=True) if cutoff else MinimumDistanceNN() # only NN

# Generate structure graphs
return [StructureGraph.with_local_env_strategy(s, strategy=strategy) for s in ordered_structures]
return [StructureGraph.from_local_env_strategy(s, strategy=strategy) for s in ordered_structures]

@staticmethod
def _get_unique_sites(structure):
Expand Down Expand Up @@ -542,7 +542,7 @@ def get_interaction_graph(self, filename=None):
structure = self.ordered_structures[0]
sgraph = self.sgraphs[0]

igraph = StructureGraph.with_empty_graph(
igraph = StructureGraph.from_empty_graph(
structure, edge_weight_name="exchange_constant", edge_weight_units="meV"
)

Expand Down