{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":39763,"databundleVersionId":11756775,"sourceType":"competition"},{"sourceId":12328662,"sourceType":"datasetVersion","datasetId":7771543},{"sourceId":247897800,"sourceType":"kernelVersion"},{"sourceId":248180627,"sourceType":"kernelVersion"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pickle\nimport matplotlib.pyplot as plt\nimport pickle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:30.665850Z","iopub.execute_input":"2025-06-30T22:57:30.666160Z","iopub.status.idle":"2025-06-30T22:57:32.809530Z","shell.execute_reply.started":"2025-06-30T22:57:30.666130Z","shell.execute_reply":"2025-06-30T22:57:32.808659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_labels = pickle.load(open('/kaggle/input/gwi-preds-37-52-ds-to-notebook/val_labels.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:32.810776Z","iopub.execute_input":"2025-06-30T22:57:32.811382Z","iopub.status.idle":"2025-06-30T22:57:33.804380Z","shell.execute_reply.started":"2025-06-30T22:57:32.811358Z","shell.execute_reply":"2025-06-30T22:57:33.803541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_ensemble = pickle.load(open('/kaggle/input/gwi-ensemble-best-nobest-w-style-4-6-weights/val_ensemble.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:33.805280Z","iopub.execute_input":"2025-06-30T22:57:33.805566Z","iopub.status.idle":"2025-06-30T22:57:36.128420Z","shell.execute_reply.started":"2025-06-30T22:57:33.805538Z","shell.execute_reply":"2025-06-30T22:57:36.127549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_ensemble = val_ensemble*100+3000\nval_labels = val_labels*100+3000\nprint(np.max(val_labels))\nprint(np.min(val_labels))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:36.130016Z","iopub.execute_input":"2025-06-30T22:57:36.130264Z","iopub.status.idle":"2025-06-30T22:57:36.453821Z","shell.execute_reply.started":"2025-06-30T22:57:36.130243Z","shell.execute_reply":"2025-06-30T22:57:36.452826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble = val_ensemble\nsub_dataset_scores = []\nfor i in range(10):\n    score = np.mean(np.abs(ensemble-val_labels)[i*1000:(i+1)*1000])\n    print(score)\n    sub_dataset_scores.append(score)\nprint(np.mean(sub_dataset_scores))\nweighted_score = (np.sum(sub_dataset_scores[:4])*3+\n                  np.sum(sub_dataset_scores[4:8])*5.4+\n                  np.sum(sub_dataset_scores[8:10])*6.7)/(4*3+4*5.4+2*6.7)\nprint(weighted_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:36.454661Z","iopub.execute_input":"2025-06-30T22:57:36.454938Z","iopub.status.idle":"2025-06-30T22:57:38.824213Z","shell.execute_reply.started":"2025-06-30T22:57:36.454911Z","shell.execute_reply":"2025-06-30T22:57:38.823255Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds_labels = pickle.load(open('/kaggle/input/gwi-test-labels/test_preds.p', 'br'))\nval_preds_labels = pickle.load(open('/kaggle/input/gwi-test-labels/metrics_2.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:38.825204Z","iopub.execute_input":"2025-06-30T22:57:38.825773Z","iopub.status.idle":"2025-06-30T22:57:38.853574Z","shell.execute_reply.started":"2025-06-30T22:57:38.825744Z","shell.execute_reply":"2025-06-30T22:57:38.852689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_kind_labels = np.argmax(val_preds_labels, axis = 1)\nplt.plot(val_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T22:57:38.854671Z","iopub.execute_input":"2025-06-30T22:57:38.854988Z","iopub.status.idle":"2025-06-30T22:57:39.075411Z","shell.execute_reply.started":"2025-06-30T22:57:38.854960Z","shell.execute_reply":"2025-06-30T22:57:39.074401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def postprocess(ensemble, labels):\n    preds = np.zeros((ensemble.shape))\n    preds[labels<8] = np.round(ensemble[labels<8])\n    preds[labels>=8] = ensemble[labels>=8] \n    preds = np.where(preds<1500, 1500, preds)\n    preds = np.where(preds>4500.0, 4500.0, preds)\n    return preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:18:14.841639Z","iopub.execute_input":"2025-06-30T23:18:14.843172Z","iopub.status.idle":"2025-06-30T23:18:14.850141Z","shell.execute_reply.started":"2025-06-30T23:18:14.843134Z","shell.execute_reply":"2025-06-30T23:18:14.849159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble_int = postprocess(val_ensemble, val_kind_labels)\nensemble_int = np.where(ensemble_int<1500, 1500, ensemble_int)\nensemble_int = np.where(ensemble_int>4500.0, 4500.0, ensemble_int)\nsub_dataset_scores = []\nfor i in range(10):\n    score = np.mean(np.abs(ensemble_int-val_labels)[i*1000:(i+1)*1000])\n    print(score)\n    sub_dataset_scores.append(score)\nprint(np.mean(sub_dataset_scores))\nweighted_score = (np.sum(sub_dataset_scores[:4])*3+\n                  np.sum(sub_dataset_scores[4:8])*5.4+\n                  np.sum(sub_dataset_scores[8:10])*6.7)/(4*3+4*5.4+2*6.7)\nprint(weighted_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:18:24.599384Z","iopub.execute_input":"2025-06-30T23:18:24.599790Z","iopub.status.idle":"2025-06-30T23:18:28.872042Z","shell.execute_reply.started":"2025-06-30T23:18:24.599646Z","shell.execute_reply":"2025-06-30T23:18:28.871143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_max_list(sample):\n    unique, count = np.unique(sample, return_counts=True)\n    max_list = []\n    while (np.max(count)>-1):\n        max_idx = np.argmax(count)\n        max_list.append(unique[max_idx])\n        start = max_idx\n        while((start>0) and (count[start-1]<=count[start]) and (count[start-1]>-1) and (np.abs(unique[start]-unique[start-1])<=5)):\n            start-=1\n        end = max_idx\n        while((end<len(count)-1) and (count[end+1]<=count[end]) and (count[end+1]>-1) and (np.abs(unique[end]-unique[end+1])<=5)):\n            end+=1\n        unique[start:end+1] = -1\n        count[start:end+1] = -1\n    return np.sort(max_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:13:20.298016Z","iopub.execute_input":"2025-06-30T23:13:20.298592Z","iopub.status.idle":"2025-06-30T23:13:20.305309Z","shell.execute_reply.started":"2025-06-30T23:13:20.298566Z","shell.execute_reply":"2025-06-30T23:13:20.304374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:00:33.303791Z","iopub.execute_input":"2025-06-30T23:00:33.304167Z","iopub.status.idle":"2025-06-30T23:00:33.314104Z","shell.execute_reply.started":"2025-06-30T23:00:33.304145Z","shell.execute_reply":"2025-06-30T23:00:33.313249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_repaired_ensemble(ensemble_temp_int, ensemble_temp, labels):\n    repaired_ensemble = np.zeros((ensemble_temp_int.shape))\n    for i in tqdm(range(len(ensemble_temp_int))):\n        sample = ensemble_temp_int[i]\n        if labels[i]<8:\n            max_list = get_max_list(sample)\n            repaired_sample =  ensemble_temp[i]\n            repaired_sample = np.tile(repaired_sample[:,:,None], (1,1,len(max_list)))\n            max_list_expanded = np.tile(max_list[None,None,:], (70,70,1))\n            pixel_indices = np.argmin(np.abs(repaired_sample-max_list_expanded), axis = (2))\n            repaired_sample = max_list[pixel_indices]\n            repaired_ensemble[i] = repaired_sample\n        else:\n            repaired_ensemble[i] = sample\n    return repaired_ensemble","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:14:28.197760Z","iopub.execute_input":"2025-06-30T23:14:28.198933Z","iopub.status.idle":"2025-06-30T23:14:28.204796Z","shell.execute_reply.started":"2025-06-30T23:14:28.198895Z","shell.execute_reply":"2025-06-30T23:14:28.203813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"repaired_ensemble = get_repaired_ensemble(ensemble_int, ensemble, val_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:14:39.519590Z","iopub.execute_input":"2025-06-30T23:14:39.520192Z","iopub.status.idle":"2025-06-30T23:15:01.176354Z","shell.execute_reply.started":"2025-06-30T23:14:39.520166Z","shell.execute_reply":"2025-06-30T23:15:01.175533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_dataset_scores = []\nfor i in range(10):\n    score = np.mean(np.abs(repaired_ensemble-val_labels)[i*1000:(i+1)*1000])\n    print(score)\n    sub_dataset_scores.append(score)\nprint(np.mean(sub_dataset_scores))\nweighted_score = (np.sum(sub_dataset_scores[:4])*3+\n                  np.sum(sub_dataset_scores[4:8])*5.4+\n                  np.sum(sub_dataset_scores[8:10])*6.7)/(4*3+4*5.4+2*6.7)\nprint(weighted_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:15:03.795821Z","iopub.execute_input":"2025-06-30T23:15:03.796172Z","iopub.status.idle":"2025-06-30T23:15:05.758283Z","shell.execute_reply.started":"2025-06-30T23:15:03.796148Z","shell.execute_reply":"2025-06-30T23:15:05.757484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"0.42313061224489795\n0.8543636734693878\n1.96833\n7.04314612244898\n1.4077892592075893\n3.2069822448979592\n2.5581527343251755\n15.670483469387754\n8.968485152637975\n22.81117491817942\n6.491203818679914\n7.81159649619545","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pickle.load(open('/kaggle/input/gwi-ensemble-best-nobest-w-style-4-6-weights/test_ensemble.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:22:32.548566Z","iopub.execute_input":"2025-06-30T23:22:32.548940Z","iopub.status.idle":"2025-06-30T23:22:34.416112Z","shell.execute_reply.started":"2025-06-30T23:22:32.548914Z","shell.execute_reply":"2025-06-30T23:22:34.415142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(np.max(test))\nprint(np.min(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:21:14.148978Z","iopub.execute_input":"2025-06-30T23:21:14.149270Z","iopub.status.idle":"2025-06-30T23:21:14.817332Z","shell.execute_reply.started":"2025-06-30T23:21:14.149251Z","shell.execute_reply":"2025-06-30T23:21:14.816475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(test[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:19:21.754947Z","iopub.execute_input":"2025-06-30T23:19:21.755228Z","iopub.status.idle":"2025-06-30T23:19:21.951779Z","shell.execute_reply.started":"2025-06-30T23:19:21.755207Z","shell.execute_reply":"2025-06-30T23:19:21.950953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/waveform-inversion/sample_submission.csv')\nsample_submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:19:41.580283Z","iopub.execute_input":"2025-06-30T23:19:41.580581Z","iopub.status.idle":"2025-06-30T23:20:02.794759Z","shell.execute_reply.started":"2025-06-30T23:19:41.580551Z","shell.execute_reply":"2025-06-30T23:20:02.794062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_kind_labels = np.argmax(test_preds_labels, axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:20:36.875015Z","iopub.execute_input":"2025-06-30T23:20:36.875335Z","iopub.status.idle":"2025-06-30T23:20:36.884108Z","shell.execute_reply.started":"2025-06-30T23:20:36.875315Z","shell.execute_reply":"2025-06-30T23:20:36.883273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(test_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:20:41.325406Z","iopub.execute_input":"2025-06-30T23:20:41.326232Z","iopub.status.idle":"2025-06-30T23:20:41.475728Z","shell.execute_reply.started":"2025-06-30T23:20:41.326203Z","shell.execute_reply":"2025-06-30T23:20:41.474919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = test*100+3000\ntest_int = postprocess(test, test_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:22:40.463275Z","iopub.execute_input":"2025-06-30T23:22:40.463566Z","iopub.status.idle":"2025-06-30T23:22:47.523261Z","shell.execute_reply.started":"2025-06-30T23:22:40.463545Z","shell.execute_reply":"2025-06-30T23:22:47.522431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(np.max(test))\nprint(np.min(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:21:27.342133Z","iopub.execute_input":"2025-06-30T23:21:27.342452Z","iopub.status.idle":"2025-06-30T23:21:27.997497Z","shell.execute_reply.started":"2025-06-30T23:21:27.342425Z","shell.execute_reply":"2025-06-30T23:21:27.996516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = get_repaired_ensemble(test_int, test, test_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:23:23.017155Z","iopub.execute_input":"2025-06-30T23:23:23.017673Z","iopub.status.idle":"2025-06-30T23:25:31.191026Z","shell.execute_reply.started":"2025-06-30T23:23:23.017649Z","shell.execute_reply":"2025-06-30T23:25:31.190235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(test[0])\nprint(np.min(test[0]))\nprint(np.max(test[0]))\nprint(np.max(test))\nprint(np.min(test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:25:58.563637Z","iopub.execute_input":"2025-06-30T23:25:58.563985Z","iopub.status.idle":"2025-06-30T23:25:59.477786Z","shell.execute_reply.started":"2025-06-30T23:25:58.563954Z","shell.execute_reply":"2025-06-30T23:25:59.476964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_preds = np.zeros((4607260,35))\nbatch_size = 128\nx_odds = np.asarray(range(1,71,2))\nfor i in range(65818//batch_size+1):\n    preds = test[i*batch_size:(i+1)*batch_size]\n    all_preds[i*70*batch_size:(i+1)*70*batch_size] = np.reshape(np.asarray(preds)[:, :, x_odds], [-1,35])\npickle.dump(test, open('test_ensemble.p', 'bw'))\nsample_submission.iloc[:, -35:] = all_preds\nsample_submission.to_parquet('submission.parquet', index=False)\nsubmission = pd.read_parquet('/kaggle/working/submission.parquet')\nplt.imshow(np.asarray(submission[:70])[:, 1:].astype(float))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:26:06.769396Z","iopub.execute_input":"2025-06-30T23:26:06.769712Z","iopub.status.idle":"2025-06-30T23:26:38.942147Z","shell.execute_reply.started":"2025-06-30T23:26:06.769686Z","shell.execute_reply":"2025-06-30T23:26:38.941204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.to_csv('submission_csv.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T23:26:38.943366Z","iopub.execute_input":"2025-06-30T23:26:38.943610Z","iopub.status.idle":"2025-06-30T23:26:39.562390Z","shell.execute_reply.started":"2025-06-30T23:26:38.943590Z","shell.execute_reply":"2025-06-30T23:26:39.561570Z"}},"outputs":[],"execution_count":null}]}