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

Improve type annotations for io.lobster.{lobsterenv/outputs} #3887

Merged
merged 66 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
a680e82
temp save of cleaning lobsterenv
DanielYang59 Jun 19, 2024
1eaf906
fix type errors in lobsterenv
DanielYang59 Jun 19, 2024
6668e51
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jun 19, 2024
3f09a68
Merge branch 'type-lobsterenv-outputs' of https://github.com/DanielYa…
DanielYang59 Jun 19, 2024
e684a29
temp save of lobster.outputs
DanielYang59 Jun 19, 2024
6fd22ff
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jun 19, 2024
489a6e5
revert chemenv additional_condition rename
DanielYang59 Jun 19, 2024
f6f032d
first go of lobster.outputs
DanielYang59 Jun 19, 2024
e5280b0
fix typo in get_doc
DanielYang59 Jun 19, 2024
acc9a1f
separate lobster inputs and outputs tests
DanielYang59 Jun 19, 2024
d08f0c2
fix unit test
DanielYang59 Jun 19, 2024
18499c4
more var name and comment cleanups
DanielYang59 Jun 19, 2024
5e24acb
fix my errors (some unit tests fail)
DanielYang59 Jun 20, 2024
00b0a7d
fix some unit test
DanielYang59 Jun 20, 2024
240ddb0
fix unit test
DanielYang59 Jun 20, 2024
565c3fa
Need Confirm: change `np.float64` to float for spilling
DanielYang59 Jun 20, 2024
4143ef6
clarify data form in docstring
DanielYang59 Jun 20, 2024
1831078
use deprecated decorator
DanielYang59 Jun 21, 2024
ff0b08a
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jun 21, 2024
07b5bce
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jun 22, 2024
0012518
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jun 26, 2024
290a8ab
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jun 26, 2024
552620e
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 2, 2024
0499def
pre-commit auto-fixes
pre-commit-ci[bot] Jul 2, 2024
75d23f1
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 3, 2024
99b3bc9
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 12, 2024
b4e4005
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 15, 2024
4456e45
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 17, 2024
447b0ae
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 18, 2024
5e4c8c9
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 24, 2024
52b2fc0
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 28, 2024
4b11fd1
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Jul 31, 2024
a24765b
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 3, 2024
0cb6329
pre-commit auto-fixes
pre-commit-ci[bot] Aug 3, 2024
eb45f95
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 3, 2024
0455262
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 4, 2024
8071c7b
remove DEBUG tag and var name tweaks
DanielYang59 Aug 4, 2024
2d3b755
add more specific types
DanielYang59 Aug 4, 2024
c199864
use PeriodicSite over Site
DanielYang59 Aug 4, 2024
134101d
standardize idx var name
DanielYang59 Aug 4, 2024
628f737
limit matplotlib version
DanielYang59 Aug 4, 2024
e8f2bd9
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 5, 2024
ec65352
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 6, 2024
45cc6af
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 6, 2024
63a2d76
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 8, 2024
74d8e43
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 9, 2024
42bca90
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 16, 2024
fbe0d6f
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 16, 2024
4f58588
remove matplotlib pin
DanielYang59 Aug 20, 2024
3a26b91
remove left out matplotlib pin
DanielYang59 Aug 20, 2024
629c447
remove ignore override tag
DanielYang59 Aug 20, 2024
b64213b
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 20, 2024
9bd107c
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 20, 2024
7cb7030
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 22, 2024
063b54e
pre-commit auto-fixes
pre-commit-ci[bot] Aug 22, 2024
f5d3f6e
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 22, 2024
ef4693a
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 24, 2024
5cf9795
Apply suggestions from code review
JaGeo Aug 29, 2024
0a253b1
Merge branch 'master' into type-lobsterenv-outputs
JaGeo Aug 29, 2024
4ddada7
use specific type for get_nn_info return
DanielYang59 Aug 29, 2024
56f8aa9
add TODO for a very likely TODO comment
DanielYang59 Aug 29, 2024
4b2d199
remove implemented TODO tag
DanielYang59 Aug 29, 2024
a7d10d0
replace Literal with | in docstring
DanielYang59 Aug 29, 2024
879a29e
Merge branch 'master' into type-lobsterenv-outputs
DanielYang59 Aug 30, 2024
04c9817
use atom{idx}_list over atom{idx}s
DanielYang59 Aug 30, 2024
be77188
revert bool description
DanielYang59 Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more var name and comment cleanups
  • Loading branch information
