Beispiel #1
0
    npy = np.load(npy_fil)
    npy_sub = np.flipud(
        np.nanmean(npy.reshape(-1, subfactor, npy.shape[1]), axis=1))
    timeseries = npy_sub.sum(0)
    return npy, npy_sub, timeseries


npy_fil = '../GMRT_D.dynamicspec_349.19_scrunch4.npy'
bandwidth = 200.  #MHz
center_frequency = 400.  #MHz
file_duration = 300.  #ms
subfactor = 1

npy, npy_sub, timeseries = sub_npy(npy_fil, subfactor, file_duration,
                                   bandwidth, center_frequency)
peaks, widths, snrs = find_burst(timeseries)
nchan = npy.shape[0]
freq_res = bandwidth / nchan

#Resolutions
tres = file_duration / npy.shape[1]
print('Raw Time Resolution (microsec): ', tres * 1e3)
nchan = npy.shape[0]
fres = bandwidth / nchan
print('Raw Frequency Resolution (kHz): ', fres * 1e3)

#Define windowing depending on where burst sits in dynspec
window_left = int(peaks - 1 * widths)
window_right = int(peaks + 1 * widths)

if window_right - window_left <= 100:
def burst_info(idx):
    npy_fils = [
        "propagation_effects/A_117_dm348.8.fits.npy",
        "propagation_effects/B_686_dm348.8.fits.npy",
        "propagation_effects/C_1164_dm348.8_lores.npy",
        "propagation_effects/D_267_dm348.8_lores.npy",
        "propagation_effects/E_579_dm348.8.fits.npy",
        "propagation_effects/F_639_dm348.8.fits.npy",
        "propagation_effects/G_1549_dm348.8.fits.npy",
        "propagation_effects/GMRT_A.dynamicspec_348.8.npy",
        "propagation_effects/GMRT_B.dynamicspec_349.19.npy",
        "propagation_effects/GMRT_C.dynamicspec_350_scrunch20.npy",
        "propagation_effects/GMRT_D.dynamicspec_349.19_scrunch4.npy"
    ]

    npy = np.load(npy_fils[idx])
    timeseries = npy.sum(0)

    keys = [
        'gbta', 'gbtb', 'gbtc', 'gbtd', 'gbte', 'gbtf', 'gbtg', 'gmrta',
        'gmrtb', 'gmrtc', 'gmrtd'
    ]
    pfits_key = keys[idx]
    print('Burst: ', pfits_key)
    bw = [400., 400., 400., 400., 400., 400., 400., 200., 200., 200.,
          200.]  #MHz
    cf = [800., 800., 800., 800., 800., 800., 800., 400., 400., 400.,
          400.]  #MHz
    fd = [
        83.33, 83.33, 333.33, 166.68, 83.33, 83.33, 83.33, 163.84, 122.88,
        400.0, 300.
    ]  #ms
    subfactor = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    bandwidth = bw[idx]
    print('Bandwidth (MHz): ', bw[idx])
    center_frequency = cf[idx]
    print('Center Frequency (MHz): ', cf[idx])
    file_duration = fd[idx]
    print('File Duration (ms): ', fd[idx])
    sampling_time = (file_duration / npy.shape[1])
    print('Sampling Time (ms): ', sampling_time)
    print('Number of Samples: ', npy.shape[1])
    nchan = npy.shape[0]
    print('Number of Channels: ', nchan)

    #def sub_npy(npy_fil, file_duration, bandwidth, center_frequency, subfactor = 1):
    #    npy = np.load(npy_fil)
    #    npy_sub = np.flipud(np.nanmean(npy.reshape(-1, subfactor, npy.shape[1]), axis=1))
    #    timeseries = npy_sub.sum(0)
    #return npy, npy_sub, timeseries
    #npy, npy_sub, timeseries = sub_npy(npy, file_duration, bandwidth, center_frequency)

    #Find burst peaks, widths & snrs
    peaks, widths, snrs = find_burst(timeseries)
    print('Peak Location (grid): ', peaks)
    print('Peak Width (grid): ', widths)

    #Resolutions
    tres = file_duration / npy.shape[1]
    print('Raw Time Resolution (microsec): ', tres * 1e3)
    fres = bandwidth / nchan
    print('Raw Frequency Resolution (kHz): ', fres * 1e3)

    #Define windowing depending on where burst sits in dynspec
    window_left = int(peaks - 1 * widths)
    window_right = int(peaks + 1 * widths)

    if window_right - window_left <= 100:
        window_right = window_right + 300
        window_left = window_left - 300
    if window_left <= 0:
        window_left = 0
    if window_right >= npy.shape[1]:
        window_right = npy.shape[1]
    print('Window (left): ', window_left)
    print('Window (right): ', window_right)

    return npy_fils
