Skip to content

Instantly share code, notes, and snippets.

@adonath
Created April 3, 2017 08:23
Show Gist options
  • Save adonath/4f6aef5f125b2cca4ae5edbd978ffc0b to your computer and use it in GitHub Desktop.
Save adonath/4f6aef5f125b2cca4ae5edbd978ffc0b to your computer and use it in GitHub Desktop.
This notebook sketches how to integrate the convolution operation into the astropy compound model framework
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# Proof of concept: compound models and convolution\n",
"\n",
"This notebook sketches how to integrate the convolution operation into the astropy compound model framework"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"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": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# define source model\n",
"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": 7,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"# wrap fftconvolve to pass arguments, any other mode than 'same' won't work\n",
"# for compound models (because the they expect the same shape for input and output arrays)\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 models and convolution operator\n",
"conv = _CompoundModelMeta._from_operator('convolve', source, psf)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"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": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# evaluate\n",
"x = np.arange(-5, 6)\n",
"conv(x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"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": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check the result\n",
"fftconvolve(source(x), psf(x), 'same')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": 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.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment