Example #1
0
def bcg_filt_average_nh_fit(x, ts, m=1024, k=20, Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using AAS 
    (average artifact subtraction), but in local neighborhood. Additionally perform a fit
    of the template to each artifact."""
    def residuals(p, y, x):
        """Residuals used for leastsq-fitting"""
        factor = p[0]
        #err = (y*factor-offset)-x
        err = (y * factor) - x
        return abs(err**2).sum()

    assert len(x.shape) in [1, 2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x = x.reshape((x.shape[0], 1))
    x_hp = _highpass(x, Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m, len(ts_bcg)), "d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:, i] = x_hp[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num]
            except ValueError, e:
                print "Value Error, probably shape missmatch", e

        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T, k)

        for i in range(bcgs.shape[1]):
            boolslice = n.zeros((bcgs.shape[1]), n.bool)
            for ni, neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            try:
                template = bcgs[:, boolslice].mean(axis=1) * window
                p_lsq = fmin(residuals, [1],
                             args=(template,
                                   x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2,
                                     ch_num]),
                             disp=False)
                factor = p_lsq[0]
                #print i, p_lsq
                if factor > 0:
                    #template = template*factor-offset
                    template = template * factor
                    x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num] -= template
            except ValueError, e:
                print "Value Error, probably shape missmatch", e
Example #2
0
def bcg_filt_pca_nh(x, ts, m=1024, k=20):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using PCA
    (principal component analysis), but in local neighborhood"""
    assert len(x.shape) in [1, 2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x = x.reshape((x.shape[0], 1))
    x = _highpass(x, Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m, len(ts_bcg)), "d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:, i] = x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num]
            except ValueError, e:
                print "Value Error, probably shape missmatch", e

        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T, k)

        for i in range(bcgs.shape[1]):
            boolslice = n.zeros((bcgs.shape[1]), n.bool)
            for ni, neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            NH = bcgs[:, boolslice]
            components = pcafilt.unmix(NH)
            for k in range(components.shape[1]):
                if components[:, k].std() > 100.0:  #acc > 0.03:
                    if debug:
                        print "Channel %i, Component %i, std %f, mean(abs) %f" % (
                            ch_num, k, components[:, k].std(),
                            abs(components[:, k]).mean()), "removed"
                    components[:, k] = n.zeros(components[:, k].shape, "d")
                else:
                    if k == 0:
                        if debug:
                            print "Channel %i, Component %i, std %f, mean(abs) %f" % (
                                ch_num, k, components[:, k].std(),
                                abs(components[:, k]).mean()), "not removed"
            NH = pcafilt.mix(components)
            try:
                x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num] = NH[:, 0]
            except ValueError, e:
                print "Value Error, probably shape missmatch", e
Example #3
0
def bcg_filt_average_nh_fit(x,ts,m=1024,k=20,Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using AAS 
    (average artifact subtraction), but in local neighborhood. Additionally perform a fit
    of the template to each artifact."""
    def residuals(p,y,x):
        """Residuals used for leastsq-fitting"""
        factor = p[0]
        #err = (y*factor-offset)-x
        err = (y*factor)-x
        return abs(err**2).sum()
    
    assert len(x.shape) in [1,2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x=x.reshape((x.shape[0],1))
    x_hp = _highpass(x,Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m,len(ts_bcg)),"d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:,i] = x_hp[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num]
            except ValueError,e:
                print "Value Error, probably shape missmatch", e
        
        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T,k)
        
        for i in range(bcgs.shape[1]):
            boolslice = n.zeros((bcgs.shape[1]),n.bool)
            for ni,neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            try:
                template = bcgs[:,boolslice].mean(axis=1) * window
                p_lsq = fmin(residuals,[1],args=(template,x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num]),disp=False)
                factor = p_lsq[0]
                #print i, p_lsq
                if factor>0:
                    #template = template*factor-offset
                    template = template*factor
                    x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num] -= template
            except ValueError,e:
                print "Value Error, probably shape missmatch", e
Example #4
0
def bcg_filt_average_nh(x, ts, m=1024, k=20, Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using AAS 
    (average artifact subtraction), but in local neighborhood"""
    assert len(x.shape) in [1, 2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x = x.reshape((x.shape[0], 1))
    x = _highpass(x, Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m, len(ts_bcg)), "d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:, i] = x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num]
            except ValueError, e:
                print "Value Error, probably shape missmatch", e

        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T, k)

        #        if debug:
        #            print "Testing neighborhood: Do some plots..."
        #        for i in range(0,neighbors.shape[0],50):
        #            boolslice = n.zeros((bcgs.shape[1]),n.bool)
        #            #neighborhood = n.zeros((m,k),"d")
        #            for j in range(k):
        #                boolslice[neighbors[i,j]] = True
        #            neighborhood = bcgs[:,boolslice]

        #p.plot(neighborhood.mean(axis=1))
        #p.plot(bcgs[:,:].mean(axis=1))

        for i in range(bcgs.shape[1]):
            boolslice = n.zeros((bcgs.shape[1]), n.bool)
            for ni, neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            try:
                x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2,
                  ch_num] -= bcgs[:, boolslice].mean(axis=1) * window
            except ValueError, e:
                print "Value Error, probably shape missmatch", e
