示例#1
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
示例#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  
示例#3
0
def bcg_filt_pca(x, ts, m=1024, Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using PCA
    (principal component analysis)"""
    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

        components = pcafilt.unmix(bcgs)
        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"
        bcgs = pcafilt.mix(components)
        for i in range(bcgs.shape[1]):
            try:
                x[ts_bcg[i] - m / 2:ts_bcg[i] + m / 2, ch_num] = bcgs[:, i]
            except ValueError, e:
                print "Value Error, probably shape missmatch", e
示例#4
0
def bcg_filt_pca(x,ts,m=1024,Fs=1000.0):
    """Filter BCG-artifacts from simultaneous recordings of EEG and fMRI using PCA
    (principal component analysis)"""
    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
                
        components = pcafilt.unmix(bcgs)
        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"
        bcgs = pcafilt.mix(components)
        for i in range(bcgs.shape[1]):
            try:
                x[ts_bcg[i]-m/2:ts_bcg[i]+m/2,ch_num] = bcgs[:,i]
            except ValueError,e:
                print "Value Error, probably shape missmatch", e