-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
217 additions
and
134 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# PGURE-SVT HyperSpy Demonstration\n", | ||
"\n", | ||
"### Tom Furnival ([[email protected]](mailto:[email protected]))\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 | ||
} |