# Machine-Learning-based Clinopyroxene-only and Clinopyroxene-Liquid Thermobarometry.

## You need to install Thermobar once on your machine, if you haven’t done this yet, uncomment the line below (remove the #)

[1]:

#!pip install Thermobar


### For Machine learning, you also need to pip install the .pkl files that have saved the pretrained model. This is to keep Thermobar smaller so we can still release on Pip

[2]:

#!pip install "https://github.com/PennyWieser/Thermobar_onnx/archive/refs/tags/v.0.0.4.zip"


### First, load the necessary python things

[3]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import Thermobar as pt
pt.__version__

[3]:

'1.0.32'


[4]:

out=pt.import_excel('Cpx_Liq_Example.xlsx', sheet_name="Sheet1")
my_input=out['my_input']
Liqs=out['Liqs']
Cpxs=out['Cpxs']

[5]:

import joblib as j
j.__version__

[5]:

'1.2.0'


## Using saved models

• This uses pickles, the model will change with different versions of python

• But, it does give you the voting approach of Jorgenson, with the median and standard deviation of the trees

[6]:

P_T_EqTests_pkl=pt.calculate_cpx_liq_press_temp(cpx_comps=Cpxs, liq_comps=Liqs,
equationP="P_Jorgenson2022_Cpx_Liq",
equationT="T_Jorgenson2022_Cpx_Liq",
H2O_Liq=3, eq_tests=False)

Im normalizing using the Jorgenson method, e.g. 100 total, 2dp
Im normalizing using the Jorgenson method, e.g. 100 total, 2dp
Youve selected a P-independent function

---------------------------------------------------------------------------
UnpicklingError                           Traceback (most recent call last)
c:\Users\penny\Box\Postdoc\MyBarometers\Thermobar_outer\docs\Examples\Cpx_Cpx_Liq_Thermobarometry\MachineLearning_Cpx_Liq_Thermobarometry.ipynb Cell 12 line 1
----> <a href='vscode-notebook-cell:/c%3A/Users/penny/Box/Postdoc/MyBarometers/Thermobar_outer/docs/Examples/Cpx_Cpx_Liq_Thermobarometry/MachineLearning_Cpx_Liq_Thermobarometry.ipynb#X13sZmlsZQ%3D%3D?line=0'>1</a> P_T_EqTests_pkl=pt.calculate_cpx_liq_press_temp(cpx_comps=Cpxs, liq_comps=Liqs,
<a href='vscode-notebook-cell:/c%3A/Users/penny/Box/Postdoc/MyBarometers/Thermobar_outer/docs/Examples/Cpx_Cpx_Liq_Thermobarometry/MachineLearning_Cpx_Liq_Thermobarometry.ipynb#X13sZmlsZQ%3D%3D?line=1'>2</a>                                                 equationP="P_Jorgenson2022_Cpx_Liq",
<a href='vscode-notebook-cell:/c%3A/Users/penny/Box/Postdoc/MyBarometers/Thermobar_outer/docs/Examples/Cpx_Cpx_Liq_Thermobarometry/MachineLearning_Cpx_Liq_Thermobarometry.ipynb#X13sZmlsZQ%3D%3D?line=2'>3</a>                                                 equationT="T_Jorgenson2022_Cpx_Liq",
<a href='vscode-notebook-cell:/c%3A/Users/penny/Box/Postdoc/MyBarometers/Thermobar_outer/docs/Examples/Cpx_Cpx_Liq_Thermobarometry/MachineLearning_Cpx_Liq_Thermobarometry.ipynb#X13sZmlsZQ%3D%3D?line=3'>4</a>                                                 H2O_Liq=3, eq_tests=False)

File c:\users\penny\box\postdoc\mybarometers\thermobar_outer\src\Thermobar\clinopyroxene_thermobarometry.py:2385, in calculate_cpx_liq_press_temp(liq_comps, cpx_comps, meltmatch, equationP, equationT, T, P, iterations, Fe3Fet_Liq, H2O_Liq, T_K_guess, eq_tests)
2383 if equationT is not None:
2384     if ('Petrelli' in equationT or "Jorgenson" in equationT) and "onnx" not in equationT:
-> 2385         T_func_all=calculate_cpx_liq_temp(meltmatch=Combo_liq_cpxs,
2386         equationT=equationT, P="Solve")
2387         T_func = T_func_all.T_K_calc
2388         Median_T=T_func_all.Median_Trees

