Skip to content

Commit

Permalink
improved the interactive widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshputalapattu committed Jan 3, 2022
1 parent 4a6e4e8 commit 29092de
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions jupyter_interactive_environment_exploration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,33 @@
"Use the below interactive widget to explore how the quality of the reconstructed image varies with $k$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compute_k_max(img_name):\n",
" \"\"\"\n",
" utility function for calculating max value of the slider range\n",
" \"\"\"\n",
" img = gray_images[img_name]\n",
" m,n = img.shape\n",
" return m*n/(m+n+1)\n",
"\n",
"#set up the widgets\n",
"import ipywidgets as widgets\n",
"\n",
"list_widget = widgets.Dropdown(options=list(gray_images.keys()))\n",
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max('cat'))\n",
"def update_k_max(*args):\n",
" img_name=list_widget.value\n",
" int_slider_widget.max = compute_k_max(img_name)\n",
"list_widget.observe(update_k_max,'value')\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
Expand All @@ -309,7 +336,7 @@
}
],
"source": [
"interact(compress_show_gray_images,img_name=list(gray_images.keys()),k=(1,300));"
"interact(compress_show_gray_images,img_name=list_widget,k=int_slider_widget);"
]
},
{
Expand Down Expand Up @@ -390,6 +417,26 @@
"Here is the interactive widget to explore image compression of color images using the reshape method. By dragging the slider to vary $k$, observe how image quality varies. Also, we can explore different images by selecting through the drop down widget."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compute_k_max_color_images(img_name):\n",
" image = color_images[img_name]\n",
" original_shape = image.shape\n",
" return (original_shape[0]*original_shape[1]*original_shape[2])//(original_shape[0] + 3*original_shape[1] + 1)\n",
"\n",
"\n",
"list_widget = widgets.Dropdown(options=list(color_images.keys()))\n",
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max_color_images('cat'))\n",
"def update_k_max_color(*args):\n",
" img_name=list_widget.value\n",
" int_slider_widget.max = compute_k_max_color_images(img_name)\n",
"list_widget.observe(update_k_max_color,'value')"
]
},
{
"cell_type": "code",
"execution_count": 22,
Expand All @@ -409,7 +456,7 @@
}
],
"source": [
"interact(compress_show_color_images_reshape,img_name=list(color_images.keys()),k=(0,512));"
"interact(compress_show_color_images_reshape,img_name=list_widget,k=int_slider_widget);"
]
},
{
Expand Down Expand Up @@ -465,6 +512,26 @@
"Here is the widget to explore layers method of compressing color images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compute_k_max_color_images_layers(img_name):\n",
" image = color_images[img_name]\n",
" original_shape = image.shape\n",
" return (original_shape[0]*original_shape[1]*original_shape[2])// (3*(original_shape[0] + original_shape[1] + 1))\n",
"\n",
"\n",
"list_widget = widgets.Dropdown(options=list(color_images.keys()))\n",
"int_slider_widget = widgets.IntSlider(min=1,max=compute_k_max_color_images_layers('cat'))\n",
"def update_k_max_color_layers(*args):\n",
" img_name=list_widget.value\n",
" int_slider_widget.max = compute_k_max_color_images_layers(img_name)\n",
"list_widget.observe(update_k_max_color_layers,'value')"
]
},
{
"cell_type": "code",
"execution_count": 21,
Expand All @@ -484,7 +551,7 @@
}
],
"source": [
"interact(compress_show_color_images_layer,img_name=list(color_images.keys()),k=(1,550));"
"interact(compress_show_color_images_layer,img_name=list_widget,k=int_slider_widget);"
]
},
{
Expand Down

0 comments on commit 29092de

Please sign in to comment.