DanielYang59 committed Jun 19, 2024
commit 18499c40dd486eeba2388e8d26b7b77d4f5b32d5
4 changes: 2 additions & 2 deletions pymatgen/io/lobster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
This package implements modules for input and output to and from Lobster. It
imports the key classes form both lobster.inputs and lobster_outputs to allow most
This package implements modules for input and output to and from LOBSTER. It
imports the key classes form both lobster.inputs and lobster.outputs to allow most
classes to be simply called as pymatgen.io.lobster.Lobsterin for example, to retain
backwards compatibility.
"""
Expand Down
93 changes: 53 additions & 40 deletions pymatgen/io/lobster/lobsterenv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This module provides classes to perform analyses of
the local environments (e.g., finding near neighbors)
of single sites in molecules and structures based on
bonding analysis with LOBSTER.
"""This module provides classes to perform analyses of the local
environments (e.g., finding near neighbors) of single sites in molecules
and structures based on bonding analysis with LOBSTER.

If you use this module, please cite:
J. George, G. Petretto, A. Naik, M. Esters, A. J. Jackson, R. Nelson, R. Dronskowski, G.-M. Rignanese, G. Hautier,
Expand Down Expand Up @@ -36,9 +35,10 @@
from typing import Any, Literal

import matplotlib as mpl
from numpy.typing import NDArray
from typing_extensions import Self

