{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GlassPy: predicting\n",
    "===================\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GlassPy contains two prediction models: `GlassNet` and `ViscNet`. GlassNet is a multitask deep neural network capable of predicting 85 different glass properties and the temperature dependence of viscosity. ViscNet is a physics-informed deep neural network capable of predicting the temperature dependence of viscosity. Both models were trained with data from the SciGlass database.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GlassNet basic usage\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below is a minimal example of how to load and use the GlassNet model.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>T0</th>\n",
       "      <th>T1</th>\n",
       "      <th>T2</th>\n",
       "      <th>T3</th>\n",
       "      <th>T4</th>\n",
       "      <th>T5</th>\n",
       "      <th>T6</th>\n",
       "      <th>T7</th>\n",
       "      <th>T8</th>\n",
       "      <th>T9</th>\n",
       "      <th>...</th>\n",
       "      <th>Cp1673K</th>\n",
       "      <th>TMaxGrowthVelocity</th>\n",
       "      <th>MaxGrowthVelocity</th>\n",
       "      <th>CrystallizationPeak</th>\n",
       "      <th>CrystallizationOnset</th>\n",
       "      <th>SurfaceTensionAboveTg</th>\n",
       "      <th>SurfaceTension1173K</th>\n",
       "      <th>SurfaceTension1473K</th>\n",
       "      <th>SurfaceTension1573K</th>\n",
       "      <th>SurfaceTension1673K</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1656.182898</td>\n",
       "      <td>1275.016879</td>\n",
       "      <td>1178.333341</td>\n",
       "      <td>1064.154863</td>\n",
       "      <td>956.535513</td>\n",
       "      <td>893.408138</td>\n",
       "      <td>857.634124</td>\n",
       "      <td>820.458542</td>\n",
       "      <td>779.195534</td>\n",
       "      <td>755.95825</td>\n",
       "      <td>...</td>\n",
       "      <td>1697.079401</td>\n",
       "      <td>1021.145482</td>\n",
       "      <td>-6.457834</td>\n",
       "      <td>898.463696</td>\n",
       "      <td>818.476775</td>\n",
       "      <td>0.315797</td>\n",
       "      <td>0.299307</td>\n",
       "      <td>0.304303</td>\n",
       "      <td>0.314809</td>\n",
       "      <td>0.312099</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            T0           T1           T2           T3          T4          T5  \\\n",
       "0  1656.182898  1275.016879  1178.333341  1064.154863  956.535513  893.408138   \n",
       "\n",
       "           T6          T7          T8         T9  ...      Cp1673K  \\\n",
       "0  857.634124  820.458542  779.195534  755.95825  ...  1697.079401   \n",
       "\n",
       "   TMaxGrowthVelocity  MaxGrowthVelocity  CrystallizationPeak  \\\n",
       "0         1021.145482          -6.457834           898.463696   \n",
       "\n",
       "   CrystallizationOnset  SurfaceTensionAboveTg  SurfaceTension1173K  \\\n",
       "0            818.476775               0.315797             0.299307   \n",
       "\n",
       "   SurfaceTension1473K  SurfaceTension1573K  SurfaceTension1673K  \n",
       "0             0.304303             0.314809             0.312099  \n",
       "\n",
       "[1 rows x 85 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glasspy.predict import GlassNet\n",
    "\n",
    "model = GlassNet()\n",
    "composition = \"Li2O(SiO2)2\"\n",
    "predictions = model.predict(composition)\n",
    "predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A composition can also be defined using a dictionary.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>T0</th>\n",
       "      <th>T1</th>\n",
       "      <th>T2</th>\n",
       "      <th>T3</th>\n",
       "      <th>T4</th>\n",
       "      <th>T5</th>\n",
       "      <th>T6</th>\n",
       "      <th>T7</th>\n",
       "      <th>T8</th>\n",
       "      <th>T9</th>\n",
       "      <th>...</th>\n",
       "      <th>Cp1673K</th>\n",
       "      <th>TMaxGrowthVelocity</th>\n",
       "      <th>MaxGrowthVelocity</th>\n",
       "      <th>CrystallizationPeak</th>\n",
       "      <th>CrystallizationOnset</th>\n",
       "      <th>SurfaceTensionAboveTg</th>\n",
       "      <th>SurfaceTension1173K</th>\n",
       "      <th>SurfaceTension1473K</th>\n",
       "      <th>SurfaceTension1573K</th>\n",
       "      <th>SurfaceTension1673K</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1656.182898</td>\n",
       "      <td>1275.016879</td>\n",
       "      <td>1178.333341</td>\n",
       "      <td>1064.154863</td>\n",
       "      <td>956.535513</td>\n",
       "      <td>893.408138</td>\n",
       "      <td>857.634124</td>\n",
       "      <td>820.458542</td>\n",
       "      <td>779.195534</td>\n",
       "      <td>755.95825</td>\n",
       "      <td>...</td>\n",
       "      <td>1697.079401</td>\n",
       "      <td>1021.145482</td>\n",
       "      <td>-6.457834</td>\n",
       "      <td>898.463696</td>\n",
       "      <td>818.476775</td>\n",
       "      <td>0.315797</td>\n",
       "      <td>0.299307</td>\n",
       "      <td>0.304303</td>\n",
       "      <td>0.314809</td>\n",
       "      <td>0.312099</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            T0           T1           T2           T3          T4          T5  \\\n",
       "0  1656.182898  1275.016879  1178.333341  1064.154863  956.535513  893.408138   \n",
       "\n",
       "           T6          T7          T8         T9  ...      Cp1673K  \\\n",
       "0  857.634124  820.458542  779.195534  755.95825  ...  1697.079401   \n",
       "\n",
       "   TMaxGrowthVelocity  MaxGrowthVelocity  CrystallizationPeak  \\\n",
       "0         1021.145482          -6.457834           898.463696   \n",
       "\n",
       "   CrystallizationOnset  SurfaceTensionAboveTg  SurfaceTension1173K  \\\n",
       "0            818.476775               0.315797             0.299307   \n",
       "\n",
       "   SurfaceTension1473K  SurfaceTension1573K  SurfaceTension1673K  \n",
       "0             0.304303             0.314809             0.312099  \n",
       "\n",
       "[1 rows x 85 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "composition = {\n",
    "    \"SiO2\": 2,\n",
    "    \"Li2O\": 1,\n",
    "}\n",
    "predictions = model.predict(composition)\n",
    "predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GlassNet also accepts `pandas` DataFrames as input. Note that each row represents a material and that only columns related to compositions can exist in the DataFrame.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Li2O</th>\n",
       "      <th>Na2O</th>\n",
       "      <th>SiO2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Li2O  Na2O  SiO2\n",
       "0     1     0     2\n",
       "1     0     1     2\n",
       "2     1     1     2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data = [\n",
    "    [1, 0, 2],\n",
    "    [0, 1, 2],\n",
    "    [1, 1, 2],\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data, columns=[\"Li2O\", \"Na2O\", \"SiO2\"])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>T0</th>\n",
       "      <th>T1</th>\n",
       "      <th>T2</th>\n",
       "      <th>T3</th>\n",
       "      <th>T4</th>\n",
       "      <th>T5</th>\n",
       "      <th>T6</th>\n",
       "      <th>T7</th>\n",
       "      <th>T8</th>\n",
       "      <th>T9</th>\n",
       "      <th>...</th>\n",
       "      <th>Cp1673K</th>\n",
       "      <th>TMaxGrowthVelocity</th>\n",
       "      <th>MaxGrowthVelocity</th>\n",
       "      <th>CrystallizationPeak</th>\n",
       "      <th>CrystallizationOnset</th>\n",
       "      <th>SurfaceTensionAboveTg</th>\n",
       "      <th>SurfaceTension1173K</th>\n",
       "      <th>SurfaceTension1473K</th>\n",
       "      <th>SurfaceTension1573K</th>\n",
       "      <th>SurfaceTension1673K</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1656.182898</td>\n",
       "      <td>1275.016816</td>\n",
       "      <td>1178.333278</td>\n",
       "      <td>1064.154806</td>\n",
       "      <td>956.535427</td>\n",
       "      <td>893.408194</td>\n",
       "      <td>857.634073</td>\n",
       "      <td>820.458542</td>\n",
       "      <td>779.195490</td>\n",
       "      <td>755.958250</td>\n",
       "      <td>...</td>\n",
       "      <td>1697.079401</td>\n",
       "      <td>1021.145445</td>\n",
       "      <td>-6.457834</td>\n",
       "      <td>898.463737</td>\n",
       "      <td>818.476730</td>\n",
       "      <td>0.315797</td>\n",
       "      <td>0.299307</td>\n",
       "      <td>0.304303</td>\n",
       "      <td>0.314809</td>\n",
       "      <td>0.312099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1896.291536</td>\n",
       "      <td>1513.411183</td>\n",
       "      <td>1279.021743</td>\n",
       "      <td>1123.006920</td>\n",
       "      <td>1035.724060</td>\n",
       "      <td>954.737548</td>\n",
       "      <td>912.160878</td>\n",
       "      <td>849.827548</td>\n",
       "      <td>809.466760</td>\n",
       "      <td>781.277537</td>\n",
       "      <td>...</td>\n",
       "      <td>1484.994346</td>\n",
       "      <td>1064.050420</td>\n",
       "      <td>-6.311865</td>\n",
       "      <td>967.297186</td>\n",
       "      <td>852.792449</td>\n",
       "      <td>0.281895</td>\n",
       "      <td>0.307567</td>\n",
       "      <td>0.284295</td>\n",
       "      <td>0.286678</td>\n",
       "      <td>0.274625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1490.654549</td>\n",
       "      <td>1233.614128</td>\n",
       "      <td>1101.165164</td>\n",
       "      <td>979.065643</td>\n",
       "      <td>921.910502</td>\n",
       "      <td>852.842278</td>\n",
       "      <td>785.283328</td>\n",
       "      <td>760.537597</td>\n",
       "      <td>724.843873</td>\n",
       "      <td>694.542212</td>\n",
       "      <td>...</td>\n",
       "      <td>1746.768638</td>\n",
       "      <td>1077.191654</td>\n",
       "      <td>-5.137042</td>\n",
       "      <td>856.429238</td>\n",
       "      <td>831.987634</td>\n",
       "      <td>0.323965</td>\n",
       "      <td>0.303260</td>\n",
       "      <td>0.322071</td>\n",
       "      <td>0.318062</td>\n",
       "      <td>0.325812</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            T0           T1           T2           T3           T4  \\\n",
       "0  1656.182898  1275.016816  1178.333278  1064.154806   956.535427   \n",
       "1  1896.291536  1513.411183  1279.021743  1123.006920  1035.724060   \n",
       "2  1490.654549  1233.614128  1101.165164   979.065643   921.910502   \n",
       "\n",
       "           T5          T6          T7          T8          T9  ...  \\\n",
       "0  893.408194  857.634073  820.458542  779.195490  755.958250  ...   \n",
       "1  954.737548  912.160878  849.827548  809.466760  781.277537  ...   \n",
       "2  852.842278  785.283328  760.537597  724.843873  694.542212  ...   \n",
       "\n",
       "       Cp1673K  TMaxGrowthVelocity  MaxGrowthVelocity  CrystallizationPeak  \\\n",
       "0  1697.079401         1021.145445          -6.457834           898.463737   \n",
       "1  1484.994346         1064.050420          -6.311865           967.297186   \n",
       "2  1746.768638         1077.191654          -5.137042           856.429238   \n",
       "\n",
       "   CrystallizationOnset  SurfaceTensionAboveTg  SurfaceTension1173K  \\\n",
       "0            818.476730               0.315797             0.299307   \n",
       "1            852.792449               0.281895             0.307567   \n",
       "2            831.987634               0.323965             0.303260   \n",
       "\n",
       "   SurfaceTension1473K  SurfaceTension1573K  SurfaceTension1673K  \n",
       "0             0.304303             0.314809             0.312099  \n",
       "1             0.284295             0.286678             0.274625  \n",
       "2             0.322071             0.318062             0.325812  \n",
       "\n",
       "[3 rows x 85 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions = model.predict(df)\n",
    "predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GlassNet can also predict viscosity and the MYEGA viscosity equation parameters.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.43780523, 4.38559973, 2.78879274])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions = model.predict_log10_viscosity(\n",
    "    T=1000,\n",
    "    composition=df,\n",
    ")\n",
    "predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>log10_eta_infinity (Pa.s)</th>\n",
       "      <th>Tg_MYEGA (K)</th>\n",
       "      <th>fragility</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.185329</td>\n",
       "      <td>697.868850</td>\n",
       "      <td>43.223825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.700694</td>\n",
       "      <td>710.957761</td>\n",
       "      <td>35.991400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.583741</td>\n",
       "      <td>637.292106</td>\n",
       "      <td>39.162854</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   log10_eta_infinity (Pa.s)  Tg_MYEGA (K)  fragility\n",
       "0                  -1.185329    697.868850  43.223825\n",
       "1                  -1.700694    710.957761  35.991400\n",
       "2                  -1.583741    637.292106  39.162854"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "viscosity_parameters = model.viscosity_parameters(df)\n",
    "viscosity_parameters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ViscNet basic usage\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The usage of ViscNet is similar to GlassNet. In fact, GlassNet performs better than ViscNet, so it is recommended to use GlassNet for viscosity prediction. Nevertheless, below is a minimal example of loading and using ViscNet.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.0632305, 5.2332606, 6.148955 ], dtype=float32)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glasspy.predict import ViscNet\n",
    "\n",
    "model = ViscNet()\n",
    "log10_viscosity = model.predict(T=1000, composition=df)\n",
    "log10_viscosity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[32.876167 29.261871 34.904648]\n"
     ]
    }
   ],
   "source": [
    "fragility = model.predict_fragility(df)\n",
    "print(fragility)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[752.03784 735.4263  791.71893]\n"
     ]
    }
   ],
   "source": [
    "Tg = model.predict_Tg(df)\n",
    "print(Tg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.3"
  },
  "org": null
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
