{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "91397969",
   "metadata": {},
   "source": [
    "# Tutorial for HN module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a0c1262",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the necessary packages along with HavNegpy\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import HavNegpy as dd\n",
    "%matplotlib qt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d2df3e15",
   "metadata": {},
   "outputs": [],
   "source": [
    "# extract the data\n",
    "\n",
    "filename = 'hn_example_data.txt'\n",
    "col_names = ['log f', 'log eps2']\n",
    "df = pd.read_csv(filename, sep=',',index_col=False,usecols = [0,1],names=col_names,header=None,skiprows=2,encoding='unicode_escape',engine='python')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50ea9f0d",
   "metadata": {},
   "source": [
    "## Fitting example of single HN function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "550f0c8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Example for HN fitting')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# plot the data \n",
    "\n",
    "x,y = df['log f'], df['log eps2']\n",
    "plt.scatter(x,y,label='example data')\n",
    "plt.xlabel('log f [Hz]')\n",
    "plt.ylabel('log $\\epsilon$\"')\n",
    "plt.legend()\n",
    "plt.title('Example for HN fitting')"
   ]
  },
  {
   "attachments": {
    "diel_loss_plot.png": {
     "image/png": "[encoded data, 26284 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "d83dbfb3",
   "metadata": {},
   "source": [
    "![diel_loss_plot.png](attachment:diel_loss_plot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b27545c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# instantiate the HN module\n",
    "hn = dd.HN()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be4655a1",
   "metadata": {},
   "source": [
    "**Select the region of interest (ROI) to fit the data using the select range method**. \n",
    ">The data in ROI is shown as image in the following cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2691ec1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_lower_limit 2.5234707551868087 x_upper_limit 4.4693345833813005\n"
     ]
    }
   ],
   "source": [
    "#select range\n",
    "hn = dd.HN()\n",
    "x1,y1 = hn.select_range(x,y)"
   ]
  },
  {
   "attachments": {
    "ROI.png": {
     "image/png": "[encoded data, 24556 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "6f48e461",
   "metadata": {},
   "source": [
    "**Plot of the ROI to fit the HN function**\n",
    "![ROI.png](attachment:ROI.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "348838d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter the beta value:0.5\n",
      "enter the gamma value:1\n",
      "enter the fm:3.75\n",
      "enter the deps:0.5\n",
      "enter the cond:0\n",
      "enter the s:1\n",
      "dumped_parameters {'beta': 0.5, 'gamma': 1.0, 'freq': 5623.413251903491, 'deps': 0.5, 'cond': 0.0, 'n': 1.0}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#dump the initial guess parameters using dump parameters method (varies for each fn), which dumps the parameters in a json file'\n",
    "#this is required before performing the first fitting as it takes the initial guess from the json file created\n",
    "\n",
    "hn.dump_parameters_hn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "86b183cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loaded parameters \n",
      " {'beta': 0.5, 'gamma': 1.0, 'freq': 5623.413251903491, 'deps': 0.5, 'cond': 0.0, 'n': 1.0}\n"
     ]
    }
   ],
   "source": [
    "# view the initial fit based on the dumped parameters\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "hn.initial_view_hn(x1,y1)"
   ]
  },
  {
   "attachments": {
    "initial_guess.png": {
     "image/png": "[encoded data, 30112 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "18257209",
   "metadata": {},
   "source": [
    "**Plot of the initial fit based on the supplied parameters**\n",
    "\n",
    "![initial_guess.png](attachment:initial_guess.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5cf8aed0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Choose the fit function\n",
      " 1 -- HN, 2 -- HN with cond, 3 -- Hn-flank, 4 -- double HN, 5 -- double HN with cond:1\n",
      "0.7326563571608663 0.34641197051059136 1945.8637251236246 3.2548369440810547\n",
      "log fmax: 3.8521833734663224\n",
      "fit parameters dumped for next iteration {'beta': 0.7326563571608663, 'gamma': 0.34641197051059136, 'freq': 1945.8637251236246, 'deps': 3.2548369440810547, 'cond': 0, 'n': 0}\n"
     ]
    }
   ],
   "source": [
    "# perform least squares fitting\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "hn.fit(x1,y1)"
   ]
  },
  {
   "attachments": {
    "fit_example.png": {
     "image/png": "[encoded data, 37988 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "9aec0e05",
   "metadata": {},
   "source": [
    "**Plot of the final fit of the HN function to the data**\n",
    "\n",
    "![fit_example.png](attachment:fit_example.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37ab42fa",
   "metadata": {},
   "source": [
    "**An analysis file has to be created only once during the whole run (i.e. all fit runs can be saved in the same file, unless you change the function or require saving separate processes in separate files.)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a9788ce9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do you want to use an existing file to save fit results? \n",
      " eg: existing file to save HN parameters, y or n:n\n",
      "Choose the fit function\n",
      " 1 -- HN, 2 -- HN with cond, 3 -- HN-flank, 4 -- double HN, 5 -- double HN with cond:1\n",
      "Enter the analysis_file_name:hn_fit.TXT\n",
      "file did not exist, created hn_fit.TXT\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# before saving fit results an analysis file has to be created using create_analysis file method\n",
    "\n",
    "hn.create_analysis_file()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "010155cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#save the fit results using save_fit method of the corresponding fit function\n",
    "#takes one argument, read more on the documentation\n",
    "\n",
    "hn.save_fit_hn(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a09f3a0f",
   "metadata": {},
   "source": [
    "## HN function together with conductivity contribution.\n",
    "**We will use the same data set but will include the conductivity slope and analyze the data.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "759995ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_lower_limit 1.0824713571766393 x_upper_limit 4.495941541576136\n"
     ]
    }
   ],
   "source": [
    "#select range\n",
    "# select peak along with conductivity slope\n",
    "hn = dd.HN()\n",
    "x1,y1 = hn.select_range(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ce974539",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Example for fitting HN with conducitivity')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# view the selected data\n",
    "plt.scatter(x1,y1,label='example data')\n",
    "plt.xlabel('log f [Hz]')\n",
    "plt.ylabel('log $\\epsilon$\"')\n",
    "plt.legend()\n",
    "plt.title('Example for fitting HN with conducitivity')\n"
   ]
  },
  {
   "attachments": {
    "diel_loss_cond_plot.png": {
     "image/png": "[encoded data, 29148 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "1296e8fa",
   "metadata": {},
   "source": [
    "**ROI selected for fitting**\n",
    "\n",
    "![diel_loss_cond_plot.png](attachment:diel_loss_cond_plot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "67455e73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter the beta value:0.7\n",
      "enter the gamma value:0.35\n",
      "enter the fm:3.85\n",
      "enter the deps:3.2\n",
      "enter the cond:10\n",
      "enter the s:1\n",
      "dumped_parameters {'beta': 0.7, 'gamma': 0.35, 'freq': 7079.457843841381, 'deps': 3.2, 'cond': 10.0, 'n': 1.0}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#dump the initial guess parameters using dump parameters method (varies for each fn), which dumps the parameters in a json file'\n",
    "#this is required before performing the first fitting as it takes the initial guess from the json file created\n",
    "\n",
    "hn.dump_parameters_hn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8462132c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loaded parameters \n",
      " {'beta': 0.7, 'gamma': 0.35, 'freq': 7079.457843841381, 'deps': 3.2, 'cond': 10.0, 'n': 1.0}\n"
     ]
    }
   ],
   "source": [
    "# view the initial fit based on the dumped parameters\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "hn.initial_view_hn(x1,y1)"
   ]
  },
  {
   "attachments": {
    "initial_guess_cond.png": {
     "image/png": "[encoded data, 37516 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "af51443d",
   "metadata": {},
   "source": [
    "**Plot of the initial fit based on the supplied parameters**\n",
    "\n",
    "![initial_guess_cond.png](attachment:initial_guess_cond.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5eac2052",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Choose the fit function\n",
      " 1 -- HN, 2 -- HN with cond, 3 -- Hn-flank, 4 -- double HN, 5 -- double HN with cond:2\n",
      "0.8032582310807094 0.23705517267041565 1429.0416095139626 3.6780528141976907 4.81979373031708 0.9999999999999999\n",
      "log fmax: 3.841362413197138\n",
      "fit parameters dumped for next iteration {'beta': 0.8032582310807094, 'gamma': 0.23705517267041565, 'freq': 1429.0416095139626, 'deps': 3.6780528141976907, 'cond': 4.81979373031708, 'n': 0.9999999999999999}\n"
     ]
    }
   ],
   "source": [
    "# perform least squares fitting\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "hn.fit(x1,y1)"
   ]
  },
  {
   "attachments": {
    "fit_example_cond.png": {
     "image/png": "[encoded data, 60104 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "154e19bc",
   "metadata": {},
   "source": [
    "**Plot of the final fit**\n",
    "\n",
    "![fit_example_cond.png](attachment:fit_example_cond.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c84bc03e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do you want to use an existing file to save fit results? \n",
      " eg: existing file to save HN parameters, y or n:n\n",
      "Choose the fit function\n",
      " 1 -- HN, 2 -- HN with cond, 3 -- HN-flank, 4 -- double HN, 5 -- double HN with cond:2\n",
      "Enter the analysis_file_name:hn_cond_fit.TXT\n",
      "file did not exist, created hn_cond_fit.TXT\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# before saving fit results an analysis file has to be created using create_analysis file methodd\n",
    "\n",
    "hn.create_analysis_file()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2b338cb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#save the fit results using save_fit method of the corresponding fit function\n",
    "#takes one argument, read more on the documentation\n",
    "\n",
    "hn.save_fit_hn(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78633690",
   "metadata": {},
   "source": [
    "## Fitting example for double HN function\n",
    "**We will now look at an example to fit two HN functions using a new datafile**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cb07b1ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = 'double_hn_example_data.txt'\n",
    "col_names = ['log f', 'log eps2']\n",
    "df2 = pd.read_csv(filename, sep=',',index_col=False,usecols = [0,1],names=col_names,header=None,skiprows=2,encoding='unicode_escape',engine='python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bd943e54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_lower_limit 0.12476912070759627 x_upper_limit 4.0548939906347545\n"
     ]
    }
   ],
   "source": [
    "#select range\n",
    "# select peak along with conductivity slope\n",
    "hn = dd.HN()\n",
    "x,y = df2['log f'], df2['log eps2']\n",
    "x1,y1 = hn.select_range(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6dfb4c19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Example for fitting HN with conducitivity')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# view the selected data\n",
    "plt.scatter(x1,y1,label='example data')\n",
    "plt.xlabel('log f [Hz]')\n",
    "plt.ylabel('log $\\epsilon$\"')\n",
    "plt.legend()\n",
    "plt.title('Example for fitting double HN function')"
   ]
  },
  {
   "attachments": {
    "ROI_double%20HN.png": {
     "image/png": "[encoded data, 30596 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "a41b60e5",
   "metadata": {},
   "source": [
    "**ROI selected for fitting**\n",
    "![ROI_double%20HN.png](attachment:ROI_double%20HN.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "642a60f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter the beta1 value:0.5\n",
      "enter the gamma1 value:1\n",
      "enter the fmax1:0.76\n",
      "enter the deps1:0.35\n",
      "enter the beta2 value:0.5\n",
      "enter the gamma2 value:1\n",
      "enter the fmax2:3.25\n",
      "enter the deps2:0.7\n",
      "enter the cond:0\n",
      "enter the s:1\n",
      "dumped_parameters {'beta1': 0.5, 'gamma1': 1.0, 'freq1': 5.7543993733715695, 'deps1': 0.35, 'beta2': 0.5, 'gamma2': 1.0, 'freq2': 1778.2794100389228, 'deps2': 0.7, 'cond': 0.0, 'n': 1.0}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#dump the initial guess parameters using dump parameters method (varies for each fn), which dumps the parameters in a json file'\n",
    "#this is required before performing the first fitting as it takes the initial guess from the json file created\n",
    "\n",
    "hn.dump_parameters_double_hn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "617d0444",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loaded parameters \n",
      " {'beta1': 0.5, 'gamma1': 1.0, 'freq1': 5.7543993733715695, 'deps1': 0.35, 'beta2': 0.5, 'gamma2': 1.0, 'freq2': 1778.2794100389228, 'deps2': 0.7, 'cond': 0.0, 'n': 1.0}\n"
     ]
    }
   ],
   "source": [
    "# view the initial fit based on the dumped parameters\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "hn.initial_view_double_hn(x1,y1)"
   ]
  },
  {
   "attachments": {
    "initial_guess_double%20HN.png": {
     "image/png": "[encoded data, 41320 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "6c8ebe43",
   "metadata": {},
   "source": [
    "**Plot of the initial fit based on the supplied parameters**\n",
    "![initial_guess_double%20HN.png](attachment:initial_guess_double%20HN.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a20565ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Choose the fit function\n",
      " 1 -- HN, 2 -- HN with cond, 3 -- Hn-flank, 4 -- double HN, 5 -- double HN with cond:4\n",
      "[0.5, 1.0, 5.7543993733715695, 0.35, 0.5, 1.0, 1778.2794100389228, 0.7]\n",
      "log fmax1: 0.6528760482656042 \n",
      "log fmax2: 3.3454996773912202\n",
      "fit parameters dumped for next iteration {'beta1': 0.5574923893637207, 'gamma1': 0.32191102439091196, 'freq1': 0.6634391099941143, 'deps1': 0.6070123067438288, 'beta2': 0.9684862515337489, 'gamma2': 0.19288873795787148, 'freq2': 539.4342107551406, 'deps2': 0.37175853830216565, 'cond': 0, 'n': 1}\n"
     ]
    }
   ],
   "source": [
    "# perform least squares fitting\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "hn.fit(x1,y1)"
   ]
  },
  {
   "attachments": {
    "fit_example_double%20HN.png": {
     "image/png": "[encoded data, 54032 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "4b66eacf",
   "metadata": {},
   "source": [
    "**Plot of the final fit**\n",
    "![fit_example_double%20HN.png](attachment:fit_example_double%20HN.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "918f1d05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do you want to use an existing file to save fit results? \n",
      " eg: existing file to save HN parameters, y or n:n\n",
      "Choose the fit function\n",
      " 1 -- HN, 2 -- HN with cond, 3 -- HN-flank, 4 -- double HN, 5 -- double HN with cond:4\n",
      "Enter the analysis_file_name:double_HN_fit.TXT\n",
      "file did not exist, created double_HN_fit.TXT\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# before saving fit results an analysis file has to be created using create_analysis file method\n",
    "\n",
    "hn.create_analysis_file()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c0f07bcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#save the fit results using save_fit method of the corresponding fit function\n",
    "#takes one argument, read more on the documentation\n",
    "\n",
    "hn.save_fit_double_HN(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f308af88",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