from pymatgen.core import Site, Structure
from pymatgen.core import PeriodicNeighbor, Site, Structure
from pymatgen.core.periodic_table import Element
from pymatgen.electronic_structure.cohp import IcohpCollection, IcohpValue
from pymatgen.util.typing import PathLike
Expand Down Expand Up @@ -92,7 +92,7 @@ def __init__(
filename_icohp (PathLike): Path to ICOHPLIST.lobster or
ICOOPLIST.lobster or ICOBILIST.lobster.
obj_icohp (Icohplist): Icohplist object.
structure (Structure): Typically constructed by Structure.from_file("POSCAR").
structure (Structure): Typically constructed by Structure.from_file("POSCAR").
are_coops (bool): Whether the file is a ICOOPLIST.lobster (True) or a
ICOHPLIST.lobster (False). Only tested for ICOHPLIST.lobster so far.
are_cobis (bool): Whether the file is a ICOBILIST.lobster (True) or
Expand Down Expand Up @@ -431,6 +431,7 @@ def get_info_icohps_to_neighbors(
labels = []
atoms = []
final_isites = []
assert self.Icohpcollection is not None
for idx, _site in enumerate(self.structure):
if idx in isites:
for keys, icohpsum in zip(self.list_keys[idx], self.list_icohps[idx]):
Expand Down Expand Up @@ -557,6 +558,7 @@ def get_info_cohps_to_neighbors(

# Check that the number of bonds in ICOHPLIST and COHPCAR are identical
# TODO: Further checks could be implemented
assert self.Icohpcollection is not None
if len(self.Icohpcollection._list_atom1) != len(self.completecohp.bonds):
raise ValueError("COHPCAR and ICOHPLIST do not fit together")

Expand Down Expand Up @@ -663,6 +665,7 @@ def get_info_icohps_between_neighbors(
number_bonds = 0
labels = []
atoms = []
assert self.Icohpcollection is not None
for isite in isites:
for in_site, n_site in enumerate(self.list_neighsite[isite]):
for in_site2, n_site2 in enumerate(self.list_neighsite[isite]):
Expand Down Expand Up @@ -756,6 +759,7 @@ def _evaluate_ce(
"""
# Get extremum
if lowerlimit is None and upperlimit is None:
assert self.Icohpcollection is not None
limits = self._get_limit_from_extremum(
self.Icohpcollection,
percentage=perc_strength_icohp,
Expand Down Expand Up @@ -790,8 +794,8 @@ def _evaluate_ce(
{
"site": neighbor,
"image": tuple(
int(round(i))
for i in (
int(round(idx))
for idx in (
neighbor.frac_coords
- self.structure[
next(
Expand Down Expand Up @@ -831,8 +835,8 @@ def _evaluate_ce(
{
"site": neighbor,
"image": tuple(
int(round(i))
for i in (
int(round(idx))
for idx in (
neighbor.frac_coords
- self.structure[
next(
Expand Down Expand Up @@ -864,25 +868,34 @@ def _find_environments(
lowerlimit: float,
upperlimit: float,
only_bonds_to: list[str] | None,
) -> tuple[list, list, list, list, list, list]: # TODO: DanielYang: more specific type
) -> tuple[
list[list[IcohpValue]],
list[list[str]],
list[list[float]],
list[list[int]],
list[list[PeriodicNeighbor]],
list[list[NDArray]],
]:
"""Find all relevant neighbors based on certain restrictions.

Args:
additional_condition (int): Additional condition (see above).
lowerlimit (float): Lower limit that tells you which ICOHPs are considered.
upperlimit (float): Upper limit that tells you which ICOHPs are considered.
additional_condition (int): Additional condition.
lowerlimit (float): Lower limit that ICOHPs are considered.
upperlimit (float): Upper limit that ICOHPs are considered.
only_bonds_to (list[str]): Only bonds to these elements will be considered.

Returns:
tuple of ICOHPs, keys, lengths, neighisite, neighsite, coords.
Tuple of ICOHPs, keys, lengths, neighisite, neighsite, coords.
"""
list_icohps: list[list[IcohpValue]] = []
list_keys: list[list[str]] = []
list_lengths: list[list[float]] = []
list_neighisite: list[list[int]] = []
list_neighsite: list[list[PeriodicNeighbor]] = []
list_coords: list[list[NDArray]] = []

# Run over structure
list_neighsite = []
list_neighisite = []
list_coords = []
list_icohps = []
list_lengths = []
list_keys = []
assert self.Icohpcollection is not None
for idx, site in enumerate(self.structure):
icohps = self._get_icohps(
icohpcollection=self.Icohpcollection,
Expand Down Expand Up @@ -979,23 +992,23 @@ def _find_environments(
def _find_relevant_atoms_additional_condition(
self,
isite: int,
icohps, # TODO: need type
icohps: dict[str, IcohpValue],
additional_condition: Literal[0, 1, 2, 3, 4, 5, 6],
) -> tuple[list, list, list, list]: # TODO: more specific type
) -> tuple[list[str], list[float], list[int], list[IcohpValue]]:
"""Find all relevant atoms that fulfill the additional condition.

Args:
isite (int): Index of site in structure (start from 0).
icohps: icohps
additional_condition (int): additional condition.
isite (int): Site index in structure (start from 0).
icohps (dict[str, IcohpValue]): ICOHP values.
additional_condition (int): Additional condition.

Returns:
tuple: keys, lengths, neighbors from selected ICOHPs and selected ICOHPs.
"""
neighbors_from_ICOHPs = []
lengths_from_ICOHPs = []
icohps_from_ICOHPs = []
keys_from_ICOHPs = []
keys_from_ICOHPs: list[str] = []
lengths_from_ICOHPs: list[float] = []
neighbors_from_ICOHPs: list[int] = []
icohps_from_ICOHPs: list[IcohpValue] = []

for key, icohp in icohps.items():
atomnr1 = self._get_atomnumber(icohp._atom1)
Expand All @@ -1008,8 +1021,8 @@ def _find_relevant_atoms_additional_condition(
val1 = self.valences[atomnr1]
val2 = self.valences[atomnr2]

# NO_ADDITIONAL_CONDITION
if additional_condition == 0:
# NO_ADDITIONAL_CONDITION
if atomnr1 == isite:
neighbors_from_ICOHPs.append(atomnr2)
lengths_from_ICOHPs.append(icohp._length)
Expand All @@ -1021,8 +1034,8 @@ def _find_relevant_atoms_additional_condition(
icohps_from_ICOHPs.append(icohp.summed_icohp)
keys_from_ICOHPs.append(key)

# ONLY_ANION_CATION_BONDS
elif additional_condition == 1:
# ONLY_ANION_CATION_BONDS
if (val1 < 0.0 < val2) or (val2 < 0.0 < val1): # type: ignore[operator]
if atomnr1 == isite:
neighbors_from_ICOHPs.append(atomnr2)
Expand All @@ -1036,8 +1049,8 @@ def _find_relevant_atoms_additional_condition(
icohps_from_ICOHPs.append(icohp.summed_icohp)
keys_from_ICOHPs.append(key)

# NO_ELEMENT_TO_SAME_ELEMENT_BONDS
elif additional_condition == 2:
# NO_ELEMENT_TO_SAME_ELEMENT_BONDS
if icohp._atom1.rstrip("0123456789") != icohp._atom2.rstrip("0123456789"):
if atomnr1 == isite:
neighbors_from_ICOHPs.append(atomnr2)
Expand All @@ -1051,8 +1064,8 @@ def _find_relevant_atoms_additional_condition(
icohps_from_ICOHPs.append(icohp.summed_icohp)
keys_from_ICOHPs.append(key)

# ONLY_ANION_CATION_BONDS_AND_NO_ELEMENT_TO_SAME_ELEMENT_BONDS
elif additional_condition == 3:
# ONLY_ANION_CATION_BONDS_AND_NO_ELEMENT_TO_SAME_ELEMENT_BONDS
if ((val1 < 0.0 < val2) or (val2 < 0.0 < val1)) and icohp._atom1.rstrip( # type: ignore[operator]
"0123456789"
) != icohp._atom2.rstrip("0123456789"):
Expand All @@ -1068,8 +1081,8 @@ def _find_relevant_atoms_additional_condition(
icohps_from_ICOHPs.append(icohp.summed_icohp)
keys_from_ICOHPs.append(key)

# ONLY_ELEMENT_TO_OXYGEN_BONDS
elif additional_condition == 4:
# ONLY_ELEMENT_TO_OXYGEN_BONDS
if icohp._atom1.rstrip("0123456789") == "O" or icohp._atom2.rstrip("0123456789") == "O":
if atomnr1 == isite:
neighbors_from_ICOHPs.append(atomnr2)
Expand All @@ -1083,8 +1096,8 @@ def _find_relevant_atoms_additional_condition(
icohps_from_ICOHPs.append(icohp.summed_icohp)
keys_from_ICOHPs.append(key)

# DO_NOT_CONSIDER_ANION_CATION_BONDS
elif additional_condition == 5:
# DO_NOT_CONSIDER_ANION_CATION_BONDS
if (val1 > 0.0 and val2 > 0.0) or (val1 < 0.0 and val2 < 0.0): # type: ignore[operator]
if atomnr1 == isite:
neighbors_from_ICOHPs.append(atomnr2)
Expand All @@ -1098,8 +1111,8 @@ def _find_relevant_atoms_additional_condition(
icohps_from_ICOHPs.append(icohp.summed_icohp)
keys_from_ICOHPs.append(key)

# ONLY_CATION_CATION_BONDS
elif additional_condition == 6 and val1 > 0.0 and val2 > 0.0: # type: ignore[operator]
# ONLY_CATION_CATION_BONDS
if atomnr1 == isite:
neighbors_from_ICOHPs.append(atomnr2)
lengths_from_ICOHPs.append(icohp._length)
Expand All @@ -1126,9 +1139,9 @@ def _get_icohps(

Args:
icohpcollection (IcohpCollection): IcohpCollection object.
isite (int): Index of a site.
lowerlimit (float): Lower limit that tells you which ICOHPs are considered.
upperlimit (float): Upper limit that tells you which ICOHPs are considered.
isite (int): Site index.
lowerlimit (float): Lower limit that ICOHPs are considered.
upperlimit (float): Upper limit that ICOHPs are considered.
only_bonds_to (list[str]): Only bonds to these elements will be considered, e.g. ["O"].

Returns:
Expand Down
Loading
Loading