{"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 h5py # import to read hdf5\nfrom pathlib import Path\nfrom glob import glob\nfrom tqdm import tqdm\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom scipy import signal\nfrom scipy.fft import fftshift","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-16T21:55:07.610345Z","iopub.execute_input":"2022-10-16T21:55:07.610753Z","iopub.status.idle":"2022-10-16T21:55:08.28094Z","shell.execute_reply.started":"2022-10-16T21:55:07.610718Z","shell.execute_reply":"2022-10-16T21:55:08.279375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load train files\ntrain_files = glob(f\"../input/g2net-detecting-continuous-gravitational-waves/train/*.hdf5\")\ntrain_files[:5]","metadata":{"execution":{"iopub.status.busy":"2022-10-16T21:55:08.282944Z","iopub.execute_input":"2022-10-16T21:55:08.283283Z","iopub.status.idle":"2022-10-16T21:55:08.334392Z","shell.execute_reply.started":"2022-10-16T21:55:08.283251Z","shell.execute_reply":"2022-10-16T21:55:08.332937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Idea from this function takes from this notebook (upvoted 😇): https://www.kaggle.com/code/ayuraj/g2net-understand-the-data\ndef read_data(file):\n    with h5py.File(file, \"r\") as f:\n        filename = file.stem\n        f = f[filename]\n        h1 = f[\"H1\"]\n        l1 = f[\"L1\"]\n        freq_hz = list(f[\"frequency_Hz\"])\n        \n        h1_stft = h1[\"SFTs\"][()]\n        h1_timestamp = h1[\"timestamps_GPS\"][()]\n        # H2 data\n        l1_stft = l1[\"SFTs\"][()]\n        l1_timestamp = l1[\"timestamps_GPS\"][()]\n        \n        \n        medatada = {\n            \"H1_fft_shape\": h1_stft.shape,\n            \"H1_timestamp_shape\": h1_timestamp.shape,\n            \"L1_fft_shape\": l1_stft.shape,\n            \"L1_timestamp_shape\": l1_timestamp.shape,\n            \"freq_hz_shape\": np.shape(freq_hz)\n        }\n        return {\n            \"H1\":\n                {\n                    \"FFT\": h1_stft,\n                    \"timestamp\": h1_timestamp,\n                },\n            \"L1\":\n                {\n                    \"FFT\": l1_stft,\n                    \"timestamp\": l1_timestamp,\n                },\n            \"freq_hz\": freq_hz\n        }, medatada\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-16T21:55:08.336452Z","iopub.execute_input":"2022-10-16T21:55:08.33678Z","iopub.status.idle":"2022-10-16T21:55:08.347634Z","shell.execute_reply.started":"2022-10-16T21:55:08.336751Z","shell.execute_reply":"2022-10-16T21:55:08.346356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's see the two examples of what the read_file returns:","metadata":{}},{"cell_type":"code","source":"example_file = train_files[0]\n_, metadata = read_data(Path(example_file))\nmetadata","metadata":{"execution":{"iopub.status.busy":"2022-10-16T21:55:22.281913Z","iopub.execute_input":"2022-10-16T21:55:22.282418Z","iopub.status.idle":"2022-10-16T21:55:22.806669Z","shell.execute_reply.started":"2022-10-16T21:55:22.282372Z","shell.execute_reply":"2022-10-16T21:55:22.805859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's see how the corresponding waveform looks like when converting from the spectrogram at t=1:","metadata":{}},{"cell_type":"code","source":"# target = 1\n# example_file = \"../input/g2net-detecting-continuous-gravitational-waves/train/05c0675fe.hdf5\"\nexample_file = \"../input/g2net-detecting-continuous-gravitational-waves/train/acdc07a39.hdf5\"\n\n# target = 0\n# example_file = \"../input/g2net-detecting-continuous-gravitational-waves/train/e877b6a70.hdf5\"\ndata, _ = read_data(Path(example_file))\n\nx = (np.real(data[\"L1\"][\"FFT\"]) * np.cos(np.imag(data[\"L1\"][\"FFT\"])) )[:,1]*10**23\nx = np.real(np.fft.ifftn(x))\nplt.plot(x)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T22:01:44.260723Z","iopub.execute_input":"2022-10-16T22:01:44.262081Z","iopub.status.idle":"2022-10-16T22:01:45.080018Z","shell.execute_reply.started":"2022-10-16T22:01:44.262035Z","shell.execute_reply":"2022-10-16T22:01:45.07863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = (np.real(data[\"L1\"][\"FFT\"]) * np.cos(np.imag(data[\"L1\"][\"FFT\"])) )*10**23\nwaveform = np.array([])\nfor window in tqdm(range(x.shape[1])):\n    waveform = np.append(waveform, np.real(np.fft.ifftn(x[:,window])))\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T22:08:34.899879Z","iopub.execute_input":"2022-10-16T22:08:34.900311Z","iopub.status.idle":"2022-10-16T22:08:40.879215Z","shell.execute_reply.started":"2022-10-16T22:08:34.900272Z","shell.execute_reply":"2022-10-16T22:08:40.877865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"window_size = 1200\nn_blocks = int(len(waveform) / window_size)\nwaveform_averaged = np.append([0]*(window_size*n_blocks - len(waveform)), waveform)\nwaveform_averaged = np.reshape(waveform_averaged, (n_blocks, window_size))\nwaveform_averaged = np.average(waveform_averaged, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-16T22:10:22.540119Z","iopub.execute_input":"2022-10-16T22:10:22.540512Z","iopub.status.idle":"2022-10-16T22:10:22.551054Z","shell.execute_reply.started":"2022-10-16T22:10:22.540479Z","shell.execute_reply":"2022-10-16T22:10:22.549582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(waveform_averaged)\n# plt.xlim(0, 300)","metadata":{"execution":{"iopub.status.busy":"2022-10-16T22:10:22.85275Z","iopub.execute_input":"2022-10-16T22:10:22.853504Z","iopub.status.idle":"2022-10-16T22:10:23.086476Z","shell.execute_reply.started":"2022-10-16T22:10:22.853464Z","shell.execute_reply":"2022-10-16T22:10:23.085621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.abs(np.fft.fftn(waveform_averaged)))","metadata":{"execution":{"iopub.status.busy":"2022-10-16T22:10:23.341248Z","iopub.execute_input":"2022-10-16T22:10:23.342247Z","iopub.status.idle":"2022-10-16T22:10:23.59326Z","shell.execute_reply.started":"2022-10-16T22:10:23.342204Z","shell.execute_reply":"2022-10-16T22:10:23.592057Z"},"trusted":true},"execution_count":null,"outputs":[]}]}