{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aa903e1f",
   "metadata": {},
   "source": [
    "# Tutorial for Conductivity module"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "55008c01",
   "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": "e27dd328",
   "metadata": {},
   "outputs": [],
   "source": [
    "# extract the data\n",
    "\n",
    "filename = 'cond_example_data.txt'\n",
    "col_names = ['Freq','Sig1','Sig2']\n",
    "df = pd.read_csv(filename, sep='\\t',index_col=False,usecols = [0,1,2],names=col_names,header=None,skiprows=10,encoding='unicode_escape',engine='python')\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0018dcc4",
   "metadata": {},
   "source": [
    "## Fitting the real part of Conductivity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ec2de83e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Example for Conductivity fitting')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# plot the data \n",
    "\n",
    "x = np.log10(df['Freq'])\n",
    "y = np.log10(df['Sig1'])\n",
    "plt.scatter(x,y,label='example data')\n",
    "plt.xlabel('log f [Hz]')\n",
    "plt.ylabel('log ( $\\sigma´$)')\n",
    "plt.legend()\n",
    "plt.title('Example for Conductivity fitting')"
   ]
  },
  {
   "attachments": {
    "data_example-2.png": {
     "image/png": "[encoded data, 31480 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "f1d5e828",
   "metadata": {},
   "source": [
    "![data_example-2.png](attachment:data_example-2.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "39511da9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# instantiate the HN module\n",
    "cond = dd.Conductivity()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "648e6324-33ae-48b0-8b24-389481a63f58",
   "metadata": {},
   "source": [
    "**Select the region of interest (ROI) to fit the data using the select range method**. <br>\n",
    "The data in ROI is shown as image in the next cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "75be026b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_lower_limit -1.039386875850182 x_upper_limit 5.614181794555549\n"
     ]
    }
   ],
   "source": [
    "#select range\n",
    "x1,y1 = cond.select_range(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a5ad9db",
   "metadata": {},
   "source": [
    "**Plot of the ROI for fitting**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6f00ed52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'log ( $\\\\sigma´$)')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plt.scatter(x1,y1)\n",
    "plt.xlabel('log f [Hz]')\n",
    "plt.ylabel('log ( $\\sigma´$)')"
   ]
  },
  {
   "attachments": {
    "ROI.png": {
     "image/png": "[encoded data, 23160 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "8450c57f",
   "metadata": {},
   "source": [
    "![ROI.png](attachment:ROI.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "38443d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enter the fc value:3\n",
      "enter the DC conductivity value:-10\n",
      "enter the exponent:0.8\n",
      "dumped_parameters {'fc': 1000.0, 'DC': -10.0, 'n': 0.8}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 7,
     "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",
    "cond.dump_parameters()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c1758bae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fit parameters:\n",
      " [274.40483973 -11.04255983   0.97224746]\n",
      " DC cond = -11.043\n",
      "fit parameters dumped for next iteration {'fc': 274.4048397299556, 'DC': -11.04255983252695, 'n': 0.9722474585471939}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# perform least squares fitting\n",
    "# the plot is shown as a image in the next cell\n",
    "\n",
    "cond.fit(x1,y1)\n"
   ]
  },
  {
   "attachments": {
    "final_fitting.png": {
     "image/png": "[encoded data, 32784 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "e0f12880",
   "metadata": {},
   "source": [
    "**Plot of the final fit**\n",
    "![final_fitting.png](attachment:final_fitting.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a01483e5",
   "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:y\n",
      "Enter the analysis_file_name:cond_fit.TXT\n",
      "file exists\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",
    "cond.create_analysis_file()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3acb6038",
   "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",
    "cond.save_fit(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7cd2fd4",
   "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
}
