{"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-05-05T23:04:30.887467Z","iopub.execute_input":"2023-05-05T23:04:30.888341Z","iopub.status.idle":"2023-05-05T23:04:30.895213Z","shell.execute_reply.started":"2023-05-05T23:04:30.888295Z","shell.execute_reply":"2023-05-05T23:04:30.893768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# About\n\nThis is a quick test to visualize the differences between the distrubtion over a smaller sample of ink vs no ink. For this example I sampled the letter 'P' in the first training example. A quick glance shows that the ink distribution significantly differs from the no-ink distributions within the first 20 layers and then continues to converge in later layers. Food for thought.","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-05-05T23:04:30.897871Z","iopub.execute_input":"2023-05-05T23:04:30.898260Z","iopub.status.idle":"2023-05-05T23:04:30.911189Z","shell.execute_reply.started":"2023-05-05T23:04:30.898222Z","shell.execute_reply":"2023-05-05T23:04:30.909538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rect = {\"x\": 1100, \"y\": 3500, \"width\": 700, \"height\": 950}","metadata":{"execution":{"iopub.status.busy":"2023-05-05T23:04:30.912664Z","iopub.execute_input":"2023-05-05T23:04:30.913145Z","iopub.status.idle":"2023-05-05T23:04:30.924797Z","shell.execute_reply.started":"2023-05-05T23:04:30.913095Z","shell.execute_reply":"2023-05-05T23:04:30.923239Z"},"_kg_hide-input":true,"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-05-05T23:04:30.927254Z","iopub.execute_input":"2023-05-05T23:04:30.927743Z","iopub.status.idle":"2023-05-05T23:04:30.938396Z","shell.execute_reply.started":"2023-05-05T23:04:30.927690Z","shell.execute_reply":"2023-05-05T23:04:30.937223Z"},"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-05-05T23:04:30.942487Z","iopub.execute_input":"2023-05-05T23:04:30.943017Z","iopub.status.idle":"2023-05-05T23:04:30.961546Z","shell.execute_reply.started":"2023-05-05T23:04:30.942932Z","shell.execute_reply":"2023-05-05T23:04:30.960453Z"},"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\",density=True)\n    plt.hist(scroll[index], bins=40, alpha=0.5, label=\"Scroll\",density=True)\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-05-05T23:04:30.963003Z","iopub.execute_input":"2023-05-05T23:04:30.963449Z","iopub.status.idle":"2023-05-05T23:04:30.975308Z","shell.execute_reply.started":"2023-05-05T23:04:30.963401Z","shell.execute_reply":"2023-05-05T23:04:30.973611Z"},"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-05-05T23:04:30.976633Z","iopub.execute_input":"2023-05-05T23:04:30.977305Z","iopub.status.idle":"2023-05-05T23:04:30.986704Z","shell.execute_reply.started":"2023-05-05T23:04:30.977255Z","shell.execute_reply":"2023-05-05T23:04:30.985646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 1","metadata":{}},{"cell_type":"code","source":"layers,mask = load_layers(folder_paths[0])\n\ntemp = []\nfor i in range(0,65):\n    t = np.array(layers[i])\n    t = t[rect[\"y\"] : rect[\"y\"] + rect[\"height\"], rect[\"x\"] : rect[\"x\"] + rect[\"width\"]]\n    temp.append(t)\n\nlayers = temp\nmask = np.array(mask)\nmask = mask[rect[\"y\"] : rect[\"y\"] + rect[\"height\"], rect[\"x\"] : rect[\"x\"] + rect[\"width\"]]\n\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-05-05T23:06:25.546906Z","iopub.execute_input":"2023-05-05T23:06:25.547375Z","iopub.status.idle":"2023-05-05T23:07:27.580492Z","shell.execute_reply.started":"2023-05-05T23:06:25.547317Z","shell.execute_reply":"2023-05-05T23:07:27.578170Z"},"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',density=True)\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-05-05T23:09:04.708165Z","iopub.execute_input":"2023-05-05T23:09:04.708606Z","iopub.status.idle":"2023-05-05T23:09:05.171117Z","shell.execute_reply.started":"2023-05-05T23:09:04.708567Z","shell.execute_reply":"2023-05-05T23:09:05.169673Z"},"trusted":true},"execution_count":null,"outputs":[]}]}