Skip to content

Commit ed09fe7

Browse files
authored
Deprecate from_string (#3158)
* types in pymatgen/core/periodic_table.py * rename all from_string methods to from_str (for API consistency) add deprecated from_string fallback for backwards compatibility * same thing for to_string -> to_str * fix lint
1 parent 141d3b1 commit ed09fe7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+501
-320
lines changed

pymatgen/alchemy/materials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def from_cif_string(
286286
Returns:
287287
TransformedStructure
288288
"""
289-
parser = CifParser.from_string(cif_string, occupancy_tolerance=occupancy_tolerance)
289+
parser = CifParser.from_str(cif_string, occupancy_tolerance=occupancy_tolerance)
290290
raw_string = re.sub(r"'", '"', cif_string)
291291
cif_dict = parser.as_dict()
292292
cif_keys = list(cif_dict)
@@ -316,7 +316,7 @@ def from_poscar_string(
316316
transformations (list[Transformation]): Sequence of transformations
317317
to be applied to the input structure.
318318
"""
319-
p = Poscar.from_string(poscar_string)
319+
p = Poscar.from_str(poscar_string)
320320
if not p.true_names:
321321
raise ValueError(
322322
"Transformation can be created only from POSCAR strings with proper VASP5 element symbols."

pymatgen/analysis/bond_valence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
# Read in yaml containing data-mined ICSD BV data.
3333
all_data = loadfn(os.path.join(module_dir, "icsd_bv.yaml"))
34-
ICSD_BV_DATA = {Species.from_string(sp): data for sp, data in all_data["bvsum"].items()}
35-
PRIOR_PROB = {Species.from_string(sp): data for sp, data in all_data["occurrence"].items()}
34+
ICSD_BV_DATA = {Species.from_str(sp): data for sp, data in all_data["bvsum"].items()}
35+
PRIOR_PROB = {Species.from_str(sp): data for sp, data in all_data["occurrence"].items()}
3636

3737

3838
def calculate_bv_sum(site, nn_list, scale_factor=1.0):

pymatgen/analysis/dimensionality.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def find_connected_atoms(struct, tolerance=0.45, ldict=None):
418418
# in case of charged species
419419
for ii, item in enumerate(species):
420420
if item not in ldict:
421-
species[ii] = str(Species.from_string(item).element)
421+
species[ii] = str(Species.from_str(item).element)
422422
connected_matrix = np.zeros((n_atoms, n_atoms))
423423

424424
for ii in range(n_atoms):

pymatgen/analysis/reaction_calculator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ def from_dict(cls, d):
247247
products = {Composition(comp): coeff for comp, coeff in d["products"].items()}
248248
return cls(reactants, products)
249249

250+
@classmethod
251+
@np.deprecate(message="Use from_str instead")
252+
def from_string(cls, *args, **kwargs):
253+
return cls.from_str(*args, **kwargs)
254+
250255
@staticmethod
251-
def from_string(rxn_string):
256+
def from_str(rxn_string):
252257
"""
253258
Generates a balanced reaction from a string. The reaction must
254259
already be balanced.

pymatgen/analysis/structure_prediction/substitution_probability.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def __init__(self, lambda_table=None, alpha=-5):
6767
self.species = set()
6868
for row in self._lambda_table:
6969
if "D1+" not in row:
70-
s1 = Species.from_string(row[0])
71-
s2 = Species.from_string(row[1])
70+
s1 = Species.from_str(row[0])
71+
s2 = Species.from_str(row[1])
7272
self.species.add(s1)
7373
self.species.add(s2)
7474
self._l[frozenset([s1, s2])] = float(row[2])

pymatgen/analysis/tests/test_dimensionality.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ def test_get_structure_components(self):
8989
assert list(components[0]["structure_graph"].graph.nodes()) == [0, 1]
9090

9191
def test_calculate_dimensionality_of_site(self):
92-
dimen = calculate_dimensionality_of_site(self.tricky_structure, 0)
93-
assert dimen == 3
92+
dim = calculate_dimensionality_of_site(self.tricky_structure, 0)
93+
assert dim == 3
9494

9595
# test vertices returned correctly
96-
dimen, vertices = calculate_dimensionality_of_site(self.cscl, 0, inc_vertices=True)
97-
assert dimen == 3
96+
dim, vertices = calculate_dimensionality_of_site(self.cscl, 0, inc_vertices=True)
97+
assert dim == 3
9898
assert len(vertices) == 4
9999

100100
def test_zero_d_to_molecule_graph(self):

pymatgen/analysis/tests/test_reaction_calculator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def test_scientific_notation(self):
160160
reactants = [Composition("FePO4")]
161161
rxn = Reaction(reactants, products)
162162
assert str(rxn) == "FePO4 -> Fe1P1O3.9999 + 5e-05 O2"
163-
assert rxn == Reaction.from_string(str(rxn))
163+
assert rxn == Reaction.from_str(str(rxn))
164164

165-
rxn2 = Reaction.from_string("FePO4 + 20 CO -> 1e1 O2 + Fe1P1O4 + 20 C")
165+
rxn2 = Reaction.from_str("FePO4 + 20 CO -> 1e1 O2 + Fe1P1O4 + 20 C")
166166
assert str(rxn2) == "20 CO -> 20 C + 10 O2"
167167

168168
def test_equals(self):
@@ -322,9 +322,9 @@ def test_to_from_dict(self):
322322
for comp in new_rxn.all_comp:
323323
assert new_rxn.get_coeff(comp) == rxn.get_coeff(comp)
324324

325-
def test_from_string(self):
325+
def test_from_str(self):
326326
rxn = BalancedReaction({Composition("Li"): 4, Composition("O2"): 1}, {Composition("Li2O"): 2})
327-
assert rxn == BalancedReaction.from_string("4 Li + O2 -> 2Li2O")
327+
assert rxn == BalancedReaction.from_str("4 Li + O2 -> 2Li2O")
328328

329329
rxn = BalancedReaction(
330330
{Composition("Li(NiO2)3"): 1},
@@ -335,7 +335,7 @@ def test_from_string(self):
335335
},
336336
)
337337

338-
assert rxn == BalancedReaction.from_string("1.000 Li(NiO2)3 -> 0.500 O2 + 1.000 Li(NiO2)2 + 1.000 NiO")
338+
assert rxn == BalancedReaction.from_str("1.000 Li(NiO2)3 -> 0.500 O2 + 1.000 Li(NiO2)2 + 1.000 NiO")
339339

340340
def test_remove_spectator_species(self):
341341
rxn = BalancedReaction(

pymatgen/cli/pmg_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def setup_cp2k_data(cp2k_data_dirs: list[str]) -> None:
5252
continue
5353
for chk in chunks:
5454
try:
55-
potential = GthPotential.from_string(chk)
55+
potential = GthPotential.from_str(chk)
5656
potential.filename = os.path.basename(potential_file)
5757
potential.version = None
5858
settings[potential.element.symbol]["potentials"][potential.get_hash()] = jsanitize(
@@ -74,7 +74,7 @@ def setup_cp2k_data(cp2k_data_dirs: list[str]) -> None:
7474
continue
7575
for chk in chunks:
7676
try:
77-
basis = GaussianTypeOrbitalBasisSet.from_string(chk)
77+
basis = GaussianTypeOrbitalBasisSet.from_str(chk)
7878
basis.filename = os.path.basename(basis_file)
7979
settings[basis.element.symbol]["basis_sets"][basis.get_hash()] = jsanitize( # type: ignore
8080
basis, strict=True

pymatgen/command_line/enumlib_caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def _get_structures(self, num_structs):
367367
data = f.read()
368368
data = re.sub(r"scale factor", "1", data)
369369
data = re.sub(r"(\d+)-(\d+)", r"\1 -\2", data)
370-
poscar = Poscar.from_string(data, self.index_species)
370+
poscar = Poscar.from_str(data, self.index_species)
371371
sub_structure = poscar.structure
372372
# Enumeration may have resulted in a super lattice. We need to
373373
# find the mapping from the new lattice to the old lattice, and

pymatgen/core/composition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def _get_oxid_state_guesses(self, all_oxi_states, max_sites, oxi_states_override
921921
if not Composition.oxi_prob:
922922
module_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
923923
all_data = loadfn(os.path.join(module_dir, "..", "analysis", "icsd_bv.yaml"))
924-
Composition.oxi_prob = {Species.from_string(sp): data for sp, data in all_data["occurrence"].items()}
924+
Composition.oxi_prob = {Species.from_str(sp): data for sp, data in all_data["occurrence"].items()}
925925
oxi_states_override = oxi_states_override or {}
926926
# assert: Composition only has integer amounts
927927
if not all(amt == int(amt) for amt in comp.values()):

0 commit comments

Comments
 (0)