{"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)\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\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-24T15:35:56.754526Z","iopub.execute_input":"2021-07-24T15:35:56.754911Z","iopub.status.idle":"2021-07-24T15:35:56.759503Z","shell.execute_reply.started":"2021-07-24T15:35:56.754875Z","shell.execute_reply":"2021-07-24T15:35:56.758292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install GWpy","metadata":{"execution":{"iopub.status.busy":"2021-07-24T15:35:10.022822Z","iopub.execute_input":"2021-07-24T15:35:10.023404Z","iopub.status.idle":"2021-07-24T15:35:28.911137Z","shell.execute_reply.started":"2021-07-24T15:35:10.023276Z","shell.execute_reply":"2021-07-24T15:35:28.91011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom gwpy.timeseries import TimeSeries\nimport gwpy\nfrom gwpy.plot import Plot\nfrom scipy import signal\nfrom sklearn.preprocessing import MinMaxScaler\nfrom PIL import Image\ntrainpath='/kaggle/input/g2net-gravitational-wave-detection/train/0/0/0/'\noutpath='/kaggle/working/imgs/'\nos.mkdir(outpath)","metadata":{"execution":{"iopub.status.busy":"2021-07-24T15:36:07.985877Z","iopub.execute_input":"2021-07-24T15:36:07.986225Z","iopub.status.idle":"2021-07-24T15:36:10.478876Z","shell.execute_reply.started":"2021-07-24T15:36:07.986196Z","shell.execute_reply":"2021-07-24T15:36:10.47785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_wave(filename):\n    data = np.load(filename)\n    wave_0 = TimeSeries(data[0], sample_rate=2048)\n    wave_1 = TimeSeries(data[1], sample_rate=2048)\n    wave_2 = TimeSeries(data[2], sample_rate=2048)\n    return [wave_0, wave_1, wave_2]","metadata":{"execution":{"iopub.status.busy":"2021-07-24T15:36:13.087529Z","iopub.execute_input":"2021-07-24T15:36:13.088012Z","iopub.status.idle":"2021-07-24T15:36:13.094253Z","shell.execute_reply.started":"2021-07-24T15:36:13.087976Z","shell.execute_reply":"2021-07-24T15:36:13.09233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepro(wave, lf=50, hf=350, whiten=0.15) :\n    whiten_data_0 = wave[0].whiten(window = (\"tukey\", whiten))\n    bp_data_0 = whiten_data_0.bandpass(lf,hf) \n    whiten_data_1 = wave[1].whiten(window = (\"tukey\", whiten))\n    bp_data_1 = whiten_data_1.bandpass(lf,hf)  \n    whiten_data_2 = wave[2].whiten(window = (\"tukey\", whiten))\n    bp_data_2 = whiten_data_2.bandpass(lf,hf)  \n    return [bp_data_0, bp_data_1, bp_data_2]","metadata":{"execution":{"iopub.status.busy":"2021-07-24T15:36:14.666798Z","iopub.execute_input":"2021-07-24T15:36:14.667218Z","iopub.status.idle":"2021-07-24T15:36:14.673576Z","shell.execute_reply.started":"2021-07-24T15:36:14.667182Z","shell.execute_reply":"2021-07-24T15:36:14.672703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt \n# filenames = os.walk('/kaggle/input/g2net-gravitational-wave-detection/train/0/0/0')\n# wave = load_wave('../input/g2net-gravitational-wave-detection/train/0/0/0/000c41d34a.npy')\n# bp_data = prepro(wave,35,350)\n# fig3 = bp_data[0].plot(figsize=[12, 6])\n# plt.xlim(0, 2)\n# ax = plt.gca()\n# ax.set_title('Whitened and bandpassed')\n# ax.set_xlabel('Time [s]');","metadata":{"execution":{"iopub.status.busy":"2021-07-24T15:36:17.772706Z","iopub.execute_input":"2021-07-24T15:36:17.77321Z","iopub.status.idle":"2021-07-24T15:36:17.77691Z","shell.execute_reply.started":"2021-07-24T15:36:17.773173Z","shell.execute_reply":"2021-07-24T15:36:17.776062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input/g2net-gravitational-wave-detection/train/0/0/0'):\n    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n        wave = wave = load_wave(trainpath+filename)\n        bp_data = prepro(wave,30, 400)\n        hq_0 = bp_data[0].q_transform(qrange=(16,16), frange=(30,300), logf=True, whiten=False)\n        hq_1 = bp_data[1].q_transform(qrange=(16,16), frange=(30,300), logf=True, whiten=False)\n        hq_2 = bp_data[2].q_transform(qrange=(16,16), frange=(30,300), logf=True, whiten=False)\n#         qseries_0 = series_0.q_transform()\n#         qseries_1 = series_1.q_transform()\n#         qseries_2 = series_2.q_transform()\n#         plt = qseries_0.plot()\n#         #plt.show()\n#         plt.savefig(outpath+os.path.splitext(filename)[0]+'_0'+'.png')\n#         plt = qseries_1.plot()\n#         #plt.show()\n#         plt.savefig(outpath+os.path.splitext(filename)[0]+'_1'+'.png')\n#         plt = qseries_2.plot()\n#         #plt.show()\n#         plt.savefig(outpath+os.path.splitext(filename)[0]+'_2'+'.png')\n\n        img = np.zeros([hq_0.shape[0], hq_0.shape[1], 3], dtype=np.uint8)\n        scaler = MinMaxScaler()\n        img[:,:,0] = 255*scaler.fit_transform(hq_0)\n        img[:,:,1] = 255*scaler.fit_transform(hq_1)\n        img[:,:,2] = 255*scaler.fit_transform(hq_2)\n        img = Image.fromarray(img).rotate(90, expand=1).resize((760,760))       \n        img.save(outpath+os.path.splitext(filename)[0]+'.png')\n        \nfrom pathlib import Path\nimport zipfile\nimg_root = Path('/kaggle/working/imgs')\nwith zipfile.ZipFile('imgs.zip', 'w') as z:\n    for img_name in img_root.iterdir():\n        print(img_name)\n        z.write(img_name)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:54:14.054825Z","iopub.execute_input":"2021-07-23T11:54:14.055597Z","iopub.status.idle":"2021-07-23T11:54:34.991396Z","shell.execute_reply.started":"2021-07-23T11:54:14.055555Z","shell.execute_reply":"2021-07-23T11:54:34.989768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.plot(bp_data[0].value)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:41:46.043647Z","iopub.execute_input":"2021-07-23T11:41:46.044102Z","iopub.status.idle":"2021-07-23T11:41:46.49477Z","shell.execute_reply.started":"2021-07-23T11:41:46.044067Z","shell.execute_reply":"2021-07-23T11:41:46.493628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wave = load_wave('../input/g2net-gravitational-wave-detection/train/0/0/1/00172aaca5.npy')\nbp_data = prepro(wave, 30, 400)\nhq_0 = bp_data[0].q_transform(qrange=(16,16), frange=(30,300), logf=True, whiten=False)\nhq_1 = bp_data[1].q_transform(qrange=(16,16), frange=(30,300), logf=True, whiten=False)\nhq_2 = bp_data[2].q_transform(qrange=(16,16), frange=(30,300), logf=True, whiten=False)\nimg = np.zeros([hq_0.shape[0], hq_0.shape[1], 3], dtype=np.uint8)\nscaler = MinMaxScaler()\nimg[:,:,0] = 255*scaler.fit_transform(hq_0)\nimg[:,:,1] = 255*scaler.fit_transform(hq_1)\nimg[:,:,2] = 255*scaler.fit_transform(hq_2)\nImage.fromarray(img).rotate(90, expand=1).resize((760,760))","metadata":{"execution":{"iopub.status.busy":"2021-07-24T15:37:45.307642Z","iopub.execute_input":"2021-07-24T15:37:45.308199Z","iopub.status.idle":"2021-07-24T15:37:46.094812Z","shell.execute_reply.started":"2021-07-24T15:37:45.308166Z","shell.execute_reply":"2021-07-24T15:37:46.093605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}