{ "cells": [ { "cell_type": "markdown", "id": "6ec6587f-6e4f-42fd-bb7c-fec379bbce7f", "metadata": {}, "source": [ "## First lets load various python things" ] }, { "cell_type": "code", "execution_count": 1, "id": "de8684e6-b0fb-417d-9a7f-1b0862caa915", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import Thermobar as pt" ] }, { "cell_type": "markdown", "id": "c8bf6b87-b356-49a2-a625-b1df865af8e8", "metadata": {}, "source": [ "## This is the ideal datafram order for oxides, and their masses\n", "- To make this fancy matrix math work, its easiest to have a common column heading between them all. So, we load in our excel with Sample_ID_Liq. " ] }, { "cell_type": "code", "execution_count": 2, "id": "aa02633a-3ade-41f6-a5db-610a62887e2a", "metadata": {}, "outputs": [], "source": [ "df_ideal_oxide = pd.DataFrame(columns=['Sample_ID_Liq', 'SiO2', 'TiO2', 'Al2O3',\n", "'FeOt', 'MnO', 'MgO', 'CaO', 'Na2O', 'K2O',\n", "'Cr2O3', 'P2O5'])\n", "\n", "oxide_mass = {'SiO2': 60.0843, 'MgO': 40.3044,\n", "'MnO': 70.9375, 'FeOt': 71.8464, 'CaO': 56.0774,\n", "'Al2O3': 101.961,'Na2O': 61.9789, 'K2O': 94.196,\n", "'TiO2': 79.8788, 'P2O5': 141.937, 'Cr2O3': 151.9982}\n", "\n", "# Now we turn this dictionary of oxide mass into a pandas dataframe so matrix functions can be used. \n", "oxide_mass_df = pd.DataFrame.from_dict(\n", " oxide_mass, orient='index').T\n", "oxide_mass_df['Sample_ID_Liq'] = 'MolWt'\n", "oxide_mass_df.set_index('Sample_ID_Liq', inplace=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "741d049d-0f1f-4545-9d95-a9b9a41ddcd6", "metadata": {}, "outputs": [], "source": [ "oxide_mass_df_altered=oxide_mass_df.set_index('Sample_ID_Liq')" ] }, { "cell_type": "code", "execution_count": 10, "id": "bd1b9156-788c-4ebf-a7d4-8a11b9b0a57f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SiO2MgOMnOFeOtCaOAl2O3Na2OK2OTiO2P2O5Cr2O3
060.084340.304470.937571.846456.0774101.96161.978994.19679.8788141.937151.9982
\n", "
" ], "text/plain": [ " SiO2 MgO MnO FeOt CaO Al2O3 Na2O K2O \\\n", "0 60.0843 40.3044 70.9375 71.8464 56.0774 101.961 61.9789 94.196 \n", "\n", " TiO2 P2O5 Cr2O3 \n", "0 79.8788 141.937 151.9982 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oxide_mass_df2 = pd.DataFrame.from_dict(\n", " oxide_mass, orient='index').T\n", "oxide_mass_df2" ] }, { "cell_type": "code", "execution_count": 12, "id": "abb42bee-4c13-4421-9a4a-3cdc267dc771", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SiO2MgOMnOFeOtCaOAl2O3Na2OK2OTiO2P2O5Cr2O3Sample_ID_Liq
060.084340.304470.937571.846456.0774101.96161.978994.19679.8788141.937151.9982MolWt
\n", "
" ], "text/plain": [ " SiO2 MgO MnO FeOt CaO Al2O3 Na2O K2O \\\n", "0 60.0843 40.3044 70.9375 71.8464 56.0774 101.961 61.9789 94.196 \n", "\n", " TiO2 P2O5 Cr2O3 Sample_ID_Liq \n", "0 79.8788 141.937 151.9982 MolWt " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oxide_mass_df2['Sample_ID_Liq'] = 'MolWt'\n", "oxide_mass_df2" ] }, { "cell_type": "code", "execution_count": 3, "id": "8a8514a0-0e8e-4a30-9c40-64fc918fb786", "metadata": {}, "outputs": [], "source": [ "input1=pd.read_excel('Christy_input.xlsx', sheet_name=\"Sheet1\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "d747f344-bbb1-4a89-8a63-72a28d8205ad", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\users\\penny\\onedrive - oregon state university\\postdoc\\pymme\\mybarometers\\thermobar_outer\\src\\Thermobar\\import_export.py:333: UserWarning: We notice you have specified a suffix, but some of your columns already have this suffix. e.g., If you already have _Liq in the file, you shouldnt specify suffix=\"_Liq\" during the import\n", " w.warn('We notice you have specified a suffix, but some of your columns already have this suffix. '\n" ] } ], "source": [ "import Thermobar as pt\n", "input2=pt.import_excel('Christy_input.xlsx', sheet_name=\"Sheet1\", suffix=\"_Liq\").get(\"Liqs\")" ] }, { "cell_type": "markdown", "id": "f4d30d16-76d0-49bb-9754-da5bc525395b", "metadata": {}, "source": [ "## This cell defines the function, you must run it" ] }, { "cell_type": "code", "execution_count": 5, "id": "6c16b987-22e5-4347-8133-ace2879bd4da", "metadata": {}, "outputs": [], "source": [ "def calculate_anhydrous_mol_proportions(df):\n", " '''Import Liq compositions using liq_comps=My_Liquids, returns anhydrous mole proportions\n", "\n", " Parameters\n", " -------\n", "\n", " inputs: df. pandas.DataFrame\n", " Panda DataFrame of input compositions with column headings SiO2, TiO2\n", "\n", " Returns\n", " -------\n", " pandas DataFrame\n", " anhydrous mole proportions with column headings of the form SiO2_Liq_mol_prop, stithced to original dataframe\n", "\n", " '''\n", " # This makes the input match the columns in the oxide mass dataframe\n", " liq_wt = df.reindex(\n", " df_ideal_oxide.columns, axis=1).fillna(0)\n", " # Combine the molecular weight and weight percent dataframes\n", " liq_wt_combo = pd.concat([oxide_mass_df, liq_wt],)\n", " \n", " # Divide throguh by the MolDrop the calculation column\n", " mol_prop_anhyd = liq_wt_combo.div(\n", " liq_wt_combo.loc['MolWt', :], axis='columns').drop(['MolWt'])\n", " mol_prop_anhyd.columns = [\n", " str(col) + '_mol_prop' for col in mol_prop_anhyd.columns]\n", " return mol_prop_anhyd" ] }, { "cell_type": "code", "execution_count": 7, "id": "9de6fc39-122e-466b-99f9-cc13e602dc7d", "metadata": {}, "outputs": [], "source": [ "df=input1\n", "liq_wt = df.reindex(\n", " df_ideal_oxide.columns, axis=1).fillna(0)\n", "# Combine the molecular weight and weight percent dataframes\n", "liq_wt_combo = pd.concat([oxide_mass_df, liq_wt],)\n", "\n", "# Divide throguh by the MolDrop the calculation column\n", "mol_prop_anhyd = liq_wt_combo.div(\n", " liq_wt_combo.loc['MolWt', :], axis='columns').drop(['MolWt'])\n", "mol_prop_anhyd.columns = [\n", " str(col) + '_mol_prop' for col in mol_prop_anhyd.columns]" ] }, { "cell_type": "code", "execution_count": 13, "id": "fead8dd5-247a-4322-aa0e-dae24d36451e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SiO2MgOMnOFeOtCaOAl2O3Na2OK2OTiO2P2O5Cr2O3Sample_ID_Liq
MolWt60.08430040.3044070.93750071.8464056.07740101.96100061.9789094.19600079.878800141.937000151.9982NaN
057.0236024.191800.1038514.361746.9485816.3328993.597020.8968950.6231060.2265840.0000liq1
157.6586002.868920.0841053.906215.9153817.1947993.859481.0186000.6541500.2149350.0000liq2
\n", "
" ], "text/plain": [ " SiO2 MgO MnO FeOt CaO Al2O3 \\\n", "MolWt 60.084300 40.30440 70.937500 71.84640 56.07740 101.961000 \n", "0 57.023602 4.19180 0.103851 4.36174 6.94858 16.332899 \n", "1 57.658600 2.86892 0.084105 3.90621 5.91538 17.194799 \n", "\n", " Na2O K2O TiO2 P2O5 Cr2O3 Sample_ID_Liq \n", "MolWt 61.97890 94.196000 79.878800 141.937000 151.9982 NaN \n", "0 3.59702 0.896895 0.623106 0.226584 0.0000 liq1 \n", "1 3.85948 1.018600 0.654150 0.214935 0.0000 liq2 " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liq_wt_combo" ] }, { "cell_type": "markdown", "id": "777c5b22-337a-4d99-a683-faefb3d8ac2d", "metadata": {}, "source": [ "## This cell uses the function" ] }, { "cell_type": "code", "execution_count": 5, "id": "91457e8a-26e1-495a-a84f-3687330ac9b0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SiO2_mol_propMgO_mol_propMnO_mol_propFeOt_mol_propCaO_mol_propAl2O3_mol_propNa2O_mol_propK2O_mol_propTiO2_mol_propP2O5_mol_propCr2O3_mol_propSample_ID_Liq_mol_prop
00.949060.1040040.0014640.0607090.1239110.1601880.0580360.0095220.0078010.0015960.0NaN
10.9596280.0711810.0011860.0543690.1054860.1686410.0622710.0108140.0081890.0015140.0NaN
\n", "
" ], "text/plain": [ " SiO2_mol_prop MgO_mol_prop MnO_mol_prop FeOt_mol_prop CaO_mol_prop \\\n", "0 0.94906 0.104004 0.001464 0.060709 0.123911 \n", "1 0.959628 0.071181 0.001186 0.054369 0.105486 \n", "\n", " Al2O3_mol_prop Na2O_mol_prop K2O_mol_prop TiO2_mol_prop P2O5_mol_prop \\\n", "0 0.160188 0.058036 0.009522 0.007801 0.001596 \n", "1 0.168641 0.062271 0.010814 0.008189 0.001514 \n", "\n", " Cr2O3_mol_prop Sample_ID_Liq_mol_prop \n", "0 0.0 NaN \n", "1 0.0 NaN " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Calc_Prop=calculate_anhydrous_mol_proportions(df=input1)\n", "Calc_Prop" ] }, { "cell_type": "markdown", "id": "733b6e26-ecc4-4bcf-b994-a204ce03ccd3", "metadata": {}, "source": [ "## Here is an example of how you would write afunction with differen normalizatoin schemes using if statements" ] }, { "cell_type": "code", "execution_count": 6, "id": "942334ec-8f23-47b4-b70c-a9c9f1962f16", "metadata": {}, "outputs": [], "source": [ "def demonstrate_classification_scheme(df, projection=None):\n", " if projection == \"Cpx\":\n", " return \"Im using the Cpx normalizatoin scheme\"\n", " if projection == \"Plag-Amp\":\n", " return \"Im using the Plag-Amp normalizatoin scheme\"\n", " " ] }, { "cell_type": "code", "execution_count": 7, "id": "573dd659-abdb-427c-8318-e6dafb131917", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Im using the Cpx normalizatoin scheme'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demonstrate_classification_scheme(df=input1, projection=\"Cpx\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "e95681f5-fa61-4353-aaf1-e5bc56d8d38b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Im using the Plag-Amp normalizatoin scheme'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demonstrate_classification_scheme(df=input1, projection=\"Plag-Amp\")" ] }, { "cell_type": "markdown", "id": "f8eb20f0-efa6-45ac-a245-11a8936965d0", "metadata": {}, "source": [ "## Here is your function to get started" ] }, { "cell_type": "code", "execution_count": 9, "id": "d6d74e38-afab-4709-88eb-faea47974094", "metadata": {}, "outputs": [], "source": [ "def calculate_liq_components_as_minerals(df, projection=None):\n", " # First, take the input dataframe of liquids, and calculate mole prop. \n", " #Using the function you defined above\n", " mol_prop_alone=calculate_anhydrous_mol_proportions(df=df)\n", " \n", " # Lets ocmbine mol_prop and the input to get 1 big dataframe\n", " mol_prop=pd.concat([df, mol_prop_alone], axis=1)\n", " \n", " # Now add whatever calculation you want to it, e.g., say I want to sum MgO and SiO2 mol proportions\n", " mol_prop['Sum_Mg_Si']=mol_prop['MgO_mol_prop'] + mol_prop['SiO2_mol_prop']\n", " \n", " # Now return this function with new columns added on. Initiall, the coluns go at the end, \n", " #but I can send you some code later to shuffle column order once its all together\n", " \n", " # Add your iff statements, append the projection-specific things on. \n", " # E.g., say projection = Cpx, I want to return a oclumn reading Cpx something \n", " if projection == \"Cpx\":\n", " mol_prop['Cpx_calc1']=5\n", " \n", " if projection == \"Plag\":\n", " mol_prop['Plag_Calc']=10\n", " \n", " if projection == \"Amp\":\n", " mol_prop['Amp_Calc']=20\n", " \n", " # You can either return a specific result beneath the if, or append to a column name and return the whole thing here\n", " \n", " return mol_prop" ] }, { "cell_type": "markdown", "id": "30e855b9-76de-421f-85bb-9cb452e6ab89", "metadata": {}, "source": [ "## Now use the function!\n", "- Scroll to the end to see the new columns" ] }, { "cell_type": "code", "execution_count": 10, "id": "8158fea8-7fb5-4c22-b864-5236bd892011", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Sample_ID_LiqSiO2TiO2Al2O3FeOtMnOMgOCaONa2OK2O...CaO_mol_propAl2O3_mol_propNa2O_mol_propK2O_mol_propTiO2_mol_propP2O5_mol_propCr2O3_mol_propSample_ID_Liq_mol_propSum_Mg_SiCpx_calc1
0liq157.0236020.62310616.3328994.361740.1038514.191806.948583.597020.896895...0.1239110.1601880.0580360.0095220.0078010.0015960.0NaN1.0530635
1liq257.6586000.65415017.1947993.906210.0841052.868925.915383.859481.018600...0.1054860.1686410.0622710.0108140.0081890.0015140.0NaN1.030815
\n", "

2 rows × 26 columns

\n", "
" ], "text/plain": [ " Sample_ID_Liq SiO2 TiO2 Al2O3 FeOt MnO MgO \\\n", "0 liq1 57.023602 0.623106 16.332899 4.36174 0.103851 4.19180 \n", "1 liq2 57.658600 0.654150 17.194799 3.90621 0.084105 2.86892 \n", "\n", " CaO Na2O K2O ... CaO_mol_prop Al2O3_mol_prop \\\n", "0 6.94858 3.59702 0.896895 ... 0.123911 0.160188 \n", "1 5.91538 3.85948 1.018600 ... 0.105486 0.168641 \n", "\n", " Na2O_mol_prop K2O_mol_prop TiO2_mol_prop P2O5_mol_prop Cr2O3_mol_prop \\\n", "0 0.058036 0.009522 0.007801 0.001596 0.0 \n", "1 0.062271 0.010814 0.008189 0.001514 0.0 \n", "\n", " Sample_ID_Liq_mol_prop Sum_Mg_Si Cpx_calc1 \n", "0 NaN 1.053063 5 \n", "1 NaN 1.03081 5 \n", "\n", "[2 rows x 26 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calculate_liq_components_as_minerals(df=input1, projection=\"Cpx\")" ] }, { "cell_type": "code", "execution_count": null, "id": "d3aef38c-fc13-4f9e-b869-1b8a1384ec23", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }