Skip to content

Commit 3a1ed00

Browse files
authored
chore: ban module-level import of pymatgen (#1597)
Pymatgen is still imcompatbile with NumPy 2. Move the import into runtime to fix the documentation builds and prevent potential errors when one uses dpgen without using pymatgen. Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Chores** - Reorganized import statements across multiple files to improve code readability and maintainability. - Localized imports within specific methods for better encapsulation and efficient module loading. - Updated linting configurations in `pyproject.toml` to include new rules and per-file exceptions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
1 parent 8f78294 commit 3a1ed00

File tree

19 files changed

+98
-38
lines changed

19 files changed

+98
-38
lines changed

dpgen/auto_test/Elastic.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
from shutil import copyfile
55

66
from monty.serialization import dumpfn, loadfn
7-
from pymatgen.analysis.elasticity.elastic import ElasticTensor
8-
from pymatgen.analysis.elasticity.strain import DeformedStructureSet, Strain
9-
from pymatgen.analysis.elasticity.stress import Stress
10-
from pymatgen.core.structure import Structure
11-
from pymatgen.io.vasp import Incar, Kpoints
127

138
import dpgen.auto_test.lib.abacus as abacus
149
import dpgen.auto_test.lib.vasp as vasp
@@ -53,6 +48,9 @@ def __init__(self, parameter, inter_param=None):
5348
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}
5449

5550
def make_confs(self, path_to_work, path_to_equi, refine=False):
51+
from pymatgen.analysis.elasticity.strain import DeformedStructureSet, Strain
52+
from pymatgen.core.structure import Structure
53+
5654
path_to_work = os.path.abspath(path_to_work)
5755
if os.path.exists(path_to_work):
5856
dlog.warning(f"{path_to_work} already exists")
@@ -189,6 +187,8 @@ def make_confs(self, path_to_work, path_to_equi, refine=False):
189187
return task_list
190188

191189
def post_process(self, task_list):
190+
from pymatgen.io.vasp import Incar, Kpoints
191+
192192
if self.inter_param["type"] == "abacus":
193193
POSCAR = "STRU"
194194
INCAR = "INPUT"
@@ -250,6 +250,9 @@ def task_param(self):
250250
return self.parameter
251251

252252
def _compute_lower(self, output_file, all_tasks, all_res):
253+
from pymatgen.analysis.elasticity.elastic import ElasticTensor
254+
from pymatgen.analysis.elasticity.stress import Stress
255+
253256
output_file = os.path.abspath(output_file)
254257
res_data = {}
255258
ptr_data = os.path.dirname(output_file) + "\n"

dpgen/auto_test/Gamma.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from ase.lattice.cubic import BodyCenteredCubic as bcc
99
from ase.lattice.cubic import FaceCenteredCubic as fcc
1010
from monty.serialization import dumpfn, loadfn
11-
from pymatgen.core.structure import Structure
12-
from pymatgen.io.ase import AseAtomsAdaptor
1311

1412
import dpgen.auto_test.lib.abacus as abacus
1513
import dpgen.auto_test.lib.vasp as vasp
@@ -94,6 +92,8 @@ def __init__(self, parameter, inter_param=None):
9492
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}
9593

9694
def make_confs(self, path_to_work, path_to_equi, refine=False):
95+
from pymatgen.core.structure import Structure
96+
9797
path_to_work = os.path.abspath(path_to_work)
9898
if os.path.exists(path_to_work):
9999
dlog.warning(f"{path_to_work} already exists")
@@ -287,6 +287,8 @@ def return_direction(self):
287287
return directions
288288

289289
def __gen_slab_ase(self, symbol, lat_param):
290+
from pymatgen.io.ase import AseAtomsAdaptor
291+
290292
if not self.lattice_type:
291293
raise RuntimeError("Error! Please provide the input lattice type!")
292294
elif self.lattice_type == "bcc":

dpgen/auto_test/Interstitial.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import numpy as np
77
from monty.serialization import dumpfn, loadfn
8-
from pymatgen.analysis.defects.generators import InterstitialGenerator
9-
from pymatgen.core.structure import Structure
108

119
import dpgen.auto_test.lib.abacus as abacus
1210
import dpgen.auto_test.lib.lammps as lammps
@@ -78,6 +76,9 @@ def __init__(self, parameter, inter_param=None):
7876
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}
7977

