{"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":"none","dataSources":[{"sourceId":93783,"databundleVersionId":11331308,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":538097,"sourceType":"modelInstanceVersion","isSourceIdPinned":false,"modelInstanceId":412668,"modelId":430430},{"sourceId":537370,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":412049,"modelId":429842}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%cd /\n!rm -rf /kaggle/working/esa_challenge\n!git clone https://github.com/lorenzoAllegrini/esa_challenge.git /kaggle/working/esa_challenge","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:30:34.264010Z","iopub.execute_input":"2025-08-25T00:30:34.264692Z","iopub.status.idle":"2025-08-25T00:30:35.577295Z","shell.execute_reply.started":"2025-08-25T00:30:34.264660Z","shell.execute_reply":"2025-08-25T00:30:35.576002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --no-cache-dir --upgrade \"scikit-learn==1.5.1\" \"numpy==1.26.4\" \"scipy==1.11.4\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:30:35.579341Z","iopub.execute_input":"2025-08-25T00:30:35.579653Z","iopub.status.idle":"2025-08-25T00:30:52.388866Z","shell.execute_reply.started":"2025-08-25T00:30:35.579623Z","shell.execute_reply":"2025-08-25T00:30:52.387704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install Cython\n%cd /kaggle/working/esa_challenge/spaceai/segmentators\n!python setup.py build_ext --inplace","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:30:52.390796Z","iopub.execute_input":"2025-08-25T00:30:52.391202Z","iopub.status.idle":"2025-08-25T00:31:06.352026Z","shell.execute_reply.started":"2025-08-25T00:30:52.391132Z","shell.execute_reply":"2025-08-25T00:31:06.351004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sys\nimport pandas as pd\nimport numpy as np\nsys.path.append('/kaggle/working/esa_challenge')\n\n\n\nfrom spaceai.segmentators.shapelet_miner import ShapeletMiner\nfrom spaceai.segmentators.esa_segmentator2 import EsaDatasetSegmentator2\n\n\nshapelet_miner1 = ShapeletMiner(\n    k_min_length=30, k_max_length=40, num_kernels=10,\n    segment_duration=50, step_duration=10,\n    run_id=\"esa_run\", exp_dir=\"experiments\", skip=True\n)\n\nsegmentator1 = EsaDatasetSegmentator2(\n    transformations=[\"min\",\"max\",\"mean\",\"std\",\"var\",\"stft\",\"sc\",\"slope\",\"diff_var\"],\n    segment_duration=50, step_duration=10,\n    shapelet_miner=shapelet_miner1,\n    telecommands=False,\n    pooling_segment_len=200, pooling_segment_stride=20,\n    poolings=[\"max\",\"min\"],\n    run_id=\"esa_run\", exp_dir=\"experiments\",\n    use_shapelets=True,\n    save_csv=False\n)\n\nshapelet_miner2 = ShapeletMiner(\n        k_min_length=450,\n        k_max_length=450,\n        num_kernels=5,\n        segment_duration=500,\n        step_duration=100,\n        run_id=\"esa_training\", \n        exp_dir=\"experiments\",\n        skip=False\n    )\n\nsegmentator2 = EsaDatasetSegmentator2(\n        transformations=[\"min\", \"max\", \"mean\", \"std\", \"var\", \"stft\", \"sc\", \"slope\", \"diff_var\"],\n        segment_duration=500,\n        step_duration=100,\n        shapelet_miner=shapelet_miner2,\n        telecommands=False,\n        pooling_segment_len=20,\n        pooling_segment_stride=2,\n        poolings=[\"max\", \"min\"],\n        run_id=\"esa_training\", \n        exp_dir=\"experiments\", \n        use_shapelets=True,\n        save_csv=False\n    )\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:31:06.354425Z","iopub.execute_input":"2025-08-25T00:31:06.354755Z","iopub.status.idle":"2025-08-25T00:31:12.961411Z","shell.execute_reply.started":"2025-08-25T00:31:06.354724Z","shell.execute_reply":"2025-08-25T00:31:12.960419Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_file = \"/kaggle/input/esa-adb-challenge/test.parquet\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:31:12.962370Z","iopub.execute_input":"2025-08-25T00:31:12.962739Z","iopub.status.idle":"2025-08-25T00:31:12.967292Z","shell.execute_reply.started":"2025-08-25T00:31:12.962715Z","shell.execute_reply":"2025-08-25T00:31:12.966370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from spaceai.benchmark.esa_competition_predictor import ESACompetitionPredictor\nfrom spaceai.utils.tools import kernel_column_selector\nimport os\n\n\n# --- Modello A ---\npredictor_a = ESACompetitionPredictor(\n    \"/kaggle/input/model2/pytorch/default/3/inference_artifacts_500/esa_inference_500\",\n    segmentator2,\n    data_root=\"/kaggle/input/model2/pytorch/default/3/inference_artifacts_500/esa_inference_500\",\n)\nsub_a = predictor_a.predict(test_file, \"/kaggle/working/submission_a.csv\")  # deve restituire almeno 'id','is_anomaly'\n\n# --- Modello B ---\npredictor_b = ESACompetitionPredictor(\n    \"/kaggle/input/esa_predictor_artifacts/pytorch/default/4/inference_artifacts\",\n    segmentator1,\n    data_root=\"/kaggle/input/esa_predictor_artifacts/pytorch/default/4/inference_artifacts\",\n)\nsub_b = predictor_b.predict(test_file, \"/kaggle/working/submission_b.csv\")\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:31:12.968124Z","iopub.execute_input":"2025-08-25T00:31:12.968506Z","iopub.status.idle":"2025-08-25T00:31:13.369056Z","shell.execute_reply.started":"2025-08-25T00:31:12.968473Z","shell.execute_reply":"2025-08-25T00:31:13.367891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# --- Allineamento/robustezza sugli ID ---\n# Se i DF hanno già 'id' e 'is_anomaly':\nif {\"id\",\"is_anomaly\"}.issubset(sub_a.columns) and {\"id\",\"is_anomaly\"}.issubset(sub_b.columns):\n    A = sub_a[[\"id\",\"is_anomaly\"]].rename(columns={\"is_anomaly\":\"is_anomaly_a\"}).copy()\n    B = sub_b[[\"id\",\"is_anomaly\"]].rename(columns={\"is_anomaly\":\"is_anomaly_b\"}).copy()\nelse:\n    # fallback robusto: prendi l'ordine e gli id direttamente dal test file\n    test_ids = (pd.read_parquet(test_file) if test_file.endswith(\".parquet\")\n                else pd.read_csv(test_file))[[\"id\"]].copy()\n    A = test_ids.assign(is_anomaly_a=sub_a[\"is_anomaly\"].to_numpy())\n    B = test_ids.assign(is_anomaly_b=sub_b[\"is_anomaly\"].to_numpy())\n\n# Merge per 'id'\nfinal = A.merge(B, on=\"id\", how=\"inner\")\n\n# Ensemble OR logico\nfinal[\"is_anomaly\"] = (\n    (final[\"is_anomaly_a\"].astype(int) == 1) |\n    (final[\"is_anomaly_b\"].astype(int) == 1)\n).astype(int)\n\n# (opzionale) preserva esattamente l'ordine del test\ntest_ids = (pd.read_parquet(test_file) if test_file.endswith(\".parquet\")\n            else pd.read_csv(test_file))[[\"id\"]]\nfinal = test_ids.merge(final[[\"id\",\"is_anomaly\"]], on=\"id\", how=\"left\")\n\n\nfinal.to_csv(\"/kaggle/working/submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-25T00:31:13.370408Z","iopub.status.idle":"2025-08-25T00:31:13.370794Z","shell.execute_reply.started":"2025-08-25T00:31:13.370636Z","shell.execute_reply":"2025-08-25T00:31:13.370658Z"}},"outputs":[],"execution_count":null}]}