{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pesfit as pf\n",
    "from silx.io.dictdump import h5todict\n",
    "import matplotlib.pyplot as plt\n",
    "import lmfit.models as lmm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = h5todict(r'..\\data\\hsymline_symmetrized.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 186)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kEmap = dataset['data']['kimage']\n",
    "kEmap.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1b65a2f80b8>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 17748 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(dataset['data']['E'][:150], kEmap[:150, 100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "xvals = dataset['data']['E'][:150]\n",
    "y = kEmap[:150, 100]\n",
    "yvals = y/y.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdl = pf.lineshape.MultipeakModel(n=3, lineshape=lmm.SkewedGaussianModel, background=lmm.GaussianModel(prefix='bg_'))\n",
    "pars = mdl.make_params()\n",
    "\n",
    "vardict = [{'lp1_':{'center':dict(value=-0.1, min=-0.4, vary=True),\n",
    "                  'amplitude':dict(value=0.3, min=0, max=1, vary=False),\n",
    "                  'sigma':dict(value=0.12, min=0.05, max=2, vary=True),\n",
    "                  'gamma':dict(value=0.05, min=0, max=2, vary=False)}},\n",
    "           \n",
    "           {'lp2_':{'center':dict(value=-0.58, vary=True),\n",
    "                   'amplitude':dict(value=0.25, min=0, max=1, vary=True),\n",
    "                   'sigma':dict(value=0.12, min=0.05, max=2, vary=False),\n",
    "                   'gamma':dict(value=0.05, min=0, max=2, vary=True)}},\n",
    "           \n",
    "           {'lp3_':{'center':dict(value=-0.9, min=-1.1, max=-0.7, vary=True),\n",
    "                   'amplitude':dict(value=0.09, min=0, max=0.5, vary=True),\n",
    "                   'sigma':dict(value=0.22, min=0.05, max=2, vary=True),\n",
    "                   'gamma':dict(value=0.02, min=0, max=2, vary=True)}},\n",
    "           \n",
    "           {'bg_':{'center':dict(value=-3, min=-5, max=-2, vary=True),\n",
    "                   'amplitude':dict(value=0.55, min=0, max=2, vary=True),\n",
    "                   'sigma':dict(value=1, min=0.5, max=5, vary=True)}}]\n",
    "\n",
    "\n",
    "pf.fitter.varsetter(pars, vardict, ret=False)\n",
    "\n",
    "out = mdl.fit(yvals, pars, x=xvals)\n",
    "comps = out.eval_components(x=xvals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35468 characters]\n",
      "text/plain": [
       "<Figure size 576x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pf.fitter.plot_fit_result(out, xvals, downsamp=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
