Esempio n. 1
0
    def __init__(self, sf, npts, nbins=18, pha_f=[2, 4], pha_meth='hilbert',
                 pha_cycle=3, amp_f=[60, 200], amp_meth='hilbert', amp_cycle=6,
                 window=None, width=None, step=None, time=None,
                 **kwargs):
        # Check pha and amp methods:
        _checkref('pha_meth', pha_meth, ['hilbert', 'hilbert1', 'hilbert2'])
        _checkref('amp_meth', amp_meth, ['hilbert', 'hilbert1', 'hilbert2'])

        # Check the type of f:
        if (len(pha_f) == 4) and isinstance(pha_f[0], (int, float)):
            pha_f = binarize(
                pha_f[0], pha_f[1], pha_f[2], pha_f[3], kind='list')
        if (len(amp_f) == 4) and isinstance(amp_f[0], (int, float)):
            amp_f = binarize(
                amp_f[0], amp_f[1], amp_f[2], amp_f[3], kind='list')
        self.xvec = []
        
        # Binarize phase vector :
        self._binsize = 360 / nbins
        self._phabin = np.arange(0, 360, self._binsize)
        self.phabin = np.concatenate((self._phabin[:, np.newaxis],
                                      self._phabin[:, np.newaxis]+self._binsize), axis=1)

        # Initialize coupling:
        _coupling.__init__(self, pha_f, 'phase', pha_meth, pha_cycle,
                           amp_f, 'amplitude', amp_meth, amp_cycle,
                           sf, npts, window, width, step, time, **kwargs)
        self._nbins = nbins
Esempio n. 2
0
    def __init__(self,
                 sf,
                 npts,
                 Id='113',
                 pha_f=[2, 4],
                 pha_meth='hilbert',
                 pha_cycle=3,
                 amp_f=[60, 200],
                 amp_meth='hilbert',
                 amp_cycle=6,
                 nbins=18,
                 window=None,
                 width=None,
                 step=None,
                 time=None,
                 **kwargs):
        # Check pha and amp methods:
        _checkref('pha_meth', pha_meth, ['hilbert', 'hilbert1', 'hilbert2'])
        _checkref('amp_meth', amp_meth, ['hilbert', 'hilbert1', 'hilbert2'])

        # Check the type of f:
        if (len(pha_f) == 4) and isinstance(pha_f[0], (int, float)):
            pha_f = binarize(pha_f[0],
                             pha_f[1],
                             pha_f[2],
                             pha_f[3],
                             kind='list')
        if (len(amp_f) == 4) and isinstance(amp_f[0], (int, float)):
            amp_f = binarize(amp_f[0],
                             amp_f[1],
                             amp_f[2],
                             amp_f[3],
                             kind='list')
        self.xvec = []

        # Initalize pac object :
        self.Id = Id
        me = Id[0]
        # Manage settings :
        #   1 - Choose if we extract phase or amplitude :
        #       - Methods using phase // amplitude :
        if me in ['1', '2', '3', '5', '6']:
            pha_kind, amp_kind = 'phase', 'amplitude'
        #       - Methods using phase // phase :
        elif me in ['4']:
            pha_kind, amp_kind = 'phase', 'amplitude'
        #   2 - Specific case of Ozkurt :
        if me == '5':
            Id = '500'
        # Initialize cfc :
        _coupling.__init__(self, pha_f, pha_kind, pha_meth, pha_cycle, amp_f,
                           amp_kind, amp_meth, amp_cycle, sf, npts, window,
                           width, step, time, **kwargs)
        # Get pac model :
        _, _, _, ModelStr, SurStr, NormStr = CfcSettings(Id, nbins)
        self.model = [
            'Method : ' + ModelStr, 'Surrogates : ' + SurStr,
            'Normalization : ' + NormStr
        ]
        self._nbins = nbins
Esempio n. 3
0
    def __init__(self, sf, npts, kind, f, baseline, norm, method, window,
                 width, step, split, time, meanT, **kwargs):

        # Check the type of f:
        if (len(f) == 4) and isinstance(f[0], (int, float)):
            f = binarize(f[0], f[1], f[2], f[3], kind='list')
        # Manage time and frequencies:
        self._window, self.xvec = _manageWindow(npts,
                                                window=window,
                                                width=width,
                                                step=step,
                                                time=time)
        self.f, self._fSplit, self._fSplitIndex = _manageFrequencies(
            f, split=split)
        # Get variables :
        self._baseline = baseline
        self._norm = norm
        self._width = width
        self._step = step
        self._split = split
        self._nf = len(self.f)
        self._sf = sf
        self._npts = npts
        self.yvec = [round((k[0] + k[1]) / 2) for k in self.f]
        self._kind = kind
        self._fobj = fextract(method, kind, **kwargs)
        self._meanT = meanT
