{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom matplotlib import pyplot as plt\nfrom scipy.fft import fft, ifft\n\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\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\ndataset = []\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input/g2net-gravitational-wave-detection/train'):\n    for filename in filenames:\n        dataset.append(os.path.join(dirname, filename))\n\ndataset.sort()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-01T14:04:51.067643Z","iopub.execute_input":"2021-11-01T14:04:51.068139Z","iopub.status.idle":"2021-11-01T14:08:16.05543Z","shell.execute_reply.started":"2021-11-01T14:04:51.068047Z","shell.execute_reply":"2021-11-01T14:08:16.054301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = '/kaggle/input/g2net-gravitational-wave-detection/training_labels.csv'\ntrain_label = pd.read_csv(train_label, sep = ',')\ntrain_label = np.array(train_label)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:08:16.057217Z","iopub.execute_input":"2021-11-01T14:08:16.057631Z","iopub.status.idle":"2021-11-01T14:08:16.614919Z","shell.execute_reply.started":"2021-11-01T14:08:16.057588Z","shell.execute_reply":"2021-11-01T14:08:16.613913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_test = 1\ndata = np.load(dataset[index_test])\n\nif train_label[index_test][1] == 0:\n    print('GW: no')\nelse:\n    print('GW: yes')\n    \n#plt.plot(data[0, :], color = 'r', label = '1')\n#plt.plot(data[1, :], color = 'g', label = '2')\nplt.plot(data[2, :], color = 'b', label = '3')\nplt.legend()\nplt.show()\n\n\ndata_fft_0 = fft(data[0, :])\ndata_fft_1 = fft(data[1, :])\ndata_fft_2 = fft(data[2, :])\nplt.plot(np.abs(data_fft_0), color = 'r', label = '1')\nplt.plot(np.abs(data_fft_1), color = 'g', label = '2')\nplt.plot(np.abs(data_fft_2), color = 'b', label = '3')\nplt.legend()\nplt.yscale('log')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:08:16.616807Z","iopub.execute_input":"2021-11-01T14:08:16.617145Z","iopub.status.idle":"2021-11-01T14:08:17.607343Z","shell.execute_reply.started":"2021-11-01T14:08:16.617115Z","shell.execute_reply":"2021-11-01T14:08:17.606434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_data = 5000\ngw_detector = 0\ndata = np.zeros((len_data, 4096))\nfor i in range(0, len_data):\n    data[i, :] = np.load(dataset[i])[gw_detector]\n    \ndata_label = train_label[0:len_data,1]\ndata_label = np.asarray(data_label).astype('float32')\nprint(np.shape(data), np.shape(data_label))","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:08:17.609042Z","iopub.execute_input":"2021-11-01T14:08:17.609514Z","iopub.status.idle":"2021-11-01T14:08:52.701193Z","shell.execute_reply.started":"2021-11-01T14:08:17.609479Z","shell.execute_reply":"2021-11-01T14:08:52.700044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_data = 4096\nx = np.linspace(0, 2*np.pi, len_data)\n\nimport random\nrandom.seed()\nrand_nums = []\nfunc = []\n\nfor n in range(5000):\n    rand_num = random.randint(0, 1)\n    if rand_num == 0:\n        a = random.uniform(-2, 2)\n        b = random.uniform(-2, 2)\n        func.append([a*i + b for i in x])\n    else:\n        alpha = random.uniform(-1, 1)\n        beta = random.uniform(0, 2*np.pi)\n        func.append([np.sin(alpha*i + beta) for i in x])\n    \n    rand_nums.append(rand_num)\n\nfunc = np.array(func)\nrand_nums = np.array(rand_nums)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:08:52.702805Z","iopub.execute_input":"2021-11-01T14:08:52.703504Z","iopub.status.idle":"2021-11-01T14:09:38.425946Z","shell.execute_reply.started":"2021-11-01T14:08:52.703446Z","shell.execute_reply":"2021-11-01T14:09:38.425093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation, Dense\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.metrics import categorical_crossentropy","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:09:38.427101Z","iopub.execute_input":"2021-11-01T14:09:38.427548Z","iopub.status.idle":"2021-11-01T14:09:43.947702Z","shell.execute_reply.started":"2021-11-01T14:09:38.427499Z","shell.execute_reply":"2021-11-01T14:09:43.946589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Dense(units = 1024, input_shape = (4096,), activation = 'relu'),\n    Dense(units = 256, activation = 'relu'),\n    Dense(units = 64, activation = 'relu'),\n    Dense(units = 1, activation = 'softmax')\n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:09:43.948994Z","iopub.execute_input":"2021-11-01T14:09:43.949308Z","iopub.status.idle":"2021-11-01T14:09:44.264346Z","shell.execute_reply.started":"2021-11-01T14:09:43.949279Z","shell.execute_reply":"2021-11-01T14:09:44.263491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = Adam(learning_rate = 0.9), loss = 'binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:09:44.266599Z","iopub.execute_input":"2021-11-01T14:09:44.26723Z","iopub.status.idle":"2021-11-01T14:09:44.284601Z","shell.execute_reply.started":"2021-11-01T14:09:44.267178Z","shell.execute_reply":"2021-11-01T14:09:44.283619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.fit(x = data, y = data_label, validation_split = 0.2, batch_size = 20, epochs = 25, verbose = 2)\n\nmodel.fit(x = func, y = rand_nums, validation_split = 0.2, batch_size = 20, epochs = 25, verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T14:09:44.285959Z","iopub.execute_input":"2021-11-01T14:09:44.286235Z","iopub.status.idle":"2021-11-01T14:10:55.431468Z","shell.execute_reply.started":"2021-11-01T14:09:44.286209Z","shell.execute_reply":"2021-11-01T14:10:55.430634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x = data, y = data_label, validation_split = 0.2, batch_size = 20, epochs = 25, verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2021-11-01T10:10:08.824559Z","iopub.execute_input":"2021-11-01T10:10:08.824958Z","iopub.status.idle":"2021-11-01T10:11:23.518645Z","shell.execute_reply.started":"2021-11-01T10:10:08.824919Z","shell.execute_reply":"2021-11-01T10:11:23.517092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nomor = 55\nplt.plot(data[nomor]);\nprint(data_label[nomor])","metadata":{"execution":{"iopub.status.busy":"2021-07-31T15:29:08.511647Z","iopub.execute_input":"2021-07-31T15:29:08.512138Z","iopub.status.idle":"2021-07-31T15:29:08.684344Z","shell.execute_reply.started":"2021-07-31T15:29:08.512098Z","shell.execute_reply":"2021-07-31T15:29:08.683116Z"},"trusted":true},"execution_count":null,"outputs":[]}]}