Skip to content

Commit

Permalink
Merge branch 'master' into kpoints-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep authored Dec 11, 2024
2 parents 2608e8a + 5abe81c commit 9259f13
Show file tree
Hide file tree
Showing 120 changed files with 1,119 additions and 645 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2
rev: v0.8.1
hooks:
- id: ruff
args: [--fix, --unsafe-fixes]
Expand Down Expand Up @@ -36,7 +36,7 @@ repos:
exclude: src/pymatgen/analysis/aflow_prototypes.json

- repo: https://github.com/MarcoGorelli/cython-lint
rev: v0.16.2
rev: v0.16.6
hooks:
- id: cython-lint
args: [--no-pycodestyle]
Expand All @@ -48,7 +48,7 @@ repos:
- id: blacken-docs

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.42.0
rev: v0.43.0
hooks:
- id: markdownlint
# MD013: line too long
Expand All @@ -59,12 +59,12 @@ repos:
args: [--disable, MD013, MD024, MD025, MD033, MD041, "--"]

- repo: https://github.com/kynan/nbstripout
rev: 0.8.0
rev: 0.8.1
hooks:
- id: nbstripout
args: [--drop-empty-cells, --keep-output]

- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.387
rev: v1.1.389
hooks:
- id: pyright
4 changes: 2 additions & 2 deletions dev_scripts/potcar_scrambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def scramble_single_potcar(self, potcar: PotcarSingle) -> str:
return scrambled_potcar_str

def to_file(self, filename: str) -> None:
with zopen(filename, mode="wt") as file:
with zopen(filename, mode="wt", encoding="utf-8") as file:
file.write(self.scrambled_potcars_str)