Esempio n. 4
0
    def __init__(self,
                 sf,
                 npts,
                 f=[2, 4],
                 method='hilbert',
                 cycle=3,
                 sample=None,
                 time=None,
                 **kwargs):
        # Check pha and amp methods:
        _checkref('pha_meth', method, ['hilbert', 'hilbert1', 'hilbert2'])

        # Check the type of f:
        if (len(f) == 4) and isinstance(f[0], (int, float)):
            f = binarize(f[0], f[1], f[2], f[3], kind='list')

        # Initialize PLV :
        _coupling.__init__(self, f, 'phase', method, cycle, f, 'phase', method,
                           cycle, sf, npts, None, None, None, time, **kwargs)
        if time is None:
            time = np.arange(npts)
        else:
            time = time

        if sample is None:
            sample = slice(npts)
        self._sample = sample
        self.time = time[sample]
        del self.amp
Esempio n. 5
0
    def __init__(self, sf, npts, kind, f, baseline, norm, method, window,
                 width, step, split, time, meanT, **kwargs):

        # Check the type of f:
        if (len(f) == 4) and isinstance(f[0], (int, float)):
            f = binarize(f[0], f[1], f[2], f[3], kind='list')
        # Manage time and frequencies:
        self._window, self.xvec = _manageWindow(
            npts, window=window, width=width, step=step, time=time)
        self.f, self._fSplit, self._fSplitIndex = _manageFrequencies(
            f, split=split)
        # Get variables :
        self._baseline = baseline
        self._norm = norm
        self._width = width
        self._step = step
        self._split = split
        self._nf = len(self.f)
        self._sf = sf
        self._npts = npts
        self.yvec = [round((k[0]+k[1])/2) for k in self.f]
        self._kind = kind
        self._fobj = fextract(method, kind, **kwargs)
        self._meanT = meanT
        if (self._window is not None) and (time is not None):
            self.xvec = binArray(time, self._window)[0]
        self.xvec = list(self.xvec)
Esempio n. 6
0
 def __init__(self, sf, npts, f=[60, 200], step=None, width=None,
              time=None):
     # Check the type of f:
     if (len(f) == 4) and isinstance(f[0], (int, float)):
         self.yvec = binarize(f[0], f[1], f[2], f[3], kind='list')
     else:
         self.yvec = f
     if not isinstance(f[0], list):
         self.yvec = [f]
     self._psd = PSD(sf, npts, step=step, width=width, time=time)
Esempio n. 7
0
    def __init__(self, sf, npts, pha_f=[2, 4], pha_meth='hilbert',
                 pha_cycle=3, amp_f=[60, 200], amp_meth='hilbert', amp_cycle=6,
                 window=None, step=None, width=None, time=None, **kwargs):
        # Check pha and amp methods:
        _checkref('pha_meth', pha_meth, ['hilbert', 'hilbert1', 'hilbert2'])
        _checkref('amp_meth', amp_meth, ['hilbert', 'hilbert1', 'hilbert2'])

        # Check the type of f:
        if (len(pha_f) == 4) and isinstance(pha_f[0], (int, float)):
            pha_f = binarize(
                pha_f[0], pha_f[1], pha_f[2], pha_f[3], kind='list')
        if (len(amp_f) == 4) and isinstance(amp_f[0], (int, float)):
            amp_f = binarize(
                amp_f[0], amp_f[1], amp_f[2], amp_f[3], kind='list')

        # Initialize cfc :
        _coupling.__init__(self, pha_f, 'phase', pha_meth, pha_cycle,
                           amp_f, 'amplitude', amp_meth, amp_cycle,
                           sf, npts, window, width, step, time, **kwargs)
