{"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 os\nimport random\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import  animation\nfrom IPython import display","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:44:56.927423Z","iopub.execute_input":"2023-05-27T03:44:56.928318Z","iopub.status.idle":"2023-05-27T03:44:56.973378Z","shell.execute_reply.started":"2023-05-27T03:44:56.928261Z","shell.execute_reply":"2023-05-27T03:44:56.972141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR='/kaggle/input/google-research-identify-contrails-reduce-global-warming'\ntrain_dir=BASE_DIR+'/train'\nval_dir=BASE_DIR+'/validation'\ntest_dir=BASE_DIR+'/test'\nos.listdir(BASE_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:44:58.566168Z","iopub.execute_input":"2023-05-27T03:44:58.568044Z","iopub.status.idle":"2023-05-27T03:44:58.584463Z","shell.execute_reply.started":"2023-05-27T03:44:58.567874Z","shell.execute_reply":"2023-05-27T03:44:58.582412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta=pd.read_json(BASE_DIR+'/train_metadata.json')\nval_meta=pd.read_json(BASE_DIR+'/validation_metadata.json')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:44:59.215463Z","iopub.execute_input":"2023-05-27T03:44:59.217216Z","iopub.status.idle":"2023-05-27T03:44:59.661919Z","shell.execute_reply.started":"2023-05-27T03:44:59.21714Z","shell.execute_reply":"2023-05-27T03:44:59.66034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:44:59.664088Z","iopub.execute_input":"2023-05-27T03:44:59.664501Z","iopub.status.idle":"2023-05-27T03:44:59.697363Z","shell.execute_reply.started":"2023-05-27T03:44:59.664461Z","shell.execute_reply":"2023-05-27T03:44:59.696081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:44:59.825326Z","iopub.execute_input":"2023-05-27T03:44:59.825741Z","iopub.status.idle":"2023-05-27T03:44:59.843733Z","shell.execute_reply.started":"2023-05-27T03:44:59.825704Z","shell.execute_reply":"2023-05-27T03:44:59.842256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getRandomPath():\n    dir_in_train=os.listdir(train_dir)\n    hasilRandomAngka = random.randint(0,len(dir_in_train))\n    randomNum = dir_in_train[hasilRandomAngka]\n    randomPath = train_dir+'/'+randomNum\n    return randomPath\ngetRandomPath()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:00.376425Z","iopub.execute_input":"2023-05-27T03:45:00.37701Z","iopub.status.idle":"2023-05-27T03:45:00.588724Z","shell.execute_reply.started":"2023-05-27T03:45:00.376953Z","shell.execute_reply":"2023-05-27T03:45:00.586984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bands=['08','09','10','11','12','13','14','15','16']\ncBand=['red','green','blue','black','purple','violet','tomato','plum','aqua']","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:00.616933Z","iopub.execute_input":"2023-05-27T03:45:00.6174Z","iopub.status.idle":"2023-05-27T03:45:00.62514Z","shell.execute_reply.started":"2023-05-27T03:45:00.617357Z","shell.execute_reply":"2023-05-27T03:45:00.623555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = getRandomPath()\npath='/kaggle/input/google-research-identify-contrails-reduce-global-warming/train/8190132030831129099'\n# print(path)\nfor i, band in enumerate(bands):\n    plot = np.load(f'{path}/band_{band}.npy')\n    plt.plot(plot[0], color=cBand[i], label=f'Band {band}')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:00.822059Z","iopub.execute_input":"2023-05-27T03:45:00.82249Z","iopub.status.idle":"2023-05-27T03:45:01.688718Z","shell.execute_reply.started":"2023-05-27T03:45:00.822454Z","shell.execute_reply":"2023-05-27T03:45:01.686774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b8 = np.load(path+'/band_08.npy')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:01.69112Z","iopub.execute_input":"2023-05-27T03:45:01.691854Z","iopub.status.idle":"2023-05-27T03:45:01.702176Z","shell.execute_reply.started":"2023-05-27T03:45:01.691799Z","shell.execute_reply":"2023-05-27T03:45:01.700344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b8.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:01.703657Z","iopub.execute_input":"2023-05-27T03:45:01.705428Z","iopub.status.idle":"2023-05-27T03:45:01.719185Z","shell.execute_reply.started":"2023-05-27T03:45:01.705372Z","shell.execute_reply":"2023-05-27T03:45:01.717222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(b8[...,0])\nplt.title(f'band 8 from {path}')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:03.083868Z","iopub.execute_input":"2023-05-27T03:45:03.084332Z","iopub.status.idle":"2023-05-27T03:45:03.476879Z","shell.execute_reply.started":"2023-05-27T03:45:03.08429Z","shell.execute_reply":"2023-05-27T03:45:03.475412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1,8,figsize=(16,16))\nplt.axis('off')\nprint('band 8')\n# for j in range(2)\nfor i in range(8):\n    axs[i].axis('off')\n    axs[i].imshow(b8[...,i])\n    axs[i].set_title(f\"Time Step {i+1}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:03.479648Z","iopub.execute_input":"2023-05-27T03:45:03.480061Z","iopub.status.idle":"2023-05-27T03:45:04.152445Z","shell.execute_reply.started":"2023-05-27T03:45:03.480022Z","shell.execute_reply":"2023-05-27T03:45:04.150704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axs=plt.subplots(8,len(bands),figsize=(32,32))\nfor i, band in enumerate(bands):\n    img = np.load(f'{path}/band_{band}.npy')\n    for j in range(8):\n        axs[j,i].axis('off')\n        axs[j,i].imshow(img[...,j])\n        axs[j,i].set_title(f'band {band} \\n sequence {j+1}')\n# plt.savefig('full.png')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:04.154064Z","iopub.execute_input":"2023-05-27T03:45:04.154428Z","iopub.status.idle":"2023-05-27T03:45:14.702509Z","shell.execute_reply.started":"2023-05-27T03:45:04.154391Z","shell.execute_reply":"2023-05-27T03:45:14.701317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"him=np.load(f'{path}/human_individual_masks.npy')\nhpm=np.load(f'{path}/human_pixel_masks.npy')\n\nhim.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:14.705233Z","iopub.execute_input":"2023-05-27T03:45:14.705975Z","iopub.status.idle":"2023-05-27T03:45:14.746502Z","shell.execute_reply.started":"2023-05-27T03:45:14.705926Z","shell.execute_reply":"2023-05-27T03:45:14.745397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hpm.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:14.748206Z","iopub.execute_input":"2023-05-27T03:45:14.748688Z","iopub.status.idle":"2023-05-27T03:45:14.757407Z","shell.execute_reply.started":"2023-05-27T03:45:14.748642Z","shell.execute_reply":"2023-05-27T03:45:14.75615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1,4,figsize=(8,8))\nplt.axis('off')\n# plt.title('Human Individual Mask')\nprint('Human Individual Mask')\n# for j in range(2)\nfor i in range(4):\n    axs[i].axis('off')\n    axs[i].imshow(him[...,i])\n#     axs[i].set_title(f\"Time Step {i+1}\")","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:14.759071Z","iopub.execute_input":"2023-05-27T03:45:14.759886Z","iopub.status.idle":"2023-05-27T03:45:15.021249Z","shell.execute_reply.started":"2023-05-27T03:45:14.759838Z","shell.execute_reply":"2023-05-27T03:45:15.019394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(hpm)\nplt.title('Human Pixel Mask')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:15.023602Z","iopub.execute_input":"2023-05-27T03:45:15.026519Z","iopub.status.idle":"2023-05-27T03:45:15.338704Z","shell.execute_reply.started":"2023-05-27T03:45:15.026405Z","shell.execute_reply":"2023-05-27T03:45:15.337153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_range(data, bounds):\n    \"\"\"Maps data to the range [0, 1].\"\"\"\n    return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef get_false_color(example_id, split_dir):\n    \"\"\"\n    Args:\n        example_id(str): The id of the example i.e. '1000216489776414077'\n        split_dir(str): The split directoryu i.e. 'test', 'train', 'val'\n    \"\"\"\n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n\n    band15 = np.load(BASE_DIR + f\"/{split_dir}/{example_id}/band_15.npy\")\n    band14 = np.load(BASE_DIR + f\"/{split_dir}/{example_id}/band_14.npy\")\n    band11 = np.load(BASE_DIR + f\"/{split_dir}/{example_id}/band_11.npy\")\n\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    \n    return false_color\n\ndef plot_false_color(example_id, split_dir):\n\n    false_color = get_false_color(example_id, split_dir)\n\n    img = false_color[..., 4] # 5th image corresponds to ground truth\n    ground_truth = np.load(BASE_DIR + f\"/{split_dir}/{example_id}/human_pixel_masks.npy\")\n    \n    fig, axs = plt.subplots(1, 3, figsize=(16, 8))\n    \n    axs[0].imshow(img)\n    axs[0].set_title(\"False Color Image\")\n    \n    axs[1].imshow(ground_truth)\n    axs[1].set_title(\"Ground Truth\")\n    \n    axs[2].imshow(img)\n    axs[2].imshow(ground_truth, cmap='Reds', alpha=.3, interpolation='none')\n    axs[2].set_title('Contrail mask on false color image')\n    \n    \n    plt.tight_layout() \n    plt.show()\n\n    \nplot_false_color('8190132030831129099', 'train')","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:15.341307Z","iopub.execute_input":"2023-05-27T03:45:15.341869Z","iopub.status.idle":"2023-05-27T03:45:16.853544Z","shell.execute_reply.started":"2023-05-27T03:45:15.341814Z","shell.execute_reply":"2023-05-27T03:45:16.852159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_animation(example_id, split_dir):\n    def draw(i):\n        im.set_array(false_color[..., i])\n        return [im]\n    \n    false_color = get_false_color(example_id, split_dir)\n    \n    fig = plt.figure(figsize=(6, 6))\n    im = plt.imshow(false_color[..., 0])\n    \n    anim = animation.FuncAnimation(\n        fig, draw, frames=false_color.shape[-1], interval=500, blit=True\n    )\n    \n    plt.close()\n    return anim \n\nanim = draw_animation('8190132030831129099', 'train')\ndisplay.HTML(anim.to_jshtml())","metadata":{"execution":{"iopub.status.busy":"2023-05-27T03:45:16.856812Z","iopub.execute_input":"2023-05-27T03:45:16.857213Z","iopub.status.idle":"2023-05-27T03:45:19.432972Z","shell.execute_reply.started":"2023-05-27T03:45:16.857174Z","shell.execute_reply":"2023-05-27T03:45:19.431619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}