Skip to content

Commit c945071

Browse files
committed
test return type and reduce function calls in test_plot_periodic_heatmap()
1 parent c5c1804 commit c945071

File tree

10 files changed

+73
-72
lines changed

10 files changed

+73
-72
lines changed

pymatgen/io/abinit/pseudos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ def as_dict(self, **kwargs):
16931693
# k, count = p.element, 1
16941694
# Handle multiple-pseudos with the same name!
16951695
while k in dct:
1696-
k += k.split("#")[0] + "#" + str(count)
1696+
k += f"{k.split('#')[0]}#{count}"
16971697
count += 1
16981698
dct.update({k: p.as_dict()})
16991699
dct["@module"] = type(self).__module__

pymatgen/io/abinit/tests/test_abiobjects.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ def test_base(self):
136136

137137
assert fd1ev
138138

139-
same_fd = Smearing.as_smearing("fermi_dirac:" + str(1.0 / Ha_to_eV))
139+
same_fd = Smearing.as_smearing(f"fermi_dirac:{1.0 / Ha_to_eV}")
140140

141141
assert same_fd == fd1ev
142142

143-
nosmear = Smearing.nosmearing()
144-
assert nosmear == Smearing.as_smearing("nosmearing")
143+
no_smear = Smearing.nosmearing()
144+
assert no_smear == Smearing.as_smearing("nosmearing")
145145

146-
assert not nosmear
147-
assert nosmear != fd1ev
148-
self.assert_msonable(nosmear)
146+
assert not no_smear
147+
assert no_smear != fd1ev
148+
self.assert_msonable(no_smear)
149149

150150
new_fd1ev = Smearing.from_dict(fd1ev.as_dict())
151151
assert new_fd1ev == fd1ev
@@ -227,7 +227,7 @@ def test_base(self):
227227
godby.to_abivars()
228228
assert godby
229229

230-
same_godby = PPModel.as_ppmodel("godby:" + str(12.0 / Ha_to_eV))
230+
same_godby = PPModel.as_ppmodel(f"godby:{12.0 / Ha_to_eV}")
231231
assert same_godby == godby
232232

233233
no_pp_model = PPModel.get_noppmodel()

pymatgen/io/abinit/tests/test_netcdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_read_Si2(self):
8181

8282
data.print_tree()
8383
for group in data.walk_tree():
84-
print("group: " + str(group))
84+
print(f"{group=}")
8585

8686
# Initialize pymatgen structure from GSR.
8787
structure = data.read_structure()

pymatgen/io/feff/outputs.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,25 +200,25 @@ def charge_transfer_from_file(feff_inp_file, ldos_file):
200200
dicts = Potential.pot_dict_from_string(pot_string)
201201
pot_dict = dicts[1]
202202

203-
for i in range(0, len(dicts[0]) + 1):
204-
if len(str(i)) == 1:
205-
with zopen(f"{ldos_file}0{i}.dat", "rt") as fobject:
206-
f = fobject.readlines()
207-
s = float(f[3].split()[2])
208-
p = float(f[4].split()[2])
209-
d = float(f[5].split()[2])
210-
f1 = float(f[6].split()[2])
211-
tot = float(f[1].split()[4])
212-
cht[str(i)] = {pot_dict[i]: {"s": s, "p": p, "d": d, "f": f1, "tot": tot}}
203+
for idx in range(len(dicts[0]) + 1):
204+
if len(str(idx)) == 1:
205+
with zopen(f"{ldos_file}0{idx}.dat", "rt") as file:
206+
lines = file.readlines()
207+
s = float(lines[3].split()[2])
208+
p = float(lines[4].split()[2])
209+
d = float(lines[5].split()[2])
210+
f1 = float(lines[6].split()[2])
211+
tot = float(lines[1].split()[4])
212+
cht[str(idx)] = {pot_dict[idx]: {"s": s, "p": p, "d": d, "f": f1, "tot": tot}}
213213
else:
214-
with zopen(ldos_file + str(i) + ".dat", "rt") as fid:
215-
f = fid.readlines()
216-
s = float(f[3].split()[2])
217-
p = float(f[4].split()[2])
218-
d = float(f[5].split()[2])
219-
f1 = float(f[6].split()[2])
220-
tot = float(f[1].split()[4])
221-
cht[str(i)] = {pot_dict[i]: {"s": s, "p": p, "d": d, "f": f1, "tot": tot}}
214+
with zopen(f"{ldos_file}{idx}.dat", "rt") as file:
215+
lines = file.readlines()
216+
s = float(lines[3].split()[2])
217+
p = float(lines[4].split()[2])
218+
d = float(lines[5].split()[2])
219+
f1 = float(lines[6].split()[2])
220+
tot = float(lines[1].split()[4])
221+
cht[str(idx)] = {pot_dict[idx]: {"s": s, "p": p, "d": d, "f": f1, "tot": tot}}
222222

