From cef6d9af7d961efbbf0be96cf5e670c34a7b7970 Mon Sep 17 00:00:00 2001 From: tjof2 Date: Tue, 10 May 2016 16:23:41 +0100 Subject: [PATCH] Added new demo --- examples/HyperSpy Demo.ipynb | 134 --------------- examples/PGURE-SVT HyperSpy Demo.ipynb | 217 +++++++++++++++++++++++++ 2 files changed, 217 insertions(+), 134 deletions(-) delete mode 100644 examples/HyperSpy Demo.ipynb create mode 100644 examples/PGURE-SVT HyperSpy Demo.ipynb diff --git a/examples/HyperSpy Demo.ipynb b/examples/HyperSpy Demo.ipynb deleted file mode 100644 index 5210ab6..0000000 --- a/examples/HyperSpy Demo.ipynb +++ /dev/null @@ -1,134 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# PGURE-SVT\n", - "## HyperSpy Demonstration\n", - "#### Written by Ben Martineau\n", - "\n", - "[HyperSpy](http://hyperspy.org/) is an open-source python library that makes signal handling and processing straightforward in python, with a friendly API. While you can use `pguresvt.pguresvt.SVT` for denoising of Numpy arrays, `pguresvt.hspysvt.HSPYSVT` can denoise HyperSpy arrays, if you prefer.\n", - "\n", - "This notebook uses the same example as the [PGURE-SVT Documentation](http://tjof2.github.io/pgure-svt/) but denoises, and returns, a HyperSpy `Signal` directly." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "# Configures the plotting backend\n", - "%matplotlib qt" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false, - "scrolled": true - }, - "outputs": [], - "source": [ - "# Import the hyperspy API\n", - "import hyperspy.api as hs" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "# Import the hyperspy wrapper for PGURE-SVT\n", - "from pguresvt import hspysvt" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "# Load example dataset\n", - "movie = hs.load(\"./test/examplesequence.tif\")" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Truncate to 15 frames, and plot the result\n", - "movie = movie.inav[:15]\n", - "movie.plot()" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "# Denoise the HyperSpy signal directly.\n", - "svt = hspysvt.HSPYSVT(threshold=0.5)\n", - "denoised_movie = svt.denoise(movie)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "# Plot denoised data\n", - "denoised_movie.plot()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "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.4.3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/examples/PGURE-SVT HyperSpy Demo.ipynb b/examples/PGURE-SVT HyperSpy Demo.ipynb new file mode 100644 index 0000000..8be8824 --- /dev/null +++ b/examples/PGURE-SVT HyperSpy Demo.ipynb @@ -0,0 +1,217 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# PGURE-SVT HyperSpy Demonstration\n", + "\n", + "### Tom Furnival ([tjof2@cam.ac.uk](mailto:tjof2@cam.ac.uk))\n", + "\n", + "HyperSpy is an open-source Python library that makes signal handling and processing straightforward in Python, with a friendly API. \n", + "\n", + "While you can use `pguresvt.pguresvt.SVT` to denoise a numpy array directly, `pguresvt.hspysvt.HSPYSVT` can directly denoise a HyperSpy signal." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Configures the plotting backend\n", + "#%matplotlib inline\n", + "%matplotlib qt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "# Import the HyperSpy API\n", + "import hyperspy.api as hs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Import the HyperSpy wrapper for PGURE-SVT\n", + "from pguresvt import hspysvt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Simulated dataset\n", + "\n", + "First, we load the simulated dataset using HyperSpy." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Load example dataset\n", + "movie = hs.load(\"../test/examplesequence.tif\")\n", + "\n", + "# Truncate to 25 frames, and plot the result\n", + "movie = movie.inav[:25]\n", + "movie.plot(navigator='slider')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we corrupt it with a mixture of Poisson and Gaussian noise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# First extract the data and rescale to [0,1] range\n", + "clean = movie._data_aligned_with_axes\n", + "clean = clean / np.amax(clean)\n", + "\n", + "# Detector gain\n", + "gain = 0.1\n", + "# Detector offset\n", + "offset = 0.1\n", + "# Detector variance\n", + "sigma = 0.1\n", + "\n", + "def addnoise(x):\n", + " return gain * np.random.poisson(x / gain) + offset + sigma * np.random.randn()\n", + "addnoise = np.vectorize(addnoise, otypes=[np.float])\n", + "\n", + "noisy = addnoise(clean)\n", + "\n", + "noisy_movie = hs.signals.Image(noisy)\n", + "noisy_movie.plot()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next we initialise the SVT denoising function. The full list of options (with default values) is:\n", + "\n", + "```\n", + "hspysvt.HSPYSVT(patchsize=4,\n", + " patchoverlap=1,\n", + " length=15,\n", + " optimize=True,\n", + " threshold=0.5,\n", + " estimatenoise=True,\n", + " alpha=-1., \n", + " mu=-1., \n", + " sigma=-1., \n", + " arpssize=7, \n", + " tol=1e-7,\n", + " median=5,\n", + " hotpixelthreshold=10)\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "# Initialize with default parameters\n", + "svt = hspysvt.HSPYSVT(patchsize=4,\n", + " patchoverlap=2,\n", + " length=15,\n", + " threshold=0.5,\n", + " tol=1e-6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we are able to run the denoising and plot the result:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Run the denoising\n", + "denoised_movie = svt.denoise(noisy_movie)\n", + "\n", + "# Plot denoised data\n", + "denoised_movie.plot()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. Time-resolved STEM image sequence\n", + "\n", + "First, we load the simulated dataset using HyperSpy, and then corrupt it with a mixture of Poisson and Gaussian noise.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.5.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}