diff --git a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb index 5a1c5b2..dbe4303 100644 --- a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb +++ b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb @@ -19,13 +19,14 @@ "- Visualize results from the simultaneous fit.\n", "\n", "### Useful links\n", + "- [qef documentation](http://qef.readthedocs.io/en/latest/) (pip install qef) Utilities for QENS fitting\n", "- [lmfit documentation](https://lmfit.github.io/lmfit-py/index.html) Curve fitting\n", - "- [qef documentation](http://qef.readthedocs.io/en/latest/) Utilities for QENS fitting\n", "- [matplotlib](https://matplotlib.org) Plotting with python\n", "- [Post your questions](https://gitter.im/basisdoc/Lobby)\n", "\n", "

Table of Contents

\n", - "- Load data and visualize data \n", + "- Donwload Data \n", + "- Load Data and Visualize \n", "- Define the Fitting Range \n", "- Define the model \n", "- Obtain an initial guess \n", @@ -66,12 +67,14 @@ "from qef.models.tabulatedmodel import TabulatedModel\n", "from qef.models.resolution import TabulatedResolutionModel\n", "from qef.models.teixeira import TeixeiraWaterModel\n", - "from qef.operators.convolve import Convolve\n", - "\n", - "# Finding out test data\n", - "qef_module_file = os.path.abspath(qef.__file__)\n", - "qef_dir = os.path.join(os.path.dirname(qef_module_file), os.pardir)\n", - "data_dir = os.path.join(qef_dir, 'tests', 'data', 'io')" + "from qef.operators.convolve import Convolve" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Imports for plotting and widgets" ] }, { @@ -94,7 +97,33 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Imports for plotting and widgets" + "(Top)

Donwload Data

\n", + "\n", + "It's assumed git is installed in your system. Otherwise,\n", + "[follow instructions](http://qef.readthedocs.io/en/latest/installation.html#testing-tutorials-data)\n", + "to download and unpack your data to /tmp/qef_data." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%bash\n", + "qef_data_dir=\"/tmp/qef_data\"\n", + "git clone https://github.com/jmborr/qef_data ${qef_data_dir}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "qef_data_dir = '/tmp/qef_data'\n", + "data_dir = os.path.join(qef_data_dir, 'data', 'io')\n", + "print(data_dir)" ] }, { @@ -215,7 +244,19 @@ "source": [ "(Top)

Obtain an initial guess