Esempio n. 8
0
    def __init__(self, sf, npts, Id='113', pha_f=[2, 4], pha_meth='hilbert',
                 pha_cycle=3, amp_f=[60, 200], amp_meth='hilbert', amp_cycle=6,
                 nbins=18, window=None, width=None, step=None, time=None,
                 **kwargs):
        # Check pha and amp methods:
        _checkref('pha_meth', pha_meth, ['hilbert', 'hilbert1', 'hilbert2'])
        _checkref('amp_meth', amp_meth, ['hilbert', 'hilbert1', 'hilbert2'])

        # Check the type of f:
        if (len(pha_f) == 4) and isinstance(pha_f[0], (int, float)):
            pha_f = binarize(
                pha_f[0], pha_f[1], pha_f[2], pha_f[3], kind='list')
        if (len(amp_f) == 4) and isinstance(amp_f[0], (int, float)):
            amp_f = binarize(
                amp_f[0], amp_f[1], amp_f[2], amp_f[3], kind='list')
        self.xvec = []

        # Initalize pac object :
        self.Id = Id
        me = Id[0]
        # Manage settings :
        #   1 - Choose if we extract phase or amplitude :
        #       - Methods using phase // amplitude :
        if me in ['1', '2', '3', '5', '6']:
            pha_kind, amp_kind = 'phase', 'amplitude'
        #       - Methods using phase // phase :
        elif me in ['4']:
            pha_kind, amp_kind = 'phase', 'amplitude'
        #   2 - Specific case of Ozkurt :
        if me == '5':
            Id = '500'
        # Initialize cfc :
        _coupling.__init__(self, pha_f, pha_kind, pha_meth, pha_cycle,
                           amp_f, amp_kind, amp_meth, amp_cycle,
                           sf, npts, window, width, step, time, **kwargs)
        # Get pac model :
        _, _, _, ModelStr, SurStr, NormStr = CfcSettings(Id, nbins)
        self.model = ['Method : '+ModelStr, 'Surrogates : '+SurStr,
                      'Normalization : '+NormStr]
        self._nbins = nbins
Esempio n. 9
0
def _manageWindow(npts, window=None, width=None, step=None, kind='tuple',
                  time=None):
    """Manage window definition

    Parameters
    ----------
    npts : int
        Number of points in the time signal

    window : tuple, list, None, optional [def: None]
        List/tuple: [100,1500]
        List of list/tuple: [(100,500),(200,4000)]
        None and the width and step paameters will be considered

    width : int, optional [def : None]
        width of a single window

    step : int, optional [def : None]
        Each window will be spaced by the "step" value

    kind : string, optional, [def: 'list']
        Return either a list or a tuple
    """
    if window and (len(window) == 2) and (type(window[0]) == int):
        window = [window]
    else:
        if width is not None:
            if step is None:
                step = round(width/2)
            window = binarize(0, npts, width, step, kind=kind)
    if window:
        xvec = [round((k[0]+k[1])/2) for k in window]
    else:
        xvec = list(n.arange(0, npts))
    if (time is not None) and not isinstance(time, list):
        tme = list(time)
    if (time is not None) and (len(time) == npts):
        xvec = time
    elif time and (len(time) != npts):
        warnings.warn("The length of 'time' ["+str(len(time))+"] must be equal"
                      "to the length of the defined window ["+str(len(xvec)) +
                      ". A default vector is going to used.")

    return window, xvec
Esempio n. 10
0
def _manageWindow(npts, window=None, width=None, step=None, kind="tuple", time=None):
    """Manage window definition

    Parameters
    ----------
    npts : int
        Number of points in the time signal

    window : tuple, list, None, optional [def: None]
        List/tuple: [100,1500]
        List of list/tuple: [(100,500),(200,4000)]
        None and the width and step paameters will be considered

    width : int, optional [def : None]
        width of a single window

    step : int, optional [def : None]
        Each window will be spaced by the "step" value

    kind : string, optional, [def: 'list']
        Return either a list or a tuple
    """
    if window and (len(window) == 2) and (type(window[0]) == int):
        window = [window]
    else:
        if width is not None:
            if step is None:
                step = round(width / 2)
            window = binarize(0, npts, width, step, kind=kind)
    if window:
        xvec = [round((k[0] + k[1]) / 2) for k in window]
    else:
        xvec = list(n.arange(0, npts))
    if (time is not None) and not isinstance(time, list):
        tme = list(time)
    if (time is not None) and (len(time) == npts):
        xvec = time
    elif time and (len(time) != npts):
        warnings.warn(
            "The length of 'time' [" + str(len(time)) + "] must be equal"
            "to the length of the defined window [" + str(len(xvec)) + ". A default vector is going to used."
        )

    return window, xvec
Esempio n. 11
0
def _manageFrequencies(f, split=None):
    """Manage frequency bands definition

    Parameters
    ----------
    f : list
        List containing the couple of frequency bands.
        Each couple can be either a list or a tuple.
        Ex: f = [(2,4),(5,7)]

    split : int or list of int, optional [def: None]
        Split the frequency band f in "split" band width.
        If f is a list which contain couple of frequencies,
        split is a list too.

    Returns
    ----------
    f : the modified list
    fSplit : the splitted f
    fSplitIndex : index of the splitted bands
    """
    if (len(f) == 2) and (type(f[0]) in [int, float]):
        f = [f]
    nf = len(f)
    if (split is None) or (type(split) == int):
        split = [split]
    if len(split) != nf:
        split = split*nf
    fSplit, fSplitIndex, lenOld = [], [], 0
    for k, i in enumerate(f):
        if split[k] is None:
            fSplit.append(f[k])
        elif type(split[k]) == int:
            fSplit.extend(binarize(i[0], i[1], split[k], split[k],
                          kind='tuple'))
        fSplitIndex.append([lenOld, len(fSplit)])
        lenOld = len(fSplit)
    return f, fSplit, fSplitIndex
