{"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":"!pip install /kaggle/input/icecubescripts/polars-0.16.13-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -q\n#!pip install datasets --no-index --find-links=file:///kaggle/input/hf-ds -U\n!pip install ../input/install-pkgs/entmax-1.1-py3-none-any.whl -q\n!pip install ../input/install-pkgs/einops-0.6.0-py3-none-any.whl -q\n!pip install ../input/install-pkgs/x_transformers-1.8.2-py3-none-any.whl -q\nimport subprocess\nfrom pathlib import Path\n\n\n# Install packages\n\n\nwhls = [\n    \"/kaggle/input/pytorchgeometric/torch_cluster-1.6.0-cp37-cp37m-linux_x86_64.whl\",\n    \"/kaggle/input/pytorchgeometric/torch_scatter-2.1.0-cp37-cp37m-linux_x86_64.whl\",\n    \"/kaggle/input/pytorchgeometric/torch_sparse-0.6.16-cp37-cp37m-linux_x86_64.whl\",\n    \"/kaggle/input/pytorchgeometric/torch_spline_conv-1.2.1-cp37-cp37m-linux_x86_64.whl\",\n    \"/kaggle/input/pytorchgeometric/torch_geometric-2.2.0-py3-none-any.whl\",\n    \"/kaggle/input/pytorchgeometric/ruamel.yaml-0.17.21-py3-none-any.whl\",\n]\n\nfor w in whls:\n    print(\"Installing\", w)\n    subprocess.call([\"pip\", \"install\", w, \"--no-deps\", \"--upgrade\", \"-q\"])\n\nimport sys\nsys.path.append(\"/kaggle/input/graphnet/graphnet-main/src\")\nsys.path.append(\"/kaggle/input/\")\nsys.path.append('/kaggle/input/icecube-model-def/')\nsys.path.append('/kaggle/input/icecubescripts/')","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2023-04-19T14:19:47.736861Z","iopub.execute_input":"2023-04-19T14:19:47.737206Z","iopub.status.idle":"2023-04-19T14:23:58.901648Z","shell.execute_reply.started":"2023-04-19T14:19:47.737107Z","shell.execute_reply":"2023-04-19T14:23:58.900325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc, os, random, math\nimport numpy as np\nfrom tqdm.notebook import tqdm\nfrom functools import partial\n\nfrom data_test import *\nfrom encoders import EncoderWithDirectionReconstructionV22,EncoderWithDirectionReconstructionV23\nfrom baselineV3_SE_globalrel_d32_2 import DeepIceModel as TransformerV3_2\nfrom baselineV4_SE_globalrel_d32_0 import DeepIceModel as TransformerV4_0","metadata":{"execution":{"iopub.status.busy":"2023-04-19T14:30:40.143763Z","iopub.execute_input":"2023-04-19T14:30:40.144136Z","iopub.status.idle":"2023-04-19T14:30:40.213338Z","shell.execute_reply.started":"2023-04-19T14:30:40.144088Z","shell.execute_reply":"2023-04-19T14:30:40.212387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODELS = [\n    #['/kaggle/input/icecube-models/baselineV3_BE_globalrel_d32_0_6ema.pth',\n    # partial(TransformerV3_2, dim=768, dim_base=192, depth=12, head_size=32), 0.06719298],\n    #['/kaggle/input/exp-24-ft-md/baselineV3_BE_globalrel_d64_0_3emaFT_2.pth',  \n    # partial(TransformerV3_2, dim=768, dim_base=192, depth=12, head_size=64), 0.12039146],\n    #['/kaggle/input/icecube-models/baselineV4_BE_globalrelgnn_d48_0_3ema.pth',  \n    # partial(TransformerV4_0, dim=768, dim_base=192, depth=12, head_size=48), 0.1430797],\n    #['/kaggle/input/exp-24-ft-md/VFTV3_4RELFT_7.pth',  \n    # partial(TransformerV3_2, dim=768, dim_base=192, depth=12, head_size=32, n_rel=4), 0.15814836],\n    #['/kaggle/input/exp-24-ft-md/V22FT6_1.pth',\n    # partial(EncoderWithDirectionReconstructionV22, dim=384, dim_base=128, depth=8, head_size=32),\n    # 0.19663629],\n    #['/kaggle/input/exp-24-ft-md/V23FT5_6.pth',\n    # partial(EncoderWithDirectionReconstructionV23, dim=768, dim_base=192, depth=12, head_size=64),\n    # 0.31455123],\n    \n    ['/kaggle/input/exp-24-ft-md/V23FT5_6.pth',\n     partial(EncoderWithDirectionReconstructionV23, dim=768, dim_base=192, depth=12, head_size=64),\n     0.31455123],\n]\n\nPATH = '/kaggle/input/icecube-neutrinos-in-deep-ice/'\nICE_PROPERTIES = '/kaggle/input/install-pkgs/'\n\nNUM_WORKERS = 2\nbs = 16 #64\nL = 768 #512\nSEED = 2023\n\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n\nseed_everything(SEED)\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2023-04-19T14:30:49.993967Z","iopub.execute_input":"2023-04-19T14:30:49.994350Z","iopub.status.idle":"2023-04-19T14:30:50.006196Z","shell.execute_reply.started":"2023-04-19T14:30:49.994319Z","shell.execute_reply":"2023-04-19T14:30:50.004338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = IceCubeDataset(PATH,ICE_PROPERTIES,L=L)\nlen_sampler = LenMatchBatchSampler(torch.utils.data.SequentialSampler(ds),batch_size=bs, drop_last=False)\ndl = DataLoader(ds, batch_sampler=len_sampler, num_workers=0)\n\nmodels,weights = [],[]\nfor path,Model,w in MODELS:\n    print('loading:',path)\n    model = Model()\n    model.load_state_dict(torch.load(path,map_location=torch.device('cpu')))\n    model.eval()\n    model.to(device)\n    models.append(model)\n    weights.append(w)\nweights = torch.FloatTensor(weights)\nweights /= weights.sum()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-04-19T14:30:51.068720Z","iopub.execute_input":"2023-04-19T14:30:51.069190Z","iopub.status.idle":"2023-04-19T14:31:02.605344Z","shell.execute_reply.started":"2023-04-19T14:30:51.069116Z","shell.execute_reply":"2023-04-19T14:31:02.604251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\nfor x in tqdm(dl):\n    with torch.no_grad():\n        with torch.cuda.amp.autocast(enabled=True):\n            x = dict_to(x,device)\n            p = (torch.stack([torch.nan_to_num(model(x)).clip(-1000,1000) \n                              for model in models],-1).cpu()*weights).sum(-1)\n    p = get_val(p).numpy()\n    for pi,idx in zip(p,x['idx']):\n        preds.append({'event_id':idx.cpu().item(), 'azimuth':pi[0], 'zenith':pi[1]})\n        \ndf = pd.read_parquet(os.path.join(PATH,'sample_submission.parquet'))\ndf = pd.merge(df['event_id'], pd.DataFrame(preds), on='event_id', how='left').fillna(value=0)\ndf.to_csv('submission.csv',index=False)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-19T14:31:02.607405Z","iopub.execute_input":"2023-04-19T14:31:02.607750Z","iopub.status.idle":"2023-04-19T14:31:03.054134Z","shell.execute_reply.started":"2023-04-19T14:31:02.607714Z","shell.execute_reply":"2023-04-19T14:31:03.052982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}