In [1]:
!pip install blimpy
!pip install setigen


Collecting blimpy
  Downloading https://files.pythonhosted.org/packages/c6/d6/f8a1ac18f213f650a2beb6dfec1cac6f6c0e220bbb2f45e78c19164f7111/blimpy-2.0.0-py3-none-any.whl (271kB)
     |████████████████████████████████| 276kB 2.8MB/s 
Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from blimpy) (1.0.3)
Requirement already satisfied: matplotlib; python_version > "2.7" in /usr/local/lib/python3.6/dist-packages (from blimpy) (3.2.1)
Requirement already satisfied: cython in /usr/local/lib/python3.6/dist-packages (from blimpy) (0.29.18)
Collecting hdf5plugin
  Downloading https://files.pythonhosted.org/packages/f0/2c/1dfab3cf0dbba35d53db9c3c5653c50cd9cb5795c23056f643830efc8de5/hdf5plugin-2.3.0-py2.py3-none-manylinux2014_x86_64.whl (5.4MB)
     |████████████████████████████████| 5.4MB 27.0MB/s 
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from blimpy) (1.18.4)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from blimpy) (1.12.0)
Requirement already satisfied: astropy; python_version > "2.7" in /usr/local/lib/python3.6/dist-packages (from blimpy) (4.0.1.post1)
Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from blimpy) (2.10.0)
Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from blimpy) (1.4.1)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->blimpy) (2018.9)
Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas->blimpy) (2.8.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib; python_version > "2.7"->blimpy) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib; python_version > "2.7"->blimpy) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib; python_version > "2.7"->blimpy) (0.10.0)
Installing collected packages: hdf5plugin, blimpy
Successfully installed blimpy-2.0.0 hdf5plugin-2.3.0
Collecting setigen
  Downloading https://files.pythonhosted.org/packages/ed/cd/d10adf7d1762632a47b542dac16aec3a00a3f207af65671c547537eb7737/setigen-1.2.8-py3-none-any.whl
Requirement already satisfied: numpy>=1.18.1 in /usr/local/lib/python3.6/dist-packages (from setigen) (1.18.4)
Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.6/dist-packages (from setigen) (1.4.1)
Collecting sphinx-rtd-theme==0.4.3
  Downloading https://files.pythonhosted.org/packages/60/b4/4df37087a1d36755e3a3bfd2a30263f358d2dea21938240fa02313d45f51/sphinx_rtd_theme-0.4.3-py2.py3-none-any.whl (6.4MB)
     |████████████████████████████████| 6.4MB 4.0MB/s 
Requirement already satisfied: matplotlib>=3.1.3 in /usr/local/lib/python3.6/dist-packages (from setigen) (3.2.1)
Requirement already satisfied: astropy>=4.0 in /usr/local/lib/python3.6/dist-packages (from setigen) (4.0.1.post1)
Requirement already satisfied: blimpy>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from setigen) (2.0.0)
Requirement already satisfied: sphinx in /usr/local/lib/python3.6/dist-packages (from sphinx-rtd-theme==0.4.3->setigen) (1.8.5)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.1.3->setigen) (2.4.7)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.1.3->setigen) (2.8.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.1.3->setigen) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=3.1.3->setigen) (1.2.0)
Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from blimpy>=2.0.0->setigen) (2.10.0)
Requirement already satisfied: cython in /usr/local/lib/python3.6/dist-packages (from blimpy>=2.0.0->setigen) (0.29.18)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from blimpy>=2.0.0->setigen) (1.12.0)
Requirement already satisfied: hdf5plugin in /usr/local/lib/python3.6/dist-packages (from blimpy>=2.0.0->setigen) (2.3.0)
Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from blimpy>=2.0.0->setigen) (1.0.3)
Requirement already satisfied: packaging in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (20.4)
Requirement already satisfied: alabaster<0.8,>=0.7 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (0.7.12)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (46.3.0)
Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (2.23.0)
Requirement already satisfied: snowballstemmer>=1.1 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (2.0.0)
Requirement already satisfied: babel!=2.0,>=1.3 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (2.8.0)
Requirement already satisfied: imagesize in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (1.2.0)
Requirement already satisfied: sphinxcontrib-websupport in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (1.2.2)
Requirement already satisfied: Pygments>=2.0 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (2.1.3)
Requirement already satisfied: docutils>=0.11 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (0.15.2)
Requirement already satisfied: Jinja2>=2.3 in /usr/local/lib/python3.6/dist-packages (from sphinx->sphinx-rtd-theme==0.4.3->setigen) (2.11.2)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas->blimpy>=2.0.0->setigen) (2018.9)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests>=2.0.0->sphinx->sphinx-rtd-theme==0.4.3->setigen) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.0.0->sphinx->sphinx-rtd-theme==0.4.3->setigen) (2020.4.5.1)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests>=2.0.0->sphinx->sphinx-rtd-theme==0.4.3->setigen) (1.24.3)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.0.0->sphinx->sphinx-rtd-theme==0.4.3->setigen) (2.9)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/dist-packages (from Jinja2>=2.3->sphinx->sphinx-rtd-theme==0.4.3->setigen) (1.1.1)
Installing collected packages: sphinx-rtd-theme, setigen
Successfully installed setigen-1.2.8 sphinx-rtd-theme-0.4.3

