{"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":"# Help Protect the Great Barrier Reef - EDA\nIt is my first EDA.\nMostly refer to [ROB MULLA](https://www.kaggle.com/robikscube). Thank you for great content. Check his code here: https://www.kaggle.com/robikscube/barrier-reef-starfish-starter-twitch-stream.","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom matplotlib.patches import Rectangle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-19T21:26:15.813182Z","iopub.execute_input":"2022-02-19T21:26:15.813628Z","iopub.status.idle":"2022-02-19T21:26:15.841244Z","shell.execute_reply.started":"2022-02-19T21:26:15.813511Z","shell.execute_reply":"2022-02-19T21:26:15.840339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/train.csv')\ntest = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:15.842853Z","iopub.execute_input":"2022-02-19T21:26:15.843319Z","iopub.status.idle":"2022-02-19T21:26:15.904144Z","shell.execute_reply.started":"2022-02-19T21:26:15.843268Z","shell.execute_reply":"2022-02-19T21:26:15.903116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:15.905338Z","iopub.execute_input":"2022-02-19T21:26:15.905570Z","iopub.status.idle":"2022-02-19T21:26:15.913049Z","shell.execute_reply.started":"2022-02-19T21:26:15.905544Z","shell.execute_reply":"2022-02-19T21:26:15.912379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:15.914008Z","iopub.execute_input":"2022-02-19T21:26:15.914699Z","iopub.status.idle":"2022-02-19T21:26:15.940246Z","shell.execute_reply.started":"2022-02-19T21:26:15.914626Z","shell.execute_reply":"2022-02-19T21:26:15.939358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:15.942768Z","iopub.execute_input":"2022-02-19T21:26:15.943559Z","iopub.status.idle":"2022-02-19T21:26:15.954642Z","shell.execute_reply.started":"2022-02-19T21:26:15.943507Z","shell.execute_reply":"2022-02-19T21:26:15.953620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:15.956310Z","iopub.execute_input":"2022-02-19T21:26:15.956843Z","iopub.status.idle":"2022-02-19T21:26:15.968188Z","shell.execute_reply.started":"2022-02-19T21:26:15.956792Z","shell.execute_reply":"2022-02-19T21:26:15.967184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# What the image looks like","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(15,20))\nimg = mpimg.imread('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/0.jpg')\nax.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:15.969963Z","iopub.execute_input":"2022-02-19T21:26:15.970585Z","iopub.status.idle":"2022-02-19T21:26:16.776124Z","shell.execute_reply.started":"2022-02-19T21:26:15.970536Z","shell.execute_reply":"2022-02-19T21:26:16.775040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The sequence in the video","metadata":{}},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nfig, ax = plt.subplots(3,1,figsize=(15,10),sharex=True,sharey=True)\nfor video in [0,1,2]:\n    for sequence, d in train.query('video_id == @video').groupby('sequence'):\n        d[\"sequence_frame\"].plot(ax=ax[video], label=f\"Sequence {sequence}\")\n    ax[video].set_title(f'The sequence frame in video:{video}')\n    ax[video].set_xlabel('video frame')\n    ax[video].set_ylabel('sequence frame')\n    ax[video].legend(bbox_to_anchor=(1.02, 1), loc=\"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:16.777701Z","iopub.execute_input":"2022-02-19T21:26:16.778276Z","iopub.status.idle":"2022-02-19T21:26:17.702227Z","shell.execute_reply.started":"2022-02-19T21:26:16.778234Z","shell.execute_reply":"2022-02-19T21:26:17.701427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Annotations","metadata":{}},{"cell_type":"code","source":"train[\"n_annotations\"] = train[\"annotations\"].apply(lambda x: len(eval(x)))\ntrain['video_sequence'] = train['video_id'].astype('str') + '_' + train['sequence'].astype('str')\n\nax = train.groupby('video_sequence')['sequence_frame'].max().sort_values().plot(kind='barh', figsize=(12, 7), title=\"Length of Sequences\")\nax.set_xlabel(\"Number of Frames in the Seqence\")","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:17.703308Z","iopub.execute_input":"2022-02-19T21:26:17.703671Z","iopub.status.idle":"2022-02-19T21:26:18.265812Z","shell.execute_reply.started":"2022-02-19T21:26:17.703641Z","shell.execute_reply":"2022-02-19T21:26:18.264847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:18.267044Z","iopub.execute_input":"2022-02-19T21:26:18.267274Z","iopub.status.idle":"2022-02-19T21:26:18.278664Z","shell.execute_reply.started":"2022-02-19T21:26:18.267248Z","shell.execute_reply":"2022-02-19T21:26:18.277846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1,2, figsize=(25,10))\n\ntrain.groupby('video_sequence')['n_annotations'].sum().sort_values().plot(kind='barh', ax=axes[0], title='Total annotations each sequence', ylabel='Number of annotations')\ntrain.groupby('video_sequence')['n_annotations'].mean().sort_values().plot(kind='barh', ax=axes[1], title='Average annotations each sequence', ylabel='Average number of annotations')","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:18.279998Z","iopub.execute_input":"2022-02-19T21:26:18.280292Z","iopub.status.idle":"2022-02-19T21:26:18.979382Z","shell.execute_reply.started":"2022-02-19T21:26:18.280235Z","shell.execute_reply":"2022-02-19T21:26:18.978504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharey=True)\nfor i, d in train.groupby([\"video_id\", \"sequence\"]):\n    d.set_index(\"sequence_frame\")[\"n_annotations\"].plot(ax=axs[i[0]])\n    axs[i[0]].set_title(f\"Video ID: {i[0]} - Sequence {i[1]}\")\nfig.suptitle(\"Number of Annotations per Frame for each Sequence\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:18.980538Z","iopub.execute_input":"2022-02-19T21:26:18.980758Z","iopub.status.idle":"2022-02-19T21:26:19.639573Z","shell.execute_reply.started":"2022-02-19T21:26:18.980731Z","shell.execute_reply":"2022-02-19T21:26:19.638665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BBOX\n","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(25,15))\n\nexample = train.query('video_id == 1 and video_frame==9114')\nimg = plt.imread('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_1/9114.jpg')\nax.imshow(img)\nax.grid(False)\n\nfor bbox in eval(train.query('video_id == 1 and video_frame==9114')['annotations'].values[0]):\n    ax.add_patch(\n    Rectangle(\n    (bbox['x'],bbox['y']),\n        bbox['width'],\n        bbox['height'],\n        lw=1,\n        facecolor='None',\n        edgecolor='red',\n    )\n    )\n\nax.axis('off')\n\nax.set_title('The image has the most annotations ')\n    ","metadata":{"execution":{"iopub.status.busy":"2022-02-19T21:26:19.640824Z","iopub.execute_input":"2022-02-19T21:26:19.641064Z","iopub.status.idle":"2022-02-19T21:26:20.903749Z","shell.execute_reply.started":"2022-02-19T21:26:19.641035Z","shell.execute_reply":"2022-02-19T21:26:20.902420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}