Lv3_swift_ngc300.py
def step_n_ramp(phase,prof,prof_err,start_phase,end_phase,pguess):
    """
    Fitting 6-model step-and-ramp parameters to the folded profile
    """
    phase_model = np.linspace(start_phase,end_phase,101)
    x = phase[(phase>=start_phase)&(phase<=end_phase)]
    y = prof[(phase>=start_phase)&(phase<=end_phase)]
    y_err = prof_err[(phase>=start_phase)&(phase<=end_phase)]

    def piecewise_linear(x,b1,b2,b3,b4,top,bottom):
        return np.piecewise(x, [(x>=start_phase)&(x<=b1), (x>b1)&(x<=b2), (x>b2)&(x<=b3), (x>b3)&(x<=b4), (x>b4)&(x<=end_phase)], [lambda x:top, lambda x:((bottom-top)/(b2-b1)*x+bottom-(bottom-top)/(b2-b1)*b2), lambda x:bottom, lambda x:((top-bottom)/(b4-b3)*x+top-(top-bottom)/(b4-b3)*b4), lambda x:top])

    plt.figure()
    popt,pcov = curve_fit(piecewise_linear,x,y,p0=pguess,sigma=y_err,absolute_sigma=True)
    pars = popt
    pars_err = np.diag(np.sqrt(pcov))
    print('Top: ' + str(pars[4]) + ' +- ' + str(pars_err[4]))
    print('Bottom: ' + str(pars[5]) + ' +- ' + str(pars_err[5]))
    print('Vertex 1: ' + str(pars[0]) + ' +- ' + str(pars_err[0]))
    print('Vertex 2: ' + str(pars[1]) + ' +- ' + str(pars_err[1]))
    print('Vertex 3: ' + str(pars[2]) + ' +- ' + str(pars_err[2]))
    print('Vertex 4: ' + str(pars[3]) + ' +- ' + str(pars_err[3]))
    plt.plot(phase_model,piecewise_linear(phase_model,*popt),'k-')

    ##### Plotting the folded profiles themselves
    plt.errorbar(x=phase,y=prof,yerr=prof_err,color='r',drawstyle='steps-mid')
    plt.title('Exposure-corrected (profiles from Stingray)',fontsize=12)
    plt.xlabel('Phase',fontsize=12)
    plt.ylabel('Counts/s',fontsize=12)
    plt.legend(('Piecewise fit','Exposure-corrected profile'),loc='best',fontsize=12)

    Fitting 6-model step-and-ramp parameters to the folded profile
    
Fitting 6-model step-and-ramp parameters to the folded profile
Fitting 6 - model step - and - ramp parameters to the folded profile
def step_n_ramp(phase,prof,prof_err,start_phase,end_phase,pguess):
    phase_model = np.linspace(start_phase,end_phase,101)
    x = phase[(phase>=start_phase)&(phase<=end_phase)]
    y = prof[(phase>=start_phase)&(phase<=end_phase)]
    y_err = prof_err[(phase>=start_phase)&(phase<=end_phase)]
    def piecewise_linear(x,b1,b2,b3,b4,top,bottom):
        return np.piecewise(x, [(x>=start_phase)&(x<=b1), (x>b1)&(x<=b2), (x>b2)&(x<=b3), (x>b3)&(x<=b4), (x>b4)&(x<=end_phase)], [lambda x:top, lambda x:((bottom-top)/(b2-b1)*x+bottom-(bottom-top)/(b2-b1)*b2), lambda x:bottom, lambda x:((top-bottom)/(b4-b3)*x+top-(top-bottom)/(b4-b3)*b4), lambda x:top])
    plt.figure()
    popt,pcov = curve_fit(piecewise_linear,x,y,p0=pguess,sigma=y_err,absolute_sigma=True)
    pars = popt
    pars_err = np.diag(np.sqrt(pcov))
    print('Top: ' + str(pars[4]) + ' +- ' + str(pars_err[4]))
    print('Bottom: ' + str(pars[5]) + ' +- ' + str(pars_err[5]))
    print('Vertex 1: ' + str(pars[0]) + ' +- ' + str(pars_err[0]))
    print('Vertex 2: ' + str(pars[1]) + ' +- ' + str(pars_err[1]))
    print('Vertex 3: ' + str(pars[2]) + ' +- ' + str(pars_err[2]))
    print('Vertex 4: ' + str(pars[3]) + ' +- ' + str(pars_err[3]))
    plt.plot(phase_model,piecewise_linear(phase_model,*popt),'k-')
    plt.errorbar(x=phase,y=prof,yerr=prof_err,color='r',drawstyle='steps-mid')
    plt.title('Exposure-corrected (profiles from Stingray)',fontsize=12)
    plt.xlabel('Phase',fontsize=12)
    plt.ylabel('Counts/s',fontsize=12)
    plt.legend(('Piecewise fit','Exposure-corrected profile'),loc='best',fontsize=12)
