{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aac63f47",
   "metadata": {},
   "source": [
    "# Tutorial for HN module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa2f98e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the necessary packages along with HNpy\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": "e7af9ea2",
   "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": "code",
   "execution_count": 3,
   "id": "5c9e0008",
   "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": "a8f196d9",
   "metadata": {},
   "source": [
    "![diel_loss_plot.png](attachment:diel_loss_plot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "60a0ea31",
   "metadata": {},
   "outputs": [],
   "source": [
    "# instantiate the HN module\n",
    "hn = dd.HN()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ade59a0",
   "metadata": {},
   "source": [
    "Select the ROI to fit the data using the select range method. \n",
    "The select range functions pops in a separate window and allows you two clicks to select the region of interest (ROI).\n",
    "The data in ROI is shown as image in the following cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ae767322",
   "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": "a163d6c4",
   "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": "ec21620b",
   "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": "7ba6f0d7",
   "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": "3c26a8e1",
   "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": "640dca70",
   "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": "df8ea510",
   "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": "code",
   "execution_count": 9,
   "id": "851699f6",
   "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": [
    "#create a file to save fit results using create_analysis file method\n",
    "#before saving fit results an analysis file has to be created\n",
    "\n",
    "hn.create_analysis_file()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bece0584",
   "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": "code",
   "execution_count": null,
   "id": "39d60211",
   "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
}
