コード例 #1
0
    def make_lc(evtfile, bkgevtfile, epochfile, dt):
        epoch_file = np.loadtxt(epochfile)
        evt_file = np.loadtxt(evtfile)
        bkgevt_file = np.loadtxt(bkgevtfile)

        tstart = epoch_file[:, 0]
        tstop = epoch_file[:, 1]
        ID = epoch_file[:, 2]
        exptime = epoch_file[:, 3]
        T_tot = tstop[-1] - tstart[0]
        cts_rate = len(evt_file) / (2 *
                                    np.sum(exptime)) * dt  # 实际的cts-rate应为这个的2倍
        num_bins = int(T_tot / dt)
        sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt)
        w = np.arange(1 / T_tot, 0.5 / dt, 1 / (T_tot))
        spectrum = bending_po(
            w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(
                w, [4.01e-4, 4.01e-4 / 16, 200, 2])
        lc = sim.simulate(spectrum)
        # lc=sim.simulate(pow)
        lc.time = lc.time + tstart[0]
        # lc.gti=[[lc.time[0],lc.time[-1]]]
        lc.counts += cts_rate
        lc.counts[np.where(lc.counts < 0)] = 0

        return lc
コード例 #2
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
    def test_compare_composite(self):
        """
        Compare the PSD of a light curve simulated using a composite model
        (using SmoothBrokenPowerLaw plus GeneralizedLorentz1D)
        with the actual model
        """
        N = 50000
        dt = 0.01
        m = 30000.

        self.simulator = simulator.Simulator(N=N, mean=m, dt=dt)
        smoothbknpo = models.SmoothBrokenPowerLaw(norm=1.,
                                                  gamma_low=1.,
                                                  gamma_high=2.,
                                                  break_freq=1.)
        lorentzian = models.GeneralizedLorentz1D(x_0=10,
                                                 fwhm=1.,
                                                 value=10.,
                                                 power_coeff=2.)
        myModel = smoothbknpo + lorentzian

        lc = [self.simulator.simulate(myModel) for i in range(1, 50)]

        simulated = self.simulator.powerspectrum(lc, lc[0].tseg)

        w = np.fft.rfftfreq(N, d=dt)[1:]
        actual = myModel(w)[:-1]

        actual_prob = actual / float(sum(actual))
        simulated_prob = simulated / float(sum(simulated))

        assert np.all(
            np.abs(actual_prob - simulated_prob) < 3 * np.sqrt(actual_prob))
コード例 #3
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
    def test_compare_smoothbknpo(self):
        """
        Compare simulated smooth broken power law spectrum with original
        spectrum.
        """
        N, red_noise, dt = 1024, 10, 1

        self.simulator = simulator.Simulator(N=N,
                                             dt=dt,
                                             mean=0.1,
                                             rms=0.7,
                                             red_noise=red_noise)
        lc = [
            self.simulator.simulate('smoothbknpo', [0.6, 0.2, 0.6, 0.5])
            for i in range(1, 30)
        ]

        simulated = self.simulator.powerspectrum(lc, lc[0].tseg)

        w = np.fft.rfftfreq(N, d=dt)[1:]
        actual = models.smoothbknpo(w, [0.6, 0.2, 0.6, 0.5])[:-1]

        actual_prob = actual / float(sum(actual))
        simulated_prob = simulated / float(sum(simulated))

        assert np.all(
            np.abs(actual_prob - simulated_prob) < 3 * np.sqrt(actual_prob))
コード例 #4
0
ファイル: test_simulator.py プロジェクト: tappina/stingray
    def test_compare_lorentzian(self):
        """
        Compare simulated lorentzian spectrum with original spectrum.
        """
        N, red_noise, dt = 1024, 10, 1

        self.simulator = simulator.Simulator(N=N,
                                             dt=dt,
                                             mean=0.1,
                                             rms=0.4,
                                             red_noise=red_noise)
        lc = [
            self.simulator.simulate('generalized_lorentzian',
                                    [0.3, 0.9, 0.6, 0.5])
            for i in range(1, 30)
        ]
        simulated = self.simulator.powerspectrum(lc, lc[0].tseg)

        w = np.fft.rfftfreq(N, d=dt)[1:]
        actual = models.generalized_lorentzian(w, [0.3, 0.9, 0.6, 0.5])[:-1]

        actual_prob = actual / float(sum(actual))
        simulated_prob = simulated / float(sum(simulated))

        assert np.all(
            np.abs(actual_prob - simulated_prob) < 3 * np.sqrt(actual_prob))
コード例 #5
0
 def test_simulate_with_tstart(self):
     """
     Simulate with a random seed value.
     """
     tstart = 10.0
     self.simulator = simulator.Simulator(N=1024, tstart=tstart)
     assert self.simulator.time[0] == tstart
コード例 #6
0
ファイル: simulate_psd_old.py プロジェクト: twiight/pulsar_2
def sim_bunch_lc_evt(CR, dt, period, epoch_file, outpath, num_trials=100):
    lenbin = dt
    # epoch_89 = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/epoch_src_89.txt'
    w = make_freq_range(dt=lenbin, epoch_file=epoch_file)
    qpo_f = 1. / period
    (TSTART, TSTOP, OBSID, exptime) = epoch_file
    if type(TSTART) == type(1.2):
        print('Single exposure')
        TSTART = [TSTART]
        TSTOP = [TSTOP]
        OBSID = [OBSID]
        exptime = [exptime]

    with open(
            outpath + 'CR_{0}_P_{1}_REJ1034_simpleway.txt'.format(
                "%.0e" % CR, str(int(period))), 'a+') as f:
        k_trial = 0
        while k_trial < num_trials:
            ev_all = EventList()
            # lc = make_lc_from_psd(psd=psd_model, cts_rate=CR / 2 * lenbin, dt=lenbin, epoch_file=epoch_file)
            print('trial' + ':  ' + str(k_trial))
            # lc_evt = Lightcurve(time=lc.time, counts=lc.counts, dt=lc.dt, gti=lc.gti)
            # lc_evt.counts = np.random.poisson(lc_evt.counts)
            T_tot = TSTOP[-1] - TSTART[0]
            for i in range(len(exptime)):
                cts_rate = 0.5 * CR * lenbin  # 实际的cts-rate应为这个的2倍
                num_bins = int(exptime[i] / dt)
                sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=lenbin)
                w = np.arange(1 / exptime[i], 0.5 / lenbin, 1 / exptime[i])
                psd_model = build_psd(x=w,
                                      p=[2.3e-3, 3.4, 0., 4.3e-4],
                                      x_2=w,
                                      p_2=[qpo_f, qpo_f / 16, 100, 2],
                                      type='bendp+lorentz')
                # psd_model = build_psd(x=w, p=[2.3e-3, 3.4, 0., 4.3e-4],
                #                       type='bendp')
                spectrum = psd_model
                lc_cut = sim.simulate(spectrum)
                lc_cut.counts += cts_rate
                lc_cut.counts[np.where(lc_cut.counts < 0)] = 0
                lc_cut.counts = np.random.poisson(lc_cut.counts)
                # print(np.sum(lc_cut.counts))
                ev = EventList()
                ev.time = func.sim_evtlist(lc_cut) + TSTART[i]
                ev_all = ev_all.join(ev)
            lc_long = ev_all.to_lc(dt=dt,
                                   tstart=ev_all.time[0] - 0.5 * dt,
                                   tseg=ev_all.time[-1] - ev_all.time[0])
            print('counts={0}'.format(np.sum(lc_long.counts)))
            T_exp = lc_long.time[-1] - lc_long.time[0]
            freq = np.arange(1 / T_exp, 0.5 / lenbin, 1 / (5 * T_exp))
            freq = freq[np.where(freq > 1 / 20000.)]
            # print(T_tot)
            # print(T_exp)
            temp = func.get_LS(lc_long.time, lc_long.counts, freq=freq)
            f.writelines((str(temp[0]) + '        ' + str(temp[1]) +
                          '        ' + str(temp[2]) + '  ' + '\n'))
            k_trial += 1
    f.close()
