{"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":"# All the libraries\n!python -m pip install gwpy\n!pip install astropy==4.2.1\n!pip install astropy\n!pip install nnAudio\n!pip install tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-20T16:42:36.597581Z","iopub.execute_input":"2021-09-20T16:42:36.597973Z","iopub.status.idle":"2021-09-20T16:43:20.264926Z","shell.execute_reply.started":"2021-09-20T16:42:36.597891Z","shell.execute_reply":"2021-09-20T16:43:20.263821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom scipy import signal\nimport os\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\n\nimport tqdm\nimport gwpy\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:20.266633Z","iopub.execute_input":"2021-09-20T16:43:20.266947Z","iopub.status.idle":"2021-09-20T16:43:27.031697Z","shell.execute_reply.started":"2021-09-20T16:43:20.266913Z","shell.execute_reply":"2021-09-20T16:43:27.030866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/g2net-gravitational-wave-detection/training_labels.csv\")\nfnames = []\npath = '/kaggle/input/g2net-gravitational-wave-detection/train/0/0/0'\npde=[]\n\nfor dirname, _, filenames in os.walk(path):\n    for filename in filenames:\n        fnames.append(os.path.join(dirname,filename))\n        pde.append(filename)\nlen(fnames)\nfnames[:20]","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:27.033827Z","iopub.execute_input":"2021-09-20T16:43:27.034203Z","iopub.status.idle":"2021-09-20T16:43:27.505924Z","shell.execute_reply.started":"2021-09-20T16:43:27.034161Z","shell.execute_reply":"2021-09-20T16:43:27.504733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading the NPY file\n","metadata":{}},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nplt.style.use('ggplot')\nplt.figure(figsize= (15,10))\n\nfor i in range(3):\n    image = np.load(fnames[1])\n    ax = plt.subplot(3,1,i+1)\n    image = image[i,:]/np.max(image[i,:])\n    plt.plot(image)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:27.507467Z","iopub.execute_input":"2021-09-20T16:43:27.507834Z","iopub.status.idle":"2021-09-20T16:43:28.116225Z","shell.execute_reply.started":"2021-09-20T16:43:27.507801Z","shell.execute_reply":"2021-09-20T16:43:28.115241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = np.load(fnames[4])\nimage.shape\n","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:28.117411Z","iopub.execute_input":"2021-09-20T16:43:28.117684Z","iopub.status.idle":"2021-09-20T16:43:28.130333Z","shell.execute_reply.started":"2021-09-20T16:43:28.117654Z","shell.execute_reply":"2021-09-20T16:43:28.129415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A function to Show all the waveforms contained in one Np.array","metadata":{}},{"cell_type":"code","source":"def wavesinone(fno):\n    \"\"\"\n    Function takes filenumber as input and plots all the 3 or 4 waves in that np.array\n    \"\"\"\n    \n\n\n    plt.style.use('ggplot')\n    plt.figure(figsize= (15,10))\n\n    for i in range(3):\n        image = np.load(fnames[fno])\n        ax = plt.subplot(3,1,i+1)\n        image = image[i,:]/np.max(image[i,:])\n        plt.plot(image)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:28.131528Z","iopub.execute_input":"2021-09-20T16:43:28.131844Z","iopub.status.idle":"2021-09-20T16:43:28.137304Z","shell.execute_reply.started":"2021-09-20T16:43:28.131807Z","shell.execute_reply":"2021-09-20T16:43:28.136595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wavesinone(10)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:28.138279Z","iopub.execute_input":"2021-09-20T16:43:28.138619Z","iopub.status.idle":"2021-09-20T16:43:28.615540Z","shell.execute_reply.started":"2021-09-20T16:43:28.138592Z","shell.execute_reply":"2021-09-20T16:43:28.614555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Writing a function to show the frequency response","metadata":{}},{"cell_type":"code","source":"def freqinone(fno):\n    b = np.load(fnames[fno])\n    w, h = signal.freqz(b[0][:])\n\n    import matplotlib.pyplot as plt\n\n    fig, ax1 = plt.subplots(figsize= (17,4))\n    ax1.set_title('LIGO Hanford Digital filter frequency response')\n\n    ax1.plot(w, 20 * np.log10(abs(h)), 'b')\n    ax1.set_ylabel('Amplitude [dB]', color='b')\n    ax1.set_xlabel('Frequency [rad/sample]')\n\n    ax2 = ax1.twinx()\n    angles = np.unwrap(np.angle(h))\n    ax2.plot(w, angles, 'g')\n    ax2.set_ylabel('Angle (radians)', color='g')\n    ax2.grid()\n    ax2.axis('tight')\n    plt.show()\n\n    b = np.load(fnames[10])\n    w, h = signal.freqz(b[1][:])\n\n\n    fig1,ax3=  plt.subplots(figsize= (17,4))\n    ax3.set_title('LIGO Livingston Digital filter frequency response')\n\n    ax3.plot(w, 20 * np.log10(abs(h)), 'b')\n    ax3.set_ylabel('Amplitude [dB]', color='b')\n    ax3.set_xlabel('Frequency [rad/sample]')\n\n    ax4 = ax3.twinx()\n    angles = np.unwrap(np.angle(h))\n    ax4.plot(w, angles, 'g')\n    ax4.set_ylabel('Angle (radians)', color='g')\n    ax4.grid()\n    ax4.axis('tight')\n    plt.show()\n\n    b = np.load(fnames[10])\n    w, h = signal.freqz(b[2][:])\n\n\n    fig2,ax5=  plt.subplots(figsize= (17,4))\n    ax5.set_title('LIGO Livingston Digital filter frequency response')\n\n    ax5.plot(w, 20 * np.log10(abs(h)), 'b')\n    ax5.set_ylabel('Amplitude [dB]', color='b')\n    ax5.set_xlabel('Frequency [rad/sample]')\n\n    ax6 = ax5.twinx()\n    angles = np.unwrap(np.angle(h))\n    ax6.plot(w, angles, 'g')\n    ax6.set_ylabel('Angle (radians)', color='g')\n    ax6.grid()\n    ax6.axis('tight')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:28.618154Z","iopub.execute_input":"2021-09-20T16:43:28.618425Z","iopub.status.idle":"2021-09-20T16:43:28.630517Z","shell.execute_reply.started":"2021-09-20T16:43:28.618398Z","shell.execute_reply":"2021-09-20T16:43:28.629449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freqinone(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:28.632288Z","iopub.execute_input":"2021-09-20T16:43:28.632557Z","iopub.status.idle":"2021-09-20T16:43:29.946692Z","shell.execute_reply.started":"2021-09-20T16:43:28.632530Z","shell.execute_reply":"2021-09-20T16:43:29.945714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a Function to extract  the info from 3 sensors\n","metadata":{}},{"cell_type":"code","source":"def tseries(fno):\n    b = np.load(fnames[fno])\n    t1= b[0][:]\n    t2 = b[1][:]\n    t3 = b[2][:]\n    \n    return t1,t2,t3","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:29.947830Z","iopub.execute_input":"2021-09-20T16:43:29.948085Z","iopub.status.idle":"2021-09-20T16:43:29.952910Z","shell.execute_reply.started":"2021-09-20T16:43:29.948059Z","shell.execute_reply":"2021-09-20T16:43:29.951968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a,b,c = tseries(1)\n\na.shape,b.shape,c.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:29.954039Z","iopub.execute_input":"2021-09-20T16:43:29.954302Z","iopub.status.idle":"2021-09-20T16:43:29.969800Z","shell.execute_reply.started":"2021-09-20T16:43:29.954277Z","shell.execute_reply":"2021-09-20T16:43:29.968939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create a Function to convert extracted Time series into tensors\n","metadata":{}},{"cell_type":"code","source":"def totensors(fno):\n    a,b,c = tseries(fno)\n    t1 = tf.convert_to_tensor(a)\n    t2 = tf.convert_to_tensor(b)\n    t3 = tf.convert_to_tensor(c)\n    \n    return t1,t2,t3\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:29.970861Z","iopub.execute_input":"2021-09-20T16:43:29.971160Z","iopub.status.idle":"2021-09-20T16:43:29.977141Z","shell.execute_reply.started":"2021-09-20T16:43:29.971131Z","shell.execute_reply":"2021-09-20T16:43:29.976164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a,b,c = totensors(32)\nd = tf.constant(a)\nd.shape, a.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:29.978614Z","iopub.execute_input":"2021-09-20T16:43:29.979008Z","iopub.status.idle":"2021-09-20T16:43:30.014243Z","shell.execute_reply.started":"2021-09-20T16:43:29.978969Z","shell.execute_reply":"2021-09-20T16:43:30.013307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.Dense(512 , activation = 'relu', input_shape=[3]),\n    layers.Dense(1024 , activation = 'relu'),\n    layers.Dense(512 , activation = 'relu'),\n    layers.Dense(1),\n    \n])\n\nmodel.compile(\n    optimizer = \"adam\",\n    loss = 'BinaryCrossentropy'\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:30.015551Z","iopub.execute_input":"2021-09-20T16:43:30.015973Z","iopub.status.idle":"2021-09-20T16:43:30.126780Z","shell.execute_reply.started":"2021-09-20T16:43:30.015931Z","shell.execute_reply":"2021-09-20T16:43:30.125850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:30.127927Z","iopub.execute_input":"2021-09-20T16:43:30.128197Z","iopub.status.idle":"2021-09-20T16:43:30.151596Z","shell.execute_reply.started":"2021-09-20T16:43:30.128172Z","shell.execute_reply":"2021-09-20T16:43:30.150526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd= df.id\ntarget = df.target\npd = pd+\".npy\"\npd","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:30.152989Z","iopub.execute_input":"2021-09-20T16:43:30.153376Z","iopub.status.idle":"2021-09-20T16:43:30.243982Z","shell.execute_reply.started":"2021-09-20T16:43:30.153336Z","shell.execute_reply":"2021-09-20T16:43:30.243094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"t=[]\nx=[]\nfor i in range(155):\n    for j in range(560000):\n        if pde[i] == pd[j]:\n                x.append(j)\n                t.append(target[j])\n                print(j,len(x))\n                \nlen(x)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T16:43:30.245269Z","iopub.execute_input":"2021-09-20T16:43:30.245565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#new_pde = {x.replace(\".npy\",\"\") for x in pde}\n#new_pde\nimport pandas as pd\ndf1= pd.DataFrame(list(zip(pde,t)),columns= [\"id\",\"target\"])\ndf1\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to split fnames into train and test\n","metadata":{}},{"cell_type":"code","source":"X_train,X_val,y_train,y_val = train_test_split(fnames,\n                                               df1.target,test_size = 0.2,\n                                               random_state=13)\nlen(X_train),len(X_val),len(y_train),len(y_val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to preprocess the images and convert them into tensor\n","metadata":{}},{"cell_type":"code","source":"def process_image(fnoss):\n    \"\"\"\n    takes an image number and turn it into tensor\n    \"\"\"\n    q,w,r= tseries(fnoss)\n    q = tf.keras.preprocessing.timeseries_dataset_from_array(q,targets= None, sequence_length=4096)\n    w = tf.keras.preprocessing.timeseries_dataset_from_array(w,targets= None, sequence_length=4096)\n    r = tf.keras.preprocessing.timeseries_dataset_from_array(r,targets= None, sequence_length=4096)\n    \n    return q,w,r\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nq,w,e = process_image(2)\nq,w,e","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_image2(fnoss):\n    \"\"\"\n    takes an image number and turn it into tensor\n    \"\"\"\n    q = np.load(fnames[fnoss])\n    q = tf.keras.preprocessing.timeseries_dataset_from_array(q,targets= None, sequence_length=4096)\n    \n    return q\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nq = process_image2(2)\nq","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}