{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# This cell is added by sphinx-gallery\n\n%matplotlib inline\n\nimport mrsimulator\nprint(f'You are using mrsimulator v{mrsimulator.__version__}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# 17O DAS NMR of Coesite\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Coesite is a high-pressure (2-3 GPa) and high-temperature (700\u00b0C) polymorph of silicon\ndioxide $\\text{SiO}_2$. Coesite has five crystallographic $^{17}\\text{O}$\nsites. The experimental dataset used in this example is published in\nGrandinetti `et. al.` [#f1]_\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport csdmpy as cp\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport mrsimulator.signal_processing as sp\nimport mrsimulator.signal_processing.apodization as apo\nfrom mrsimulator import Simulator\nfrom mrsimulator.methods import Method2D\nfrom mrsimulator.utils import get_spectral_dimensions\nfrom mrsimulator.utils.collection import single_site_system_generator\nfrom mrsimulator.utils.spectral_fitting import LMFIT_min_function, make_LMFIT_params\nfrom lmfit import Minimizer, report_fit\n\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [4.5, 3.0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Import the dataset\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "filename = \"https://sandbox.zenodo.org/record/687656/files/DASCoesite.csdf\"\nexperiment = cp.load(filename)\n\n# For spectral fitting, we only focus on the real part of the complex dataset\nexperiment = experiment.real\n\n# Convert the coordinates along each dimension from Hz to ppm.\n_ = [item.to(\"ppm\", \"nmr_frequency_ratio\") for item in experiment.dimensions]\n\n# Normalize the spectrum\nexperiment /= experiment.max()\n\n# plot of the dataset.\nlevels = (np.arange(10) + 0.3) / 15  # contours are drawn at these levels.\nax = plt.subplot(projection=\"csdm\")\ncb = ax.contour(experiment, colors=\"k\", levels=levels, alpha=0.5, linewidths=0.5)\nplt.colorbar(cb)\nax.invert_xaxis()\nax.set_ylim(30, -30)\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create a fitting model\nThe fitting model includes the Simulator and SignalProcessor objects. First,\ncreate the Simulator object.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Create the guess sites and spin systems.\n# default unit of isotropic_chemical_shift is ppm and Cq is Hz.\nshifts = [29, 41, 57, 53, 58]  # in ppm\nCq = [6.1e6, 5.4e6, 5.5e6, 5.5e6, 5.1e6]  # in  Hz\neta = [0.1, 0.2, 0.1, 0.1, 0.3]\nabundance = [1, 1, 2, 2, 2]\n\nspin_systems = single_site_system_generator(\n    isotopes=\"17O\",\n    isotropic_chemical_shifts=shifts,\n    quadrupolar={\"Cq\": Cq, \"eta\": eta},\n    abundance=abundance,\n)\n\n# Create the DAS method.\n# Get the spectral dimension paramters from the experiment.\nspectral_dims = get_spectral_dimensions(experiment)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "das = Method2D(\n    channels=[\"17O\"],\n    magnetic_flux_density=11.7,  # in T\n    spectral_dimensions=[\n        {\n            **spectral_dims[0],\n            \"events\": [\n                {\"fraction\": 0.5, \"rotor_angle\": 37.38 * 3.14159 / 180},\n                {\"fraction\": 0.5, \"rotor_angle\": 79.19 * 3.14159 / 180},\n            ],\n        },\n        # The last spectral dimension block is the direct-dimension\n        {**spectral_dims[1], \"events\": [{\"rotor_angle\": 54.735 * 3.14159 / 180}]},\n    ],\n    experiment=experiment,  # also add the measurement to the method.\n)\n\n# Optimize the script by pre-setting the transition pathways for each spin system from\n# the das method.\nfor sys in spin_systems:\n    sys.transition_pathways = das.get_transition_pathways(sys)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Create the Simulator object and add the method and spin system objects.\nsim = Simulator()\nsim.spin_systems = spin_systems  # add the spin systems\nsim.methods = [das]  # add the method\nsim.run()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Add Post simulation processing.\nprocessor = sp.SignalProcessor(\n    operations=[\n        # Gaussian convolution along both dimensions.\n        sp.IFFT(dim_index=(0, 1)),\n        apo.Gaussian(FWHM=\"0.15 kHz\", dim_index=0),\n        apo.Gaussian(FWHM=\"0.15 kHz\", dim_index=1),\n        sp.FFT(dim_index=(0, 1)),\n        sp.Scale(factor=1 / 8),\n    ]\n)\n# Apply post simulation operations.\nprocessed_data = processor.apply_operations(data=sim.methods[0].simulation).real"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# The plot of the simulation after signal processing.\nax = plt.subplot(projection=\"csdm\")\nax.contour(processed_data, colors=\"r\", levels=levels, alpha=0.75, linewidths=0.5)\ncb = ax.contour(experiment, colors=\"k\", levels=levels, alpha=0.5, linewidths=0.5)\nplt.colorbar(cb)\nax.invert_xaxis()\nax.set_ylim(30, -30)\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Least-squares minimization with LMFIT\nFirst, create the fitting parameters.\nUse the :func:`~mrsimulator.utils.spectral_fitting.make_LMFIT_params` for a quick\nsetup.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "params = make_LMFIT_params(sim, processor)\n\n# Here, we fix the abundance parameters to their initial value.\nfor i in range(5):\n    params[f\"sys_{i}_abundance\"].vary = False\n\nparams.pretty_print()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the minimization using LMFIT\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "minner = Minimizer(LMFIT_min_function, params, fcn_args=(sim, processor))\nresult = minner.minimize()\nreport_fit(result)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Simulate the spectrum corresponding to the optimum parameters\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim.run()\nprocessed_data = processor.apply_operations(data=sim.methods[0].simulation).real"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the spectrum\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = plt.subplot(projection=\"csdm\")\nax.contour(processed_data, colors=\"r\", levels=levels, alpha=0.75, linewidths=0.5)\ncb = ax.contour(experiment, colors=\"k\", levels=levels, alpha=0.5, linewidths=0.5)\nplt.colorbar(cb)\nax.invert_xaxis()\nax.set_ylim(30, -30)\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#f1] Grandinetti, P. J., Baltisberger, J. H., Farnan, I., Stebbins, J. F.,\n      Werner, U. and Pines, A.\n      Solid-State $^{17}\\text{O}$ Magic-Angle and Dynamic-Angle Spinning NMR\n      Study of the $\\text{SiO}_2$ Polymorph Coesite, J. Phys. Chem. 1995,\n      **99**, *32*, 12341-12348.\n      `DOI: 10.1021/j100032a045 <https://doi.org/10.1021/j100032a045>`_\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.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}