mirror of
https://github.com/LCPQ/EMSL_Basis_Set_Exchange_Local
synced 2025-01-03 01:55:54 +01:00
Cleaning, Pep 8
This commit is contained in:
parent
5c2e20af96
commit
18bc6bf347
@ -11,9 +11,9 @@ Usage:
|
|||||||
EMSL_api.py list_atoms --basis=<basis_name>
|
EMSL_api.py list_atoms --basis=<basis_name>
|
||||||
[--db_path=<db_path>]
|
[--db_path=<db_path>]
|
||||||
EMSL_api.py get_basis_data --basis=<basis_name>
|
EMSL_api.py get_basis_data --basis=<basis_name>
|
||||||
[--atom=<atom_name>...]
|
[--atom=<atom_name>...]
|
||||||
[--db_path=<db_path>]
|
[--db_path=<db_path>]
|
||||||
[(--save [--path=<path>])]
|
[(--save [--path=<path>])]
|
||||||
EMSL_api.py list_formats
|
EMSL_api.py list_formats
|
||||||
EMSL_api.py create_db --db_path=<db_path>
|
EMSL_api.py create_db --db_path=<db_path>
|
||||||
--format=<format>
|
--format=<format>
|
||||||
|
@ -59,47 +59,100 @@ def checkSQLite3(db_path):
|
|||||||
|
|
||||||
|
|
||||||
def cond_sql_or(table_name, l_value, glob=False):
|
def cond_sql_or(table_name, l_value, glob=False):
|
||||||
|
"""Take a table_name, a list of value and create the sql or combande"""
|
||||||
|
|
||||||
opr = "GLOB" if glob else "="
|
opr = "GLOB" if glob else "="
|
||||||
|
|
||||||
l = []
|
return [" OR ".join(['{} {} "{}"'.format(table_name,
|
||||||
dmy = " OR ".join(['%s %s "%s"' % (table_name, opr, i) for i in l_value])
|
opr,
|
||||||
if dmy:
|
val) for val in l_value])]
|
||||||
l.append("(%s)" % dmy)
|
|
||||||
|
|
||||||
return l
|
|
||||||
|
|
||||||
|
|
||||||
def string_to_nb_mo(str_l):
|
def string_to_nb_mo(str_type):
|
||||||
|
"""Take a string and return the nb of orbital"""
|
||||||
|
assert len(str_type) == 1
|
||||||
|
|
||||||
assert len(str_l) == 1
|
d = {"S": 1,
|
||||||
|
"P": 2,
|
||||||
|
"D": 3}
|
||||||
|
|
||||||
d = {"S": 3,
|
if str_type in d:
|
||||||
"P": 5,
|
return 2 * d[str_type] + 1
|
||||||
"D": 7}
|
|
||||||
|
|
||||||
if str_l in d:
|
|
||||||
return d[str_l]
|
|
||||||
# ord("F") = 70 and ord("Z") = 87
|
# ord("F") = 70 and ord("Z") = 87
|
||||||
elif 70 <= ord(str_l) <= 87:
|
elif 70 <= ord(str_type) <= 87:
|
||||||
# ord("F") = 70 and l = 4 so ofset if 66
|
# ord("F") = 70 and l = 4 so ofset if 66
|
||||||
return 2 * (ord(str_l) - 66) + 1
|
return 2 * (ord(str_type) - 66) + 1
|
||||||
else:
|
else:
|
||||||
raise BaseException
|
raise BaseException
|
||||||
|
|
||||||
|
|
||||||
|
# _
|
||||||
|
# / |_ _ _ | _. | o _| o _|_
|
||||||
|
# \_ | | (/_ (_ |< \/ (_| | | (_| | |_ \/
|
||||||
|
# /
|
||||||
|
|
||||||
|
def check_gamess(str_type):
|
||||||
|
"""Check is the orbital type is handle by gamess"""
|
||||||
|
|
||||||
|
assert len(str_type) == 1
|
||||||
|
|
||||||
|
if str_type in "S P D".split():
|
||||||
|
return True
|
||||||
|
elif str_type > "L":
|
||||||
|
raise BaseException
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def check_NWChem(str_type):
|
||||||
|
"""Check is the orbital type is handle by gamess"""
|
||||||
|
|
||||||
|
assert len(str_type) == 1
|
||||||
|
|
||||||
|
if str_type in "S P D".split():
|
||||||
|
return True
|
||||||
|
elif str_type > "I" or str_type in "K L M".split():
|
||||||
|
raise BaseException
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# _ __
|
||||||
|
# |_ |\/| (_ | | _ _ _. |
|
||||||
|
# |_ | | __) |_ |_ (_) (_ (_| |
|
||||||
|
#
|
||||||
class EMSL_local:
|
class EMSL_local:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
All the method for using the EMSL db localy
|
All the method for using the EMSL db localy
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
def __init__(self, db_path=None):
|
def __init__(self, db_path=None):
|
||||||
self.db_path = db_path
|
self.db_path = db_path
|
||||||
self.p = re.compile(ur'^(\w)\s+\d+\b')
|
self.p = re.compile(ur'^(\w)\s+\d+\b')
|
||||||
|
|
||||||
|
def get_list_type(self, atom_basis):
|
||||||
|
"""
|
||||||
|
Return the begin and the end of all the type of orbital
|
||||||
|
input: atom_basis = [name, ]
|
||||||
|
output: [ [type, begin, end], ...]
|
||||||
|
"""
|
||||||
|
# Example
|
||||||
|
# [[u'S', 1, 5], [u'L', 5, 9], [u'L', 9, 12], [u'D', 16, 18]]"
|
||||||
|
|
||||||
|
l = []
|
||||||
|
for i, line in enumerate(atom_basis):
|
||||||
|
m = re.search(self.p, line)
|
||||||
|
if m:
|
||||||
|
l.append([m.group(1), i])
|
||||||
|
try:
|
||||||
|
l[-2].append(i)
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
l[-1].append(i + 1)
|
||||||
|
return l
|
||||||
|
|
||||||
def get_list_basis_available(self,
|
def get_list_basis_available(self,
|
||||||
elts=[],
|
elts=[],
|
||||||
basis=[],
|
basis=[],
|
||||||
@ -122,17 +175,17 @@ class EMSL_local:
|
|||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
|
|
||||||
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
# ~#~#~#~#~#~ #
|
||||||
# G e t i n g B a s i s _ i d #
|
# F i l t e r #
|
||||||
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
# ~#~#~#~#~#~ #
|
||||||
|
|
||||||
if basis:
|
if basis:
|
||||||
cmd_basis = " ".join(cond_sql_or("name", basis, glob=True))
|
cmd_basis = " ".join(cond_sql_or("name", basis, glob=True))
|
||||||
else:
|
else:
|
||||||
cmd_basis = "(1)"
|
cmd_basis = "(1)"
|
||||||
|
|
||||||
|
# Not Ets
|
||||||
if not elts:
|
if not elts:
|
||||||
|
|
||||||
if not average_mo_number:
|
if not average_mo_number:
|
||||||
cmd = """SELECT DISTINCT name, description
|
cmd = """SELECT DISTINCT name, description
|
||||||
FROM basis_tab
|
FROM basis_tab
|
||||||
@ -145,6 +198,11 @@ class EMSL_local:
|
|||||||
cmd = cmd.format(cmd_basis)
|
cmd = cmd.format(cmd_basis)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
||||||
|
# G e t t i n g _ B a s i s I d #
|
||||||
|
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
||||||
|
|
||||||
str_ = """SELECT DISTINCT basis_id
|
str_ = """SELECT DISTINCT basis_id
|
||||||
FROM output_tab
|
FROM output_tab
|
||||||
WHERE elt=? AND {0}""".format(cmd_basis)
|
WHERE elt=? AND {0}""".format(cmd_basis)
|
||||||
@ -156,19 +214,24 @@ class EMSL_local:
|
|||||||
cmd_basis = " ".join(cond_sql_or("basis_id", dump))
|
cmd_basis = " ".join(cond_sql_or("basis_id", dump))
|
||||||
cmd_ele = " ".join(cond_sql_or("elt", elts))
|
cmd_ele = " ".join(cond_sql_or("elt", elts))
|
||||||
|
|
||||||
|
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
||||||
|
# C r e a t e _ t h e _ c m d #
|
||||||
|
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
||||||
|
|
||||||
|
column_to_fech = "name, description"
|
||||||
|
if average_mo_number:
|
||||||
|
column_to_fech += ", data"
|
||||||
|
|
||||||
|
filter_where = cmd_ele + " AND " + cmd_basis
|
||||||
|
|
||||||
cmd = """SELECT DISTINCT {0}
|
cmd = """SELECT DISTINCT {0}
|
||||||
FROM output_tab
|
FROM output_tab
|
||||||
WHERE {1}
|
WHERE {1}
|
||||||
ORDER BY name"""
|
ORDER BY name""".format(column_to_fech, filter_where)
|
||||||
|
|
||||||
if average_mo_number:
|
# ~#~#~#~#~ #
|
||||||
column = "name, description, data"
|
# F e t c h #
|
||||||
else:
|
# ~#~#~#~#~ #
|
||||||
column = "name, description"
|
|
||||||
|
|
||||||
filter_ = cmd_ele + " AND " + cmd_basis
|
|
||||||
|
|
||||||
cmd = cmd.format(column, filter_)
|
|
||||||
|
|
||||||
c.execute(cmd)
|
c.execute(cmd)
|
||||||
info = c.fetchall()
|
info = c.fetchall()
|
||||||
@ -184,14 +247,11 @@ class EMSL_local:
|
|||||||
# Description : dict_info[name] = [description, nb_mo, nb_ele]
|
# Description : dict_info[name] = [description, nb_mo, nb_ele]
|
||||||
|
|
||||||
if average_mo_number:
|
if average_mo_number:
|
||||||
for name, description, data in info:
|
for name, description, atom_basis in info:
|
||||||
nb_mo = 0
|
nb_mo = 0
|
||||||
for line in data.split("\n")[1:]:
|
|
||||||
str_l = line.split()[0]
|
for type_, _, _ in self.get_list_type(atom_basis.split("\n")):
|
||||||
try:
|
nb_mo += string_to_nb_mo(type_)
|
||||||
nb_mo += string_to_nb_mo(str_l)
|
|
||||||
except BaseException:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dict_info[name][1] += nb_mo
|
dict_info[name][1] += nb_mo
|
||||||
@ -210,31 +270,50 @@ class EMSL_local:
|
|||||||
|
|
||||||
def get_list_element_available(self, basis_name):
|
def get_list_element_available(self, basis_name):
|
||||||
|
|
||||||
|
# ~#~#~#~ #
|
||||||
|
# I n i t #
|
||||||
|
# ~#~#~#~ #
|
||||||
|
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
|
|
||||||
str_ = "SELECT DISTINCT elt from output_tab WHERE name=:name_us COLLATE NOCASE"
|
# ~#~#~#~#~#~ #
|
||||||
|
# F i l t e r #
|
||||||
|
# ~#~#~#~#~#~ #
|
||||||
|
|
||||||
|
str_ = """SELECT DISTINCT elt
|
||||||
|
FROM output_tab
|
||||||
|
WHERE name=:name_us COLLATE NOCASE"""
|
||||||
|
|
||||||
|
# ~#~#~#~#~ #
|
||||||
|
# F e t c h #
|
||||||
|
# ~#~#~#~#~ #
|
||||||
|
|
||||||
c.execute(str_, {"name_us": basis_name})
|
c.execute(str_, {"name_us": basis_name})
|
||||||
|
|
||||||
data = c.fetchall()
|
|
||||||
|
|
||||||
data = [str(i[0]) for i in data]
|
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
return data
|
|
||||||
|
|
||||||
def get_basis(self, basis_name, elts=None):
|
# ~#~#~#~#~#~ #
|
||||||
|
# R e t u r n #
|
||||||
|
# ~#~#~#~#~#~ #
|
||||||
|
|
||||||
|
return [str(i[0]) for i in c.fetchall()]
|
||||||
|
|
||||||
|
def get_basis(self, basis_name, elts=None, checking=None):
|
||||||
"""
|
"""
|
||||||
Return the data from the basis set
|
Return the data from the basis set
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# __ _
|
# ~#~#~#~ #
|
||||||
# /__ _ _|_ _|_ ._ _ ._ _ _ _. |
|
# I n i t #
|
||||||
# \_| (/_ |_ | | (_) | | | _> (_| |
|
# ~#~#~#~ #
|
||||||
# |
|
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
|
|
||||||
|
# ~#~#~#~#~#~ #
|
||||||
|
# F i l t e r #
|
||||||
|
# ~#~#~#~#~#~ #
|
||||||
|
|
||||||
cmd_ele = " ".join(cond_sql_or("elt", elts)) if elts else "(1)"
|
cmd_ele = " ".join(cond_sql_or("elt", elts)) if elts else "(1)"
|
||||||
|
|
||||||
c.execute('''SELECT DISTINCT data from output_tab
|
c.execute('''SELECT DISTINCT data from output_tab
|
||||||
@ -243,10 +322,25 @@ class EMSL_local:
|
|||||||
cmd_ele=cmd_ele))
|
cmd_ele=cmd_ele))
|
||||||
|
|
||||||
# We need to take i[0] because fetchall return a tuple [(value),...]
|
# We need to take i[0] because fetchall return a tuple [(value),...]
|
||||||
l_data_raw = [i[0].strip() for i in c.fetchall()]
|
l_atom_basis = [i[0].strip() for i in c.fetchall()]
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
return l_data_raw
|
# ~#~#~#~#~ #
|
||||||
|
# C h e c k #
|
||||||
|
# ~#~#~#~#~ #
|
||||||
|
|
||||||
|
checking = False
|
||||||
|
|
||||||
|
if checking:
|
||||||
|
for atom_basis in l_atom_basis:
|
||||||
|
for type_, _, _ in self.get_list_type(atom_basis.split("\n")):
|
||||||
|
check_gamess(type_)
|
||||||
|
|
||||||
|
# ~#~#~#~#~#~ #
|
||||||
|
# R e t u r n #
|
||||||
|
# ~#~#~#~#~#~ #
|
||||||
|
|
||||||
|
return l_atom_basis
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user