\n", "\n", - "A widget that compares the evaluation of the model with one of the experimental spectra. You can tweak only the free (unconstrained) parameters." + "A widget that compares the evaluation of the model with one of the experimental spectra. You can tweak only the free (unconstrained) parameters.\n", + "\n", + "When run, you will see two empty panels, one for comparison between experiment and model, and the second panel for residuals. Start changing the values of the parameters for the panels to be populated.\n", + "\n", + "A good initial guess for the first spectrum is :\n", + "\n", + "> spectum index = 0 \n", + "> I_c = 4 \n", + "> e_center = 0 \n", + "> e_amplitude = 0.1 \n", + "> l_sigma = 0.03 \n", + "> b_intercept = 0 \n", + "> b_slope = 0 \n" ] }, { @@ -233,7 +274,12 @@ "y_exp = fr['y'][0] # associated experimental values for the first histogram\n", "e_exp = fr['e'][0]\n", "\n", - "f, (ax1, ax2) = plt.subplots(2, 1)\n", + "f = plt.figure()\n", + "gs = plt.GridSpec(nrows=2, ncols=1, height_ratios=[4, 1])\n", + "ax1 = f.add_subplot(gs[0]) # host the fit\n", + "ax2 = f.add_subplot(gs[1], sharex=ax1) # host the residuals\n", + "\n", + "#f, (ax1, ax2) = plt.subplots(2, 1)\n", "def plot_new_spectrum(an_axis):\n", " global y_exp\n", " an_axis.clear()\n", @@ -653,14 +699,14 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.12" + "pygments_lexer": "ipython3", + "version": "3.6.4" } }, "nbformat": 4, diff --git a/notebooks/water_fitting.ipynb b/notebooks/water_fitting.ipynb index 474e197..41d0274 100644 --- a/notebooks/water_fitting.ipynb +++ b/notebooks/water_fitting.ipynb @@ -19,8 +19,8 @@ "- Visualize results from the simultaneous fit.\n", "\n", "### Useful links\n", + "- [qef documentation](http://qef.readthedocs.io/en/latest/) (pip install qef) Utilities for QENS fitting\n", "- [lmfit documentation](https://lmfit.github.io/lmfit-py/index.html) Curve fitting\n", - "- [qef documentation](http://qef.readthedocs.io/en/latest/) Utilities for QENS fitting\n", "- [matplotlib](https://matplotlib.org) Plotting with python\n", "- [Post your questions](https://gitter.im/basisdoc/Lobby)\n", "\n", @@ -48,18 +48,9 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/sw/miniconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", - " from ._conv import register_converters as _register_converters\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "from __future__ import (absolute_import, division, print_function)\n", "\n", @@ -76,17 +67,19 @@ "from qef.models.tabulatedmodel import TabulatedModel\n", "from qef.models.resolution import TabulatedResolutionModel\n", "from qef.models.teixeira import TeixeiraWaterModel\n", - "from qef.operators.convolve import Convolve\n", - "\n", - "# Finding out test data\n", - "qef_module_file = os.path.abspath(qef.__file__)\n", - "qef_dir = os.path.join(os.path.dirname(qef_module_file), os.pardir)\n", - "data_dir = os.path.join(qef_dir, 'tests', 'data', 'io')" + "from qef.operators.convolve import Convolve" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Imports for plotting and widgets" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -101,34 +94,36 @@ ] }, { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/sw/miniconda3/lib/python3.6/site-packages/qef/../tests/data/io\n" - ] - } - ], + "cell_type": "markdown", + "metadata": {}, "source": [ - "print(data_dir)" + "(Top)

Donwload Data

\n", + "\n", + "It's assumed git is installed in your system. Otherwise,\n", + "[follow instructions](http://qef.readthedocs.io/en/latest/installation.html#testing-tutorials-data)\n", + "to download and unpack your data to /tmp/qef_data." ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "### Imports for plotting and widgets" + "%%bash\n", + "qef_data_dir=\"/tmp/qef_data\"\n", + "git clone https://github.com/jmborr/qef_data ${qef_data_dir}" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "(Top)

Donwload Data

\n" + "qef_data_dir = '/tmp/qef_data'\n", + "data_dir = os.path.join(qef_data_dir, 'data', 'io')\n", + "print(data_dir)" ] }, { @@ -249,7 +244,19 @@ "source": [ "(Top)

Obtain an initial guess

\n", "\n", - "A widget that compares the evaluation of the model with one of the experimental spectra. You can tweak only the free (unconstrained) parameters." + "A widget that compares the evaluation of the model with one of the experimental spectra. You can tweak only the free (unconstrained) parameters.\n", + "\n", + "When run, you will see two empty panels, one for comparison between experiment and model, and the second panel for residuals. Start changing the values of the parameters for the panels to be populated.\n", + "\n", + "A good initial guess for the first spectrum is :\n", + "\n", + "> spectum index = 0 \n", + "> I_c = 4 \n", + "> e_center = 0 \n", + "> e_amplitude = 0.1 \n", + "> l_sigma = 0.03 \n", + "> b_intercept = 0 \n", + "> b_slope = 0 \n" ] }, { @@ -267,7 +274,12 @@ "y_exp = fr['y'][0] # associated experimental values for the first histogram\n", "e_exp = fr['e'][0]\n", "\n", - "f, (ax1, ax2) = plt.subplots(2, 1)\n", + "f = plt.figure()\n", + "gs = plt.GridSpec(nrows=2, ncols=1, height_ratios=[4, 1])\n", + "ax1 = f.add_subplot(gs[0]) # host the fit\n", + "ax2 = f.add_subplot(gs[1], sharex=ax1) # host the residuals\n", + "\n", + "#f, (ax1, ax2) = plt.subplots(2, 1)\n", "def plot_new_spectrum(an_axis):\n", " global y_exp\n", " an_axis.clear()\n", @@ -362,9 +374,9 @@ "source": [ "(Top)

