def subtract_baseline(y_data, deg=3, plot=False, x_data=None):
    """
    Function that fits an n-degree polynomial (default: n = 3) baseline
    to the spectral data, and subtracts it from the input data.

    Args:
        y_data (list like): The intensity data of the spectra to be baselined.
        deg (integer): (Optional) Integer value for the degree of the polynomial
                       to be used to baseline data. The value defaults to 3 if
                       no value is specified.
        plot (boolean): (Optional) Boolean value that indicates whether or not to
                        plot the baselined and original data. If true, it plots
                        both spectra on the same plot. Note that if the user wants
                        plotting functionality, x_data must be provided.
        x_data (list like): (Optional) The x-values associated with the y_data that
                            is being baselined. These values are only needed for
                            the function if the user desires plotting.

    Returns:
        y_out (list like): The baselined values of the y-axis.
    """
    # handling errors in inputs
    if not isinstance(y_data, (list, np.ndarray)):
        raise TypeError('Passed value of `y_data` is not a list or numpy.ndarray! Instead, it is: '
                        + str(type(y_data)))
    y_base = baseline(y_data, deg=deg, max_it=200)
    # to avoid strange results,
    # change all negative values to zero
    yb_plus = [0 if i < 0 else i for i in y_base]
    y_out = y_data - yb_plus
    # plot that lets you see the baseline fitting
    if plot and x_data is None:
        raise ValueError('Please add x_data as input to plot')
    elif plot:
        plt.figure(figsize=(10, 4))
        plt.plot(x_data, y_data, 'b--', label='input')
        plt.plot(x_data, y_out, 'b', label='output')
        plt.plot(x_data, yb_plus, 'r', label='baseline')
        plt.plot(x_data, y_base, 'r--', label='negative baseline')
        plt.axhline(y=0, color='orange', alpha=0.5, label='zero')
        plt.legend()
    else:
        pass
    return y_out
Beispiel #2
0
import sys
#sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append('/home/matt/PycharmProjects')
from StimPy import minis
import numpy as np
import matplotlib.pyplot as plt
from peakutils.baseline import baseline

file_path = '/home/matt/Data/patch_data/dnm1_ftfl/cortical/170425_170428/\
170425/2017_04_25_0044.abf'

#file_path = '/home/matt/Data/patch_data/dyn1/170222/170221_0046.abf'

recording = minis.MiniRec(file_path)

recording.filter(ftype='bessel', freq=1000.0)

events = recording.detect(method='derivative', threshold=25)
#events = recording.detect(template_type='ipsc')
baselined_events = list()
for e in events:
    base = baseline(-e, deg=2)
    baselined_events.append((e + base))
events = np.array(baselined_events)
plt.plot(events.T, alpha=0.1)
plt.plot(np.mean(events, axis=0))
plt.show()
Beispiel #3
0
def structure_para(filename, freql, freqh, snr, dm, onp):
    '''
    Copied from original to just measure rms across on-pulse
    '''

    #Using PyPulse
    '''
    data = pp.Archive(filename,prepare=False)
    data1 = data.data[0][0]
    #time = np.sum(data1,axis=0)	
    freq = data.freq[0]
    '''

    #Using PSRCHIVE
    #'''
    fpsr = psr.Archive_load(filename)
    fpsr.remove_baseline()
    fpsr.set_dispersion_measure(0)
    fpsr.dedisperse()
    fpsr.set_dispersion_measure(dm)
    fpsr.dedisperse()
    ds = fpsr.get_data().squeeze()
    w = fpsr.get_weights().flatten()
    w = w / np.max(w)  # Normalized it
    idx = np.where(w == 0)[0]
    ds = np.multiply(ds, w[np.newaxis, :, np.newaxis])  # Apply it
    ds[:, idx, :] = np.nan
    data1 = ds[0, :, :]
    #print np.shape(data1)
    freq = fpsr.get_frequencies()
    #'''
    #print freq,freql,freqh
    #print find_nearidx(freq,freql),find_nearidx(freq,freqh)
    #plt.xlim(283,332)
    #plt.imshow(data1,aspect='auto')
    #plt.show()

    #time = np.sum(data1[find_nearidx(freq,freqh):find_nearidx(freq,freql),:],axis=0)
    #time = np.nansum(data1[find_nearidx(freq,freql):find_nearidx(freq,freqh),:],axis=0)

    time = np.nansum(data1, axis=0)
    time = time - baseline(time, 6)
    #time = (time - np.mean(time))/np.max(time)

    #tbin = list(data1.durations/data1[0].size)[0]
    #taxis = np.arange(0,data1.durations,tbin)

    # Settings
    lag = 200
    #lag = 30
    threshold = snr
    influence = 0.4
    ext = 20  # Extra phase bins around the pulse
    #-----------

    y = time
    # Run algo with settings from above
    '''
    result = thresholding_algo(time, lag=lag, threshold=threshold, influence=influence)
  
    res = np.where(result['signals']==1)
    rescen = int(np.nanmean(res))

    low1 = rescen - ext
    high1 = rescen + ext
    
     
    #Orig	
    low = np.min(res)-1
    high = np.max(res)+1
    '''
    # Test
    #low = 301
    #high = 317

    #For TschFsch file
    #low1 = 282
    #high1 = 332

    #For TscFsch2 file
    #low1 = 1
    #high1 = 128

    #For TschFsch3 file
    #low1 = 1
    #high1 = 256

    low1 = onp[0]
    high1 = onp[1]

    #print time[low:high]
    #spec = np.sum(data1[:,low:high],axis=1)
    onpulse = time[low1:high1]
    #onpulse = onpulse - np.mean(onpulse)

    offpulse = time[low1 + 4 * ext:high1 + 4 * ext]
    #print low1+4*ext,high1+4*ext

    #error = np.std(offpulse)
    #strct_para = np.mean(abs(np.diff(abs(np.diff(onpulse)))))

    #Orig
    #strct_para = np.mean(abs(np.diff(onpulse)))

    #DMgrad = abs(np.diff(time))
    DMgrad = abs(np.gradient(time, 2))
    #DMgrad = time
    strct_para = np.mean(DMgrad)

    error = np.mean(abs(np.diff(offpulse)))
    #print dm,strct_para,error,low1,high1

    #plt.xlim(low1,high1)
    #plt.plot(time[low1:high1])
    #plt.plot(time)
    #plt.plot(onpulse)
    #plt.plot(np.diff(onpulse))
    #plt.plot(offpulse)
    #plt.show()

    #oname = "".join(filename.split(".")[:-1]) + ".eps"
    #specname = "".join(filename.split(".")[:-1]) + "_spectra.txt"
    '''	
    oname = filename + ".eps"
    specname = filename + "_spectra.txt"
	 
    np.savetxt(specname,spec,fmt="%.2f")	
    #np.savetxt(specname,np.c_[freq,spec],fmt="%.2f %.2f")	

    plt.rcParams['axes.linewidth'] = 2
    plt.subplots_adjust(hspace = .001)
    plt.subplots_adjust(wspace = .001)
    ax1 = plt.subplot2grid((6,5), (0,0), rowspan=2,colspan=4)

    #plt.subplot(211)
    plt.xlim(low-ext,high+ext)
    plt.setp(ax1.get_xticklabels(), visible=False)
    #plt.setp(ax1.get_yticklabels(), visible=False
    ax1.set_ylabel('Flux (mJy)',fontsize=24, fontweight='bold')
    plt.tick_params(axis='both', which='major', labelsize=22)
    ax1.yaxis.set_ticks(np.arange(0,max(y),max(y)/5))
    #plt.yticks(rotation=90)
    #plt.locator_params(axis='y', nticks=4)
    ax1.plot(np.arange(1, len(y)+1), y,linewidth=2,color='black')

    #plt.subplot(212)
    #plt.step(np.arange(1, len(y)+1), result["signals"], color="red", lw=2)
    ax2 = plt.subplot2grid((6,5), (2,0), rowspan=4,colspan=4)
    #plt.xlim(low-20,high+20)
    pdata = data1[:,low-ext:high+ext]
    ptime = taxis[low-ext:high+ext]
    lowedge = (taxis[low]-np.mean(ptime))*1000 # msec
    highedge = (taxis[high]-np.mean(ptime))*1000 # msec	
    ptime=(ptime-np.mean(ptime))*1000 # msec 
    plt.yticks(rotation=90)
    ax2.tick_params(length=4, width=2)
    plt.tick_params(axis='both', which='major', labelsize=22)
    ax2.set_ylabel('Frequency (MHz)',fontsize=24, fontweight='bold')
    ax2.set_xlabel('Time (msec)',fontsize=24, fontweight='bold')
    plt.axvline(lowedge, color='b', linestyle='dashed', linewidth=2)
    plt.axvline(highedge, color='b', linestyle='dashed', linewidth=2)
    plt.axhline(freqh,color='b', linestyle='dashed', linewidth=2)
    plt.axhline(freql, color='b', linestyle='dashed', linewidth=2)
    plt.imshow(pdata,aspect='auto',cmap='binary',extent=[min(ptime),max(ptime),min(freq),max(freq)],interpolation='none')

    ax3 = plt.subplot2grid((6,5), (2,4), rowspan=4,colspan=1)
    plt.setp(ax3.get_xticklabels(), visible=False)
    plt.setp(ax3.get_yticklabels(), visible=False)
    plt.ylim(min(freq),max(freq))
    plt.plot(spec,freq,linewidth=2,color='black')
  
    fig = plt.gcf()
    fig.set_size_inches(8,20) 
    plt.savefig(oname, bbox_inches='tight',dpi=300)	
    '''
    return strct_para, DMgrad, time