Skip to content

Commit

Permalink
Merge pull request #255 from larrybradley/fix-bkgsub
Browse files Browse the repository at this point in the history
Fix background subtraction notebook
  • Loading branch information
camipacifici authored Oct 31, 2024
2 parents e1a7136 + 60e16d5 commit aa0a499
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion notebooks/NIRCam/psf_photometry/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ numpy>=1.25.2
pandas>=2.1.0
jwst>=1.11.4
astropy>=5.3.3
photutils>=1.11.0
photutils>=2.0.2
ipywidgets>=8.1.1
matplotlib>=3.7.2
webbpsf>=1.2.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"from photutils.background import (Background2D,\n",
" BkgIDWInterpolator, BkgZoomInterpolator,\n",
" MedianBackground)\n",
"from photutils.datasets import make_gaussian_sources_image\n",
"from photutils.datasets import make_model_image\n",
"from photutils.segmentation import detect_sources, make_2dgaussian_kernel\n",
"from scipy.ndimage import median_filter"
]
Expand Down Expand Up @@ -413,7 +413,9 @@
"metadata": {},
"outputs": [],
"source": [
"source_img = make_gaussian_sources_image(sky_bkgd.shape, sources)\n",
"model = models.Gaussian2D()\n",
"source_img = make_model_image(sky_bkgd.shape, model, sources, \n",
" x_name='x_mean', y_name='y_mean')\n",
"scene = source_img + sky_bkgd"
]
},
Expand Down Expand Up @@ -485,7 +487,8 @@
" bkg_estimator=MedianBackground(),\n",
" interpolator=BkgZoomInterpolator(order=3))\n",
"plt.figure(figsize=(6, 6))\n",
"plt.imshow(bkg.background-noiseless_sky, vmin=0.1*zmin, vmax=0.1*zmax, origin='lower')"
"bkgimg = bkg.background\n",
"plt.imshow(bkgimg-noiseless_sky, vmin=0.1*zmin, vmax=0.1*zmax, origin='lower')"
]
},
{
Expand Down Expand Up @@ -525,9 +528,9 @@
"metadata": {},
"outputs": [],
"source": [
"mask_2sigma = make_source_mask(scene-bkg.background, nsigma=2, npixels=3,\n",
"mask_2sigma = make_source_mask(scene-bkgimg, nsigma=2, npixels=3,\n",
" dilate_size=5, filter_fwhm=3)\n",
"mask_3sigma = make_source_mask(scene-bkg.background, nsigma=3, npixels=3,\n",
"mask_3sigma = make_source_mask(scene-bkgimg, nsigma=3, npixels=3,\n",
" dilate_size=5, filter_fwhm=3)\n",
"plot_two(mask_2sigma, mask_3sigma, 0, 1)"
]
Expand All @@ -551,7 +554,9 @@
" sigma_clip=sigma_clip, bkg_estimator=bkg_estimator)\n",
"bkg2 = Background2D(scene, (15, 15), filter_size=(3, 3), mask=mask_3sigma,\n",
" sigma_clip=sigma_clip, bkg_estimator=bkg_estimator)\n",
"plot_two(bkg1.background-noiseless_sky, bkg2.background-noiseless_sky,\n",
"bkg1img = bkg1.background\n",
"bkg2img = bkg2.background\n",
"plot_two(bkg1img-noiseless_sky, bkg2img-noiseless_sky,\n",
" 0.1*zmin, 0.1*zmax, titles=['bkg1', 'bkg2'])"
]
},
Expand Down Expand Up @@ -862,7 +867,8 @@
"outputs": [],
"source": [
"bkg3 = my_background(scene, box_size=10, filter_size=5, mask=mask)\n",
"plot_bkgd(scene, mask, bkg1.background, noiseless_sky, sources, zmin, zmax)"
"bkg3img = bkg3.background\n",
"plot_bkgd(scene, mask, bkg3img, noiseless_sky, sources, zmin, zmax)"
]
},
{
Expand All @@ -885,7 +891,8 @@
"interpolator = BkgIDWInterpolator(n_neighbors=20, power=1, reg=30)\n",
"bkg4 = my_background(scene, box_size=10, filter_size=5, mask=mask,\n",
" interp=interpolator, exclude_percentile=90)\n",
"plot_bkgd(scene, mask, bkg4.background, noiseless_sky, sources, zmin, zmax)"
"bkg4img = bkg4.background\n",
"plot_bkgd(scene, mask, bkg4img, noiseless_sky, sources, zmin, zmax)"
]
},
{
Expand All @@ -902,10 +909,10 @@
"metadata": {},
"outputs": [],
"source": [
"sm = SourceMask(scene-bkg4.background, nsigma=1.5)\n",
"sm = SourceMask(scene-bkg4img, nsigma=1.5)\n",
"mask = sm.multiple(filter_fwhm=[1, 3, 5],\n",
" tophat_size=[4, 2, 1])\n",
"plot_mask(scene, bkg4.background, mask, zmin, zmax)\n",
"plot_mask(scene, bkg4img, mask, zmin, zmax)\n",
"mask.sum()"
]
},
Expand All @@ -926,7 +933,8 @@
"interpolator = BkgIDWInterpolator(n_neighbors=20, power=0, reg=30)\n",
"bkg5 = my_background(scene, box_size=6, filter_size=3, mask=mask,\n",
" interp=interpolator, exclude_percentile=90)\n",
"plot_bkgd(scene, mask, bkg5.background, noiseless_sky, sources, zmin, zmax)"
"bkg5img = bkg5.background\n",
"plot_bkgd(scene, mask, bkg5img, noiseless_sky, sources, zmin, zmax)"
]
},
{
Expand All @@ -946,7 +954,7 @@
"metadata": {},
"outputs": [],
"source": [
"bkgd = bkg5.background\n",
"bkgd = bkg5img\n",
"residual_image = bkgd-noiseless_sky\n",
"\n",
"# Create columns in the table for the background estimate, the residual,\n",
Expand Down Expand Up @@ -1196,11 +1204,11 @@
"source": [
"rms = Table()\n",
"widths = rms['widths'] = np.arange(3, 30, 1)\n",
"rms['bkg1'] = calculate_stats(scene-bkg1.background, mask, widths)\n",
"rms['bkg2'] = calculate_stats(scene-bkg2.background, mask, widths)\n",
"rms['bkg3'] = calculate_stats(scene-bkg3.background, mask, widths)\n",
"rms['bkg4'] = calculate_stats(scene-bkg4.background, mask, widths)\n",
"rms['bkg5'] = calculate_stats(scene-bkg5.background, mask, widths)\n",
"rms['bkg1'] = calculate_stats(scene-bkg1img, mask, widths)\n",
"rms['bkg2'] = calculate_stats(scene-bkg2img, mask, widths)\n",
"rms['bkg3'] = calculate_stats(scene-bkg3img, mask, widths)\n",
"rms['bkg4'] = calculate_stats(scene-bkg4img, mask, widths)\n",
"rms['bkg5'] = calculate_stats(scene-bkg5img, mask, widths)\n",
"rms['perfect'] = calculate_stats(sky_bkgd-noiseless_sky, mask, widths)"
]
},
Expand Down Expand Up @@ -1252,7 +1260,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ numpy >= 2.0.0
astropy >= 6.1.1
matplotlib >= 3.9.1
scipy >= 1.14.0
photutils >= 1.13.0
photutils >= 2.0.2
scikit-image >= 0.21.0
jdaviz >= 3.10.2

0 comments on commit aa0a499

Please sign in to comment.