{"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 nibabel","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-13T04:48:03.943474Z","iopub.execute_input":"2023-06-13T04:48:03.944056Z","iopub.status.idle":"2023-06-13T04:48:39.115357Z","shell.execute_reply.started":"2023-06-13T04:48:03.944023Z","shell.execute_reply":"2023-06-13T04:48:39.114085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader, Dataset\nimport nibabel as nib\nimport pandas as pd\nimport os\nfrom sklearn.model_selection import KFold\nimport torchvision.transforms as transforms","metadata":{"execution":{"iopub.status.busy":"2023-06-13T04:48:39.117443Z","iopub.execute_input":"2023-06-13T04:48:39.117914Z","iopub.status.idle":"2023-06-13T04:48:46.206562Z","shell.execute_reply.started":"2023-06-13T04:48:39.117877Z","shell.execute_reply":"2023-06-13T04:48:46.205652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the 3D ViT model\nclass ViTModel(nn.Module):\n    def __init__(self, embedding_dim, num_classes):\n        super(ViTModel, self).__init__()\n        self.transformer = nn.TransformerEncoder(nn.TransformerEncoderLayer(d_model=embedding_dim, nhead=5), num_layers=1)\n        self.fc = nn.Linear(embedding_dim, num_classes)\n        self.patch_size = 16\n    def forward(self, x):\n        # Assuming `x` has shape (batch_size, channels, height, width)\n        batch_size, channels, height, width = x.shape\n       \n        # Calculate the number of patches\n        num_patches = (height // self.patch_size) * (width // self.patch_size)\n       \n        # Reshape the query tensor\n        x = x.view(batch_size, num_patches, channels, self.patch_size, self.patch_size)\n        x = x.permute(0, 1, 3, 4, 2)  # Rearrange dimensions\n\n        # Flatten the spatial dimensions\n        x = x.reshape(batch_size * num_patches, self.patch_size * self.patch_size, channels)\n\n        # Forward pass through the transformer\n        x = self.transformer(x)\n\n        # Reshape back to the original format\n        x = x.reshape(batch_size, num_patches, self.patch_size, self.patch_size, channels)\n        x = x.permute(0, 1, 4, 2, 3)  # Rearrange dimensions\n        x = x.reshape(batch_size * num_patches, channels, self.patch_size, self.patch_size)\n\n        # Apply the fully connected layer\n        x = x.mean(dim=(2, 3))  # Average pooling over patch dimensions\n        x = self.fc(x)\n        return x\n   \n\n# Define the custom dataset class for NII images\nclass NiiDataset(Dataset):\n    def __init__(self, file_paths, labels, transform=None):\n        self.file_paths = file_paths\n        self.labels = labels\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.file_paths)\n   \n    def __getitem__(self, index):\n        #index =\n        file_path = self.file_paths[index]\n        start_index = file_path.find(\"_\") + 1\n        end_index = file_path.rfind(\"_\")\n\n        p_id = file_path[start_index:end_index]\n        #print(p_id)\n        p_id_int = int(p_id)\n\n        #print(p_id_int)\n        csv_file = '/kaggle/input/train-csv/train_labels (2).csv'\n        search_column = 'BraTS21ID'\n        search_value = int(p_id)\n\n        df = pd.read_csv(csv_file)\n        matching_rows = df.loc[df[search_column] == search_value]\n\n        if not matching_rows.empty:\n            row_index = matching_rows.index[0]  # Get the index of the first matching row\n            #print(f\"Row index corresponding to '{search_value}': {row_index}\")\n            column_name = 'MGMT_value'\n            label = df.iloc[row_index][column_name]\n\n           \n        else:\n            print(f\"No matching row found for '{search_value}'.\")\n        #label = self.labels[p_id]\n\n        # Load the NII image\n        image = nib.load(file_path).get_fdata()\n\n        # Apply transformations if specified\n        if self.transform:\n            image = self.transform(image)\n       \n        # Convert the label to a tensor\n        label = torch.tensor(label, requires_grad=False)\n       # print(  index)\n        #print(  file_path)\n        #print( label)\n        return image, label\n\n# Set the device (CPU or GPU)\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# Define the hyperparameters\nembedding_dim = 155  # Input size for the ViT model\nnum_classes = 2  # Number of output classes\nnum_epochs = 3\nbatch_size = 2\npatch_size = 16\nlearning_rate = 0.001\n\n# Read the CSV file\ndf = pd.read_csv(\"/kaggle/input/t2-filepath-mgmt/t2_filepath_mgmt.csv\")\n\n# Specify the column name from the CSV file\ncolumn_name = 'MGMT_value'\n\n# Convert the column to a list\ncolumn_list = df[column_name].tolist()\n\n# Define data paths and labels\ndata_dir = '/kaggle/input/t2-images'  # Replace with the path to your data directory containing NII files\nlabels = column_list  # Replace with the labels for your dataset\n\nlabels = torch.LongTensor(labels)\n\n#print(labels.shape)\n# Define data transformations\ndata_transform = transforms.Compose([\n    transforms.ToTensor()\n])\n\n# Create the dataset\nfile_paths = [os.path.join(data_dir, file) for file in os.listdir(data_dir)]\ndataset = NiiDataset(file_paths, labels, transform=data_transform)\n\n# Create the k-fold splits\nk = 2\n\n# Create the k-fold cross-validation\nkf = KFold(n_splits=k, shuffle=True)\n\n# Track best weights and corresponding metrics\nbest_metrics = []\nbest_weights = []\n\n#checkpoint_dir = '/kaggle/working/checkpoints'\n#os.makedirs(checkpoint_dir, exist_ok=True)\n\n# Specify the path to the checkpoint weights file\ncheckpoint_path = '/kaggle/input/trained-weights-3folds-2epochs/vit_3D_best_weights.pth'\n\n# Create the ViT model\nmodel = ViTModel(embedding_dim, num_classes).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T04:48:48.938795Z","iopub.execute_input":"2023-06-13T04:48:48.939428Z","iopub.status.idle":"2023-06-13T04:48:53.792231Z","shell.execute_reply.started":"2023-06-13T04:48:48.939392Z","shell.execute_reply":"2023-06-13T04:48:53.791251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport csv\nimport pandas as pd\n\nlist_ =[]\n\n# Define the custom dataset class for NII images\nclass NiiDataset_Test(Dataset):\n    def __init__(self, file_paths, labels, transform=None):\n        self.file_paths = file_paths\n        self.labels = labels\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.file_paths)\n\n   \n    def __getitem__(self, index):\n        #index =\n        file_path = self.file_paths[index]\n        \n# Extract p_id from the file name\n        start_index = 37\n        end_index = 42\n        global p_id\n        p_id = file_path[start_index:end_index]\n       # print(p_id)\n\n        \n\n# ...\n\n\n        #print(p_id_int)\n        csv_file = '/kaggle/input/sample-submission/sample_submission (1).csv'\n        search_column = 'BraTS21ID'\n        \n        search_value = int(p_id)\n        \n        list_.append(p_id)\n\n        df = pd.read_csv(csv_file)\n        matching_rows = df.loc[df[search_column] == search_value]\n\n        if not matching_rows.empty:\n            row_index = matching_rows.index[0]  # Get the index of the first matching row\n            #print(f\"Row index corresponding to '{search_value}': {row_index}\")\n            column_name = 'MGMT_value'\n            label = df.iloc[row_index][column_name]\n        else:\n            print(f\"No matching row found for '{search_value}'.\")\n        #label = self.labels[p_id]\n\n        # Load the NII image\n        image = nib.load(file_path).get_fdata()\n\n        # Apply transformations if specified\n        if self.transform:\n            image = self.transform(image)\n       \n        # Convert the label to a tensor\n        label = torch.tensor(label, requires_grad=False)\n       # print(  index)\n        #print(  file_path)\n        #print( label)\n        return image, label\n   \n# Load the best weights for the selected fold\nbest_weights_fold = torch.load('/kaggle/input/trained-weights-3folds-2epochs/vit_3D_best_weights.pth')\nmodel.load_state_dict(best_weights_fold)\n\n# Define data paths and labels\ndata_dir = '/kaggle/input/t2-test-mgmt'  # Replace with the path to your data directory containing NII files\n#labels = column_list  # Replace with the labels for your dataset\n\nfile_paths = [os.path.join(data_dir, file) for file in os.listdir(data_dir)]\nlabels = [0] * 80\ntest_dataset = NiiDataset_Test(file_paths, labels, transform=data_transform)\n\n# Create a data loader for the test dataset\ntest_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)\n\n# Set the model in evaluation mode\nmodel.eval()\n\n# List to store the predicted labels\npredicted_labels = []\n\n# Disable gradient computation\nwith torch.no_grad():\n    for images, labels in test_loader:\n        images = images.to(device)\n        #print(images.size(0))\n        # Forward pass\n        outputs = model(images.float())\n        #print(outputs)\n       \n        # Reshape to (batch_size * num_patches, num_classes)\n        outputs = outputs.reshape(1, 225, num_classes)\n        outputs = outputs.mean(dim=1)\n       \n        outputs = outputs.sigmoid()\n        #print(outputs)\n        predicted_classes,_ = torch.max(outputs, dim=1)\n       \n        predicted_labels.extend(predicted_classes.cpu().numpy().tolist())\n\n# Convert the predicted labels to a numpy array\n#predicted_labels = np.array(predicted_labels)\n\n# Print the predicted labelsA\n#print(predicted_labels)\nprint(\".................................\")\n\ndata = list(zip(predicted_labels))\nprint(data)\n\n# Define the CSV file path\ncsv_file_path = \"/kaggle/working/submission.csv\"\n\n\n# Define the data for the columns\nBraTS21ID = list_\nMGMT_value = predicted_labels \n\n\n# Combine the data into rows\nrows = zip(BraTS21ID, MGMT_value)\n\n# Define the CSV file path\ncsv_file_path = '/kaggle/working/submission.csv'  # Replace with the path where you want to save the CSV file\n\ndata = {'BraTS21ID': BraTS21ID, 'MGMT_value': predicted_labels}\nsubmission_df = pd.DataFrame(data)\nprint(submission_df)\n# Save the updated DataFrame to a new CSV file\n#output_file_path_2 = '/kaggle/working/submission_1.csv'\n#submission_df.to_csv(output_file_path_2, index=False)\n#print(merged_df)\n\n# Read the first CSV file\ncsv_file_path1 = '/kaggle/input/sample-submission/sample_submission (1).csv'\ndf1 = pd.read_csv(csv_file_path1)\nprint(df1)\n\n\n# Read the second CSV file\ncsv_file_path2 = '/kaggle/input/submission-80/submission_1 (1).csv'\ndf2 = pd.read_csv(csv_file_path2)\nprint(df2)\n\n# Merge the two DataFrames based on a common BraTS21ID BraTS21ID MGMT_value\nmerged_df = pd.merge(df1, df2, on='BraTS21ID', how='left')\n\nprint(merged_df)\n\n# Update values in the desired column based on values from another column\nmerged_df['MGMT_value_y'] = merged_df['MGMT_value_y'].fillna(0.5)\nmerged_df['MGMT_value_y'] = merged_df.apply(lambda row: row['BraTS21ID'] if pd.isnull(row['MGMT_value_y']) else row['MGMT_value_y'], axis=1)\n#merged_df['BraTS21ID'] = merged_df.apply(lambda row: row['BraTS21ID'] if pd.isnull(row['BraTS21ID']) else row['BraTS21ID'], axis=1)\n\n#merged_df.drop([1])\nmerged_df = merged_df.drop('MGMT_value_x', axis=1)\n\nmerged_df = merged_df.rename(columns={'MGMT_value_y': 'MGMT_value'})\n\n#merged_df = pd.merge(merged_df, submission_df, on='BraTS21ID', how='left')\n#merged_df['BraTS21ID'] = merged_df.apply(lambda row: row['BraTS21ID'] if pd.isnull(row['BraTS21ID']) else row['BraTS21ID'], axis=1)\n#merged_df['BraTS21ID'] = merged_df['BraTS21ID'].astype(str).str.zfill(5)\n\n\n# Save the updated DataFrame to a new CSV file\noutput_file_path = '/kaggle/working/submission.csv'\nmerged_df.to_csv(output_file_path, index=False)\nprint(merged_df)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-13T05:16:03.699460Z","iopub.execute_input":"2023-06-13T05:16:03.699824Z","iopub.status.idle":"2023-06-13T05:16:22.643273Z","shell.execute_reply.started":"2023-06-13T05:16:03.699797Z","shell.execute_reply":"2023-06-13T05:16:22.642251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ncsv_file_path = '/kaggle/working/submission.csv'  # Replace with the path to your CSV file\n\n# Read the CSV file into a pandas DataFrame\ndf = pd.read_csv(csv_file_path)\n\n# Print the DataFrame\nprint(df)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-13T05:17:11.710589Z","iopub.execute_input":"2023-06-13T05:17:11.711000Z","iopub.status.idle":"2023-06-13T05:17:11.723628Z","shell.execute_reply.started":"2023-06-13T05:17:11.710971Z","shell.execute_reply":"2023-06-13T05:17:11.722188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}