{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-30T19:33:08.137097Z","iopub.execute_input":"2021-06-30T19:33:08.137748Z","iopub.status.idle":"2021-06-30T19:33:08.142803Z","shell.execute_reply.started":"2021-06-30T19:33:08.137696Z","shell.execute_reply":"2021-06-30T19:33:08.142032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/g2net-gravitational-wave-detection/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:22:41.270436Z","iopub.execute_input":"2021-06-30T19:22:41.270946Z","iopub.status.idle":"2021-06-30T19:22:41.281294Z","shell.execute_reply.started":"2021-06-30T19:22:41.270900Z","shell.execute_reply":"2021-06-30T19:22:41.280357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_image_id_2_path(image_id: str, is_train: bool = True) -> str:\n    folder = \"train\" if is_train else \"test\"\n    return \"../input/g2net-gravitational-wave-detection/{}/{}/{}/{}/{}.npy\".format(\n        folder, image_id[0], image_id[1], image_id[2], image_id \n    )","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:27:22.472803Z","iopub.execute_input":"2021-06-30T19:27:22.473217Z","iopub.status.idle":"2021-06-30T19:27:22.481350Z","shell.execute_reply.started":"2021-06-30T19:27:22.473183Z","shell.execute_reply":"2021-06-30T19:27:22.480623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv(path+'training_labels.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:22:53.439181Z","iopub.execute_input":"2021-06-30T19:22:53.439610Z","iopub.status.idle":"2021-06-30T19:22:54.155148Z","shell.execute_reply.started":"2021-06-30T19:22:53.439573Z","shell.execute_reply":"2021-06-30T19:22:54.154008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number train samples:', len(train_labels))\nprint('Number submission samples:', len(samp_subm))","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:23:06.048661Z","iopub.execute_input":"2021-06-30T19:23:06.049018Z","iopub.status.idle":"2021-06-30T19:23:06.055398Z","shell.execute_reply.started":"2021-06-30T19:23:06.048989Z","shell.execute_reply":"2021-06-30T19:23:06.054306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_data(data):\n    \"\"\" Plot 3 Detections of data array\"\"\"\n    \n    fig, axs = plt.subplots(1, 3, figsize=(20, 5))\n    axs = axs.ravel()\n    for i in range(3):\n        x = range(len(data[i]))\n        y = data[i]\n        axs[i].plot(x, y)\n        axs[i].grid()\n        axs[i].set_title('Detection '+str((i+1)))","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:23:20.008127Z","iopub.execute_input":"2021-06-30T19:23:20.008505Z","iopub.status.idle":"2021-06-30T19:23:20.015382Z","shell.execute_reply.started":"2021-06-30T19:23:20.008473Z","shell.execute_reply":"2021-06-30T19:23:20.014342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = train_labels.loc[0, 'id']\nid_\npath_in = convert_image_id_2_path(id_, True)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:37:55.489552Z","iopub.execute_input":"2021-06-30T19:37:55.489933Z","iopub.status.idle":"2021-06-30T19:37:55.500136Z","shell.execute_reply.started":"2021-06-30T19:37:55.489903Z","shell.execute_reply":"2021-06-30T19:37:55.499068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_array = np.load(path_in)\ndata_array.shape\nprint(data_array)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:44:24.525311Z","iopub.execute_input":"2021-06-30T19:44:24.525747Z","iopub.status.idle":"2021-06-30T19:44:24.544204Z","shell.execute_reply.started":"2021-06-30T19:44:24.525709Z","shell.execute_reply":"2021-06-30T19:44:24.543165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_data(data_array)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:29:43.014523Z","iopub.execute_input":"2021-06-30T19:29:43.015035Z","iopub.status.idle":"2021-06-30T19:29:43.489796Z","shell.execute_reply.started":"2021-06-30T19:29:43.015003Z","shell.execute_reply":"2021-06-30T19:29:43.488761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=train_labels, x=\"target\")","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:31:24.227074Z","iopub.execute_input":"2021-06-30T19:31:24.227606Z","iopub.status.idle":"2021-06-30T19:31:24.404378Z","shell.execute_reply.started":"2021-06-30T19:31:24.227569Z","shell.execute_reply":"2021-06-30T19:31:24.403328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_sample(\n    _id, \n    target, \n    colors=(\"black\", \"red\", \"green\"), \n    signal_names=(\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\")\n):\n    path = convert_image_id_2_path(_id)\n    x = np.load(path)\n    plt.figure(figsize=(16, 7))\n    for i in range(3):\n        plt.subplot(4, 1, i + 1)\n        plt.plot(x[i], color=colors[i])\n#         plt.title(signal_name[i], fontsize=14)\n#         plt.xlabel(\"time\")\n        plt.legend([signal_names[i]], fontsize=12, loc=\"lower right\")\n        \n        plt.subplot(4, 1, 4)\n        plt.plot(x[i], color=colors[i])\n    \n    plt.subplot(4, 1, 4)\n    plt.legend(signal_names, fontsize=12, loc=\"lower right\")\n\n    plt.suptitle(f\"id: {_id} target: {target}\", fontsize=16)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:32:17.735042Z","iopub.execute_input":"2021-06-30T19:32:17.735604Z","iopub.status.idle":"2021-06-30T19:32:17.744893Z","shell.execute_reply.started":"2021-06-30T19:32:17.735569Z","shell.execute_reply":"2021-06-30T19:32:17.743911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(train_labels.index.tolist(), 3):\n    _id = train_labels.iloc[i][\"id\"]\n    target = train_labels.iloc[i][\"target\"]\n\n    visualize_sample(_id, target)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T19:33:23.876829Z","iopub.execute_input":"2021-06-30T19:33:23.877201Z","iopub.status.idle":"2021-06-30T19:33:25.510220Z","shell.execute_reply.started":"2021-06-30T19:33:23.877171Z","shell.execute_reply":"2021-06-30T19:33:25.509192Z"},"trusted":true},"execution_count":null,"outputs":[]}]}