223223
return cht
224224

pymatgen/io/lobster/inputs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def write_lobsterin(self, path="lobsterin", overwritedict=None):
267267
for key in Lobsterin.AVAILABLEKEYWORDS:
268268
if key.lower() in [element.lower() for element in self]:
269269
if key.lower() in [element.lower() for element in Lobsterin.FLOAT_KEYWORDS]:
270-
f.write(key + " " + str(self.get(key)) + "\n")
270+
f.write(f"{key} {self.get(key)}\n")
271271
elif key.lower() in [element.lower() for element in Lobsterin.BOOLEAN_KEYWORDS]:
272272
# checks if entry is True or False
273273
for key_here in self:
@@ -276,10 +276,10 @@ def write_lobsterin(self, path="lobsterin", overwritedict=None):
276276
if self.get(new_key):
277277
f.write(key + "\n")
278278
elif key.lower() in [element.lower() for element in Lobsterin.STRING_KEYWORDS]:
279-
f.write(key + " " + str(self.get(key) + "\n"))
279+
f.write(f"{key} {self.get(key)}\n")
280280
elif key.lower() in [element.lower() for element in Lobsterin.LISTKEYWORDS]:
281281
for entry in self.get(key):
282-
f.write(key + " " + str(entry) + "\n")
282+
f.write(f"{key} {entry}\n")
283283

284284
def as_dict(self):
285285
""":return: MSONable dict"""

pymatgen/io/pwscf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def __str__(self):
9797
name = k
9898

9999
if name is None:
100-
name = site.specie.symbol + str(c)
100+
name = f"{site.specie.symbol}{c}"
101101
site_descriptions[name] = site.properties
102102
c += 1
103103

pymatgen/transformations/standard_transformations.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ def apply_transformation(self, structure: Structure) -> Structure:
310310
return struct
311311

312312
def __str__(self):
313-
return "Substitution Transformation :" + ", ".join(
314-
[str(k) + "->" + str(v) for k, v in self._species_map.items()]
315-
)
313+
return "Substitution Transformation :" + ", ".join([f"{k}->{v}" for k, v in self._species_map.items()])
316314

317315
def __repr__(self):
318316
return str(self)

pymatgen/util/convergence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ def multi_reciprocal_extra(xs, ys, noise=False):
441441
def print_plot_line(function, popt, xs, ys, name, tol: float = 0.05, extra=""):
442442
"""Print the gnuplot command line to plot the x, y data with the fitted function using the popt parameters."""
443443
idp = id_generator()
444-
with open("convdat." + str(idp), mode="w") as f:
444+
with open(f"convdat.{idp}", mode="w") as f:
445445
for n in range(0, len(ys), 1):
446-
f.write(str(xs[n]) + " " + str(ys[n]) + "\n")
446+
f.write(f"{xs[n]} {ys[n]}\n")
447447
tol = abs(tol)
448448
line = f"plot 'convdat.{idp}' pointsize 4 lt 0, "
449449
line += f"{popt[0]} lt 3, {popt[0] - tol} lt 4, {popt[0] + tol} lt 4, "