コード例 #7
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
 def test_io_with_unsupported_format(self):
     sim = simulator.Simulator(N=1024)
     with pytest.raises(KeyError):
         sim.write('sim.hdf5', format_='hdf5')
     with pytest.raises(KeyError):
         sim.write('sim.pickle', format_='pickle')
         sim.read('sim.pickle', format_='hdf5')
     os.remove('sim.pickle')
コード例 #8
0
ファイル: sim_psd.py プロジェクト: twiight/pulsar_2
def make_lc_from_psd(psd, cts_rate, dt, epoch_file, frms=1):
    (TSTART, TSTOP, OBSID, exptime) = epoch_file
    T_tot = TSTOP[-1] - TSTART[0]
    num_bins = int(T_tot / dt)
    sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt, rms=frms)
    lc = sim.simulate(psd)
    lc.time = lc.time + TSTART[0]
    lc.counts[np.where(lc.counts < 0)] = 0
    lc.gti = [[lc.time[0], lc.time[-1]]]
    return lc
コード例 #9
0
ファイル: stingray_sim.py プロジェクト: twiight/pulsar_2
def get_lc_onesource_fixpds(k,src_index,num_trials=2):
    k_trial =0
    FP = [];
    period = [];
    cts_num=[];
    peakP=[];
    power_P=[]
    path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k)
    epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k))
    tstart = epoch_file[:, 0];tstop = epoch_file[:, 1]
    ID=epoch_file[:,2];exptime = epoch_file[:, 3]
    evt_file = np.loadtxt(path + '{0}.txt'.format(src_index))
    bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index))
    for i in range(len(ID)):
        index = len(np.where(evt_file[:,2] == ID[i])[0])
        index_b=len(np.where(bkgevt_file[:,2] == ID[i])[0])
        cts_num.append(index-index_b/12.)
    dt = 100
    T_exp = 11000154.981141508
    freq=np.arange(1/T_exp,0.5/dt,1/(5*T_exp))
    freq=freq[np.where(freq > 1 / 20000.)]
    if os.path.exists(path+'/simulation/{0}_LS_simP_fixpds.csv'.format(src_index)):
        print('caution! file exists')
        return None
    with open(path + '/simulation/{0}_LS_simP_fixpds.csv'.format(src_index), 'a+') as csvfile:
        header = freq
        header = header.astype('str')
        writer = csv.writer(csvfile)
        for i in range(len(exptime)):
            cts_rate = cts_num[i] / (2 * exptime[i]) * dt  # 实际的cts-rate应为这个的2倍
            num_bins = int(exptime[i] / dt)
            sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt)
            w = np.arange(1 / exptime[i], 0.5 / dt, 1 / exptime[i])
            spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4])
            # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w, [1.0518215e-3,1.0518215e-3/16,200,2])
            lc = sim.simulate(spectrum)
            lc.counts += cts_rate
            lc.counts[np.where(lc.counts < 0)] = 0
            lc.time+=tstart[i]
            if i==0: lc_all=lc
            else: lc_all=lc_all.join(lc)
        print('run')
        print(lc_all.time)
        while k_trial<num_trials:
            ev_all = EventList()
            ev_all.time = sim_evtlist(lc_all) + tstart[0]
            # ev_all = ev_all.join(ev)
            lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0] - 0.5 * dt, tseg=ev_all.time[-1] - ev_all.time[0])
            temp = get_LS(lc_new.time, lc_new.counts, freq=freq, trial=k_trial)
            writer.writerows([temp[-1]])
            k_trial+=1