File c:\users\penny\box\postdoc\mybarometers\thermobar_outer\src\Thermobar\clinopyroxene_thermobarometry.py:2209, in calculate_cpx_liq_temp(equationT, cpx_comps, liq_comps, meltmatch, P, eq_tests, H2O_Liq, Fe3Fet_Liq, sigma, Kd_Err)
2207 # Easiest to treat Machine Learning ones differently
2208 if ('Petrelli' in equationT or "Jorgenson" in equationT) and "onnx" not in equationT:
-> 2209     df_stats=func(meltmatch=Combo_liq_cpxs)
2210     T_K=df_stats['T_K_calc']
2212 elif ('Petrelli' in equationT or "Jorgenson" in equationT) and "onnx" in equationT:

File c:\users\penny\box\postdoc\mybarometers\thermobar_outer\src\Thermobar\clinopyroxene_thermobarometry.py:941, in T_Jorgenson2022_Cpx_Liq(P, cpx_comps, liq_comps, meltmatch)
938 Thermobar_dir=Path(Thermobar_onnx.__file__).parent
940 with open(Thermobar_dir/'ETR_Temp_Jorg21_Cpx_Liq_NotNorm_sklearn_1_3.pkl', 'rb') as f:
944 Pred_T_K=ETR_Temp_J22_Cpx_Liq.predict(x_test)
945 df_stats, df_voting=get_voting_stats_ExtraTreesRegressor(x_test, ETR_Temp_J22_Cpx_Liq)


[ ]:

P_T_EqTests_pkl=pt.calculate_cpx_liq_press_temp(cpx_comps=Cpxs, liq_comps=Liqs,
equationP="P_Petrelli2020_Cpx_Liq",
equationT="T_Petrelli2020_Cpx_Liq",
T=1300,
H2O_Liq=0, eq_tests=False)
P_T_EqTests_pkl

c:\users\penny\onedrive\documents\postdoc_missing\mybarometers\thermobar_outer\src\Thermobar\core.py:1561: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
cpx_calc.loc[(AlVI_minus_Na<0), 'Jd']=cpx_calc['Al_VI_cat_6ox']

Youve selected a P-independent function
Youve selected a T-independent function
Youve selected a T-independent function

P_kbar_calc T_K_calc Delta_P_kbar_Iter Delta_T_K_Iter Median_Trees_P Std_Trees_P IQR_Trees_P Median_Trees_T Std_Trees_T IQR_Trees_T
0 2.012874 1436.353636 0 0 2.0000 2.014248 2.014248 1434.15 37.734604 37.734604
1 2.385743 1422.872182 0 0 2.0000 2.516823 2.516823 1423.15 58.692923 58.692923
2 2.031163 1388.809636 0 0 2.0000 1.335548 1.335548 1398.15 100.495001 100.495001
3 2.175246 1418.986727 0 0 2.0000 2.290651 2.290651 1423.15 62.539834 62.539834
4 2.194894 1358.247818 0 0 2.0000 2.305614 2.305614 1373.15 80.220643 80.220643
5 6.846097 1483.411818 0 0 5.0000 7.297546 7.297546 1473.15 90.448301 90.448301
6 6.049409 1473.710000 0 0 4.0500 6.731814 6.731814 1466.15 85.025018 85.025018
7 9.449931 1483.408909 0 0 8.0000 8.203280 8.203280 1473.15 88.209247 88.209247
8 4.635460 1462.542727 0 0 3.9745 5.282807 5.282807 1449.15 63.993766 63.993766
9 6.107720 1462.148182 0 0 4.9850 6.226865 6.226865 1448.65 68.141951 68.141951
10 10.808077 1506.979455 0 0 9.7000 8.622634 8.622634 1493.15 95.431122 95.431122
11 5.851677 1461.882727 0 0 4.0430 6.151823 6.151823 1453.15 70.670782 70.670782
12 7.826454 1482.080909 0 0 5.0000 7.960571 7.960571 1473.15 94.254987 94.254987
13 10.300083 1496.978000 0 0 8.0000 8.729611 8.729611 1473.15 92.537615 92.537615
14 10.300083 1496.978000 0 0 8.0000 8.729611 8.729611 1473.15 92.537615 92.537615
15 10.300083 1496.978000 0 0 8.0000 8.729611 8.729611 1473.15 92.537615 92.537615
16 10.775014 1500.690364 0 0 9.6000 9.103614 9.103614 1478.15 97.447848 97.447848
17 9.463969 1500.291818 0 0 8.0000 8.543886 8.543886 1473.15 99.245966 99.245966
18 11.731851 1506.468182 0 0 10.0000 9.319109 9.319109 1493.15 93.497859 93.497859
19 9.431157 1495.166727 0 0 7.2500 8.513357 8.513357 1483.15 95.327742 95.327742

