Exemplo n.º 1
0
def cohere_pairs_eeg( eeg, newLength, NFFT, offset, eoiPairs=None, indMin=0, indMax=None,
                      data=None, returnPxx=False, **kwargs):
    """
    FUNC: cohere_pairs_eeg
    DESCR: Cxy, Phase, freqs = cohere_pairs_eeg(  ...)
    
    Compute the coherence for all pairs in the eoi.  eeg is a
    EEG instance.

    eoiPairs is a list of electrode tuples; if none, use all.  Each
    tuple is a pair of electrodes, eg,

      eoiPairs = [ ( ('MT',7), ('MT',8) ),
                     ( ('MT',7), ('MT',9) ),
                     ....
                     ]

    indMin, indmax if provided, give the sample number indices into
    eeg.data to do the coherence over (default all)

    if data is not None, use data rather than eeg.data to compute
    coherence
    
    The other function arguments, except for 'preferSpeedOverMemory'
    (see below), are explained in the help string of 'psd'.

    Return value is a tuple (Cxy, Phase, freqs).

      Cxy -- a dictionary of electrode tuples -> coherence vector for that
        pair.  
      
      Phase -- a dictionary of phases of the cross spectral density at
        each frequency for each pair.  keys are (e1,e2).

      freqs -- a vector of frequencies, equal in length to either the
        coherence or phase vectors for any electrode key.  Eg, to make
        a coherence
        
        Bode plot:
          e1 = ('MT', 7)
          e2 = ('MT', 8)
          subplot(211)
          plot( freqs, Cxy[(e1,e2)])
          subplot(212)
          plot( freqs, Phase[(e1,e2)])
      
    For a large number of pairs, cohere_pairs can be much more
    efficient than just calling cohere for each pair, because it
    caches most of the intensive computations.  If N is the number of
    pairs, this function is O(N) for most of the heavy lifting,
    whereas calling cohere for each pair is O(N^2).  However, because
    of the caching, it is also more memory intensive, making 2
    additional complex arrays with approximately the same number of
    elements as X.

    See mlab cohere_pairs for optional kwargs
    
    See test/cohere_pairs_test.py in the src tree for an example
    script that shows that this cohere_pairs and cohere give the same
    results for a given pair.

    """

    print "utils.cohere_pairs_eeg: eeg.freq: ", eeg.freq   
    
    amp = eeg.get_amp()
    print "UTILS AMP: ", amp
    if eoiPairs is None:
        eoiPairs = all_pairs_eoi( amp.to_eoi() )


    m = amp.get_electrode_to_indices_dict()
    ij = [ (m[e1], m[e2]) for e1, e2 in eoiPairs]
    ij.sort()
        
    print len(ij), len(eoiPairs)

    if data is None: data = eeg.data

    #print "cohere_pairs_eeg: data.shape is ", data.shape
    
    if indMax is None: indMax = data.shape[0]
    X = data[indMin:indMax]
    if returnPxx:
        try:
            Cxy, Phase, freqs, Pxx = cohere_pairs(
                X, ij, newLength, NFFT, offset, Fs=eeg.freq, returnPxx=True, **kwargs)
        except OverflowError, overflowerror:
            print "cohere_pairs_eeg(): caught overflow error!! bailing: ", overflowerror
Exemplo n.º 2
0
    if data is None: data = eeg.data

    #print "cohere_pairs_eeg: data.shape is ", data.shape
    
    if indMax is None: indMax = data.shape[0]
    X = data[indMin:indMax]
    if returnPxx:
        try:
            Cxy, Phase, freqs, Pxx = cohere_pairs(
                X, ij, newLength, NFFT, offset, Fs=eeg.freq, returnPxx=True, **kwargs)
        except OverflowError, overflowerror:
            print "cohere_pairs_eeg(): caught overflow error!! bailing: ", overflowerror
            
    else:
        Cxy, Phase, freqs = cohere_pairs(
            X, ij, newLength, NFFT, offset, Fs=eeg.freq, **kwargs)

    seen = {}
    keys = Cxy.keys()
    keys.sort()

    assert(len(ij)==len(eoiPairs))

    for keyIJ, keyEOI in zip(ij, eoiPairs):
        Cxy[keyEOI] = Cxy[keyIJ]
        del Cxy[keyIJ]
        Phase[keyEOI] = Phase[keyIJ]
        del Phase[keyIJ]

        i,j = keyIJ
        e1, e2 = keyEOI
