{"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":"# Contrails Data View\n## add animation gif","metadata":{"papermill":{"duration":0.012355,"end_time":"2023-05-22T04:55:53.771606","exception":false,"start_time":"2023-05-22T04:55:53.759251","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"![](https://storage.googleapis.com/kaggle-media/competitions/Google-Contrails/waterdroplets.png)","metadata":{"papermill":{"duration":0.010893,"end_time":"2023-05-22T04:55:53.793633","exception":false,"start_time":"2023-05-22T04:55:53.78274","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"Contrails, short for \"condensation trails,\" are long, thin, and visible cloud-like trails that form behind aircraft as they fly through the air. Contrails are composed of ice crystals or water droplets that condense and freeze around particles in the aircraft's exhaust gases, such as soot or sulfur compounds.","metadata":{"papermill":{"duration":0.010527,"end_time":"2023-05-22T04:55:53.815686","exception":false,"start_time":"2023-05-22T04:55:53.805159","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:53.840964Z","iopub.status.busy":"2023-05-22T04:55:53.840103Z","iopub.status.idle":"2023-05-22T04:55:54.079421Z","shell.execute_reply":"2023-05-22T04:55:54.078451Z"},"papermill":{"duration":0.254751,"end_time":"2023-05-22T04:55:54.082237","exception":false,"start_time":"2023-05-22T04:55:53.827486","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):\n    fig=plt.figure(figsize=(3,3))\n    plt.axis('off')\n    im=plt.imshow(ims[0])\n    plt.tight_layout()\n    plt.close()    \n    def animate_func(i):\n        im.set_array(ims[i]) \n        return [im]\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000/2)   ","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:54.106703Z","iopub.status.busy":"2023-05-22T04:55:54.10601Z","iopub.status.idle":"2023-05-22T04:55:54.11257Z","shell.execute_reply":"2023-05-22T04:55:54.111658Z"},"papermill":{"duration":0.021916,"end_time":"2023-05-22T04:55:54.115187","exception":false,"start_time":"2023-05-22T04:55:54.093271","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data ","metadata":{"papermill":{"duration":0.010804,"end_time":"2023-05-22T04:55:54.13712","exception":false,"start_time":"2023-05-22T04:55:54.126316","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Slide Show","metadata":{"papermill":{"duration":0.011877,"end_time":"2023-05-22T04:55:54.159951","exception":false,"start_time":"2023-05-22T04:55:54.148074","status":"completed"},"tags":[]}},{"cell_type":"code","source":"pathid='/kaggle/input/google-research-identify-contrails-reduce-global-warming/train/1000603527582775543/'","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:54.185984Z","iopub.status.busy":"2023-05-22T04:55:54.185515Z","iopub.status.idle":"2023-05-22T04:55:54.192279Z","shell.execute_reply":"2023-05-22T04:55:54.190639Z"},"papermill":{"duration":0.024344,"end_time":"2023-05-22T04:55:54.195765","exception":false,"start_time":"2023-05-22T04:55:54.171421","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_08.npy'\nbandi=np.load(pathi)\nimagesi0=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi0)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:54.2207Z","iopub.status.busy":"2023-05-22T04:55:54.220263Z","iopub.status.idle":"2023-05-22T04:55:54.942126Z","shell.execute_reply":"2023-05-22T04:55:54.941083Z"},"papermill":{"duration":0.746922,"end_time":"2023-05-22T04:55:54.953824","exception":false,"start_time":"2023-05-22T04:55:54.206902","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_09.npy'\nbandi=np.load(pathi)\nimagesi1=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi1)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:55.012224Z","iopub.status.busy":"2023-05-22T04:55:55.011066Z","iopub.status.idle":"2023-05-22T04:55:55.684063Z","shell.execute_reply":"2023-05-22T04:55:55.682729Z"},"papermill":{"duration":0.713722,"end_time":"2023-05-22T04:55:55.695356","exception":false,"start_time":"2023-05-22T04:55:54.981634","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_10.npy'\nbandi=np.load(pathi)\nimagesi2=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi2)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:55.789925Z","iopub.status.busy":"2023-05-22T04:55:55.789415Z","iopub.status.idle":"2023-05-22T04:55:56.460477Z","shell.execute_reply":"2023-05-22T04:55:56.45915Z"},"papermill":{"duration":0.731582,"end_time":"2023-05-22T04:55:56.473042","exception":false,"start_time":"2023-05-22T04:55:55.74146","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_11.npy'\nbandi=np.load(pathi)\nimagesi3=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi3)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:56.606124Z","iopub.status.busy":"2023-05-22T04:55:56.605341Z","iopub.status.idle":"2023-05-22T04:55:57.363586Z","shell.execute_reply":"2023-05-22T04:55:57.362514Z"},"papermill":{"duration":0.846216,"end_time":"2023-05-22T04:55:57.382986","exception":false,"start_time":"2023-05-22T04:55:56.53677","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_12.npy'\nbandi=np.load(pathi)\nimagesi4=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi4)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:57.594198Z","iopub.status.busy":"2023-05-22T04:55:57.593338Z","iopub.status.idle":"2023-05-22T04:55:58.373008Z","shell.execute_reply":"2023-05-22T04:55:58.370836Z"},"papermill":{"duration":0.901118,"end_time":"2023-05-22T04:55:58.38922","exception":false,"start_time":"2023-05-22T04:55:57.488102","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_13.npy'\nbandi=np.load(pathi)\nimagesi5=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi5)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:58.661436Z","iopub.status.busy":"2023-05-22T04:55:58.66103Z","iopub.status.idle":"2023-05-22T04:55:59.567084Z","shell.execute_reply":"2023-05-22T04:55:59.566015Z"},"papermill":{"duration":1.061664,"end_time":"2023-05-22T04:55:59.584749","exception":false,"start_time":"2023-05-22T04:55:58.523085","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_14.npy'\nbandi=np.load(pathi)\nimagesi6=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi6)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:55:59.920241Z","iopub.status.busy":"2023-05-22T04:55:59.919828Z","iopub.status.idle":"2023-05-22T04:56:00.677976Z","shell.execute_reply":"2023-05-22T04:56:00.676359Z"},"papermill":{"duration":0.947818,"end_time":"2023-05-22T04:56:00.696794","exception":false,"start_time":"2023-05-22T04:55:59.748976","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_15.npy'\nbandi=np.load(pathi)\nimagesi7=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi7)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:01.101407Z","iopub.status.busy":"2023-05-22T04:56:01.100503Z","iopub.status.idle":"2023-05-22T04:56:01.847236Z","shell.execute_reply":"2023-05-22T04:56:01.845992Z"},"papermill":{"duration":0.965874,"end_time":"2023-05-22T04:56:01.865319","exception":false,"start_time":"2023-05-22T04:56:00.899445","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pathi=pathid+'band_16.npy'\nbandi=np.load(pathi)\nimagesi8=[bandi[:,:,i] for i in range(bandi.shape[2])]\nprint(pathi.split('/')[-2],pathi.split('/')[-1][0:-4])\ncreate_animation(imagesi8)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:02.338652Z","iopub.status.busy":"2023-05-22T04:56:02.33797Z","iopub.status.idle":"2023-05-22T04:56:03.066903Z","shell.execute_reply":"2023-05-22T04:56:03.065527Z"},"papermill":{"duration":0.987196,"end_time":"2023-05-22T04:56:03.083958","exception":false,"start_time":"2023-05-22T04:56:02.096762","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Animation Gif","metadata":{"papermill":{"duration":0.263249,"end_time":"2023-05-22T04:56:03.602802","exception":false,"start_time":"2023-05-22T04:56:03.339553","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport imageio\n\nnp0=np.load(pathi)\nnp0=np0.reshape(256,256,8)\nframes=np.transpose(np0,(2,0,1))\n\nfor i, frame in enumerate(frames):\n    plt.imsave(f\"frame_{i}.png\", frame)\n\nimages = []\nfor i in range(8):\n    image = Image.open(f\"frame_{i}.png\")\n    images.append(np.array(image))\n\noutput_file = \"detections.gif\"\nimageio.mimsave(output_file, images, fps=10)\n\nfrom IPython.display import Image\nImage(open(output_file, 'rb').read())","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:04.137059Z","iopub.status.busy":"2023-05-22T04:56:04.135847Z","iopub.status.idle":"2023-05-22T04:56:04.765887Z","shell.execute_reply":"2023-05-22T04:56:04.764884Z"},"papermill":{"duration":0.909724,"end_time":"2023-05-22T04:56:04.776261","exception":false,"start_time":"2023-05-22T04:56:03.866537","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### image of 3 layers","metadata":{"papermill":{"duration":0.27726,"end_time":"2023-05-22T04:56:05.328101","exception":false,"start_time":"2023-05-22T04:56:05.050841","status":"completed"},"tags":[]}},{"cell_type":"code","source":"image_data=bandi[:,:,0:3]\nimage_data_normalized = (image_data - np.min(image_data)) / (np.max(image_data) - np.min(image_data))\nfig=plt.figure(figsize=(3,3))\nplt.imshow(image_data_normalized)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:05.869389Z","iopub.status.busy":"2023-05-22T04:56:05.868535Z","iopub.status.idle":"2023-05-22T04:56:06.032168Z","shell.execute_reply":"2023-05-22T04:56:06.030303Z"},"papermill":{"duration":0.439851,"end_time":"2023-05-22T04:56:06.039134","exception":false,"start_time":"2023-05-22T04:56:05.599283","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.273239,"end_time":"2023-05-22T04:56:06.601759","exception":false,"start_time":"2023-05-22T04:56:06.32852","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mask Image","metadata":{"papermill":{"duration":0.272387,"end_time":"2023-05-22T04:56:07.144443","exception":false,"start_time":"2023-05-22T04:56:06.872056","status":"completed"},"tags":[]}},{"cell_type":"code","source":"ind_mask=np.load(pathid+'human_individual_masks.npy')\nprint(ind_mask.shape)\nfig, axs = plt.subplots(2,2, figsize=(6,6))\nfor i, ax in enumerate(axs.flatten()):\n    ax.imshow(ind_mask[:,:,0,i].reshape(256,256))\n    ax.axis('off')\nplt.tight_layout()\nplt.show()  \n","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:07.689096Z","iopub.status.busy":"2023-05-22T04:56:07.688326Z","iopub.status.idle":"2023-05-22T04:56:08.089754Z","shell.execute_reply":"2023-05-22T04:56:08.088386Z"},"papermill":{"duration":0.678333,"end_time":"2023-05-22T04:56:08.09259","exception":false,"start_time":"2023-05-22T04:56:07.414257","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_mask=np.load(pathid+'human_pixel_masks.npy')\nprint(pixel_mask.shape)\nfig=plt.figure(figsize=(3,3))\nplt.axis('off')\nplt.imshow(pixel_mask.reshape(256,256))\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:08.635622Z","iopub.status.busy":"2023-05-22T04:56:08.635221Z","iopub.status.idle":"2023-05-22T04:56:08.751168Z","shell.execute_reply":"2023-05-22T04:56:08.749934Z"},"papermill":{"duration":0.388218,"end_time":"2023-05-22T04:56:08.753784","exception":false,"start_time":"2023-05-22T04:56:08.365566","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.2608,"end_time":"2023-05-22T04:56:09.278616","exception":false,"start_time":"2023-05-22T04:56:09.017816","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission Style","metadata":{"papermill":{"duration":0.26375,"end_time":"2023-05-22T04:56:09.806968","exception":false,"start_time":"2023-05-22T04:56:09.543218","status":"completed"},"tags":[]}},{"cell_type":"code","source":"submit=pd.read_csv('/kaggle/input/google-research-identify-contrails-reduce-global-warming/sample_submission.csv')\ndisplay(submit)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:10.35821Z","iopub.status.busy":"2023-05-22T04:56:10.35673Z","iopub.status.idle":"2023-05-22T04:56:10.406Z","shell.execute_reply":"2023-05-22T04:56:10.404789Z"},"papermill":{"duration":0.322344,"end_time":"2023-05-22T04:56:10.408779","exception":false,"start_time":"2023-05-22T04:56:10.086435","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create encoded_pixels using run-length encoding \nhttps://www.kaggle.com/code/inversion/contrails-rle-submission","metadata":{"papermill":{"duration":0.267042,"end_time":"2023-05-22T04:56:10.941143","exception":false,"start_time":"2023-05-22T04:56:10.674101","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# these functions are given in https://www.kaggle.com/code/inversion/contrails-rle-submission\n\ndef rle_encode(x, fg_val=1):    \n    dots = np.where(\n        x.T.flatten() == fg_val)[0]  # .T sets Fortran order down-then-right\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\n\ndef list_to_string(x):\n    if x: # non-empty list\n        s = str(x).replace(\"[\", \"\").replace(\"]\", \"\").replace(\",\", \"\")\n    else:\n        s = '-'\n    return s\n\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')  # Needed to align to RLE direction","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:11.470366Z","iopub.status.busy":"2023-05-22T04:56:11.468931Z","iopub.status.idle":"2023-05-22T04:56:11.483068Z","shell.execute_reply":"2023-05-22T04:56:11.482075Z"},"papermill":{"duration":0.277625,"end_time":"2023-05-22T04:56:11.485906","exception":false,"start_time":"2023-05-22T04:56:11.208281","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### before encode","metadata":{"papermill":{"duration":0.266293,"end_time":"2023-05-22T04:56:12.013304","exception":false,"start_time":"2023-05-22T04:56:11.747011","status":"completed"},"tags":[]}},{"cell_type":"code","source":"fig=plt.figure(figsize=(3,3))\nplt.imshow(ind_mask[:,:,0,0])\nplt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:12.546104Z","iopub.status.busy":"2023-05-22T04:56:12.545241Z","iopub.status.idle":"2023-05-22T04:56:12.613935Z","shell.execute_reply":"2023-05-22T04:56:12.612333Z"},"papermill":{"duration":0.337439,"end_time":"2023-05-22T04:56:12.617016","exception":false,"start_time":"2023-05-22T04:56:12.279577","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### rle encode","metadata":{"papermill":{"duration":0.264945,"end_time":"2023-05-22T04:56:13.146256","exception":false,"start_time":"2023-05-22T04:56:12.881311","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(ind_mask[:,:,0,0].shape)\nrle0=rle_encode(ind_mask[:,:,0,0])\nrle1=list_to_string(rle0)\nprint(rle1)","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:13.673664Z","iopub.status.busy":"2023-05-22T04:56:13.67327Z","iopub.status.idle":"2023-05-22T04:56:13.686722Z","shell.execute_reply":"2023-05-22T04:56:13.685696Z"},"papermill":{"duration":0.280206,"end_time":"2023-05-22T04:56:13.689656","exception":false,"start_time":"2023-05-22T04:56:13.40945","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### after decode","metadata":{"papermill":{"duration":0.261652,"end_time":"2023-05-22T04:56:14.2152","exception":false,"start_time":"2023-05-22T04:56:13.953548","status":"completed"},"tags":[]}},{"cell_type":"code","source":"fig=plt.figure(figsize=(3,3))\nimg=rle_decode(rle1)\nplt.imshow(img)\nplt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-22T04:56:14.747616Z","iopub.status.busy":"2023-05-22T04:56:14.746426Z","iopub.status.idle":"2023-05-22T04:56:14.859028Z","shell.execute_reply":"2023-05-22T04:56:14.857382Z"},"papermill":{"duration":0.385025,"end_time":"2023-05-22T04:56:14.863462","exception":false,"start_time":"2023-05-22T04:56:14.478437","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.275466,"end_time":"2023-05-22T04:56:15.44226","exception":false,"start_time":"2023-05-22T04:56:15.166794","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n### Contrails are expected to move in a straight line in a different direction from drifting clouds.\n\n","metadata":{"papermill":{"duration":0.258856,"end_time":"2023-05-22T04:56:16.506771","exception":false,"start_time":"2023-05-22T04:56:16.247915","status":"completed"},"tags":[]}}]}