{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":101849,"databundleVersionId":13093295,"sourceType":"competition"},{"sourceId":13006009,"sourceType":"datasetVersion","datasetId":7946971},{"sourceId":13158809,"sourceType":"datasetVersion","datasetId":7946981}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Description is here: https://www.kaggle.com/competitions/ariel-data-challenge-2025/writeups/24-place-solution#3294229","metadata":{}},{"cell_type":"code","source":"!pip install -r /kaggle/input/ariel-packages/requirements.txt \\\n    -U --no-index --find-links /kaggle/input/ariel-packages/packages","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:11:13.765869Z","iopub.execute_input":"2025-09-24T13:11:13.766043Z","iopub.status.idle":"2025-09-24T13:13:55.094491Z","shell.execute_reply.started":"2025-09-24T13:11:13.766028Z","shell.execute_reply":"2025-09-24T13:13:55.093607Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sys\nsys.path.append(\"/kaggle/input/ariel-solution\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:13:55.09619Z","iopub.execute_input":"2025-09-24T13:13:55.096506Z","iopub.status.idle":"2025-09-24T13:13:55.100269Z","shell.execute_reply.started":"2025-09-24T13:13:55.096472Z","shell.execute_reply":"2025-09-24T13:13:55.099638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import lightning as L\nfrom tqdm import tqdm\nimport torch\nimport torch.nn as nn\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\nimport joblib\nfrom torch.utils.data import DataLoader\nimport shutil\n\nfrom src.data import TransitDataset, DataProcessor\nfrom src.model import TransitModel, UncertaintyModel\nfrom src.loss import GaussianLogLikelihoodLoss\nfrom src.utils import read_yaml, ConstantCosineLR, plot_curves","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:13:55.100951Z","iopub.execute_input":"2025-09-24T13:13:55.101116Z","iopub.status.idle":"2025-09-24T13:14:06.990513Z","shell.execute_reply.started":"2025-09-24T13:13:55.101101Z","shell.execute_reply":"2025-09-24T13:14:06.989912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"CACHE_FOLDER = \"/kaggle/temp/cache\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:14:06.99119Z","iopub.execute_input":"2025-09-24T13:14:06.991446Z","iopub.status.idle":"2025-09-24T13:14:06.995036Z","shell.execute_reply.started":"2025-09-24T13:14:06.991427Z","shell.execute_reply":"2025-09-24T13:14:06.994292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fabric = L.Fabric(accelerator=\"auto\", precision=32)\nfabric.seed_everything(42, workers=True)\nfabric.launch()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:14:06.99582Z","iopub.execute_input":"2025-09-24T13:14:06.996052Z","iopub.status.idle":"2025-09-24T13:14:07.139371Z","shell.execute_reply.started":"2025-09-24T13:14:06.99603Z","shell.execute_reply":"2025-09-24T13:14:07.138579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"planets = list(Path(\"/kaggle/input/ariel-data-challenge-2025/test\").glob(\"*\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:14:07.140185Z","iopub.execute_input":"2025-09-24T13:14:07.14062Z","iopub.status.idle":"2025-09-24T13:14:07.147679Z","shell.execute_reply.started":"2025-09-24T13:14:07.140585Z","shell.execute_reply":"2025-09-24T13:14:07.146893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"meta = pd.read_csv(\"/kaggle/input/ariel-data-challenge-2025/test_star_info.csv\")\naxis_info = pd.read_parquet(\"/kaggle/input/ariel-data-challenge-2025/axis_info.parquet\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:14:07.150012Z","iopub.execute_input":"2025-09-24T13:14:07.150703Z","iopub.status.idle":"2025-09-24T13:14:07.20903Z","shell.execute_reply.started":"2025-09-24T13:14:07.150679Z","shell.execute_reply":"2025-09-24T13:14:07.208514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_processor = DataProcessor(planets, axis_info=axis_info, cache_folder=CACHE_FOLDER)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:14:07.209671Z","iopub.execute_input":"2025-09-24T13:14:07.209927Z","iopub.status.idle":"2025-09-24T13:14:07.217534Z","shell.execute_reply.started":"2025-09-24T13:14:07.209902Z","shell.execute_reply":"2025-09-24T13:14:07.216839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"outputs = []\n\nfor i in range(10):\n    dataset = TransitDataset(\n    data_processor=data_processor, \n    gt=None, \n    meta=meta, \n    output_stats=joblib.load(f\"/kaggle/input/ariel-solution/stats_{i}.joblib\")\n    )\n\n    dataloader = DataLoader(dataset, batch_size=4, num_workers=4, shuffle=False)\n    dataloader = fabric.setup_dataloaders(dataloader)\n\n    for prefix in [\"best\", \"last\"]:\n\n        model = TransitModel()\n        model.load_state_dict(torch.load(f\"/kaggle/input/ariel-solution/{prefix}_model_{i}.pth\", map_location=torch.device(\"cpu\")))\n        model = fabric.setup(model)\n        model.eval()\n    \n        fold_outputs = []\n        planet_ids = []\n        \n        with torch.no_grad():\n            for batch in dataloader:\n                planet_ids += list(batch[\"planet_id\"])\n        \n                out = model(batch)\n                out[:, :283] = dataloader.dataset.denorm(out[:, :283], \"targets\")\n        \n                fold_outputs.append(out.cpu())\n        \n        fold_outputs = torch.cat(fold_outputs, dim=0)\n    \n        outputs.append(fold_outputs)\n\n    print(f\"Fold {i} done\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:14:07.218313Z","iopub.execute_input":"2025-09-24T13:14:07.218643Z","iopub.status.idle":"2025-09-24T13:16:04.363834Z","shell.execute_reply.started":"2025-09-24T13:14:07.218617Z","shell.execute_reply":"2025-09-24T13:16:04.362888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"shutil.rmtree(CACHE_FOLDER)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:04.364994Z","iopub.execute_input":"2025-09-24T13:16:04.365305Z","iopub.status.idle":"2025-09-24T13:16:04.387616Z","shell.execute_reply.started":"2025-09-24T13:16:04.365267Z","shell.execute_reply":"2025-09-24T13:16:04.386909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"outputs = torch.stack(outputs, dim=0).mean(dim=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:04.388578Z","iopub.execute_input":"2025-09-24T13:16:04.388847Z","iopub.status.idle":"2025-09-24T13:16:04.392524Z","shell.execute_reply.started":"2025-09-24T13:16:04.388825Z","shell.execute_reply":"2025-09-24T13:16:04.391957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.clip(outputs.numpy(), a_min=0.0, a_max=None)[0, :283])\nplt.title(\"signal\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:04.393274Z","iopub.execute_input":"2025-09-24T13:16:04.393542Z","iopub.status.idle":"2025-09-24T13:16:04.632714Z","shell.execute_reply.started":"2025-09-24T13:16:04.39352Z","shell.execute_reply":"2025-09-24T13:16:04.631946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(outputs[0, 283:])\nplt.title(\"Uncertainty\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:04.633503Z","iopub.execute_input":"2025-09-24T13:16:04.633765Z","iopub.status.idle":"2025-09-24T13:16:04.797777Z","shell.execute_reply.started":"2025-09-24T13:16:04.633743Z","shell.execute_reply":"2025-09-24T13:16:04.797074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/ariel-data-challenge-2025/sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:04.798468Z","iopub.execute_input":"2025-09-24T13:16:04.798711Z","iopub.status.idle":"2025-09-24T13:16:04.815091Z","shell.execute_reply.started":"2025-09-24T13:16:04.798687Z","shell.execute_reply":"2025-09-24T13:16:04.814565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame(planet_ids, columns=[\"planet_id\"])\nsubmission.loc[:, sample_submission.columns[1:]] = np.clip(outputs.numpy(), a_min=0.0, a_max=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:04.815824Z","iopub.execute_input":"2025-09-24T13:16:04.81606Z","iopub.status.idle":"2025-09-24T13:16:05.154187Z","shell.execute_reply.started":"2025-09-24T13:16:04.816043Z","shell.execute_reply":"2025-09-24T13:16:05.153667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = submission.groupby(\"planet_id\").mean()\nsubmission.to_csv(\"submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:05.154861Z","iopub.execute_input":"2025-09-24T13:16:05.155095Z","iopub.status.idle":"2025-09-24T13:16:05.198343Z","shell.execute_reply.started":"2025-09-24T13:16:05.15507Z","shell.execute_reply":"2025-09-24T13:16:05.197795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-24T13:16:05.199044Z","iopub.execute_input":"2025-09-24T13:16:05.199283Z","iopub.status.idle":"2025-09-24T13:16:05.219661Z","shell.execute_reply.started":"2025-09-24T13:16:05.199263Z","shell.execute_reply":"2025-09-24T13:16:05.218956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}