In [0]:
import math
%matplotlib inline
import pylab as plt
import numpy as np 
from blimpy import Waterfall
from astropy import units as u
import numpy as np
import setigen as stg
from copy import deepcopy

def SNR(arr):
  index = np.argmax(arr)
  average_noise = arr.mean(axis=0)
  return math.log(arr[index]/average_noise) 

def delay(fchan, data, DM):
  val = 1+data.shape[1]-fchan
  return int(DM*val**2)

def de_disperse(data,DM):
  clean = deepcopy(data)
  for i in range(clean.shape[1]):
    end = clean.shape[0]
    time = delay(i,clean, DM)
    if time!=0 and time<clean.shape[0]:
      zero_block = np.zeros((time))
      shift_block = clean[:end-time,i]
      clean[time:end,i]= shift_block
      clean[:time,i]= zero_block
    elif time!=0:
      clean[:,i]= np.zeros(clean[:,i].shape)
  return clean

def DM_can(data, data_base, sens, DM_base, candidates ):
  snrs = np.zeros((candidates,2))
  for i in range(candidates):
    DM = DM_base+sens*i
    data = de_disperse(data, DM=DM)
    time_series = data.sum(axis=1)
    snrs[i,1] = SNR(time_series)
    snrs[i,0] =DM
    if i%int(candidates/10)==0:
      print(snrs[i,1])
    data = data_base
  return snrs

In [174]:
# Define time and frequency arrays, essentially labels for the 2D data array
fchans = 300
tchans = 30
df = 2.7939677238464355+10
dt = 18.25361108
fch1 = 6095.214842353016 * 10**6

# Generate the signal
frame = stg.Frame(fchans=fchans,
                  tchans=tchans,
                  df=df,
                  dt=dt,
                  fch1=fch1)

signal = frame.add_signal(stg.squared_path(f_start=frame.get_frequency(40),
                                           drift_rate=0.05*u.Hz/u.s),
                          stg.constant_t_profile(level=10),
                          stg.box_f_profile(width=100*u.Hz),
                          stg.constant_bp_profile(level=0.1))
data = frame.data
fig = plt.figure(figsize=(10, 6))
plt.imshow(data, aspect='auto')
plt.colorbar()
plt.show()

DM = 0.0002
clean = de_disperse(data, DM=DM)
fig = plt.figure(figsize=(10, 6))
plt.imshow(clean, aspect='auto')
plt.colorbar()
plt.show()



In [177]:
frame = stg.Frame(fchans=fchans,
                  tchans=tchans,
                  df=df,
                  dt=dt,
                  fch1=fch1)

signal = frame.add_signal(stg.squared_path(f_start=frame.get_frequency(40),
                                           drift_rate=0.05*u.Hz/u.s),
                          stg.constant_t_profile(level=10),
                          stg.box_f_profile(width=100*u.Hz),
                          stg.constant_bp_profile(level=0.1))



data_base = frame.data
data = frame.data
sens = 0.0000001
DM_base = 0.00001
candidates = 5000

snrs = DM_can(data, data_base, sens, DM_base, candidates)


0.41173472112175985
1.104881901681705
1.2226649373380887
2.02117263355586
2.6497812929782345
2.302585092994046
1.9432110920861483
1.7980290822416505
1.791759469228055
2.1092137000135063

In [178]:
plt.plot(snrs[:,0], snrs[:,1])
plt.title('DM values vs SNR')
plt.xlabel("DM values")
plt.ylabel("SNR of Dedispersion")


Out[178]:
Text(0, 0.5, 'SNR of Dedispersion')

In [180]:
print(snrs[np.argmax(snrs[:,1]),0])


0.0002264