コード例 #10
0
ファイル: stingray_sim.py プロジェクト: twiight/pulsar_2
def get_lc_byspec_1hr(k,j,num_trials=1000):
    k_trial=0
    FP = [];
    period = [];
    while k_trial <num_trials:
        path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k)
        epoch_file=np.loadtxt(path+'CDFS_epoch_ep{0}.txt'.format(k))
        # path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8/'
        # epoch_file = np.loadtxt(path + 'CDFS_epoch.txt')
        tstart=epoch_file[:,0];tstop=epoch_file[:,1];exptime=epoch_file[:,3]
        ev_all = EventList()
        for i in range(len(exptime)):
            dt=100
            cts_rate=cr[j] *dt  #实际的cts-rate应为这个的2倍
            num_bins=int(exptime[i]/dt)
            sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt)
            w = np.arange(1 / exptime[i], 0.5 / dt, 1 / exptime[i])
            # w = np.fft.rfftfreq(sim.N, d=sim.dt)[1:]
            # spectrum = smoothbknpo(w, [0.01, 2, 1e-2, 1e-3])
            # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) ## 这是RE J1034+396 的参数
            # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w, [6.68e-4 ,6.68e-4 /16,200,2])
            spectrum = bending_po(w, [2.3e-3, 3.4, 0., 4.3e-4]) + generalized_lorentzian(w,[5.6e-4, 5.6e-4 / 16, 200,2])
            # spectrum =powerlaw + generalized_lorentzian(w, [1 / 1000., 1 / 10000., 0.5*np.max(powerlaw), 2])
            lc = sim.simulate(spectrum)
            lc.counts += cts_rate
            lc.counts[np.where(lc.counts<0)]=0
            ps=Powerspectrum(lc,norm='abs')
            ev = EventList()
            # ev.simulate_times(use_spline=False,lc=lc,bin_time=dt)
            # ev.time+=tstart[i]
            ev.time=sim_evtlist(lc)+tstart[i]
            # print(ev.time)RX J1301.9+2747
            ev_all=ev_all.join(ev)
        print('cts={0}'.format(len(ev_all.time)))
        lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0]-0.5*dt, tseg=ev_all.time[-1]-ev_all.time[0])
        T_exp=lc_new.time[-1]-lc_new.time[0]
        freq = np.arange(1 / T_exp, 0.5 / dt, 1 / (5 * T_exp))
        freq=freq[np.where(freq > 1 / 20000.)]
        # print(len(freq))
        temp=get_LS(lc_new.time, lc_new.counts, freq=freq)
        FP.append(temp[0]);period.append(temp[1])
        k_trial+=1
    result=np.column_stack((FP,period))
    np.savetxt(path+'simulation/'+'trial_out_1hr_{0}_REJ1034+396_test_noC.txt'.format(cr_str[j]),result,fmt="%10.5f %10.5f")
    return ev_all
