{"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":"!pip install git+https://github.com/PyFstat/PyFstat@python37","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:44:31.048648Z","iopub.execute_input":"2023-01-04T08:44:31.049343Z","iopub.status.idle":"2023-01-04T08:45:26.330439Z","shell.execute_reply.started":"2023-01-04T08:44:31.049223Z","shell.execute_reply":"2023-01-04T08:45:26.328664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\nimport pyfstat\n\nimport time\nimport h5py\nfrom tqdm import tqdm \nimport math\nimport optuna\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:54:51.616517Z","iopub.execute_input":"2023-01-04T08:54:51.616936Z","iopub.status.idle":"2023-01-04T08:54:52.003843Z","shell.execute_reply.started":"2023-01-04T08:54:51.616901Z","shell.execute_reply":"2023-01-04T08:54:52.002724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"di =  '/kaggle/input/g2net-detecting-continuous-gravitational-waves'\ndf = pd.read_csv(di + '/train_labels.csv')\ndf = df[df.target >= 0]  # Remove 3 unknowns (target = -1)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:46:22.441246Z","iopub.execute_input":"2023-01-04T08:46:22.442799Z","iopub.status.idle":"2023-01-04T08:46:22.467189Z","shell.execute_reply.started":"2023-01-04T08:46:22.442749Z","shell.execute_reply":"2023-01-04T08:46:22.466375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_id=\"e0cdcda46\"\n\nfilename = '%s/%s/%s.hdf5' % (di, \"train\", file_id)\nf = h5py.File(filename, 'r')\ng = f[file_id]","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:48:28.964164Z","iopub.execute_input":"2023-01-04T08:48:28.964643Z","iopub.status.idle":"2023-01-04T08:48:28.994662Z","shell.execute_reply.started":"2023-01-04T08:48:28.964605Z","shell.execute_reply":"2023-01-04T08:48:28.993435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g[\"frequency_Hz\"][0]","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:48:30.23562Z","iopub.execute_input":"2023-01-04T08:48:30.236021Z","iopub.status.idle":"2023-01-04T08:48:30.250353Z","shell.execute_reply.started":"2023-01-04T08:48:30.235989Z","shell.execute_reply":"2023-01-04T08:48:30.249176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g[\"frequency_Hz\"][-1]","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:48:30.803658Z","iopub.execute_input":"2023-01-04T08:48:30.804061Z","iopub.status.idle":"2023-01-04T08:48:30.812974Z","shell.execute_reply.started":"2023-01-04T08:48:30.804027Z","shell.execute_reply":"2023-01-04T08:48:30.811632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.empty((2, 360, 128), dtype=np.float32)\nfor ch, s in enumerate(['H1', 'L1']):\n    a = g[s]['SFTs'][:, :4096] * 1e22\n\n    p = a.real**2 + a.imag**2  # power\n    p /= np.mean(p)  # normalize\n    p = np.mean(p.reshape(360, 128, 32), axis=2)\n\n    img[ch] = p","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:48:31.218405Z","iopub.execute_input":"2023-01-04T08:48:31.218804Z","iopub.status.idle":"2023-01-04T08:48:31.714299Z","shell.execute_reply.started":"2023-01-04T08:48:31.218774Z","shell.execute_reply":"2023-01-04T08:48:31.713102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nax1 = fig.add_subplot(1, 2, 1)\nax2 = fig.add_subplot(1, 2, 2)\n\nax1.imshow(img[0]) \nax2.imshow(img[1]) ","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:48:31.716321Z","iopub.execute_input":"2023-01-04T08:48:31.717072Z","iopub.status.idle":"2023-01-04T08:48:32.046616Z","shell.execute_reply.started":"2023-01-04T08:48:31.717036Z","shell.execute_reply":"2023-01-04T08:48:32.045676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# step1: create SFT file","metadata":{}},{"cell_type":"code","source":"import os\n\nimport numpy as np\n\nimport pyfstat\n\nlabel = \"PyFstat_example_grid_search_F0F1\"\noutdir = os.path.join(\"PyFstat_example_data\", label)\n\nwriter_kwargs = {\n    \"label\": \"single_detector_gaussian_noise\",\n    \"outdir\": \"PyFstat_example_data\",\n    \"timestamps\":{\n        \"H1\": g[\"H1\"][\"timestamps_GPS\"][:],\n        \"L1\": g[\"L1\"][\"timestamps_GPS\"][:]\n    },\n    \n    \"detectors\": \"H1,L1\",\n    \"sqrtSX\": 1e-22,\n    \"Tsft\": 1800,\n    \"SFTWindowType\": \"tukey\",\n    \"SFTWindowBeta\": 0.01,\n    \"F0\" : (g[\"frequency_Hz\"][0] + g[\"frequency_Hz\"][-1])/2,\n    \"Band\": 0.6, \n}\n\nwriter = pyfstat.Writer(**writer_kwargs)\nwriter.make_data()# Create SFTs\n","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:04.045739Z","iopub.execute_input":"2023-01-04T08:51:04.046206Z","iopub.status.idle":"2023-01-04T08:51:09.173666Z","shell.execute_reply.started":"2023-01-04T08:51:04.046169Z","shell.execute_reply":"2023-01-04T08:51:09.171765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"freqs, times, sft_data = pyfstat.utils.get_sft_as_arrays(writer.sftfilepath)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:09.176276Z","iopub.execute_input":"2023-01-04T08:51:09.176721Z","iopub.status.idle":"2023-01-04T08:51:09.579676Z","shell.execute_reply.started":"2023-01-04T08:51:09.176679Z","shell.execute_reply":"2023-01-04T08:51:09.578485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(times[\"H1\"]),len(times[\"L1\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:09.581341Z","iopub.execute_input":"2023-01-04T08:51:09.58168Z","iopub.status.idle":"2023-01-04T08:51:09.589543Z","shell.execute_reply.started":"2023-01-04T08:51:09.581651Z","shell.execute_reply":"2023-01-04T08:51:09.588315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(freqs)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:09.592032Z","iopub.execute_input":"2023-01-04T08:51:09.592688Z","iopub.status.idle":"2023-01-04T08:51:09.601024Z","shell.execute_reply.started":"2023-01-04T08:51:09.592629Z","shell.execute_reply":"2023-01-04T08:51:09.599827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"amplitudes = {key: val[:, :]\n        for key, val in sft_data.items()}","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:10.283284Z","iopub.execute_input":"2023-01-04T08:51:10.28373Z","iopub.status.idle":"2023-01-04T08:51:10.289459Z","shell.execute_reply.started":"2023-01-04T08:51:10.283696Z","shell.execute_reply":"2023-01-04T08:51:10.288238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"amplitudes[\"H1\"].shape","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:11.522981Z","iopub.execute_input":"2023-01-04T08:51:11.523648Z","iopub.status.idle":"2023-01-04T08:51:11.529895Z","shell.execute_reply.started":"2023-01-04T08:51:11.523609Z","shell.execute_reply":"2023-01-04T08:51:11.528927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"amplitudes[\"L1\"].shape","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:11.819037Z","iopub.execute_input":"2023-01-04T08:51:11.819481Z","iopub.status.idle":"2023-01-04T08:51:11.827049Z","shell.execute_reply.started":"2023-01-04T08:51:11.819421Z","shell.execute_reply":"2023-01-04T08:51:11.825633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"amplitudes[\"H1\"].shape","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:13.539357Z","iopub.execute_input":"2023-01-04T08:51:13.539745Z","iopub.status.idle":"2023-01-04T08:51:13.547156Z","shell.execute_reply.started":"2023-01-04T08:51:13.539716Z","shell.execute_reply":"2023-01-04T08:51:13.545981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.empty((2, 1080, 128), dtype=np.float32)\nfor ch, s in enumerate(['H1', 'L1']):\n\n    a = amplitudes[s][:1080,:4096] * 1e22\n    p = a.real **2 + a.imag**2\n    p /= np.mean(p)\n    print(p.shape)\n    p = np.mean(p.reshape(1080, 128, 32), axis=2) \n    img[ch] = p","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:15.443202Z","iopub.execute_input":"2023-01-04T08:51:15.444505Z","iopub.status.idle":"2023-01-04T08:51:15.521053Z","shell.execute_reply.started":"2023-01-04T08:51:15.444449Z","shell.execute_reply":"2023-01-04T08:51:15.519939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:16.683031Z","iopub.execute_input":"2023-01-04T08:51:16.684084Z","iopub.status.idle":"2023-01-04T08:51:16.893648Z","shell.execute_reply.started":"2023-01-04T08:51:16.684032Z","shell.execute_reply":"2023-01-04T08:51:16.892472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:51:17.018944Z","iopub.execute_input":"2023-01-04T08:51:17.019319Z","iopub.status.idle":"2023-01-04T08:51:17.21958Z","shell.execute_reply.started":"2023-01-04T08:51:17.019279Z","shell.execute_reply":"2023-01-04T08:51:17.218436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# step2: load the SFT file and write sft data","metadata":{}},{"cell_type":"code","source":"writer.sftfilepath","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:01.075999Z","iopub.execute_input":"2023-01-04T08:52:01.076449Z","iopub.status.idle":"2023-01-04T08:52:01.083647Z","shell.execute_reply.started":"2023-01-04T08:52:01.076412Z","shell.execute_reply":"2023-01-04T08:52:01.082118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lalpulsar\n\nconstraints = lalpulsar.SFTConstraints()\nsft_catalog = lalpulsar.SFTdataFind(writer.sftfilepath, constraints)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:02.44358Z","iopub.execute_input":"2023-01-04T08:52:02.444009Z","iopub.status.idle":"2023-01-04T08:52:02.577217Z","shell.execute_reply.started":"2023-01-04T08:52:02.443973Z","shell.execute_reply":"2023-01-04T08:52:02.575897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sft_catalog.data)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:03.442928Z","iopub.execute_input":"2023-01-04T08:52:03.444145Z","iopub.status.idle":"2023-01-04T08:52:03.450897Z","shell.execute_reply.started":"2023-01-04T08:52:03.444088Z","shell.execute_reply":"2023-01-04T08:52:03.450045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fMin = fMax = -1\nmulti_sfts = lalpulsar.LoadMultiSFTs(sft_catalog, fMin, fMax)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:03.9713Z","iopub.execute_input":"2023-01-04T08:52:03.972303Z","iopub.status.idle":"2023-01-04T08:52:04.159577Z","shell.execute_reply.started":"2023-01-04T08:52:03.972262Z","shell.execute_reply":"2023-01-04T08:52:04.158488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(multi_sfts.data))\nprint(len(multi_sfts.data[0].data))\nprint(len(multi_sfts.data[0].data[0].data.data))","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:05.083304Z","iopub.execute_input":"2023-01-04T08:52:05.08373Z","iopub.status.idle":"2023-01-04T08:52:05.091179Z","shell.execute_reply.started":"2023-01-04T08:52:05.083697Z","shell.execute_reply":"2023-01-04T08:52:05.089905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(g[\"H1\"]['SFTs'].shape)\nprint(g[\"L1\"]['SFTs'].shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:06.243271Z","iopub.execute_input":"2023-01-04T08:52:06.243996Z","iopub.status.idle":"2023-01-04T08:52:06.251592Z","shell.execute_reply.started":"2023-01-04T08:52:06.243959Z","shell.execute_reply":"2023-01-04T08:52:06.250229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:07.378921Z","iopub.execute_input":"2023-01-04T08:52:07.379862Z","iopub.status.idle":"2023-01-04T08:52:07.384851Z","shell.execute_reply.started":"2023-01-04T08:52:07.379823Z","shell.execute_reply":"2023-01-04T08:52:07.383701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"array = g[\"H1\"]['SFTs'][:]\n\nfor j in tqdm(range(len(multi_sfts.data[0].data))):\n    for k in range(360):\n        val = array[k][j]\n        multi_sfts.data[0].data[j].data.data[k] = val\n        multi_sfts.data[0].data[j].data.data[k+360] = val\n        multi_sfts.data[0].data[j].data.data[k+360*2] = val\n\narray = g[\"L1\"]['SFTs'][:]\n\nfor j in tqdm(range(len(multi_sfts.data[1].data))):\n    for k in range(360):\n        val = array[k][j]\n        multi_sfts.data[1].data[j].data.data[k] = val\n        multi_sfts.data[1].data[j].data.data[k+360] = val\n        multi_sfts.data[1].data[j].data.data[k+360*2] = val","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:22.423435Z","iopub.execute_input":"2023-01-04T08:52:22.423861Z","iopub.status.idle":"2023-01-04T08:52:36.83143Z","shell.execute_reply.started":"2023-01-04T08:52:22.423824Z","shell.execute_reply":"2023-01-04T08:52:36.830326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir h1.sft\n!mkdir l1.sft\n\nlalpulsar.WriteSFTVector2File(multi_sfts.data[0],\"h1.sft\",\"comment\",\"misc\")\nlalpulsar.WriteSFTVector2File(multi_sfts.data[1],\"l1.sft\",\"comment\",\"misc\")","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:36.833158Z","iopub.execute_input":"2023-01-04T08:52:36.83352Z","iopub.status.idle":"2023-01-04T08:52:39.428403Z","shell.execute_reply.started":"2023-01-04T08:52:36.833488Z","shell.execute_reply":"2023-01-04T08:52:39.42709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls h1.sft","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:39.430041Z","iopub.execute_input":"2023-01-04T08:52:39.431108Z","iopub.status.idle":"2023-01-04T08:52:40.520224Z","shell.execute_reply.started":"2023-01-04T08:52:39.431068Z","shell.execute_reply":"2023-01-04T08:52:40.518959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls l1.sft","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:40.52375Z","iopub.execute_input":"2023-01-04T08:52:40.524248Z","iopub.status.idle":"2023-01-04T08:52:41.634655Z","shell.execute_reply.started":"2023-01-04T08:52:40.5242Z","shell.execute_reply":"2023-01-04T08:52:41.633625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h1_fname = !ls h1.sft\nl1_fname = !ls l1.sft","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:41.635922Z","iopub.execute_input":"2023-01-04T08:52:41.636571Z","iopub.status.idle":"2023-01-04T08:52:41.684359Z","shell.execute_reply.started":"2023-01-04T08:52:41.636536Z","shell.execute_reply":"2023-01-04T08:52:41.682903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_sftfilepath = f\"h1.sft/{h1_fname[0]};l1.sft/{l1_fname[0]}\"\nmy_sftfilepath","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:41.686421Z","iopub.execute_input":"2023-01-04T08:52:41.68676Z","iopub.status.idle":"2023-01-04T08:52:41.694779Z","shell.execute_reply.started":"2023-01-04T08:52:41.686727Z","shell.execute_reply":"2023-01-04T08:52:41.693642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Slice out the band of interest\nfreqs, times, sft_data = pyfstat.utils.get_sft_as_arrays(my_sftfilepath)\n\namplitudes = {key: val[:, :]\n        for key, val in sft_data.items()}\n\nimg = np.empty((2, 1080, 128), dtype=np.float32)\nfor ch, s in enumerate(['H1', 'L1']):\n\n    a = amplitudes[s][:1080,:4096] * 1e22\n    p = a.real **2 + a.imag**2\n    p /= np.mean(p)\n    print(p.shape)\n    p = np.mean(p.reshape(1080, 128, 32), axis=2) \n    img[ch] = p","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:54.356596Z","iopub.execute_input":"2023-01-04T08:52:54.356975Z","iopub.status.idle":"2023-01-04T08:52:54.804245Z","shell.execute_reply.started":"2023-01-04T08:52:54.356946Z","shell.execute_reply":"2023-01-04T08:52:54.803046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img[0])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:55.771609Z","iopub.execute_input":"2023-01-04T08:52:55.772476Z","iopub.status.idle":"2023-01-04T08:52:55.981232Z","shell.execute_reply.started":"2023-01-04T08:52:55.772421Z","shell.execute_reply":"2023-01-04T08:52:55.980119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:52:56.003243Z","iopub.execute_input":"2023-01-04T08:52:56.005044Z","iopub.status.idle":"2023-01-04T08:52:56.205185Z","shell.execute_reply.started":"2023-01-04T08:52:56.004991Z","shell.execute_reply":"2023-01-04T08:52:56.204045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# step3: matched-filter search","metadata":{}},{"cell_type":"code","source":"my_sftfilepath","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:53:11.45487Z","iopub.execute_input":"2023-01-04T08:53:11.455269Z","iopub.status.idle":"2023-01-04T08:53:11.462484Z","shell.execute_reply.started":"2023-01-04T08:53:11.45523Z","shell.execute_reply":"2023-01-04T08:53:11.461015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def search(f0,f1,nsegs,alphas,deltas, visualize=False):\n\n    tstart = (g[\"H1\"][\"timestamps_GPS\"][0] + g[\"L1\"][\"timestamps_GPS\"][0])/2\n    tend = (g[\"H1\"][\"timestamps_GPS\"][-1] + g[\"L1\"][\"timestamps_GPS\"][-1])/2\n    tref = 0.5 * (tstart + tend)\n\n    F0s = [f0]\n    F1s = [f1]\n    F2s = [0]\n\n    search = pyfstat.GridSearch(\n        label=label,\n        outdir=outdir,\n        sftfilepattern=my_sftfilepath,\n        F0s=F0s,\n        F1s=F1s,\n        F2s=F2s,\n        Alphas=alphas,\n        Deltas=deltas,\n        tref=tref,\n        nsegs=nsegs,\n        detectors=\"H1,L1\",\n    )\n    search.run()\n    \n    if visualize:\n        search.plot_2D(xkey=\"Alpha\", ykey=\"Delta\", colorbar=True)\n    return np.max(search.data[\"twoF\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:53:38.823508Z","iopub.execute_input":"2023-01-04T08:53:38.823934Z","iopub.status.idle":"2023-01-04T08:53:38.832998Z","shell.execute_reply.started":"2023-01-04T08:53:38.823898Z","shell.execute_reply":"2023-01-04T08:53:38.831845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"now_f0 = (g[\"frequency_Hz\"][0] + g[\"frequency_Hz\"][-1])/2\nnow_f1 = -1e-10\n\nnow_f0,now_f1","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:53:40.340249Z","iopub.execute_input":"2023-01-04T08:53:40.341043Z","iopub.status.idle":"2023-01-04T08:53:40.350956Z","shell.execute_reply.started":"2023-01-04T08:53:40.341004Z","shell.execute_reply":"2023-01-04T08:53:40.349652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alphas = [0, 2*math.pi, 2*math.pi / 100]\ndeltas = [-math.pi/2, math.pi/2, math.pi / 100]\n\nsearch(now_f0,now_f1, 1000,alphas,deltas,True)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:53:42.892328Z","iopub.execute_input":"2023-01-04T08:53:42.892839Z","iopub.status.idle":"2023-01-04T08:54:24.963972Z","shell.execute_reply.started":"2023-01-04T08:53:42.892802Z","shell.execute_reply":"2023-01-04T08:54:24.962898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def objective(trial):\n    alphas = [0, 2*math.pi, 2*math.pi / 100]\n    deltas = [-math.pi/2, math.pi/2, math.pi / 100]\n    \n    f0 = trial.suggest_float('f0', now_f0 - 0.1, now_f0 + 0.1)\n    f1 = trial.suggest_float('f1', -4e-9, 0)\n    result = search(f0,f1, 1000,alphas,deltas,False)\n\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:57:12.143766Z","iopub.execute_input":"2023-01-04T08:57:12.14421Z","iopub.status.idle":"2023-01-04T08:57:12.151836Z","shell.execute_reply.started":"2023-01-04T08:57:12.144176Z","shell.execute_reply":"2023-01-04T08:57:12.150586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study = optuna.create_study(direction='maximize')\nstudy.optimize(objective, timeout= 3 * 60 * 60)\n\nresult = study.trials_dataframe()\nresult.sort_values(\"value\",ascending=False).head(10)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:57:21.743732Z","iopub.execute_input":"2023-01-04T08:57:21.744147Z","iopub.status.idle":"2023-01-04T08:58:49.882512Z","shell.execute_reply.started":"2023-01-04T08:57:21.744114Z","shell.execute_reply":"2023-01-04T08:58:49.881355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.sort_values(\"value\",ascending=False).to_csv(\"optuna_results.csv\",index=None)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:58:49.884461Z","iopub.execute_input":"2023-01-04T08:58:49.884802Z","iopub.status.idle":"2023-01-04T08:58:49.895172Z","shell.execute_reply.started":"2023-01-04T08:58:49.884773Z","shell.execute_reply":"2023-01-04T08:58:49.894047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(result.params_f0,result.params_f1,c=result.value)\nplt.colorbar()\nplt.xlabel(\"f0\")\nplt.ylabel(\"f1\")\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:58:49.896437Z","iopub.execute_input":"2023-01-04T08:58:49.89677Z","iopub.status.idle":"2023-01-04T08:58:50.198208Z","shell.execute_reply.started":"2023-01-04T08:58:49.89674Z","shell.execute_reply":"2023-01-04T08:58:50.196789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_f0 = study.best_params[\"f0\"]\nbest_f1 = study.best_params[\"f1\"]\n\nbest_f0,best_f1","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:58:50.200878Z","iopub.execute_input":"2023-01-04T08:58:50.20158Z","iopub.status.idle":"2023-01-04T08:58:50.210217Z","shell.execute_reply.started":"2023-01-04T08:58:50.20153Z","shell.execute_reply":"2023-01-04T08:58:50.208722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alphas = [0, 2*math.pi, 2*math.pi / 100]\ndeltas = [-math.pi/2, math.pi/2, math.pi / 100]\nsearch(best_f0,best_f1, 1000,alphas,deltas,True)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T08:58:50.212399Z","iopub.execute_input":"2023-01-04T08:58:50.212871Z","iopub.status.idle":"2023-01-04T08:58:52.628671Z","shell.execute_reply.started":"2023-01-04T08:58:50.212838Z","shell.execute_reply":"2023-01-04T08:58:52.627045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}