Beispiel #3
0
def nested_sampling(npy_fil,
                    p0,
                    comp_num,
                    nlive=500,
                    bandwidth=400.,
                    center_frequency=800.,
                    file_duration=83.33,
                    subfactor=1.,
                    debug=False):
    """
    Perform nested sampling profile fitting with bilby.
    """

    npy, npy_sub, timeseries = sub_npy(npy_fil, subfactor, file_duration,
                                       bandwidth, center_frequency)
    peaks, widths, snrs = find_burst(timeseries)

    # Calculate the time and frequency resolutions of the array
    time_res = file_duration / npy.shape[1]
    print('Raw Time Resolution (microsec): ', time_res * 1e3)
    num_chan = npy.shape[0]
    freq_res = bandwidth / num_chan
    print('Raw Frequency Resolution (kHz): ', freq_res * 1e3)

    #Define windowing depending on where burst sits in dynspec
    window_left = int(peaks - 1 * widths)
    window_right = int(peaks + 1 * widths)

    sub_factor_time = 1
    y_data = (npy[:].sum(0) / np.max(npy[:].sum(0)))[window_left:window_right]
    #y_data = y_data.reshape(-1, sub_factor_time).mean(axis=1)
    sampling_time = (file_duration / npy.shape[1]) * sub_factor_time
    print('Sampling Time (ms): ', sampling_time)
    time = np.arange(len(y_data)) * sampling_time
    sigma = np.repeat(sampling_time, len(time))

    ##Initial Guesses (fit with Wael's slider)
    #p0 = [7.43, 0.29, 0.27, 0.05,
    #      7.86, 0.38, 0.68, 1.67,
    #      9.98, 0.32, 0.32, 1.78,
    #     13.57, 0.23, 0.10, 1.29]

    #Upper and lower bounds for prior
    lower_bounds = [(i - i / 2) for i in p0]
    upper_bounds = [(i + i / 2) for i in p0]
    lower_bounds = [round(i, 2) for i in lower_bounds]
    upper_bounds = [round(i, 2) for i in upper_bounds]

    print('Lower: ', lower_bounds)
    print('Upper: ', upper_bounds)

    #Define data to fit to
    injection_params = dict(x1=p0[0],
                            amp1=p0[1],
                            sig1=p0[2],
                            tau1=p0[3],
                            x2=p0[4],
                            amp2=p0[5],
                            sig2=p0[6],
                            tau2=p0[7],
                            x3=p0[8],
                            amp3=p0[9],
                            sig3=p0[10],
                            tau3=p0[11],
                            x4=p0[12],
                            amp4=p0[13],
                            sig4=p0[14],
                            tau4=p0[15])

    if debug:
        #Random Exponential Gaussian to test fit
        fig = plt.figure()
        plt.errorbar(time, y_data, yerr=sigma)
        plt.show()

    print('Fitting Initiated')

    label = str(npy_fil)
    outdir = str(npy_fil) + '_profilefit'

    likeli = bilby.core.likelihood.GaussianLikelihood(time,
                                                      y_data,
                                                      exp_gauss_multi,
                                                      sigma=sigma)

    prior = dict(x1=bilby.core.prior.Uniform(lower_bounds[0], upper_bounds[0],
                                             'x1'),
                 amp1=bilby.core.prior.Uniform(lower_bounds[1],
                                               upper_bounds[1], 'amp1'),
                 sig1=bilby.core.prior.Uniform(lower_bounds[2],
                                               upper_bounds[2], 'sig1'),
                 tau1=bilby.core.prior.Uniform(lower_bounds[3],
                                               upper_bounds[3], 'tau1'),
                 x2=bilby.core.prior.Uniform(lower_bounds[4], upper_bounds[4],
                                             'x2'),
                 amp2=bilby.core.prior.Uniform(lower_bounds[5],
                                               upper_bounds[5], 'amp2'),
                 sig2=bilby.core.prior.Uniform(lower_bounds[6],
                                               upper_bounds[6], 'sig2'),
                 tau2=bilby.core.prior.Uniform(lower_bounds[7],
                                               upper_bounds[7], 'tau2'),
                 x3=bilby.core.prior.Uniform(lower_bounds[8], upper_bounds[8],
                                             'x3'),
                 amp3=bilby.core.prior.Uniform(lower_bounds[9],
                                               upper_bounds[9], 'amp3'),
                 sig3=bilby.core.prior.Uniform(lower_bounds[10],
                                               upper_bounds[10], 'sig3'),
                 tau3=bilby.core.prior.Uniform(lower_bounds[11],
                                               upper_bounds[11], 'tau3'),
                 x4=bilby.core.prior.Uniform(lower_bounds[12],
                                             upper_bounds[12], 'x4'),
                 amp4=bilby.core.prior.Uniform(lower_bounds[13],
                                               upper_bounds[13], 'amp4'),
                 sig4=bilby.core.prior.Uniform(lower_bounds[14],
                                               upper_bounds[14], 'sig4'),
                 tau4=bilby.core.prior.Uniform(lower_bounds[15],
                                               upper_bounds[15], 'tau4'))

    # Adjust initial conditions and priors depending the number of burst components
    if comp_num == 1:
        injection_params = dict(list(injection_params.items())[0:4])
        prior = dict(list(prior.items())[0:4])
    elif comp_num == 2:
        injection_params = dict(list(injection_params.items())[0:8])
        prior = dict(list(prior.items())[0:8])
    elif comp_num == 3:
        injection_params = dict(list(injection_params.items())[0:12])
        prior = dict(list(prior.items())[0:12])
    elif comp_num == 4:
        injection_params = dict(list(injection_params.items())[0:16])
        prior = dict(list(prior.items())[0:16])

    print('Sampler Running')

    # Do the nested sampling
    result = bilby.run_sampler(likelihood=likeli,
                               priors=prior,
                               injection_parameters=injection_params,
                               sampler='dynesty',
                               nlive=500,
                               outdir=outdir,
                               label=label)

    result.plot_corner()

    print('Fit Complete!')

    return
