Skip to content

Commit

Permalink
Merge branch 'database' of github.com:CalebBell/chemicals into database
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebBell committed Jun 2, 2022
2 parents 7c7b834 + bdb4e45 commit 94a63cc
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 111 deletions.
6 changes: 4 additions & 2 deletions chemicals/acentric.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'omega_methods', 'omega_all_methods', 'omega_definition']

from chemicals import critical
from chemicals import data_reader as dr
from chemicals.data_reader import (list_available_methods_from_df_dict,
database_constant_lookup,
retrieve_any_from_df_dict,
Expand Down Expand Up @@ -150,8 +151,9 @@ def omega(CASRN, method=None):
Hydrocarbons, Second Edition. Amsterdam Boston: Gulf Professional
Publishing, 2014.
'''
val, found = database_constant_lookup(CASRN, 'omega') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'omega')
if found: return val
if method:
return retrieve_from_df_dict(critical.omega_sources, CASRN, 'omega', method)
else:
Expand Down
17 changes: 10 additions & 7 deletions chemicals/combustion.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"""

from fluids.numerics import normalize

from chemicals import data_reader as dr
from chemicals.data_reader import (data_source, database_constant_lookup,
list_available_methods_from_df_dict,
register_df_source,
Expand Down Expand Up @@ -260,8 +260,9 @@ def RON(CASRN, method=None):
Learning Projects for Fuel Property Prediction." Journal of Open Source
Software 2, no. 17 (2017): 401.
'''
val, found = database_constant_lookup(CASRN, 'RON') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'RON')
if found: return val
if not _combustion_data_loaded: _load_combustion_data()
if method:
value = retrieve_from_df_dict(RON_sources, CASRN, 'RON', method)
Expand Down Expand Up @@ -354,8 +355,9 @@ def MON(CASRN, method=None):
Learning Projects for Fuel Property Prediction." Journal of Open Source
Software 2, no. 17 (2017): 401.
'''
val, found = database_constant_lookup(CASRN, 'MON') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'MON')
if found: return val
if not _combustion_data_loaded: _load_combustion_data()
if method:
value = retrieve_from_df_dict(MON_sources, CASRN, 'MON', method)
Expand Down Expand Up @@ -387,8 +389,9 @@ def ignition_delay_methods(CASRN):
--------
ignition_delay
"""
val, found = database_constant_lookup(CASRN, 'IGNITION_DELAY') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE:
val, found = database_constant_lookup(CASRN, 'IGNITION_DELAY')
if found: return val
if not _combustion_data_loaded: _load_combustion_data()
return list_available_methods_from_df_dict(ignition_delay_sources, CASRN, 'IGNITION_DELAY')

Expand Down
22 changes: 13 additions & 9 deletions chemicals/critical.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
import os

from fluids.constants import N_A, R, R_inv

from chemicals import data_reader as dr
from chemicals import miscdata
from chemicals.data_reader import (data_source,
list_available_methods_from_df_dict,
Expand Down Expand Up @@ -405,8 +405,9 @@ def Tc(CASRN, method=None):
Estimation Based on Zero, First and Second Order Methods." In
Proceedings of the AIChE Spring Meeting, 21, 1996.
'''
val, found = database_constant_lookup(CASRN, 'Tc') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'Tc')
if found: return val
if not _critical_data_loaded: _load_critical_data()
if method:
return retrieve_from_df_dict(Tc_sources, CASRN, 'Tc', method)
Expand Down Expand Up @@ -583,8 +584,9 @@ def Pc(CASRN, method=None):
Estimation Based on Zero, First and Second Order Methods." In
Proceedings of the AIChE Spring Meeting, 21, 1996.
'''
val, found = database_constant_lookup(CASRN, 'Pc') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'Pc')
if found: return val
if not _critical_data_loaded: _load_critical_data()
if method:
return retrieve_from_df_dict(Pc_sources, CASRN, 'Pc', method)
Expand Down Expand Up @@ -755,8 +757,9 @@ def Vc(CASRN, method=None):
.. [20] Fedors, R. F. "A Method to Estimate Critical Volumes." AIChE
Journal 25, no. 1 (1979): 202-202. https://doi.org/10.1002/aic.690250129.
'''
val, found = database_constant_lookup(CASRN, 'Vc') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'Vc')
if found: return val
if not _critical_data_loaded: _load_critical_data()
if method:
return retrieve_from_df_dict(Vc_sources, CASRN, 'Vc', method)
Expand Down Expand Up @@ -924,8 +927,9 @@ def Zc(CASRN, method=None):
Tc-PR." AIChE Journal n/a, no. n/a (n.d.): e17518.
https://doi.org/10.1002/aic.17518.
'''
val, found = database_constant_lookup(CASRN, 'Zc') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'Zc')
if found: return val
if not _critical_data_loaded: _load_critical_data()
if method:
return retrieve_from_df_dict(Zc_sources, CASRN, 'Zc', method)
Expand Down
71 changes: 34 additions & 37 deletions chemicals/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def make_df_sparse(df, non_sparse_columns=[]):

def register_df_source(folder, name, sep='\t', index_col=0, csv_kwargs=None,
postload=None, sparsify=False, int_CAS=False):
if csv_kwargs is None:
csv_kwargs = {}
if csv_kwargs is None: csv_kwargs = {}
load_cmds[name] = (folder, name, sep, index_col, csv_kwargs, postload, sparsify, int_CAS)

'''The following flags will strip out the excess memory usage of redundant
Expand Down Expand Up @@ -166,7 +165,7 @@ def retrieve_from_df(df, index, key):
df_index = df.index
if df_index.dtype is int64_dtype and isinstance(index, str):
try: index = CAS_to_int(index)
except: pass
except: return None
if index in df_index:
if isinstance(key, (int, str)):
return get_value_from_df(df, index, key)
Expand All @@ -177,7 +176,7 @@ def retrieve_any_from_df(df, index, keys):
df_index = df.index
if df_index.dtype is int64_dtype and isinstance(index, str):
try: index = CAS_to_int(index)
except: pass
except: return None
if index not in df.index: return None
for key in keys:
value = df.at[index, key]
Expand All @@ -191,24 +190,23 @@ def get_value_from_df(df, index, key):
value = df.at[index, key]
try:
return None if isnan(value) else float(value)
except TypeError:
# Not a number
except TypeError: # Not a number
return value