@classmethod
Expand Down Expand Up @@ -164,7 +164,7 @@ def generate_fake_potcar_libraries() -> None:
zpath(f"{func_dir}/{psp_name}/POTCAR"),
]
if not any(map(os.path.isfile, paths_to_try)):
warnings.warn(f"Could not find {psp_name} in {paths_to_try}")
warnings.warn(f"Could not find {psp_name} in {paths_to_try}", stacklevel=2)
for potcar_path in paths_to_try:
if os.path.isfile(potcar_path):
os.makedirs(rebase_dir, exist_ok=True)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ ignore = [

# Single rules
"B023", # Function definition does not bind loop variable
"B028", # No explicit stacklevel keyword argument found
"B904", # Within an except clause, raise exceptions with ...
"C408", # unnecessary-collection-call
"D105", # Missing docstring in magic method
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/alchemy/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def to_snl(self, authors: list[str], **kwargs) -> StructureNL:
StructureNL: The generated StructureNL object.
"""
if self.other_parameters:
warn("Data in TransformedStructure.other_parameters discarded during type conversion to SNL")
warn("Data in TransformedStructure.other_parameters discarded during type conversion to SNL", stacklevel=2)
history = []
for hist in self.history:
snl_metadata = hist.pop("_snl", {})
Expand Down
10 changes: 6 additions & 4 deletions src/pymatgen/analysis/bond_dissociation.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def __init__(
if multibreak:
warnings.warn(
"Breaking pairs of ring bonds. WARNING: Structure changes much more likely, meaning dissociation values"
" are less reliable! This is a bad idea!"
" are less reliable! This is a bad idea!",
stacklevel=2,
)
self.bond_pairs = []
for ii, bond in enumerate(self.ring_bonds, start=1):
Expand Down Expand Up @@ -164,7 +165,8 @@ def fragment_and_process(self, bonds):
warnings.warn(
f"Missing ring opening fragment resulting from the breakage of {specie[bonds[0][0]]} "
f"{specie[bonds[0][1]]} bond {bonds[0][0]} {bonds[0][1]} which would yield a "
f"molecule with this SMILES string: {smiles}"
f"molecule with this SMILES string: {smiles}",
stacklevel=2,
)
elif len(good_entries) == 1:
# If we have only one good entry, format it and add it to the list that will eventually return
Expand Down Expand Up @@ -212,14 +214,14 @@ def fragment_and_process(self, bonds):
smiles = pb_mol.write("smi").split()[0]
for charge in self.expected_charges:
if charge not in frag1_charges_found:
warnings.warn(f"Missing {charge=} for fragment {smiles}")
warnings.warn(f"Missing {charge=} for fragment {smiles}", stacklevel=2)
if len(frag2_charges_found) < len(self.expected_charges):
bb = BabelMolAdaptor(fragments[1].molecule)
pb_mol = bb.pybel_mol
smiles = pb_mol.write("smi").split()[0]
for charge in self.expected_charges:
if charge not in frag2_charges_found:
warnings.warn(f"Missing {charge=} for fragment {smiles}")
warnings.warn(f"Missing {charge=} for fragment {smiles}", stacklevel=2)
# Now we attempt to pair fragments with the right total charge, starting with only fragments with no
# structural change:
for frag1 in frag1_entries[0]: # 0 -> no structural change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2051,10 +2051,7 @@ def coordination_geometry_symmetry_measures_fallback_random(
The symmetry measures for the given coordination geometry for each permutation investigated.
"""
if "NRANDOM" in kwargs:
warnings.warn(
"NRANDOM is deprecated, use n_random instead",
category=DeprecationWarning,
)
warnings.warn("NRANDOM is deprecated, use n_random instead", category=DeprecationWarning, stacklevel=2)
n_random = kwargs.pop("NRANDOM")
permutations_symmetry_measures = [None] * n_random
permutations = []
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/analysis/chempot_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def _get_3d_plot(
if formulas_to_draw:
for formula in formulas_to_draw:
if formula not in domain_simplexes:
warnings.warn(f"Specified formula to draw, {formula}, not found!")
warnings.warn(f"Specified formula to draw, {formula}, not found!", stacklevel=2)

if draw_formula_lines:
data.extend(self._get_3d_formula_lines(draw_domains, formula_colors))
Expand Down
11 changes: 7 additions & 4 deletions src/pymatgen/analysis/elasticity/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __new__(cls, input_array, check_rank=None, tol: float = 1e-4) -> Self:
if obj.rank % 2 != 0:
raise ValueError("ElasticTensor must have even rank")
if not obj.is_voigt_symmetric(tol):
warnings.warn("Input elastic tensor does not satisfy standard Voigt symmetries")
warnings.warn("Input elastic tensor does not satisfy standard Voigt symmetries", stacklevel=2)
return obj.view(cls)

@property
Expand Down Expand Up @@ -476,7 +476,8 @@ def from_pseudoinverse(cls, strains, stresses) -> Self:
# convert the stress/strain to Nx6 arrays of voigt notation
warnings.warn(
"Pseudo-inverse fitting of Strain/Stress lists may yield "
"questionable results from vasp data, use with caution."
"questionable results from vasp data, use with caution.",
stacklevel=2,
)
stresses = np.array([Stress(stress).voigt for stress in stresses])
with warnings.catch_warnings():
Expand Down Expand Up @@ -505,7 +506,9 @@ def from_independent_strains(cls, strains, stresses, eq_stress=None, vasp=False,
if not set(strain_states) <= set(ss_dict):
raise ValueError(f"Missing independent strain states: {set(strain_states) - set(ss_dict)}")
if len(set(ss_dict) - set(strain_states)) > 0:
warnings.warn("Extra strain states in strain-stress pairs are neglected in independent strain fitting")
warnings.warn(
"Extra strain states in strain-stress pairs are neglected in independent strain fitting", stacklevel=2
)
c_ij = np.zeros((6, 6))
for ii in range(6):
strains = ss_dict[strain_states[ii]]["strains"]
Expand Down Expand Up @@ -916,7 +919,7 @@ def find_eq_stress(strains, stresses, tol: float = 1e-10):
)
eq_stress = eq_stress[0]
else:
warnings.warn("No eq state found, returning zero voigt stress")
warnings.warn("No eq state found, returning zero voigt stress", stacklevel=2)
eq_stress = Stress(np.zeros((3, 3)))
return eq_stress

Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/analysis/ewald.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def get_site_energy(self, site_index):
self._initialized = True

if self._charged:
warn("Per atom energies for charged structures not supported in EwaldSummation")
warn("Per atom energies for charged structures not supported in EwaldSummation", stacklevel=2)
return np.sum(self._recip[:, site_index]) + np.sum(self._real[:, site_index]) + self._point[site_index]

def _calc_ewald_terms(self):
Expand Down
1 change: 1 addition & 0 deletions src/pymatgen/analysis/gb/grain.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
"Grain boundary analysis has been moved to pymatgen.core.interface."
"This stub is retained for backwards compatibility and will be removed Dec 31 2024.",
DeprecationWarning,
stacklevel=2,
)
11 changes: 7 additions & 4 deletions src/pymatgen/analysis/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def add_edge(
# edges if appropriate
if to_jimage is None:
# assume we want the closest site
warnings.warn("Please specify to_jimage to be unambiguous, trying to automatically detect.")
warnings.warn("Please specify to_jimage to be unambiguous, trying to automatically detect.", stacklevel=2)
dist, to_jimage = self.structure[from_index].distance_and_image(self.structure[to_index])
if dist == 0:
# this will happen when from_index == to_index,
Expand Down Expand Up @@ -417,7 +417,7 @@ def add_edge(
# this is a convention to avoid duplicate hops
if to_index == from_index:
if to_jimage == (0, 0, 0):
warnings.warn("Tried to create a bond to itself, this doesn't make sense so was ignored.")
warnings.warn("Tried to create a bond to itself, this doesn't make sense so was ignored.", stacklevel=2)
return

# ensure that the first non-zero jimage index is positive
Expand All @@ -439,7 +439,8 @@ def add_edge(
if warn_duplicates:
warnings.warn(
"Trying to add an edge that already exists from "
f"site {from_index} to site {to_index} in {to_jimage}."
f"site {from_index} to site {to_index} in {to_jimage}.",
stacklevel=2,
)
return

Expand Down Expand Up @@ -1826,7 +1827,9 @@ def add_edge(
# between two sites
existing_edge_data = self.graph.get_edge_data(from_index, to_index)
if existing_edge_data and warn_duplicates:
warnings.warn(f"Trying to add an edge that already exists from site {from_index} to site {to_index}.")
warnings.warn(
f"Trying to add an edge that already exists from site {from_index} to site {to_index}.", stacklevel=2
)
return

# generic container for additional edge properties,
Expand Down
11 changes: 7 additions & 4 deletions src/pymatgen/analysis/interface_reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,10 @@ def _get_entry_energy(pd: PhaseDiagram, composition: Composition):

if not candidate:
warnings.warn(
f"The reactant {composition.reduced_formula} has no matching entry with negative formation"
" energy, instead convex hull energy for this composition will be used for reaction energy calculation."
f"The reactant {composition.reduced_formula} has no matching entry "
"with negative formation energy, instead convex hull energy for "
"this composition will be used for reaction energy calculation.",
stacklevel=2,
)
return pd.get_hull_energy(composition)
min_entry_energy = min(candidate)
Expand Down Expand Up @@ -545,8 +547,9 @@ def get_chempot_correction(cls, element: str, temp: float, pres: float): # code
The correction of chemical potential in eV/atom of the gas
phase at given temperature and pressure.
"""
if element not in ["O", "N", "Cl", "F", "H"]:
warnings.warn(f"{element=} not one of valid options: ['O', 'N', 'Cl', 'F', 'H']")
valid_elements = {"O", "N", "Cl", "F", "H"}
if element not in valid_elements:
warnings.warn(f"{element=} not one of valid options: {valid_elements}", stacklevel=2)
return 0

std_temp = 298.15
Expand Down
6 changes: 4 additions & 2 deletions src/pymatgen/analysis/local_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4025,7 +4025,8 @@ def get_nn_data(self, structure: Structure, n: int, length=None):
warnings.warn(
"CrystalNN: cannot locate an appropriate radius, "
"covalent or atomic radii will be used, this can lead "
"to non-optimal results."
"to non-optimal results.",
stacklevel=2,
)
diameter = _get_default_radius(structure[n]) + _get_default_radius(entry["site"])

Expand Down Expand Up @@ -4231,7 +4232,8 @@ def _get_radius(site):
else:
warnings.warn(
"No oxidation states specified on sites! For better results, set "
"the site oxidation states in the structure."
"the site oxidation states in the structure.",
stacklevel=2,
)
return 0

Expand Down
16 changes: 9 additions & 7 deletions src/pymatgen/analysis/magnetism/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
try:
DEFAULT_MAGMOMS = loadfn(f"{MODULE_DIR}/default_magmoms.yaml")
except (FileNotFoundError, MarkedYAMLError):
warnings.warn("Could not load default_magmoms.yaml, falling back to VASPIncarBase.yaml")
warnings.warn("Could not load default_magmoms.yaml, falling back to VASPIncarBase.yaml", stacklevel=2)
DEFAULT_MAGMOMS = loadfn(f"{MODULE_DIR}/../../io/vasp/VASPIncarBase.yaml")["INCAR"]["MAGMOM"]


Expand Down Expand Up @@ -159,7 +159,7 @@ def __init__(
try:
structure = trans.apply_transformation(structure)
except ValueError:
warnings.warn(f"Could not assign valences for {structure.reduced_formula}")
warnings.warn(f"Could not assign valences for {structure.reduced_formula}", stacklevel=2)

# Check if structure has magnetic moments
# on site properties or species spin properties,
Expand All @@ -186,7 +186,8 @@ def __init__(
if None in structure.site_properties["magmom"]:
warnings.warn(
"Be careful with mixing types in your magmom site properties. "
"Any 'None' magmoms have been replaced with zero."
"Any 'None' magmoms have been replaced with zero.",
stacklevel=2,
)
magmoms = [m or 0 for m in structure.site_properties["magmom"]]
elif has_spin:
Expand All @@ -207,7 +208,8 @@ def __init__(
"This class is not designed to be used with "
"non-collinear structures. If your structure is "
"only slightly non-collinear (e.g. canted) may still "
"give useful results, but use with caution."
"give useful results, but use with caution.",
stacklevel=2,
)

# this is for collinear structures only, make sure magmoms are all floats
Expand Down Expand Up @@ -313,8 +315,8 @@ def _round_magmoms(magmoms: ArrayLike, round_magmoms_mode: float) -> np.ndarray:

except Exception as exc:
# TODO: typically a singular matrix warning, investigate this
warnings.warn("Failed to round magmoms intelligently, falling back to simple rounding.")
warnings.warn(str(exc))
warnings.warn("Failed to round magmoms intelligently, falling back to simple rounding.", stacklevel=2)
warnings.warn(str(exc), stacklevel=2)

# and finally round roughly to the number of significant figures in our kde width
n_decimals = len(str(round_magmoms_mode).split(".")[1]) + 1
Expand Down Expand Up @@ -483,7 +485,7 @@ def ordering(self) -> Ordering:
(in which case a warning is issued).
"""
if not self.is_collinear:
warnings.warn("Detecting ordering in non-collinear structures not yet implemented.")
warnings.warn("Detecting ordering in non-collinear structures not yet implemented.", stacklevel=2)
return Ordering.Unknown

if "magmom" not in self.structure.site_properties:
Expand Down
6 changes: 3 additions & 3 deletions src/pymatgen/analysis/magnetism/jahnteller.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def is_jahn_teller_active(
)
active = analysis["active"]
except Exception as exc:
warnings.warn(f"Error analyzing {structure.reduced_formula}: {exc}")
warnings.warn(f"Error analyzing {structure.reduced_formula}: {exc}", stacklevel=2)

return active

Expand Down Expand Up @@ -330,7 +330,7 @@ def tag_structure(
structure.add_site_property("possible_jt_active", jt_sites)
return structure
except Exception as exc:
warnings.warn(f"Error analyzing {structure.reduced_formula}: {exc}")
warnings.warn(f"Error analyzing {structure.reduced_formula}: {exc}", stacklevel=2)
return structure

@staticmethod
Expand Down Expand Up @@ -380,7 +380,7 @@ def get_magnitude_of_effect_from_species(self, species: str | Species, spin_stat
spin_config = self.spin_configs[motif][d_electrons][spin_state]
magnitude = JahnTellerAnalyzer.get_magnitude_of_effect_from_spin_config(motif, spin_config)
else:
warnings.warn("No data for this species.")
warnings.warn("No data for this species.", stacklevel=2)

return magnitude

Expand Down
Loading

0 comments on commit 9259f13

Please sign in to comment.