{"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":"markdown","source":"# Time Step Visualization\nShows the contrail sequence in html with soft labels at t=0 (false_color list entry 4).\nAdapted from [this EDA](https://www.kaggle.com/code/leonidkulyk/eda-ic-to-rgw-preprint-insights-vis).","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import os\nimport math\nfrom IPython import display\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:05:58.287143Z","iopub.execute_input":"2023-08-21T12:05:58.287557Z","iopub.status.idle":"2023-08-21T12:05:58.29397Z","shell.execute_reply.started":"2023-08-21T12:05:58.287525Z","shell.execute_reply":"2023-08-21T12:05:58.291875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    train_path: str = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/train\"\n    validation_path: str = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/validation\"\n    test_path: str = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/test\"","metadata":{"execution":{"iopub.status.busy":"2023-08-21T12:05:58.308772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_record(record_id, directory):\n    record_data = {}\n    for x in [\n        \"band_11\", \n        \"band_14\", \n        \"band_15\", \n        \"human_pixel_masks\", \n        \"human_individual_masks\"\n    ]:\n        try:\n            with open(os.path.join(directory, record_id, x + \".npy\"), 'rb') as f:\n                record_data[x] = np.load(f)\n        except Exception as e:\n            pass\n    \n    return record_data\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\n\ndef get_false_color(record_data):\n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n\n    r = normalize_range(record_data[\"band_15\"] - record_data[\"band_14\"], _TDIFF_BOUNDS)\n    g = normalize_range(record_data[\"band_14\"] - record_data[\"band_11\"], _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(record_data[\"band_14\"], _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    \n    return false_color\n\n\ndef draw(i):\n    im.set_array(false_color[..., i])\n    return [im]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def false_color_gen(record_id):\n    N_TIMES_BEFORE = 4\n    record_data = read_record(str(record_id), CFG.train_path)\n    false_color = get_false_color(record_data)\n    false_color[:,:,0,4] += record_data[\"human_individual_masks\"].squeeze().mean(axis=2)\n    return false_color","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"List of good_examples:\n* 1000603527582775543\n* 7829917977180135058\n* 3727988927537047897\n* 7052120479722658749","metadata":{"execution":{"iopub.status.busy":"2023-08-21T11:56:31.795463Z","iopub.execute_input":"2023-08-21T11:56:31.795958Z","iopub.status.idle":"2023-08-21T11:56:31.805091Z","shell.execute_reply.started":"2023-08-21T11:56:31.795894Z","shell.execute_reply":"2023-08-21T11:56:31.803267Z"}}},{"cell_type":"code","source":"false_color = false_color_gen(1000603527582775543)\n\nfig = plt.figure(figsize=(6, 6))\nim = plt.imshow(false_color[..., 0])\nanim = animation.FuncAnimation(fig, draw, frames=false_color.shape[-1], interval=500, blit=True)\nplt.close()\ndisplay.HTML(anim.to_jshtml())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"false_color = false_color_gen(7829917977180135058)\n\nfig = plt.figure(figsize=(6, 6))\nim = plt.imshow(false_color[..., 0])\nanim = animation.FuncAnimation(fig, draw, frames=false_color.shape[-1], interval=500, blit=True)\nplt.close()\ndisplay.HTML(anim.to_jshtml())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"false_color = false_color_gen(3727988927537047897)\n\nfig = plt.figure(figsize=(6, 6))\nim = plt.imshow(false_color[..., 0])\nanim = animation.FuncAnimation(fig, draw, frames=false_color.shape[-1], interval=500, blit=True)\nplt.close()\ndisplay.HTML(anim.to_jshtml())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"false_color = false_color_gen(7052120479722658749)\n\nfig = plt.figure(figsize=(6, 6))\nim = plt.imshow(false_color[..., 0])\nanim = animation.FuncAnimation(fig, draw, frames=false_color.shape[-1], interval=500, blit=True)\nplt.close()\ndisplay.HTML(anim.to_jshtml())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}