def list_available_methods_from_df_dict(df_dict, index, key):
methods = []
int_index = None
int_index = None if isinstance(index, str) else index # Assume must be string or int
for method, df in df_dict.items():
df_index = df.index
if df_index.dtype is int64_dtype and isinstance(index, str):
if df_index.dtype is int64_dtype:
if int_index is None:
try:
int_index = CAS_to_int(index)
except:
int_index = 'skip'
elif int_index == 'skip':
continue
if (int_index in df_index) and not isnan(df.at[int_index, key]):
else:
(int_index in df_index) and not isnan(df.at[int_index, key]) and methods.append(method)
elif int_index != 'skip' and (int_index in df_index) and not isnan(df.at[int_index, key]):
methods.append(method)
elif (index in df_index) and not isnan(df.at[index, key]):
methods.append(method)
Expand Down Expand Up @@ -239,41 +237,40 @@ def list_available_methods_from_df(df, index, keys_by_method):

DATABASE_CONSTANTS_CACHE = {}
def cached_constant_lookup(CASi, prop):
if CONSTANTS_CURSOR is None:
init_constants_db()
prop_idx = CONSTANT_DATABASE_NAME_TO_IDX[prop]
try:
return DATABASE_CONSTANTS_CACHE[CASi][prop_idx], True
except KeyError:
pass
# Fetch and store the whole row
CONSTANTS_CURSOR.execute("SELECT * FROM constants WHERE `index`=?", (str(CASi),))
result = CONSTANTS_CURSOR.fetchone()
DATABASE_CONSTANTS_CACHE[CASi] = result
if result is not None:
if CONSTANTS_CURSOR is None: init_constants_db()
if CASi in DATABASE_CONSTANTS_CACHE:
result = DATABASE_CONSTANTS_CACHE[CASi]
else:
# Fetch and store the whole row
CONSTANTS_CURSOR.execute("SELECT * FROM constants WHERE `index`=?", (str(CASi),))
result = CONSTANTS_CURSOR.fetchone()
DATABASE_CONSTANTS_CACHE[CASi] = result
if result is None:
# Result the value, and whether the compound was in the index
return result, False
else:
prop_idx = CONSTANT_DATABASE_NAME_TO_IDX[prop]
return result[prop_idx], True

