{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":70367,"databundleVersionId":9188054,"sourceType":"competition"},{"sourceId":10021303,"sourceType":"datasetVersion","datasetId":6170825},{"sourceId":10051495,"sourceType":"datasetVersion","datasetId":6189119}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Setting","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport random \nimport os\nimport sys\nimport scipy.stats\nimport pandas as pd\nfrom tqdm import tqdm\n\nimport pickle\nimport matplotlib.pyplot as plt\nfrom matplotlib.ticker import ScalarFormatter","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:18.579608Z","iopub.execute_input":"2024-11-30T18:24:18.579976Z","iopub.status.idle":"2024-11-30T18:24:19.258361Z","shell.execute_reply.started":"2024-11-30T18:24:18.579944Z","shell.execute_reply":"2024-11-30T18:24:19.257672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf \n\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.losses import MeanAbsoluteError\nfrom tensorflow.keras.callbacks import LearningRateScheduler, ModelCheckpoint\nfrom keras.layers import Input, Conv1D, Conv2D, MaxPooling1D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, Concatenate,AveragePooling1D\nfrom keras.models import Model, load_model\n\nfrom keras.optimizers import Adam, RMSprop","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:19.25961Z","iopub.execute_input":"2024-11-30T18:24:19.25993Z","iopub.status.idle":"2024-11-30T18:24:30.822317Z","shell.execute_reply.started":"2024-11-30T18:24:19.259906Z","shell.execute_reply":"2024-11-30T18:24:30.821625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def set_seed(seed=42):    \n    '''랜덤시드 고정. Hyperparm tuning 제외 모든 학습환경에서 \n    같은 성능이 나오게 합니다.'''\n    tf.random.set_seed(seed)\n    np.random.seed(seed)\n    random.seed(seed)\nset_seed(42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:30.823439Z","iopub.execute_input":"2024-11-30T18:24:30.824087Z","iopub.status.idle":"2024-11-30T18:24:30.829698Z","shell.execute_reply.started":"2024-11-30T18:24:30.824048Z","shell.execute_reply":"2024-11-30T18:24:30.828728Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Import data, model","metadata":{}},{"cell_type":"code","source":"nb_dropout_1d = 1000\nnb_dropout_2d = 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:30.832206Z","iopub.execute_input":"2024-11-30T18:24:30.83249Z","iopub.status.idle":"2024-11-30T18:24:30.853557Z","shell.execute_reply.started":"2024-11-30T18:24:30.83246Z","shell.execute_reply":"2024-11-30T18:24:30.852657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_path = '/kaggle/input/testset-2d/'\nauxiliary_folder = '/kaggle/input/ariel-data-challenge-2024/'\npred_path = '/kaggle/input/testset-2d/'\noutput_path = '/kaggle/working/'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:30.854701Z","iopub.execute_input":"2024-11-30T18:24:30.854986Z","iopub.status.idle":"2024-11-30T18:24:30.865426Z","shell.execute_reply.started":"2024-11-30T18:24:30.85496Z","shell.execute_reply":"2024-11-30T18:24:30.864623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_adc_info = pd.read_csv(auxiliary_folder + 'test_adc_info.csv', index_col='planet_id')\ntrain_labels = pd.read_csv(auxiliary_folder + 'train_labels.csv', index_col='planet_id')\nsample_submission = pd.read_csv(auxiliary_folder + 'sample_submission.csv', index_col='planet_id')\nwavelengths = pd.read_csv(auxiliary_folder + 'wavelengths.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:30.866548Z","iopub.execute_input":"2024-11-30T18:24:30.866808Z","iopub.status.idle":"2024-11-30T18:24:31.089114Z","shell.execute_reply.started":"2024-11-30T18:24:30.866784Z","shell.execute_reply":"2024-11-30T18:24:31.088415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"testset_1d = np.load(pred_path + 'testset_1d.npy')\ntestset_2d = np.load(pred_path + 'testset_2d.npy')\ntestset_1d.shape, testset_2d.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:31.090301Z","iopub.execute_input":"2024-11-30T18:24:31.090673Z","iopub.status.idle":"2024-11-30T18:24:31.108355Z","shell.execute_reply.started":"2024-11-30T18:24:31.090644Z","shell.execute_reply":"2024-11-30T18:24:31.107531Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 보정값 저장 (Values for denormalizing)","metadata":{}},{"cell_type":"code","source":"targets_min_1d = 0.0004002855578500475\ntargets_max_1d = 0.007354582286725231\n\ntarget_abs_max_2d = 0.0030516885275461087","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:31.109638Z","iopub.execute_input":"2024-11-30T18:24:31.110232Z","iopub.status.idle":"2024-11-30T18:24:31.114313Z","shell.execute_reply.started":"2024-11-30T18:24:31.110192Z","shell.execute_reply":"2024-11-30T18:24:31.113454Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predict wl","metadata":{}},{"cell_type":"code","source":"cnn1d_model = load_model(model_path+'/model_cnn1d.h5', compile=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:31.11543Z","iopub.execute_input":"2024-11-30T18:24:31.115837Z","iopub.status.idle":"2024-11-30T18:24:32.070743Z","shell.execute_reply.started":"2024-11-30T18:24:31.11577Z","shell.execute_reply":"2024-11-30T18:24:32.069952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn1d_model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mse'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:32.073995Z","iopub.execute_input":"2024-11-30T18:24:32.074565Z","iopub.status.idle":"2024-11-30T18:24:32.086855Z","shell.execute_reply.started":"2024-11-30T18:24:32.07453Z","shell.execute_reply":"2024-11-30T18:24:32.086128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_1d = cnn1d_model.predict(testset_1d)\npredictions_1d","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:32.087915Z","iopub.execute_input":"2024-11-30T18:24:32.088169Z","iopub.status.idle":"2024-11-30T18:24:33.615447Z","shell.execute_reply.started":"2024-11-30T18:24:32.088144Z","shell.execute_reply":"2024-11-30T18:24:33.614583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_1d = predictions_1d*(targets_max_1d - targets_min_1d) + targets_min_1d\npredictions_1d","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:33.61653Z","iopub.execute_input":"2024-11-30T18:24:33.616787Z","iopub.status.idle":"2024-11-30T18:24:33.623212Z","shell.execute_reply.started":"2024-11-30T18:24:33.616762Z","shell.execute_reply":"2024-11-30T18:24:33.621695Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 2D CNN","metadata":{}},{"cell_type":"code","source":"cnn2d_model = load_model(model_path+'model_cnn2d.h5', compile=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:33.624211Z","iopub.execute_input":"2024-11-30T18:24:33.624553Z","iopub.status.idle":"2024-11-30T18:24:34.520389Z","shell.execute_reply.started":"2024-11-30T18:24:33.62452Z","shell.execute_reply":"2024-11-30T18:24:34.519376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn2d_model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mse'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:34.521619Z","iopub.execute_input":"2024-11-30T18:24:34.52199Z","iopub.status.idle":"2024-11-30T18:24:34.53086Z","shell.execute_reply.started":"2024-11-30T18:24:34.521954Z","shell.execute_reply":"2024-11-30T18:24:34.530145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_2d = cnn2d_model.predict(testset_2d)\npredictions_2d = predictions_2d * target_abs_max_2d\npredictions_2d.shape, predictions_2d.max(), predictions_2d.min()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:34.53187Z","iopub.execute_input":"2024-11-30T18:24:34.532112Z","iopub.status.idle":"2024-11-30T18:24:35.255731Z","shell.execute_reply.started":"2024-11-30T18:24:34.532089Z","shell.execute_reply":"2024-11-30T18:24:35.254932Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Sum 1D & 2D result","metadata":{}},{"cell_type":"code","source":"# wl_prediction = predictions_2d + predictions_1d\n# wl_prediction.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:35.256685Z","iopub.execute_input":"2024-11-30T18:24:35.256949Z","iopub.status.idle":"2024-11-30T18:24:35.260845Z","shell.execute_reply.started":"2024-11-30T18:24:35.256925Z","shell.execute_reply":"2024-11-30T18:24:35.259978Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predict Uncertainty","metadata":{}},{"cell_type":"markdown","source":"### 1D CNN","metadata":{}},{"cell_type":"code","source":"def MC_dropout_predict_1d(model, data, nb_iter, targets_min, targets_max):\n    predictions = np.zeros((nb_iter, data.shape[0]))\n    for i in range(nb_iter):\n        preds_norm = model(data, training=True).numpy().flatten()\n        preds = preds_norm * (targets_max - targets_min) + targets_min\n        predictions[i, :] = preds\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:35.261858Z","iopub.execute_input":"2024-11-30T18:24:35.262157Z","iopub.status.idle":"2024-11-30T18:24:35.270176Z","shell.execute_reply.started":"2024-11-30T18:24:35.262115Z","shell.execute_reply":"2024-11-30T18:24:35.269363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Performing MC Dropout for 1D CNN...')\npredictions_1d_mc = MC_dropout_predict_1d(\n    cnn1d_model, testset_1d, nb_dropout_1d, targets_min_1d, targets_max_1d)\nprint('Done.')\n\n# 평균과 표준편차 계산. Find mean and std \nmean_predictions_1d = predictions_1d_mc.mean()\nstd_predictions_1d = predictions_1d_mc.std()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:35.271282Z","iopub.execute_input":"2024-11-30T18:24:35.271638Z","iopub.status.idle":"2024-11-30T18:24:56.835413Z","shell.execute_reply.started":"2024-11-30T18:24:35.271603Z","shell.execute_reply":"2024-11-30T18:24:56.834459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def MC_dropout_predict_2d(model, data, nb_iter, target_abs_max):\n    predictions = []\n    for _ in range(nb_iter):\n        preds_norm = model(data, training=True).numpy()\n        preds = preds_norm * target_abs_max\n        predictions.append(preds)\n    predictions = np.array(predictions)\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:56.836738Z","iopub.execute_input":"2024-11-30T18:24:56.836985Z","iopub.status.idle":"2024-11-30T18:24:56.84161Z","shell.execute_reply.started":"2024-11-30T18:24:56.836962Z","shell.execute_reply":"2024-11-30T18:24:56.840765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Performing MC Dropout for 2D CNN...')\npredictions_2d_mc = MC_dropout_predict_2d(\n    cnn2d_model, testset_2d, nb_dropout_2d, target_abs_max_2d)\nprint('Done.')\n\n# 평균과 표준편차 계산. Find mean and std \nmean_predictions_2d = predictions_2d_mc.mean(axis=0)\nstd_predictions_2d = predictions_2d_mc.std(axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:56.842964Z","iopub.execute_input":"2024-11-30T18:24:56.843341Z","iopub.status.idle":"2024-11-30T18:24:57.279655Z","shell.execute_reply.started":"2024-11-30T18:24:56.843301Z","shell.execute_reply":"2024-11-30T18:24:57.278543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean_predictions_2d.max(), mean_predictions_2d.min(), ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.280948Z","iopub.execute_input":"2024-11-30T18:24:57.281274Z","iopub.status.idle":"2024-11-30T18:24:57.288149Z","shell.execute_reply.started":"2024-11-30T18:24:57.281242Z","shell.execute_reply":"2024-11-30T18:24:57.287085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"std_predictions_2d.max(), std_predictions_2d.min()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.289359Z","iopub.execute_input":"2024-11-30T18:24:57.28977Z","iopub.status.idle":"2024-11-30T18:24:57.300546Z","shell.execute_reply.started":"2024-11-30T18:24:57.289711Z","shell.execute_reply":"2024-11-30T18:24:57.299478Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Merge Uncertainty ","metadata":{}},{"cell_type":"code","source":"# 1D 예측값을 확장하여 2D 예측값과 결합 (expand 1D prediction, and merge em with 2d predicions)\nwl_prediction = mean_predictions_1d + mean_predictions_2d\nwl_prediction","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.301861Z","iopub.execute_input":"2024-11-30T18:24:57.302182Z","iopub.status.idle":"2024-11-30T18:24:57.317608Z","shell.execute_reply.started":"2024-11-30T18:24:57.302155Z","shell.execute_reply":"2024-11-30T18:24:57.316521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1D 불확실성을 확장하여 2D 불확실성과 결합 (expand 1D uncertainty and merge em with 2D uncertainty)\ntotal_uncertainty = np.sqrt(std_predictions_1d**2 + std_predictions_2d**2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.31869Z","iopub.execute_input":"2024-11-30T18:24:57.319108Z","iopub.status.idle":"2024-11-30T18:24:57.33354Z","shell.execute_reply.started":"2024-11-30T18:24:57.319079Z","shell.execute_reply":"2024-11-30T18:24:57.332777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.DataFrame(total_uncertainty).T.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.334528Z","iopub.execute_input":"2024-11-30T18:24:57.334866Z","iopub.status.idle":"2024-11-30T18:24:57.35835Z","shell.execute_reply.started":"2024-11-30T18:24:57.334821Z","shell.execute_reply":"2024-11-30T18:24:57.357564Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Make Submission","metadata":{}},{"cell_type":"code","source":"df = sample_submission.copy()\nnum_samples = wl_prediction.shape[0]\nnum_wavelengths = wl_prediction.shape[1]\n\nprint(f'Number of samples: {num_samples}, Number of wavelengths: {num_wavelengths}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.359357Z","iopub.execute_input":"2024-11-30T18:24:57.359662Z","iopub.status.idle":"2024-11-30T18:24:57.364679Z","shell.execute_reply.started":"2024-11-30T18:24:57.359637Z","shell.execute_reply":"2024-11-30T18:24:57.363812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for each samples\n# for idx in df.index:\n#     sample_idx = df.index.get_loc(idx)  # find index location\n#     # for every wavelengths\n#     for wl_idx in range(num_wavelengths):\n#         df.at[idx, f'wl_{wl_idx+1}'] = wl_prediction[sample_idx, wl_idx]\n#         df.at[idx, f'sigma_{wl_idx+1}'] = 0.0\n#        df.at[idx, f'sigma_{wl_idx+1}'] = total_uncertainty[sample_idx, wl_idx]\n\nfor i in range(1, 284):\n    df[f'wl_{i}'] = wl_prediction[0, i-1]  # wl_prediction의 i번째 값을 할당\n    df[f'sigma_{i}'] = total_uncertainty[0, i-1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:27:24.734811Z","iopub.execute_input":"2024-11-30T18:27:24.73517Z","iopub.status.idle":"2024-11-30T18:27:24.758524Z","shell.execute_reply.started":"2024-11-30T18:27:24.735139Z","shell.execute_reply":"2024-11-30T18:27:24.757667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load submission in csv file\nsubmission_file = 'submission.csv'\ndf.to_csv(submission_file)\nprint(f'Submission file saved to {submission_file}')\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:27:26.79787Z","iopub.execute_input":"2024-11-30T18:27:26.798211Z","iopub.status.idle":"2024-11-30T18:27:26.831426Z","shell.execute_reply.started":"2024-11-30T18:27:26.798179Z","shell.execute_reply":"2024-11-30T18:27:26.830528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = sample_submission.copy()\n# for i in range(1, 284):\n#     df[f'wl_{i}'] = wl_prediction[0, i-1]-0.0001  # wl_prediction의 i번째 값을 할당\n#     df[f'sigma_{i}'] = 0.000156  # sigma 값을 0.0으로 설정\n\n# # 결과를 submission.csv로 저장합니다.\n# df.to_csv('submission.csv')\n# df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T18:24:57.462372Z","iopub.execute_input":"2024-11-30T18:24:57.462694Z","iopub.status.idle":"2024-11-30T18:24:57.466836Z","shell.execute_reply.started":"2024-11-30T18:24:57.462664Z","shell.execute_reply":"2024-11-30T18:24:57.465898Z"}},"outputs":[],"execution_count":null}]}