From 36a987001fad9c73d8ca071c0d3d0526879bd326 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 12 Apr 2018 14:15:25 -0400 Subject: [PATCH 1/5] Refs #47 Notebook updated --- .../water_fitting-checkpoint.ipynb | 52 +++++++++++--- notebooks/water_fitting.ipynb | 68 +++++++++---------- 2 files changed, 73 insertions(+), 47 deletions(-) diff --git a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb index 5a1c5b2..d88a3bf 100644 --- a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb +++ b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb @@ -25,7 +25,9 @@ "- [Post your questions](https://gitter.im/basisdoc/Lobby)\n", "\n", "

Table of Contents

\n", - "- Load data and visualize data \n", + "- Donwload 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 +68,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 +98,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)" ] }, { @@ -653,14 +683,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..d88a3bf 100644 --- a/notebooks/water_fitting.ipynb +++ b/notebooks/water_fitting.ipynb @@ -26,6 +26,7 @@ "\n", "

Table of Contents

\n", "- Donwload Data \n", + "- Donwload Data \n", "- Load Data and Visualize \n", "- Define the Fitting Range \n", "- Define the model \n", @@ -48,18 +49,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 +68,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 +95,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)" ] }, { From 0dc4db6d6695af4f824ce4605bdc1b203d145e9e Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 12 Apr 2018 14:19:54 -0400 Subject: [PATCH 2/5] Refs #47 Notebook tells how to install qef --- notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb | 2 +- notebooks/water_fitting.ipynb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb index d88a3bf..43270a8 100644 --- a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb +++ b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.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", diff --git a/notebooks/water_fitting.ipynb b/notebooks/water_fitting.ipynb index d88a3bf..43270a8 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", From abf6cece103b727e3dd6986288a51c9d7adc6576 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 12 Apr 2018 14:31:04 -0400 Subject: [PATCH 3/5] Refs #47 Help with the intial guess, improve figure panels --- .../water_fitting-checkpoint.ipynb | 22 ++++++++++++++++--- notebooks/water_fitting.ipynb | 22 ++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb index 43270a8..dbe4303 100644 --- a/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb +++ b/notebooks/.ipynb_checkpoints/water_fitting-checkpoint.ipynb @@ -26,7 +26,6 @@ "\n", "

Table of Contents

\n", "- Donwload Data \n", - "- Donwload Data \n", "- Load Data and Visualize \n", "- Define the Fitting Range \n", "- Define the model \n", @@ -245,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" ] }, { @@ -263,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", diff --git a/notebooks/water_fitting.ipynb b/notebooks/water_fitting.ipynb index 43270a8..dbe4303 100644 --- a/notebooks/water_fitting.ipynb +++ b/notebooks/water_fitting.ipynb @@ -26,7 +26,6 @@ "\n", "

Table of Contents

\n", "- Donwload Data \n", - "- Donwload Data \n", "- Load Data and Visualize \n", "- Define the Fitting Range \n", "- Define the model \n", @@ -245,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" ] }, { @@ -263,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", From 0c5819305ebe990b64072daed2801af8fe966efa Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 12 Apr 2018 14:37:01 -0400 Subject: [PATCH 4/5] Refs #47 Rephrase warning when fitting other than fist spectrum --- notebooks/water_fitting.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notebooks/water_fitting.ipynb b/notebooks/water_fitting.ipynb index dbe4303..41d0274 100644 --- a/notebooks/water_fitting.ipynb +++ b/notebooks/water_fitting.ipynb @@ -374,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." ] From 35b3c400579518cf348b300d7dccf3452af718f0 Mon Sep 17 00:00:00 2001 From: Jose Borreguero Date: Thu, 12 Apr 2018 14:40:54 -0400 Subject: [PATCH 5/5] Refs #47 Remove unused notebook --- notebooks/widget_builder_on_the_fly.ipynb | 189 ---------------------- 1 file changed, 189 deletions(-) delete mode 100644 notebooks/widget_builder_on_the_fly.ipynb 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 -}