8078
def make_confs(self, path_to_work, path_to_equi, refine=False):
79+
from pymatgen.analysis.defects.generators import InterstitialGenerator
80+
from pymatgen.core.structure import Structure
81+
8182
path_to_work = os.path.abspath(path_to_work)
8283
path_to_equi = os.path.abspath(path_to_equi)
8384

dpgen/auto_test/Surface.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import dpdata
77
import numpy as np
88
from monty.serialization import dumpfn, loadfn
9-
from pymatgen.core.structure import Structure
10-
from pymatgen.core.surface import generate_all_slabs
119

1210
import dpgen.auto_test.lib.abacus as abacus
1311
import dpgen.auto_test.lib.vasp as vasp
@@ -85,6 +83,9 @@ def __init__(self, parameter, inter_param=None):
8583
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}
8684

8785
def make_confs(self, path_to_work, path_to_equi, refine=False):
86+
from pymatgen.core.structure import Structure
87+
from pymatgen.core.surface import generate_all_slabs
88+
8889
path_to_work = os.path.abspath(path_to_work)
8990
if os.path.exists(path_to_work):
9091
dlog.warning(f"{path_to_work} already exists")

dpgen/auto_test/VASP.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from dpdata import LabeledSystem
44
from monty.serialization import dumpfn
5-
from pymatgen.core.structure import Structure
6-
from pymatgen.io.vasp import Incar, Kpoints
75

86
import dpgen.auto_test.lib.vasp as vasp
97
from dpgen import dlog
@@ -22,6 +20,8 @@ def __init__(self, inter_parameter, path_to_poscar):
2220
self.path_to_poscar = path_to_poscar
2321

2422
def make_potential_files(self, output_dir):
23+
from pymatgen.core.structure import Structure
24+
2525
potcar_not_link_list = ["vacancy", "interstitial"]
2626
task_type = output_dir.split("/")[-2].split("_")[0]
2727

@@ -69,6 +69,8 @@ def make_potential_files(self, output_dir):
6969
dumpfn(self.inter, os.path.join(output_dir, "inter.json"), indent=4)
7070

7171
def make_input_file(self, output_dir, task_type, task_param):
72+
from pymatgen.io.vasp import Incar, Kpoints
73+
7274
sepline(ch=output_dir)
7375
dumpfn(task_param, os.path.join(output_dir, "task.json"), indent=4)
7476

dpgen/auto_test/Vacancy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import numpy as np
77
from monty.serialization import dumpfn, loadfn
8-
from pymatgen.analysis.defects.generators import VacancyGenerator
9-
from pymatgen.core.structure import Structure
108

119
import dpgen.auto_test.lib.abacus as abacus
1210
from dpgen import dlog
@@ -77,6 +75,9 @@ def __init__(self, parameter, inter_param=None):
7775
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}
7876

7977
def make_confs(self, path_to_work, path_to_equi, refine=False):
78+
from pymatgen.analysis.defects.generators import VacancyGenerator
79+
from pymatgen.core.structure import Structure
80+
8081
path_to_work = os.path.abspath(path_to_work)
8182
if os.path.exists(path_to_work):
8283
dlog.warning(f"{path_to_work} already exists")

dpgen/auto_test/gen_confs.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import os
55
import re
66

7-
from pymatgen.analysis.structure_matcher import StructureMatcher
8-
from pymatgen.ext.matproj import MPRester
9-
107
import dpgen.auto_test.lib.crys as crys
118

