{"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":"<div align=\"center\">\n<font size=\"6\"> G2Net Gravitational Wave Detection  </font>  \n</div> \n\n\n<div align=\"center\">\n<font size=\"4\"> Find gravitational wave signals from binary black hole collisions  </font>  \n</div> ","metadata":{}},{"cell_type":"markdown","source":"<img align=\"left\" src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/header.png\" data-canonical-src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/header.png\" width=\"1350\" />","metadata":{}},{"cell_type":"markdown","source":"<a id=\"0\"></a>\n<h1 style='background:#0788f0; font-size:200%; border:0; color:white;'><center> Table of Contents</center></h1>\n\n1. [Gravitational Wave Detection](#1)\n2. [Competition Overview](#2)\n3. [Import packages](#3)\n4. [Processing GW data: GWpy](#4)  \n    4.1 [Time Series](#4.1)  \n    4.2 [Spectrograms](#4.2)  \n   \n[References](#100)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<h2 style='background:#0788f0; font-size:200%; border:0; color:white'><center> 1 Graviational Waves Detection <center><h0>","metadata":{}},{"cell_type":"markdown","source":"On September 14, 2015 at 09:50:45 UTC the two detectors of the Laser Interferometer Gravitational-Wave Observatory ([LIGO](https://www.ligo.org/)) simultaneously observed a transient gravitational-wave signal. \n\n<img style=\"float:left; padding-right:10px\" src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/ripple.jpg\" data-canonical-src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/ripple.jpg\" width=\"225\" height=\"225\"/>  \n\nIt required the collaboration of experts in physics, mathematics, information science, and computing. GW signals have led researchers to observe a new population of massive, stellar-origin **black holes (BH)**, to unlock the mysteries of neutron star mergers, and to measure the expansion of the Universe. These signals are unimaginably **tiny ripples** in the fabric of space-time and even though the global network of GW detectors are some of the most sensitive instruments on the planet, the signals are buried in detector noise. Analysis of GW data and the detection of these signals is a crucial mission for the growing global network of increasingly sensitive GW detectors. These challenges in data analysis and noise characterization could be solved with the help of data science. As with the multi-disciplined approach to the discovery of GWs, additional expertise will be needed to further GW research. In particular, social and natural sciences have taken an interest in machine learning, deep learning, classification problems, data mining, and visualization to develop new techniques and algorithms to efficiently handle complex and massive data sets. The increase in computing power and the development of innovative techniques for the rapid analysis of data will be vital to the exciting new field of GW Astronomy. Potential outcomes may include increased sensitivity to GW signals, application to control and feedback systems for next-generation detectors, noise removal, data conditioning tools, and signal characterization.  \n\n\n<img style=\"float:left; padding-right:10px\" src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/bh.png\" data-canonical-src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/bh.png\" width=\"70\" height=\"70\"/>  \n\n[G2Net](www.g2net.eu) is a network of **Gravitational Wave, Geophysics and Machine Learning**. Via an Action from [COST](www.cost.eu) (European Cooperation in Science and Technology), a funding agency for research and innovation networks, G2Net aims to create a broad network of scientists. From four different areas of expertise, namely GW physics, Geophysics, Computing Science and Robotics, these scientists have agreed on a common goal of tackling challenges in data analysis and noise characterization for GW detectors.","metadata":{}},{"cell_type":"code","source":"import IPython.display\nIPython.display.YouTubeVideo('B4XzLDM3Py8', width=768, height=524)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:10:40.245459Z","iopub.execute_input":"2021-07-03T01:10:40.246075Z","iopub.status.idle":"2021-07-03T01:10:40.356075Z","shell.execute_reply.started":"2021-07-03T01:10:40.245961Z","shell.execute_reply":"2021-07-03T01:10:40.35497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n<h2 style='background:#0788f0; font-size:200%; border:0; color:white'><center> 2 Competition Overview <center><h0>","metadata":{}},{"cell_type":"markdown","source":"<img style=\"float:left; padding-right:10px\" src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/ego_logo.png\" data-canonical-src=\"https://raw.githubusercontent.com/kabartay/kaggle-g2net-gravitational-wave-detection/main/pics/ego_logo.png\" width=\"70\" height=\"70\"/> \n\nThis competition is hosted by [European Gravitational Observatory (EGO)](https://www.ego-gw.it/). We **aim to detect GW signals** from the mergers of **binary black holes (BBH)**.  It is assumed that we build a model to analyze simulated GW time-series data from a network of 3 Earth-based GW interferometers (LIGO Hanford, LIGO Livingston, and Virgo). We are provided with a **72 GB time-series dataset** of time series data containing simulated GW measurements. Each time series contains either detector noise or detector noise plus a simulated GW signal. \n\n## Task\nIdentify when a signal is present in the data (`target=1`).  \n\n## Files\n- **train/** - the training set files, one npy file per observation; labels are provided in a files shown below   \n- **test/** - the test set files; you must predict the probability that the observation contains a gravitational wave   \n- **training_labels.csv** - target values of whether the associated signal contains a gravitational wave   \n- **sample_submission.csv** - a sample submission file in the correct format","metadata":{}},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n<h2 style='background:#0788f0; font-size:200%; border:0; color:white'><center> 3 Import packages <center><h0>","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport json\nimport random\n\nimport numpy as np\nimport pandas as pd\nimport pickle as pkl\n\nimport cv2\nfrom PIL import Image\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n#%matplotlib inline\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:10:40.357539Z","iopub.execute_input":"2021-07-03T01:10:40.357903Z","iopub.status.idle":"2021-07-03T01:10:41.395948Z","shell.execute_reply.started":"2021-07-03T01:10:40.357873Z","shell.execute_reply":"2021-07-03T01:10:41.394845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '/kaggle/input/g2net-gravitational-wave-detection/'\ndf_labels = pd.read_csv(PATH+'training_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:10:41.398106Z","iopub.execute_input":"2021-07-03T01:10:41.398493Z","iopub.status.idle":"2021-07-03T01:10:41.849762Z","shell.execute_reply.started":"2021-07-03T01:10:41.398458Z","shell.execute_reply":"2021-07-03T01:10:41.848699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,6))\nax = sns.countplot(x = df_labels['target'])","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:14.077208Z","iopub.status.idle":"2021-07-03T01:11:14.077683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Each data sample (`.npy` file) contains 3 time series (1 for each detector: LIGO Hanford, LIGO Livingston, and Virgo) and each spans 2 sec and is sampled at 2048 Hz.","metadata":{}},{"cell_type":"code","source":"# Example \nevent = '00000e74ad'\nfile_npy_ev = '/kaggle/input/g2net-gravitational-wave-detection/train/0/0/0/{}.npy'.format(event)\nprint(np.load(file_npy_ev).shape)\nnp.load(file_npy_ev)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:10:42.059066Z","iopub.execute_input":"2021-07-03T01:10:42.0595Z","iopub.status.idle":"2021-07-03T01:10:42.083118Z","shell.execute_reply.started":"2021-07-03T01:10:42.059458Z","shell.execute_reply":"2021-07-03T01:10:42.082001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We have GW\ndf_labels[df_labels['id'] == event]","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:10:42.084766Z","iopub.execute_input":"2021-07-03T01:10:42.085231Z","iopub.status.idle":"2021-07-03T01:10:42.190624Z","shell.execute_reply.started":"2021-07-03T01:10:42.085185Z","shell.execute_reply":"2021-07-03T01:10:42.189447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n<h2 style='background:#0788f0; font-size:200%; border:0; color:white'><center> 4 Processing GW data: GWpy <center><h0>","metadata":{}},{"cell_type":"markdown","source":"[GWpy](https://gwpy.github.io/docs/latest/index.html) is a collaboration-driven Python package providing tools for studying data from ground-based GW detectors. It provides a user-friendly, intuitive interface to the common time-domain and frequency-domain data produced by the [LIGO](https://www.ligo.org/) and [Virgo](https://www.ego-gw.it/) instruments and their analysis, with easy-to-follow tutorials at each step.","metadata":{}},{"cell_type":"code","source":"# The recommended way of installing GWpy is:\n# !conda install -c conda-forge gwpy  # with Conda\n# !python -m pip install gwpy          # with Pip\n\ntry:\n    from gwpy.timeseries import TimeSeries, TimeSeriesDict\n    from gwpy.plot import Plot\nexcept:\n    ! python -m pip install -q gwpy\n    from gwpy.timeseries import TimeSeries, TimeSeriesDict\n    from gwpy.plot import Plot","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-03T01:10:42.19199Z","iopub.execute_input":"2021-07-03T01:10:42.192397Z","iopub.status.idle":"2021-07-03T01:11:01.730583Z","shell.execute_reply.started":"2021-07-03T01:10:42.192366Z","shell.execute_reply":"2021-07-03T01:11:01.729546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_npy_data(file):\n    \"\"\"Get .npy file content.\"\"\"\n    data_npy = np.load(file)\n    Hanford    = TimeSeries(data_npy[0,:], sample_rate=2048)\n    Livingston = TimeSeries(data_npy[1,:], sample_rate=2048)\n    Virgo      = TimeSeries(data_npy[2,:], sample_rate=2048)\n    return Hanford, Livingston, Virgo","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:01.733242Z","iopub.execute_input":"2021-07-03T01:11:01.73356Z","iopub.status.idle":"2021-07-03T01:11:01.741706Z","shell.execute_reply.started":"2021-07-03T01:11:01.73353Z","shell.execute_reply":"2021-07-03T01:11:01.739757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get detectors data\nHanford, Livingston, Virgo = get_npy_data(file_npy_ev)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:01.743438Z","iopub.execute_input":"2021-07-03T01:11:01.743751Z","iopub.status.idle":"2021-07-03T01:11:01.759091Z","shell.execute_reply.started":"2021-07-03T01:11:01.743722Z","shell.execute_reply":"2021-07-03T01:11:01.758014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Hanford.shape)\nHanford","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:01.760558Z","iopub.execute_input":"2021-07-03T01:11:01.760948Z","iopub.status.idle":"2021-07-03T01:11:01.772346Z","shell.execute_reply.started":"2021-07-03T01:11:01.760917Z","shell.execute_reply":"2021-07-03T01:11:01.771114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 9))\nsns.histplot(data=Hanford)\nplt.xlim(-2e-20, 2e-20)\nplt.xlabel('Amplitude [strain]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:01.773918Z","iopub.execute_input":"2021-07-03T01:11:01.774228Z","iopub.status.idle":"2021-07-03T01:11:02.399797Z","shell.execute_reply.started":"2021-07-03T01:11:01.774199Z","shell.execute_reply":"2021-07-03T01:11:02.398686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 9))\nsns.histplot(data=Livingston)\nplt.xlim(-2e-20, 2e-20)\nplt.xlabel('Amplitude [strain]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:02.401327Z","iopub.execute_input":"2021-07-03T01:11:02.401743Z","iopub.status.idle":"2021-07-03T01:11:02.860938Z","shell.execute_reply.started":"2021-07-03T01:11:02.4017Z","shell.execute_reply":"2021-07-03T01:11:02.859746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 9))\nsns.histplot(data=Virgo)\nplt.xlim(-0.5e-20, 0.5e-20)\nplt.xlabel('Amplitude [strain]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:02.862535Z","iopub.execute_input":"2021-07-03T01:11:02.862977Z","iopub.status.idle":"2021-07-03T01:11:03.349448Z","shell.execute_reply.started":"2021-07-03T01:11:02.862934Z","shell.execute_reply":"2021-07-03T01:11:03.348551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 9))\nsns.histplot(data=Hanford)\nsns.histplot(data=Livingston)\n#sns.histplot(data=Virgo)\nplt.xlim(-2e-20, 2e-20)\nplt.xlabel('Amplitude [strain]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:03.350699Z","iopub.execute_input":"2021-07-03T01:11:03.350988Z","iopub.status.idle":"2021-07-03T01:11:03.880495Z","shell.execute_reply.started":"2021-07-03T01:11:03.350961Z","shell.execute_reply":"2021-07-03T01:11:03.87938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4.1\"></a>\n<h3 style='background:#0788f0; font-size:200%; border:0; color:white'><center> 4.1 Time Series <center><h0>","metadata":{}},{"cell_type":"code","source":"# Check out: \n# https://gwpy.github.io/docs/latest/overview.html\n# https://gwpy.github.io/docs/latest/plot/index.html\n\ndef plot_time_series(Hanford, Livingston, Virgo):\n    \"\"\"Plot time series. Separate subplots.\n    Detectors order: LIGO Hanford, LIGO Livingston, and Virgo\n    \"\"\"\n    plot = Plot(Hanford, Livingston, Virgo, \n                separate=True, \n                sharex=True, \n                figsize=[18, 12])\n    ax = plot.gca()\n    ax.set_xlim(0,2)\n    ax.set_xlabel('Time [s]')\n    plot.show()\n    \ndef plot_time_series_all(Hanford, Livingston, Virgo):\n    \"\"\"Plot time series. All detectors together.\"\"\"\n    plot = Plot(figsize=(18, 4))\n    ax = plot.add_subplot()\n    ax.plot(Hanford, color='gwpy:ligo-hanford', label='LIGO-Hanford')\n    ax.plot(Livingston, color='gwpy:ligo-livingston', label='LIGO-Livingston')\n    ax.plot(Virgo, color='gwpy:virgo', label='Virgo')\n    ax.set_ylabel('Amplitude [strain]')\n    ax.set_xlim(0, 2)\n    ax.set_ylim(-2e-20, 2e-20)\n    ax.legend()\n    \ndef plot_time_series_LIGO(Hanford, Livingston):\n    \"\"\"Plot time series. LIGO detectors.\"\"\"\n    plot = Plot(figsize=(18, 4))\n    ax = plot.add_subplot()\n    ax.plot(Hanford, color='gwpy:ligo-hanford', label='LIGO-Hanford')\n    ax.plot(Livingston, color='gwpy:ligo-livingston', label='LIGO-Livingston')\n    ax.set_ylabel('Amplitude [strain]')\n    ax.set_xlim(0, 2)\n    ax.set_ylim(-2e-20, 2e-20)\n    ax.legend()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:03.881735Z","iopub.execute_input":"2021-07-03T01:11:03.882008Z","iopub.status.idle":"2021-07-03T01:11:03.892483Z","shell.execute_reply.started":"2021-07-03T01:11:03.881982Z","shell.execute_reply":"2021-07-03T01:11:03.891571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_time_series(Hanford, Livingston, Virgo)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:03.893814Z","iopub.execute_input":"2021-07-03T01:11:03.89412Z","iopub.status.idle":"2021-07-03T01:11:07.626881Z","shell.execute_reply.started":"2021-07-03T01:11:03.894085Z","shell.execute_reply":"2021-07-03T01:11:07.626105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_time_series_LIGO(Hanford, Livingston)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:07.628105Z","iopub.execute_input":"2021-07-03T01:11:07.628544Z","iopub.status.idle":"2021-07-03T01:11:08.109039Z","shell.execute_reply.started":"2021-07-03T01:11:07.6285Z","shell.execute_reply":"2021-07-03T01:11:08.108039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_time_series_all(Hanford, Livingston, Virgo)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:08.110312Z","iopub.execute_input":"2021-07-03T01:11:08.110623Z","iopub.status.idle":"2021-07-03T01:11:08.639076Z","shell.execute_reply.started":"2021-07-03T01:11:08.110578Z","shell.execute_reply":"2021-07-03T01:11:08.638004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4.2\"></a>\n<h2 style='background:#0788f0; font-size:200%; border:0; color:white'><center> 4.2 Spectrograms <center><h0>","metadata":{}},{"cell_type":"markdown","source":"### Generate the Q-transform of a TimeSeries\nOne of the most useful tools for filtering and visualising short-duration features in a **TimeSeries** is the **Q-transform**. This is regularly used by the Detector Characterization working groups of the LIGO Scientific Collaboration and the Virgo Collaboration to produce high-resolution time-frequency maps of transient noise (glitches) and potential gravitational-wave signals. Check out [here](https://gwpy.github.io/docs/latest/examples/timeseries/qscan.html).\n","metadata":{}},{"cell_type":"code","source":"# Check out:\n# https://gwpy.github.io/docs/latest/examples/timeseries/qscan.html\n\ndef plot_spectrograms(detector_data, detector_name, event, target_ev, grid):\n    \"\"\"Plot spectrograms for specific detector's event.\"\"\"\n    \n    qspecgram = detector_data.q_transform(outseg=(0.0, 2.0))\n    \n    plot = qspecgram.plot(figsize=[12, 10])\n    ax = plot.gca()\n    ax.set_title('{}. Event: {}. Target: {}'.format(detector_name, event, target_ev))\n    ax.set_xlabel('Time [s]')\n    ax.set_ylabel('Frequency [Hz]')\n    #ax.set_yscale('log')\n    ax.grid(grid)\n    ax.colorbar(cmap='viridis', label='Normalized energy')\n    plot.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:08.640732Z","iopub.execute_input":"2021-07-03T01:11:08.641114Z","iopub.status.idle":"2021-07-03T01:11:08.6492Z","shell.execute_reply.started":"2021-07-03T01:11:08.641077Z","shell.execute_reply":"2021-07-03T01:11:08.648109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get data and target value for specific event\nHanford, Livingston, Virgo = get_npy_data(file_npy_ev)\ntarget_ev = df_labels[df_labels['id'] == event]['target'].iloc[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:08.650666Z","iopub.execute_input":"2021-07-03T01:11:08.651009Z","iopub.status.idle":"2021-07-03T01:11:08.766216Z","shell.execute_reply.started":"2021-07-03T01:11:08.650979Z","shell.execute_reply":"2021-07-03T01:11:08.765198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms(Hanford, 'LIGO Hanford', event, target_ev, False)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:08.767478Z","iopub.execute_input":"2021-07-03T01:11:08.767848Z","iopub.status.idle":"2021-07-03T01:11:10.670027Z","shell.execute_reply.started":"2021-07-03T01:11:08.767816Z","shell.execute_reply":"2021-07-03T01:11:10.668959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms(Livingston, 'LIGO Livingston', event, target_ev, False)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:10.671735Z","iopub.execute_input":"2021-07-03T01:11:10.672172Z","iopub.status.idle":"2021-07-03T01:11:12.366897Z","shell.execute_reply.started":"2021-07-03T01:11:10.672118Z","shell.execute_reply":"2021-07-03T01:11:12.366086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms(Virgo, 'Virgo', event, target_ev, True)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:11:12.369716Z","iopub.execute_input":"2021-07-03T01:11:12.370203Z","iopub.status.idle":"2021-07-03T01:11:14.064716Z","shell.execute_reply.started":"2021-07-03T01:11:12.370146Z","shell.execute_reply":"2021-07-03T01:11:14.063633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculating a Spectrogram from a TimeSeries\n# The time-frequency Spectrogram of a TimeSeries can be calculated using the spectrogram() method. \n# We can extend previous examples of plotting a TimeSeries with calculation of a Spectrogram with a 2-second stride:\n# https://gwpy.github.io/docs/latest/spectrogram/index.html#calculating-a-spectrogram-from-a-timeseries\n# https://gwpy.github.io/docs/latest/api/gwpy.spectrogram.Spectrogram.html#gwpy.spectrogram.Spectrogram\n# https://gwpy.github.io/docs/stable/api/gwpy.timeseries.TimeSeries.html\n# https://gwpy.github.io/docs/stable/api/gwpy.timeseries.TimeSeries.html#gwpy.timeseries.TimeSeries.spectrogram\n\ndef plot_spectrograms_from_timeseries(spectrogram):\n    \"\"\"Plot spectrograms from Time Series.\"\"\"\n    plot = spectrogram.plot(figsize=[12, 10], \n                       norm='log', \n                       #vmin=1e-24, \n                       #vmax=1e-21\n                      )\n    ax = plot.gca()\n    ax.set_ylim(1, 1000)\n    ax.set_yscale('log')\n    ax.set_xlabel('Time [s]')\n    ax.set_ylabel('Frequency [Hz]')\n    ax.colorbar(label='GW strain ASD [strain/$\\sqrt{\\mathrm{Hz}}$]')\n    plot.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:38.035547Z","iopub.execute_input":"2021-07-03T01:30:38.035953Z","iopub.status.idle":"2021-07-03T01:30:38.042021Z","shell.execute_reply.started":"2021-07-03T01:30:38.03592Z","shell.execute_reply":"2021-07-03T01:30:38.041208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Rules for spectrogram function:\n- `stride` cannot be greater than the duration of this TimeSeries\n- `fftlength` cannot be greater than stride\n- `overlap` must be less than fftlength\n- `window` should be not longer than input signal","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:26:17.426192Z","iopub.execute_input":"2021-07-03T01:26:17.426797Z","iopub.status.idle":"2021-07-03T01:26:17.436865Z","shell.execute_reply.started":"2021-07-03T01:26:17.42676Z","shell.execute_reply":"2021-07-03T01:26:17.435809Z"}}},{"cell_type":"code","source":"H_spec = Hanford.spectrogram(stride=2,       # number of seconds in single PSD (column of spectrogram). \n                             fftlength=0.75, # number of seconds in single FFT\n                             overlap=0,      # number of seconds of overlap between FFTs, defaults to the recommended overlap for the given window (if given), or 0\n                             nproc=8         # number of CPUs to use in parallel processing of FFTs\n                            ) ** (1/2.)      # sqrt","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:39.986127Z","iopub.execute_input":"2021-07-03T01:30:39.986557Z","iopub.status.idle":"2021-07-03T01:30:39.997089Z","shell.execute_reply.started":"2021-07-03T01:30:39.98652Z","shell.execute_reply":"2021-07-03T01:30:39.995979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L_spec = Livingston.spectrogram(stride=2,       # number of seconds in single PSD (column of spectrogram). \n                                fftlength=0.75, # number of seconds in single FFT\n                                overlap=0,      # number of seconds of overlap between FFTs, defaults to the recommended overlap for the given window (if given), or 0\n                                nproc=8         # number of CPUs to use in parallel processing of FFTs\n                                ) ** (1/2.)      # sqrt","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:41.740365Z","iopub.execute_input":"2021-07-03T01:30:41.740749Z","iopub.status.idle":"2021-07-03T01:30:41.750448Z","shell.execute_reply.started":"2021-07-03T01:30:41.740713Z","shell.execute_reply":"2021-07-03T01:30:41.749435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"V_spec = Virgo.spectrogram(stride=2,       # number of seconds in single PSD (column of spectrogram). \n                           fftlength=0.75, # number of seconds in single FFT\n                           overlap=0,      # number of seconds of overlap between FFTs, defaults to the recommended overlap for the given window (if given), or 0\n                           nproc=8         # number of CPUs to use in parallel processing of FFTs\n                          ) ** (1/2.)      # sqrt","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:42.117205Z","iopub.execute_input":"2021-07-03T01:30:42.117612Z","iopub.status.idle":"2021-07-03T01:30:42.127199Z","shell.execute_reply.started":"2021-07-03T01:30:42.117567Z","shell.execute_reply":"2021-07-03T01:30:42.126217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms_from_timeseries(H_spec)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:50.156462Z","iopub.execute_input":"2021-07-03T01:30:50.156843Z","iopub.status.idle":"2021-07-03T01:30:52.244275Z","shell.execute_reply.started":"2021-07-03T01:30:50.15681Z","shell.execute_reply":"2021-07-03T01:30:52.243246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms_from_timeseries(L_spec)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:52.245926Z","iopub.execute_input":"2021-07-03T01:30:52.246229Z","iopub.status.idle":"2021-07-03T01:30:54.568376Z","shell.execute_reply.started":"2021-07-03T01:30:52.246201Z","shell.execute_reply":"2021-07-03T01:30:54.56725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrograms_from_timeseries(V_spec)","metadata":{"execution":{"iopub.status.busy":"2021-07-03T01:30:54.57004Z","iopub.execute_input":"2021-07-03T01:30:54.570397Z","iopub.status.idle":"2021-07-03T01:30:56.633542Z","shell.execute_reply.started":"2021-07-03T01:30:54.570364Z","shell.execute_reply":"2021-07-03T01:30:56.632396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"100\"></a>\n<h2 style='background:#0788f0; font-size:200%; border:0; color:white'><center> References <center><h0>","metadata":{}},{"cell_type":"markdown","source":"- [G2Net](www.g2net.eu). COST Action CA17137: A network for Gravitational Waves, Geophysics and Machine Learning.\n- [COST](www.cost.eu): European Cooperation in Science and Technology.\n- [EGO](https://www.ego-gw.it/): European Gravitational Observatory.\n- [LIGO](https://www.ligo.org/): Laser Interferometer Gravitational-Wave Observatory.\n- [LIGO](https://www.ligo.caltech.edu/) at CalTech.\n- B. P. Abbott et al. (LIGO Scientific Collaboration and Virgo Collaboration) Phys. Rev. Lett. 116, 061102 [DOI:10.1103/PhysRevLett.116.061102](https://link.aps.org/doi/10.1103/PhysRevLett.116.061102)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}