{"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\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n    #for filename in filenames:\n        #print(os.path.join(dirname, filename))","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-01T08:25:41.572842Z","iopub.execute_input":"2023-01-01T08:25:41.573411Z","iopub.status.idle":"2023-01-01T08:25:41.598934Z","shell.execute_reply.started":"2023-01-01T08:25:41.573301Z","shell.execute_reply":"2023-01-01T08:25:41.597875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","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\nfor 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"}},{"cell_type":"code","source":"import pandas as pd\nimport h5py\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-01-07T04:58:39.162186Z","iopub.execute_input":"2023-01-07T04:58:39.162842Z","iopub.status.idle":"2023-01-07T04:58:39.425857Z","shell.execute_reply.started":"2023-01-07T04:58:39.162703Z","shell.execute_reply":"2023-01-07T04:58:39.424461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import stats","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:03:08.601712Z","iopub.execute_input":"2023-01-07T05:03:08.602179Z","iopub.status.idle":"2023-01-07T05:03:09.080739Z","shell.execute_reply.started":"2023-01-07T05:03:08.602147Z","shell.execute_reply":"2023-01-07T05:03:09.079181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nfrom mpl_toolkits.mplot3d import Axes3D","metadata":{"execution":{"iopub.status.busy":"2023-01-02T13:49:27.707254Z","iopub.execute_input":"2023-01-02T13:49:27.707777Z","iopub.status.idle":"2023-01-02T13:49:27.716902Z","shell.execute_reply.started":"2023-01-02T13:49:27.707737Z","shell.execute_reply":"2023-01-02T13:49:27.715347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def signaltonoise(a, axis=0, ddof=0):\n    a = np.asanyarray(a)\n    m = a.mean(axis)\n    sd = a.std(axis=axis, ddof=ddof)\n    return np.where(sd == 0, 0, m/sd)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:03:18.46275Z","iopub.execute_input":"2023-01-07T05:03:18.463211Z","iopub.status.idle":"2023-01-07T05:03:18.469305Z","shell.execute_reply.started":"2023-01-07T05:03:18.463176Z","shell.execute_reply":"2023-01-07T05:03:18.468051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The SFTs here have the amplitudes of signals, with real and imaginary parts denoting two different orientations.The number of signals to be considered equals min(len(timestamp),len(sft[0])).","metadata":{}},{"cell_type":"code","source":"def extractfactors(hdff):\n    snrt=[]\n    lk=list(hdff.keys())\n    hdfid=lk[0]\n    particular=hdff[hdfid]\n    h1=particular['H1']\n    l1=particular['L1']\n    freq=particular['frequency_Hz']\n    sfth1=h1['SFTs']\n    sfth1_val=list(sfth1)*360\n    sftl1=l1['SFTs']\n    sftl1_val=list(sftl1)*360\n    h1sft=np.array(sfth1_val).view(np.float32)\n    l1sft=np.array(sftl1_val).view(np.float32)\n    snrh1=signaltonoise(h1sft,axis=0,ddof=0)\n    snrl1=signaltonoise(l1sft,axis=0,ddof=0)\n    snrt.append(list(snrh1))\n    snrt.append(list(snrl1))\n    return snrt","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:55:46.047682Z","iopub.execute_input":"2023-01-07T06:55:46.048164Z","iopub.status.idle":"2023-01-07T06:55:46.057723Z","shell.execute_reply.started":"2023-01-07T06:55:46.04813Z","shell.execute_reply":"2023-01-07T06:55:46.056197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The function above generates the signal-to-noise for each of the given signals in a set through the SFTs given.","metadata":{}},{"cell_type":"code","source":"hdffi=h5py.File('/kaggle/input/g2net-detecting-continuous-gravitational-waves/train/09531cde3.hdf5','r')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:03:37.972036Z","iopub.execute_input":"2023-01-07T05:03:37.97265Z","iopub.status.idle":"2023-01-07T05:03:37.986808Z","shell.execute_reply.started":"2023-01-07T05:03:37.972615Z","shell.execute_reply":"2023-01-07T05:03:37.985427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l=extractfactors(hdffi)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:03:40.284274Z","iopub.execute_input":"2023-01-07T05:03:40.285411Z","iopub.status.idle":"2023-01-07T05:04:08.496273Z","shell.execute_reply.started":"2023-01-07T05:03:40.285369Z","shell.execute_reply":"2023-01-07T05:04:08.495222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(l[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:04:11.432292Z","iopub.execute_input":"2023-01-07T05:04:11.432696Z","iopub.status.idle":"2023-01-07T05:04:11.753504Z","shell.execute_reply.started":"2023-01-07T05:04:11.432665Z","shell.execute_reply":"2023-01-07T05:04:11.752132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hdffn=h5py.File('/kaggle/input/g2net-detecting-continuous-gravitational-waves/train/01bcf6533.hdf5','r')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:04:17.151486Z","iopub.execute_input":"2023-01-07T05:04:17.151929Z","iopub.status.idle":"2023-01-07T05:04:17.168411Z","shell.execute_reply.started":"2023-01-07T05:04:17.151891Z","shell.execute_reply":"2023-01-07T05:04:17.167021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ln=extractfactors(hdffn)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:04:18.667238Z","iopub.execute_input":"2023-01-07T05:04:18.667639Z","iopub.status.idle":"2023-01-07T05:04:43.949803Z","shell.execute_reply.started":"2023-01-07T05:04:18.667606Z","shell.execute_reply":"2023-01-07T05:04:43.948438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(ln[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:04:46.50729Z","iopub.execute_input":"2023-01-07T05:04:46.507802Z","iopub.status.idle":"2023-01-07T05:04:46.796336Z","shell.execute_reply.started":"2023-01-07T05:04:46.507761Z","shell.execute_reply":"2023-01-07T05:04:46.794855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.stats import chisquare\nfrom scipy.stats import chi2","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:04:51.394599Z","iopub.execute_input":"2023-01-07T05:04:51.395078Z","iopub.status.idle":"2023-01-07T05:04:51.400808Z","shell.execute_reply.started":"2023-01-07T05:04:51.395036Z","shell.execute_reply":"2023-01-07T05:04:51.399802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/g2net-detecting-continuous-gravitational-waves/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:02:26.453215Z","iopub.execute_input":"2023-01-07T06:02:26.453711Z","iopub.status.idle":"2023-01-07T06:02:26.462529Z","shell.execute_reply.started":"2023-01-07T06:02:26.453677Z","shell.execute_reply":"2023-01-07T06:02:26.461065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"snrlist=[]\nidtrain=[]\ntraindir='/kaggle/input/g2net-detecting-continuous-gravitational-waves/train'\nfor i in range(15):\n    tid=train_df['id'][i].replace(' '' ',\" \")\n    train_files='%s/%s.hdf5' % (traindir,tid)\n    thdf=h5py.File(train_files,'r')\n    idtrain=extractfactors(thdf)\n    snrlist.append(idtrain)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:02:28.092866Z","iopub.execute_input":"2023-01-07T06:02:28.093338Z","iopub.status.idle":"2023-01-07T06:09:30.267319Z","shell.execute_reply.started":"2023-01-07T06:02:28.093299Z","shell.execute_reply":"2023-01-07T06:09:30.26605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The list has SNR(Signal-to-Noise) of signals detected on H1 and L1.A statistical parameter would be chosen to check the hypothesis on the presence of a signal or its absence.","metadata":{}},{"cell_type":"code","source":"sdfd=pd.DataFrame(snrlist)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:12:59.482574Z","iopub.execute_input":"2023-01-07T06:12:59.48304Z","iopub.status.idle":"2023-01-07T06:12:59.489217Z","shell.execute_reply.started":"2023-01-07T06:12:59.482995Z","shell.execute_reply":"2023-01-07T06:12:59.488035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(15):\n    fig1, ax1 = plt.subplots()\n    xh1=sdfd[0][i]\n    yh1=chi2.pdf(sdfd[0][i],df=100)\n    ax1.plot(xh1, yh1,color=\"purple\")\n    ax1.set_title(\"Variation-H1\")\n    ax1.set_xlabel(\"Signal \"+str(i))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:46:33.986848Z","iopub.execute_input":"2023-01-07T06:46:33.988129Z","iopub.status.idle":"2023-01-07T06:46:37.064559Z","shell.execute_reply.started":"2023-01-07T06:46:33.98807Z","shell.execute_reply":"2023-01-07T06:46:37.063203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The width shows the variation of frequncies in the signal set with a continuous gravitational wave detected and the other showing its absence.The peak shows the maximum deviation from mean, which follows from the calculation of the chi-square statistic.The width and peak determine the presence of fluctuations,i.e, noise.Similarly,the variation is checked for L1 detector, to observe the dependency in the relative strain of H1-L1.","metadata":{}},{"cell_type":"code","source":"for i in range(15):\n    fig2, ax2 = plt.subplots()\n    xl1=sdfd[1][i]\n    yl1=chi2.pdf(sdfd[1][i],df=100)\n    ax2.plot(xl1, yl1,color=\"brown\")\n    ax2.set_title(\"Variation-L1\")\n    ax2.set_xlabel(\"Signal \"+str(i))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:52:07.9757Z","iopub.execute_input":"2023-01-07T06:52:07.97623Z","iopub.status.idle":"2023-01-07T06:52:11.061389Z","shell.execute_reply.started":"2023-01-07T06:52:07.976182Z","shell.execute_reply":"2023-01-07T06:52:11.060044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The Hypothesis test considered is the t-test test. The value of SNR of each set is noted and the mean snr is considered. The amount of deviation on the mean SNR is used to derive the probability of finding the signal.","metadata":{}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/sumamallapragada/observations/edit shows the variation of the signals received with time and frequency.","metadata":{}},{"cell_type":"code","source":"stats.ttest_ind(ln[0],ln[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:05:25.930085Z","iopub.execute_input":"2023-01-07T05:05:25.930502Z","iopub.status.idle":"2023-01-07T05:05:25.943433Z","shell.execute_reply.started":"2023-01-07T05:05:25.930467Z","shell.execute_reply":"2023-01-07T05:05:25.942075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The independence of the h1 and l1 detectors is verified using the t-test, so that the best combination is chosen while considering the probability of signal detection. ","metadata":{}},{"cell_type":"code","source":"a=stats.ttest_1samp(ln[1],0.00033)\nb=stats.ttest_1samp(ln[0],0.00045)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:07:04.188994Z","iopub.execute_input":"2023-01-07T05:07:04.189376Z","iopub.status.idle":"2023-01-07T05:07:04.197579Z","shell.execute_reply.started":"2023-01-07T05:07:04.189347Z","shell.execute_reply":"2023-01-07T05:07:04.196128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One-tailed t-test is considered to predict the expected signal magnitudes detected for the periods noted in each signal set.","metadata":{}},{"cell_type":"code","source":"test_df=pd.read_csv('/kaggle/input/g2net-detecting-continuous-gravitational-waves/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:07:01.747044Z","iopub.execute_input":"2023-01-07T05:07:01.747455Z","iopub.status.idle":"2023-01-07T05:07:01.770398Z","shell.execute_reply.started":"2023-01-07T05:07:01.747425Z","shell.execute_reply":"2023-01-07T05:07:01.768998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"snrtest=[]\nnd=len(test_df)\nidtest=[]\ntestdir='/kaggle/input/g2net-detecting-continuous-gravitational-waves/test'\nfor i in range(929,960):\n    teid=test_df['id'][i].replace(' '' ',\" \")\n    test_files='%s/%s.hdf5' % (testdir,teid)\n    tehdf=h5py.File(test_files,'r')\n    idtest=extractfactors(tehdf)\n    snrtest.append(idtest)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"SNR values of the given range of signal sets are obtained to validate the probability of presence of a continuous gravitational wave signal through the t-test expected value check, with observed signal guarantee.","metadata":{}},{"cell_type":"code","source":"testcheck=pd.DataFrame(snrtest)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T05:21:11.434951Z","iopub.execute_input":"2023-01-07T05:21:11.435888Z","iopub.status.idle":"2023-01-07T05:21:11.440994Z","shell.execute_reply.started":"2023-01-07T05:21:11.435847Z","shell.execute_reply":"2023-01-07T05:21:11.440078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testcheck.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T06:34:17.025611Z","iopub.execute_input":"2023-01-07T06:34:17.026079Z","iopub.status.idle":"2023-01-07T06:34:17.052091Z","shell.execute_reply.started":"2023-01-07T06:34:17.026042Z","shell.execute_reply":"2023-01-07T06:34:17.05054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chtestsample=len(testcheck)\nfor i in range(0,30):\n    mh1samp=round(np.mean(testcheck[0][i]),6)\n    ml1samp=round(np.mean(testcheck[1][i]),6)\n    tcorr=stats.ttest_ind(testcheck[0][i],testcheck[1][i])\n    th1=stats.ttest_1samp(testcheck[0][i],mh1samp)\n    tl1=stats.ttest_1samp(testcheck[1][i],ml1samp)\n    test_df['target'][929+i]=abs((th1[1]*mh1samp)+(tl1[1]*ml1samp))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T07:06:49.76767Z","iopub.execute_input":"2023-01-07T07:06:49.768114Z","iopub.status.idle":"2023-01-07T07:06:49.927048Z","shell.execute_reply.started":"2023-01-07T07:06:49.768078Z","shell.execute_reply":"2023-01-07T07:06:49.925673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df=test_df[929:960]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T07:06:24.062701Z","iopub.execute_input":"2023-01-07T07:06:24.063213Z","iopub.status.idle":"2023-01-07T07:06:24.069943Z","shell.execute_reply.started":"2023-01-07T07:06:24.063172Z","shell.execute_reply":"2023-01-07T07:06:24.068433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T07:07:07.783153Z","iopub.execute_input":"2023-01-07T07:07:07.783576Z","iopub.status.idle":"2023-01-07T07:07:07.796579Z","shell.execute_reply.started":"2023-01-07T07:07:07.783545Z","shell.execute_reply":"2023-01-07T07:07:07.794992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"lastpart3.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-02T16:16:55.360268Z","iopub.execute_input":"2023-01-02T16:16:55.360734Z","iopub.status.idle":"2023-01-02T16:16:55.370656Z","shell.execute_reply.started":"2023-01-02T16:16:55.360697Z","shell.execute_reply":"2023-01-02T16:16:55.368888Z"},"trusted":true},"execution_count":null,"outputs":[]}]}