### Using onnx models

-Using Onnx means you will always get the same answer. But, you dont get the voting results.

[ ]:

P_T_EqTests_onnx=pt.calculate_cpx_liq_press_temp(cpx_comps=Cpxs, liq_comps=Liqs,
equationP="P_Petrelli2020_Cpx_Liq_onnx",
equationT="T_Petrelli2020_Cpx_Liq_onnx",
T=1300,
H2O_Liq=0, eq_tests=True)
P_T_EqTests_onnx

c:\users\penny\onedrive\documents\postdoc_missing\mybarometers\thermobar_outer\src\Thermobar\core.py:1561: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
cpx_calc.loc[(AlVI_minus_Na<0), 'Jd']=cpx_calc['Al_VI_cat_6ox']

Youve selected a P-independent function
Youve selected a T-independent function
Youve selected a T-independent function
Using Fe3FeT from input file to calculate Kd Fe-Mg

c:\users\penny\onedrive\documents\postdoc_missing\mybarometers\thermobar_outer\src\Thermobar\core.py:1561: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
cpx_calc.loc[(AlVI_minus_Na<0), 'Jd']=cpx_calc['Al_VI_cat_6ox']

P_kbar_calc T_K_calc Eq Tests Neave2017? Delta_P_kbar_Iter Delta_T_K_Iter Delta_Kd_Put2008 Delta_Kd_Mas2013 Delta_EnFs_Mollo13 Delta_EnFs_Put1999 Delta_CaTs_Put1999 ... Delta_EnFs_I_M_Mollo13 CaTs_Pred_Put1999 Delta_CaTs_I_M_Put1999 CrCaTs_Pred_Put1999 Delta_CrCaTs_I_M_Put1999 CaTi_Pred_Put1999 Delta_CaTi_I_M_Put1999 Jd_Pred_Put1999 Delta_Jd_Put1999 Delta_Jd_I_M_Put1999
0 2.012875 1436.355103 False 0 0 0.047419 0.090012 0.001449 0.024126 0.016574 ... 0.001449 0.013801 -0.016574 0.000000 0.009562 0.042741 0.001728 0.016176 0.000880 0.000880
1 2.385741 1422.873779 False 0 0 0.045964 0.082381 0.006055 0.027422 0.023015 ... 0.006055 0.013802 -0.023015 0.000000 0.004122 0.055375 0.010437 0.017422 0.000092 0.000092
2 2.031165 1388.808716 False 0 0 0.048797 0.163165 0.066580 0.001942 0.074375 ... 0.066580 0.017510 -0.074375 0.000000 0.003245 0.028704 0.042963 0.018321 0.000236 0.000236
3 2.175245 1418.988159 False 0 0 0.042373 0.094228 0.002781 0.034579 0.030821 ... 0.002781 0.015006 -0.030821 0.000000 0.003909 0.049805 0.008608 0.019322 0.002988 0.002988
4 2.194893 1358.247559 False 0 0 0.030098 0.090116 0.007655 0.053517 0.031539 ... -0.007655 0.011571 -0.031539 0.000000 0.001315 0.049482 0.013529 0.027939 0.005724 0.005724
5 6.846100 1483.413330 False 0 0 0.005372 0.116855 0.004940 0.039434 0.014345 ... -0.004940 0.014345 0.014345 0.297720 0.277125 0.054752 0.029543 0.019475 0.006131 0.006131
6 6.049410 1473.711060 False 0 0 0.023545 0.078386 0.005900 0.031687 0.015636 ... -0.005900 0.015636 0.015636 0.130559 0.110135 0.051265 0.034527 0.015666 0.002724 0.002724
7 9.449942 1483.410156 False 0 0 0.063562 0.067061 0.016248 0.032413 0.016880 ... -0.016248 0.016880 0.016880 0.148010 0.125617 0.044143 0.030339 0.018284 0.011828 0.011828
8 4.635460 1462.543457 False 0 0 0.050014 0.082419 0.017233 0.046123 0.007871 ... -0.017233 0.016970 0.007871 0.001101 0.017485 0.052259 0.016795 0.015820 0.008623 0.008623
9 6.107720 1462.150146 False 0 0 0.028846 0.106569 0.012896 0.038910 0.014222 ... -0.012896 0.014222 0.014222 0.003516 0.010965 0.057974 0.016738 0.018212 0.004594 0.004594
10 10.808082 1506.980347 False 0 0 0.004871 0.122097 0.012649 0.033275 0.016476 ... -0.012649 0.016476 0.016476 0.857156 0.836370 0.041477 0.043774 0.016485 0.003244 0.003244
11 5.851676 1461.884766 False 0 0 0.028515 0.096888 0.008876 0.030977 0.016659 ... -0.008876 0.016659 0.016659 0.001648 0.014651 0.046359 0.034214 0.017362 0.009226 0.009226
12 7.826456 1482.082642 False 0 0 0.058960 0.048230 0.020680 0.045986 0.015751 ... -0.020680 0.015751 0.015751 0.574581 0.555774 0.050106 0.037956 0.016896 0.002418 0.002418
13 10.300095 1496.979736 False 0 0 0.019680 0.100772 0.045940 0.065435 0.015095 ... -0.045940 0.015095 0.015095 0.052732 0.042651 0.049537 0.031106 0.019058 0.018333 0.018333
14 10.300095 1496.979736 False 0 0 0.019680 0.100772 0.045940 0.065435 0.015095 ... -0.045940 0.015095 0.015095 0.052732 0.042651 0.049537 0.031106 0.019058 0.018333 0.018333
15 10.300095 1496.979736 False 0 0 0.019680 0.100772 0.045940 0.065435 0.015095 ... -0.045940 0.015095 0.015095 0.052732 0.042651 0.049537 0.031106 0.019058 0.018333 0.018333
16 10.775019 1500.692017 False 0 0 0.019647 0.093438 0.022763 0.043110 0.017213 ... -0.022763 0.017213 0.017213 0.356916 0.342040 0.050433 0.037272 0.018307 0.002141 0.002141
17 9.463977 1500.293457 False 0 0 0.046416 0.060569 0.024181 0.052928 0.015462 ... -0.024181 0.015462 0.015462 0.603117 0.588110 0.051726 0.035954 0.017911 0.017911 0.017911
18 11.731860 1506.469360 False 0 0 0.025058 0.088923 0.023740 0.043642 0.016187 ... -0.023740 0.016187 0.016187 0.160617 0.141226 0.057375 0.024345 0.017608 0.003885 0.003885
19 9.431162 1495.168457 False 0 0 0.021281 0.110671 0.019382 0.046185 0.015849 ... -0.019382 0.015849 0.015849 0.310037 0.296888 0.054943 0.032126 0.016572 0.002465 0.002465

