Lv2_average_ps_methods.py
def plotting(eventfile,segment_length,demod,tbin,threshold,PI1,PI2,t1,t2,starting_freq,W,hist_min_sig,N,xlims,plot_mode):
    """
    Plotting the averaged power spectrum and the noise histogram

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    demod - whether we're dealing with demodulated data or not!
    tbin_size - size of the time bin
    threshold - if data is under threshold (in percentage), then don't use the segment!
    PI1 - lower bound of PI (not energy in keV!) desired for the energy range
    PI2 - upper bound of PI (not energy in keV!) desired for the energy range
    t1 - starting time for calculation of averaged power spectra
    t2 - ending time for calculation of averaged power spectra
    (note that t=0 corresponds to the MET of the FIRST event in the eventfile, so will need to inspect light curve with Lv2_lc.py to get times)
    starting_freq - frequency to start constructing the histogram of powers from
    W - number of consecutive frequency bins to AVERAGE over
    hist_min_sig - minimum significance for a candidate frequency to be added to a text file; will be used to calculate histograms of candidates
    N - number of trials
    xlims - limits to apply on the x axis if desired
    plot_mode - whether to "show" the plots or to "save" them
    """
    if demod != True and demod != False:
        raise ValueError("demod should either be True or False!")
    if plot_mode != "show" and plot_mode != "save":
        raise ValueError("plot_mode should either be 'show' or 'save'!")

    parent_folder = str(pathlib.Path(eventfile).parent)

    f,ps,ps_bins,N_greaterthanP,M = average_ps(eventfile,segment_length,demod,tbin,threshold,PI1,PI2,t1,t2,starting_freq,W)

    power_required_3 = Lv3_detection_level.power_for_sigma(3,N,M,W) #power required for significance
    power_required_4 = Lv3_detection_level.power_for_sigma(4,N,M,W) #power required for significance

    ### to create the histogram of pulsation candidates
    ps_sig = Lv3_detection_level.signal_significance(N,M,W,ps)

    if PI1 == '':
        output_file = open(parent_folder + '/S' + str(segment_length) + '_W' + str(W) + '_T' + str(threshold) + '_t1t2_' + str(t1) + '-' + str(t2) + '.txt','w')
    else:
        output_file = open(parent_folder + '/S' + str(segment_length) + '_W' + str(W) + '_T' + str(threshold) + '_E' + str(PI1) + '-' + str(PI2) + '_t1t2_' + str(t1) + '-' + str(t2) + '.txt','w')
    cand_f = f[ps_sig>=hist_min_sig] #decided not to use hist_min_f ; otherwise I get empty files...
    cand_ps = ps_sig[ps_sig>=hist_min_sig]
    for i in range(len(cand_f)):
        output_file.write(str(cand_f[i]) + ' ' + str(cand_ps[i]) + '\n')
    output_file.close()

    plt.figure(num=1,figsize=(10,5.63))
    plt.errorbar(x=f,y=ps,color='r',drawstyle='steps-mid')
    plt.axhline(y=power_required_3,lw=0.8,alpha=0.5,color='b')
    plt.axhline(y=power_required_4,lw=0.8,alpha=0.5,color='k')
    plt.axhline(y=2,lw=0.8,alpha=0.5,color='k',linestyle='--')
    plt.xlabel('Frequency (Hz)',fontsize=12)
    plt.ylabel('Leahy-normalized power',fontsize=12)
    plt.xscale('log')
    plt.yscale('log')
    plt.ylim([1,min(20.0,3*power_required_4)])
    plt.xlim([0.001,1/(2*tbin)])
    if len(xlims) != 0:
        plt.xlim([xlims[0],xlims[1]])
    #plt.axvline(x=271.453,lw=0.5,alpha=0.5)
    plt.title('PI: ' + str(PI1)+'-'+str(PI2) + '; W = ' + str(W) + ', Threshold = ' + str(threshold) + '%' + '\n' + 't1 = ' + str(t1) + ', t2 = ' + str(t2) + ' ; Segment Length: ' + str(segment_length) + 's, No. Segments = ' + str(M) + '\n' + 'Demodulated: ' + str(demod) + ' ; St.D = ' + str(np.std(ps)), fontsize=12)
    plt.legend(('Power Spectrum','3 sigma','4 sigma','Poisson noise'),loc='best')
    if plot_mode == "save":
        if PI1 != '':
            energy_suffix = '_E' + str(PI1).zfill(4) + '-' + str(PI2).zfill(4)
        else:
            energy_suffix = ''
        if demod == True:
            demod_suffix = '_demod'
        else:
            demod_suffix = ''
        plt.savefig(parent_folder + '/' + str(segment_length) + 's_average_ps_W' + str(W) + '_T' + str(threshold) + demod_suffix + energy_suffix + '_t1t2_' + str(t1) + '-' + str(t2) + '.pdf',dpi=900)
        plt.close()

    plt.figure(2)
    plt.semilogy(ps_bins,N_greaterthanP,'rx')
    plt.xlabel('Leahy-normalized power',fontsize=12)
    plt.ylabel('log[N(>P)]',fontsize=12)
    plt.title('Energy range: ' + str(PI1) + ' - ' + str(PI2) + ', W = ' + str(W),fontsize=12)
    if plot_mode == "save":
        if PI1 != '':
            energy_suffix = '_E' + str(PI1).zfill(4) + '-' + str(PI2).zfill(4)
        else:
            energy_suffix = ''
        if demod == True:
            demod_suffix = '_demod'
        else:
            demod_suffix = ''
            plt.savefig(parent_folder + '/' + str(segment_length) + 's_noise_hist_W' + str(W) + '_T' + str(threshold) + demod_suffix + energy_suffix + '_t1t2_' + str(t1) + '-' + str(t2) + '.pdf',dpi=900)
        plt.close()

    if plot_mode == "show":
        plt.show()

    Plotting the averaged power spectrum and the noise histogram

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    demod - whether we're dealing with demodulated data or not!
    tbin_size - size of the time bin
    threshold - if data is under threshold (in percentage), then don't use the segment!
    PI1 - lower bound of PI (not energy in keV!) desired for the energy range
    PI2 - upper bound of PI (not energy in keV!) desired for the energy range
    t1 - starting time for calculation of averaged power spectra
    t2 - ending time for calculation of averaged power spectra
    (note that t=0 corresponds to the MET of the FIRST event in the eventfile, so will need to inspect light curve with Lv2_lc.py to get times)
    starting_freq - frequency to start constructing the histogram of powers from
    W - number of consecutive frequency bins to AVERAGE over
    hist_min_sig - minimum significance for a candidate frequency to be added to a text file; will be used to calculate histograms of candidates
    N - number of trials
    xlims - limits to apply on the x axis if desired
    plot_mode - whether to "show" the plots or to "save" them
    