def step_n_ramp ( phase , prof , prof_err , start_phase , end_phase , pguess ) : phase_model = np . linspace ( start_phase , end_phase , 101 ) x = phase [ ( phase >= start_phase ) & ( phase <= end_phase ) ] y = prof [ ( phase >= start_phase ) & ( phase <= end_phase ) ] y_err = prof_err [ ( phase >= start_phase ) & ( phase <= end_phase ) ] def piecewise_linear ( x , b1 , b2 , b3 , b4 , top , bottom ) : return np . piecewise ( x , [ ( x >= start_phase ) & ( x <= b1 ) , ( x > b1 ) & ( x <= b2 ) , ( x > b2 ) & ( x <= b3 ) , ( x > b3 ) & ( x <= b4 ) , ( x > b4 ) & ( x <= end_phase ) ] , [ lambda x : top , lambda x : ( ( bottom - top ) / ( b2 - b1 ) * x + bottom - ( bottom - top ) / ( b2 - b1 ) * b2 ) , lambda x : bottom , lambda x : ( ( top - bottom ) / ( b4 - b3 ) * x + top - ( top - bottom ) / ( b4 - b3 ) * b4 ) , lambda x : top ] ) plt . figure ( ) popt , pcov = curve_fit ( piecewise_linear , x , y , p0 = pguess , sigma = y_err , absolute_sigma = True ) pars = popt pars_err = np . diag ( np . sqrt ( pcov ) ) print ( 'Top: ' + str ( pars [ 4 ] ) + ' +- ' + str ( pars_err [ 4 ] ) ) print ( 'Bottom: ' + str ( pars [ 5 ] ) + ' +- ' + str ( pars_err [ 5 ] ) ) print ( 'Vertex 1: ' + str ( pars [ 0 ] ) + ' +- ' + str ( pars_err [ 0 ] ) ) print ( 'Vertex 2: ' + str ( pars [ 1 ] ) + ' +- ' + str ( pars_err [ 1 ] ) ) print ( 'Vertex 3: ' + str ( pars [ 2 ] ) + ' +- ' + str ( pars_err [ 2 ] ) ) print ( 'Vertex 4: ' + str ( pars [ 3 ] ) + ' +- ' + str ( pars_err [ 3 ] ) ) plt . plot ( phase_model , piecewise_linear ( phase_model , * popt ) , 'k-' ) plt . errorbar ( x = phase , y = prof , yerr = prof_err , color = 'r' , drawstyle = 'steps-mid' ) plt . title ( 'Exposure-corrected (profiles from Stingray)' , fontsize = 12 ) plt . xlabel ( 'Phase' , fontsize = 12 ) plt . ylabel ( 'Counts/s' , fontsize = 12 ) plt . legend ( ( 'Piecewise fit' , 'Exposure-corrected profile' ) , loc = 'best' , fontsize = 12 )
Fitting 6-model step-and-ramp parameters to the folded profile
Fitting 6 - model step - and - ramp parameters to the folded profile
"""
    Fitting 6-model step-and-ramp parameters to the folded profile
    """ ##### Plotting the folded profiles themselves
phase
prof
prof_err
start_phase
end_phase
pguess