{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This webpage shows how to setup pyvista for jupyter notebook.\nHowever I cannot get it to work for interative 3d plot.\n\nhttps://docs.pyvista.org/version/stable/user-guide/jupyter/trame.html#trame-jupyter \n\nhttps://github.com/pyvista/pyvista/issues/313 \n","metadata":{}},{"cell_type":"code","source":"!pip install ipywidgets 'pyvista[all,trame]'   ","metadata":{"execution":{"iopub.status.busy":"2023-12-06T08:02:28.152235Z","iopub.execute_input":"2023-12-06T08:02:28.153235Z","iopub.status.idle":"2023-12-06T08:02:44.886395Z","shell.execute_reply.started":"2023-12-06T08:02:28.153187Z","shell.execute_reply":"2023-12-06T08:02:44.884529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# setup enviornment for interactive plotting\n# https://github.com/pyvista/pyvista/issues/313\n\ntry:\n    import pyvirtualdisplay\nexcept: \n    print('pip ...')\n    !pip install -q piglet pyvirtualdisplay\n    !pip install -q vtk\n    !pip install -q trame ipywidgets\n    !pip install -q trame-vuetify\n    !pip install -q pyvista  \n    !apt-get install -y xvfb\n\n    \nimport vtk\nfrom pyvirtualdisplay import Display\ndisplay = Display(visible=0, size=(600, 400))\ndisplay.start()\nprint('IMPORT OK')\n\n#[optional] follow instruct here to restart kernel: https://www.kaggle.com/code/aohorodnyk/pandas-sql-restart-kernel","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-06T08:02:44.900771Z","iopub.execute_input":"2023-12-06T08:02:44.901213Z","iopub.status.idle":"2023-12-06T08:02:45.015381Z","shell.execute_reply.started":"2023-12-06T08:02:44.901179Z","shell.execute_reply":"2023-12-06T08:02:45.014194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# notebook plotting: https://docs.pyvista.org/version/stable/user-guide/jupyter/index.html\nimport pyvista as pv\npv.set_jupyter_backend('trame')\n\n# short example\nsphere = pv.Sphere()\nsphere.plot(jupyter_backend = 'trame')\n## BUG !!! this should be interactive, i.e. you can use mouse to rotate the mesh","metadata":{"execution":{"iopub.status.busy":"2023-12-06T08:02:45.016550Z","iopub.execute_input":"2023-12-06T08:02:45.016922Z","iopub.status.idle":"2023-12-06T08:02:45.326073Z","shell.execute_reply.started":"2023-12-06T08:02:45.016842Z","shell.execute_reply":"2023-12-06T08:02:45.324638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\n\nif 1:\n    data_dir = '/kaggle/input/blood-vessel-segmentation'\n    file = [f'{data_dir}/train/kidney_3_sparse/labels/{i:04d}.tif' for i in  range(496, 496+100)]\n    mask=[]\n    for i,f in enumerate(file):\n        print('\\r',i, end='')\n        v = cv2.imread(f,cv2.IMREAD_GRAYSCALE)\n        mask.append(v)\n    mask = np.stack(mask)\n    print('')\n    mask = mask/255\n    print(mask.shape)\n    #np.save('truth.npy',mask)\n   \n\n\npl = pv.Plotter()\npoint1 = np.stack(np.where(mask > 0.1)).T\npd1 = pv.PolyData(point1)\nmesh1 = pd1.glyph(geom=pv.Cube())\npl.add_mesh(mesh1, color='red')\npl.show()\n## BUG !!! this should be interactive, i.e. you can use mouse to rotate the mesh","metadata":{"execution":{"iopub.status.busy":"2023-12-06T08:02:45.327764Z","iopub.execute_input":"2023-12-06T08:02:45.328196Z","iopub.status.idle":"2023-12-06T08:03:04.653449Z","shell.execute_reply.started":"2023-12-06T08:02:45.328151Z","shell.execute_reply":"2023-12-06T08:03:04.651842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#https://laustep.github.io/stlahblog/posts/plotly_python_isosurface.html\nimport plotly.graph_objects as go\nimport plotly.io as pio # to save the graphics as html\n\n\n\nif 1:\n    \n    #maybe will work if you change mesh2 = apply matching cube on voxel mask\n    \n    D,H,W = mask.shape\n    p = mask > 0.1\n\n    value = np.ascontiguousarray(p.transpose(2,1,0)).reshape(-1)\n    grid = pv.ImageData(\n        dimensions=(D,H,W),\n        spacing=(1,1,1),\n        origin=(0,0,0),\n    )\n    x, y, z = grid.points.T\n    mesh2 = grid.contour([1], value, method='marching_cubes')\n    mesh2.plot( smooth_shading=True,  color=\"red\", show_scalar_bar=False)\n\n    points    = mesh2.points\n    triangles = mesh2.faces.reshape(-1, 4)\n\n    fig = go.Figure(data=[\n        go.Mesh3d(\n            x = points[:, 0],\n            y = points[:, 1],\n            z = points[:, 2],\n\n            # i, j and k give the vertices of the triangles\n            i = triangles[:, 1],\n            j = triangles[:, 2],\n            k = triangles[:, 3],\n\n            #colorscale =   \n            #intensity = ,\n            showscale = False\n        )\n    ])\n    fig.update_scenes(aspectmode='data')\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-06T08:03:04.654821Z","iopub.execute_input":"2023-12-06T08:03:04.655304Z"},"trusted":true},"execution_count":null,"outputs":[]}]}