{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfrom PIL import Image\nfrom IPython.display import clear_output\nimport time\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport ipywidgets as widgets\nfrom IPython.display import display\nfrom ipywidgets import interactive\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.173056Z","iopub.execute_input":"2023-04-27T17:22:42.173468Z","iopub.status.idle":"2023-04-27T17:22:42.263585Z","shell.execute_reply.started":"2023-04-27T17:22:42.173431Z","shell.execute_reply":"2023-04-27T17:22:42.262432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# About\n\n**This notebook plots the histograms of each .tif file and separates the ink from the scroll. Hopefully this will allow the viewer to understand more about the distribution of each object. Scrollbars are presented above each viewer.**","metadata":{}},{"cell_type":"code","source":"folder_paths = [\n    '/kaggle/input/vesuvius-challenge-ink-detection/train/1/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/train/2/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/train/3/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/test/a/surface_volume/',\n    '/kaggle/input/vesuvius-challenge-ink-detection/test/b/surface_volume/']","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.265299Z","iopub.execute_input":"2023-04-27T17:22:42.265636Z","iopub.status.idle":"2023-04-27T17:22:42.271188Z","shell.execute_reply.started":"2023-04-27T17:22:42.265603Z","shell.execute_reply":"2023-04-27T17:22:42.269808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_layers(folder_path):\n    filenames = sorted(os.listdir(folder_path))\n    tiff_files = [filename for filename in filenames if filename.endswith('.tif')]\n    layers = []\n    for tiff_file in tiff_files:\n        file_path = os.path.join(folder_path, tiff_file)\n        layer = Image.open(file_path)\n        layers.append(layer)\n    mask = Image.open(folder_path[:-15]+\"inklabels.png\")\n\n    return layers,mask","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.272554Z","iopub.execute_input":"2023-04-27T17:22:42.272987Z","iopub.status.idle":"2023-04-27T17:22:42.281459Z","shell.execute_reply.started":"2023-04-27T17:22:42.272947Z","shell.execute_reply":"2023-04-27T17:22:42.280427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_histograms(layers):\n    # Convert the image to a NumPy array\n    histograms = []\n    for layer in layers:\n        layer = np.array(layer)\n        histograms.append(layer.ravel())\n        \n    return histograms","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.283966Z","iopub.execute_input":"2023-04-27T17:22:42.284265Z","iopub.status.idle":"2023-04-27T17:22:42.291885Z","shell.execute_reply.started":"2023-04-27T17:22:42.284237Z","shell.execute_reply":"2023-04-27T17:22:42.290839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_histogram(index, ink, scroll):\n    plt.figure(figsize=(10, 6))\n    plt.hist(ink[index], bins=40, alpha=0.5, label=\"Ink\")\n    plt.hist(scroll[index], bins=40, alpha=0.5, label=\"Scroll\")\n    plt.title(f'Histograms (Slider Value: {index})')\n    plt.xlabel('Values')\n    plt.ylabel('Frequency')\n    plt.legend()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.293190Z","iopub.execute_input":"2023-04-27T17:22:42.293710Z","iopub.status.idle":"2023-04-27T17:22:42.302177Z","shell.execute_reply.started":"2023-04-27T17:22:42.293669Z","shell.execute_reply":"2023-04-27T17:22:42.301258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def separate_by_label(histograms,mask):\n    mask = mask.ravel()\n    \n    ink_idx = np.where(mask==1)\n    scroll_idx = np.where(mask==0)\n    \n    scroll = []\n    ink = []\n    \n    for i,histogram in enumerate(histograms):\n        ink_ = histogram[ink_idx]\n        scroll_ = histogram[scroll_idx]\n        \n        ink_zero_idx = np.where(ink_>0)\n        scroll_zero_idx = np.where(scroll_>0)\n        \n        scroll.append(scroll_[scroll_zero_idx])\n        ink.append(ink_[ink_zero_idx])\n        \n    return scroll,ink","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.305572Z","iopub.execute_input":"2023-04-27T17:22:42.306431Z","iopub.status.idle":"2023-04-27T17:22:42.315589Z","shell.execute_reply.started":"2023-04-27T17:22:42.306397Z","shell.execute_reply":"2023-04-27T17:22:42.314780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 1","metadata":{}},{"cell_type":"code","source":"layers,mask = load_layers(folder_paths[0])\nhistograms = return_histograms(layers)\nmask = np.array(mask)\nscroll_,ink_ = separate_by_label(histograms,mask)\ndel layers,mask","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:22:42.316846Z","iopub.execute_input":"2023-04-27T17:22:42.317354Z","iopub.status.idle":"2023-04-27T17:24:37.967441Z","shell.execute_reply.started":"2023-04-27T17:22:42.317322Z","shell.execute_reply":"2023-04-27T17:24:37.966151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a slider for the image indices\nhistogram_index_slider = widgets.IntSlider(min=0, max=len(histograms) - 1, step=1, value=0, description='Slice Index')\n\n# Display the interactive plot\ninteractive(plot_histogram, scroll = widgets.fixed(scroll_), ink = widgets.fixed(ink_),index=histogram_index_slider)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:24:37.968962Z","iopub.execute_input":"2023-04-27T17:24:37.969371Z","iopub.status.idle":"2023-04-27T17:24:39.038048Z","shell.execute_reply.started":"2023-04-27T17:24:37.969326Z","shell.execute_reply":"2023-04-27T17:24:39.036934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 2","metadata":{}},{"cell_type":"markdown","source":"**Too Large, but leaving it here**","metadata":{}},{"cell_type":"code","source":"#layers,mask = load_layers(folder_paths[1])\n#histograms = return_histograms(layers)\n#mask = np.array(mask)\n#scroll_,ink_ = separate_by_label(histograms,mask)\n#del layers,mask","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-27T17:24:39.041846Z","iopub.execute_input":"2023-04-27T17:24:39.042350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a slider for the image indices\n#histogram_index_slider = widgets.IntSlider(min=0, max=len(histograms) - 1, step=1, value=0, description='Slice Index')\n\n# Display the interactive plot\n#interactive(plot_histogram, scroll = widgets.fixed(scroll_), ink = widgets.fixed(ink_),index=histogram_index_slider)","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 3","metadata":{}},{"cell_type":"code","source":"layers,mask = load_layers(folder_paths[2])\nhistograms = return_histograms(layers)\nmask = np.array(mask)\nscroll_,ink_ = separate_by_label(histograms,mask)\ndel layers,mask","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a slider for the image indices\nhistogram_index_slider = widgets.IntSlider(min=0, max=len(histograms) - 1, step=1, value=0, description='Slice Index')\n\n# Display the interactive plot\ninteractive(plot_histogram, scroll = widgets.fixed(scroll_), ink = widgets.fixed(ink_),index=histogram_index_slider)","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}