Skip to content

Commit b0e4eb2

Browse files
authored
Return self in SiteCollection spin/oxi state add/remove methods (#3573)
* return self in SiteCollection.remove_oxidation_states() * test_remove_oxidation_states() assert struct_out is struct_specie refactor existing tests to use new return value * same for add_oxidation_state_by_guess(), add_spin_by_element(), add_spin_by_site(), remove_spin() * add tests * doc str tweaks
1 parent f9e2830 commit b0e4eb2

File tree

15 files changed

+77
-73
lines changed

15 files changed

+77
-73
lines changed

pymatgen/analysis/local_env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ValenceIonicRadiusEvaluator:
6868
def __init__(self, structure: Structure) -> None:
6969
"""
7070
Args:
71-
structure: pymatgen.core.structure.Structure.
71+
structure: pymatgen Structure.
7272
"""
7373
self._structure = structure.copy()
7474
self._valences = self._get_valences()

pymatgen/analysis/structure_analyzer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,7 @@ def sulfide_type(structure):
518518
Returns:
519519
(str) sulfide/polysulfide or None if structure is a sulfate.
520520
"""
521-
structure = structure.copy()
522-
structure.remove_oxidation_states()
521+
structure = structure.copy().remove_oxidation_states()
523522
sulphur = Element("S")
524523
comp = structure.composition
525524
if comp.is_element or sulphur not in comp:

pymatgen/command_line/gulp_caller.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def specie_potential_lines(structure, potential, **kwargs):
317317
structure.
318318
319319
Args:
320-
structure: pymatgen.core.structure.Structure object
320+
structure: pymatgen Structure object
321321
potential: String specifying the type of potential used
322322
kwargs: Additional parameters related to potential. For
323323
potential == "buckingham",
@@ -375,7 +375,7 @@ def buckingham_input(self, structure: Structure, keywords, library=None, uc=True
375375
from library.
376376
377377
Args:
378-
structure: pymatgen.core.structure.Structure
378+
structure: pymatgen Structure
379379
keywords: GULP first line keywords.
380380
library (Default=None): File containing the species and potential.
381381
uc (Default=True): Unit Cell Flag.
@@ -401,7 +401,7 @@ def buckingham_potential(structure, val_dict=None):
401401
J. Mater Chem., 4, 831-837 (1994)
402402
403403
Args:
404-
structure: pymatgen.core.structure.Structure
404+
structure: pymatgen Structure
405405
val_dict (Needed if structure is not charge neutral): {El:valence}
406406
dict, where El is element.
407407
"""
@@ -462,7 +462,7 @@ def tersoff_input(self, structure: Structure, periodic=False, uc=True, *keywords
462462
"""Gets a GULP input with Tersoff potential for an oxide structure.
463463
464464
Args:
465-
structure: pymatgen.core.structure.Structure
465+
structure: pymatgen Structure
466466
periodic (Default=False): Flag denoting whether periodic
467467
boundary conditions are used
468468
library (Default=None): File containing the species and potential.
@@ -487,7 +487,7 @@ def tersoff_potential(structure):
487487
"""Generate the species, Tersoff potential lines for an oxide structure.
488488
489489
Args:
490-
structure: pymatgen.core.structure.Structure
490+
structure: pymatgen Structure
491491
"""
492492
bv = BVAnalyzer()
493493
el = [site.specie.symbol for site in structure]
@@ -702,7 +702,7 @@ def get_energy_tersoff(structure, gulp_cmd="gulp"):
702702
"""Compute the energy of a structure using Tersoff potential.
703703
704704
Args:
705-
structure: pymatgen.core.structure.Structure
705+
structure: pymatgen Structure
706706
gulp_cmd: GULP command if not in standard place
707707
"""
708708
gio = GulpIO()
@@ -716,7 +716,7 @@ def get_energy_buckingham(structure, gulp_cmd="gulp", keywords=("optimise", "con
716716
"""Compute the energy of a structure using Buckingham potential.
717717
718718
Args:
719-
structure: pymatgen.core.structure.Structure
719+
structure: pymatgen Structure
720720
gulp_cmd: GULP command if not in standard place
721721
keywords: GULP first line keywords
722722
valence_dict: {El: valence}. Needed if the structure is not charge
@@ -733,7 +733,7 @@ def get_energy_relax_structure_buckingham(structure, gulp_cmd="gulp", keywords=(
733733
"""Relax a structure and compute the energy using Buckingham potential.
734734
735735
Args:
736-
structure: pymatgen.core.structure.Structure
736+
structure: pymatgen Structure
737737
gulp_cmd: GULP command if not in standard place
738738
keywords: GULP first line keywords
739739
valence_dict: {El: valence}. Needed if the structure is not charge

pymatgen/core/interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class Interface(Structure):
2121
"""This class stores data for defining an interface between two structures.
22-
It is a subclass of pymatgen.core.structure.Structure.
22+
It is a subclass of pymatgen Structure.
2323
"""
2424

2525
def __init__(

pymatgen/core/structure.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def add_oxidation_state_by_site(self, oxidation_states: list[float]) -> None:
578578
new_sp[Species(sym, ox)] = occu
579579
site.species = Composition(new_sp)
580580

581-
def remove_oxidation_states(self) -> None:
581+
def remove_oxidation_states(self) -> SiteCollection:
582582
"""Removes oxidation states from a structure."""
583583
for site in self:
584584
new_sp: dict[Element, float] = collections.defaultdict(float)
@@ -587,7 +587,9 @@ def remove_oxidation_states(self) -> None:
587587
new_sp[Element(sym)] += occu
588588
site.species = Composition(new_sp)
589589

590-
def add_oxidation_state_by_guess(self, **kwargs) -> None:
590+
return self
591+
592+
def add_oxidation_state_by_guess(self, **kwargs) -> SiteCollection:
591593
"""Decorates the structure with oxidation state, guessing
592594
using Composition.oxi_state_guesses(). If multiple guesses are found
593595
we take the first one.
@@ -599,7 +601,9 @@ def add_oxidation_state_by_guess(self, **kwargs) -> None:
599601
oxi_guess = oxi_guess or [{e.symbol: 0 for e in self.composition}]
600602
self.add_oxidation_state_by_element(oxi_guess[0])
601603

602-
def add_spin_by_element(self, spins: dict[str, float]) -> None:
604+
return self
605+
606+
def add_spin_by_element(self, spins: dict[str, float]) -> SiteCollection:
603607
"""Add spin states to structure.
604608
605609
Args:
@@ -615,7 +619,9 @@ def add_spin_by_element(self, spins: dict[str, float]) -> None:
615619
new_species[species] = occu
616620
site.species = Composition(new_species)
617621

618-
def add_spin_by_site(self, spins: Sequence[float]) -> None:
622+
return self
623+
624+
def add_spin_by_site(self, spins: Sequence[float]) -> SiteCollection:
619625
"""Add spin states to structure by site.
620626
621627
Args:
@@ -632,7 +638,9 @@ def add_spin_by_site(self, spins: Sequence[float]) -> None:
632638
new_species[Species(sym, oxidation_state=oxi_state, spin=spin)] = occu
633639
site.species = Composition(new_species)
634640

635-
def remove_spin(self) -> None:
641+
return self
642+
643+
def remove_spin(self) -> SiteCollection:
636644
"""Remove spin states from structure."""
637645
for site in self:
638646
new_sp: dict[Element, float] = collections.defaultdict(float)
@@ -641,6 +649,8 @@ def remove_spin(self) -> None:
641649
new_sp[Species(sp.symbol, oxidation_state=oxi_state)] += occu
642650
site.species = Composition(new_sp)
643651

652+
return self
653+
644654
def extract_cluster(self, target_sites: list[Site], **kwargs) -> list[Site]:
645655
"""Extracts a cluster of atoms based on bond lengths.
646656

pymatgen/ext/cod.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def query(self, sql: str) -> str:
5252
Returns:
5353
Response from SQL query.
5454
"""
55-
r = subprocess.check_output(["mysql", "-u", "cod_reader", "-h", self.url, "-e", sql, "cod"])
56-
return r.decode("utf-8")
55+
resp = subprocess.check_output(["mysql", "-u", "cod_reader", "-h", self.url, "-e", sql, "cod"])
56+
return resp.decode("utf-8")
5757

5858
@requires(which("mysql"), "mysql must be installed to use this query.")
5959
def get_cod_ids(self, formula):
@@ -84,8 +84,7 @@ def get_structure_by_id(self, cod_id, **kwargs):
8484
8585
Args:
8686
cod_id (int): COD id.
87-
kwargs: All kwargs supported by
88-
:func:`pymatgen.core.structure.Structure.from_str`.
87+
kwargs: All kwargs supported by Structure.from_str.
8988
9089
Returns:
9190
A Structure.
@@ -100,8 +99,7 @@ def get_structure_by_formula(self, formula: str, **kwargs) -> list[dict[str, str
10099
101100
Args:
102101
formula (str): Chemical formula.
103-
kwargs: All kwargs supported by
104-
:func:`pymatgen.core.structure.Structure.from_str`.
102+
kwargs: All kwargs supported by Structure.from_str.
105103
106104
Returns:
107105
A list of dict of the format [{"structure": Structure, "cod_id": int, "sg": "P n m a"}]

pymatgen/io/ase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def get_structure(atoms: Atoms, cls: type[Structure] = Structure, **cls_kwargs)
183183
**cls_kwargs: Any additional kwargs to pass to the cls
184184
185185
Returns:
186-
Equivalent pymatgen.core.structure.Structure
186+
Structure: Equivalent pymatgen Structure
187187
"""
188188
symbols = atoms.get_chemical_symbols()
189189
positions = atoms.get_positions()

pymatgen/io/cp2k/utils.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def atoi(t):
120120
return [atoi(c) for c in re.split(r"_(\d+)", text)]
121121

122122

123-
def get_unique_site_indices(structure: Structure | Molecule):
123+
def get_unique_site_indices(struct: Structure | Molecule):
124124
"""
125125
Get unique site indices for a structure according to site properties. Whatever site-property
126126
has the most unique values is used for indexing.
@@ -147,31 +147,27 @@ def get_unique_site_indices(structure: Structure | Molecule):
147147
"aux_basis",
148148
}
149149

150-
for site in structure:
150+
for site in struct:
151151
for sp in site.species:
152152
oxi_states.append(getattr(sp, "oxi_state", 0))
153153
spins.append(getattr(sp, "_properties", {}).get("spin", 0))
154154

155-
structure.add_site_property("oxi_state", oxi_states)
156-
structure.add_site_property("spin", spins)
157-
structure.remove_oxidation_states()
155+
struct.add_site_property("oxi_state", oxi_states)
156+
struct.add_site_property("spin", spins)
157+
struct.remove_oxidation_states()
158158
items = [
159159
(
160160
site.species_string,
161-
*[
162-
structure.site_properties[k][i]
163-
for k in structure.site_properties
164-
if k.lower() in parsable_site_properties
165-
],
161+
*[struct.site_properties[k][i] for k in struct.site_properties if k.lower() in parsable_site_properties],
166162
)
167-
for i, site in enumerate(structure)
163+
for i, site in enumerate(struct)
168164
]
169165
unique_itms = list(set(items))
170166
_sites: dict[tuple, list] = {u: [] for u in unique_itms}
171167
for i, itm in enumerate(items):
172168
_sites[itm].append(i)
173169
sites = {}
174-
nums = {s: 1 for s in structure.symbol_set}
170+
nums = {s: 1 for s in struct.symbol_set}
175171
for s in _sites:
176172
sites[f"{s[0]}_{nums[s[0]]}"] = _sites[s]
177173
nums[s[0]] += 1

pymatgen/io/jarvis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_atoms(structure):
2222
Returns JARVIS Atoms object from pymatgen structure.
2323
2424
Args:
25-
structure: pymatgen.core.structure.Structure
25+
structure: pymatgen Structure
2626
2727
Returns:
2828
JARVIS Atoms object
@@ -49,7 +49,7 @@ def get_structure(atoms):
4949
atoms: JARVIS Atoms object
5050
5151
Returns:
52-
Equivalent pymatgen.core.structure.Structure
52+
Equivalent pymatgen Structure
5353
"""
5454
return Structure(
5555
lattice=atoms.lattice_mat, species=atoms.elements, coords=atoms.frac_coords, coords_are_cartesian=False

pymatgen/io/zeopp.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def get_voronoi_nodes(structure, rad_dict=None, probe_rad=0.1):
219219
Calls Zeo++ for Voronoi decomposition.
220220
221221
Args:
222-
structure: pymatgen.core.structure.Structure
222+
structure: pymatgen Structure
223223
rad_dict (optional): Dictionary of radii of elements in structure.
224224
If not given, Zeo++ default values are used.
225225
Note: Zeo++ uses atomic radii of elements.
@@ -228,10 +228,9 @@ def get_voronoi_nodes(structure, rad_dict=None, probe_rad=0.1):
228228
0.1 A
229229
230230
Returns:
231-
voronoi nodes as pymatgen.core.structure.Structure within the
232-
unit cell defined by the lattice of input structure
233-
voronoi face centers as pymatgen.core.structure.Structure within the
234-
unit cell defined by the lattice of input structure
231+
voronoi nodes as pymatgen Structure within the unit cell defined by the lattice of
232+
input structure voronoi face centers as pymatgen Structure within the unit cell
233+
defined by the lattice of input structure
235234
"""
236235
with ScratchDir("."):
237236
name = "temp_zeo1"
@@ -306,7 +305,7 @@ def get_high_accuracy_voronoi_nodes(structure, rad_dict, probe_rad=0.1):
306305
Calls Zeo++ for Voronoi decomposition.
307306
308307
Args:
309-
structure: pymatgen.core.structure.Structure
308+
structure: pymatgen Structure
310309
rad_dict (optional): Dictionary of radii of elements in structure.
311310
If not given, Zeo++ default values are used.
312311
Note: Zeo++ uses atomic radii of elements.
@@ -315,9 +314,9 @@ def get_high_accuracy_voronoi_nodes(structure, rad_dict, probe_rad=0.1):
315314
Default is 0.1 A
316315
317316
Returns:
318-
voronoi nodes as pymatgen.core.structure.Structure within the
317+
voronoi nodes as pymatgen Structure within the
319318
unit cell defined by the lattice of input structure
320-
voronoi face centers as pymatgen.core.structure.Structure within the
319+
voronoi face centers as pymatgen Structure within the
321320
unit cell defined by the lattice of input structure
322321
"""
323322
with ScratchDir("."):
@@ -368,7 +367,7 @@ def get_free_sphere_params(structure, rad_dict=None, probe_rad=0.1):
368367
Calls Zeo++ for Voronoi decomposition.
369368
370369
Args:
371-
structure: pymatgen.core.structure.Structure
370+
structure: pymatgen Structure
372371
rad_dict (optional): Dictionary of radii of elements in structure.
373372
If not given, Zeo++ default values are used.
374373
Note: Zeo++ uses atomic radii of elements.
@@ -377,9 +376,9 @@ def get_free_sphere_params(structure, rad_dict=None, probe_rad=0.1):
377376
0.1 A
378377
379378
Returns:
380-
voronoi nodes as pymatgen.core.structure.Structure within the
379+
voronoi nodes as pymatgen Structure within the
381380
unit cell defined by the lattice of input structure
382-
voronoi face centers as pymatgen.core.structure.Structure within the
381+
voronoi face centers as pymatgen Structure within the
383382
unit cell defined by the lattice of input structure
384383
"""
385384
with ScratchDir("."):

0 commit comments

Comments
 (0)