Example #5
0
def bcg_filt_average_nh(x,ts,m=1024,k=20,Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using AAS 
    (average artifact subtraction), but in local neighborhood"""
    assert len(x.shape) in [1,2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x=x.reshape((x.shape[0],1))
    x = _highpass(x,Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m,len(ts_bcg)),"d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:,i] = x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num]
            except ValueError,e:
                print "Value Error, probably shape missmatch", e
        
        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T,k)
        
#        if debug:
#            print "Testing neighborhood: Do some plots..."
#        for i in range(0,neighbors.shape[0],50):
#            boolslice = n.zeros((bcgs.shape[1]),n.bool)
#            #neighborhood = n.zeros((m,k),"d")
#            for j in range(k):
#                boolslice[neighbors[i,j]] = True
#            neighborhood = bcgs[:,boolslice]
            
            #p.plot(neighborhood.mean(axis=1))
            #p.plot(bcgs[:,:].mean(axis=1))


        for i in range(bcgs.shape[1]):
            boolslice = n.zeros((bcgs.shape[1]),n.bool)
            for ni,neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            try:
                x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num] -= bcgs[:,boolslice].mean(axis=1) * window
            except ValueError,e:
                print "Value Error, probably shape missmatch", e
Example #6
0
def bcg_filt_pca_nh(x,ts,m=1024,k=20):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using PCA
    (principal component analysis), but in local neighborhood"""
    assert len(x.shape) in [1,2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x=x.reshape((x.shape[0],1))
    x = _highpass(x,Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m,len(ts_bcg)),"d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:,i] = x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num]
            except ValueError,e:
                print "Value Error, probably shape missmatch", e
        
        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T,k)

        for i in range(bcgs.shape[1]):
            boolslice = n.zeros((bcgs.shape[1]),n.bool)
            for ni,neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            NH = bcgs[:,boolslice]
            components = pcafilt.unmix(NH)
            for k in range(components.shape[1]):
                if components[:,k].std()>100.0:#acc > 0.03:
                    if debug:
                        print "Channel %i, Component %i, std %f, mean(abs) %f" % (ch_num,k,components[:,k].std(), abs(components[:,k]).mean()), "removed"
                    components[:,k] = n.zeros(components[:,k].shape,"d")  
                else:
                    if k==0:
                        if debug:
                            print "Channel %i, Component %i, std %f, mean(abs) %f" % (ch_num,k,components[:,k].std(), abs(components[:,k]).mean()), "not removed"
            NH = pcafilt.mix(components)
            try:
                x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num] = NH[:,0]
            except ValueError,e:
                print "Value Error, probably shape missmatch", e  