Plotting the averaged power spectrum and the noise histogram
eventfile - path to the event file. Will extract ObsID from this for the NICER files.
segment_length - length of the segments
demod - whether we're dealing with demodulated data or not.
tbin_size - size of the time bin
threshold - if data is under threshold (in percentage), then don't use the segment.
Plotting the averaged power spectrum and the noise histogram eventfile - path to the event file . Will extract ObsID from this for the NICER files . segment_length - length of the segments demod - whether we ' re dealing with demodulated data or not . tbin_size - size of the time bin threshold - if data is under threshold ( in percentage ) then don ' t use the segment .
def plotting(eventfile,segment_length,demod,tbin,threshold,PI1,PI2,t1,t2,starting_freq,W,hist_min_sig,N,xlims,plot_mode):
    if demod != True and demod != False:
        raise ValueError("demod should either be True or False!")
    if plot_mode != "show" and plot_mode != "save":
        raise ValueError("plot_mode should either be 'show' or 'save'!")
    parent_folder = str(pathlib.Path(eventfile).parent)
    f,ps,ps_bins,N_greaterthanP,M = average_ps(eventfile,segment_length,demod,tbin,threshold,PI1,PI2,t1,t2,starting_freq,W)
    power_required_3 = Lv3_detection_level.power_for_sigma(3,N,M,W) 
    power_required_4 = Lv3_detection_level.power_for_sigma(4,N,M,W) 
    ps_sig = Lv3_detection_level.signal_significance(N,M,W,ps)
    if PI1 == '':
        output_file = open(parent_folder + '/S' + str(segment_length) + '_W' + str(W) + '_T' + str(threshold) + '_t1t2_' + str(t1) + '-' + str(t2) + '.txt','w')
    else:
        output_file = open(parent_folder + '/S' + str(segment_length) + '_W' + str(W) + '_T' + str(threshold) + '_E' + str(PI1) + '-' + str(PI2) + '_t1t2_' + str(t1) + '-' + str(t2) + '.txt','w')
    cand_f = f[ps_sig>=hist_min_sig] 
    cand_ps = ps_sig[ps_sig>=hist_min_sig]
    for i in range(len(cand_f)):
        output_file.write(str(cand_f[i]) + ' ' + str(cand_ps[i]) + '\n')
    output_file.close()
    plt.figure(num=1,figsize=(10,5.63))
    plt.errorbar(x=f,y=ps,color='r',drawstyle='steps-mid')
    plt.axhline(y=power_required_3,lw=0.8,alpha=0.5,color='b')
    plt.axhline(y=power_required_4,lw=0.8,alpha=0.5,color='k')
    plt.axhline(y=2,lw=0.8,alpha=0.5,color='k',linestyle='--')
    plt.xlabel('Frequency (Hz)',fontsize=12)
    plt.ylabel('Leahy-normalized power',fontsize=12)
    plt.xscale('log')
    plt.yscale('log')
    plt.ylim([1,min(20.0,3*power_required_4)])
    plt.xlim([0.001,1/(2*tbin)])
    if len(xlims) != 0:
        plt.xlim([xlims[0],xlims[1]])
    plt.title('PI: ' + str(PI1)+'-'+str(PI2) + '; W = ' + str(W) + ', Threshold = ' + str(threshold) + '%' + '\n' + 't1 = ' + str(t1) + ', t2 = ' + str(t2) + ' ; Segment Length: ' + str(segment_length) + 's, No. Segments = ' + str(M) + '\n' + 'Demodulated: ' + str(demod) + ' ; St.D = ' + str(np.std(ps)), fontsize=12)
    plt.legend(('Power Spectrum','3 sigma','4 sigma','Poisson noise'),loc='best')
    if plot_mode == "save":
        if PI1 != '':
            energy_suffix = '_E' + str(PI1).zfill(4) + '-' + str(PI2).zfill(4)
        else:
            energy_suffix = ''
        if demod == True:
            demod_suffix = '_demod'
        else:
            demod_suffix = ''
        plt.savefig(parent_folder + '/' + str(segment_length) + 's_average_ps_W' + str(W) + '_T' + str(threshold) + demod_suffix + energy_suffix + '_t1t2_' + str(t1) + '-' + str(t2) + '.pdf',dpi=900)
        plt.close()
    plt.figure(2)
    plt.semilogy(ps_bins,N_greaterthanP,'rx')
    plt.xlabel('Leahy-normalized power',fontsize=12)
    plt.ylabel('log[N(>P)]',fontsize=12)
    plt.title('Energy range: ' + str(PI1) + ' - ' + str(PI2) + ', W = ' + str(W),fontsize=12)
    if plot_mode == "save":
        if PI1 != '':
            energy_suffix = '_E' + str(PI1).zfill(4) + '-' + str(PI2).zfill(4)
        else:
            energy_suffix = ''
        if demod == True:
            demod_suffix = '_demod'
        else:
            demod_suffix = ''
            plt.savefig(parent_folder + '/' + str(segment_length) + 's_noise_hist_W' + str(W) + '_T' + str(threshold) + demod_suffix + energy_suffix + '_t1t2_' + str(t1) + '-' + str(t2) + '.pdf',dpi=900)
        plt.close()
    if plot_mode == "show":
        plt.show()