# Result the value, and whether the compound was in the index
return result, False

def init_constants_db():
global CONSTANTS_CURSOR
import sqlite3
conn = sqlite3.connect(path_join(source_path, 'Misc', 'default.sqlite'))
CONSTANTS_CURSOR = conn.cursor()

def database_constant_lookup(CAS, prop):
if not USE_CONSTANTS_DATABASE:
return None, False
try:
CASi = CAS_to_int(CAS)
except:
if type(CAS) is not int:
def database_constant_lookup(CASi, prop):
if isinstance(CASi, str): # Assume it must be either an int or string
try:
CASi = CAS_to_int(CASi)
except:
return None, False
else:
# Was already an int
CASi = CAS
try:
return cached_constant_lookup(CASi, prop)
except:
except (TypeError, KeyError) as e:
raise e from None
except Exception:
# Prevent database lookup after first failure considering it should work everytime.
# It will possibly fail for users every time if database has not been created.
global USE_CONSTANTS_DATABASE
USE_CONSTANTS_DATABASE = False
return None, False
8 changes: 5 additions & 3 deletions chemicals/dipole.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
'dipole_moment_methods',
'dipole_moment_all_methods']

from chemicals import data_reader as dr
from chemicals.data_reader import (data_source, database_constant_lookup,
retrieve_any_from_df_dict,
list_available_methods_from_df_dict,
register_df_source,
retrieve_any_from_df_dict,
retrieve_from_df_dict)
from chemicals.miscdata import PSI4_2022A
from chemicals.utils import (PY37, can_load_data, mark_numba_incompatible,
Expand Down Expand Up @@ -186,8 +187,9 @@ def dipole_moment(CASRN, method=None):
Program." WIREs Computational Molecular Science 2, no. 4 (2012): 556-65.
https://doi.org/10.1002/wcms.93.
'''
val, found = database_constant_lookup(CASRN, 'dipole_moment') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'dipole_moment')
if found: return val
if not _dipole_data_loaded: _load_dipole_data()
if method:
return retrieve_from_df_dict(dipole_sources, CASRN, 'dipole_moment', method)
Expand Down
16 changes: 10 additions & 6 deletions chemicals/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
__all__ = ['GWP', 'ODP', 'logP',
'GWP_all_methods', 'ODP_all_methods', 'logP_all_methods',
'GWP_methods', 'ODP_methods', 'logP_methods']
from chemicals import data_reader as dr
from chemicals import miscdata
from chemicals.data_reader import (
data_source, list_available_methods_from_df, database_constant_lookup,
Expand Down Expand Up @@ -248,8 +249,9 @@ def GWP(CASRN, method=None):
Other Weak Atmospheric Absorbers." Reviews of Geophysics 58, no. 3
(2020): e2019RG000691. https://doi.org/10.1029/2019RG000691.
'''
val, found = database_constant_lookup(CASRN, 'GWP') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'GWP')
if found: return val
if not _GWP_ODP_data_loaded: _load_GWP_ODP_data()
if method:
if method in _IPCC_2014_GWP_keys_by_method:
Expand Down Expand Up @@ -364,8 +366,9 @@ def ODP(CASRN, method=None):
Project-Report No. 52, Geneva, Switzerland, 516 p.
https://www.wmo.int/pages/prog/arep/gaw/ozone_2010/documents/Ozone-Assessment-2010-complete.pdf
'''
val, found = database_constant_lookup(CASRN, 'ODP') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'ODP')
if found: return val
if not _GWP_ODP_data_loaded: _load_GWP_ODP_data()
if method:
key = _ODP_keys_by_method[method]
Expand Down Expand Up @@ -448,8 +451,9 @@ def logP(CASRN, method=None):
.. [2] Haynes, W.M., Thomas J. Bruno, and David R. Lide. CRC Handbook of
Chemistry and Physics, 95E. Boca Raton, FL: CRC press, 2014.
'''
val, found = database_constant_lookup(CASRN, 'logP') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'logP')
if found: return val
if not _logP_data_loaded: _load_logP_data()
if method:
return retrieve_from_df_dict(logP_sources, CASRN, 'logP', method)
Expand Down
12 changes: 7 additions & 5 deletions chemicals/lennard_jones.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
import os

