{"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":"raw","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install polars\nfrom pathlib import Path\nfrom timeit import default_timer as timer\nimport pandas as pd\nimport polars as pl\nimport torch\nfrom torch import nn\nfrom torch.utils.data import Dataset, DataLoader\nimport gc\nfrom IPython.display import display, HTML\nfrom tqdm.autonotebook import tqdm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta = pd.read_parquet('/kaggle/input/icecube-neutrinos-in-deep-ice/train_meta.parquet')\ntrain_meta_batch_1=train_meta[train_meta[\"batch_id\"]==1]\ntrain_meta_batch_1.to_parquet('batch_1_meta.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:14:02.166411Z","iopub.execute_input":"2023-03-18T03:14:02.167089Z","iopub.status.idle":"2023-03-18T03:14:37.301508Z","shell.execute_reply.started":"2023-03-18T03:14:02.167020Z","shell.execute_reply":"2023-03-18T03:14:37.299455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass EventsDataset(Dataset):\n    def __init__(self,split,read_lib):\n        start=timer()\n        self.data_path=Path('/kaggle/input/icecube-neutrinos-in-deep-ice')\n        if split=='train':\n            self.path=self.data_path / 'train'\n            self.meta_path=self.data_path/ 'train_meta.parquet'\n        elif split=='test':\n            self.path=self.data_path /'test'\n            self.meta_path=self.data_path/ 'test_meta.parquet'\n        \n        self.sensor_geometry_path= self.data_path / \"sensor_geometry.csv\"\n        self.unsorted_data_files=list(self.path.glob('*.parquet'))\n        self.sorted_data_files=sorted(self.unsorted_data_files)\n        self.number_of_batches=len(self.sorted_data_files)\n        self.current_parquetframe=None\n        self.currentbatch=0\n        \n        if read_lib==\"pandas\":\n            self.read_lib=pd  \n        elif read_lib==\"polars\":\n            self.read_lib=pl\n            \n        end=timer()\n        print(f\" Initializing the paths took {end-start} seconds\")\n        start=timer()\n        self.sensorgeometry=self.read_lib.read_csv(self.sensor_geometry_path)\n        #self.metadata= self.read_lib.read_parquet(self.meta_path)\n        self.metadata=self.read_lib.read_parquet('/kaggle/working/batch_1_meta.parquet')\n        self.metadata.sort(\"event_id\")\n        #print(self.metadata.head)\n        end=timer()\n        print(f\" Loading of {split} metadata took {end-start} seconds\")\n        \n        self.number_of_events=self.metadata.shape[0]\n        #print(f\"number of events {self.number_of_events}\")\n        \n        \n        #start=timer()\n        \n        \n        #if read_lib==\"pandas\":\n            #self.events_per_batch=self.metadata[\"batch_id\"].value_counts()\n            #self.different_event_counts=self.events_per_batch[\"counts\"].value_counts()\n\n        #elif read_lib == \"polars\":\n            #self.events_per_batch=self.metadata[\"batch_id\"].value_counts()\n            #self.different_event_counts=self.events_per_batch[\"counts\"].value_counts()\n            \n        #display(HTML(self.different_event_counts.to_html()))\n        #print(self.events_per_batch)\n        #display(self.events_per_batch)\n            \n        #end=timer()\n        \n        #print(f\" Loading the number of events array took {end-start} seconds\")\n        \n            \n            \n        \n        \n    def __len__(self):\n        return self.number_of_events\n    \n    def __getitem__(self,index):\n        start=timer()\n        metarow = self.metadata[index,:]\n        batch_number=metarow[\"batch_id\"].item()\n        event_number=metarow[\"event_id\"].item()\n        azimuth=metarow[\"azimuth\"].item()\n        zenith=metarow[\"zenith\"].item()\n        file_name= \"batch_\"+str(batch_number)+\".parquet\"\n        file_path=self.path / file_name\n        if (self.currentbatch==0 or self.currentbatch != batch_number):\n            self.currentbatch=int(batch_number)\n            self.current_parquetframe=self.read_lib.read_parquet(file_path)\n            \n        event_frame = self.current_parquetframe.filter(self.read_lib.col(\"event_id\")== event_number)\n        event_frame=event_frame.with_columns(self.read_lib.col(\"sensor_id\").cast(pl.Int64, strict=False))\n        merged_event_frame=event_frame.join(self.sensorgeometry, how=\"inner\", on=\"sensor_id\")\n        train_x=merged_event_frame[[\"time\",\"charge\",\"x\",\"y\",\"z\"]]\n        train_x=train_x.to_numpy()\n        train_x=torch.from_numpy(train_x)\n        train_y=torch.tensor([azimuth,zenith])\n        #train_x=train_x.sort_values(\"time\")\n        #print(train_x)\n        #eventframe=self.current_parquetframe[self.current_parquetframe[\"event_id\"]==event_number]\n        #print(event_frame)\n        #print(self.sensorgeometry)\n        end=timer()\n        #print(f\"Loading the event frame took {end-start} seconds\")\n        \n        \n        \n            \n            \n        return (train_x,train_y)\n        \n    \n    \n    \n            \n            \n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:14:46.765602Z","iopub.execute_input":"2023-03-18T03:14:46.766051Z","iopub.status.idle":"2023-03-18T03:14:46.783083Z","shell.execute_reply.started":"2023-03-18T03:14:46.765996Z","shell.execute_reply":"2023-03-18T03:14:46.781568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset=EventsDataset(\"train\", \"polars\")","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:19:52.661693Z","iopub.execute_input":"2023-03-18T03:19:52.662204Z","iopub.status.idle":"2023-03-18T03:19:52.709982Z","shell.execute_reply.started":"2023-03-18T03:19:52.662157Z","shell.execute_reply":"2023-03-18T03:19:52.708766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x,train_y=train_dataset[2000]","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:19:55.224838Z","iopub.execute_input":"2023-03-18T03:19:55.225231Z","iopub.status.idle":"2023-03-18T03:19:58.552873Z","shell.execute_reply.started":"2023-03-18T03:19:55.225199Z","shell.execute_reply":"2023-03-18T03:19:58.551888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x=event_frame.to_numpy()\nprint(train_x.shape)\nmytensor=torch.from_numpy(train_x)\nprint(mytensor.dtype)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:19:46.666294Z","iopub.execute_input":"2023-03-18T03:19:46.666725Z","iopub.status.idle":"2023-03-18T03:19:46.774435Z","shell.execute_reply.started":"2023-03-18T03:19:46.666686Z","shell.execute_reply":"2023-03-18T03:19:46.772476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(event_frame)","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:23:14.232123Z","iopub.execute_input":"2023-03-11T16:23:14.232600Z","iopub.status.idle":"2023-03-11T16:23:14.245853Z","shell.execute_reply.started":"2023-03-11T16:23:14.232556Z","shell.execute_reply":"2023-03-11T16:23:14.244384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader= DataLoader(train_dataset, batch_size=1, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:15:06.289427Z","iopub.execute_input":"2023-03-18T03:15:06.289812Z","iopub.status.idle":"2023-03-18T03:15:06.296771Z","shell.execute_reply.started":"2023-03-18T03:15:06.289781Z","shell.execute_reply":"2023-03-18T03:15:06.294870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=next(iter(train_dataloader))","metadata":{"execution":{"iopub.status.busy":"2023-03-12T16:41:49.772774Z","iopub.execute_input":"2023-03-12T16:41:49.773297Z","iopub.status.idle":"2023-03-12T16:41:49.863148Z","shell.execute_reply.started":"2023-03-12T16:41:49.773263Z","shell.execute_reply":"2023-03-12T16:41:49.861744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make a model","metadata":{}},{"cell_type":"code","source":"linear_layer=nn.Linear(in_features=5,out_features=512,dtype=torch.float64)\nencoder_layer=nn.TransformerEncoderLayer(d_model=512,nhead=8, dtype=torch.float64)\ntransformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=6)","metadata":{"execution":{"iopub.status.busy":"2023-03-11T17:07:35.213112Z","iopub.execute_input":"2023-03-11T17:07:35.213566Z","iopub.status.idle":"2023-03-11T17:07:35.352100Z","shell.execute_reply.started":"2023-03-11T17:07:35.213499Z","shell.execute_reply":"2023-03-11T17:07:35.350986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"linear_layer=nn.LazyLinear(out_features=512,dtype=torch.float64)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:26:22.148468Z","iopub.execute_input":"2023-03-18T03:26:22.148905Z","iopub.status.idle":"2023-03-18T03:26:22.156671Z","shell.execute_reply.started":"2023-03-18T03:26:22.148867Z","shell.execute_reply":"2023-03-18T03:26:22.154691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"addRepresentationToken(train_x)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:26:24.394176Z","iopub.execute_input":"2023-03-18T03:26:24.395312Z","iopub.status.idle":"2023-03-18T03:26:24.426464Z","shell.execute_reply.started":"2023-03-18T03:26:24.395264Z","shell.execute_reply":"2023-03-18T03:26:24.425245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyModel (nn.Module):\n    def __init__(self,\n                 dmodel: int, \n                 num_layers: int,\n                 encoder_layer: nn.TransformerEncoderLayer\n                 ):\n        super().__init__()\n        \n        self.input_embedding=nn.Linear(in_features=5, \n                                       out_features=dmodel, \n                                       dtype=torch.float64)\n    \n        self.transformer_encoder=nn.TransformerEncoder(encoder_layer=encoder_layer, \n                                                       num_layers=num_layers)\n        \n        self.output_linear=nn.Linear(in_features=dmodel, \n                                     out_features=5, \n                                     dtype=torch.float64)\n        self.anglemaker=nn.Linear(in_features=5, \n                                  out_features=2, \n                                  dtype=torch.float64)\n        \n    def addRepresentationToken(self,trainx):\n        reptoken=torch.zeros((1,1,5), dtype=torch.float64)\n        #print(reptoken.shape)\n        #print(trainx.shape)\n        trainx=torch.cat((reptoken, trainx),dim=1)\n        return trainx\n        #print(trainx)\n        \n        \n    def forward(self, x):\n        x=self.addRepresentationToken(x)\n        #print(x.shape)\n        x=self.input_embedding(x)\n        x=self.transformer_encoder(x)\n        x=self.output_linear(x)\n        x=x[0,0,:]\n        x=self.anglemaker(x)\n        x=torch.sigmoid(x)\n        \n        \n        return(x)\n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:45:20.632175Z","iopub.execute_input":"2023-03-18T03:45:20.632602Z","iopub.status.idle":"2023-03-18T03:45:20.643962Z","shell.execute_reply.started":"2023-03-18T03:45:20.632562Z","shell.execute_reply":"2023-03-18T03:45:20.642611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train","metadata":{}},{"cell_type":"code","source":"encoder_layer=nn.TransformerEncoderLayer(d_model=512,nhead=8, dtype=torch.float64)\nmodel=MyModel(dmodel=512, \n              encoder_layer=encoder_layer, \n              num_layers=6)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:45:25.885069Z","iopub.execute_input":"2023-03-18T03:45:25.885445Z","iopub.status.idle":"2023-03-18T03:45:25.950117Z","shell.execute_reply.started":"2023-03-18T03:45:25.885409Z","shell.execute_reply":"2023-03-18T03:45:25.948591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.train()\n\nfor i,(trainx, trainy) in tqdm(enumerate(train_dataloader)):\n    output=model(trainx)\n    #print(output)\n    logits=output.detach()\n    pred_azimuth=(logits[0]*360)\n    pred_zenith=(logits[1]*180)\n    pred_output=torch.tensor([pred_zimuth, pred_zenith])\n    \n    print(logits[0])\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:46:02.399174Z","iopub.execute_input":"2023-03-18T03:46:02.399548Z","iopub.status.idle":"2023-03-18T03:46:04.175800Z","shell.execute_reply.started":"2023-03-18T03:46:02.399519Z","shell.execute_reply":"2023-03-18T03:46:04.174331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lin_output=linear_layer(mytensor)\nprint(lin_outpu\n","metadata":{"execution":{"iopub.status.busy":"2023-03-11T17:07:37.018185Z","iopub.execute_input":"2023-03-11T17:07:37.018614Z","iopub.status.idle":"2023-03-11T17:07:37.025962Z","shell.execute_reply.started":"2023-03-11T17:07:37.018577Z","shell.execute_reply":"2023-03-11T17:07:37.024565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder_output=transformer_encoder(lin_output)\nprint(encoder_output.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-11T17:07:39.845440Z","iopub.execute_input":"2023-03-11T17:07:39.846130Z","iopub.status.idle":"2023-03-11T17:07:39.982774Z","shell.execute_reply.started":"2023-03-11T17:07:39.846089Z","shell.execute_reply":"2023-03-11T17:07:39.981308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src=torch.rand(10,32,512)\nprint(src.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-11T16:40:23.865208Z","iopub.execute_input":"2023-03-11T16:40:23.865652Z","iopub.status.idle":"2023-03-11T16:40:23.907243Z","shell.execute_reply.started":"2023-03-11T16:40:23.865614Z","shell.execute_reply":"2023-03-11T16:40:23.905970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor_events_frame=torch.tensor(event_frame['x'].values)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output=myModel(query=event_frame, key=event_frame, value=event_frame)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntensor1=torch.zeros(1,1,5)\ntensor2=torch.zeros(1,79,5)\ntensor3=torch.cat((tensor1,tensor2),dim=1)\nprint(tensor3.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-18T03:35:56.175383Z","iopub.execute_input":"2023-03-18T03:35:56.175852Z","iopub.status.idle":"2023-03-18T03:35:56.184636Z","shell.execute_reply.started":"2023-03-18T03:35:56.175811Z","shell.execute_reply":"2023-03-18T03:35:56.182668Z"},"trusted":true},"execution_count":null,"outputs":[]}]}