{"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":"# Prepare Environment","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport math\nimport pickle\nimport cv2\nfrom IPython.lib.display import Audio\n\nimport scipy\nfrom scipy.interpolate import interp1d\nfrom scipy.signal import butter, filtfilt, iirdesign, zpk2tf, freqz\n\nimport librosa\nimport librosa.display\nimport torch\n\nfrom sklearn.model_selection import train_test_split\n\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \n\nfrom tensorflow import keras\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *","metadata":{"_uuid":"45b62b0b-8179-4a7e-8a41-05381a3eb83d","_cell_guid":"ae0092a3-c476-4c40-a99f-fed965618361","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:22:04.142981Z","iopub.execute_input":"2022-03-30T20:22:04.143311Z","iopub.status.idle":"2022-03-30T20:22:05.661060Z","shell.execute_reply.started":"2022-03-30T20:22:04.143277Z","shell.execute_reply":"2022-03-30T20:22:05.660334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"faff9fae-f066-4219-b305-5504e9801368","_cell_guid":"07b85087-6667-4116-b744-c62dd59f1d18","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:15:04.428568Z","iopub.execute_input":"2022-03-30T20:15:04.430364Z","iopub.status.idle":"2022-03-30T20:15:04.439970Z","shell.execute_reply.started":"2022-03-30T20:15:04.430337Z","shell.execute_reply":"2022-03-30T20:15:04.439258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def merge_history(hlist):\n    history = {}\n    for k in hlist[0].history.keys():\n        history[k] = sum([h.history[k] for h in hlist], [])\n    return history","metadata":{"_uuid":"b1b470aa-90f1-4034-9dea-655f88b6e086","_cell_guid":"45da22cc-bacc-4a4c-b544-806c9bdfbd4f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:15:04.441343Z","iopub.execute_input":"2022-03-30T20:15:04.441711Z","iopub.status.idle":"2022-03-30T20:15:04.448362Z","shell.execute_reply.started":"2022-03-30T20:15:04.441653Z","shell.execute_reply":"2022-03-30T20:15:04.447639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vis_training(h, start=1):\n    epoch_range = range(start, len(h['loss'])+1)\n    s = slice(start-1, None)\n\n    plt.figure(figsize=[14,4])\n\n    n = int(len(h.keys()) / 2)\n\n    for i in range(n):\n        k = list(h.keys())[i]\n        plt.subplot(1,n,i+1)\n        plt.plot(epoch_range, h[k][s], label='Training')\n        plt.plot(epoch_range, h['val_' + k][s], label='Validation')\n        plt.xlabel('Epoch'); plt.ylabel(k); plt.title(k)\n        plt.grid()\n        plt.legend()\n\n    plt.tight_layout()\n    plt.show()","metadata":{"_uuid":"d7335f26-9547-46f9-8a80-1bdc76e3b352","_cell_guid":"5e38aca1-4c13-4a87-a175-a46a88dfaa44","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:15:04.449540Z","iopub.execute_input":"2022-03-30T20:15:04.449797Z","iopub.status.idle":"2022-03-30T20:15:04.458758Z","shell.execute_reply.started":"2022-03-30T20:15:04.449753Z","shell.execute_reply":"2022-03-30T20:15:04.458013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HOME = '../input/g2net-gravitational-wave-detection'\nTRAIN = '../input/g2net-gravitational-wave-detection/train'","metadata":{"execution":{"iopub.status.busy":"2022-03-30T20:18:14.727169Z","iopub.execute_input":"2022-03-30T20:18:14.727631Z","iopub.status.idle":"2022-03-30T20:18:14.731269Z","shell.execute_reply.started":"2022-03-30T20:18:14.727591Z","shell.execute_reply":"2022-03-30T20:18:14.730346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_full = pd.read_csv(f'{HOME}/training_labels.csv', dtype=str)\ntrain_full.shape","metadata":{"_uuid":"006a180a-5b26-406e-8e4a-3cdab6aff08b","_cell_guid":"0813f542-9183-4ac8-bf55-86671bae0843","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:18:45.086148Z","iopub.execute_input":"2022-03-30T20:18:45.086414Z","iopub.status.idle":"2022-03-30T20:18:45.380399Z","shell.execute_reply.started":"2022-03-30T20:18:45.086384Z","shell.execute_reply":"2022-03-30T20:18:45.379669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_full['filename'] = train_full.id + '.npy'","metadata":{"_uuid":"bdc79336-d2e8-4609-ad3b-60160fcc8eef","_cell_guid":"c8d3d416-2db9-4d7b-becb-f1a88b5eafac","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:18:50.458391Z","iopub.execute_input":"2022-03-30T20:18:50.458654Z","iopub.status.idle":"2022-03-30T20:18:50.533512Z","shell.execute_reply.started":"2022-03-30T20:18:50.458623Z","shell.execute_reply":"2022-03-30T20:18:50.532795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_full.head()","metadata":{"_uuid":"0a70e93d-c08e-4025-a38c-6112c10372c8","_cell_guid":"1ae09d5f-eaaa-45df-a4e4-5620f12973fe","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:18:51.292298Z","iopub.execute_input":"2022-03-30T20:18:51.293147Z","iopub.status.idle":"2022-03-30T20:18:51.303148Z","shell.execute_reply.started":"2022-03-30T20:18:51.293097Z","shell.execute_reply":"2022-03-30T20:18:51.302327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore Data","metadata":{"_uuid":"682fd945-a7a8-4f14-a913-88d75c759e6f","_cell_guid":"d0821d1d-04a3-4ba9-9f16-43f26fb40710","trusted":true}},{"cell_type":"code","source":"def id_to_path(idx, is_train=True):\n    f = train_full.id[idx]\n    SET = 'train' if is_train else 'test'\n    path = f'{TRAIN}/{f[0]}/{f[1]}/{f[2]}/{f}.npy'\n    return path\n\nx = np.load(id_to_path(0))\nprint(x.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-30T20:20:05.502374Z","iopub.execute_input":"2022-03-30T20:20:05.502651Z","iopub.status.idle":"2022-03-30T20:20:05.534714Z","shell.execute_reply.started":"2022-03-30T20:20:05.502620Z","shell.execute_reply":"2022-03-30T20:20:05.534027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[12,6])\nfor i,t in enumerate([\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\"]):\n    plt.subplot(3, 1, i+1)\n    plt.plot(x[i,:])\n    plt.title(t)\n\nplt.tight_layout()    \nplt.show()","metadata":{"_uuid":"88ea5288-377c-4377-9c1e-052bb499cc33","_cell_guid":"aa5557bf-a3d3-4e53-bd9e-bca55759c1b4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:20:30.857022Z","iopub.execute_input":"2022-03-30T20:20:30.857310Z","iopub.status.idle":"2022-03-30T20:20:31.297464Z","shell.execute_reply.started":"2022-03-30T20:20:30.857280Z","shell.execute_reply":"2022-03-30T20:20:31.296761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Processing","metadata":{}},{"cell_type":"code","source":"dt = 10/5000\nsample_rate = 2048 #Hz (1/seconds)\ntime_span = 2 #seconds\nsamples_total = time_span * sample_rate\nfband = [35.0, 200.0]","metadata":{"_uuid":"059e6f7b-3fd5-4028-b0c0-5fdf3a537247","_cell_guid":"cd9700f0-a3b4-4bd1-9b45-6e9ad50423c6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:21:22.207380Z","iopub.execute_input":"2022-03-30T20:21:22.207946Z","iopub.status.idle":"2022-03-30T20:21:22.212022Z","shell.execute_reply.started":"2022-03-30T20:21:22.207906Z","shell.execute_reply":"2022-03-30T20:21:22.211127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def whiten(strain, samples_total, dt):    \n    fhat = np.fft.fft(strain, samples_total)\n    PSD = fhat * np.conj(fhat) / samples_total\n    freq = 1/(dt*samples_total) * np.arange(samples_total)\n    \n    # scipy interp1d interpolation\n    interp_psd = interp1d(freq, PSD, \"nearest\")\n    \n    w_fhat = fhat/np.sqrt(interp_psd(freq))\n    w_strain = np.fft.ifft(w_fhat)\n    return w_strain, interp_psd(freq)","metadata":{"_uuid":"29db6534-4cb9-4eed-b6fb-32e3e829d89f","_cell_guid":"98877861-e852-4d6e-96a3-e338083f2346","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:21:23.555973Z","iopub.execute_input":"2022-03-30T20:21:23.556600Z","iopub.status.idle":"2022-03-30T20:21:23.561886Z","shell.execute_reply.started":"2022-03-30T20:21:23.556556Z","shell.execute_reply":"2022-03-30T20:21:23.561042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"whitened, ip = whiten(x[0,:], samples_total, dt)\n\nplt.plot(whitened)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T20:23:13.974505Z","iopub.execute_input":"2022-03-30T20:23:13.974777Z","iopub.status.idle":"2022-03-30T20:23:14.164930Z","shell.execute_reply.started":"2022-03-30T20:23:13.974735Z","shell.execute_reply":"2022-03-30T20:23:14.164183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bandpass(x, N, fband, fs):\n    bb, ab = scipy.signal.butter(N, [fband[0]*2./fs, fband[1]*2./fs], btype='band')\n    normalization = np.sqrt((fband[1]-fband[0])/(fs/2))\n    x_bp = scipy.signal.filtfilt(bb, ab, x) / normalization\n    return x_bp","metadata":{"execution":{"iopub.status.busy":"2022-03-30T20:23:39.192076Z","iopub.execute_input":"2022-03-30T20:23:39.192514Z","iopub.status.idle":"2022-03-30T20:23:39.198064Z","shell.execute_reply.started":"2022-03-30T20:23:39.192478Z","shell.execute_reply":"2022-03-30T20:23:39.197108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bandpassed_strain = bandpass(x, 4, fband, samples_total)\n\nplt.figure(figsize=[12,6])\nfor i,t in enumerate([\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\"]):\n    plt.subplot(3, 1, i+1)\n    plt.plot(bandpassed_strain[i,:])\n    plt.title(t)\n\nplt.tight_layout()    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T20:23:45.992990Z","iopub.execute_input":"2022-03-30T20:23:45.993288Z","iopub.status.idle":"2022-03-30T20:23:46.430301Z","shell.execute_reply.started":"2022-03-30T20:23:45.993254Z","shell.execute_reply":"2022-03-30T20:23:46.429662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w = x.copy()\n\nfor i in range(3):\n    w[i,:], _ = whiten(x[i,:], samples_total, dt)\n                    \nbpw = bandpass(w, 4, fband, samples_total)\n\nplt.figure(figsize=[12,6])\nfor i,t in enumerate([\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\"]):\n    plt.subplot(3, 1, i+1)\n    plt.plot(bpw[i,:])\n    plt.title(t)\n\nplt.tight_layout()    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-30T20:25:59.325008Z","iopub.execute_input":"2022-03-30T20:25:59.325546Z","iopub.status.idle":"2022-03-30T20:25:59.752432Z","shell.execute_reply.started":"2022-03-30T20:25:59.325509Z","shell.execute_reply":"2022-03-30T20:25:59.751658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator","metadata":{"_uuid":"3c00f705-4a05-490f-a497-fd477e25a7c0","_cell_guid":"fc8c70db-7e03-4318-adbb-801c7b82ba18","trusted":true}},{"cell_type":"code","source":"class DataGenerator(keras.utils.Sequence):\n    \n    #####################################################################\n    # Constructor\n    #####################################################################\n    def __init__(self, df, batch_size=32, n_batches=None, shuffle=True, is_train=True):\n        self.df = df\n        self.n = len(df)\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        self.is_train = is_train\n        self.n_batches = n_batches\n        self.on_epoch_end()\n        \n    def on_epoch_end(self):\n        self.indices = np.arange(self.n)\n        if self.shuffle == True:\n            np.random.shuffle(self.indices)   \n    \n    def __len__(self):\n        # Determine batches per epoch\n        if self.n_batches is None: \n            return math.ceil( self.n / self.batch_size )\n        return self.n_batches\n    \n    def __getitem__(self, batch_index):\n        # Get and return a single batch of data\n        start = batch_index * self.batch_size\n        end = (batch_index + 1) * self.batch_size\n        \n        indices = self.indices[start:end]\n        \n        return self.__data_generation(indices)\n    \n    def __data_generation(self, batch_indices):\n        batch_size = len(batch_indices)\n        \n        SHAPE = (batch_size, 4096, 3)\n        X = np.zeros(shape=SHAPE)\n        y = np.zeros(batch_size)\n        id_list = []\n        \n        for i, idx in enumerate(batch_indices):\n            ID = self.df.id.values[idx]\n            y[i] = self.df.target.values[idx]\n            \n            path = id_to_path(idx)\n            x = np.load(path)\n            \n            # Whitening\n            for j in range(3):\n                x[j,:], _ = whiten(x[j,:], samples_total, dt)\n            \n            # Bandpass filter\n            x = bandpass(x, 4, fband=[35.0, 200.0], fs=4096)\n            \n            X[i,:,:] = x.T \n            \n            id_list.append(ID)\n            \n        return X, y\n            \ntemp_gen = DataGenerator(train_full, batch_size=8, shuffle=False)\nX,y = temp_gen.__getitem__(0)\n\nprint(X.shape)\n\nplt.figure(figsize=(16,4))\nfor i in range(3):\n    plt.subplot(1,3,i+1)\n    plt.plot(X[0,:,i])\nplt.show()","metadata":{"_uuid":"8a8457a4-1de9-4a33-a59b-45941d678e9e","_cell_guid":"a7244008-be9d-4c20-bb92-d2e2ff458cf9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:27:21.271754Z","iopub.execute_input":"2022-03-30T20:27:21.272171Z","iopub.status.idle":"2022-03-30T20:27:21.888921Z","shell.execute_reply.started":"2022-03-30T20:27:21.272141Z","shell.execute_reply":"2022-03-30T20:27:21.887669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, valid = train_test_split(train_full, test_size=0.9, random_state=1, stratify=train_full.target)\ntrain_gen = DataGenerator(train, batch_size=512, n_batches=20, shuffle=False)\nvalid_gen = DataGenerator(valid, batch_size=512, n_batches=20, shuffle=False)","metadata":{"_uuid":"dda7346f-f9d4-4a99-8247-980463c4f3a4","_cell_guid":"3b238085-2070-4a71-8190-9c3de6438188","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:27:24.937518Z","iopub.execute_input":"2022-03-30T20:27:24.938001Z","iopub.status.idle":"2022-03-30T20:27:25.863745Z","shell.execute_reply.started":"2022-03-30T20:27:24.937966Z","shell.execute_reply":"2022-03-30T20:27:25.863018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Network","metadata":{"_uuid":"2e444acd-6f97-4132-951c-a077c6702d7e","_cell_guid":"073a9c59-a821-47c0-9501-a75efc8ee181","trusted":true}},{"cell_type":"code","source":"tf.random.set_seed(1)\n\ncnn = Sequential([\n    Conv1D(64, input_shape=(4096, 3), kernel_size=64, activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling1D(),\n    \n    Conv1D(64, kernel_size=32, activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling1D(),\n    \n    Conv1D(128, kernel_size=32, activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling1D(),\n    \n    Conv1D(128, kernel_size=16, activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling1D(),\n    \n    Conv1D(256, kernel_size=16, activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling1D(),\n\n    Conv1D(256, kernel_size=16, activation='relu', padding='same'),\n    BatchNormalization(),\n    MaxPooling1D(),\n\n    Flatten(),\n    Dropout(0.2),\n    \n    Dense(128, activation='relu'),\n    Dropout(0.2),\n    \n    Dense(64, activation='relu'),\n    Dropout(0.1),\n    \n    Dense(1, activation='sigmoid')\n])\n\ncnn.summary()","metadata":{"_uuid":"bdb5246f-6e13-419b-b4ac-6d3588fe1a65","_cell_guid":"88158647-57bd-4bcf-87d2-2bca2aca21c0","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T20:27:55.664504Z","iopub.execute_input":"2022-03-30T20:27:55.664832Z","iopub.status.idle":"2022-03-30T20:27:55.862149Z","shell.execute_reply.started":"2022-03-30T20:27:55.664802Z","shell.execute_reply":"2022-03-30T20:27:55.861431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nopt = tf.keras.optimizers.Adam(1e-5)\ncnn.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])\n\nh1 = cnn.fit(train_gen, epochs=5, verbose=1, validation_data=valid_gen)","metadata":{"_uuid":"8ed63eca-4ab2-4bc8-8d46-acdf367c88ca","_cell_guid":"e69429ea-fe33-41fc-b7ee-7711e2b365f4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-03-30T21:07:57.407138Z","iopub.execute_input":"2022-03-30T21:07:57.407397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"33eb940c-e8c9-448c-a115-f447c100728c","_cell_guid":"8187666f-ce6a-4176-a769-9a1cd69ce306","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"1bc24374-7ced-4dbd-8d32-d6ce80f1096b","_cell_guid":"ec6efa31-6778-4d50-8813-4ea9106eb8cb","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"db438826-005c-4b0c-8c9b-a78034667f28","_cell_guid":"52b6253d-1542-4d1f-9fd1-861615b31ab1","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"2510c405-1e15-4745-90e4-46b8ba3335c9","_cell_guid":"fae1decd-2b2c-4fcc-b346-d7226b4b7ff1","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}