Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ban module-level import of pymatgen #1597

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions dpgen/auto_test/Elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
from shutil import copyfile

from monty.serialization import dumpfn, loadfn
from pymatgen.analysis.elasticity.elastic import ElasticTensor
from pymatgen.analysis.elasticity.strain import DeformedStructureSet, Strain
from pymatgen.analysis.elasticity.stress import Stress
from pymatgen.core.structure import Structure
from pymatgen.io.vasp import Incar, Kpoints

import dpgen.auto_test.lib.abacus as abacus
import dpgen.auto_test.lib.vasp as vasp
Expand Down Expand Up @@ -53,6 +48,9 @@
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}

def make_confs(self, path_to_work, path_to_equi, refine=False):
from pymatgen.analysis.elasticity.strain import DeformedStructureSet, Strain
from pymatgen.core.structure import Structure

path_to_work = os.path.abspath(path_to_work)
if os.path.exists(path_to_work):
dlog.warning(f"{path_to_work} already exists")
Expand Down Expand Up @@ -189,6 +187,8 @@
return task_list

def post_process(self, task_list):
from pymatgen.io.vasp import Incar, Kpoints

if self.inter_param["type"] == "abacus":
POSCAR = "STRU"
INCAR = "INPUT"
Expand Down Expand Up @@ -250,6 +250,9 @@
return self.parameter

def _compute_lower(self, output_file, all_tasks, all_res):
from pymatgen.analysis.elasticity.elastic import ElasticTensor
from pymatgen.analysis.elasticity.stress import Stress

Check warning on line 254 in dpgen/auto_test/Elastic.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/Elastic.py#L253-L254

Added lines #L253 - L254 were not covered by tests

output_file = os.path.abspath(output_file)
res_data = {}
ptr_data = os.path.dirname(output_file) + "\n"
Expand Down
6 changes: 4 additions & 2 deletions dpgen/auto_test/Gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from ase.lattice.cubic import BodyCenteredCubic as bcc
from ase.lattice.cubic import FaceCenteredCubic as fcc
from monty.serialization import dumpfn, loadfn
from pymatgen.core.structure import Structure
from pymatgen.io.ase import AseAtomsAdaptor

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

def make_confs(self, path_to_work, path_to_equi, refine=False):
from pymatgen.core.structure import Structure

path_to_work = os.path.abspath(path_to_work)
if os.path.exists(path_to_work):
dlog.warning(f"{path_to_work} already exists")
Expand Down Expand Up @@ -287,6 +287,8 @@ def return_direction(self):
return directions

def __gen_slab_ase(self, symbol, lat_param):
from pymatgen.io.ase import AseAtomsAdaptor

if not self.lattice_type:
raise RuntimeError("Error! Please provide the input lattice type!")
elif self.lattice_type == "bcc":
Expand Down
5 changes: 3 additions & 2 deletions dpgen/auto_test/Interstitial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import numpy as np
from monty.serialization import dumpfn, loadfn
from pymatgen.analysis.defects.generators import InterstitialGenerator
from pymatgen.core.structure import Structure

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

def make_confs(self, path_to_work, path_to_equi, refine=False):
from pymatgen.analysis.defects.generators import InterstitialGenerator
from pymatgen.core.structure import Structure

path_to_work = os.path.abspath(path_to_work)
path_to_equi = os.path.abspath(path_to_equi)

Expand Down
5 changes: 3 additions & 2 deletions dpgen/auto_test/Surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import dpdata
import numpy as np
from monty.serialization import dumpfn, loadfn
from pymatgen.core.structure import Structure
from pymatgen.core.surface import generate_all_slabs

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

def make_confs(self, path_to_work, path_to_equi, refine=False):
from pymatgen.core.structure import Structure
from pymatgen.core.surface import generate_all_slabs

path_to_work = os.path.abspath(path_to_work)
if os.path.exists(path_to_work):
dlog.warning(f"{path_to_work} already exists")
Expand Down
6 changes: 4 additions & 2 deletions dpgen/auto_test/VASP.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from dpdata import LabeledSystem
from monty.serialization import dumpfn
from pymatgen.core.structure import Structure
from pymatgen.io.vasp import Incar, Kpoints

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

def make_potential_files(self, output_dir):
from pymatgen.core.structure import Structure

potcar_not_link_list = ["vacancy", "interstitial"]
task_type = output_dir.split("/")[-2].split("_")[0]

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

def make_input_file(self, output_dir, task_type, task_param):
from pymatgen.io.vasp import Incar, Kpoints

sepline(ch=output_dir)
dumpfn(task_param, os.path.join(output_dir, "task.json"), indent=4)

Expand Down
5 changes: 3 additions & 2 deletions dpgen/auto_test/Vacancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import numpy as np
from monty.serialization import dumpfn, loadfn
from pymatgen.analysis.defects.generators import VacancyGenerator
from pymatgen.core.structure import Structure

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

def make_confs(self, path_to_work, path_to_equi, refine=False):
from pymatgen.analysis.defects.generators import VacancyGenerator
from pymatgen.core.structure import Structure

path_to_work = os.path.abspath(path_to_work)
if os.path.exists(path_to_work):
dlog.warning(f"{path_to_work} already exists")
Expand Down
10 changes: 7 additions & 3 deletions dpgen/auto_test/gen_confs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import os
import re

from pymatgen.analysis.structure_matcher import StructureMatcher
from pymatgen.ext.matproj import MPRester

import dpgen.auto_test.lib.crys as crys

global_std_crystal = {
Expand All @@ -20,6 +17,8 @@


def test_fit(struct, data):
from pymatgen.analysis.structure_matcher import StructureMatcher

Check warning on line 20 in dpgen/auto_test/gen_confs.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/gen_confs.py#L20

Added line #L20 was not covered by tests

m = StructureMatcher()
for ii in data:
if m.fit(ii["structure"], struct):
Expand Down Expand Up @@ -50,6 +49,9 @@


def gen_element(ele_name, key):
from pymatgen.analysis.structure_matcher import StructureMatcher
from pymatgen.ext.matproj import MPRester

Check warning on line 53 in dpgen/auto_test/gen_confs.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/gen_confs.py#L52-L53

Added lines #L52 - L53 were not covered by tests

assert isinstance(ele_name, str)
mpr = MPRester(key)
data = mpr.query(
Expand Down Expand Up @@ -93,6 +95,8 @@


def gen_alloy(eles, key):
from pymatgen.ext.matproj import MPRester

Check warning on line 98 in dpgen/auto_test/gen_confs.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/gen_confs.py#L98

Added line #L98 was not covered by tests

mpr = MPRester(key)

data = mpr.query(
Expand Down
3 changes: 2 additions & 1 deletion dpgen/auto_test/lib/abacus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dpdata.abacus.scf import make_unlabeled_stru
from dpdata.utils import uniq_atom_names
from dpdata.vasp import poscar as dpdata_poscar
from pymatgen.core.structure import Structure

import dpgen.generator.lib.abacus_scf as abacus_scf

Expand Down Expand Up @@ -343,6 +342,8 @@ def final_stru(abacus_path):


def stru2Structure(struf):
from pymatgen.core.structure import Structure

stru = dpdata.System(struf, fmt="stru")
stru.to("poscar", "POSCAR.tmp")
ss = Structure.from_file("POSCAR.tmp")
Expand Down
22 changes: 20 additions & 2 deletions dpgen/auto_test/lib/crys.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import numpy as np
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure


def fcc(ele_name="ele", a=4.05):
from pymatgen.core.structure import Structure

Check warning on line 5 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L5

Added line #L5 was not covered by tests

box = np.array([[0.0, 0.5, 0.5], [0.5, 0.0, 0.5], [0.5, 0.5, 0.0]])
box *= a
return Structure(box, [ele_name], [[0, 0, 0]])


def fcc1(ele_name="ele", a=4.05):
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure

Check warning on line 14 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L13-L14

Added lines #L13 - L14 were not covered by tests

latt = Lattice.cubic(a)
return Structure(
latt,
Expand All @@ -19,11 +22,17 @@


def sc(ele_name="ele", a=2.551340126037118):
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure

Check warning on line 26 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L25-L26

Added lines #L25 - L26 were not covered by tests

latt = Lattice.cubic(a)
return Structure(latt, [ele_name], [[0, 0, 0]])


def bcc(ele_name="ele", a=3.2144871302356037):
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure

Check warning on line 34 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L33-L34

Added lines #L33 - L34 were not covered by tests

latt = Lattice.cubic(a)
return Structure(
latt,
Expand All @@ -38,6 +47,9 @@
def hcp(
ele_name="ele", a=4.05 / np.sqrt(2), c=4.05 / np.sqrt(2) * 2.0 * np.sqrt(2.0 / 3.0)
):
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure

Check warning on line 51 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L50-L51

Added lines #L50 - L51 were not covered by tests

box = np.array([[1, 0, 0], [0.5, 0.5 * np.sqrt(3), 0], [0, 0, 1]])
box[0] *= a
box[1] *= a
Expand All @@ -51,6 +63,9 @@
def dhcp(
ele_name="ele", a=4.05 / np.sqrt(2), c=4.05 / np.sqrt(2) * 4.0 * np.sqrt(2.0 / 3.0)
):
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure

Check warning on line 67 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L66-L67

Added lines #L66 - L67 were not covered by tests

box = np.array([[1, 0, 0], [0.5, 0.5 * np.sqrt(3), 0], [0, 0, 1]])
box[0] *= a
box[1] *= a
Expand All @@ -69,6 +84,9 @@


def diamond(ele_name="ele", a=2.551340126037118):
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Structure

Check warning on line 88 in dpgen/auto_test/lib/crys.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/crys.py#L87-L88

Added lines #L87 - L88 were not covered by tests

box = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0]])
box *= a
latt = Lattice(box)
Expand Down
3 changes: 2 additions & 1 deletion dpgen/auto_test/lib/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import warnings

import numpy as np
from pymatgen.io.vasp import Incar, Kpoints

import dpgen.auto_test.lib.util as util
from dpgen.generator.lib.vasp import incar_upper
Expand Down Expand Up @@ -503,6 +502,8 @@


def make_vasp_kpoints_from_incar(work_dir, jdata):
from pymatgen.io.vasp import Incar, Kpoints

Check warning on line 505 in dpgen/auto_test/lib/vasp.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/lib/vasp.py#L505

Added line #L505 was not covered by tests

cwd = os.getcwd()
fp_aniso_kspacing = jdata.get("fp_aniso_kspacing")
os.chdir(work_dir)
Expand Down
4 changes: 2 additions & 2 deletions dpgen/auto_test/mpdb.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os

from pymatgen.ext.matproj import MPRester, MPRestError

from dpgen import dlog

web = "materials.org"


def check_apikey():
from pymatgen.ext.matproj import MPRester, MPRestError

Check warning on line 9 in dpgen/auto_test/mpdb.py

View check run for this annotation

Codecov / codecov/patch

dpgen/auto_test/mpdb.py#L9

Added line #L9 was not covered by tests

try:
apikey = os.environ["MAPI_KEY"]
except KeyError:
Expand Down
8 changes: 6 additions & 2 deletions dpgen/data/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import dpdata
import numpy as np
from pymatgen.core import Structure
from pymatgen.io.vasp import Incar

import dpgen.data.tools.bcc as bcc
import dpgen.data.tools.diamond as diamond
Expand Down Expand Up @@ -300,6 +298,8 @@


def make_super_cell(jdata):
from pymatgen.core import Structure

out_dir = jdata["out_dir"]
super_cell = jdata["super_cell"]
path_uc = os.path.join(out_dir, global_dirname_02)
Expand Down Expand Up @@ -342,6 +342,8 @@


def make_super_cell_poscar(jdata):
from pymatgen.core import Structure

Check warning on line 345 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L345

Added line #L345 was not covered by tests

out_dir = jdata["out_dir"]
super_cell = jdata["super_cell"]
path_sc = os.path.join(out_dir, global_dirname_02)
Expand Down Expand Up @@ -1444,6 +1446,8 @@


def gen_init_bulk(args):
from pymatgen.io.vasp import Incar

Check warning on line 1449 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L1449

Added line #L1449 was not covered by tests

jdata = load_file(args.PARAM)
if args.MACHINE is not None:
mdata = load_file(args.MACHINE)
Expand Down
12 changes: 7 additions & 5 deletions dpgen/data/surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
from ase.build import general_surface

# -----ASE-------
from pymatgen.core import Element, Structure
from pymatgen.io.ase import AseAtomsAdaptor

# -----PMG---------
from pymatgen.io.vasp import Poscar

import dpgen.data.tools.bcc as bcc
import dpgen.data.tools.diamond as diamond
import dpgen.data.tools.fcc as fcc
Expand Down Expand Up @@ -168,6 +163,8 @@ def poscar_scale_direct(str_in, scale):


def poscar_elong(poscar_in, poscar_out, elong, shift_center=True):
from pymatgen.core import Structure

with open(poscar_in) as fin:
lines = list(fin)
if lines[7][0].upper() != "C":
Expand Down Expand Up @@ -215,6 +212,9 @@ def make_unit_cell(jdata):


def make_super_cell_pymatgen(jdata):
from pymatgen.core import Element, Structure
from pymatgen.io.ase import AseAtomsAdaptor

make_unit_cell(jdata)
out_dir = jdata["out_dir"]
path_uc = os.path.join(out_dir, global_dirname_02)
Expand Down Expand Up @@ -401,6 +401,8 @@ def poscar_scale_cartesian(str_in, scale):


def poscar_scale(poscar_in, poscar_out, scale):
from pymatgen.io.vasp import Poscar

with open(poscar_in) as fin:
lines = list(fin)
if "D" == lines[7][0] or "d" == lines[7][0]:
Expand Down
Loading