Esempio n. 12
0
def _kl_hr(pha, amp, nbins):
    nPha, npts, nAmp = *pha.shape, amp.shape[0]
    step = 2 * np.pi / nbins
    vecbin = binarize(-np.pi, np.pi + step, step, step)
    if len(vecbin) > nbins:
        vecbin = vecbin[0:-1]

    abin = np.zeros((nAmp, nPha, nbins))
    for k, i in enumerate(vecbin):
        # Find where phase take vecbin values :
        pL, pC = np.where((pha >= i[0]) & (pha < i[1]))

        # Matrix to do amp x binMat :
        binMat = np.zeros((npts, nPha))
        binMat[pC, pL] = 1
        meanMat = np.matlib.repmat(binMat.sum(axis=0), nAmp, 1)
        meanMat[meanMat == 0] = 1

        # Multiply matrix :
        abin[:, :, k] = np.divide(np.dot(amp, binMat), meanMat)
    abinsum = np.array([abin.sum(axis=2) for k in range(nbins)])

    return abin, abinsum
Esempio n. 13
0
def _kl_hr(pha, amp, nbins):
    nPha, npts, nAmp = *pha.shape, amp.shape[0]
    step = 2 * np.pi / nbins
    vecbin = binarize(-np.pi, np.pi + step, step, step)
    if len(vecbin) > nbins:
        vecbin = vecbin[0:-1]

    abin = np.zeros((nAmp, nPha, nbins))
    for k, i in enumerate(vecbin):
        # Find where phase take vecbin values :
        pL, pC = np.where((pha >= i[0]) & (pha < i[1]))

        # Matrix to do amp x binMat :
        binMat = np.zeros((npts, nPha))
        binMat[pC, pL] = 1
        meanMat = np.matlib.repmat(binMat.sum(axis=0), nAmp, 1)
        meanMat[meanMat == 0] = 1

        # Multiply matrix :
        abin[:, :, k] = np.divide(np.dot(amp, binMat), meanMat)
    abinsum = np.array([abin.sum(axis=2) for k in range(nbins)])

    return abin, abinsum
Esempio n. 14
0
    def __init__(self, sf, npts, f=[2, 4], method='hilbert', cycle=3,
                 sample=None, time=None, **kwargs):
        # Check pha and amp methods:
        _checkref('pha_meth', method, ['hilbert', 'hilbert1', 'hilbert2'])

        # Check the type of f:
        if (len(f) == 4) and isinstance(f[0], (int, float)):
            f = binarize(f[0], f[1], f[2], f[3], kind='list')

        # Initialize PLV :
        _coupling.__init__(self, f, 'phase', method, cycle,
                           f, 'phase', method, cycle,
                           sf, npts, None, None, None, time, **kwargs)
        if time is None:
            time = np.arange(npts)
        else:
            time = time

        if sample is None:
            sample = slice(npts)
        self._sample = sample
        self.time = time[sample]
        del self.amp
Esempio n. 15
0
def _manageFrequencies(f, split=None):
    """Manage frequency bands definition

    Parameters
    ----------
    f : list
        List containing the couple of frequency bands.
        Each couple can be either a list or a tuple.
        Ex: f = [(2,4),(5,7)]

    split : int or list of int, optional [def: None]
        Split the frequency band f in "split" band width.
        If f is a list which contain couple of frequencies,
        split is a list too.

    Returns
    ----------
    f : the modified list
    fSplit : the splitted f
    fSplitIndex : index of the splitted bands
    """
    if (len(f) == 2) and (type(f[0]) in [int, float]):
        f = [f]
    nf = len(f)
    if (split is None) or (type(split) == int):
        split = [split]
    if len(split) != nf:
        split = split * nf
    fSplit, fSplitIndex, lenOld = [], [], 0
    for k, i in enumerate(f):
        if split[k] is None:
            fSplit.append(f[k])
        elif type(split[k]) == int:
            fSplit.extend(binarize(i[0], i[1], split[k], split[k], kind="tuple"))
        fSplitIndex.append([lenOld, len(fSplit)])
        lenOld = len(fSplit)
    return f, fSplit, fSplitIndex