def plotting ( eventfile , segment_length , demod , tbin , threshold , PI1 , PI2 , t1 , t2 , starting_freq , W , hist_min_sig , N , xlims , plot_mode ) : if demod != True and demod != False : raise ValueError ( "demod should either be True or False!" ) if plot_mode != "show" and plot_mode != "save" : raise ValueError ( "plot_mode should either be 'show' or 'save'!" ) parent_folder = str ( pathlib . Path ( eventfile ) . parent ) f , ps , ps_bins , N_greaterthanP , M = average_ps ( eventfile , segment_length , demod , tbin , threshold , PI1 , PI2 , t1 , t2 , starting_freq , W ) power_required_3 = Lv3_detection_level . power_for_sigma ( 3 , N , M , W ) power_required_4 = Lv3_detection_level . power_for_sigma ( 4 , N , M , W ) ps_sig = Lv3_detection_level . signal_significance ( N , M , W , ps ) if PI1 == '' : output_file = open ( parent_folder + '/S' + str ( segment_length ) + '_W' + str ( W ) + '_T' + str ( threshold ) + '_t1t2_' + str ( t1 ) + '-' + str ( t2 ) + '.txt' , 'w' ) else : output_file = open ( parent_folder + '/S' + str ( segment_length ) + '_W' + str ( W ) + '_T' + str ( threshold ) + '_E' + str ( PI1 ) + '-' + str ( PI2 ) + '_t1t2_' + str ( t1 ) + '-' + str ( t2 ) + '.txt' , 'w' ) cand_f = f [ ps_sig >= hist_min_sig ] cand_ps = ps_sig [ ps_sig >= hist_min_sig ] for i in range ( len ( cand_f ) ) : output_file . write ( str ( cand_f [ i ] ) + ' ' + str ( cand_ps [ i ] ) + '\n' ) output_file . close ( ) plt . figure ( num = 1 , figsize = ( 10 , 5.63 ) ) plt . errorbar ( x = f , y = ps , color = 'r' , drawstyle = 'steps-mid' ) plt . axhline ( y = power_required_3 , lw = 0.8 , alpha = 0.5 , color = 'b' ) plt . axhline ( y = power_required_4 , lw = 0.8 , alpha = 0.5 , color = 'k' ) plt . axhline ( y = 2 , lw = 0.8 , alpha = 0.5 , color = 'k' , linestyle = '--' ) plt . xlabel ( 'Frequency (Hz)' , fontsize = 12 ) plt . ylabel ( 'Leahy-normalized power' , fontsize = 12 ) plt . xscale ( 'log' ) plt . yscale ( 'log' ) plt . ylim ( [ 1 , min ( 20.0 , 3 * power_required_4 ) ] ) plt . xlim ( [ 0.001 , 1 / ( 2 * tbin ) ] ) if len ( xlims ) != 0 : plt . xlim ( [ xlims [ 0 ] , xlims [ 1 ] ] ) plt . title ( 'PI: ' + str ( PI1 ) + '-' + str ( PI2 ) + '; W = ' + str ( W ) + ', Threshold = ' + str ( threshold ) + '%' + '\n' + 't1 = ' + str ( t1 ) + ', t2 = ' + str ( t2 ) + ' ; Segment Length: ' + str ( segment_length ) + 's, No. Segments = ' + str ( M ) + '\n' + 'Demodulated: ' + str ( demod ) + ' ; St.D = ' + str ( np . std ( ps ) ) , fontsize = 12 ) plt . legend ( ( 'Power Spectrum' , '3 sigma' , '4 sigma' , 'Poisson noise' ) , loc = 'best' ) if plot_mode == "save" : if PI1 != '' : energy_suffix = '_E' + str ( PI1 ) . zfill ( 4 ) + '-' + str ( PI2 ) . zfill ( 4 ) else : energy_suffix = '' if demod == True : demod_suffix = '_demod' else : demod_suffix = '' plt . savefig ( parent_folder + '/' + str ( segment_length ) + 's_average_ps_W' + str ( W ) + '_T' + str ( threshold ) + demod_suffix + energy_suffix + '_t1t2_' + str ( t1 ) + '-' + str ( t2 ) + '.pdf' , dpi = 900 ) plt . close ( ) plt . figure ( 2 ) plt . semilogy ( ps_bins , N_greaterthanP , 'rx' ) plt . xlabel ( 'Leahy-normalized power' , fontsize = 12 ) plt . ylabel ( 'log[N(>P)]' , fontsize = 12 ) plt . title ( 'Energy range: ' + str ( PI1 ) + ' - ' + str ( PI2 ) + ', W = ' + str ( W ) , fontsize = 12 ) if plot_mode == "save" : if PI1 != '' : energy_suffix = '_E' + str ( PI1 ) . zfill ( 4 ) + '-' + str ( PI2 ) . zfill ( 4 ) else : energy_suffix = '' if demod == True : demod_suffix = '_demod' else : demod_suffix = '' plt . savefig ( parent_folder + '/' + str ( segment_length ) + 's_noise_hist_W' + str ( W ) + '_T' + str ( threshold ) + demod_suffix + energy_suffix + '_t1t2_' + str ( t1 ) + '-' + str ( t2 ) + '.pdf' , dpi = 900 ) plt . close ( ) if plot_mode == "show" : plt . show ( )
Plotting the averaged power spectrum and the noise histogram
eventfile - path to the event file.
Plotting the averaged power spectrum and the noise histogram eventfile - path to the event file .
"""
    Plotting the averaged power spectrum and the noise histogram

    eventfile - path to the event file. Will extract ObsID from this for the NICER files.
    segment_length - length of the segments
    demod - whether we're dealing with demodulated data or not!
    tbin_size - size of the time bin
    threshold - if data is under threshold (in percentage), then don't use the segment!
    PI1 - lower bound of PI (not energy in keV!) desired for the energy range
    PI2 - upper bound of PI (not energy in keV!) desired for the energy range
    t1 - starting time for calculation of averaged power spectra
    t2 - ending time for calculation of averaged power spectra
    (note that t=0 corresponds to the MET of the FIRST event in the eventfile, so will need to inspect light curve with Lv2_lc.py to get times)
    starting_freq - frequency to start constructing the histogram of powers from
    W - number of consecutive frequency bins to AVERAGE over
    hist_min_sig - minimum significance for a candidate frequency to be added to a text file; will be used to calculate histograms of candidates
    N - number of trials
    xlims - limits to apply on the x axis if desired
    plot_mode - whether to "show" the plots or to "save" them
    """ #power required for significance #power required for significance ### to create the histogram of pulsation candidates #decided not to use hist_min_f ; otherwise I get empty files... #plt.axvline(x=271.453,lw=0.5,alpha=0.5)
eventfile
segment_length
demod
tbin
threshold
PI1
PI2
t1
t2
starting_freq
W
hist_min_sig
N
xlims
plot_mode