129
global_std_crystal = {
@@ -20,6 +17,8 @@
2017

2118

2219
def test_fit(struct, data):
20+
from pymatgen.analysis.structure_matcher import StructureMatcher
21+
2322
m = StructureMatcher()
2423
for ii in data:
2524
if m.fit(ii["structure"], struct):
@@ -50,6 +49,9 @@ def gen_ele_std(ele_name, ctype):
5049

5150

5251
def gen_element(ele_name, key):
52+
from pymatgen.analysis.structure_matcher import StructureMatcher
53+
from pymatgen.ext.matproj import MPRester
54+
5355
assert isinstance(ele_name, str)
5456
mpr = MPRester(key)
5557
data = mpr.query(
@@ -93,6 +95,8 @@ def gen_element_std(ele_name):
9395

9496

9597
def gen_alloy(eles, key):
98+
from pymatgen.ext.matproj import MPRester
99+
96100
mpr = MPRester(key)
97101

98102
data = mpr.query(

dpgen/auto_test/lib/abacus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from dpdata.abacus.scf import make_unlabeled_stru
88
from dpdata.utils import uniq_atom_names
99
from dpdata.vasp import poscar as dpdata_poscar
10-
from pymatgen.core.structure import Structure
1110

1211
import dpgen.generator.lib.abacus_scf as abacus_scf
1312

@@ -343,6 +342,8 @@ def final_stru(abacus_path):
343342

344343

345344
def stru2Structure(struf):
345+
from pymatgen.core.structure import Structure
346+
346347
stru = dpdata.System(struf, fmt="stru")
347348
stru.to("poscar", "POSCAR.tmp")
348349
ss = Structure.from_file("POSCAR.tmp")

dpgen/auto_test/lib/crys.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import numpy as np
2-
from pymatgen.core.lattice import Lattice
3-
from pymatgen.core.structure import Structure
42

53

64
def fcc(ele_name="ele", a=4.05):
5+
from pymatgen.core.structure import Structure
6+
77
box = np.array([[0.0, 0.5, 0.5], [0.5, 0.0, 0.5], [0.5, 0.5, 0.0]])
88
box *= a
99
return Structure(box, [ele_name], [[0, 0, 0]])
1010

1111

1212
def fcc1(ele_name="ele", a=4.05):
13+
from pymatgen.core.lattice import Lattice
14+
from pymatgen.core.structure import Structure
15+
1316
latt = Lattice.cubic(a)
1417
return Structure(
1518
latt,
@@ -19,11 +22,17 @@ def fcc1(ele_name="ele", a=4.05):
1922

2023

2124
def sc(ele_name="ele", a=2.551340126037118):
25+
from pymatgen.core.lattice import Lattice
26+
from pymatgen.core.structure import Structure
27+
2228
latt = Lattice.cubic(a)
2329
return Structure(latt, [ele_name], [[0, 0, 0]])
2430

2531

2632
def bcc(ele_name="ele", a=3.2144871302356037):
33+
from pymatgen.core.lattice import Lattice
34+
from pymatgen.core.structure import Structure
35+
2736
latt = Lattice.cubic(a)
2837
return Structure(
2938
latt,
@@ -38,6 +47,9 @@ def bcc(ele_name="ele", a=3.2144871302356037):
3847
def hcp(
3948
ele_name="ele", a=4.05 / np.sqrt(2), c=4.05 / np.sqrt(2) * 2.0 * np.sqrt(2.0 / 3.0)
4049
):
50+
from pymatgen.core.lattice import Lattice
51+
from pymatgen.core.structure import Structure
52+
4153
box = np.array([[1, 0, 0], [0.5, 0.5 * np.sqrt(3), 0], [0, 0, 1]])
4254
box[0] *= a
4355
box[1] *= a
@@ -51,6 +63,9 @@ def hcp(
5163
def dhcp(
5264
ele_name="ele", a=4.05 / np.sqrt(2), c=4.05 / np.sqrt(2) * 4.0 * np.sqrt(2.0 / 3.0)
5365
):
66+
from pymatgen.core.lattice import Lattice
67+
from pymatgen.core.structure import Structure
68+
5469
box = np.array([[1, 0, 0], [0.5, 0.5 * np.sqrt(3), 0], [0, 0, 1]])
5570
box[0] *= a
5671
box[1] *= a
@@ -69,6 +84,9 @@ def dhcp(
6984

7085

7186
def diamond(ele_name="ele", a=2.551340126037118):
87+
from pymatgen.core.lattice import Lattice
88+
from pymatgen.core.structure import Structure
89+
7290
box = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0]])
7391
box *= a
7492
latt = Lattice(box)

dpgen/auto_test/lib/vasp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import warnings
44

55
import numpy as np
6-
from pymatgen.io.vasp import Incar, Kpoints
76

87
import dpgen.auto_test.lib.util as util
98
from dpgen.generator.lib.vasp import incar_upper
@@ -503,6 +502,8 @@ def make_vasp_kpoints(kpoints, kgamma=False):
503502

504503

505504
def make_vasp_kpoints_from_incar(work_dir, jdata):
505+
from pymatgen.io.vasp import Incar, Kpoints
506+
506507
cwd = os.getcwd()
507508
fp_aniso_kspacing = jdata.get("fp_aniso_kspacing")
508509
os.chdir(work_dir)

0 commit comments

Comments
 (0)