{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Mueller matrix\n\nMueller matrix fit to a SiO2 on Si measurement.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import elli\nfrom elli.fitting import ParamsHist, fit_mueller_matrix"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Read data\n\nWe load the data from an ascii file containing each of the mueller matrix elements.\nThe wavelength range is cut to be in between 210 nm and 820 nm,\nto stay in the range of the provided literature values for Si.\nThe data is expected to be in a pandas dataframe containing the columns Mxy,\nwhere x and y refer to the matrix element inside the mueller matrix.\nThe data is scaled by the M11 element, such that $M_{11} = 1$ for all wavelengths.\nTo show the structure we print the `MM` dataframe.\nIf you load your data from another source make sure it adheres to this form.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "MM = elli.SpectraRay.read_mmatrix(\"Wafer_MM_70.txt\").loc[210:820]\nprint(MM)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Setting start parameters\nHere we set the start parameters for the SiO2 cauchy dispersion\nand thickness of the layer.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "params = ParamsHist()\nparams.add(\"SiO2_n0\", value=1.452, min=-100, max=100, vary=True)\nparams.add(\"SiO2_n1\", value=36.0, min=-40000, max=40000, vary=True)\nparams.add(\"SiO2_n2\", value=0, min=-40000, max=40000, vary=True)\nparams.add(\"SiO2_k0\", value=0, min=-100, max=100, vary=True)\nparams.add(\"SiO2_k1\", value=0, min=-40000, max=40000, vary=True)\nparams.add(\"SiO2_k2\", value=0, min=-40000, max=40000, vary=True)\nparams.add(\"SiO2_d\", value=120, min=0, max=40000, vary=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Building the model\nHere the model is build and the experimental structure is returned.\nFor details on this process please refer to the `Basic usage` example.\nWhen executed in an jupyter notebook this displays an interactive graph\nwith which you can select the start parameters before fitting the data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "@fit_mueller_matrix(MM, params, display_single=False, sharex=True, full_scale=False)\ndef model(lbda, params):\n    sr = elli.SpectraRay(\"./\")\n    Si = elli.IsotropicMaterial(sr.loadDispersionTable(\"Si_Aspnes.mat\"))\n\n    SiO2 = elli.Cauchy(\n        params[\"SiO2_n0\"],\n        params[\"SiO2_n1\"],\n        params[\"SiO2_n2\"],\n        params[\"SiO2_k0\"],\n        params[\"SiO2_k1\"],\n        params[\"SiO2_k2\"],\n    ).get_mat()\n\n    Layer = [elli.Layer(SiO2, params[\"SiO2_d\"])]\n\n    return elli.Structure(elli.AIR, Layer, Si).evaluate(\n        lbda, 70, solver=elli.Solver4x4, propagator=elli.PropagatorExpm()\n    )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Plot & Fit the model\nHere we plot the model at the initial parameter set vs. the experimental data.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can also plot the residual between measurement and model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model.plot_residual()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we execute a fit and plot the model afterwards.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fit_stats = model.fit()\nmodel.plot(full_scale=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "For comparison we plot the residual again to have a figure of merit\nfor the fit quality\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model.plot_residual()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We may also print the fit statistics.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fit_stats"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## References\n[Here](https://github.com/PyEllips/pyElli/tree/master/examples/SiO2_Si%20Mueller%20Matrix)\nyou can find the latest jupyter notebook and data files of this example.\n\n"
      ]
    }
  ],
  "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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}