コード例 #11
0
ファイル: stingray_sim.py プロジェクト: twiight/pulsar_2
def get_lc_byspec_single():
    ## stingray做simulation的时候powerspectrum的normlization的对lc似乎毫无影响;反正模拟的结果是这么显示的``````
    exptime=1e6
    dt = 10
    cts_rate = 1e-4* dt  # 实际得到的lc中的cts-rate应为这个的2倍,换言之这里应该是你要的cts-rate除以2
    num_bins = int(exptime / dt)
    sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt)
    w = np.arange(1 / exptime, 0.5 / dt, 1 / exptime)
    # w = np.fft.rfftfreq(sim.N, d=sim.dt)[1:]
    spectrum=generalized_lorentzian(w,[1/500.,1/4000.,100,2])
    # spectrum = smoothbknpo(w, [cts_rate * dt * 0.1, 3, 0, 1e-3])
    # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) ## 这是RE J1034+396 的参数
    spectrum= bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4])+generalized_lorentzian(w,[2.7e-4,2.7e-4/16,200,2])
    # spectrum = smoothbknpo(w, [cts_rate*dt*1, 2, 0, 1e-3])  + generalized_lorentzian(w, [1 / 1000., 1 / 10000., cts_rate*dt*100, 2])
    print(np.mean(spectrum))
    lc = sim.simulate(spectrum)
    lc.counts += cts_rate
    lc.counts[np.where(lc.counts < 0)] = 0
    print(np.mean(lc.counts))
    print(np.sum(lc.counts))
    print(np.var(lc.counts))
    plt.plot(lc.time,lc.counts)
    plt.show()
    ps = Powerspectrum(lc, norm='abs')
    fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
    ax1.plot(ps.freq, ps.power, lw=2, color='blue')
    ax1.set_ylabel("Frequency (Hz)", fontproperties=font_prop)
    ax1.set_ylabel("Power (raw)", fontproperties=font_prop)
    # ax1.set_yscale('log')
    ax1.tick_params(axis='x', labelsize=16)
    ax1.tick_params(axis='y', labelsize=16)
    ax1.tick_params(which='major', width=1.5, length=7)
    ax1.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax1.spines[axis].set_linewidth(1.5)
    plt.loglog()
    plt.show()

    T_exp =  exptime
    freq = np.arange(1 / T_exp, 0.5 / dt, 1 / T_exp)
    freq = freq[np.where(freq > 1 / 20000.)]
コード例 #12
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
    def test_compare_powerlaw(self):
        """
        Compare simulated power spectrum with actual one.
        """
        B, N, red_noise, dt = 2, 1024, 10, 1

        self.simulator = simulator.Simulator(N=N,
                                             dt=dt,
                                             mean=5,
                                             rms=1,
                                             red_noise=red_noise)
        lc = [self.simulator.simulate(B) for i in range(1, 30)]
        simulated = self.simulator.powerspectrum(lc, lc[0].tseg)

        w = np.fft.rfftfreq(N, d=dt)[1:]
        actual = np.power((1 / w), B / 2)[:-1]

        actual_prob = actual / float(sum(actual))
        simulated_prob = simulated / float(sum(simulated))

        assert np.all(
            np.abs(actual_prob - simulated_prob) < 3 * np.sqrt(actual_prob))
コード例 #13
0
ファイル: GL_sim_QPO.py プロジェクト: twiight/pulsar_2

