Skip to content

Instantly share code, notes, and snippets.

@larrybradley
Created March 18, 2015 01:19
Show Gist options
  • Save larrybradley/66d69609392c831a4cee to your computer and use it in GitHub Desktop.
Save larrybradley/66d69609392c831a4cee to your computer and use it in GitHub Desktop.
imarith
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:f6e73f26a8a6619f2cc7b956424240a6fd72091748ec05092c3b86c4d2020835"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# create dummy images (random Gaussian noise)\n",
"import numpy as np\n",
"from photutils.datasets import make_noise_image\n",
"shape = (1000, 1000)\n",
"img1 = make_noise_image(shape, mean=0., stddev=5., random_state=12345)\n",
"img2 = make_noise_image(shape, mean=10., stddev=25., random_state=12345)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from imutils import StdUncertainty\n",
"err1 = StdUncertainty(np.sqrt(abs(img1)))\n",
"err2 = StdUncertainty(np.sqrt(abs(img2)))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from astropy.nddata import NDData\n",
"nd1 = NDData(img1, uncertainty=err1)\n",
"nd2 = NDData(img2, uncertainty=err2)\n",
"nd1.meta['exptime'] = 1000.\n",
"nd2.meta['exptime'] = 700."
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from imutils import imarith"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd3 = imarith(nd1, nd2, '+', keywords='exptime')\n",
"nd3.meta['exptime']"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"1700.0"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.array_equal(nd3.data, nd1.data + nd2.data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"True"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.array_equal(nd3.uncertainty.value, np.sqrt(nd1.uncertainty.value**2 + nd2.uncertainty.value**2))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"True"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd3 = imarith(nd1, nd2, 'min', keywords='exptime')\n",
"print(nd3.uncertainty)\n",
"print(nd3.meta['exptime'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"INFO:astropy:Error propagation is not performed for the '//', 'min', and 'max' operators.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"INFO: Error propagation is not performed for the '//', 'min', and 'max' operators. [imutils.core]\n",
"None\n",
"700.0\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd3 = imarith(nd1, nd2, 'max', keywords='exptime')\n",
"print(nd3.meta['exptime'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"INFO:astropy:Error propagation is not performed for the '//', 'min', and 'max' operators.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"INFO: Error propagation is not performed for the '//', 'min', and 'max' operators. [imutils.core]\n",
"1000.0\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd3 = imarith(nd1, nd2, '//', keywords='exptime')\n",
"print(nd3.meta['exptime'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"INFO:astropy:Error propagation is not performed for the '//', 'min', and 'max' operators.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"INFO: Error propagation is not performed for the '//', 'min', and 'max' operators. [imutils.core]\n",
"1.0\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd3 = imarith(nd1, nd2, '/', keywords='exptime')\n",
"print(nd3.meta['exptime'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1.42857142857\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd3 = imarith(nd1, nd2, '-', keywords=['exptime', 'gain'])\n",
"nd3.meta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"OrderedDict([('exptime', 300.0), ('gain', None)])"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd1.meta['gain'] = 2.\n",
"nd3 = imarith(nd1, nd2, '-', keywords=['exptime', 'gain'])\n",
"nd3.meta"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"OrderedDict([('exptime', 300.0), ('gain', None)])"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd2.data[0, 0] = 0.\n",
"nd3 = imarith(nd1, nd2, '/')\n",
"print nd3.data\n",
"print nd3.mask"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0. 0.10898162 0.86979957 ..., 0.30527345 0.12148895\n",
" 0.15490139]\n",
" [ 0.33710258 0.13989228 0.39432775 ..., -0.307804 0.23010926\n",
" 0.16549349]\n",
" [ 0.647358 0.16440404 0.10629458 ..., 0.09942584 0.13139872\n",
" 0.16665953]\n",
" ..., \n",
" [ 0.33688304 0.1214611 0.12899675 ..., -0.32645919 0.06603463\n",
" 0.33177105]\n",
" [ 0.11414654 0.24997464 0.49101887 ..., 0.63539232 0.33137196\n",
" 0.56982561]\n",
" [ 0.17164995 0.39052198 0.09576098 ..., 0.07791704 6.08580083\n",
" 0.15255363]]\n",
"[[ True False False ..., False False False]\n",
" [False False False ..., False False False]\n",
" [False False False ..., False False False]\n",
" ..., \n",
" [False False False ..., False False False]\n",
" [False False False ..., False False False]\n",
" [False False False ..., False False False]]\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd2.data[0, 0] = 0.\n",
"nd3 = imarith(nd1, nd2, '/', fill_value=-99)\n",
"print nd3.data\n",
"print nd3.mask"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ -9.90000000e+01 1.08981619e-01 8.69799570e-01 ..., 3.05273446e-01\n",
" 1.21488945e-01 1.54901387e-01]\n",
" [ 3.37102576e-01 1.39892276e-01 3.94327748e-01 ..., -3.07804000e-01\n",
" 2.30109262e-01 1.65493489e-01]\n",
" [ 6.47358002e-01 1.64404043e-01 1.06294577e-01 ..., 9.94258444e-02\n",
" 1.31398717e-01 1.66659532e-01]\n",
" ..., \n",
" [ 3.36883035e-01 1.21461104e-01 1.28996749e-01 ..., -3.26459190e-01\n",
" 6.60346279e-02 3.31771052e-01]\n",
" [ 1.14146535e-01 2.49974639e-01 4.91018872e-01 ..., 6.35392319e-01\n",
" 3.31371962e-01 5.69825611e-01]\n",
" [ 1.71649948e-01 3.90521984e-01 9.57609793e-02 ..., 7.79170387e-02\n",
" 6.08580083e+00 1.52553635e-01]]\n",
"[[ True False False ..., False False False]\n",
" [False False False ..., False False False]\n",
" [False False False ..., False False False]\n",
" ..., \n",
" [False False False ..., False False False]\n",
" [False False False ..., False False False]\n",
" [False False False ..., False False False]]\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"d1 = np.arange(10.)\n",
"d2 = np.arange(10.)**2\n",
"m1 = np.zeros_like(d1, dtype=bool)\n",
"m1[4] = True\n",
"m2 = np.zeros_like(d2, dtype=bool)\n",
"m2[7] = True\n",
"nd4 = NDData(d1, mask=m1)\n",
"nd5 = NDData(d2, mask=m2)\n",
"print(d1)\n",
"print(m1)\n",
"print(d2)\n",
"print(m2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n",
"[False False False False True False False False False False]\n",
"[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]\n",
"[False False False False False False False True False False]\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd6 = imarith(nd4, nd5, '/')\n",
"print(nd6.data)\n",
"print(nd6.mask)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 0. 1. 0.5 0.33333333 0. 0.2\n",
" 0.16666667 0. 0.125 0.11111111]\n",
"[ True False False False True False False True False False]\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nd6 = imarith(nd4, nd5, '/', fill_value=np.nan)\n",
"print(nd6.data)\n",
"print(nd6.mask)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ nan 1. 0.5 0.33333333 nan 0.2\n",
" 0.16666667 nan 0.125 0.11111111]\n",
"[ True False False False True False False True False False]\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment