{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":51753,"databundleVersionId":5692552,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nnp.random.seed(0)\n\nNUM_IMGS = float('inf')\nBASE_DIR = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/train\"\nSAVE_DIR = \"/kaggle/working/\"\nN_TIMES_BEFORE = 4\n\n_T11_BOUNDS = (243, 303)\n_CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n_TDIFF_BOUNDS = (-4, 2)\n\n'''\nFunctions to create false images\nTaken from this notebook: https://www.kaggle.com/code/inversion/visualizing-contrails\n'''\n\ndef normalize_range(data, bounds):\n    \"\"\"Maps data to the range [0, 1].\"\"\"\n    return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef create_false_color(band11, band14, band15):\n    r = normalize_range(band15 - band14, _TDIFF_BOUNDS)\n    g = normalize_range(band14 - band11, _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(band14, _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    return false_color\n\ndef open_band(record_id, file):\n    with open(os.path.join(BASE_DIR, record_id, file), 'rb') as f:\n        band = np.load(f)\n        return band\n\n'''\nLoad directory and images, then save\n'''\n\n\n# get and load directories\nrecord_ids = os.listdir(BASE_DIR)[:-1]\nrecord_ids = record_ids[: min(NUM_IMGS, len(record_ids))]\n\nprint(len(record_ids))\n\nstart = 0\nend = 8000\nprint(f\"{start} - {end} train data\")\ncurr_save_dir = os.path.join(SAVE_DIR, f\"training_{start}_{end}\")\nos.mkdir(curr_save_dir)\n\nfor record_id in tqdm(record_ids[start:end + 1]):\n    \n    # load relevant bands\n    band11 = open_band(record_id, \"band_11.npy\")\n    band14 = open_band(record_id, \"band_14.npy\")\n    band15 = open_band(record_id, \"band_15.npy\")\n    true_mask = open_band(record_id, \"human_pixel_masks.npy\")\n\n    # create false color mask using band, then only select desired image\n    false_color = create_false_color(band11, band14, band15)\n    false_color = false_color[..., N_TIMES_BEFORE]\n    \n    # save images\n    np.save(os.path.join(curr_save_dir, f'{record_id}.npy'), false_color)\n    np.save(os.path.join(curr_save_dir, f'{record_id}_mask.npy'), true_mask)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-04T23:29:27.211386Z","iopub.execute_input":"2023-12-04T23:29:27.212012Z","iopub.status.idle":"2023-12-04T23:30:25.55622Z","shell.execute_reply.started":"2023-12-04T23:29:27.211958Z","shell.execute_reply":"2023-12-04T23:30:25.553149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}