def burst_info_all(npy_fils, idx):

    pfits, pfits_errors, fwhm_width_errors, fwhm2_width_errors = pfits_dict()

    keys = [
        'gbta', 'gbtb', 'gbtc', 'gbtd', 'gbte', 'gbtf', 'gbtg', 'gmrta',
        'gmrtb', 'gmrtc', 'gmrtd'
    ]

    npy = np.load(npy_fils[idx])
    timeseries = npy.sum(0)

    if keys[idx] == 'gbta':
        npy = npy[1600:, :]

    pfits_key = keys[idx]
    print('Burst: ', pfits_key)
    bw = [400., 400., 400., 400., 400., 400., 400., 200., 200., 200.,
          200.]  #MHz
    cf = [800., 800., 800., 800., 800., 800., 800., 400., 400., 400.,
          400.]  #MHz
    fd = [
        83.33, 83.33, 333.33, 166.68, 83.33, 83.33, 83.33, 163.84, 122.88,
        400.0, 300.
    ]  #ms
    subfactor = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    print('Archive File Duration (ms): ', fd[idx])
    bandwidth = bw[idx]
    print('Bandwidth (MHz): ', bw[idx])
    center_frequency = cf[idx]
    file_duration = fd[idx]
    sampling_time = (file_duration / npy.shape[1])
    nchan = npy.shape[0]
    freq_res = bandwidth / nchan

    #Resolutions
    tres = file_duration / npy.shape[1]
    fres = bandwidth / nchan

    peaks, widths, snrs = find_burst(timeseries)

    #Define windowing depending on where burst sits in dynspec
    window_left = int(peaks - 1 * widths)
    window_right = int(peaks + 1 * widths)

    if keys[idx] == 'gmrtc' or keys[idx] == 'gmrtd':
        wrr = 50
        wll = 50
    else:
        wrr = 300
        wll = 300

    if window_right - window_left <= 100:
        window_right = window_right + wrr
        window_left = window_left - wll
    if window_left <= 0:
        window_left = 0
    if window_right >= npy.shape[1]:
        window_right = npy.shape[1]

    y_data = (npy[:].sum(0) / np.max(npy[:].sum(0)))[window_left:window_right]
    x = np.arange(len(y_data)) * sampling_time

    y, burst_width, alt_burst_width = exp_gauss_4(x, *pfits[str(pfits_key)])

    print(str(keys[idx]) + ' width (ms): ' + str(round(burst_width, 2)) + \
          ' +/- ' + str(round(fwhm_width_errors[str(pfits_key)], 2)))

    if keys[idx] == 'gbtb':
        print('No Alternative Burst Width')

    else:
        print(str(keys[idx]) + ' width (ms): ' + str(round(alt_burst_width, 2)) + \
              ' +/- ' + str(round(fwhm2_width_errors[str(pfits_key)], 2)))

    return x, y, y_data, pfits_key