pymatgen/util/string.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,27 @@ def transformation_to_string(matrix, translation_vec=(0, 0, 0), components=("x",
300300
:return: xyz string.
301301
"""
302302
parts = []
303-
for i in range(3):
304-
s = ""
305-
m = matrix[i]
306-
t = translation_vec[i]
303+
for idx in range(3):
304+
string = ""
305+
m = matrix[idx]
306+
offset = translation_vec[idx]
307307
for j, dim in enumerate(components):
308308
if m[j] != 0:
309309
f = Fraction(m[j]).limit_denominator()
310-
if s != "" and f >= 0:
311-
s += "+"
310+
if string != "" and f >= 0:
311+
string += "+"
312312
if abs(f.numerator) != 1:
313-
s += str(f.numerator)
313+
string += str(f.numerator)
314314
elif f < 0:
315-
s += "-"
316-
s += c + dim
315+
string += "-"
316+
string += c + dim
317317
if f.denominator != 1:
318-
s += "/" + str(f.denominator)
319-
if t != 0:
320-
s += ("+" if (t > 0 and s != "") else "") + str(Fraction(t).limit_denominator())
321-
if s == "":
322-
s += "0"
323-
parts.append(s)
318+
string += "/" + str(f.denominator)
319+
if offset != 0:
320+
string += ("+" if (offset > 0 and string != "") else "") + str(Fraction(offset).limit_denominator())
321+
if string == "":
322+
string += "0"
323+
parts.append(string)
324324
return delim.join(parts)
325325

326326

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
from __future__ import annotations
22

3+
import matplotlib.pyplot as plt
4+
35
from pymatgen.util.plotting import periodic_table_heatmap, van_arkel_triangle
46
from pymatgen.util.testing import PymatgenTest
57

68

79
class FuncTestCase(PymatgenTest):
810
def test_plot_periodic_heatmap(self):
9-
random_data = {
10-
"Te": 0.11083818874391202,
11-
"Au": 0.7575629917425387,
12-
"Th": 1.2475885304040335,
13-
"Ni": -2.0354391922547705,
14-
}
15-
_ = periodic_table_heatmap(random_data)
16-
_ = periodic_table_heatmap(random_data, cmap="plasma")
17-
_ = periodic_table_heatmap(random_data, max_row=7)
18-
_ = periodic_table_heatmap(random_data, max_row=10)
19-
_ = periodic_table_heatmap(random_data, cbar_label_size=18)
20-
_ = periodic_table_heatmap(random_data, cmap_range=[0, 1])
21-
_ = periodic_table_heatmap(random_data, cbar_label="Hello World")
22-
_ = periodic_table_heatmap(random_data, blank_color="white")
23-
_ = periodic_table_heatmap(random_data, value_format="%.4f")
24-
_ = periodic_table_heatmap(random_data, edge_color="black")
25-
_ = periodic_table_heatmap(random_data, value_fontsize=12)
26-
_ = periodic_table_heatmap(random_data, symbol_fontsize=18)
27-
_ = periodic_table_heatmap(random_data, readable_fontcolor=True)
11+
random_data = {"Te": 0.11083, "Au": 0.75756, "Th": 1.24758, "Ni": -2.0354}
12+
ret_val = periodic_table_heatmap(random_data)
13+
assert ret_val is plt
14+
15+
# Test all keywords
16+
periodic_table_heatmap(
17+
random_data,
18+
cmap="plasma",
19+
max_row=10,
20+
cbar_label_size=18,
21+
cmap_range=[0, 1],
22+
cbar_label="Hello World",
23+
blank_color="white",
24+
value_format="%.4f",
25+
edge_color="black",
26+
value_fontsize=12,
27+
symbol_fontsize=18,
28+
readable_fontcolor=True,
29+
)
2830

2931
def test_van_arkel_triangle(self):
3032
random_list = [("Fe", "C"), ("Ni", "F")]
31-
_ = van_arkel_triangle(random_list)
32-
_ = van_arkel_triangle(random_list, annotate=True)
33+
ret_val = van_arkel_triangle(random_list)
34+
assert ret_val is plt
35+
van_arkel_triangle(random_list, annotate=True)

0 commit comments

Comments
 (0)