{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","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 torch\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport typing\nimport PIL\nfrom torchvision.transforms.functional import to_pil_image #Importa solo la función de interés, no la librería completa\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        \ndirectory=\"/kaggle/input/hms-harmful-brain-activity-classification/\"\nspectrogram_directory=directory+\"train_spectrograms/\"\nspectrogram:pd.DataFrame=pd.read_parquet(spectrogram_directory+\"1000086677.parquet\")\nprint(spectrogram.columns)\ntensor=torch.tensor(spectrogram.values)[:,1:]\nprint(tensor.shape)\nimage=to_pil_image(tensor)\nimage\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","execution":{"iopub.status.busy":"2024-07-05T15:32:16.077736Z","iopub.execute_input":"2024-07-05T15:32:16.078578Z","iopub.status.idle":"2024-07-05T15:32:16.151987Z","shell.execute_reply.started":"2024-07-05T15:32:16.078530Z","shell.execute_reply":"2024-07-05T15:32:16.150887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv(directory+\"train.csv\")\nprint(data.info())","metadata":{"execution":{"iopub.status.busy":"2024-07-05T15:32:16.154640Z","iopub.execute_input":"2024-07-05T15:32:16.155604Z","iopub.status.idle":"2024-07-05T15:32:16.380250Z","shell.execute_reply.started":"2024-07-05T15:32:16.155562Z","shell.execute_reply":"2024-07-05T15:32:16.378944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset\nclass SpectrogramDataset(Dataset):\n    def __init__(self):\n        self.directory=\"/kaggle/input/hms-harmful-brain-activity-classification/\"\n        self.spectrogram_directory=directory+\"train_spectrograms/\" \n        self.data=pd.read_csv(directory+\"train.csv\")\n    \n    def __len__(self):\n        return len(self.data)\n    \n    def create_vote_tensor(self,row):\n        vote_list=pd.to_numeric(row[[\"seizure_vote\",\"lpd_vote\",\"gpd_vote\",\"lrda_vote\",\"grda_vote\",\"other_vote\"]])\n        vote_tensor=torch.tensor(vote_list.values)\n        total=torch.sum(vote_tensor)\n        prob=vote_tensor/total\n        return prob\n       \n        \n    def create_spectrogram_tensor(self,row):\n        spectrogram_id=row[\"spectrogram_id\"]\n        start_time=row[\"spectrogram_label_offset_seconds\"]\n        end_time=start_time+600\n        spectrogram:pd.DataFrame=pd.read_parquet(\n            spectrogram_directory+f\"{spectrogram_id}.parquet\"\n        )\n        spectrogram=spectrogram[(spectrogram[\"time\"]>=start_time)&(spectrogram[\"time\"]<=end_time)]\n        tensor=torch.tensor(spectrogram.values)[:,1:]\n        tensor=torch.nan_to_num(tensor)\n        return tensor[:300]\n        \n\n        \n    def __getitem__(self, idx):\n        row=self.data.iloc[idx]\n        #print(self.create_vote_tensor(row))\n        return self.create_spectrogram_tensor(row),self.create_vote_tensor(row)\n        \n\n\n#NOTES\n#__len__ define: len(dataset)\n#__get__ define: dataset[0]\n#self.data.iloc is essentially a list of the rows of self.data\n#\"iloc\" means index location\n#row[\"spectrogram_id\"], of for multiple columns\n#row[[\"spectrogram_id\",\"spectrogram_label_offset_seconds\"]]\n\n#ACTIVITY\n#Create a variable \"start_time\" which has the value of\n#spectrogram_label_offset_seconds\n#And another variable which is a list of the votes\n#(seizure,vote,lpd_vote,...)\n\n#Finish this by returning the probabilities\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-05T15:32:16.382170Z","iopub.execute_input":"2024-07-05T15:32:16.382713Z","iopub.status.idle":"2024-07-05T15:32:16.396683Z","shell.execute_reply.started":"2024-07-05T15:32:16.382621Z","shell.execute_reply":"2024-07-05T15:32:16.395269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"dataset=SpectrogramDataset()\nfor spectrogram,votes in dataset:\n    print(spectrogram.shape)\n    print(votes)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-07-05T15:32:16.397974Z","iopub.execute_input":"2024-07-05T15:32:16.398413Z","iopub.status.idle":"2024-07-05T15:32:16.413778Z","shell.execute_reply.started":"2024-07-05T15:32:16.398357Z","shell.execute_reply":"2024-07-05T15:32:16.412433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.nn import Module\nclass Model(Module):\n    def __init__(self):\n        super().__init__()\n        self.layer=torch.nn.Linear(300*400,6)\n        self.dtype=self.layer.weight.dtype\n        \n    def forward(self,spectrogram):\n        shape=spectrogram.shape\n        batch_size=shape[0]\n        spectrogram=spectrogram.reshape((batch_size,300*400))\n        spectrogram=spectrogram.to(dtype=self.dtype)\n        spectrogram=self.layer(spectrogram)\n        return spectrogram","metadata":{"execution":{"iopub.status.busy":"2024-07-05T15:32:16.417337Z","iopub.execute_input":"2024-07-05T15:32:16.417793Z","iopub.status.idle":"2024-07-05T15:32:16.427512Z","shell.execute_reply.started":"2024-07-05T15:32:16.417751Z","shell.execute_reply":"2024-07-05T15:32:16.426126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Trainer:\n    def __init__(self,model,num_epochs,dataloader,loss,optimizer):\n        self.model=model\n        self.num_epochs=num_epochs\n        self.dataloader=dataloader\n        self.loss=loss\n        self.optimizer=optimizer\n        \n    def training_loop(self):\n        for epoch in range(self.num_epochs):\n            for spectrogram, votes in self.dataloader:\n                self.training_step(spectrogram,votes)\n                \n    def training_step(self,spectrogram,votes):\n        self.optimizer.zero_grad()\n        prediction=self.model.forward(spectrogram)\n        loss=self.loss(prediction,votes)\n        loss.backward()\n        self.optimizer.step()\n        print(loss)","metadata":{"execution":{"iopub.status.busy":"2024-07-05T15:32:16.428634Z","iopub.execute_input":"2024-07-05T15:32:16.429322Z","iopub.status.idle":"2024-07-05T15:32:16.442257Z","shell.execute_reply.started":"2024-07-05T15:32:16.429278Z","shell.execute_reply":"2024-07-05T15:32:16.440996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\nmodel=Model()\nnum_epochs=1\ndataset=SpectrogramDataset()\nbatch_size=10\ndataloader=DataLoader(dataset,batch_size,shuffle=True)\nloss=torch.nn.CrossEntropyLoss(torch.tensor([1,1,1,1,1,1]))\noptimizer=torch.optim.Adam(model.parameters(),lr=.001)\ntrainer=Trainer(\n    model,\n    num_epochs,\n    dataloader,\n    loss,\n    optimizer\n)\ntrainer.training_loop()","metadata":{"execution":{"iopub.status.busy":"2024-07-05T15:36:54.049996Z","iopub.execute_input":"2024-07-05T15:36:54.051059Z","iopub.status.idle":"2024-07-05T15:37:12.002446Z","shell.execute_reply.started":"2024-07-05T15:36:54.051020Z","shell.execute_reply":"2024-07-05T15:37:12.000622Z"},"trusted":true},"execution_count":null,"outputs":[]}]}