{"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 os\nimport json\nimport random\nimport collections\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-14T11:08:16.472584Z","iopub.execute_input":"2023-04-14T11:08:16.473019Z","iopub.status.idle":"2023-04-14T11:08:16.479766Z","shell.execute_reply.started":"2023-04-14T11:08:16.472981Z","shell.execute_reply":"2023-04-14T11:08:16.478385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Visualization**","metadata":{}},{"cell_type":"code","source":"#the function returns the file path of the image corresponding to the given image_id.\ndef imageIdToPath(imageId: str, isTrain: bool = True) -> str:\n    folder = \"train\" if isTrain else \"test\"\n    return \"../input/g2net-gravitational-wave-detection/{}/{}/{}/{}/{}.npy\".format(folder, imageId[0], imageId[1], imageId[2], imageId)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:16.482093Z","iopub.execute_input":"2023-04-14T11:08:16.482511Z","iopub.status.idle":"2023-04-14T11:08:16.495557Z","shell.execute_reply.started":"2023-04-14T11:08:16.482474Z","shell.execute_reply":"2023-04-14T11:08:16.494312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainDf = pd.read_csv(\"../input/g2net-gravitational-wave-detection/training_labels.csv\")\ntrainDf","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:16.498277Z","iopub.execute_input":"2023-04-14T11:08:16.499604Z","iopub.status.idle":"2023-04-14T11:08:16.825875Z","shell.execute_reply.started":"2023-04-14T11:08:16.499551Z","shell.execute_reply":"2023-04-14T11:08:16.824221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create a count plot of the \"target\" variable from a pandas dataframe called 'trainDf'\n\nsns.countplot(data=trainDf, x=\"target\",palette=\"dark:#5A9_r\")","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:16.828380Z","iopub.execute_input":"2023-04-14T11:08:16.828808Z","iopub.status.idle":"2023-04-14T11:08:17.087493Z","shell.execute_reply.started":"2023-04-14T11:08:16.828773Z","shell.execute_reply":"2023-04-14T11:08:17.086070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef visualizeSample(_id, target, colors=(\"red\", \"blue\", \"green\"), signal_names=(\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\")):\n    # get file path\n    path = imageIdToPath(_id)\n    # load data\n    x = np.load(path)\n    \n    # set up the figure\n    fig, axs = plt.subplots(nrows=4, ncols=1, figsize=(16,7), gridspec_kw={\"height_ratios\": [1,1,1,2]})\n    \n    # plot each signal\n    for i in range(3):\n        axs[i].plot(x[i], color=colors[i])\n        axs[i].legend([signal_names[i]], fontsize=12, loc=\"lower right\")\n    # plot all three signals overlaid on each other\n    axs[3].plot(x[0], color=colors[0], label=signal_names[0])\n    axs[3].plot(x[1], color=colors[1], label=signal_names[1])\n    axs[3].plot(x[2], color=colors[2], label=signal_names[2])\n    axs[3].legend(fontsize=12, loc=\"lower right\")\n    \n    # set the title\n    fig.suptitle(f\"id: {_id} target: {target}\", fontsize=16)\n    \n    # show the plot\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:17.090272Z","iopub.execute_input":"2023-04-14T11:08:17.090726Z","iopub.status.idle":"2023-04-14T11:08:17.103071Z","shell.execute_reply.started":"2023-04-14T11:08:17.090687Z","shell.execute_reply":"2023-04-14T11:08:17.101408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(trainDf.index.tolist(), 3):\n    _id = trainDf.iloc[i][\"id\"]\n    target = trainDf.iloc[i][\"target\"]\n\n    visualizeSample(_id, target)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:17.104605Z","iopub.execute_input":"2023-04-14T11:08:17.105001Z","iopub.status.idle":"2023-04-14T11:08:18.944115Z","shell.execute_reply.started":"2023-04-14T11:08:17.104965Z","shell.execute_reply":"2023-04-14T11:08:18.943153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Spectrogram**","metadata":{}},{"cell_type":"code","source":"import librosa\nimport librosa.display","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:18.945527Z","iopub.execute_input":"2023-04-14T11:08:18.946563Z","iopub.status.idle":"2023-04-14T11:08:18.951704Z","shell.execute_reply.started":"2023-04-14T11:08:18.946522Z","shell.execute_reply":"2023-04-14T11:08:18.950467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualizeSampleSpectogram(_id, target, signal_names=(\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\"), sr=2048, fmin=20, fmax=1024):\n    # Load the signal data\n    x = np.load(imageIdToPath(_id))\n    \n    # Create a figure with a size of 16x5 inches\n    plt.figure(figsize=(16, 5))\n    \n    # Loop over the three signals and plot their spectrograms\n    for i in range(3):\n        # Compute the short-time Fourier transform (STFT) of the signal\n        X = librosa.stft(x[i] / x[i].max())\n        \n        # Convert the amplitude of the STFT to decibels (dB)\n        Xdb = librosa.amplitude_to_db(abs(X))\n        \n        # Create a subplot for each signal\n        plt.subplot(1, 3, i + 1)\n        \n        # Display the spectrogram of the signal\n        librosa.display.specshow(Xdb, sr=sr, x_axis=\"time\", y_axis=\"linear\", fmin=fmin, fmax=fmax, vmin=-50, vmax=50) \n        \n        # Add a colorbar to the plot\n        plt.colorbar()\n        \n        # Add the name of the signal as the title of the subplot\n        plt.title(signal_names[i], fontsize=14)\n\n    # Add a main title to the plot\n    plt.suptitle(f\"id: {_id} target: {target}\", fontsize=16)\n    \n    # Show the plot\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:18.953323Z","iopub.execute_input":"2023-04-14T11:08:18.953884Z","iopub.status.idle":"2023-04-14T11:08:18.965301Z","shell.execute_reply.started":"2023-04-14T11:08:18.953825Z","shell.execute_reply":"2023-04-14T11:08:18.963866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(trainDf.index.tolist(), 3):\n    _id = trainDf.iloc[i][\"id\"]\n    target = trainDf.iloc[i][\"target\"]\n\n    visualizeSampleSpectogram(_id, target)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:18.967129Z","iopub.execute_input":"2023-04-14T11:08:18.967496Z","iopub.status.idle":"2023-04-14T11:08:21.274658Z","shell.execute_reply.started":"2023-04-14T11:08:18.967462Z","shell.execute_reply":"2023-04-14T11:08:21.273449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install pycbc -qq\n#import pycbc.types","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:21.276573Z","iopub.execute_input":"2023-04-14T11:08:21.277328Z","iopub.status.idle":"2023-04-14T11:08:21.282684Z","shell.execute_reply.started":"2023-04-14T11:08:21.277281Z","shell.execute_reply":"2023-04-14T11:08:21.281022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q nnAudio -qq\nimport torch\nfrom nnAudio.Spectrogram import CQT1992v2\n","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:21.287224Z","iopub.execute_input":"2023-04-14T11:08:21.287854Z","iopub.status.idle":"2023-04-14T11:08:32.864064Z","shell.execute_reply.started":"2023-04-14T11:08:21.287790Z","shell.execute_reply":"2023-04-14T11:08:32.862091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Q-Transfrom**","metadata":{}},{"cell_type":"code","source":"Q_TRANSFORM = CQT1992v2(sr=2048, fmin=20, fmax=1024, hop_length=32)\n\ndef visualizeSampleQtransform(\n    _id, \n    target,\n    signal_names=(\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\"),\n    sr=2048,\n):\n    \"\"\"\n    Visualizes the CQT transform of a waveform signal for three different detectors.\n\n    Args:\n        _id (str): The ID of the image to visualize.\n        target (int): The target class of the image.\n        signal_names (tuple of str, optional): The names of the three signals to display.\n            Defaults to (\"LIGO Hanford\", \"LIGO Livingston\", \"Virgo\").\n        sr (int, optional): The sampling rate of the signal. Defaults to 2048.\n    \"\"\"\n    x = np.load(imageIdToPath(_id))\n    plt.figure(figsize=(16, 5))\n    for i in range(len(signal_names)):\n        waves = x[i] / np.max(x[i])\n        waves = torch.from_numpy(waves).float()\n        image = Q_TRANSFORM(waves)\n        \n        plt.subplot(1, len(signal_names), i + 1)\n        plt.imshow(image.squeeze())\n        plt.title(signal_names[i], fontsize=14)\n\n    plt.suptitle(f\"ID: {_id} | Target: {target}\", fontsize=16)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:32.866133Z","iopub.execute_input":"2023-04-14T11:08:32.866668Z","iopub.status.idle":"2023-04-14T11:08:32.903794Z","shell.execute_reply.started":"2023-04-14T11:08:32.866620Z","shell.execute_reply":"2023-04-14T11:08:32.902532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in random.sample(trainDf.index.tolist(), 5):\n    _id = trainDf.iloc[i][\"id\"]\n    target = trainDf.iloc[i][\"target\"]\n\n    visualizeSample(_id, target)\n    visualizeSampleQtransform(_id, target)","metadata":{"execution":{"iopub.status.busy":"2023-04-14T11:08:32.905173Z","iopub.execute_input":"2023-04-14T11:08:32.906334Z","iopub.status.idle":"2023-04-14T11:08:38.689731Z","shell.execute_reply.started":"2023-04-14T11:08:32.906271Z","shell.execute_reply":"2023-04-14T11:08:38.688420Z"},"trusted":true},"execution_count":null,"outputs":[]}]}