From 29092de32147bb4e9e80cd6340c8823a5337ce52 Mon Sep 17 00:00:00 2001 From: Ramesh Putalapattu Date: Mon, 3 Jan 2022 16:45:53 +0530 Subject: [PATCH] improved the interactive widgets --- ..._interactive_environment_exploration.ipynb | 73 ++++++++++++++++++- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/jupyter_interactive_environment_exploration.ipynb b/jupyter_interactive_environment_exploration.ipynb index 74ff2ed..6f30afd 100644 --- a/jupyter_interactive_environment_exploration.ipynb +++ b/jupyter_interactive_environment_exploration.ipynb @@ -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, @@ -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);" ] }, { @@ -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, @@ -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);" ] }, { @@ -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, @@ -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);" ] }, {