Sequential Fit

\n", "\n", - "Starting from the first spectrum, we iteratively fit spectra of higher q's.\n", + "**Special instructions:** If the fit you carried out in the previous cell was not for the first spectrum, the sequential fit will not run but raise an error. Go back to the cell for the Initial Guess and carry out a guess and a subsequent fit for the first spectrum, then come back here.\n", "\n", - "*Special instructions:* If the previous guess was not for the first spectrum, the code will not run but raise and error. Go back to the previous cell and carry out a guess for the first spectrum, then come back here.\n", + "Starting from the first spectrum, we iteratively fit spectra of higher q's.\n", "\n", "We do not assume any particular Q-dependence for the width of the Lorentzian function." ] diff --git a/notebooks/widget_builder_on_the_fly.ipynb b/notebooks/widget_builder_on_the_fly.ipynb deleted file mode 100644 index d28bbb4..0000000 --- a/notebooks/widget_builder_on_the_fly.ipynb +++ /dev/null @@ -1,189 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from IPython.core.display import HTML\n", - "from IPython.core.display import display\n", - "from ipywidgets import widgets\n", - "\n", - "import numpy as np\n", - "\n", - "import matplotlib.pyplot as plt\n", - "%matplotlib notebook" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "726a1be1595849c7bc5bfc91ea5449a6", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "A Jupyter Widget" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "user_input_ui = widgets.HBox([widgets.Label(\"Define Function:\",\n", - " layout=widgets.Layout(width='10%')),\n", - " widgets.Text(value='',\n", - " placeholder=\"Define function here\",\n", - " layout=widgets.Layout(width='60%')),\n", - " widgets.Label(\"Ex: f(a,b,c)=a+b*c\",\n", - " layout=widgets.Layout(width='20%'))])\n", - "display(user_input_ui)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "..." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "At this point, you retrieve the name of the coefficient defined in the function, let's pretend those are \n", - "\n", - " * argument_1\n", - " * argument_2\n", - " * argument_3\n", - " \n", - " ```\n", - " function(x, argument_1, argument_2, argument_3) = 3*argument_1*x + argument_2 * argument_3\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "list_arguments = ['argument_1', 'argument_2', 'argument_3']" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "def function(x, argument_1, argument_2, argument_3):\n", - " return 3*argument_1*x*x + argument_2*x + argument_3" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "def value_changed(value):\n", - " # retrieve value of all widgets\n", - " list_value = []\n", - " for _top_widget in vertical_layout.children:\n", - " _text_float = _top_widget.children[1]\n", - " list_value.append(_text_float.value)\n", - "\n", - " x = np.linspace(0,100)\n", - " plt.clf()\n", - " plt.plot(x, function(x, list_value[0], list_value[1], list_value[2]))\n", - " plt.title(\"argument_1: {}, argument_2: {}, argument_3: {}\".format(list_value[0], list_value[1], list_value[2]))" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "2d8bd4928ce143c1b41d1ace62a16e7c", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "A Jupyter Widget" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "\n", - "list_ui = []\n", - "for _argument in list_arguments:\n", - " \n", - " _arg_ui = widgets.HBox([widgets.Label(_argument,\n", - " \n", - " layout=widgets.Layout(width='10%')),\n", - " widgets.FloatText(value=0,\n", - " layout=widgets.Layout(width='20%'))])\n", - " _arg_ui.children[1].observe(value_changed, 'value')\n", - " \n", - " list_ui.append(_arg_ui)\n", - " vertical_layout = widgets.VBox(list_ui)\n", - " \n", - "display(vertical_layout) \n", - "plt.clf()" - ] - }, - { - "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": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.12" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}