Skip to content

Commit

Permalink
Unignore ruff PD011 (#3020)
Browse files Browse the repository at this point in the history
* ruff unignore PD011 (pandas-use-of-dot-values)

* drop .values where not needed, else replace with .to_numpy(), noqa false positives
  • Loading branch information
janosh authored May 30, 2023
1 parent 7973b53 commit 3c91e69
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 79 deletions.
8 changes: 4 additions & 4 deletions pymatgen/entries/mixing_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ def get_adjustments(self, entry, mixing_state_data: pd.DataFrame = None):
)

# Verify that the entry is included in the mixing state data
if (entry.entry_id not in mixing_state_data["entry_id_1"].values) and (
entry.entry_id not in mixing_state_data["entry_id_2"].values
if (entry.entry_id not in mixing_state_data["entry_id_1"].values) and ( # noqa: PD011
entry.entry_id not in mixing_state_data["entry_id_2"].values # noqa: PD011
):
raise CompatibilityError(
f"WARNING! Discarding {run_type} entry {entry.entry_id} for {entry.composition.formula} "
Expand All @@ -308,8 +308,8 @@ def get_adjustments(self, entry, mixing_state_data: pd.DataFrame = None):
)

# Verify that the entry's energy has not been modified since mixing state data was generated
if (entry.energy_per_atom not in mixing_state_data["energy_1"].values) and (
entry.energy_per_atom not in mixing_state_data["energy_2"].values
if (entry.energy_per_atom not in mixing_state_data["energy_1"].values) and ( # noqa: PD011
entry.energy_per_atom not in mixing_state_data["energy_2"].values # noqa: PD011
):
raise CompatibilityError(
f"WARNING! Discarding {run_type} entry {entry.entry_id} for {entry.composition.formula} "
Expand Down
21 changes: 7 additions & 14 deletions pymatgen/io/abinit/abitimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,24 +304,17 @@ def summarize(self, **kwargs):
"""
import pandas as pd

colnames = [
"fname",
"wall_time",
"cpu_time",
"mpi_nprocs",
"omp_nthreads",
"mpi_rank",
]

frame = pd.DataFrame(columns=colnames)
col_names = ["fname", "wall_time", "cpu_time", "mpi_nprocs", "omp_nthreads", "mpi_rank"]

frame = pd.DataFrame(columns=col_names)
for timer in self.timers():
frame = frame.append({k: getattr(timer, k) for k in colnames}, ignore_index=True)
frame = frame.append({key: getattr(timer, key) for key in col_names}, ignore_index=True)
frame["tot_ncpus"] = frame["mpi_nprocs"] * frame["omp_nthreads"]

# Compute parallel efficiency (use the run with min number of cpus to normalize).
i = frame["tot_ncpus"].values.argmin()
ref_wtime = frame.iloc[i]["wall_time"]
ref_ncpus = frame.iloc[i]["tot_ncpus"]
idx = frame["tot_ncpus"].argmin()
ref_wtime = frame.iloc[idx]["wall_time"]
ref_ncpus = frame.iloc[idx]["tot_ncpus"]
frame["peff"] = (ref_ncpus * ref_wtime) / (frame["wall_time"] * frame["tot_ncpus"])

return frame
Expand Down
8 changes: 4 additions & 4 deletions pymatgen/io/abinit/pseudos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ def plot_densities(self, ax=None, **kwargs):
for i, den_name in enumerate(["ae_core_density", "pseudo_core_density"]):
rden = getattr(self, den_name)
label = "$n_c$" if i == 1 else r"$\tilde{n}_c$"
ax.plot(rden.mesh, rden.mesh * rden.values, label=label, lw=2)
ax.plot(rden.mesh, rden.mesh * rden.values, label=label, lw=2) # noqa: PD011

ax.legend(loc="best")

Expand Down Expand Up @@ -1479,10 +1479,10 @@ def plot_waves(self, ax=None, fontsize=12, **kwargs):
# ax.annotate("$r_c$", xy=(self.paw_radius + 0.1, 0.1))

for state, rfunc in self.pseudo_partial_waves.items():
ax.plot(rfunc.mesh, rfunc.mesh * rfunc.values, lw=2, label="PS-WAVE: " + state)
ax.plot(rfunc.mesh, rfunc.mesh * rfunc.values, lw=2, label="PS-WAVE: " + state) # noqa: PD011

for state, rfunc in self.ae_partial_waves.items():
ax.plot(rfunc.mesh, rfunc.mesh * rfunc.values, lw=2, label="AE-WAVE: " + state)
ax.plot(rfunc.mesh, rfunc.mesh * rfunc.values, lw=2, label="AE-WAVE: " + state) # noqa: PD011

ax.legend(loc="best", shadow=True, fontsize=fontsize)

Expand All @@ -1508,7 +1508,7 @@ def plot_projectors(self, ax=None, fontsize=12, **kwargs):
# ax.annotate("$r_c$", xy=(self.paw_radius + 0.1, 0.1))

for state, rfunc in self.projector_functions.items():
ax.plot(rfunc.mesh, rfunc.mesh * rfunc.values, label="TPROJ: " + state)
ax.plot(rfunc.mesh, rfunc.mesh * rfunc.values, label="TPROJ: " + state) # noqa: PD011

ax.legend(loc="best", shadow=True, fontsize=fontsize)

Expand Down
10 changes: 5 additions & 5 deletions pymatgen/io/cp2k/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(
repeats: Whether or not this keyword may be repeated. Default=False.
"""
self.name = name
self.values = values
self.values = values # noqa: PD011
self.description = description
self.repeats = repeats
self.units = units
Expand All @@ -110,8 +110,8 @@ def __eq__(self, other: object) -> bool:
if not isinstance(other, Keyword):
return NotImplemented
if self.name.upper() == other.name.upper():
v1 = [_.upper() if isinstance(_, str) else _ for _ in self.values]
v2 = [_.upper() if isinstance(_, str) else _ for _ in other.values]
v1 = [_.upper() if isinstance(_, str) else _ for _ in self.values] # noqa: PD011
v2 = [_.upper() if isinstance(_, str) else _ for _ in other.values] # noqa: PD011
if v1 == v2 and self.units == self.units:
return True
return False
Expand All @@ -120,7 +120,7 @@ def __add__(self, other):
return KeywordList(keywords=[self, other])

def __getitem__(self, item):
return self.values[item]
return self.values[item] # noqa: PD011

def as_dict(self):
"""
Expand All @@ -130,7 +130,7 @@ def as_dict(self):
dct["@module"] = type(self).__module__
dct["@class"] = type(self).__name__
dct["name"] = self.name
dct["values"] = self.values
dct["values"] = self.values # noqa: PD011
dct["description"] = self.description
dct["repeats"] = self.repeats
dct["units"] = self.units
Expand Down
10 changes: 5 additions & 5 deletions pymatgen/io/cp2k/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ def spin_polarized(self) -> bool:
@property
def charge(self) -> float:
"""Get charge from the input file"""
return self.input["FORCE_EVAL"]["DFT"].get("CHARGE", Keyword("", 0)).values[0]
return self.input["FORCE_EVAL"]["DFT"].get("CHARGE", Keyword("", 0)).values[0] # noqa: PD011

@property
def multiplicity(self) -> int:
"""Get the spin multiplicity from input file"""
return self.input["FORCE_EVAL"]["DFT"].get("Multiplicity", Keyword("")).values[0]
return self.input["FORCE_EVAL"]["DFT"].get("Multiplicity", Keyword("")).values[0] # noqa: PD011

@property
def is_molecule(self) -> bool:
Expand Down Expand Up @@ -757,9 +757,9 @@ def parse_cell_params(self):
cell = self.input["force_eval"]["subsys"]["cell"]
if cell.get("abc"):
return [
[cell["abc"].values[0], 0, 0],
[0, cell["abc"].values[1], 0],
[0, 0, cell["abc"].values[2]],
[cell["abc"].values[0], 0, 0], # noqa: PD011
[0, cell["abc"].values[1], 0], # noqa: PD011
[0, 0, cell["abc"].values[2]], # noqa: PD011
]
return [
list(cell.get("A").values),
Expand Down
13 changes: 5 additions & 8 deletions pymatgen/io/cp2k/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,11 +1009,8 @@ def activate_motion(
if not self.check("MOTION"):
self.insert(Section("MOTION", subsections={}))

run_type = self["global"].get("run_type", Keyword("run_type", "energy")).values[0].upper()
if run_type == "GEOMETRY_OPTIMIZATION":
run_type = "GEO_OPT"
if run_type == "MOLECULAR_DYNAMICS":
run_type = "MD"
run_type = self["global"].get("run_type", Keyword("run_type", "energy")).values[0].upper() # noqa: PD011
run_type = {"GEOMETRY_OPTIMIZATION": "GEO_OPT", "MOLECULAR_DYNAMICS": "MD"}.get(run_type, run_type)

self["MOTION"].insert(Section("PRINT", subsections={}))
self["MOTION"]["PRINT"].insert(Section("TRAJECTORY", section_parameters=["ON"], subsections={}))
Expand Down Expand Up @@ -1332,7 +1329,7 @@ def modify_dft_print_iters(self, iters, add_last="no"):
no: do not explicitly include the last iteration
"""
assert add_last.lower() in ["no", "numeric", "symbolic"]
run_type = self["global"].get("run_type", Keyword("run_type", "energy")).values[0].upper()
run_type = self["global"].get("run_type", Keyword("run_type", "energy")).values[0].upper() # noqa: PD011
if run_type not in ["ENERGY_FORCE", "ENERGY", "WAVEFUNCTION_OPTIMIZATION", "WFN_OPT"] and self.check(
"FORCE_EVAL/DFT/PRINT"
):
Expand All @@ -1358,8 +1355,8 @@ def validate(self):
for _, v in self["force_eval"]["subsys"].subsections.items():
if (
v.name.upper() == "KIND"
and v["POTENTIAL"].values[0].upper() == "ALL"
and self["force_eval"]["dft"]["qs"]["method"].values[0].upper() != "GAPW"
and v["POTENTIAL"].values[0].upper() == "ALL" # noqa: PD011
and self["force_eval"]["dft"]["qs"]["method"].values[0].upper() != "GAPW" # noqa: PD011
):
raise Cp2kValidationError("All electron basis sets require GAPW method")

Expand Down
30 changes: 15 additions & 15 deletions pymatgen/io/cp2k/tests/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ def test_potential_info(self):

def test_basis(self):
# Ensure cp2k formatted string can be read for data correctly
molopt = GaussianTypeOrbitalBasisSet.from_string(basis)
assert molopt.nexp == [7]
mol_opt = GaussianTypeOrbitalBasisSet.from_string(basis)
assert mol_opt.nexp == [7]
# Basis file can read from strings
bf = BasisFile.from_string(basis)
for obj in [molopt, bf.objects[0]]:
for obj in [mol_opt, bf.objects[0]]:
self.assert_all_close(
obj.exponents[0],
[
Expand All @@ -119,12 +119,12 @@ def test_basis(self):
)

# Ensure keyword can be properly generated
kw = molopt.get_keyword()
assert kw.values[0] == "SZV-MOLOPT-GTH"
molopt.info.admm = True
kw = molopt.get_keyword()
kw = mol_opt.get_keyword()
assert kw.values[0] == "SZV-MOLOPT-GTH" # noqa: PD011
mol_opt.info.admm = True
kw = mol_opt.get_keyword()
assert_array_equal(kw.values, ["AUX_FIT", "SZV-MOLOPT-GTH"])
molopt.info.admm = False
mol_opt.info.admm = False

def test_potentials(self):
# Ensure cp2k formatted string can be read for data correctly
Expand All @@ -142,9 +142,9 @@ def test_potentials(self):

# Ensure keyword can be properly generated
kw = pot.get_keyword()
assert kw.values[0] == "GTH-PBE-q1"
assert kw.values[0] == "GTH-PBE-q1" # noqa: PD011
kw = all.get_keyword()
assert kw.values[0] == "ALL"
assert kw.values[0] == "ALL" # noqa: PD011


class InputTest(PymatgenTest):
Expand Down Expand Up @@ -177,9 +177,9 @@ def test_sectionlist(self):

def test_basic_keywords(self):
kwd = Keyword("TEST1", 1, 2)
assert kwd.values == (1, 2)
assert kwd.values == (1, 2) # noqa: PD011
kwd = Keyword("TEST2", [1, 2, 3])
assert kwd.values == ([1, 2, 3],)
assert kwd.values == ([1, 2, 3],) # noqa: PD011
kwd = Keyword("TEST3", "xyz", description="testing", units="Ha")
assert kwd.description == "testing"
assert "[Ha]" in kwd.get_string()
Expand All @@ -197,9 +197,9 @@ def test_kind(self):

def test_ci_file(self):
# proper type retrieval
assert isinstance(self.ci["FORCE_EVAL"]["DFT"]["MGRID"]["NGRIDS"].values[0], int)
assert isinstance(self.ci["FORCE_EVAL"]["DFT"]["UKS"].values[0], bool)
assert isinstance(self.ci["FORCE_EVAL"]["DFT"]["QS"]["EPS_DEFAULT"].values[0], float)
assert isinstance(self.ci["FORCE_EVAL"]["DFT"]["MGRID"]["NGRIDS"].values[0], int) # noqa: PD011
assert isinstance(self.ci["FORCE_EVAL"]["DFT"]["UKS"].values[0], bool) # noqa: PD011
assert isinstance(self.ci["FORCE_EVAL"]["DFT"]["QS"]["EPS_DEFAULT"].values[0], float) # noqa: PD011

# description retrieval
assert self.ci["FORCE_EVAL"]["SUBSYS"]["CELL"].description == "Input parameters needed to set up the CELL."
Expand Down
6 changes: 3 additions & 3 deletions pymatgen/io/cp2k/tests/test_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def test_dft_set(self):
basis_and_potential = {"Si": {"basis": "SZV-GTH-q4", "potential": "GTH-PBE-q4", "aux_basis": "cFIT3"}}
ss = DftSet(Si_structure, basis_and_potential=basis_and_potential, xc_functionals="PBE")
basis_sets = ss["force_eval"]["subsys"]["Si_1"].get("basis_set")
assert any("AUX_FIT" in b.values for b in basis_sets)
assert any("cFIT3" in b.values for b in basis_sets)
assert any("AUX_FIT" in b.values for b in basis_sets) # noqa: PD011
assert any("cFIT3" in b.values for b in basis_sets) # noqa: PD011

# Basis sets / potentials by hash value
basis_and_potential = {
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_dft_set(self):

ss = DftSet(molecule, basis_and_potential=basis_and_potential, xc_functionals="PBE")
assert ss.check("force_eval/dft/poisson")
assert ss["force_eval"]["dft"]["poisson"].get("periodic").values[0].upper() == "NONE"
assert ss["force_eval"]["dft"]["poisson"].get("periodic").values[0].upper() == "NONE" # noqa: PD011


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions pymatgen/io/lammps/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ def structure(self):
latt = self.box.to_lattice()
site_properties = {}
if "q" in atoms:
site_properties["charge"] = atoms["q"].values
site_properties["charge"] = atoms["q"].to_numpy()
if self.velocities is not None:
site_properties["velocities"] = self.velocities.values
site_properties["velocities"] = self.velocities.to_numpy()
return Structure(
latt,
species,
Expand Down Expand Up @@ -605,7 +605,7 @@ def label_topo(t):
species = masses.loc[type_ids, "element"]
labels = masses.loc[type_ids, "label"]
coords = atoms[["x", "y", "z"]]
m = Molecule(species.values, coords.values, site_properties={ff_label: labels.values})
m = Molecule(species.values, coords.values, site_properties={ff_label: labels.to_numpy()})
charges = atoms.get("q")
velocities = atoms[["vx", "vy", "vz"]] if "vx" in atoms.columns else None
topologies = {}
Expand Down Expand Up @@ -870,7 +870,7 @@ def set_charge_atom_type(self, charges: dict[str | int, float]):
for iat, q in charges.items():
if isinstance(iat, str):
mass_iat = Element(iat).atomic_mass
iat = self.masses.loc[self.masses["mass"] == mass_iat].index.values[0]
iat = self.masses.loc[self.masses["mass"] == mass_iat].index[0]
self.atoms.loc[self.atoms["type"] == iat, "q"] = q


Expand Down Expand Up @@ -1009,7 +1009,7 @@ class ForceField(MSONable):

@staticmethod
def _is_valid(df):
return not pd.isna(df).values.any()
return not pd.isna(df).to_numpy().any()

def __init__(self, mass_info, nonbond_coeffs=None, topo_coeffs=None):
"""
Expand Down
18 changes: 9 additions & 9 deletions pymatgen/io/lammps/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def test_structure(self):

ethane = self.ethane.structure
assert_array_almost_equal(ethane.lattice.matrix, np.diag([10.0] * 3))
lbounds = np.array(self.ethane.box.bounds)[:, 0]
coords = self.ethane.atoms[["x", "y", "z"]].values - lbounds
l_bounds = np.array(self.ethane.box.bounds)[:, 0]
coords = self.ethane.atoms[["x", "y", "z"]] - l_bounds
assert_array_almost_equal(ethane.cart_coords, coords)
assert_array_almost_equal(ethane.site_properties["charge"], self.ethane.atoms["q"])
tatb = self.tatb.structure
Expand All @@ -126,13 +126,13 @@ def test_sort_structure(self):
# internally element:type will be {Fe: 1, S: 2},
# therefore without sorting the atom types in structure
# will be [2, 1], i.e., (S, Fe)
assert lmp2.atoms["type"].values.tolist() == [2, 1]
assert lmp2.atoms["type"].tolist() == [2, 1]

# with sorting the atom types in structures will be [1, 2]
lmp = LammpsData.from_structure(s, is_sort=True)
lmp.write_file("test1.data")
lmp2 = LammpsData.from_file("test1.data", atom_style="charge")
assert lmp2.atoms["type"].values.tolist() == [1, 2]
assert lmp2.atoms["type"].tolist() == [1, 2]

def test_get_string(self):
pep = self.peptide.get_string(distance=7, velocity=5, charge=4)
Expand Down Expand Up @@ -330,19 +330,19 @@ def test_disassemble(self):
topo_kw = kw + "s"
topos_df = c.topology[topo_kw]
topo_df = topos_df[topos_df["atom1"] >= shift]
topo_arr = topo_df.drop("type", axis=1).values
topo_arr = topo_df.drop("type", axis=1)
np.testing.assert_array_equal(topo.topologies[topo_kw], topo_arr - shift, topo_kw)
sample_topo = random.sample(list(topo_df.itertuples(False, None)), 1)[0]
topo_type_idx = sample_topo[0] - 1
topo_type = tuple(atom_labels[i - 1] for i in atoms.loc[list(sample_topo[1:])]["type"])

assert topo_type in ff_coeffs[topo_type_idx]["types"], ff_kw
# test no guessing element and pairij as nonbond coeffs
# test no guessing element and pairij as non-bond coeffs
v = self.virus
_, v_ff, _ = v.disassemble(guess_element=False)
assert v_ff.maps["Atoms"] == {"Qa1": 1, "Qb1": 2, "Qc1": 3, "Qa2": 4}
pairij_coeffs = v.force_field["PairIJ Coeffs"].drop(["id1", "id2"], axis=1)
np.testing.assert_array_equal(v_ff.nonbond_coeffs, pairij_coeffs.values)
pair_ij_coeffs = v.force_field["PairIJ Coeffs"].drop(["id1", "id2"], axis=1)
np.testing.assert_array_equal(v_ff.nonbond_coeffs, pair_ij_coeffs.values)
# test class2 ff
_, e_ff, _ = self.ethane.disassemble()
e_topo_coeffs = e_ff.topo_coeffs
Expand Down Expand Up @@ -1032,7 +1032,7 @@ def test_structure(self):
)
assert li_ec_structure.formula == "Li1 H4 C3 O3"
lbounds = np.array(self.li_ec.box.bounds)[:, 0]
coords = self.li_ec.atoms[["x", "y", "z"]].values - lbounds
coords = self.li_ec.atoms[["x", "y", "z"]] - lbounds
assert_array_almost_equal(li_ec_structure.cart_coords, coords)
assert_array_almost_equal(li_ec_structure.site_properties["charge"], self.li_ec.atoms["q"])
frac_coords = li_ec_structure.frac_coords[0]
Expand Down
Loading

0 comments on commit 3c91e69

Please sign in to comment.