if __name__ == '__main__':
    path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/'
    epoch_file = np.loadtxt(path + 'epoch_src_89.txt')
    cts_rate = 1e-3
    dt = 100

    tstart = epoch_file[:, 0]
    tstop = epoch_file[:, 1]
    ID = epoch_file[:, 2]
    exptime = epoch_file[:, 3]
    T_tot = tstop[-1] - tstart[0]
    cts_rate = cts_rate / 2 * dt  ##这里cts_rate单位都是per bin
    num_bins = int(T_tot / dt)
    sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt)
    w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot)
    spectrum = bending_po(
        w, [2.3e-4, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(
            w, [4.01e-4, 4.01e-4 / 16, 200, 2])
    lc = sim.simulate(spectrum)
    lc.time = lc.time + tstart[0]
    lc.counts += cts_rate
    lc.counts[np.where(lc.counts < 0)] = 0
    lc.gti = [[lc.time[0], lc.time[-1]]]
    # plt.plot(w,spectrum)
    k_trials = 1
    for i in range(k_trials):
        [time, lc_src] = make_evt_nobkg(cts_rate=1e-3,
                                        dt=3.2,
                                        epoch_file=epoch_file,
コード例 #14
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
 def test_io(self):
     sim = simulator.Simulator(N=1024)
     sim.write('sim.pickle')
     sim = sim.read('sim.pickle')
     assert sim.N == 1024
     os.remove('sim.pickle')
コード例 #15
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
 def test_simulate_with_random_state(self):
     self.simulator = simulator.Simulator(
         N=1024, random_state=np.random.RandomState(12))
コード例 #16
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
 def test_simulate_with_seed(self):
     """
     Simulate with a random seed value.
     """
     self.simulator = simulator.Simulator(N=1024, random_state=12)
     assert len(self.simulator.simulate(2).counts), 1024
コード例 #17
0
def slow_sim_fixpds_qpo_XMM(src_index, num_trials=2):
    k_trial = 0
    dt = 100
    path = '/Users/baotong/Desktop/CDFS/xmm_CDFS/xmm_txt/'
    epoch_file = np.loadtxt(path + 'epoch6_xmmobs.txt')

    tstart = epoch_file[:, 0]
    tstop = epoch_file[:, 1]
    ID = epoch_file[:, 2]
    exptime = epoch_file[:, 3]

    # evt_file_origin =np.loadtxt(path + 'XID{0}_{1}_all_obs_10sec.txt'.format(source_id, det))
    # bkgevt_file_origin = np.loadtxt(path + 'bkg_XID{0}_{1}_all_obs_10sec.txt'.format(source_id, det))
    # (evt_file,bkgevt_file)=filter_obs(evt_file_origin,bkgevt_file_origin,ID)

    T_tot = tstop[-1] - tstart[0]
    # cts_rate = len(evt_file)/ (2 * np.sum(exptime)) * dt  # 实际的cts-rate应为这个的2倍
    cts_rate = 0.001 * 0.67 / 2 * dt  ##这里cts_rate单位都是per bin
    bkg_cts_rate = 0.001 * 0.33

    num_bins = int(T_tot / dt)
    freq = np.arange(1 / T_tot, 0.5 / dt, 1 / (5 * T_tot))
    freq = freq[np.where(freq > 1 / 20000.)]

    sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt)
    w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot)
    spectrum = bending_po(
        w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(
            w, [4.01e-4, 4.01e-4 / 16, 200, 2])
    # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4])

    lc = sim.simulate(spectrum)
    lc.time = lc.time + tstart[0]
    # lc.gti=[[lc.time[0],lc.time[-1]]]
    lc.counts += cts_rate
    lc.counts[np.where(lc.counts < 0)] = 0
    with open(path + 'simulation/ep6_1e-3_SN3_LSP_qpo.txt', 'a+') as f:
        while k_trial < num_trials:
            ev_all = EventList()
            ev_all_bkg = EventList()
            for i in range(len(ID)):

                lc_cut = lc.truncate(start=tstart[i],
                                     stop=tstop[i],
                                     method='time')
                ev = EventList()
                ev.time = sim_evtlist(lc_cut)
                ev_all = ev_all.join(ev)
                index_b = (tstop[i] - tstart[i]) * bkg_cts_rate
                temp = index_b
                cts_num = np.random.poisson(temp)
                ev_bkg = EventList()
                ev_bkg.time = np.random.uniform(tstart[i] - dt / 2,
                                                tstop[i] + dt / 2, cts_num)
                ev_all_bkg = ev_all_bkg.join(ev_bkg)

            lc_src = ev_all.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot)
            lc_bkg = ev_all_bkg.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot)
            print(np.sum(lc_src))
            print(np.sum(lc_bkg))
            # T_exp=lc_new.time[-1]-lc_new.time[0]
            # lc_new=lc_src-lc_bkg
            lc_new = lc_src + lc_bkg

            temp = get_LS(lc_new.time, lc_new.counts, freq=freq)
            f.writelines(
                (str(temp[0]) + '        ' + str(temp[1]) + '        ' +
                 str(temp[2]) + '        ' + str(temp[3]) + '\n'))
            k_trial += 1
    f.close()
