Skip to content

Commit

Permalink
Changed onnx method
Browse files Browse the repository at this point in the history
  • Loading branch information
PennyWieser committed May 16, 2022
1 parent d85bef1 commit 7e6ff74
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\penny\\anaconda3\\lib\\site-packages\\Thermobar\\import_export.py:373: UserWarning: You've got a column heading with a lower case _liq, this is okay if this column is for your own use, but if its an input to Thermobar, it needs to be capitalized (_Liq)\n",
"c:\\users\\penny\\onedrive - oregon state university\\postdoc\\pymme\\mybarometers\\thermobar_outer\\src\\Thermobar\\import_export.py:373: UserWarning: You've got a column heading with a lower case _liq, this is okay if this column is for your own use, but if its an input to Thermobar, it needs to be capitalized (_Liq)\n",
" w.warn(\"You've got a column heading with a lower case _liq, this is okay if this column is for your\"\n"
]
}
Expand All @@ -90,10 +90,19 @@
"execution_count": 4,
"id": "e205b6ac-48b9-4899-9b76-09a3973426a4",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\penny\\anaconda3\\lib\\site-packages\\pandas\\core\\arraylike.py:364: RuntimeWarning: divide by zero encountered in log\n",
" result = getattr(ufunc, method)(*inputs, **kwargs)\n"
]
}
],
"source": [
"# P_Calc_CL=pt.calculate_cpx_liq_press(cpx_comps=Cpx_test, liq_comps=Liq_test, \n",
"# equationP=\"P_Petrelli2020_Cpx_Liq_onnx\")"
"P_Calc_CL=pt.calculate_cpx_liq_press(cpx_comps=Cpx_test, liq_comps=Liq_test, \n",
"equationP=\"P_Petrelli2020_Cpx_Liq_onnx\")"
]
},
{
Expand All @@ -108,100 +117,33 @@
"text": [
"Im normalizing using the Jorgenson method, e.g. 100 total, 2dp\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\penny\\anaconda3\\lib\\site-packages\\pandas\\core\\arraylike.py:364: RuntimeWarning: divide by zero encountered in log\n",
" result = getattr(ufunc, method)(*inputs, **kwargs)\n"
]
},
{
"ename": "RuntimeError",
"evalue": "Please install Thermobar onnx - see ReadMe",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"File \u001b[1;32m~\\anaconda3\\lib\\site-packages\\Thermobar\\clinopyroxene_thermobarometry.py:432\u001b[0m, in \u001b[0;36mP_Jorgenson2022_Cpx_Liq_onnx_local\u001b[1;34m(T, cpx_comps, liq_comps, meltmatch)\u001b[0m\n\u001b[0;32m 431\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 432\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mThermobar_onnx\u001b[39;00m\n\u001b[0;32m 433\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'Thermobar_onnx'",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [5]\u001b[0m, in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[0m P_Calc_CL_J\u001b[38;5;241m=\u001b[39m\u001b[43mpt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcalculate_cpx_liq_press\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcpx_comps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mCpx_test\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mliq_comps\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mLiq_test\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[0;32m 2\u001b[0m \u001b[43mequationP\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mP_Jorgenson2022_Cpx_Liq_onnx_local\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\anaconda3\\lib\\site-packages\\Thermobar\\clinopyroxene_thermobarometry.py:1832\u001b[0m, in \u001b[0;36mcalculate_cpx_liq_press\u001b[1;34m(equationP, cpx_comps, liq_comps, meltmatch, T, eq_tests, Fe3Fet_Liq, H2O_Liq, sigma, Kd_Err)\u001b[0m\n\u001b[0;32m 1829\u001b[0m \u001b[38;5;66;03m# Easiest to treat Machine Learning ones differently\u001b[39;00m\n\u001b[0;32m 1831\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPetrelli\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m equationP \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mJorgenson\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m equationP) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124monnx\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m equationP:\n\u001b[1;32m-> 1832\u001b[0m P_kbar\u001b[38;5;241m=\u001b[39m\u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmeltmatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mCombo_liq_cpxs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1834\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPetrelli\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m equationP \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mJorgenson\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m equationP) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124monnx\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m equationP:\n\u001b[0;32m 1835\u001b[0m df_stats\u001b[38;5;241m=\u001b[39mfunc(meltmatch\u001b[38;5;241m=\u001b[39mCombo_liq_cpxs)\n",
"File \u001b[1;32m~\\anaconda3\\lib\\site-packages\\Thermobar\\clinopyroxene_thermobarometry.py:434\u001b[0m, in \u001b[0;36mP_Jorgenson2022_Cpx_Liq_onnx_local\u001b[1;34m(T, cpx_comps, liq_comps, meltmatch)\u001b[0m\n\u001b[0;32m 432\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mThermobar_onnx\u001b[39;00m\n\u001b[0;32m 433\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[1;32m--> 434\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mPlease install Thermobar onnx - see ReadMe\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 435\u001b[0m path\u001b[38;5;241m=\u001b[39mPath(Thermobar_onnx\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__file__\u001b[39m)\u001b[38;5;241m.\u001b[39mparent\n\u001b[0;32m 436\u001b[0m sess \u001b[38;5;241m=\u001b[39m rt\u001b[38;5;241m.\u001b[39mInferenceSession(\u001b[38;5;28mstr\u001b[39m(path\u001b[38;5;241m/\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mJorg21_Cpx_Liq_Press.onnx\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n",
"\u001b[1;31mRuntimeError\u001b[0m: Please install Thermobar onnx - see ReadMe"
]
}
],
"source": [
"P_Calc_CL_J=pt.calculate_cpx_liq_press(cpx_comps=Cpx_test, liq_comps=Liq_test, \n",
"equationP=\"P_Jorgenson2022_Cpx_Liq_onnx_local\")"
"equationP=\"P_Jorgenson2022_Cpx_Liq_onnx\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0e410020-07b0-47db-8838-eec691d1463d",
"execution_count": 10,
"id": "12542ca6-5e50-4859-bbf1-faf3d29df889",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 4.684525\n",
"1 5.266253\n",
"2 5.255477\n",
"3 5.302085\n",
"4 5.505522\n",
" ... \n",
"142 4.202968\n",
"143 2.646567\n",
"144 2.862119\n",
"145 3.763875\n",
"146 2.615850\n",
"Length: 147, dtype: float32"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"P_Calc_CL_J"
"P_Calc_CL_J=pt.calculate_cpx_only_press(cpx_comps=Cpx_test, \n",
"equationP=\"P_Petrelli2020_Cpx_only_onnx\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cc09ef38-294f-440b-8ee6-80daa06e8e0e",
"execution_count": 11,
"id": "a0c12242-e9bf-4569-a8ba-cebbe95e8f8a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 4.684525\n",
"1 5.266253\n",
"2 5.255477\n",
"3 5.302085\n",
"4 5.505522\n",
" ... \n",
"142 4.202968\n",
"143 2.646567\n",
"144 2.862119\n",
"145 3.763875\n",
"146 2.615850\n",
"Length: 147, dtype: float32"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"P_Calc_CL_J"
"P_Calc_CL_J=pt.calculate_cpx_only_press(cpx_comps=Cpx_test, \n",
"equationP=\"P_Jorgenson2022_Cpx_only_onnx\")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
'skl2onnx',
'onnxruntime'
],
extra_requires={
'onnx': ["https://github.com/PennyWieser/Thermobar_onnx/archive/refs/tags/0.0.1dev.zip"]
extras_require={
'onnx': ["Thermobar_onnx @ https://github.com/PennyWieser/Thermobar_onnx/archive/refs/tags/0.0.1dev.zip"]
},
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
2 changes: 1 addition & 1 deletion src/Thermobar/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# 1) we don't load dependencies by storing it in __init__.py
# 2) we can import it in setup.py for the same reason
# 3) we can import it into your module
__version__ = '0.0.24'
__version__ = '0.0.25'
112 changes: 45 additions & 67 deletions src/Thermobar/clinopyroxene_thermobarometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def P_Jorgenson2022_Cpx_Liq(T=None, *, cpx_comps=None, liq_comps=None, meltmatch

return df_stats

def P_Jorgenson2022_Cpx_Liq_onnx_local(T=None, *, cpx_comps=None,
def P_Jorgenson2022_Cpx_Liq_onnx(T=None, *, cpx_comps=None,
liq_comps=None, meltmatch=None):
'''
Clinopyroxene-liquid barometer of Jorgenson et al. (2022) based on
Expand Down Expand Up @@ -431,7 +431,7 @@ def P_Jorgenson2022_Cpx_Liq_onnx_local(T=None, *, cpx_comps=None,
try:
import Thermobar_onnx
except ImportError:
raise RuntimeError('Please install Thermobar onnx - see ReadMe')
raise RuntimeError('Please do pip install Thermobar[onnx] to be able to access onnx files')
path=Path(Thermobar_onnx.__file__).parent
sess = rt.InferenceSession(str(path/"Jorg21_Cpx_Liq_Press.onnx"))
input_name = sess.get_inputs()[0].name
Expand All @@ -443,64 +443,9 @@ def P_Jorgenson2022_Cpx_Liq_onnx_local(T=None, *, cpx_comps=None,

return df_stats

def P_Jorgenson2022_Cpx_Liq_onnx(T=None, *, cpx_comps=None, liq_comps=None, meltmatch=None):
'''
Clinopyroxene-liquid barometer of Jorgenson et al. (2022) based on
Machine Learning. Uses onnx, so doesnt return voting
:cite:`jorgenson2021machine`
SEE==+-2.7 kbar
'''
if meltmatch is None:
cpx_test=cpx_comps.copy()
liq_test=liq_comps.copy()
cpx_liq_combo=pd.concat([cpx_test, liq_test], axis=1)

if meltmatch is not None:
cpx_liq_combo=meltmatch



Cpx_Liq_ML_in=pd.DataFrame(data={
'SiO2_Liq': cpx_liq_combo['SiO2_Liq'],
'TiO2_Liq': cpx_liq_combo['TiO2_Liq'],
'Al2O3_Liq': cpx_liq_combo['Al2O3_Liq'],
'FeOt_Liq': cpx_liq_combo['FeOt_Liq'],
'MnO_Liq': cpx_liq_combo['MnO_Liq'],
'MgO_Liq': cpx_liq_combo['MgO_Liq'],
'CaO_Liq': cpx_liq_combo['CaO_Liq'],
'Na2O_Liq': cpx_liq_combo['Na2O_Liq'],
'K2O_Liq': cpx_liq_combo['K2O_Liq'],
'Cr2O3_Liq': cpx_liq_combo['Cr2O3_Liq'],
'P2O5_Liq': cpx_liq_combo['P2O5_Liq'],
'SiO2_Cpx': cpx_liq_combo['SiO2_Cpx'],
'TiO2_Cpx': cpx_liq_combo['TiO2_Cpx'],
'Al2O3_Cpx': cpx_liq_combo['Al2O3_Cpx'],
'FeOt_Cpx': cpx_liq_combo['FeOt_Cpx'],
'MnO_Cpx': cpx_liq_combo['MnO_Cpx'],
'MgO_Cpx': cpx_liq_combo['MgO_Cpx'],
'CaO_Cpx': cpx_liq_combo['CaO_Cpx'],
'Na2O_Cpx': cpx_liq_combo['Na2O_Cpx'],
'K2O_Cpx': cpx_liq_combo['K2O_Cpx'],
'Cr2O3_Cpx': cpx_liq_combo['Cr2O3_Cpx'],
})


x_test=Cpx_Liq_ML_in.values



sess = rt.InferenceSession(str(Thermobar_dir/"Jorg21_Cpx_Liq_Press.onnx"))
#sess = rt.InferenceSession(Petrelli2020_Cpx_Liq_Temp.onnx)
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
Pred_P_kbar = sess.run([label_name], {input_name: x_test.astype(np.float32)})[0]

P_kbar=pd.Series(Pred_P_kbar[:, 0])
return P_kbar

return df_stats


def P_Petrelli2020_Cpx_Liq_onnx(T=None, *, cpx_comps=None, liq_comps=None, meltmatch=None):
'''
Expand Down Expand Up @@ -550,7 +495,18 @@ def P_Petrelli2020_Cpx_Liq_onnx(T=None, *, cpx_comps=None, liq_comps=None, meltm

# using Onnx #Thermobar_dir/
#str(load_dir / "model.onnx")
sess = rt.InferenceSession(str(Thermobar_dir/"Petrelli2020_Cpx_Liq_Press.onnx"))


try:
import Thermobar_onnx
except ImportError:
raise RuntimeError('Please do pip install Thermobar[onnx] to be able to access onnx files')
path=Path(Thermobar_onnx.__file__).parent
sess = rt.InferenceSession(str(path/"Petrelli2020_Cpx_Liq_Press.onnx"))




#sess = rt.InferenceSession(Petrelli2020_Cpx_Liq_Temp.onnx)
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
Expand Down Expand Up @@ -954,10 +910,15 @@ def T_Jorgenson2022_Cpx_Liq_onnx(P=None, *, cpx_comps=None, liq_comps=None, melt

x_test=Cpx_Liq_ML_in.values

try:
import Thermobar_onnx
except ImportError:
raise RuntimeError('Please do pip install Thermobar[onnx] to be able to access onnx files')
path=Path(Thermobar_onnx.__file__).parent
sess = rt.InferenceSession(str(path/"Jorg21_Cpx_Liq_Temp.onnx"))



sess = rt.InferenceSession(str(Thermobar_dir/"Jorg21_Cpx_Liq_Temp.onnx"))
#sess = rt.InferenceSession(str(Thermobar_dir/"Jorg21_Cpx_Liq_Temp.onnx"))
#sess = rt.InferenceSession(Petrelli2020_Cpx_Liq_Temp.onnx)
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
Expand Down Expand Up @@ -1166,7 +1127,14 @@ def P_Petrelli2020_Cpx_only_onnx(T=None, *, cpx_comps):
x_test=Cpx_test_noID_noT.values


sess = rt.InferenceSession(str(Thermobar_dir/"Petrelli2020_Cpx_only_Press.onnx"))
try:
import Thermobar_onnx
except ImportError:
raise RuntimeError('Please do pip install Thermobar[onnx] to be able to access onnx files')
path=Path(Thermobar_onnx.__file__).parent
sess = rt.InferenceSession(str(path/"Petrelli2020_Cpx_only_Press.onnx"))


#sess = rt.InferenceSession(Petrelli2020_Cpx_Liq_Temp.onnx)
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
Expand Down Expand Up @@ -1279,9 +1247,13 @@ def P_Jorgenson2022_Cpx_only_onnx(T=None, *, cpx_comps):
x_test=Cpx_test_noID_noT.values


try:
import Thermobar_onnx
except ImportError:
raise RuntimeError('Please do pip install Thermobar[onnx] to be able to access onnx files')
path=Path(Thermobar_onnx.__file__).parent
sess = rt.InferenceSession(str(path/"Jorg21_Cpx_only_Press.onnx"))

sess = rt.InferenceSession(str(Thermobar_dir/"Jorg21_Cpx_only_Press.onnx"))
#sess = rt.InferenceSession(Petrelli2020_Cpx_Liq_Temp.onnx)
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
Pred_P_kbar = sess.run([label_name], {input_name: x_test.astype(np.float32)})[0]
Expand Down Expand Up @@ -1610,8 +1582,14 @@ def T_Jorgenson2022_Cpx_only_onnx(P=None, *, cpx_comps):
x_test=Cpx_test_noID_noT.values


sess = rt.InferenceSession(str(Thermobar_dir/"Jorg21_Cpx_only_Temp.onnx"))
#sess = rt.InferenceSession(Petrelli2020_Cpx_Liq_Temp.onnx)
try:
import Thermobar_onnx
except ImportError:
raise RuntimeError('Please do pip install Thermobar[onnx] to be able to access onnx files')
path=Path(Thermobar_onnx.__file__).parent
sess = rt.InferenceSession(str(path/"Jorg21_Cpx_only_Temp.onnx"))


input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
Pred_T_K = sess.run([label_name], {input_name: x_test.astype(np.float32)})[0]
Expand Down Expand Up @@ -1705,8 +1683,8 @@ def T_Petrelli2020_Cpx_only_withH2O(P=None, *, cpx_comps):
P_Put2003, P_Put2008_eq30, P_Put2008_eq31, P_Put2008_eq32c, P_Mas2013_eqalk32c,
P_Mas2013_Palk2012, P_Wieser2021_H2O_indep, P_Neave2017, P_Petrelli2020_Cpx_Liq,
P_Jorgenson2022_Cpx_Liq, P_Jorgenson2022_Cpx_Liq_onnx, P_Jorgenson2022_Cpx_Liq_Norm,
P_Jorgenson2022_Cpx_Liq_onnx_local,
P_Petrelli2020_Cpx_Liq_onnx,
P_Jorgenson2022_Cpx_Liq_onnx,
P_Petrelli2020_Cpx_Liq_onnx,
P_Put2008_eq32a, P_Put2008_eq32b, P_Wang2021_eq1,
P_Petrelli2020_Cpx_only, P_Petrelli2020_Cpx_only_withH2O, P_Nimis1999_BA} # put on outside

Expand Down

0 comments on commit 7e6ff74

Please sign in to comment.