Skip to content

Commit

Permalink
Merge pull request #491 from dirac-institute/notebook_fix
Browse files Browse the repository at this point in the history
Fix the KBMOD reference notebook
  • Loading branch information
jeremykubica authored Feb 26, 2024
2 parents 1e8f893 + 7996cc3 commit a9c5128
Showing 1 changed file with 21 additions and 176 deletions.
197 changes: 21 additions & 176 deletions notebooks/Kbmod_Reference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"metadata": {},
"source": [
"\n",
"### [psf](#psf) \n",
Expand Down Expand Up @@ -166,7 +164,9 @@
"metadata": {},
"outputs": [],
"source": [
"im = kb.LayeredImage(im_path + \"000000.fits\", p)\n",
"from kbmod.data_interface import load_deccam_layered_image\n",
"\n",
"im = load_deccam_layered_image(im_path + \"000000.fits\", p)\n",
"print(f\"Loaded a {im.get_width()} by {im.get_height()} image at time {im.get_obstime()}\")"
]
},
Expand All @@ -185,7 +185,9 @@
"metadata": {},
"outputs": [],
"source": [
"im = kb.LayeredImage(\"image2\", 100, 100, 5.0, 25.0, 0.0, p)\n",
"from kbmod.fake_data.fake_data_creator import make_fake_layered_image\n",
"\n",
"im = make_fake_layered_image(100, 100, 5.0, 25.0, 0.0, p)\n",
"# name, width, height, background_noise_sigma, variance, capture_time, PSF"
]
},
Expand Down Expand Up @@ -229,7 +231,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"A LayeredImage can have its layers set from any numpy array"
"A LayeredImage can have its layers set from any numpy array with type `float32` and observation time. We use -1.0 to indicate no time is given."
]
},
{
Expand All @@ -238,7 +240,8 @@
"metadata": {},
"outputs": [],
"source": [
"raw = kb.RawImage(np.ones_like(pixels))"
"all_ones = np.ones((im.get_height(), im.get_width()))\n",
"raw = kb.RawImage(all_ones.astype(np.float32), -1.0)"
]
},
{
Expand All @@ -252,29 +255,6 @@
"im.get_science()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: Due to how pybind handles references, you will need to call the `set_` function for any layer you change. Simply getting a layer and changing the values directly will not propagate those changes back into the C++ object."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The image at any point can be saved to a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# im.save_layers(im_path+\"/out\") # file will use original name"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -290,29 +270,10 @@
"metadata": {},
"outputs": [],
"source": [
"im.add_object(20.0, 35.0, 2500.0)\n",
"# x, y, flux"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Image masking\n",
"from kbmod.fake_data.fake_data_creator import add_fake_object\n",
"\n",
"The image can mask itself by providing a bitmask of flags (note: masked pixels are set to -9999 so they can be distinguished later from 0.0 pixles)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"flags = ~0\n",
"flag_exceptions = [32, 39]\n",
"# mask all of pixels with flags except those with specifed combiniations\n",
"im.apply_mask_flags(flags, flag_exceptions)"
"add_fake_object(im, 20.0, 35.0, 2500.0)\n",
"# x, y, flux"
]
},
{
Expand Down Expand Up @@ -350,126 +311,10 @@
"source": [
"# Create a stack with 10 50x50 images with random noise and times ranging from 0 to 1\n",
"count = 10\n",
"imlist = [kb.LayeredImage(\"img\" + str(n), 100, 100, 10.0, 5.0, n / count, p) for n in range(count)]\n",
"imlist = [make_fake_layered_image(100, 100, 5.0, 25.0, n / count, p) for n in range(count)]\n",
"stack = kb.ImageStack(imlist)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Manually set the times the images in the stack were taken "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stack.set_times([0, 2, 3, 4.5, 5, 6, 7, 10, 11, 14])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A shortcut is provided to initialize a stack automatically from a list of files. If 'MJD' is in the header for each image, the stack will automatically load the times as well. If not, you can set them as above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"if os.path.exists(im_path):\n",
" files = os.listdir(im_path)\n",
" files = [im_path + f for f in files if \".fits\" in f]\n",
" files.sort()\n",
" print(\"Using loaded files:\")\n",
" print(files)\n",
"\n",
" # Create default PSFs for each image.\n",
" all_psfs = [p for _ in range(len(files))]\n",
"\n",
" # Load the images.\n",
" stack = kb.ImageStack(files, all_psfs)\n",
"else:\n",
" print(\"Cannot find data directory. Using fake images.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A global mask can be generated and applied to the stack"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"flags = ~0 # mask pixels with any flags\n",
"flag_exceptions = [32, 39] # unless it has one of these special combinations of flags\n",
"global_flags = int(\"100111\", 2) # mask any pixels which have any of\n",
"# these flags in more than two images\n",
"\n",
"stack.apply_global_mask(global_flags, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The global mask is saved for future reference"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stack.get_global_mask()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Most features of the LayeredImage can be used on the whole stack"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Apply the masking flags to each image.\n",
"stack.apply_mask_flags(flags, flag_exceptions)\n",
"\n",
"# Convolve each image with its saved PFS.\n",
"stack.convolve_psf()\n",
"\n",
"# Get image statitics. These functions return the information for the first image in the case\n",
"# where the stack contains images of different sizes.\n",
"print(f\"Width = {stack.get_width()}\")\n",
"print(f\"Height = {stack.get_height()}\")\n",
"print(f\"Pixels Per Image = {stack.get_npixels()}\")\n",
"\n",
"# Retrieve a list of LayeredImages back from the stack.\n",
"stack.get_images()\n",
"\n",
"# Get the list of image time stamps.\n",
"print(f\"Times = {stack.get_times()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -488,8 +333,8 @@
"\n",
"# Create a new list of LayeredImages with the added object.\n",
"new_im_list = []\n",
"for im, time in zip(im_list, stack.get_times()):\n",
" im.add_object(20.0 + (time * 8.0), 35.0 + (time * 0.0), 25000.0)\n",
"for im, time in zip(im_list, stack.build_zeroed_times()):\n",
" add_fake_object(im, 20.0 + (time * 8.0), 35.0 + (time * 0.0), 25000.0)\n",
" new_im_list.append(im)\n",
"\n",
"# Save these images in a new ImageStack.\n",
Expand Down Expand Up @@ -544,7 +389,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Launch a basic search that uses the a grid of velocities and angles."
"Launch a basic search that uses the a grid of velocities and angles. To do this we need to first create a generator object to generate the trajectories. Those trajectories get feed into the search function."
]
},
{
Expand Down Expand Up @@ -652,9 +497,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python (.conda-kbmod)",
"display_name": "Jeremy's KBMOD",
"language": "python",
"name": "conda-env-.conda-kbmod-py"
"name": "kbmod_jk"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -666,9 +511,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}

0 comments on commit a9c5128

Please sign in to comment.