コード例 #18
0
def slow_sim_fixpds_qpo_chandra(src_index, num_trials=2, dt=100):
    k_trial = 0
    path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep3/'
    epoch_file = np.loadtxt(path + 'epoch_src_{0}.txt'.format(src_index))
    # epoch_file=epoch_file[23:]

    tstart = epoch_file[:, 0]
    tstop = epoch_file[:, 1]
    ID = epoch_file[:, 2]
    exptime = epoch_file[:, 3]
    evt_file_origin = np.loadtxt(path + '{0}.txt'.format(src_index))
    bkgevt_file_origin = np.loadtxt(path + '{0}_bkg.txt'.format(src_index))
    # (evt_file,bkgevt_file)=filter_obs(evt_file_origin,bkgevt_file_origin,ID)
    evt_file = evt_file_origin
    bkgevt_file = bkgevt_file_origin
    T_tot = tstop[-1] - tstart[0]
    cts_rate = len(evt_file) / (2 * np.sum(exptime)) * dt  # 实际的cts-rate应为这个的2倍
    bkg_rate = len(bkgevt_file_origin) / (2 * np.sum(exptime)) * dt
    num_bins = int(T_tot / dt)
    freq = np.arange(1 / T_tot, 0.5 / dt, 1 / (5 * T_tot))
    freq = freq[np.where(freq > 1 / 20000.)]

    sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt)
    w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot)
    spectrum = bending_po(
        w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(
            w, [4.01e-4, 4.01e-4 / 16, 200, 2])
    # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4])
    lc = sim.simulate(spectrum)
    # lc=sim.simulate(pow)
    lc.time = lc.time + tstart[0]
    lc.gti = [[lc.time[0], lc.time[-1]]]
    lc.counts += cts_rate
    lc.counts[np.where(lc.counts < 0)] = 0
    ps = Powerspectrum(lc, norm='frac')
    fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
    ax1.loglog()
    ax1.step(ps.freq, ps.power, lw=2, color='blue')
    ax1.plot([1 / 950.7, 1 / 950.7], [0, np.max(ps.power)], '--', linewidth=1)
    ax1.set_ylabel("Frequency (Hz)", fontproperties=font1)
    ax1.set_ylabel("Power (raw)", fontproperties=font1)
    ax1.set_yscale('log')
    ax1.tick_params(axis='x', labelsize=16)
    ax1.tick_params(axis='y', labelsize=16)
    ax1.tick_params(which='major', width=1.5, length=7)
    ax1.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax1.spines[axis].set_linewidth(1.5)
    plt.show()

    with open(path + 'simulation/{0}_LSP_FP_qpo_nofix.txt'.format(src_index),
              'a+') as f:
        while k_trial < num_trials:

            ev_all = EventList()
            ev_all_bkg = EventList()
            for i in range(len(ID)):

                lc_cut = lc.truncate(start=tstart[i],
                                     stop=tstop[i],
                                     method='time')
                ev = EventList()
                ev.time = sim_evtlist(lc_cut)
                ev_all = ev_all.join(ev)
                index_b = len(np.where(bkgevt_file[:, 2] == ID[i])[0])
                temp = index_b / 12.
                cts_num = np.random.poisson(temp)
                ev_bkg = EventList()
                ev_bkg.time = np.random.uniform(tstart[i] - dt / 2,
                                                tstop[i] + dt / 2, cts_num)
                ev_all_bkg = ev_all_bkg.join(ev_bkg)

            lc_src = ev_all.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot)
            lc_bkg = ev_all_bkg.to_lc(dt=dt, tstart=tstart[0], tseg=T_tot)
            print(np.sum(lc_src))
            print(np.sum(lc_bkg))
            # T_exp=lc_new.time[-1]-lc_new.time[0]
            lc_new = lc_src
            lc_new.counts -= lc_bkg.counts

            temp = get_LS(lc_new.time, lc_new.counts, freq=freq)
            # f.writelines((str(temp[0]) + '        ' + str(temp[1]) +'            '+ str(temp[2])+'             '+ str(temp[3])+'\n'))
            k_trial += 1
    f.close()
コード例 #19
0
ファイル: test_simulator.py プロジェクト: wafels/stingray
 def setup_class(self):
     self.simulator = simulator.Simulator(N=1024, mean=0.5, dt=0.125)
