示例#1
0
def test_icoherence():
    data = np.load("../examples/data/eeg_32chans_10secs.npy")

    csdparams = {'NFFT': 256, 'noverlap': 256 / 2.0}

    icoh = icoherence(data, [1.0, 4.0], 128.0, **csdparams)

    expected = np.load("data/test_icoherence.npy")
    np.testing.assert_array_equal(icoh, expected)
示例#2
0
def _get_fc_graph(x, band_freq, sampling_freq, fc_measure='corr',
                  link_cutoff=0., band_freq_hi=(20., 45.), nfft=128,
                  n_overlap=64):
    """
    Build a functional connectivity network from the given data stream.
    :param x: numpy array of shape (n_channels, n_samples);
    :param band_freq: list with two elements, the band in which to estimate FC;
    :param sampling_freq: float, sampling frequency of the stream;
    :param fc_measure: functional connectivity measure to use;
    :param link_cutoff: links with absolute FC measure below this value will be
      removed;
    :param band_freq_hi: high band used to estimate FC when using 'aec';
    :param nfft: TODO, affects 'wpli' and 'dwpli';
    :param n_overlap: TODO, affects 'wpli' and 'dwpli';
    :return: FC graph in numpy format (note that node features are all ones).
    """
    if fc_measure == 'iplv':
        _, ef = fc.iplv(x, band_freq, sampling_freq)
    elif fc_measure == 'icoh':
        ef = fc.icoherence(x, band_freq, sampling_freq)
    elif fc_measure == 'corr':
        ef = fc.corr(x, band_freq, sampling_freq)
    elif fc_measure == 'aec':
        ef = fc.aec(x, band_freq, band_freq_hi, sampling_freq)
    elif fc_measure == 'wpli':
        csdparams = {'NFFT': nfft, 'noverlap': n_overlap}
        ef = fc.wpli(x, band_freq, sampling_freq, **csdparams)
        ef = np.abs(ef)
    elif fc_measure == 'dwpli':
        csdparams = {'NFFT': nfft, 'noverlap': n_overlap}
        ef = fc.dwpli(x, band_freq, sampling_freq, **csdparams)
        ef = np.abs(ef)
    elif fc_measure == 'dpli':
        ef = fc.dpli(x, band_freq, sampling_freq)
    else:
        raise ValueError('Invalid fc_measure')

    # Set the main diagonal to zero (no self-loops)
    np.fill_diagonal(ef, 0.0)

    # Compute adjacency matrix by rounding to 0 and 1 based on the cutoff
    adj = ef.copy()
    if link_cutoff != 0:
        adj[np.abs(adj) >= link_cutoff] = 1
        adj[np.abs(adj) < link_cutoff] = 0
    else:
        adj[...] = 1

    # Dummy node features
    # TODO: proper nf
    nf = np.ones((ef.shape[0], 1))

    # Edge features
    ef = ef[..., None]

    return adj, nf, ef
示例#3
0
def _get_fc_graph(x,
                  fc_measure,
                  nf_mode,
                  band_freq=None,
                  band_freq_hi=None,
                  sampling_freq=None,
                  nfft=None,
                  n_overlap=None):
    if fc_measure == 'iplv':
        _, ef = fc.iplv(x, band_freq, sampling_freq)
    elif fc_measure == 'icoh':
        ef = fc.icoherence(x, band_freq, sampling_freq)
    elif fc_measure == 'corr':
        ef = fc.corr(x, band_freq, sampling_freq)
    elif fc_measure == 'aec':
        ef = fc.aec(x, band_freq, band_freq_hi, sampling_freq)
    elif fc_measure == 'wpli':
        csdparams = {'NFFT': nfft, 'noverlap': n_overlap}
        ef = fc.wpli(x, band_freq, sampling_freq, **csdparams)
    elif fc_measure == 'dwpli':
        csdparams = {'NFFT': nfft, 'noverlap': n_overlap}
        ef = fc.dwpli(x, band_freq, sampling_freq, **csdparams)
    elif fc_measure == 'dpli':
        ef = fc.dpli(x, band_freq, sampling_freq)
    else:
        raise ValueError('Invalid fc_measure ' + fc_measure)

    # Dummy node features
    if nf_mode == 'full':
        nf = x.copy()
    elif nf_mode == 'mean':
        nf = np.mean(x, -1)
    elif nf_mode == 'energy':
        nf = np.sum(x**2, -1)
    elif nf_mode == 'power':
        nf = np.sum(x**2, -1) / x.shape[-1]
    elif nf_mode == 'ones':
        nf = np.ones((ef.shape[0], 1))
    else:
        raise ValueError('Invalid nf_mode' + nf_mode)
    return nf, ef
