{"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":"This notebook visualizes the graph used in the GNN approach.\n* https://arxiv.org/abs/2209.03042","metadata":{}},{"cell_type":"code","source":"!pip install polars","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport polars as pl\n\nimport plotly.express as px\nimport plotly.graph_objects as go\n\nfrom sklearn.neighbors import kneighbors_graph","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df = pl.scan_parquet('/kaggle/input/icecube-neutrinos-in-deep-ice/train_meta.parquet')\nsensor_geometry = (pl.scan_csv('/kaggle/input/icecube-neutrinos-in-deep-ice/sensor_geometry.csv')\n                .with_columns([pl.col('sensor_id').cast(pl.Int16)]))\n\nbatch_id = meta_df.select('batch_id').unique().collect()[0].item()\nbatch_df = meta_df.filter(pl.col('batch_id')==batch_id)\nbatch_features = pl.scan_parquet(f'/kaggle/input/icecube-neutrinos-in-deep-ice/train/batch_{batch_id}.parquet')\n\nevent_id = 79\nevent_features = batch_features.filter(pl.col('event_id')==event_id).join(sensor_geometry, on='sensor_id', how='left').collect()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"position = event_features.select(['x', 'y', 'z']).to_numpy()\ntime = event_features.select('time').to_numpy()[:,0]\ncharge = event_features.select('charge').to_numpy()[:,0]\nauxiliary = event_features.select('auxiliary').to_numpy()[:,0]\n\nposition = position[~auxiliary]\ntime = time[~auxiliary]\ncharge = charge[~auxiliary]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# k-Nearest Neighbors Graph","metadata":{}},{"cell_type":"code","source":"k = 8\n\nA = kneighbors_graph(position, n_neighbors=k, mode='connectivity', include_self=True, n_jobs=-1)\nsrcs, dsts = A.nonzero()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# draw nodes\nXn = position[:,0]\nYn = position[:,1]\nZn = position[:,2]\nnodes = go.Scatter3d(x=Xn,\n                     y=Yn,\n                     z=Zn,\n                     mode='markers',\n                     marker=dict(symbol='circle',\n                                 size=3,\n                                 color='blue'),\n                     name='node'\n                    )\n\n# draw edges\nXe=[]\nYe=[]\nZe=[]\nfor src, dst in zip(srcs, dsts):\n    Xe += [Xn[src], Xn[dst], None]\n    Ye += [Yn[src], Yn[dst], None]\n    Ze += [Zn[src], Zn[dst], None]\nedges = go.Scatter3d(x=Xe, \n                     y=Ye,\n                     z=Ze,\n                     mode='lines',\n                     line=dict(color='black', width=3),\n                     name='edge'\n                    )\n\n\nlayout = go.Layout(\n    width=800,\n    height=700,\n    autosize=False,\n    scene=dict(\n        camera=dict(\n            up=dict(\n                x=0,\n                y=0,\n                z=1\n            ),\n            eye=dict(\n                x=0,\n                y=1,\n                z=1,\n            )\n        ),\n        aspectratio=dict(x=1,y=1,z=1),\n        aspectmode='manual'\n    ),\n)\n    \ndata=[nodes, edges]\nfig=go.Figure(data=data, layout=layout)\nfig.show()","metadata":{},"execution_count":null,"outputs":[]}]}