{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-03T08:44:46.386821Z","iopub.execute_input":"2023-07-03T08:44:46.387181Z","iopub.status.idle":"2023-07-03T08:44:46.416201Z","shell.execute_reply.started":"2023-07-03T08:44:46.387151Z","shell.execute_reply":"2023-07-03T08:44:46.415128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = Path('/kaggle/input/google-research-identify-contrails-reduce-global-warming')\ndata_path","metadata":{"execution":{"iopub.status.busy":"2023-07-03T08:44:46.418237Z","iopub.execute_input":"2023-07-03T08:44:46.418605Z","iopub.status.idle":"2023-07-03T08:44:46.427327Z","shell.execute_reply.started":"2023-07-03T08:44:46.418577Z","shell.execute_reply":"2023-07-03T08:44:46.426205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encode(x, fg_val=1):\n    dots = np.where(x.T.flatten() == fg_val)[0]\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if b > prev + 1:\n            run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\ndef list_to_string(x):\n    return '-'.join(map(str, x)) if x else '-'\n\ndef rle_decode(mask_rle, shape=(256, 256)):\n    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    if mask_rle != '-':\n        s = mask_rle.split()\n        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n        starts -= 1\n        ends = starts + lengths\n        for lo, hi in zip(starts, ends):\n            img[lo:hi] = 1\n    return img.reshape(shape, order='F')\n\ntest_recs = os.listdir(data_path / 'test')\nprint(test_recs)","metadata":{"execution":{"iopub.status.busy":"2023-07-03T08:44:46.428584Z","iopub.execute_input":"2023-07-03T08:44:46.428899Z","iopub.status.idle":"2023-07-03T08:44:46.447034Z","shell.execute_reply.started":"2023-07-03T08:44:46.428873Z","shell.execute_reply":"2023-07-03T08:44:46.446104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 999  # Modified value of 'n'\nband_08 = np.load(data_path / 'test' / test_recs[0] / 'band_08.npy').sum(axis=2)\npreds = np.unravel_index(np.argpartition(band_08.ravel(), -n)[-n:], band_08.shape)\nmask = np.zeros((256, 266))\nmask[preds[0], preds[1]] = 1\n\nplt.imshow(mask, cmap='Greys')\nplt.title(\"Obviously Not Contrails\", fontsize='16')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-03T08:44:46.450304Z","iopub.execute_input":"2023-07-03T08:44:46.450619Z","iopub.status.idle":"2023-07-03T08:44:46.774936Z","shell.execute_reply.started":"2023-07-03T08:44:46.450592Z","shell.execute_reply":"2023-07-03T08:44:46.773802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_to_string(rle_encode(mask))","metadata":{"execution":{"iopub.status.busy":"2023-07-03T08:44:46.776525Z","iopub.execute_input":"2023-07-03T08:44:46.776933Z","iopub.status.idle":"2023-07-03T08:44:46.783593Z","shell.execute_reply.started":"2023-07-03T08:44:46.776897Z","shell.execute_reply":"2023-07-03T08:44:46.782864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='record_id')\n\nfor rec in test_recs:\n    band_08 = np.load(data_path / 'test' / rec / 'band_08.npy').sum(axis=2)\n    preds = np.unravel_index(np.argpartition(band_08.ravel(), -n)[-n:], band_08.shape)\n    mask = np.zeros((256, 266))\n    mask[preds[0], preds[1]] = 1\n    submission.loc[int(rec), 'encoded_pixels'] = list_to_string(rle_encode(mask))\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-03T08:44:46.784746Z","iopub.execute_input":"2023-07-03T08:44:46.785214Z","iopub.status.idle":"2023-07-03T08:44:46.867733Z","shell.execute_reply.started":"2023-07-03T08:44:46.785187Z","shell.execute_reply":"2023-07-03T08:44:46.866546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-03T08:44:46.869908Z","iopub.execute_input":"2023-07-03T08:44:46.870212Z","iopub.status.idle":"2023-07-03T08:44:46.883577Z","shell.execute_reply.started":"2023-07-03T08:44:46.870189Z","shell.execute_reply":"2023-07-03T08:44:46.882321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}