{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":14313,"databundleVersionId":862019,"sourceType":"competition"},{"sourceId":593372,"sourceType":"datasetVersion","datasetId":288635}],"dockerImageVersionId":29188,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-05T05:42:53.118477Z","iopub.execute_input":"2024-11-05T05:42:53.118807Z","iopub.status.idle":"2024-11-05T05:42:53.123869Z","shell.execute_reply.started":"2024-11-05T05:42:53.118759Z","shell.execute_reply":"2024-11-05T05:42:53.123096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport numpy as np\n\n# check if CUDA is available\ntrain_on_gpu = torch.cuda.is_available()\n\nif not train_on_gpu:\n    print('CUDA is not available.  Training on CPU ...')\nelse:\n    print('CUDA is available!  Training on GPU ...')\n\n# set seed\ntorch.manual_seed(0)\ntorch.backends.cudnn.deterministic = True\ntorch.backends.cudnn.benchmark = False\nnp.random.seed(0)","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2024-11-05T05:42:53.674474Z","iopub.execute_input":"2024-11-05T05:42:53.674787Z","iopub.status.idle":"2024-11-05T05:42:53.682262Z","shell.execute_reply.started":"2024-11-05T05:42:53.674742Z","shell.execute_reply":"2024-11-05T05:42:53.681236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1. Dataloader\n source: https://pytorch.org/tutorials/beginner/data_loading_tutorial.html","metadata":{}},{"cell_type":"code","source":"import pickle\nimport os\nimport pandas as pd\nfrom torch.utils.data import Dataset, DataLoader\n\nclass CustomImageDataset(Dataset):\n   \n    def __init__(self, csv_file=None, num_rows=None, root_dir=None, transform=None):\n        self.df = pd.read_csv(root_dir + csv_file)\n        # randomly sample m rows\n        self.df = self.df.sample(num_rows)\n        self.root_dir = root_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        \n        img_name = os.path.join(\n            self.root_dir,\n            str(self.df.iloc[idx]['molecule_name']) + '_' + str(self.df.iloc[idx]['id']) + '.pkl'\n        )\n        \n        with open (img_name, 'rb') as fp:\n            image = pickle.load(fp)\n        \n        for c in range(5):\n            image[c] = np.clip(image[c], 0, 255) / 255\n        \n        img = torch.from_numpy(np.array(image))\n        img = img.type(torch.FloatTensor)\n        \n        sample = {'image': img,\n                  'target': self.df.iloc[idx]['scalar_coupling_constant']}\n\n        if self.transform:\n            sample['image'] = self.transform(sample['image'])\n\n        return sample['image'], sample['target']","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:54.611940Z","iopub.execute_input":"2024-11-05T05:42:54.612233Z","iopub.status.idle":"2024-11-05T05:42:54.625894Z","shell.execute_reply.started":"2024-11-05T05:42:54.612191Z","shell.execute_reply":"2024-11-05T05:42:54.624956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path =\"../input/full-images-1jhn/image_1jhn/Image_1JHN/\"\n\nimage_dataset = CustomImageDataset(\n    csv_file='description.csv',\n    num_rows=1000,\n    root_dir=images_path)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:55.043288Z","iopub.execute_input":"2024-11-05T05:42:55.043754Z","iopub.status.idle":"2024-11-05T05:42:55.126004Z","shell.execute_reply.started":"2024-11-05T05:42:55.043539Z","shell.execute_reply":"2024-11-05T05:42:55.125360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Cross validation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GroupKFold\ngroup_kfold = GroupKFold(n_splits=5)\n\n#df = pd.read_csv(images_path + 'description.csv', index_col = 0)\ndf = image_dataset.df.copy()\ndf.set_index('Unnamed: 0', inplace=True)\n# set index_col=0\n\ndf.reset_index(drop=True, inplace=True)\n\nX = df[['id', 'molecule_name']].copy()\ny = df['scalar_coupling_constant']\ngroups = df['molecule_name'].unique()\n\nfolds = []\nfor train_idx, valid_idx in group_kfold.split(X, y, X['molecule_name']):\n    folds.append([train_idx, valid_idx])","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:55.940418Z","iopub.execute_input":"2024-11-05T05:42:55.940753Z","iopub.status.idle":"2024-11-05T05:42:55.962378Z","shell.execute_reply.started":"2024-11-05T05:42:55.940702Z","shell.execute_reply":"2024-11-05T05:42:55.961710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_fold = 1","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:56.385544Z","iopub.execute_input":"2024-11-05T05:42:56.385897Z","iopub.status.idle":"2024-11-05T05:42:56.389438Z","shell.execute_reply.started":"2024-11-05T05:42:56.385847Z","shell.execute_reply":"2024-11-05T05:42:56.388596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_loaders(index_fold, train_data=None, images_path=None, folds=None):\n    # from torchvision import datasets\n    import torchvision.transforms as transforms\n    from torch.utils.data.sampler import SubsetRandomSampler\n\n    # number of subprocesses to use for data loading\n    num_workers = 4\n    # how many samples per batch to load\n    batch_size = 600\n\n    # convert data to a normalized torch.FloatTensor\n    transform = transforms.Compose([\n        transforms.Normalize((0.5, 0.5, 0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 0.5, 0.5))\n        ])\n    '''\n    train_data = CustomImageDataset(\n        csv_file='description.csv',\n        root_dir = images_path,\n    #     transform=transform\n    )'''\n\n    train_idx, valid_idx = folds[index_fold][0], folds[index_fold][1]\n\n    # define samplers for obtaining training and validation batches\n    train_sampler = SubsetRandomSampler(train_idx)\n    valid_sampler = SubsetRandomSampler(valid_idx)\n\n    # prepare data loaders (combine dataset and sampler)\n    train_loader = torch.utils.data.DataLoader(train_data,\n                                               batch_size=batch_size,\n                                               sampler=train_sampler,\n                                               num_workers=num_workers\n                                              )\n    valid_loader = torch.utils.data.DataLoader(train_data,\n                                               batch_size=batch_size, \n                                               sampler=valid_sampler,\n                                               num_workers=num_workers\n                                              )\n    return train_loader, valid_loader\n\ntrain_loader, valid_loader = get_loaders(index_fold, train_data=image_dataset, images_path=images_path, folds=folds)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:56.832755Z","iopub.execute_input":"2024-11-05T05:42:56.833062Z","iopub.status.idle":"2024-11-05T05:42:56.843650Z","shell.execute_reply.started":"2024-11-05T05:42:56.833021Z","shell.execute_reply":"2024-11-05T05:42:56.842665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNN(nn.Module):\n    \"\"\"CNN.\"\"\"\n\n    def __init__(self):\n        \"\"\"CNN Builder.\"\"\"\n        super(CNN, self).__init__()\n\n        self.conv_layer = nn.Sequential(\n\n            # Conv Layer block 1\n            nn.Conv2d(in_channels=5, out_channels=32, kernel_size=3, padding=1),\n            nn.BatchNorm2d(32),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=2, stride=2),\n\n            # Conv Layer block 2\n            nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),\n            nn.BatchNorm2d(128),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=2, stride=2),\n            nn.Dropout2d(p=0.05),\n\n            # Conv Layer block 3\n            nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),\n            nn.BatchNorm2d(256),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),\n            nn.ReLU(inplace=True),\n            nn.MaxPool2d(kernel_size=2, stride=2),\n        )\n\n        self.fc_layer = nn.Sequential(\n            nn.Dropout(p=0.3),\n            nn.Linear(4096, 1024),\n            nn.ReLU(inplace=True),\n            nn.Linear(1024, 512),\n            nn.ReLU(inplace=True),\n            nn.Dropout(p=0.3),\n            nn.Linear(512, 1)\n        )\n\n    def forward(self, x):\n        x = self.conv_layer(x)\n        x = x.view(x.size(0), -1)\n        x = self.fc_layer(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:57.356282Z","iopub.execute_input":"2024-11-05T05:42:57.356630Z","iopub.status.idle":"2024-11-05T05:42:57.372982Z","shell.execute_reply.started":"2024-11-05T05:42:57.356550Z","shell.execute_reply":"2024-11-05T05:42:57.372241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create a complete CNN\nmodel = CNN()\nprint(model)\n\n# move tensors to GPU if CUDA is available\nif train_on_gpu:\n    model.cuda()","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:42:57.862460Z","iopub.execute_input":"2024-11-05T05:42:57.862831Z","iopub.status.idle":"2024-11-05T05:42:57.911274Z","shell.execute_reply.started":"2024-11-05T05:42:57.862764Z","shell.execute_reply":"2024-11-05T05:42:57.910299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\n# specify loss function (categorical cross-entropy)\ncriterion = nn.SmoothL1Loss()\n\n# specify optimizer\noptimizer = optim.Adam(model.parameters(), lr=0.0003)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T06:11:39.464689Z","iopub.execute_input":"2024-11-05T06:11:39.464981Z","iopub.status.idle":"2024-11-05T06:11:39.470526Z","shell.execute_reply.started":"2024-11-05T06:11:39.464939Z","shell.execute_reply":"2024-11-05T06:11:39.469476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef train(\n    n_epochs=500, \n    train_loader=None, \n    valid_loader=None, \n    model=None, \n    optimizer=None, \n    criterion=None, \n    index_fold=None,\n    max_training_time=1,  # in hours\n    max_output_file_nb=50, \n    checkpoint_path='checkpoints'\n):\n    \"\"\"\n    Train a neural network model with early stopping and checkpointing.\n\n    Args:\n        n_epochs (int): Number of epochs to train the model.\n        train_loader (DataLoader): Dataloader for the training set.\n        valid_loader (DataLoader): Dataloader for the validation set.\n        model (torch.nn.Module): The neural network model.\n        optimizer (torch.optim.Optimizer): Optimizer to update model parameters.\n        criterion (torch.nn.Module): Loss function to evaluate model performance.\n        max_training_time (float): Maximum training time in hours. Default is 1 hour.\n        max_checkpoints (int): Maximum number of model checkpoints to save.\n        checkpoint_path (str): Path to save model checkpoints.\n    \"\"\"\n    # number of epochs to train the model\n    import time\n    #n_epochs = 10 # default is 500\n    loss_per_iter = []\n    start_time = time.time()\n\n    valid_loss_min = np.Inf # track change in validation loss\n\n    output_file_nb = 0\n    #max_output_file_nb = 50\n\n    for epoch in range(1, n_epochs+1):\n\n        if (time.time() - start_time) / 3600 > 1:\n            output_file_nb += 1\n            print('Last iteration ({:.6f} --> {:.6f}).  Saving model ...'.format(\n            valid_loss_min, valid_loss))\n            torch.save(model.state_dict(), 'model_last.pt')\n            break\n\n        # keep track of training and validation loss\n        train_loss = 0.0\n        valid_loss = 0.0\n\n        ###################\n        # train the model #\n        ###################\n        model.train()\n        for ind, (data, target) in enumerate(train_loader):\n            print(ind, end='\\r')\n\n            if train_on_gpu:\n                data, target = data.cuda(), target.cuda()\n\n            optimizer.zero_grad()\n            output = model(data)\n\n            loss = criterion(output.view(data.shape[0]), target.float())\n            loss.backward()\n            optimizer.step()\n\n            train_loss += loss.item()*data.size(0)\n\n        ######################    \n        # validate the model #\n        ######################\n        model.eval()\n        for data, target in valid_loader:\n            # move tensors to GPU if CUDA is available\n            if train_on_gpu:\n                data, target = data.cuda(), target.cuda()\n\n            # forward pass: compute predicted outputs by passing inputs to the model\n            output = model(data)    \n            loss = criterion(output.view(data.shape[0]), target.float())    \n            valid_loss += loss.item() * data.size(0)\n\n        # calculate average losses\n        train_loss = train_loss / len(train_loader.sampler)\n        valid_loss = valid_loss / len(valid_loader.sampler)\n\n        # print training/validation statistics \n        print('Epoch: {} \\tTraining Loss: {:.6f} \\tValidation Loss: {:.6f}'.format(\n            epoch, train_loss, valid_loss))\n\n        loss_per_iter.append([train_loss, valid_loss])\n\n        # save model if validation loss has decreased\n        if valid_loss <= valid_loss_min:\n            output_file_nb += 1\n            print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n            valid_loss_min,\n            valid_loss))\n            torch.save(model.state_dict(), f'model_best_fold{index_fold}.pt')\n            valid_loss_min = valid_loss\n\n            if epoch > 20:\n                output_file_nb += 1\n                torch.save(model.state_dict(), f'model_t{round(train_loss, 3)}_v{round(valid_loss, 3)}_ep{epoch}.pt')\n\n        elif valid_loss <= 1.03 * valid_loss_min and epoch > 20:\n            output_file_nb += 1\n            print('Validation loss saved at ({:.6f}).  Saving model ...'.format(valid_loss))\n            torch.save(model.state_dict(), f'model_t{round(train_loss, 3)}_v{round(valid_loss, 3)}_ep{epoch}.pt')\n\n        if output_file_nb > max_output_file_nb: break\n\n    model.load_state_dict(torch.load(f'model_best_fold{index_fold}.pt'))\n    return loss_per_iter\n\nloss_per_iter = train(n_epochs=200,\n                      train_loader=train_loader,\n                      valid_loader=valid_loader,\n                      model=model,\n                      index_fold=-1,\n                      optimizer=optimizer,\n                      criterion=criterion)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T06:11:40.126114Z","iopub.execute_input":"2024-11-05T06:11:40.126415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_loss(loss_per_iter=None):\n    import matplotlib.pyplot as plt\n    plt.figure(figsize=(14,8))\n    plt.plot(np.array(loss_per_iter)[:, 0], 'o-', label = 'train')\n    plt.plot(np.array(loss_per_iter)[:, 1], 'o-', label = 'valid')\n    plt.legend()\n    plt.show()\nplot_loss(loss_per_iter)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T06:10:58.958381Z","iopub.execute_input":"2024-11-05T06:10:58.958742Z","iopub.status.idle":"2024-11-05T06:10:59.292607Z","shell.execute_reply.started":"2024-11-05T06:10:58.958687Z","shell.execute_reply":"2024-11-05T06:10:59.291354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'fold {index_fold} [train] min loss: ', np.array(loss_per_iter)[:, 0].min())\nprint(f'fold {index_fold} [valid] min loss: ', np.array(loss_per_iter)[:, 1].min())","metadata":{"execution":{"iopub.status.busy":"2024-11-05T06:11:05.959561Z","iopub.execute_input":"2024-11-05T06:11:05.959882Z","iopub.status.idle":"2024-11-05T06:11:05.965921Z","shell.execute_reply.started":"2024-11-05T06:11:05.959839Z","shell.execute_reply":"2024-11-05T06:11:05.964828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate(valid_loader=None, model=None):\n    # number of epochs to train the model\n    import time\n    from random import randint \n    import matplotlib.pyplot as plt\n    import numpy as np\n    coupling_constant_predict = []\n    coupling_constant_ground_truth = []\n    ######################    \n    # validate the model #\n    ######################\n    model.eval()\n    for data, target in  valid_loader:\n        # move tensors to GPU if CUDA is available\n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()\n\n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = model(data)    \n        output = output.view(data.shape[0]) \n\n        coupling_constant_predict.append(output.cpu().detach().numpy())\n        coupling_constant_ground_truth.append(target.cpu().detach().numpy())\n\n        # calculate average losses\n        #valid_loss = loss.item() \n\n    coupling_constant_predict = np.concatenate(coupling_constant_predict)\n    coupling_constant_ground_truth = np.concatenate(coupling_constant_ground_truth)\n    fig, ax = plt.subplots(figsize=(8, 8))\n    for x, y in zip(coupling_constant_predict, coupling_constant_ground_truth):\n        plt.scatter(x, y, s=10)\n    plt.xlabel(r'$J_{ground truth}$ (Hz)', fontsize=20)\n    plt.ylabel(r'$J_{predict}$ (Hz)', fontsize=20)\n    plt.xticks(fontsize=20)\n    plt.yticks(fontsize=20)\n    plt.show()\n    \n    return coupling_constant_predict, coupling_constant_ground_truth\n\ncoupling_constant_predict, coupling_constant_ground_truth = validate(valid_loader=valid_loader,\n                                                                     model=model)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:52:05.779406Z","iopub.execute_input":"2024-11-05T05:52:05.779793Z","iopub.status.idle":"2024-11-05T05:52:07.323148Z","shell.execute_reply.started":"2024-11-05T05:52:05.779728Z","shell.execute_reply":"2024-11-05T05:52:07.322091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_metric(coupling_constant_predict=None, coupling_constant_ground_truth=None):\n    mae = np.mean(np.abs(coupling_constant_predict - coupling_constant_ground_truth))\n    mse = np.mean((coupling_constant_predict - coupling_constant_ground_truth)**2)\n    rmse = np.sqrt(np.mean((coupling_constant_predict - coupling_constant_ground_truth)**2))\n    metrics = {'mae': mae, 'mse': mse, 'rmse': rmse}\n    return metrics\n\nmetrics = get_metric(coupling_constant_predict=coupling_constant_predict,\n                      coupling_constant_ground_truth=coupling_constant_ground_truth)\nprint(metrics)","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:52:15.180710Z","iopub.execute_input":"2024-11-05T05:52:15.181042Z","iopub.status.idle":"2024-11-05T05:52:15.188059Z","shell.execute_reply.started":"2024-11-05T05:52:15.180984Z","shell.execute_reply":"2024-11-05T05:52:15.187324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cross Validation","metadata":{}},{"cell_type":"code","source":"fold_metrics = []\nfor index_fold in range(len(folds)):\n    train_loader, valid_loader = get_loaders(index_fold, \n                                             train_data=image_dataset, \n                                             images_path=images_path, \n                                             folds=folds)\n    # create a complete CNN\n    model = CNN()\n    print(model)\n\n    # move tensors to GPU if CUDA is available\n    if train_on_gpu:\n        model.cuda()\n\n    import torch.optim as optim\n\n    # specify loss function (categorical cross-entropy)\n    criterion = nn.SmoothL1Loss()\n\n    # specify optimizer\n    optimizer = optim.Adam(model.parameters(), lr=0.0003)\n    \n    \n    loss_per_iter = train(n_epochs=200,\n                          train_loader=train_loader,\n                          valid_loader=valid_loader,\n                          model=model,\n                          index_fold=index_fold,\n                          optimizer=optimizer,\n                          criterion=criterion,\n                          max_output_file_nb=200) \n    \n    plot_loss(loss_per_iter)\n    print(f'fold {index_fold} [train] min loss: ', np.array(loss_per_iter)[:, 0].min())\n    print(f'fold {index_fold} [valid] min loss: ', np.array(loss_per_iter)[:, 1].min())\n\n    coupling_constant_predict, coupling_constant_ground_truth = validate(valid_loader=valid_loader,\n                                                                     model=model)\n\n    metrics = get_metric(coupling_constant_predict=coupling_constant_predict,\n                          coupling_constant_ground_truth=coupling_constant_ground_truth)\n    print(metrics)\n\n    fold_metrics.append(metrics)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:52:30.445457Z","iopub.execute_input":"2024-11-05T05:52:30.445854Z","iopub.status.idle":"2024-11-05T05:59:23.063860Z","shell.execute_reply.started":"2024-11-05T05:52:30.445791Z","shell.execute_reply":"2024-11-05T05:59:23.062653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mae\nmae = []\nfor metrics in fold_metrics:\n    mae.append(metrics['mae'])\nmae = np.array(mae)\nprint(f\"mae: {mae.mean():.5f} +/- {mae.std():.5f}\")\n\n# mse\nmse = []\nfor metrics in fold_metrics:\n    mse.append(metrics['mse'])\nmse = np.array(mse)\nprint(f\"mse: {mse.mean():.10f} +/- {mse.std():.10f}\")\n\n# rmse\nrmse = []\nfor metrics in fold_metrics:\n    rmse.append(metrics['rmse'])\nrmse = np.array(rmse)\nprint(f\"rmse: {rmse.mean():.5f} +/- {rmse.std():.5f}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-05T05:40:28.686714Z","iopub.execute_input":"2024-11-05T05:40:28.687348Z","iopub.status.idle":"2024-11-05T05:40:28.697468Z","shell.execute_reply.started":"2024-11-05T05:40:28.687066Z","shell.execute_reply":"2024-11-05T05:40:28.696535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission\n","metadata":{}},{"cell_type":"code","source":"'''import os\ndf = pd.read_csv('../input/champs-scalar-coupling/test.csv')\nimage_names = os.listdir('../input/full-images-1jhn/Image_1JHN/')  \n#keep df rows that type == 1JHN\ndf = df[df['type'] == '1JHN']\n\ndf.head()'''","metadata":{"execution":{"iopub.status.busy":"2024-11-05T04:51:18.851524Z","iopub.status.idle":"2024-11-05T04:51:18.852342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''num_rows = len(df)\ni_row = np.random.randint(0, num_rows)\n\n#get id(key) and molecule name from df.iloc[i_row]\nkey = df.iloc[i_row]['id']\nmolecule_name = df.iloc[i_row]['molecule_name']\n\nif( str(molecule_name) +\"_\" + str(key) +\".pkl\" in image_names):\n    print(\"image exists\")\nelse:\n    print(\"image does not exist\")'''","metadata":{"execution":{"iopub.status.busy":"2024-11-05T04:51:18.853662Z","iopub.status.idle":"2024-11-05T04:51:18.854275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''image_names[0]'''","metadata":{"execution":{"iopub.status.busy":"2024-11-05T04:51:18.855850Z","iopub.status.idle":"2024-11-05T04:51:18.856342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}