示例#4
0
def _get_fc_graph(x,
                  band_freq,
                  sampling_freq,
                  fc_measure='corr',
                  link_cutoff=0.,
                  band_freq_hi=(20., 45.),
                  nfft=128,
                  n_overlap=64,
                  nf_mode='mean',
                  self_loops=True):
    """
    Build a functional connectivity network from the given data stream.
    :param x: numpy array of shape (n_channels, n_samples);
    :param band_freq: list with two elements, the band in which to estimate FC;
    :param sampling_freq: float, sampling frequency of the stream;
    :param fc_measure: functional connectivity measure to use. Possible measures
    are: iplv, icoh, corr, aec, wpli, dwpli, dpli (see documentation of
    Dyfunconn);
    :param link_cutoff: links with absolute FC measure below this value will be
      removed;
    :param band_freq_hi: high band used to estimate FC when using 'aec';
    :param nfft: TODO, affects 'wpli' and 'dwpli';
    :param n_overlap: TODO, affects 'wpli' and 'dwpli';
    :param nf_mode: how to compute node features. Possible modes are: full,
    mean, energy, ones.
    :param self_loops: add self loops to FC network;
    :return: FC graph in numpy format (note that node features are all ones).
    """
    if fc_measure == 'iplv':
        _, ef = fc.iplv(x, band_freq, sampling_freq)
    elif fc_measure == 'icoh':
        ef = fc.icoherence(x, band_freq, sampling_freq)
    elif fc_measure == 'corr':
        ef = fc.corr(x, band_freq, sampling_freq)
    elif fc_measure == 'aec':
        ef = fc.aec(x, band_freq, band_freq_hi, sampling_freq)
    elif fc_measure == 'wpli':
        csdparams = {'NFFT': nfft, 'noverlap': n_overlap}
        ef = fc.wpli(x, band_freq, sampling_freq, **csdparams)
        ef = np.abs(ef)
    elif fc_measure == 'dwpli':
        csdparams = {'NFFT': nfft, 'noverlap': n_overlap}
        ef = fc.dwpli(x, band_freq, sampling_freq, **csdparams)
        ef = np.abs(ef)
    elif fc_measure == 'dpli':
        ef = fc.dpli(x, band_freq, sampling_freq)
    else:
        raise ValueError('Invalid fc_measure {}'.format(fc_measure))

    # Compute adjacency matrix by rounding to 0 and 1 based on the cutoff
    adj = ef.copy()
    if link_cutoff != 0:
        adj[np.abs(adj) >= link_cutoff] = 1
        adj[np.abs(adj) < link_cutoff] = 0
    else:
        adj[...] = 1

    if self_loops:
        # Set the main diagonal to zero
        np.fill_diagonal(adj, 1.0)
    else:
        np.fill_diagonal(ef, 0.0)
        np.fill_diagonal(adj, 1.0)

    # Dummy node features
    if nf_mode == 'full':
        nf = x.copy()
    elif nf_mode == 'mean':
        nf = np.mean(x, -1)
    elif nf_mode == 'energy':
        nf = np.sum(x**2, -1)
    elif nf_mode == 'ones':
        nf = np.ones((ef.shape[0], 1))
    else:
        raise ValueError('Invalid nf_mode {}'.format(nf_mode))
    # Edge features
    ef = ef[..., None]

    return adj, nf, ef
示例#5
0
# -*- coding: utf-8 -*-
# Author: Avraam Marimpis <*****@*****.**>

import numpy as np
np.set_printoptions(precision=3, linewidth=256)

from dyfunconn.fc import coherence, icoherence

if __name__ == "__main__":
    data = np.load(
        "/home/makism/Github/dyfunconn/examples/data/eeg_32chans_10secs.npy")
    data = data[0:5, :]

    csdparams = {'NFFT': 256, 'noverlap': 256 / 2.0}

    coh = coherence(data, [1.0, 4.0], 128.0, **csdparams)
    icoh = icoherence(data, [1.0, 4.0], 128.0)

    print "Coherence: \n", coh
    print "Imagenary Coherence: \n", icoh