Exemplo n.º 3
0
            Cxy, Phase, freqs, Pxx = cohere_pairs(X,
                                                  ij,
                                                  newLength,
                                                  NFFT,
                                                  offset,
                                                  Fs=eeg.freq,
                                                  returnPxx=True,
                                                  **kwargs)
        except OverflowError, overflowerror:
            print "cohere_pairs_eeg(): caught overflow error!! bailing: ", overflowerror

    else:
        Cxy, Phase, freqs = cohere_pairs(X,
                                         ij,
                                         newLength,
                                         NFFT,
                                         offset,
                                         Fs=eeg.freq,
                                         **kwargs)

    seen = {}
    keys = Cxy.keys()
    keys.sort()

    assert (len(ij) == len(eoiPairs))

    for keyIJ, keyEOI in zip(ij, eoiPairs):
        Cxy[keyEOI] = Cxy[keyIJ]
        del Cxy[keyIJ]
        Phase[keyEOI] = Phase[keyIJ]
        del Phase[keyIJ]
Exemplo n.º 4
0
def cohere_pairs_eeg(eeg,
                     newLength,
                     NFFT,
                     offset,
                     eoiPairs=None,
                     indMin=0,
                     indMax=None,
                     data=None,
                     returnPxx=False,
                     **kwargs):
    """
    FUNC: cohere_pairs_eeg
    DESCR: Cxy, Phase, freqs = cohere_pairs_eeg(  ...)
    
    Compute the coherence for all pairs in the eoi.  eeg is a
    EEG instance.

    eoiPairs is a list of electrode tuples; if none, use all.  Each
    tuple is a pair of electrodes, eg,

      eoiPairs = [ ( ('MT',7), ('MT',8) ),
                     ( ('MT',7), ('MT',9) ),
                     ....
                     ]

    indMin, indmax if provided, give the sample number indices into
    eeg.data to do the coherence over (default all)

    if data is not None, use data rather than eeg.data to compute
    coherence
    
    The other function arguments, except for 'preferSpeedOverMemory'
    (see below), are explained in the help string of 'psd'.

    Return value is a tuple (Cxy, Phase, freqs).

      Cxy -- a dictionary of electrode tuples -> coherence vector for that
        pair.  
      
      Phase -- a dictionary of phases of the cross spectral density at
        each frequency for each pair.  keys are (e1,e2).

      freqs -- a vector of frequencies, equal in length to either the
        coherence or phase vectors for any electrode key.  Eg, to make
        a coherence
        
        Bode plot:
          e1 = ('MT', 7)
          e2 = ('MT', 8)
          subplot(211)
          plot( freqs, Cxy[(e1,e2)])
          subplot(212)
          plot( freqs, Phase[(e1,e2)])
      
    For a large number of pairs, cohere_pairs can be much more
    efficient than just calling cohere for each pair, because it
    caches most of the intensive computations.  If N is the number of
    pairs, this function is O(N) for most of the heavy lifting,
    whereas calling cohere for each pair is O(N^2).  However, because
    of the caching, it is also more memory intensive, making 2
    additional complex arrays with approximately the same number of
    elements as X.

    See mlab cohere_pairs for optional kwargs
    
    See test/cohere_pairs_test.py in the src tree for an example
    script that shows that this cohere_pairs and cohere give the same
    results for a given pair.

    """

    print("utils.cohere_pairs_eeg: eeg.freq: ", eeg.freq)

    amp = eeg.get_amp()
    print("UTILS AMP: ", amp)
    if eoiPairs is None:
        eoiPairs = all_pairs_eoi(amp.to_eoi())

    m = amp.get_electrode_to_indices_dict()
    ij = [(m[e1], m[e2]) for e1, e2 in eoiPairs]
    ij.sort()

    print(len(ij), len(eoiPairs))

    if data is None: data = eeg.data

    #print "cohere_pairs_eeg: data.shape is ", data.shape

    if indMax is None: indMax = data.shape[0]
    X = data[indMin:indMax]
    if returnPxx:
        try:
            Cxy, Phase, freqs, Pxx = cohere_pairs(X,
                                                  ij,
                                                  newLength,
                                                  NFFT,
                                                  offset,
                                                  Fs=eeg.freq,
                                                  returnPxx=True,
                                                  **kwargs)
        except OverflowError as overflowerror:
            print("cohere_pairs_eeg(): caught overflow error!! bailing: ",
                  overflowerror)

    else:
        Cxy, Phase, freqs = cohere_pairs(X,
                                         ij,
                                         newLength,
                                         NFFT,
                                         offset,
                                         Fs=eeg.freq,
                                         **kwargs)

    seen = {}
    keys = list(Cxy.keys())
    keys.sort()

    assert (len(ij) == len(eoiPairs))

    for keyIJ, keyEOI in zip(ij, eoiPairs):
        Cxy[keyEOI] = Cxy[keyIJ]
        del Cxy[keyIJ]
        Phase[keyEOI] = Phase[keyIJ]
        del Phase[keyIJ]

        i, j = keyIJ
        e1, e2 = keyEOI
        seen[i] = e1
        seen[j] = e2
    #print Cxy
    #print Phase, "&*&*&"
    #print freqs
    if returnPxx:
        for i, ei in list(seen.items()):
            Pxx[ei] = Pxx[i]
            del Pxx[i]

    if returnPxx:
        return Cxy, Phase, freqs, Pxx
    else:
        return Cxy, Phase, freqs