Example #7
0
def bcg_filt_wavelet(x,
                     ts,
                     m=1024,
                     wave_name="coif4",
                     k=20,
                     lambda_r=1,
                     Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI"""
    assert len(x.shape) in [1, 2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x = x.reshape((x.shape[0], 1))
    x = _highpass(x, Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m, len(ts_bcg)), "d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:, i] = x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2,
                               ch_num] * window
            except ValueError, e:
                print "Value Error, probably shape missmatch", e

        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T, k)

        #        if debug:
        #            print "Testing neighborhood: Do some plots..."
        #        for i in range(0,neighbors.shape[0],50):
        #            boolslice = n.zeros((bcgs.shape[1]),n.bool)
        #            #neighborhood = n.zeros((m,k),"d")
        #            for j in range(k):
        #                boolslice[neighbors[i,j]] = True
        #            neighborhood = bcgs[:,boolslice]

        #p.plot(neighborhood.mean(axis=1))
        #p.plot(bcgs[:,:].mean(axis=1))

        if debug:
            print "Wavelet transform"
        wavelet_tr_s_lin = wavedec_lin(bcgs, wave_name)
        #walk through all points
        #Calculate the center-of-mass and sd for all wavelets
        sqrt_kp1 = n.sqrt(k + 1)

        wts_to_shrink = wavelet_tr_s_lin.copy()
        #print "Shrinking for lambda_r =", lambda_r
        for i in range(wts_to_shrink.shape[1]):
            #numShrinked.append(0)
            if debug:
                if i % 100 == 0:
                    if debug:
                        print "i:", i
            boolslice = n.zeros((wts_to_shrink.shape[1]), n.bool)
            for ni, neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            NH = wavelet_tr_s_lin[:, boolslice]
            COM = NH.mean(axis=1)
            STD = NH.std(axis=1)
            #apply the shrinking condition
            for idx in range(wts_to_shrink.shape[0]):
                if abs(COM[idx]) < (2 * lambda_r * STD[idx] / sqrt_kp1):
                    wts_to_shrink[idx, i] = COM[idx]

        if debug:
            print "Wavelet-reconstruction"
        bcgs_f = waverec_lin(wts_to_shrink, wave_name)

        if debug:
            print "Writing back results to eeg"
        #I have to subtract the filtered bcgs from the eeg as these are now the
        #"pure" artifact.
        for i in range(bcgs.shape[1]):
            try:
                x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num] -= bcgs_f[:, i]
            except ValueError, e:
                print "Value Error, probably shape missmatch", e
Example #8
0
def bcg_filt_wavelet(x,ts,m=1024,wave_name="coif4",k=20, lambda_r=1,Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI"""
    assert len(x.shape) in [1,2], "x must be a 1d- or 2d-array"
    if len(x.shape) == 1:
        x=x.reshape((x.shape[0],1))
    x = _highpass(x,Fs=Fs)
    window = n.hanning(m)
    #Other possibilities for window:
    #n.ones((m),"d")
    #n.hamming(m)
    #n.bartlett(m)
    ts_bcg = ts

    for ch_num in range(x.shape[1]):
        if debug:
            print "Get all BCG-artifacts of channel %i in one array" % ch_num
        bcgs = n.zeros((m,len(ts_bcg)),"d")
        for i in range(bcgs.shape[1]):
            #print eeg[ts_bcg[i]-m/2:ts_bcg[i]+m/2,14].shape
            try:
                bcgs[:,i] = x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num]*window
            except ValueError,e:
                print "Value Error, probably shape missmatch", e
        
        if debug:
            print "Find nearest neighbors"
        neighbors = find_n_nearest_neighbors(bcgs.T,k)
        
#        if debug:
#            print "Testing neighborhood: Do some plots..."
#        for i in range(0,neighbors.shape[0],50):
#            boolslice = n.zeros((bcgs.shape[1]),n.bool)
#            #neighborhood = n.zeros((m,k),"d")
#            for j in range(k):
#                boolslice[neighbors[i,j]] = True
#            neighborhood = bcgs[:,boolslice]
            
            #p.plot(neighborhood.mean(axis=1))
            #p.plot(bcgs[:,:].mean(axis=1))
            
        if debug:
            print "Wavelet transform"
        wavelet_tr_s_lin = wavedec_lin(bcgs,wave_name)
        #walk through all points
        #Calculate the center-of-mass and sd for all wavelets
        sqrt_kp1 = n.sqrt(k+1)
        
        wts_to_shrink = wavelet_tr_s_lin.copy()
        #print "Shrinking for lambda_r =", lambda_r 
        for i in range(wts_to_shrink.shape[1]):
            #numShrinked.append(0)
            if debug:
                if i%100==0:
                    if debug:
                        print "i:", i
            boolslice = n.zeros((wts_to_shrink.shape[1]),n.bool)
            for ni,neighbor in enumerate(neighbors[i]):
                boolslice[neighbor] = True
            NH = wavelet_tr_s_lin[:,boolslice]            
            COM = NH.mean(axis=1)
            STD = NH.std(axis=1)
            #apply the shrinking condition
            for idx in range(wts_to_shrink.shape[0]):
                if abs(COM[idx])<(2*lambda_r*STD[idx]/sqrt_kp1):
                    wts_to_shrink[idx,i]= COM[idx]
                    
        if debug:
            print "Wavelet-reconstruction"
        bcgs_f = waverec_lin(wts_to_shrink,wave_name)
        
        if debug:
            print "Writing back results to eeg"
        #I have to subtract the filtered bcgs from the eeg as these are now the 
        #"pure" artifact.
        for i in range(bcgs.shape[1]):
            try:
                x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num] -= bcgs_f[:,i]
            except ValueError,e:
                print "Value Error, probably shape missmatch", e