{"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\nfrom glob import glob\nimport matplotlib.pyplot as plt\nimport torch\nimport torchaudio\nimport librosa\nimport scipy","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-13T02:59:45.892245Z","iopub.execute_input":"2021-07-13T02:59:45.892808Z","iopub.status.idle":"2021-07-13T02:59:45.899171Z","shell.execute_reply.started":"2021-07-13T02:59:45.892767Z","shell.execute_reply":"2021-07-13T02:59:45.897947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Utils","metadata":{}},{"cell_type":"code","source":"SR = 2048","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:45.901128Z","iopub.execute_input":"2021-07-13T02:59:45.901640Z","iopub.status.idle":"2021-07-13T02:59:45.915424Z","shell.execute_reply.started":"2021-07-13T02:59:45.901587Z","shell.execute_reply":"2021-07-13T02:59:45.914270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_full_path(signal_file_name):\n    folder_id = signal_file_name[:3]\n    path = f\"/kaggle/input/g2net-gravitational-wave-detection/train/{folder_id[0]}/{folder_id[1]}/{folder_id[2]}/{signal_file_name}.npy\"\n    return path","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:45.917568Z","iopub.execute_input":"2021-07-13T02:59:45.918138Z","iopub.status.idle":"2021-07-13T02:59:45.927659Z","shell.execute_reply.started":"2021-07-13T02:59:45.918090Z","shell.execute_reply":"2021-07-13T02:59:45.926573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_signal(file_path):\n    with open(file_path, \"rb\") as file:\n        return np.load(file)    ","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:45.929859Z","iopub.execute_input":"2021-07-13T02:59:45.930402Z","iopub.status.idle":"2021-07-13T02:59:45.939532Z","shell.execute_reply.started":"2021-07-13T02:59:45.930356Z","shell.execute_reply":"2021-07-13T02:59:45.938650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_transformation(\n    negative_signal,\n    positive_signal,\n    transformation,\n    title=\"CQT\"\n):\n    fig, ax = plt.subplots(3, 2, figsize=(18, 10))\n    plt.suptitle(f\"{title}\\n\\n\", fontsize=18)\n    fig.tight_layout()\n    \n    for i  in range(3):\n        ax[i, 0].imshow(transformation(negative_signal[i, :]), aspect='auto')\n        ax[i, 1].imshow(transformation(positive_signal[i, :]), aspect='auto')\n\n    for i in range(ax.shape[0]):\n        for j in range(ax.shape[1]):    \n            ax[i, j].set(xlabel='time', ylabel='pseudo freq')\n\n    ax[0, 0].set_title('label=0', fontsize=16)\n    ax[0, 1].set_title('label=1', fontsize=16)\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:45.941018Z","iopub.execute_input":"2021-07-13T02:59:45.941638Z","iopub.status.idle":"2021-07-13T02:59:45.953602Z","shell.execute_reply.started":"2021-07-13T02:59:45.941601Z","shell.execute_reply":"2021-07-13T02:59:45.952572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_raw_signal(\n    negative_signal,\n    positive_signal\n):\n    fig, ax = plt.subplots(1, 2, figsize=(14, 5))\n    plt.suptitle(f\"Raw signal\\n\\n\", fontsize=18)\n    \n    for i  in range(3):\n        ax[0].plot(negative_signal[i, :])\n        ax[1].plot(positive_signal[i, :])        \n\n    for i in range(2):\n        ax[i].set(xlabel='time', ylabel='amplitude')\n\n    ax[0].set_title('label=0', fontsize=16)\n    ax[1].set_title('label=1', fontsize=16)\n    fig.tight_layout()\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:45.955030Z","iopub.execute_input":"2021-07-13T02:59:45.955345Z","iopub.status.idle":"2021-07-13T02:59:45.979996Z","shell.execute_reply.started":"2021-07-13T02:59:45.955313Z","shell.execute_reply":"2021-07-13T02:59:45.979019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load labels/data path","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/g2net-gravitational-wave-detection/training_labels.csv\")\n\nall_negatives = train[train.target == 0].id.apply(get_full_path)\nall_positives = train[train.target == 1].id.apply(get_full_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:45.981632Z","iopub.execute_input":"2021-07-13T02:59:45.982434Z","iopub.status.idle":"2021-07-13T02:59:46.986872Z","shell.execute_reply.started":"2021-07-13T02:59:45.982374Z","shell.execute_reply":"2021-07-13T02:59:46.985799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id0 = get_signal(all_negatives.iloc[0])\nid1 = get_signal(all_positives.iloc[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:46.988547Z","iopub.execute_input":"2021-07-13T02:59:46.988969Z","iopub.status.idle":"2021-07-13T02:59:47.002687Z","shell.execute_reply.started":"2021-07-13T02:59:46.988927Z","shell.execute_reply":"2021-07-13T02:59:47.001550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Raw signal","metadata":{}},{"cell_type":"code","source":"plot_raw_signal(id0, id1)","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:47.005378Z","iopub.execute_input":"2021-07-13T02:59:47.005714Z","iopub.status.idle":"2021-07-13T02:59:47.407099Z","shell.execute_reply.started":"2021-07-13T02:59:47.005682Z","shell.execute_reply":"2021-07-13T02:59:47.406237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### STFT\n\n[The Short-time Fourier transform (STFT), is a Fourier-related transform used to determine the sinusoidal frequency and phase content of local sections of a signal as it changes over time](https://en.wikipedia.org/wiki/Short-time_Fourier_transform)","metadata":{}},{"cell_type":"code","source":"def get_stft(signal):\n    return torch.stft(torch.Tensor(signal), n_fft=128, return_complex=True, normalized=True).abs().numpy()","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:47.408318Z","iopub.execute_input":"2021-07-13T02:59:47.408789Z","iopub.status.idle":"2021-07-13T02:59:47.413006Z","shell.execute_reply.started":"2021-07-13T02:59:47.408757Z","shell.execute_reply":"2021-07-13T02:59:47.412213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_transformation(id0, id1, get_stft, \"STFT\")","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:47.413994Z","iopub.execute_input":"2021-07-13T02:59:47.414391Z","iopub.status.idle":"2021-07-13T02:59:48.758713Z","shell.execute_reply.started":"2021-07-13T02:59:47.414348Z","shell.execute_reply":"2021-07-13T02:59:48.757573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CQT\n\n[In mathematics and signal processing, the constant-Q transform, simply known as CQT transforms a data series to the frequency domain. It is related to the Fourier transform and very closely related to the complex Morlet wavelet transform](https://en.wikipedia.org/wiki/Constant-Q_transform)","metadata":{}},{"cell_type":"code","source":"def get_cqt(signal):    \n    return np.abs(librosa.cqt(signal, sr=SR, hop_length=5*8, n_bins=40, bins_per_octave=12))    ","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:48.760630Z","iopub.execute_input":"2021-07-13T02:59:48.761043Z","iopub.status.idle":"2021-07-13T02:59:48.767636Z","shell.execute_reply.started":"2021-07-13T02:59:48.761005Z","shell.execute_reply":"2021-07-13T02:59:48.766243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_transformation(id0, id1, get_cqt, \"CQT\")","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:48.769134Z","iopub.execute_input":"2021-07-13T02:59:48.769491Z","iopub.status.idle":"2021-07-13T02:59:49.867100Z","shell.execute_reply.started":"2021-07-13T02:59:48.769454Z","shell.execute_reply":"2021-07-13T02:59:49.865863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CWT Morlet\n\n[In mathematics, the continuous wavelet transform (CWT) is a formal (i.e., non-numerical) tool that provides an overcomplete representation of a signal by letting the translation and scale parameter of the wavelets vary continuously](https://en.wikipedia.org/wiki/Continuous_wavelet_transform)","metadata":{}},{"cell_type":"code","source":"def get_cwt_morlet(signal):\n    return np.abs(scipy.signal.cwt(signal, scipy.signal.morlet, np.arange(1, 10)))","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:49.868754Z","iopub.execute_input":"2021-07-13T02:59:49.869155Z","iopub.status.idle":"2021-07-13T02:59:49.875129Z","shell.execute_reply.started":"2021-07-13T02:59:49.869117Z","shell.execute_reply":"2021-07-13T02:59:49.874029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_transformation(id0, id1, get_cwt_morlet, \"CWT Morlet\")","metadata":{"execution":{"iopub.status.busy":"2021-07-13T02:59:49.876906Z","iopub.execute_input":"2021-07-13T02:59:49.877394Z","iopub.status.idle":"2021-07-13T02:59:51.327341Z","shell.execute_reply.started":"2021-07-13T02:59:49.877346Z","shell.execute_reply":"2021-07-13T02:59:51.326161Z"},"trusted":true},"execution_count":null,"outputs":[]}]}