Exemplo n.º 5
0
def cohere_pairs_eeg( eeg, newLength, NFFT, offset, eoiPairs=None, indMin=0, indMax=None,
                      data=None, returnPxx=False, calc_type='coherence', shape=None,**kwargs):
    """
    FUNC: cohere_pairs_eeg
    DESCR: Cxy, Phase, freqs = cohere_pairs_eeg(  ...)
    
    Compute the coherence for all pairs in the eoi.  eeg is a
    EEG instance.

    eoiPairs is a list of electrode tuples; if none, use all.  Each
    tuple is a pair of electrodes, eg,

      eoiPairs = [ ( ('MT',7), ('MT',8) ),
                     ( ('MT',7), ('MT',9) ),
                     ....
                     ]

    indMin, indmax if provided, give the sample number indices into
    eeg.data to do the coherence over (default all)

    if data is not None, use data rather than eeg.data to compute
    coherence
    
    The other function arguments, except for 'preferSpeedOverMemory'
    (see below), are explained in the help string of 'psd'.

    Return value is a tuple (Cxy, Phase, freqs).

      Cxy -- a dictionary of electrode tuples -> coherence vector for that
        pair.  
      
      Phase -- a dictionary of phases of the cross spectral density at
        each frequency for each pair.  keys are (e1,e2).

      freqs -- a vector of frequencies, equal in length to either the
        coherence or phase vectors for any electrode key.  Eg, to make
        a coherence
        
        Bode plot:
          e1 = ('MT', 7)
          e2 = ('MT', 8)
          subplot(211)
          plot( freqs, Cxy[(e1,e2)])
          subplot(212)
          plot( freqs, Phase[(e1,e2)])
      
    For a large number of pairs, cohere_pairs can be much more
    efficient than just calling cohere for each pair, because it
    caches most of the intensive computations.  If N is the number of
    pairs, this function is O(N) for most of the heavy lifting,
    whereas calling cohere for each pair is O(N^2).  However, because
    of the caching, it is also more memory intensive, making 2
    additional complex arrays with approximately the same number of
    elements as X.

    See mlab cohere_pairs for optional kwargs
    
    See test/cohere_pairs_test.py in the src tree for an example
    script that shows that this cohere_pairs and cohere give the same
    results for a given pair.

    """

    print "utils.cohere_pairs_eeg: eeg.freq: ", eeg.freq   
    
    amp = eeg.get_amp()
    #print "UTILS AMP: ", amp
    if eoiPairs is None:
        eoiPairs = all_pairs_eoi( amp.to_eoi() )

    offset = int(offset)
    print "COHERE_PAIRS_EEG: int offset: ", offset 
    
    m = amp.get_electrode_to_indices_dict()
    ij = [ (m[e1], m[e2]) for e1, e2 in eoiPairs]
    ij.sort()
        
    print "COHERE_PAIRS EEG PAIRS LENGTH: ", len(ij), len(eoiPairs)

    if data is None: data = eeg.data

    #print "cohere_pairs_eeg: data.shape is ", data.shape
    All = {}
    if indMax is None: indMax = data.shape[0]
    X = data[indMin:indMax]
    if calc_type == 'ddtf':
        Cxy, Phase, freqs = granger.ddtf_test(X, ij, newLength, NFFT, offset, Fs=eeg.freq,progressCallback=donothing_callback,calc_type=calc_type,shape=shape)
    elif calc_type == 'correlation':
        Cxy, Phase, freqs = granger.ddtf_test(X, ij, newLength, NFFT, offset, Fs=eeg.freq,progressCallback=donothing_callback,calc_type=calc_type,shape=shape)
    else:
        if returnPxx:
            try:
                Cxy, Phase, freqs, Pxx = cohere_pairs(
                    X, ij, newLength, NFFT, offset, Fs=eeg.freq, returnPxx=True, **kwargs)
            except OverflowError, overflowerror:
                print "cohere_pairs_eeg(): caught overflow error!! bailing: ", overflowerror
            
        else: