Skip to content

Commit

Permalink
Cleanup io and ext.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Sep 15, 2019
1 parent a4e43c0 commit 6fe6368
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pmg-lint.yml
Expand Up @@ -32,6 +32,6 @@ jobs:
pip install flake8
# stop the build if there are Python syntax errors or undefined names
flake8 --count --select=E9,F63,F7,F82 --show-source --statistics pymatgen
flake8 --count --show-source --statistics pymatgen/core pymatgen/electronic_structure
flake8 --count --show-source --statistics pymatgen/core pymatgen/electronic_structure pymatgen/ext
# exit-zero treats all errors as warnings.
flake8 --count --exit-zero --max-complexity=10 --statistics pymatgen
4 changes: 2 additions & 2 deletions pre-commit
Expand Up @@ -13,13 +13,13 @@ files=`echo $files | tr '\n' ' '`
echo "Check code styles for $files"
pycodestyle pymatgen/dao.py $files
if [ $? -ne 0 ]; then
echo "Bad code style detected. Fix before commit."
echo "Bad code style detected by pycodestyle. Fix before commit."
exit 1
fi
echo "Flake8 for $files"
flake8 pymatgen/dao.py $files
if [ $? -ne 0 ]; then
echo "Bad doc style detected. Fix before commit."
echo "Bad code style detected by flake8. Fix before commit."
exit 1
fi
echo "Check doc styles for $files"
Expand Down
12 changes: 10 additions & 2 deletions pymatgen/ext/cod.py
Expand Up @@ -38,7 +38,6 @@
import re
from pymatgen.core.composition import Composition
from pymatgen.core.structure import Structure
from pymatgen.util.string import formula_double_format

__author__ = "Shyue Ping Ong"
__copyright__ = "Copyright 2012, The Materials Project"
Expand All @@ -53,9 +52,18 @@ class COD:
"""

def __init__(self):
"""
Blank __init__. No args required.
"""
pass

def query(self, sql):
def query(self, sql: str) -> str:
"""
Perform a query.
:param sql: SQL string
:return: Response from SQL query.
"""
r = subprocess.check_output(["mysql", "-u", "cod_reader", "-h",
"www.crystallography.net", "-e",
sql, "cod"])
Expand Down
1 change: 0 additions & 1 deletion pymatgen/ext/jhu.py
Expand Up @@ -11,7 +11,6 @@
import tempfile
import shutil
import os
from monty.tempfile import ScratchDir

from pymatgen.io.vasp.inputs import Kpoints

Expand Down
14 changes: 2 additions & 12 deletions pymatgen/ext/matproj.py
Expand Up @@ -19,7 +19,7 @@
import re
import warnings
from time import sleep

import requests
from monty.json import MontyDecoder, MontyEncoder

from copy import deepcopy
Expand Down Expand Up @@ -115,16 +115,6 @@ def __init__(self, api_key=None, endpoint=None, include_user_agent=True):
if self.preamble != "https://materialsproject.org/rest/v2":
warnings.warn("Non-default endpoint used: {}".format(self.preamble))

import requests
if sys.version_info[0] < 3:
try:
from pybtex import __version__
except ImportError:
warnings.warn("If you query for structure data encoded using MP's "
"Structure Notation Language (SNL) format and you use "
"`mp_decode=True` (the default) for MPRester queries, "
"you should install dependencies via "
"`pip install pymatgen[matproj.snl]`.")
self.session = requests.Session()
self.session.headers = {"x-api-key": self.api_key}
if include_user_agent:
Expand Down Expand Up @@ -1208,7 +1198,7 @@ def get_wulff_shape(self, material_id):
pymatgen.analysis.wulff.WulffShape
"""
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.analysis.wulff import WulffShape, hkl_tuple_to_str
from pymatgen.analysis.wulff import WulffShape

structure = self.get_structure_by_material_id(material_id)
surfaces = self.get_surface_data(material_id)["surfaces"]
Expand Down
111 changes: 67 additions & 44 deletions pymatgen/io/fiesta.py
@@ -1,5 +1,14 @@
# coding: utf-8

"""
This module implements input and output for Fiesta (http://perso.neel.cnrs.fr/xavier.blase/fiesta/index.html).
and
-Nwchem2Fiesta class: to create the input files needed for a Fiesta run
-Fiesta_run: run gw_fiesta and bse_fiesta
-Localised Basis set reader
"""

import re
import os
Expand All @@ -12,16 +21,6 @@

from pymatgen.core.structure import Molecule

"""
This module implements input and output for Fiesta (http://perso.neel.cnrs.fr/xavier.blase/fiesta/index.html).
and
-Nwchem2Fiesta class: to create the input files needed for a Fiesta run
-Fiesta_run: run gw_fiesta and bse_fiesta
-Localised Basis set reader
"""

__author__ = 'ndardenne'
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
Expand Down Expand Up @@ -73,46 +72,57 @@ def run(self):
os.chdir(init_folder)

def as_dict(self):
"""
:return: MSONable dict
"""
return {"@module": self.__class__.__module__,
"@class": self.__class__.__name__,
"filename": self.filename,
"folder": self.folder}

@classmethod
def from_dict(cls, d):
"""
:param d: Dict representation.
:return: Nwchem2Fiesta
"""
return Nwchem2Fiesta(folder=d["folder"], filename=d["filename"])


class Fiesta_run(MSONable):
class FiestaRun(MSONable):
"""
To run FIESTA inside python:
if grid is [x,x] then bse runs
if grid is [x,x,y] the fiesta(gw) runs
otherwise it breaks
if grid is [x,x] then bse runs
if grid is [x,x,y] the fiesta(gw) runs
otherwise it breaks
"""

