Skip to content

Instantly share code, notes, and snippets.

@adonath
Last active August 29, 2015 14:24
Show Gist options
  • Save adonath/49bfe20ef994bf69ae1f to your computer and use it in GitHub Desktop.
Save adonath/49bfe20ef994bf69ae1f to your computer and use it in GitHub Desktop.
GSoC2015_Model_Rasterization
.ipynb_checkpoints
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Compound models and convolution\n",
"\n",
"This notebook sketches how to integrate the convolution operation into the compound model framework"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"from astropy.modeling.models import Gaussian1D\n",
"from astropy.modeling.core import _make_arithmetic_operator, BINARY_OPERATORS, _CompoundModelMeta\n",
"from scipy.signal import fftconvolve\n",
"from functools import partial"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"source = Gaussian1D(1, 0, 1, name='Source')\n",
"\n",
"# PSF is not normalized, but OK for a quick test\n",
"psf = Gaussian1D(1, 0, 1, name='PSF')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# adding convolution is straight forward\n",
"# this should be done in the astropy.convolve function\n",
"\n",
"# wrap fftconvolve to pass arguments\n",
"# any other mode than 'same' won't work for compound models (because the they expect the same shape for input and output)\n",
"convolve_func = partial(fftconvolve, mode='same')\n",
"\n",
"# add convolution to the binary operators\n",
"BINARY_OPERATORS['convolve'] = _make_arithmetic_operator(convolve_func)\n",
"\n",
"# create compound model from two model and convolution operator\n",
"conv = _CompoundModelMeta._from_operator('convolve', source, psf)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.00342127, 0.03246698, 0.18679594, 0.65211678, 1.38024565,\n",
" 1.7726372 , 1.38024565, 0.65211678, 0.18679594, 0.03246698,\n",
" 0.00342127])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# evaluate\n",
"x = np.arange(-5, 6)\n",
"conv(x)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.00342127, 0.03246698, 0.18679594, 0.65211678, 1.38024565,\n",
" 1.7726372 , 1.38024565, 0.65211678, 0.18679594, 0.03246698,\n",
" 0.00342127])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check he result\n",
"fftconvolve(source(x), psf(x), 'same')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment