{"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":"markdown","source":"# Vesuvius - Interactive visualization using Plotly library for the 1st train example","metadata":{}},{"cell_type":"markdown","source":"Interactive view for 800 px X 800 px:\n+ Zoom by selecting an area with the mouse\n+ Zoom out with a double click\n+ Move by holding shift + click (or using the Plotly toolbar at the top of the figure)\n+ Use slider to view different surfaces (inklabels is visible on the last element)","metadata":{}},{"cell_type":"code","source":"import plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nfrom PIL import Image\nimport plotly.subplots as sp\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nir_img=(plt.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png\")*255).astype(np.uint8)\n\nsurfaces=[]\nfor i in tqdm(range(65)) :\n    i=str(i)\n    if len(i)==1 :\n        i='0'+i\n    surfaces.append((plt.imread(f'/kaggle/input/vesuvius-challenge-ink-detection/train/1/surface_volume/{i}.tif')/255).astype(np.uint8))\n\nsurfaces.append((plt.imread('/kaggle/input/vesuvius-challenge-ink-detection/train/1/inklabels.png')*255).astype(np.uint8))\n\n\ndef dynamic_vis(X_POS,Y_POS) :\n#     X_POS=2000\n#     Y_POS=3000\n\n    image1 = ir_img[Y_POS-400:Y_POS+400,X_POS-400:X_POS+400]\n    image1[0,0]=0\n    image1[0,1]=255\n\n    n_images=[]\n    for i in range(66) :\n        n_images.append(surfaces[i][Y_POS-400:Y_POS+400,X_POS-400:X_POS+400])\n        n_images[-1][0,0]=0\n        n_images[-1][0,1]=255\n\n    img_width, img_height = np.shape(image1)\n\n    img1 = Image.fromarray((plt.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png\")*255).astype(np.uint8)[2000:3600,2000:2900])\n    img2 = Image.fromarray((plt.imread(\"/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png\")*255).astype(np.uint8)[4000:5600,2000:2900])\n\n    fig = make_subplots(\n        rows=1, cols=2, subplot_titles=(\"surface_volume + inklabels\", \"ir\"),\n        vertical_spacing=0.075\n    )\n\n    fig.add_trace(go.Scatter(\n                        x=[0, img_width],\n                        y=[0, img_height],\n                        mode=\"markers\",\n                        marker_opacity=0\n                    ),\n                  row=1, col=1)\n\n    fig.add_trace(go.Scatter(\n                        x=[0, img_width],\n                        y=[0, img_height],\n                        mode=\"markers\",\n                        marker_opacity=0\n                    ),\n                  row=1, col=2)\n\n    fig.update_layout(width=1300, height=700, showlegend=False,\n                    images= [    dict(\n            x=0,\n            sizex=img_width,\n            y=img_height,\n            sizey=img_height,\n            xref=\"x\",\n            yref=\"y\",\n            opacity=1.0,\n            layer=\"below\",\n            sizing=\"stretch\",\n            source=Image.fromarray(n_images[0])),\n        dict(\n            x=0,\n            sizex=img_width,\n            y=img_height,\n            sizey=img_height,\n            xref=\"x2\",\n            yref=\"y\",\n            opacity=1.0,\n            layer=\"below\",\n            sizing=\"stretch\",\n            source=Image.fromarray(image1))\n                            ])\n\n    fig.update_xaxes(showgrid=False, zeroline=False)\n    fig.update_yaxes(showgrid=False, zeroline=False)\n\n    fig.update_xaxes(matches='x')\n    fig.update_yaxes(matches='y')\n\n    steps = []\n    for i in range(66) :\n        steps.append({\"label\": f\"{i}\", \"method\": \"update\", \"args\": [{'images': []},\n                {'images': [{'layer': 'below',\n                 'opacity': 1.0,\n                 'sizex': img_width,\n                 'sizey': img_height,\n                 'sizing': 'stretch',\n                 'source': Image.fromarray(n_images[i]),\n                 'x': 0,\n                 'xref': 'x',\n                 'y': img_height,\n                 'yref': 'y'},\n                 {'layer': 'below',\n                 'opacity': 1.0,\n                 'sizex': img_width,\n                 'sizey': img_height,\n                 'sizing': 'stretch',\n                 'source': Image.fromarray(image1),\n                 'x': 0,\n                 'xref': 'x2',\n                 'y': img_height,\n                 'yref': 'y'}]}]})\n\n    slider = go.layout.Slider(\n        active=0,\n        steps=steps\n    )\n\n    fig.update_layout(\n        sliders=[slider],\n    )\n\n    fig.update_layout(\n        xaxis=dict(range=[0, img_width]),\n        yaxis=dict(range=[0, img_height])\n    )\n\n    fig.show(config={'doubleClick': 'reset'})\n    \nfig = plt.figure()\nax = plt.gca()\n\nplt.imshow(ir_img,cmap=\"gray\")\nplt.xticks(np.arange(0,np.shape(ir_img)[1],800), rotation=60)\nplt.yticks(np.arange(0,np.shape(ir_img)[0],800))\n\nax.set_xticks(np.arange(-400,np.shape(ir_img)[1]+400,800), minor=True)\nax.set_yticks(np.arange(-400,np.shape(ir_img)[0]+400,800), minor=True)\n\nax.grid(which='minor', alpha=1)\nplt.show()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-03T18:28:31.258296Z","iopub.execute_input":"2023-04-03T18:28:31.258735Z","iopub.status.idle":"2023-04-03T18:30:21.752957Z","shell.execute_reply.started":"2023-04-03T18:28:31.258696Z","shell.execute_reply":"2023-04-03T18:30:21.751233Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the grid above to determinate your wanted X_POS and Y_POS\ndynamic_vis(X_POS=2400,Y_POS=4000)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T18:30:24.555739Z","iopub.execute_input":"2023-04-03T18:30:24.556137Z","iopub.status.idle":"2023-04-03T18:30:42.879210Z","shell.execute_reply.started":"2023-04-03T18:30:24.556102Z","shell.execute_reply":"2023-04-03T18:30:42.876045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}