Skip to content

Instantly share code, notes, and snippets.

@larrybradley
Last active August 29, 2015 14:16
Show Gist options
  • Save larrybradley/aaa810ebce0ad1edf62d to your computer and use it in GitHub Desktop.
Save larrybradley/aaa810ebce0ad1edf62d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:53eca0c18b2bd006fca50481b1ec7e16474ef0d8b6bba598e69681968ab2eb6b"
},
"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 astropy.nddata import NDData\n",
"nd1 = NDData(img1)\n",
"nd2 = NDData(img2)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# the default is no sigma clipping\n",
"import imutils\n",
"imutils.imstats(nd1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<Table masked=False length=1>\n",
"<table id=\"table4379588880\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>1000000</td><td>0.0074686903326</td><td>4.9997632595</td><td>-25.2879522527</td><td>24.8988998824</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- --------------- ------------ -------------- -------------\n",
"1000000 0.0074686903326 4.9997632595 -25.2879522527 24.8988998824"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# specify sigma (and optional iters, cenfunc, varfunc) for sigma clipping\n",
"import imutils\n",
"imutils.imstats(nd1, sigma=3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4379471504\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>997318</td><td>0.00712145604183</td><td>4.93314377092</td><td>-14.9923827908</td><td>15.005682427</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- ---------------- ------------- -------------- ------------\n",
" 997318 0.00712145604183 4.93314377092 -14.9923827908 15.005682427"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# changing the sigma-clipping sigma and number of iterations\n",
"imutils.imstats(nd1, sigma=4.5, iters=5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4379590608\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>999992</td><td>0.00746781507137</td><td>4.99934483146</td><td>-22.2759046337</td><td>22.4065515421</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- ---------------- ------------- -------------- -------------\n",
" 999992 0.00746781507137 4.99934483146 -22.2759046337 22.4065515421"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# the returned statistics are specified with a \"columns\" list\n",
"columns = ['npixels', 'nrejected', 'mean', 'median' 'mode', 'std', 'mad_std', 'biweight_location']\n",
"imutils.imstats(nd1, columns=columns)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4379589648\">\n",
"<thead><tr><th>npixels</th><th>nrejected</th><th>mean</th><th>medianmode</th><th>std</th><th>mad_std</th><th>biweight_location</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>int64</th><th>float64</th><th>object</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>499445</td><td>500555</td><td>-3.98592356622</td><td>None</td><td>3.01069956152</td><td>2.95244560808</td><td>-3.55773744221</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"<Table masked=False length=1>\n",
"npixels nrejected mean ... mad_std biweight_location\n",
" int64 int64 float64 ... float64 float64 \n",
"------- --------- -------------- ... ------------- -----------------\n",
" 499445 500555 -3.98592356622 ... 2.95244560808 -3.55773744221"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# all of the currently available statistics\n",
"columns = ['npixels', 'nrejected', 'mean', 'median', 'mode', 'std',\n",
" 'mad_std', 'biweight_location', 'biweight_midvariance',\n",
" 'min', 'max', 'skew', 'kurtosis']\n",
"imutils.imstats(nd1, columns=columns)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4379588944\">\n",
"<thead><tr><th>npixels</th><th>nrejected</th><th>mean</th><th>median</th><th>mode</th><th>std</th><th>mad_std</th><th>biweight_location</th><th>biweight_midvariance</th><th>min</th><th>max</th><th>skew</th><th>kurtosis</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>499445</td><td>500555</td><td>-3.98592356622</td><td>-3.37052882895</td><td>-2.13973935442</td><td>3.01069956152</td><td>2.95244560808</td><td>-3.55773744221</td><td>2.90686974988</td><td>-25.2879522527</td><td>-1.15963585925e-06</td><td>-0.998542531167</td><td>0.881125288394</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"<Table masked=False length=1>\n",
"npixels nrejected mean ... skew kurtosis \n",
" int64 int64 float64 ... float64 float64 \n",
"------- --------- -------------- ... --------------- --------------\n",
" 499445 500555 -3.98592356622 ... -0.998542531167 0.881125288394"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# NDData with a mask\n",
"mask1 = img1 > 0.\n",
"nd1 = NDData(img1, mask=mask1)\n",
"imutils.imstats(nd1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4401600464\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>499445</td><td>-3.98592356622</td><td>3.01069956152</td><td>-25.2879522527</td><td>-1.15963585925e-06</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- -------------- ------------- -------------- ------------------\n",
" 499445 -3.98592356622 3.01069956152 -25.2879522527 -1.15963585925e-06"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# lower and upper data bounds can also be specified\n",
"nd1 = NDData(img1)\n",
"imutils.imstats(nd1, lower_bound=0, upper_bound=1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4401599824\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>79444</td><td>0.499201683171</td><td>0.288499234654</td><td>3.77379641907e-05</td><td>0.999984689966</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- -------------- -------------- ----------------- --------------\n",
" 79444 0.499201683171 0.288499234654 3.77379641907e-05 0.999984689966"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# a particular data value can also be masked (e.g. 0)\n",
"img1[0:100, 0:100] = 0.\n",
"nd1 = NDData(img1)\n",
"imutils.imstats(nd1, mask_value=0.)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4401600976\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>990000</td><td>0.00720837634415</td><td>4.99999657348</td><td>-25.2879522527</td><td>24.8988998824</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- ---------------- ------------- -------------- -------------\n",
" 990000 0.00720837634415 4.99999657348 -25.2879522527 24.8988998824"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# any unmasked invalid values (NaN or inf) are automatically masked\n",
"img1[0, 0] = np.nan\n",
"nd1 = NDData(img1)\n",
"imutils.imstats(nd1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"WARNING: The data array contains at least one unmasked invalid value (NaN or inf). These values will be automatically masked. [imutils.stats]\n",
"WARNING:astropy:The data array contains at least one unmasked invalid value (NaN or inf). These values will be automatically masked.\n"
]
},
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4501307280\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>999999</td><td>0.00713629971701</td><td>4.97493631535</td><td>-25.2879522527</td><td>24.8988998824</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- ---------------- ------------- -------------- -------------\n",
" 999999 0.00713629971701 4.97493631535 -25.2879522527 24.8988998824"
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# if the invalid value is already masked by NDData.mask, then no warning is issued\n",
"img1[0, 0] = np.nan\n",
"mask = np.zeros_like(img1, dtype=bool)\n",
"mask[0, 0] = True\n",
"nd1 = NDData(img1, mask=mask)\n",
"imutils.imstats(nd1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4437036752\">\n",
"<thead><tr><th>npixels</th><th>mean</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>999999</td><td>0.00713629971701</td><td>4.97493631535</td><td>-25.2879522527</td><td>24.8988998824</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 28,
"text": [
"<Table masked=False length=1>\n",
"npixels mean std min max \n",
" int64 float64 float64 float64 float64 \n",
"------- ---------------- ------------- -------------- -------------\n",
" 999999 0.00713629971701 4.97493631535 -25.2879522527 24.8988998824"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# the 'columns' input can also include NDData.meta (i.e header) keys\n",
"# getting other values from NDData.meta\n",
"nd1.meta['exptime'] = 1200.\n",
"nd1.meta['gain'] = 2.\n",
"columns = ['exptime', 'gain', 'npixels', 'mean', 'std']\n",
"imutils.imstats(nd1, columns=columns)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4401599504\">\n",
"<thead><tr><th>exptime</th><th>gain</th><th>npixels</th><th>mean</th><th>std</th></tr></thead>\n",
"<thead><tr><th>float64</th><th>float64</th><th>int64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>1200.0</td><td>2.0</td><td>1000000</td><td>0.00713629258071</td><td>4.97493382789</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"<Table masked=False length=1>\n",
"exptime gain npixels mean std \n",
"float64 float64 int64 float64 float64 \n",
"------- ------- ------- ---------------- -------------\n",
" 1200.0 2.0 1000000 0.00713629258071 4.97493382789"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# non-existing keys in NDData.meta return None\n",
"columns = ['exptime', 'gain', 'readnoise', 'npixels', 'mean', 'std']\n",
"imutils.imstats(nd1, columns=columns)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4401599056\">\n",
"<thead><tr><th>exptime</th><th>gain</th><th>readnoise</th><th>npixels</th><th>mean</th><th>std</th></tr></thead>\n",
"<thead><tr><th>float64</th><th>float64</th><th>object</th><th>int64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>1200.0</td><td>2.0</td><td>None</td><td>1000000</td><td>0.00713629258071</td><td>4.97493382789</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"<Table masked=False length=1>\n",
"exptime gain readnoise npixels mean std \n",
"float64 float64 object int64 float64 float64 \n",
"------- ------- --------- ------- ---------------- -------------\n",
" 1200.0 2.0 None 1000000 0.00713629258071 4.97493382789"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# columns can take a single string or list of strings\n",
"imutils.imstats(nd1, columns='mean')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=1&gt;\n",
"<table id=\"table4401600720\">\n",
"<thead><tr><th>mean</th></tr></thead>\n",
"<thead><tr><th>float64</th></tr></thead>\n",
"<tr><td>0.00713629258071</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"<Table masked=False length=1>\n",
" mean \n",
" float64 \n",
"----------------\n",
"0.00713629258071"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# a list of multiple NDData objects can be input\n",
"columns = ['npixels', 'nrejected', 'mean', 'median', 'mode', 'std', 'min', 'max']\n",
"imutils.imstats([nd1, nd2], columns=columns)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"&lt;Table masked=False length=2&gt;\n",
"<table id=\"table4501306768\">\n",
"<thead><tr><th>npixels</th><th>nrejected</th><th>mean</th><th>median</th><th>mode</th><th>std</th><th>min</th><th>max</th></tr></thead>\n",
"<thead><tr><th>int64</th><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>\n",
"<tr><td>999999</td><td>1</td><td>0.00713629971701</td><td>0.0</td><td>-0.014272599434</td><td>4.97493631535</td><td>-25.2879522527</td><td>24.8988998824</td></tr>\n",
"<tr><td>1000000</td><td>0</td><td>10.0373434517</td><td>10.0341016532</td><td>10.0276180564</td><td>24.9988162975</td><td>-116.439761264</td><td>134.494499412</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 33,
"text": [
"<Table masked=False length=2>\n",
"npixels nrejected mean ... min max \n",
" int64 int64 float64 ... float64 float64 \n",
"------- --------- ---------------- ... -------------- -------------\n",
" 999999 1 0.00713629971701 ... -25.2879522527 24.8988998824\n",
"1000000 0 10.0373434517 ... -116.439761264 134.494499412"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment