{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\n\ndef rle_encode(image, foreground_value=1):\n    # Run length encoding.\n    dots = np.where(image.T.flatten() == foreground_value)[0]\n    run_lengths = []\n    prev = -2\n    for dot in dots:\n        if (dot > prev + 1): run_lengths.extend((dot + 1, 0))\n        run_lengths[-1] += 1\n        prev = dot\n    return run_lengths\n\ndef rle_to_string(rle):\n    # Converts RLE to a string.\n    return '-'.join(map(str, rle)) if rle else '-'\n\ndef create_mask(data_path, record, num_pixels, shape=(256, 256)):\n    # Creates a mask for the given record.\n    band_08 = np.load(data_path / 'test' / record / 'band_08.npy').sum(axis=2)\n    preds = np.c_[np.unravel_index(np.argpartition(band_08.ravel(), -num_pixels)[-num_pixels:], band_08.shape)]\n    mask = np.zeros(shape)\n    mask[preds[:, 0], preds[:, 1]] = 1\n    return mask\n\ndef visualize_mask(mask):\n    # Visualizes the mask using matplotlib.\n    plt.imshow(mask, cmap='Greys')\n    plt.title(\"Obviously Not Contrails\")\n    plt.show()\n\ndef prepare_submission(submission, record, mask):\n    # Prepares and updates the submission DataFrame.\n    rle_mask = rle_encode(mask)\n    submission.loc[int(record), 'encoded_pixels'] = rle_to_string(rle_mask)\n\ndef main(data_path, num_plots=1):\n    # Main function.\n    data_path = Path(data_path)\n    test_records = [entry.name for entry in data_path.glob('test/*') if entry.is_dir()]\n    num_pixels = 1000\n\n    submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='record_id')\n\n    for i, record in enumerate(test_records):\n        mask = create_mask(data_path, record, num_pixels)\n        if i < num_plots:  # Visualize the masks for the first 'num_plots' records\n            visualize_mask(mask)\n        prepare_submission(submission, record, mask)\n\n    submission.to_csv('submission.csv')\n\n# Directly call main with your data_path and num_plots as arguments\nmain('/kaggle/input/google-research-identify-contrails-reduce-global-warming', num_plots=2)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T03:19:14.391266Z","iopub.execute_input":"2023-07-11T03:19:14.391704Z","iopub.status.idle":"2023-07-11T03:19:15.094096Z","shell.execute_reply.started":"2023-07-11T03:19:14.391674Z","shell.execute_reply":"2023-07-11T03:19:15.092951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Contrail Detection Submission with Run-Length Encoding (RLE)\n- The script imports necessary libraries such as `numpy`, `pandas`, and `pathlib`.\n- Defines several functions to perform RLE, create and visualize masks, and prepare the submission `DataFrame`.\n- The main function calls these functions and saves the submission in a CSV file.\n\n## What is RLE?\n- Run-length encoding (RLE) is a form of lossless data compression.\n- It stores runs of data as a single data value and count.\n- For example, a string of characters `AAABBCCCC` can be compressed to `3A2B4C`.\n- RLE excels at compressing data with runs: numerous consecutive repetitions.\n\n## Handling Empty Predictions\n- Empty predictions are handled by the `rle_to_string` function.\n- If the input RLE mask is `empty`, the function returns a string containing a single dash (`'-'`).\n\n# Acknowledgement\n- This script is inspired by [INVERSION’s notebook](https://www.kaggle.com/code/inversion/contrails-rle-submission) on Kaggle.\n- GitHub: To run this code locally, visit [src/utils/rle_encoding_submission.py](https://github.com/patmejia/contrails-vision/blob/main/src/utils/rle_encoding_submission.py).","metadata":{}},{"cell_type":"markdown","source":"<div style=\"background-color: #f2f2f2; padding: 53px; border-radius: 5px;\">\n  <h3>If you found this notebook helpful...</h3>\n  <p>\n  Please consider giving it a star. Your support helps me continue to develop high-quality code and pursue my career as a data analyst/engineer. Feedback is always welcome and appreciated. Thank you for taking the time to read my work! \n  </p> \n  <h4>\n  <p style=\"text-align: right;\">\n  <a href=\"https://github.com/patmejia\"> - pat [¬º-°]¬ </a>\n  </h4>\n  </p>\n</div>","metadata":{}}]}