def __init__(self, folder=os.getcwd(), grid=[2, 2, 2], log_file="log"):
"""
folder:
logfile: logfile of Fiesta
Args:
folder: Folder to look for runs.
grid:
log_file: logfile of Fiesta
"""

self.folder = folder
self.log_file = log_file
self.grid = grid

def run(self):
"""
Performs FIESTA (gw) run.
"""
if len(self.grid) == 3:
self.mpi_procs = self.grid[0] * self.grid[1] * self.grid[2]
self.gw_run()
self._gw_run()
elif len(self.grid) == 2:
self.mpi_procs = self.grid[0] * self.grid[1]
self.bse_run()
else:
raise ValueError(
"Wrong grid size: must be [nrow, ncolumn, nslice] for gw of [nrow, nslice] for bse")

def gw_run(self):
def _gw_run(self):
"""
Performs FIESTA (gw) run
"""
Expand Down Expand Up @@ -147,6 +157,9 @@ def bse_run(self):
os.chdir(init_folder)

def as_dict(self):
"""
:return: MSONable dict
"""
return {"@module": self.__class__.__module__,
"@class": self.__class__.__name__,
"log_file": self.log_file,
Expand All @@ -155,22 +168,27 @@ def as_dict(self):

@classmethod
def from_dict(cls, d):
return Fiesta_run(folder=d["folder"], grid=d["grid"],
log_file=d['log_file'])
"""
:param d: Dict representation
:return: FiestaRun
"""
return FiestaRun(folder=d["folder"], grid=d["grid"],
log_file=d['log_file'])


class Basis_set_reader:
class BasisSetReader:
"""
A basis set reader.
Args:
filename: Filename to read.
Basis set are stored in data as a dict:
:key l_zeta_ng for each nl orbitals which contain list of tuple (alpha, coef) for each of the ng gaussians
in l_zeta orbital
Basis set are stored in data as a dict:
:key l_zeta_ng for each nl orbitals which contain list of tuple (alpha, coef) for each of the ng gaussians
in l_zeta orbital
"""

def __init__(self, filename):
"""
Args:
filename: Filename to read.
"""
self.filename = filename

with zopen(filename) as f:
Expand Down Expand Up @@ -232,7 +250,7 @@ def _parse_file(self, input):

def set_n_nlmo(self):
"""
:return: the number of nlm orbitals for the basis set
:return: the number of nlm orbitals for the basis set
"""

nnlmo = 0
Expand Down Expand Up @@ -510,10 +528,17 @@ def __str__(self):
geometry="\n".join(geometry))

def write_file(self, filename):
"""
Write FiestaInput to a file
:param filename: Filename
"""
with zopen(filename, "w") as f:
f.write(self.__str__())

def as_dict(self):
"""
:return: MSONable dict
"""
return {
"mol": self._mol.as_dict(),
"correlation_grid": self.correlation_grid,
Expand All @@ -525,6 +550,10 @@ def as_dict(self):

@classmethod
def from_dict(cls, d):
"""
:param d: Dict representation
:return: FiestaInput
"""
return FiestaInput(Molecule.from_dict(d["mol"]),
correlation_grid=d["correlation_grid"],
Exc_DFT_option=d["Exc_DFT_option"],
Expand Down Expand Up @@ -562,7 +591,6 @@ def from_string(cls, string_input):
lines.pop(0)
l = lines.pop(0).strip()
toks = l.split()
nvbands = toks[0]

# correlation_grid
# number of points and spacing in eV for correlation grid
Expand Down Expand Up @@ -644,7 +672,6 @@ def from_string(cls, string_input):
lines.pop(0)
l = lines.pop(0).strip()
toks = l.split()
scale = toks[0]
# atoms x,y,z cartesian .. will be multiplied by scale
lines.pop(0)
# Parse geometry
Expand Down Expand Up @@ -687,12 +714,13 @@ class FiestaOutput:
A Fiesta output file parser.
All energies are in eV.
Args:
filename: Filename to read.
"""

def __init__(self, filename):
"""
Args:
filename: Filename to read.
"""
self.filename = filename

with zopen(filename) as f:
Expand All @@ -701,7 +729,7 @@ def __init__(self, filename):
chunks = re.split(r"GW Driver iteration", data)

# preamble: everything before the first GW Driver iteration
preamble = chunks.pop(0)
chunks.pop(0)

# self.job_info = self._parse_preamble(preamble)
self.data = [self._parse_job(c) for c in chunks]
Expand All @@ -722,9 +750,6 @@ def _parse_job(self, output):
total_time_patt = re.compile(r"\s*total \s+ time: \s+ ([\d.]+) .*",
re.VERBOSE)

error_defs = {
"calculations not reaching convergence": "Bad convergence"}

GW_results = {}
parse_gw_results = False
parse_total_time = False
Expand Down Expand Up @@ -774,12 +799,13 @@ class BSEOutput:
A bse output file parser. The start...
All energies are in eV.
Args:
filename: Filename to read.
"""

def __init__(self, filename):
"""
Args:
filename: Filename to read.
"""
self.filename = filename

with zopen(filename) as f:
Expand All @@ -799,9 +825,6 @@ def _parse_job(self, output):
total_time_patt = re.compile(r"\s*total \s+ time: \s+ ([\d.]+) .*",
re.VERBOSE)

error_defs = {
"calculations not reaching convergence": "Bad convergence"}

BSE_results = {}
parse_BSE_results = False
parse_total_time = False
Expand Down

0 comments on commit 6fe6368

Please sign in to comment.