from fluids.constants import k

from chemicals import data_reader as dr
from chemicals.data_reader import (data_source, database_constant_lookup,
list_available_methods_from_df_dict,
register_df_source,
Expand Down Expand Up @@ -252,8 +252,9 @@ def Stockmayer(CASRN='', Tm=None, Tb=None, Tc=None, Zc=None, omega=None,
Ranges of Temperature and Density." The Journal of Supercritical Fluids
76 (April 2013): 94-114. doi:10.1016/j.supflu.2013.02.002.
'''
val, found = database_constant_lookup(CASRN, 'Stockmayer') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'Stockmayer')
if found: return val
if not _LJ_data_loaded: _load_LJ_data()
if method is not None:
if method == FLYNN:
Expand Down Expand Up @@ -411,8 +412,9 @@ def molecular_diameter(CASRN=None, Tc=None, Pc=None, Vc=None, Zc=None, omega=Non
Ranges of Temperature and Density." The Journal of Supercritical Fluids
76 (April 2013): 94-114. doi:10.1016/j.supflu.2013.02.002.
'''
val, found = database_constant_lookup(CASRN, 'molecular_diameter') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'molecular_diameter')
if found: return val
if not _LJ_data_loaded: _load_LJ_data()
if method is not None:
if method == FLYNN:
Expand Down
12 changes: 7 additions & 5 deletions chemicals/molecular_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'linear', 'linear_methods', 'linear_all_methods']

from fluids.numerics import horner, interp

from chemicals import data_reader as dr
from chemicals.data_reader import (data_source, database_constant_lookup,
list_available_methods_from_df_dict,
register_df_source,
Expand Down Expand Up @@ -171,8 +171,9 @@ def RG(CASRN, method=None):
.. [2] Kooijman, Harry A., and Ross Taylor. The ChemSep Book. Books on
Demand Norderstedt, Germany, 2000.
'''
val, found = database_constant_lookup(CASRN, 'RG') if method is None else (None, False)
if found: return val
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'RG')
if found: return val
if not _RG_data_loaded: _load_RG_data()
if method:
value = retrieve_from_df_dict(RG_sources, CASRN, 'RG', method)
Expand Down Expand Up @@ -256,8 +257,9 @@ def linear(CASRN, method=None):
Program." WIREs Computational Molecular Science 2, no. 4 (2012): 556-65.
https://doi.org/10.1002/wcms.93.
'''
val, found = database_constant_lookup(CASRN, 'linear') if method is None else (None, False)
if found: return bool(val)
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'linear')
if found: return bool(val)
if not _RG_data_loaded: _load_RG_data()
if method:
value = retrieve_from_df_dict(linear_sources, CASRN, 'linear', method)
Expand Down
Loading

0 comments on commit 94a63cc

Please sign in to comment.