{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":97984,"databundleVersionId":14096757,"sourceType":"competition"}],"dockerImageVersionId":31236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Imports & Config","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:31:01.920119Z","iopub.execute_input":"2026-01-13T03:31:01.920569Z","iopub.status.idle":"2026-01-13T03:31:01.931088Z","shell.execute_reply.started":"2026-01-13T03:31:01.920539Z","shell.execute_reply":"2026-01-13T03:31:01.929625Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Test Metadata","metadata":{}},{"cell_type":"code","source":"TEST_CSV = \"/kaggle/input/physionet-ecg-image-digitization/test.csv\"\nSAMPLE_SUB = \"/kaggle/input/physionet-ecg-image-digitization/sample_submission.parquet\"\n\ntest_df = pd.read_csv(TEST_CSV)\nsample_sub = pd.read_parquet(SAMPLE_SUB)\n\nprint(\"Test rows:\", len(test_df))\nprint(\"Submission rows:\", len(sample_sub))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:31:04.269816Z","iopub.execute_input":"2026-01-13T03:31:04.270088Z","iopub.status.idle":"2026-01-13T03:31:04.540551Z","shell.execute_reply.started":"2026-01-13T03:31:04.270068Z","shell.execute_reply":"2026-01-13T03:31:04.539436Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ECG Length Helper","metadata":{}},{"cell_type":"code","source":"def expected_length(fs, lead):\n    if lead == \"II\":\n        return int(np.floor(fs * 10.0))\n    else:\n        return int(np.floor(fs * 2.5))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:31:27.009846Z","iopub.execute_input":"2026-01-13T03:31:27.010108Z","iopub.status.idle":"2026-01-13T03:31:27.015425Z","shell.execute_reply.started":"2026-01-13T03:31:27.010089Z","shell.execute_reply":"2026-01-13T03:31:27.014140Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Baseline ECG Generator","metadata":{}},{"cell_type":"code","source":"def generate_baseline_ecg(length, fs):\n    t = np.arange(length) / fs\n    \n    signal = 0.01 * np.sin(2 * np.pi * 1.2 * t)\n    \n    noise = 0.002 * np.random.randn(length)\n    \n    return signal + noise","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:31:45.030634Z","iopub.execute_input":"2026-01-13T03:31:45.030950Z","iopub.status.idle":"2026-01-13T03:31:45.037292Z","shell.execute_reply.started":"2026-01-13T03:31:45.030929Z","shell.execute_reply":"2026-01-13T03:31:45.036029Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Generate Predictions","metadata":{}},{"cell_type":"code","source":"records = []\n\nfor _, row in tqdm(test_df.iterrows(), total=len(test_df)):\n    base_id = row[\"id\"]\n    fs = row[\"fs\"]\n    lead = row[\"lead\"]\n    \n    length = expected_length(fs, lead)\n    signal = generate_baseline_ecg(length, fs)\n    \n    for i, val in enumerate(signal):\n        records.append({\n            \"id\": f\"{base_id}_{i}_{lead}\",\n            \"value\": float(val)\n        })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:31:47.989970Z","iopub.execute_input":"2026-01-13T03:31:47.990263Z","iopub.status.idle":"2026-01-13T03:31:48.157730Z","shell.execute_reply.started":"2026-01-13T03:31:47.990240Z","shell.execute_reply":"2026-01-13T03:31:48.156671Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create Submission File","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(records)\n\nsubmission = submission.set_index(\"id\").loc[sample_sub[\"id\"]].reset_index()\n\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:31:55.355212Z","iopub.execute_input":"2026-01-13T03:31:55.355481Z","iopub.status.idle":"2026-01-13T03:31:55.437090Z","shell.execute_reply.started":"2026-01-13T03:31:55.355463Z","shell.execute_reply":"2026-01-13T03:31:55.436147Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Save Submission","metadata":{}},{"cell_type":"code","source":"submission_path = \"submission.csv\"\nsubmission.to_csv(submission_path, index=False)\n\nprint(\"Saved:\", submission_path)\nprint(\"Rows:\", len(submission))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-13T03:32:04.090337Z","iopub.execute_input":"2026-01-13T03:32:04.090670Z","iopub.status.idle":"2026-01-13T03:32:04.210387Z","shell.execute_reply.started":"2026-01-13T03:32:04.090644Z","shell.execute_reply":"2026-01-13T03:32:04.209017Z"}},"outputs":[],"execution_count":null}]}