コード例 #20
0
def slow_sim_fixpds(k, src_index, num_trials=2):
    k_trial = 0
    dt = 100
    FP = []
    period = []
    cts_num = []
    peakP = []
    power_P = []
    path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k)
    epoch_file = np.loadtxt(path + 'CDFS_epoch_ep{0}.txt'.format(k))
    tstart = epoch_file[:, 0]
    tstop = epoch_file[:, 1]
    ID = epoch_file[:, 2]
    exptime = epoch_file[:, 3]
    evt_file = np.loadtxt(path + '{0}.txt'.format(src_index))
    bkgevt_file = np.loadtxt(path + '{0}_bkg.txt'.format(src_index))

    T_tot = tstop[-1] - tstart[0]
    cts_rate = len(evt_file) / (2 * np.sum(exptime)) * dt  # 实际的cts-rate应为这个的2倍
    num_bins = int(T_tot / dt)

    sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt)
    w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot)
    spectrum = bending_po(
        w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(
            w, [2.7e-4, 2.7e-4 / 16, 200, 2])
    lc = sim.simulate(spectrum)
    lc.time = lc.time + tstart[0]
    # lc.gti=[[lc.time[0],lc.time[-1]]]
    lc.counts += cts_rate
    lc.counts[np.where(lc.counts < 0)] = 0

    ps = Powerspectrum(lc, norm='leahy')
    fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
    ax1.loglog()
    ax1.step(ps.freq, ps.power, lw=2, color='blue')
    ax1.plot([1 / 950.7, 1 / 950.7], [0, np.max(ps.power)], '--', linewidth=1)
    ax1.set_ylabel("Frequency (Hz)", fontproperties=font1)
    ax1.set_ylabel("Power (raw)", fontproperties=font1)
    ax1.set_yscale('log')
    ax1.tick_params(axis='x', labelsize=16)
    ax1.tick_params(axis='y', labelsize=16)
    ax1.tick_params(which='major', width=1.5, length=7)
    ax1.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax1.spines[axis].set_linewidth(1.5)
    plt.show()
    T_exp = T_tot
    freq = np.arange(1 / T_exp, 0.5 / dt, 1 / (5 * T_exp))
    freq = freq[np.where(freq > 1 / 20000.)]
    with open(path + '/simulation/{0}_max_LSP_fixpds.txt'.format(src_index),
              'a+') as f:
        res_period = []
        res_power = []
        while k_trial < num_trials:
            ev_all = EventList()
            for i in range(len(exptime)):
                lc_cut = lc.truncate(start=tstart[i],
                                     stop=tstop[i],
                                     method='time')
                ev = EventList()
                ev.time = sim_evtlist(lc_cut)
                ev_all = ev_all.join(ev)

            lc_new = ev_all.to_lc(dt=dt,
                                  tstart=ev_all.time[0] - 0.5 * dt,
                                  tseg=ev_all.time[-1] - ev_all.time[0])

            temp = get_LS(lc_new.time, lc_new.counts, freq=freq, trial=k_trial)
            # res_period.append(temp[0]);res_power.append(temp[-1])
            f.writelines((str(temp[0]) + '        ' + str(temp[1]) + '\n'))
            k_trial += 1
    f.close()
コード例 #21
0
ファイル: GL_sim_QPO.py プロジェクト: twiight/pulsar_2
def make_evt_nobkg(cts_rate, dt, epoch_file, lc_bin=100, fixpds=True, lc=0):
    tstart = epoch_file[:, 0]
    tstop = epoch_file[:, 1]
    ID = epoch_file[:, 2]
    exptime = epoch_file[:, 3]
    T_tot = tstop[-1] - tstart[0]
    cts_rate = cts_rate / 2 * dt  ##这里cts_rate单位都是per bin
    num_bins = int(T_tot / dt)
    freq = np.arange(1 / T_tot, 0.5 / dt, 1 / (5 * T_tot))
    freq = freq[np.where(freq > 1 / 20000.)]
    if fixpds:
        lc = lc
    else:
        print('not fix!')
        sim = simulator.Simulator(N=num_bins + 1, mean=cts_rate, dt=dt)
        w = np.arange(1 / T_tot, 0.5 / dt, 1 / T_tot)
        spectrum = bending_po(
            w, [2.3e-4, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(
                w, [4.01e-4, 4.01e-4 / 16, 0.1, 2])
        lc = sim.simulate(spectrum)
        lc.time = lc.time + tstart[0]
        lc.counts += cts_rate
        lc.counts[np.where(lc.counts < 0)] = 0
        lc.gti = [[lc.time[0], lc.time[-1]]]
    plt.plot(w, spectrum)
    plt.loglog()
    plt.show()
    plt.plot(lc.time, lc.counts)
    plt.show()
    ps = Powerspectrum(lc, norm='abs')
    fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
    ax1.plot(ps.freq, ps.power, lw=2, color='blue')
    ax1.set_ylabel("Frequency (Hz)", fontproperties=font1)
    ax1.set_ylabel("Power (raw)", fontproperties=font1)
    # ax1.set_yscale('log')
    ax1.tick_params(axis='x', labelsize=16)
    ax1.tick_params(axis='y', labelsize=16)
    ax1.tick_params(which='major', width=1.5, length=7)
    ax1.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax1.spines[axis].set_linewidth(1.5)
    plt.loglog()
    plt.show()

    ev_all = EventList()
    ev_all_bkg = EventList()
    for i in range(len(ID)):
        lc_cut = lc.truncate(start=tstart[i], stop=tstop[i], method='time')
        ev = EventList()
        ev.time = sim_evtlist(lc_cut)
        ev_all = ev_all.join(ev)

    lc_src = ev_all.to_lc(dt=lc_bin, tstart=tstart[0], tseg=T_tot)
    ps = Powerspectrum(lc_src, norm='abs')
    fig, ax2 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
    ax2.plot(ps.freq, ps.power, lw=2, color='blue')
    ax2.set_ylabel("Frequency (Hz)", fontproperties=font1)
    ax2.set_ylabel("Power (raw)", fontproperties=font1)
    # ax1.set_yscale('log')
    ax2.tick_params(axis='x', labelsize=16)
    ax2.tick_params(axis='y', labelsize=16)
    ax2.tick_params(which='major', width=1.5, length=7)
    ax2.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax2.spines[axis].set_linewidth(1.5)
    plt.loglog()
    plt.show()

    return [ev_all.time, lc_src]
コード例 #22
0
ファイル: stingray_sim.py プロジェクト: twiight/pulsar_2
def get_lc_byspec(k,j,num_trials=1000):
    k_trial=0
    FP = [];
    period = [];
    while k_trial <num_trials:
        path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8_ep{0}/'.format(k)
        epoch_file=np.loadtxt(path+'CDFS_epoch_ep{0}.txt'.format(k))
        # path = '/Users/baotong/Desktop/CDFS/txt_all_obs_0.5_8/'
        # epoch_file = np.loadtxt(path + 'CDFS_epoch.txt')
        tstart=epoch_file[:,0];tstop=epoch_file[:,1];exptime=epoch_file[:,3]
        ev_all = EventList()
        for i in range(len(exptime)):
            dt=100
            cts_rate=cr[j] *dt  #实际的cts-rate应为这个的2倍
            num_bins=int(exptime[i]/dt)
            sim = simulator.Simulator(N=num_bins, mean=cts_rate, dt=dt)
            w = np.arange(1 / exptime[i], 0.5 / dt, 1 / exptime[i])
            # w = np.fft.rfftfreq(sim.N, d=sim.dt)[1:]
            # spectrum = smoothbknpo(w, [0.01, 2, 1e-2, 1e-3])
            spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) ## 这是RE J1034+396 的参数
            # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w, [6.68e-4 ,6.68e-4 /16,200,2])
            # spectrum = bending_po(w, [2.3e-3, 3.4, 0.40, 4.3e-4]) + generalized_lorentzian(w,[2.7e-4, 2.7e-4 / 16, 200,2])
            # spectrum =powerlaw + generalized_lorentzian(w, [1 / 1000., 1 / 10000., 0.5*np.max(powerlaw), 2])
            lc = sim.simulate(spectrum)
            lc.counts += cts_rate
            lc.counts[np.where(lc.counts<0)]=0
            # plt.plot(w,spectrum)
            # plt.loglog()
            # plt.xlabel("Frequency (Hz)", fontproperties=font_prop)
            # plt.ylabel("Power (abs)", fontproperties=font_prop)
            # plt.show()
            ps=Powerspectrum(lc,norm='abs')
            # fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
            # ax1.plot(ps.freq, ps.power, lw=2, color='blue')
            # ax1.set_ylabel("Frequency (Hz)", fontproperties=font_prop)
            # ax1.set_ylabel("Power (raw)", fontproperties=font_prop)
            # # ax1.set_yscale('log')
            # ax1.tick_params(axis='x', labelsize=16)
            # ax1.tick_params(axis='y', labelsize=16)
            # ax1.tick_params(which='major', width=1.5, length=7)
            # ax1.tick_params(which='minor', width=1.5, length=4)
            # for axis in ['top', 'bottom', 'left', 'right']:
            #     ax1.spines[axis].set_linewidth(1.5)
            # plt.loglog()
            # plt.show()
            ev = EventList()
            # ev.simulate_times(use_spline=False,lc=lc,bin_time=dt)
            # ev.time+=tstart[i]
            ev.time=sim_evtlist(lc)+tstart[i]
            # print(ev.time)RX J1301.9+2747
            ev_all=ev_all.join(ev)
            # lc_temp = ev.to_lc(dt=dt, tstart=ev.time[0]-0.5*dt, tseg=ev.time[-1]-ev.time[0])
            # ps_temp=Powerspectrum(lc_temp,norm='abs')
            # fig2, ax2 = plt.subplots(1, 1, figsize=(9, 6), sharex=True)
            # ax2.plot(ps_temp.freq, ps_temp.power, lw=2, color='blue')
            # ax2.set_ylabel("Frequency (Hz)", fontproperties=font_prop)
            # ax2.set_ylabel("Power (raw)", fontproperties=font_prop)
            # # ax1.set_yscale('log')
            # ax2.tick_params(axis='x', labelsize=16)
            # ax2.tick_params(axis='y', labelsize=16)
            # ax2.tick_params(which='major', width=1.5, length=7)
            # ax2.tick_params(which='minor', width=1.5, length=4)
            # for axis in ['top', 'bottom', 'left', 'right']:
            #     ax2.spines[axis].set_linewidth(1.5)
            # plt.loglog()
            # plt.show()
            # 
            # plt.figure(2)
            # plt.plot(lc.time,lc.counts,c='green')
            # plt.plot(lc_temp.time,lc_temp.counts,c='r')
            # plt.show()
            # plt.figure(3)
            # get_LS(lc.time,lc.counts,w)
        # print('cts={0}'.format(len(ev_all.time)))
        lc_new = ev_all.to_lc(dt=dt, tstart=ev_all.time[0]-0.5*dt, tseg=ev_all.time[-1]-ev_all.time[0])
        # plt.figure(2)
        # plt.plot(lc_new.time,lc_new.counts,c='r')
        # plt.show()
        # plt.figure(3)
        T_exp=lc_new.time[-1]-lc_new.time[0]
        freq=np.arange(1/T_exp,0.5/dt,1/(5*T_exp))
        freq=freq[np.where(freq > 1 / 10000.)]
        # print(len(freq))
        temp=get_LS(lc_new.time, lc_new.counts, freq=freq)
        FP.append(temp[0]);period.append(temp[1])
        k_trial+=1
    result=np.column_stack((FP,period))
    np.savetxt(path+'simulation/'+'trial_out_{0}_REJ1034+396_noQPO.txt'.format(cr_str[j]),result,fmt="%10.5f %10.5f")
    return ev_all