{"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":"![](https://storage.googleapis.com/kaggle-media/competitions/Google-Contrails/waterdroplets.png)","metadata":{}},{"cell_type":"code","source":"import os, glob\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nDATASET_FOLDER = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming\"\npath_json = os.path.join(DATASET_FOLDER, \"train_metadata.json\")\ndf_train = pd.read_json(path_json)\ndisplay(df_train.head())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-11T22:39:39.230074Z","iopub.execute_input":"2023-05-11T22:39:39.230486Z","iopub.status.idle":"2023-05-11T22:39:39.690581Z","shell.execute_reply.started":"2023-05-11T22:39:39.230453Z","shell.execute_reply":"2023-05-11T22:39:39.68939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pprint import pprint\n\npprint(df_train.iloc[0][\"projection_wkt\"])","metadata":{"execution":{"iopub.status.busy":"2023-05-11T22:56:38.708496Z","iopub.execute_input":"2023-05-11T22:56:38.708942Z","iopub.status.idle":"2023-05-11T22:56:38.715903Z","shell.execute_reply.started":"2023-05-11T22:56:38.708908Z","shell.execute_reply":"2023-05-11T22:56:38.714387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show raw image\n\n- **band_{08-16}.npy:** array with size of H x W x T, where T = n_times_before + n_times_after + 1, representing the number of images in the sequence. There are n_times_before and n_times_after images before and after the labeled frame respectively. In our dataset all examples have n_times_before=4 and n_times_after=3. Each band represents an infrared channel at different wavelengths and is converted to brightness temperatures based on the calibration parameters. The number in the filename corresponds to the GOES-16 ABI band number. Details of the ABI bands can be found here.\n- **human_individual_masks.npy:** array with size of H x W x 1 x R. Each example is labeled by R individual human labelers. R is not the same for all samples. The labeled masks have value either 0 or 1 and correspond to the (n_times_before+1)-th image in band_{08-16}.npy. They are available only in the training set.\n- **human_pixel_masks.npy:** array with size of H x W x 1 containing the binary ground truth. A pixel is regarded as contrail pixel in evaluation if it is labeled as contrail by more than half of the labelers.","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\ndef show_sample(spl_id):\n    folder = os.path.join(DATASET_FOLDER, \"train\", spl_id)\n    fig, axarr = plt.subplots(ncols=8, nrows=9, figsize=(16, 14))\n    for i, bi in enumerate(range(8, 17)):\n        img = np.load(os.path.join(folder, f\"band_{bi:02}.npy\"))\n        print(img.shape, img.min(), img.max())\n        for j in range(8):\n            axarr[i, j].imshow(img[:, :, j])\n    for n in (\"human_individual_masks\", \"human_pixel_masks\"):\n        mask = np.load(os.path.join(folder, f\"{n}.npy\"))\n        print(mask.shape, mask.min(), mask.max())\n\n\nshow_sample(\"1000823728928031783\")","metadata":{"execution":{"iopub.status.busy":"2023-05-11T23:10:18.624435Z","iopub.execute_input":"2023-05-11T23:10:18.624841Z","iopub.status.idle":"2023-05-11T23:10:27.631241Z","shell.execute_reply.started":"2023-05-11T23:10:18.624801Z","shell.execute_reply":"2023-05-11T23:10:27.629656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Conver to RGB\n\n**Following example from: https://www.kaggle.com/code/inversion/visualizing-contrails**","metadata":{}},{"cell_type":"code","source":"_T11_BOUNDS = (243, 303)\n_CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n_TDIFF_BOUNDS = (-4, 2)\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 show_rgb_sample(spl_id, t=4):\n    folder = os.path.join(DATASET_FOLDER, \"train\", spl_id)\n    bands = [None] * 17\n    for bi in range(8, 17):\n        bands[bi] = np.load(os.path.join(folder, f\"band_{bi:02}.npy\"))\n        # print(bands[bi].shape, bands[bi].min(), bands[bi].max())\n    r = normalize_range(bands[15] - bands[14], _TDIFF_BOUNDS)\n    g = normalize_range(bands[14] - bands[11], _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(bands[14], _T11_BOUNDS)\n    img = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    \n    mask = np.load(os.path.join(folder, \"human_pixel_masks.npy\"))\n    # print(mask.shape, mask.min(), mask.max())\n\n    fig, axarr = plt.subplots(ncols=2, nrows=2, figsize=(10, 10))\n    axarr[0, 0].imshow(img[..., t])\n    axarr[0, 1].imshow(mask[..., 0], interpolation='none')\n    axarr[1, 0].imshow(img[..., t])\n    axarr[1, 0].contour(mask[..., 0], linewidths=1, colors='red')\n\nshow_rgb_sample(\"1000823728928031783\")","metadata":{"execution":{"iopub.status.busy":"2023-05-11T23:42:35.048215Z","iopub.execute_input":"2023-05-11T23:42:35.048639Z","iopub.status.idle":"2023-05-11T23:42:36.092177Z","shell.execute_reply.started":"2023-05-11T23:42:35.048591Z","shell.execute_reply":"2023-05-11T23:42:36.090872Z"},"trusted":true},"execution_count":null,"outputs":[]}]}