20 rows × 132 columns

[ ]:

P_T_EqTests_pkl_Jorg=pt.calculate_cpx_liq_press_temp(cpx_comps=Cpxs, liq_comps=Liqs,
equationP="P_Jorgenson2022_Cpx_Liq",
equationT="T_Jorgenson2022_Cpx_Liq",
T=1300,
H2O_Liq=0, eq_tests=False)
P_T_EqTests_pkl_Jorg

Im normalizing using the Jorgenson method, e.g. 100 total, 2dp
Im normalizing using the Jorgenson method, e.g. 100 total, 2dp
Youve selected a P-independent function

c:\users\penny\onedrive\documents\postdoc_missing\mybarometers\thermobar_outer\src\Thermobar\core.py:1561: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
cpx_calc.loc[(AlVI_minus_Na<0), 'Jd']=cpx_calc['Al_VI_cat_6ox']

Youve selected a T-independent function
Youve selected a T-independent function

P_kbar_calc T_K_calc Delta_P_kbar_Iter Delta_T_K_Iter Median_Trees_P Std_Trees_P IQR_Trees_P Median_Trees_T Std_Trees_T IQR_Trees_T
0 1.000000 1355.150000 0 0 1.00 0.000000 0.000000 1355.15 3.183231e-12 3.183231e-12
1 2.000000 1285.150000 0 0 2.00 0.000000 0.000000 1285.15 4.092726e-12 4.092726e-12
2 2.000000 1238.150000 0 0 2.00 0.000000 0.000000 1238.15 4.092726e-12 4.092726e-12
3 2.000000 1273.150000 0 0 2.00 0.000000 0.000000 1273.15 4.088325e-12 4.088325e-12
4 2.000000 1238.150000 0 0 2.00 0.000000 0.000000 1238.15 4.092726e-12 4.092726e-12
5 8.419458 1446.550498 0 0 8.00 7.241728 7.241728 1433.15 9.848431e+01 9.848431e+01
6 7.142953 1432.000746 0 0 7.00 6.398140 6.398140 1423.15 8.921307e+01 8.921307e+01
7 10.956871 1450.309204 0 0 10.00 8.394058 8.394058 1433.15 1.100438e+02 1.100438e+02
8 5.233430 1401.965920 0 0 4.01 5.269948 5.269948 1408.15 6.391195e+01 6.391195e+01
9 7.305813 1417.891294 0 0 7.00 6.391484 6.391484 1413.15 7.970738e+01 7.970738e+01
10 12.012990 1466.025622 0 0 10.00 8.862633 8.862633 1448.15 1.268004e+02 1.268004e+02
11 6.400005 1412.503234 0 0 5.00 5.518597 5.518597 1413.15 8.188373e+01 8.188373e+01
12 8.628219 1439.751990 0 0 8.00 7.451500 7.451500 1428.15 9.935043e+01 9.935043e+01
13 11.173898 1470.112687 0 0 10.00 8.591048 8.591048 1449.15 1.162777e+02 1.162777e+02
14 11.173898 1470.112687 0 0 10.00 8.591048 8.591048 1449.15 1.162777e+02 1.162777e+02
15 11.173898 1470.112687 0 0 10.00 8.591048 8.591048 1449.15 1.162777e+02 1.162777e+02
16 11.642547 1469.455970 0 0 10.00 8.798096 8.798096 1448.15 1.235193e+02 1.235193e+02
17 11.593090 1468.349005 0 0 10.00 8.791730 8.791730 1448.15 1.283919e+02 1.283919e+02
18 13.392448 1468.339055 0 0 10.00 8.665804 8.665804 1448.15 1.180593e+02 1.180593e+02
19 9.767323 1465.881343 0 0 9.70 7.868123 7.868123 1448.15 1.155477e+02 1.155477e+02
[ ]:

