pwollstadt / IDTxl

The Information Dynamics Toolkit xl (IDTxl) is a comprehensive software package for efficient inference of networks and their node dynamics from multivariate time series data using information theory.
http://pwollstadt.github.io/IDTxl/
GNU General Public License v3.0
249 stars 76 forks source link

BivariateMI does not work with zero lag #20

Closed aleksejs-fomins closed 6 years ago

aleksejs-fomins commented 6 years ago

I wrote a test code creating a random distribution for process 1, and cycling it by a certain number of steps for process 2. Using the following settings

settings = {'cmi_estimator': 'JidtGaussianCMI', 'max_lag_sources': 5, 'min_lag_sources': 0, 'verbose' : False}

The algorithm finds connection between two processes for lags 1-5, but does not find a connection for lag=0.

pwollstadt commented 6 years ago

Hi @aleksejs-fomins, I pieced together a little example script that finds the MI between two correlated Gaussian processes with zero lag:

import numpy as np
from idtxl.data import Data
from idtxl.bivariate_mi import BivariateMI

covariance = 0.4
n = 1000
source = np.random.randn(n)
target = (1 - covariance) * np.random.randn(n) + covariance * source

data = Data(np.vstack((source, target)),
            dim_order='ps', normalise=False)
settings = {
    'cmi_estimator': 'JidtKraskovCMI',
    'n_perm_max_stat': 21,
    'n_perm_min_stat': 21,
    'n_perm_max_seq': 21,
    'n_perm_omnibus': 21,
    'max_lag_sources': 0,
    'min_lag_sources': 0}
nw = BivariateMI()
results = nw.analyse_single_target(
    settings, data, target=1, sources='all')

print(results.get_single_target(1, fdr=False))
# the following should return [(0, 0)]
print(results.get_single_target(1, fdr=False).selected_vars_sources)

This returns a significant MI at lag 0 for me. Could you try this as well? Also, could you post some code and tell me what version/branch of the toolbox you are using?

aleksejs-fomins commented 6 years ago

Hi @pwollstadt, thanks for your reply. I have pulled master on 5th Nov (Head at 4b1b4a4466daac1f7217bc2d3f5ca8ae59aa4ca6).

I have tried running your code, and it does say "significant" in the output. But when I try to print or plot the connections, it states that "No significant links found in the network"

results.print_edge_list(weights='max_te_lag', fdr=False)
plot_network(results=results, weights='max_te_lag', fdr=False)
plt.show()

Here is the code I have mentioned in the previous post

import numpy as np

# Import classes
from idtxl.bivariate_mi import BivariateMI
from idtxl.data import Data
from idtxl.visualise_graph import plot_network
import matplotlib.pyplot as plt

def cycle(data, n):
    if n == 0:
        return data
    else:
        return np.hstack((data[n:], data[:n]))

# TEST 1: Random data
NDATA = 1000

dataNP = np.zeros((2, NDATA))
dataNP[0,:] = np.random.normal(0, 1, NDATA)
dataNP[1,:] = cycle(2*np.copy(dataNP[0,:]) + np.random.normal(0, 1, NDATA), 0)
data = Data(dataNP, dim_order='ps')

# b) Initialise analysis object and define settings
network_analysis = BivariateMI()
settings = {'cmi_estimator': 'JidtGaussianCMI',
            'max_lag_sources': 0,
            'min_lag_sources': 0,
           'verbose' : False}

# c) Run analysis
results = network_analysis.analyse_network(settings=settings, data=data)
for i in range(2):
    rezThis = results.get_single_target(i, fdr=False)
    print("For var", i, "sources", rezThis['sources_tested'], 'mi', rezThis['mi'], 'p', rezThis['selected_sources_pval'])

# d) Plot inferred network to console and via matplotlib
results.print_edge_list(weights='max_te_lag', fdr=False)
plot_network(results=results, weights='max_te_lag', fdr=False)
plt.show()
pwollstadt commented 6 years ago

Hi @aleksejs-fomins, there is a little bug in the plotting function that causes trouble if one tries to plot the maximum TE lag for results with lags of zero (if you switch the weights parameter in your call to plot_network to 'binary', you get a network plot). I am working on a fix for this, I will let you know as soon as I have uploaded it.

pwollstadt commented 6 years ago

I fixed the problem of plotting zero-lag interactions in https://github.com/pwollstadt/IDTxl/commit/96a179f82f181c3c772860ecf3397ed5a6d91af7. Have a look and let me know if you encounter any more issues. I will close this issue, feel free to reopen if you find anything else.