Skip to content

Commit 7b74fd1

Browse files
authored
Merge branch 'master' into pgh-ruff-rules
2 parents 59832d2 + b19508e commit 7b74fd1

File tree

7 files changed

+61
-29
lines changed

7 files changed

+61
-29
lines changed

pymatgen/core/composition.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,12 @@ def oxi_state_guesses(
767767
element's common oxidation states, e.g. {"V": [2,3,4,5]}
768768
target_charge (int): the desired total charge on the structure.
769769
Default is 0 signifying charge balance.
770-
all_oxi_states (bool): if True, an element defaults to
771-
all oxidation states in pymatgen Element.icsd_oxidation_states.
772-
Otherwise, default is Element.common_oxidation_states. Note
773-
that the full oxidation state list is *very* inclusive and
774-
can produce nonsensical results.
770+
all_oxi_states (bool): if True, all oxidation states of an element, even rare ones, are used in the search
771+
for guesses. However, the full oxidation state list is *very* inclusive and can produce nonsensical
772+
results. If False, the icsd_oxidation_states list is used when present, or the common_oxidation_states
773+
is used when icsd_oxidation_states is not present. These oxidation states lists comprise more
774+
commonly occurring oxidation states and results in more reliable guesses, albeit at the cost of
775+
missing some uncommon situations. The default is False.
775776
max_sites (int): if possible, will reduce Compositions to at most
776777
this many sites to speed up oxidation state guesses. If the
777778
composition cannot be reduced to this many sites a ValueError
@@ -859,11 +860,12 @@ def add_charges_from_oxi_state_guesses(
859860
element's common oxidation states, e.g. {"V": [2, 3, 4, 5]}
860861
target_charge (float): the desired total charge on the structure.
861862
Default is 0 signifying charge balance.
862-
all_oxi_states (bool): If True, an element defaults to
863-
all oxidation states in pymatgen Element.icsd_oxidation_states.
864-
Otherwise, default is Element.common_oxidation_states. Note
865-
that the full oxidation state list is *very* inclusive and
866-
can produce nonsensical results.
863+
all_oxi_states (bool): if True, all oxidation states of an element, even rare ones, are used in the search
864+
for guesses. However, the full oxidation state list is *very* inclusive and can produce nonsensical
865+
results. If False, the icsd_oxidation_states list is used when present, or the common_oxidation_states
866+
is used when icsd_oxidation_states is not present. These oxidation states lists comprise more
867+
commonly occurring oxidation states and results in more reliable guesses, albeit at the cost of
868+
missing some uncommon situations. The default is False.
867869
max_sites (int): If possible, will reduce Compositions to at most
868870
this many sites to speed up oxidation state guesses. If the
869871
composition cannot be reduced to this many sites a ValueError
@@ -916,15 +918,15 @@ def _get_oxi_state_guesses(
916918
calculation of the most likely oxidation states
917919
918920
Args:
919-
oxi_states_override (dict): dict of str->list to override an
920-
element's common oxidation states, e.g. {"V": [2,3,4,5]}
921-
target_charge (float): the desired total charge on the structure.
922-
Default is 0 signifying charge balance.
923-
all_oxi_states (bool): if True, an element defaults to
924-
all oxidation states in pymatgen Element.icsd_oxidation_states.
925-
Otherwise, default is Element.common_oxidation_states. Note
926-
that the full oxidation state list is *very* inclusive and
927-
can produce nonsensical results.
921+
oxi_states_override (dict): dict of str->list to override an element's common oxidation states, e.g.
922+
{"V": [2,3,4,5]}.
923+
target_charge (float): the desired total charge on the structure. Default is 0 signifying charge balance.
924+
all_oxi_states (bool): if True, all oxidation states of an element, even rare ones, are used in the search
925+
for guesses. However, the full oxidation state list is *very* inclusive and can produce nonsensical
926+
results. If False, the icsd_oxidation_states list is used when present, or the common_oxidation_states
927+
is used when icsd_oxidation_states is not present. These oxidation states lists comprise more
928+
commonly occurring oxidation states and results in more reliable guesses, albeit at the cost of
929+
missing some uncommon situations. The default is False.
928930
max_sites (int): if possible, will reduce Compositions to at most
929931
this many sites to speed up oxidation state guesses. If the
930932
composition cannot be reduced to this many sites a ValueError
@@ -980,7 +982,7 @@ def _get_oxi_state_guesses(
980982
elif all_oxi_states:
981983
oxids = Element(el).oxidation_states
982984
else:
983-
oxids = Element(el).icsd_oxidation_states or Element(el).oxidation_states
985+
oxids = Element(el).icsd_oxidation_states or Element(el).common_oxidation_states
984986

985987
# Get all possible combinations of oxidation states
986988
# and sum each combination

pymatgen/core/structure.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from pymatgen.util.typing import CompositionLike, MillerIndex, PathLike, PbcLike, SpeciesLike
6565

6666
FileFormats = Literal["cif", "poscar", "cssr", "json", "yaml", "yml", "xsf", "mcsqs", "res", "pwmat", ""]
67+
StructureSources = Literal["Materials Project", "COD"]
6768

6869

6970
class Neighbor(Site):
@@ -2937,6 +2938,28 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
29372938
writer.write_file(filename)
29382939
return str(writer)
29392940

2941+
@classmethod
2942+
def from_id(cls, id: str, source: StructureSources = "Materials Project", **kwargs) -> Structure:
2943+
"""
2944+
Load a structure file based on an id, usually from an online source.
2945+
2946+
Args:
2947+
id: The id. E.g., the materials project id.
2948+
source: Source of the data. Defaults to "Materials Project".
2949+
**kwargs: Pass-through to any API calls.
2950+
"""
2951+
if source == "Materials Project":
2952+
from pymatgen.ext.matproj import MPRester
2953+
2954+
mpr = MPRester(**kwargs)
2955+
return mpr.get_structure_by_material_id(id) # type: ignore
2956+
if source == "COD":
2957+
from pymatgen.ext.cod import COD
2958+
2959+
cod = COD()
2960+
return cod.get_structure_by_id(int(id))
2961+
raise ValueError(f"Invalid source: {source}")
2962+
29402963
@classmethod
29412964
def from_str( # type: ignore[override]
29422965
cls,

pymatgen/ext/matproj.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def get_structure_by_material_id(self, material_id: str, conventional_unit_cell:
229229
Structure object.
230230
"""
231231
prop = "structure"
232-
response = self.request(f"materials/summary/{material_id}/?_fields={prop}")
232+
response = self.request(f"materials/summary?material_ids={material_id}&_fields={prop}")
233233
structure = response[0][prop]
234234
if conventional_unit_cell:
235235
return SpacegroupAnalyzer(structure).get_conventional_standard_structure()

pymatgen/util/testing/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
"""Common test support for pymatgen test scripts.
1+
"""This module implements testing utilities for materials science codes.
22
3-
This single module should provide all the common functionality for pymatgen
4-
tests in a single location, so that test scripts can just import it and work
5-
right away.
3+
While the primary use is within pymatgen, the functionality is meant to be useful for external materials science
4+
codes as well. For instance, obtaining example crystal structures to perform tests, specialized assert methods for
5+
materials science, etc.
66
"""
77

88
from __future__ import annotations

requirements-optional.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ h5py==3.11.0
99
# hiphive>=0.6
1010
icet>=2.2
1111
jarvis-tools>=2022.9.16
12-
matgl==1.1.1
12+
matgl==1.1.2
1313
netCDF4>=1.5.8
1414
phonopy==2.23.1
1515
seekpath>=2.0.1

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
numpy==1.26.4
22
sympy==1.12
3-
requests==2.32.0
3+
requests==2.32.3
44
monty==2024.5.24
55
ruamel.yaml==0.18.6
6-
scipy==1.11.3
6+
scipy==1.13.1
77
tabulate==0.9.0
88
matplotlib==3.8.0
99
palettable==3.3.3
@@ -12,7 +12,7 @@ pandas==2.1.1
1212
networkx==3.3
1313
plotly==5.17.0
1414
uncertainties==3.1.7
15-
Cython==3.0.2
15+
Cython==3.0.10
1616
pybtex==0.24.0
1717
tqdm==4.66.4
1818
joblib==1.3.2

tests/core/test_structure.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,13 @@ def setUp(self):
924924
self.disordered = Structure.from_spacegroup("Im-3m", Lattice.cubic(3), [Composition("Fe0.5Mn0.5")], [[0, 0, 0]])
925925
self.labeled_structure = Structure(lattice, ["Si", "Si"], coords, labels=["Si1", "Si2"])
926926

927+
def test_from_id(self):
928+
s = Structure.from_id("mp-1143")
929+
assert isinstance(s, Structure)
930+
assert s.reduced_formula == "Al2O3"
931+
s = Structure.from_id("1101077", source="COD")
932+
assert s.reduced_formula == "LiV2O4"
933+
927934
def test_mutable_sequence_methods(self):
928935
struct = self.struct
929936
struct[0] = "Fe"

0 commit comments

Comments
 (0)