P_T_EqTests_onnx_Jorg=pt.calculate_cpx_liq_press_temp(cpx_comps=Cpxs, liq_comps=Liqs,
equationP="P_Jorgenson2022_Cpx_Liq_onnx",
equationT="T_Jorgenson2022_Cpx_Liq_onnx",
T=1300,
H2O_Liq=0, eq_tests=False)
P_T_EqTests_onnx_Jorg

Im normalizing using the Jorgenson method, e.g. 100 total, 2dp
Im normalizing using the Jorgenson method, e.g. 100 total, 2dp
Youve selected a P-independent function

c:\users\penny\onedrive\documents\postdoc_missing\mybarometers\thermobar_outer\src\Thermobar\core.py:1561: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
cpx_calc.loc[(AlVI_minus_Na<0), 'Jd']=cpx_calc['Al_VI_cat_6ox']

Youve selected a T-independent function
Youve selected a T-independent function

P_kbar_calc T_K_calc Delta_P_kbar_Iter Delta_T_K_Iter
0 1.000001 1355.152954 0 0
1 2.000002 1285.151611 0 0
2 2.000002 1238.816772 0 0
3 2.000002 1273.152954 0 0
4 2.000002 1238.821777 0 0
5 8.419460 1452.618042 0 0
6 7.142956 1438.607666 0 0
7 10.956876 1446.971313 0 0
8 5.233430 1408.817017 0 0
9 7.305816 1418.339111 0 0
10 12.012997 1482.637695 0 0
11 6.400006 1409.916504 0 0
12 8.628222 1452.076050 0 0
13 11.173905 1468.081055 0 0
14 11.173905 1468.081055 0 0
15 11.173905 1468.081055 0 0
16 11.642552 1479.100586 0 0
17 11.593097 1460.817383 0 0
18 13.392457 1472.319458 0 0
19 9.767327 1466.672852 0 0
[ ]: