{"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":"markdown","source":"# Generating Pure Signal\n\nThis notebook generates pure signal without any noise.\n\nOutput is stored in: []()","metadata":{}},{"cell_type":"code","source":"!pip install -q git+https://github.com/PyFstat/PyFstat@python37","metadata":{"execution":{"iopub.status.busy":"2022-12-10T23:19:25.889448Z","iopub.execute_input":"2022-12-10T23:19:25.890191Z","iopub.status.idle":"2022-12-10T23:20:21.313591Z","shell.execute_reply.started":"2022-12-10T23:19:25.890097Z","shell.execute_reply":"2022-12-10T23:20:21.312064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import logging\nlogging.getLogger('pyfstat').setLevel(logging.WARNING)\n\nSAMPLES = 20000  # increase to produce more samples","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T23:20:21.316315Z","iopub.execute_input":"2022-12-10T23:20:21.316719Z","iopub.status.idle":"2022-12-10T23:20:21.326559Z","shell.execute_reply.started":"2022-12-10T23:20:21.31668Z","shell.execute_reply":"2022-12-10T23:20:21.324198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nimport numpy as np\nfrom scipy import stats\n# https://pyfstat.readthedocs.io/en/latest/pyfstat.html\n\ndef get_signal_config():\n    t_start = 1238166018 // 1800 * 1800\n\n    # These parameters describe background noise and data format\n    writer_kwargs = {\n            'sqrtSX': 1e-23, # Single-sided Amplitude Spectral Density of the noise\n            'Tsft': 1800, # Fourier transform time duration\n            \"SFTWindowType\": \"tukey\",  # Window function to compute short Fourier transforms\n            \"SFTWindowBeta\": 0.01,  # Parameter associated to the window function\n            \"detectors\": \"H1,L1\",\n            # 'timestamps': {\n            #     'H1': t_start + 1800 * get_timestamp_idxs(),\n            #     'L1': t_start + 1800 * get_timestamp_idxs(),\n            # }\n            'tstart': t_start,\n            'duration': (1248512757 - t_start) // 1800 * 1800\n       }\n\n    signal_params = {\n        # polarization angle\n        'psi': np.random.uniform(-math.pi / 4, math.pi / 4),\n        # phase\n        'phi': np.random.uniform(0, math.pi * 2),\n        # Cosine of the angle between the source and us. Range: [-1, 1]\n        'cosi': np.random.uniform(-1, 1),\n        # Central frequency of the band to be generated [Hz]\n        'F0': np.random.uniform(50, 500),\n        'F1': -10**stats.uniform(-12, 4).rvs(),\n        'F2': 0.0,\n        'Band': 0.2, # Frequency band-width around F0 [Hz]\n        'Alpha': np.random.uniform(0, math.pi * 2), # Right ascension of the source's position on the sky\n        'Delta': np.random.uniform(-math.pi / 2, math.pi / 2), # Declination of the source's position on the sky,\n        'tp': t_start, #+ 86400 * random.randint(0, 30), # signal offset\n        # 'h0': writer_kwargs[\"sqrtSX\"] * np.random.uniform(0.10, 0.02),\n        'h0': writer_kwargs[\"sqrtSX\"] * 100,\n#         'asini': random.randint(10, 500), # amplitude of signal\n#         'period': random.randint(90, 730) * 86400,\n    }\n\n    return writer_kwargs, signal_params\n\nget_signal_config()","metadata":{"execution":{"iopub.status.busy":"2022-12-10T23:20:21.329839Z","iopub.execute_input":"2022-12-10T23:20:21.331099Z","iopub.status.idle":"2022-12-10T23:20:21.834813Z","shell.execute_reply.started":"2022-12-10T23:20:21.331044Z","shell.execute_reply":"2022-12-10T23:20:21.833572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tempfile\nimport sys\nimport cv2\nimport cv2\n\n\ndef generate_signal(idx):\n\n    import pyfstat\n    dir = tempfile.mkdtemp()\n    np.random.seed(idx)\n    writer_kwargs, signal_params = get_signal_config()\n    writer_kwargs['outdir'] = dir\n    writer_kwargs['label'] = 'Signal'\n    try:\n        writer = pyfstat.BinaryModulatedWriter(**writer_kwargs, **signal_params)\n        writer.make_data()\n        frequency, timestamps, amplitudes = pyfstat.utils.get_sft_as_arrays(\n            writer.sftfilepath\n        )\n        !rm -rf $dir\n        files = []\n        for det in amplitudes:\n            amp = np.abs(amplitudes[det][-360:, :])\n            amp = amp[:, :256 * 22].reshape(360, 512, 11).mean(axis=2)\n            amp = (amp - amp.min())\n            amp = amp * 255 / (amp.max())\n            os.makedirs(f'data/pure_signal/{idx:05d}', exist_ok=True)\n            fname = f'data/pure_signal/{idx:05d}/{det}.png'\n            files.append(fname)\n            cv2.imwrite(fname, amp)\n        return files\n    except Exception as ex:\n        print(ex)","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T23:20:21.836979Z","iopub.execute_input":"2022-12-10T23:20:21.837326Z","iopub.status.idle":"2022-12-10T23:20:22.040376Z","shell.execute_reply.started":"2022-12-10T23:20:21.837295Z","shell.execute_reply":"2022-12-10T23:20:22.03922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfrom concurrent.futures import ProcessPoolExecutor\nfrom matplotlib import pyplot as plt\n\n\nwith ProcessPoolExecutor() as p:\n    for files in tqdm(p.map(generate_signal, range(SAMPLES))):\n        # print(files)\n        pass\n    # plt.figure(figsize=(30, 5))\n    # amp = generate_signal()\n    # plt.imshow(amp)","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T23:20:22.042077Z","iopub.execute_input":"2022-12-10T23:20:22.042666Z","iopub.status.idle":"2022-12-10T23:23:06.382548Z","shell.execute_reply.started":"2022-12-10T23:20:22.04263Z","shell.execute_reply":"2022-12-10T23:23:06.38026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Examples of pure signal images","metadata":{}},{"cell_type":"code","source":"import glob\nfrom PIL import Image\nfor i in glob.glob('data/pure_signal/*/*')[:10]:\n    display(Image.open(i))","metadata":{"execution":{"iopub.status.busy":"2022-12-10T23:23:37.470354Z","iopub.execute_input":"2022-12-10T23:23:37.470793Z","iopub.status.idle":"2022-12-10T23:23:37.631405Z","shell.execute_reply.started":"2022-12-10T23:23:37.470759Z","shell.execute_reply":"2022-12-10T23:23:37.630248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Examples of generated images with realistic noise","metadata":{}},{"cell_type":"markdown","source":"import glob\n\nfor noise, signal in zip(np.random.choice(glob.glob('/kaggle/input/g2net-realistic-simulation-of-test-noise/data/realistic_noise/images/*/*.png'), size=10), np.random.choice(glob.glob('data/pure_signal/*/*.png'), size=10)):\n    noise = cv2.imread(noise)\n    signal = cv2.imread(signal)\n    plt.figure()\n    plt.imshow(noise + (signal / 5).astype(np.uint8))","metadata":{"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-12-10T23:23:45.455006Z","iopub.execute_input":"2022-12-10T23:23:45.455566Z","iopub.status.idle":"2022-12-10T23:23:46.27769Z","shell.execute_reply.started":"2022-12-10T23:23:45.455451Z","shell.execute_reply":"2022-12-10T23:23:46.276409Z"}}}]}