Esempio n. 1
0
    def _fit(ts_set1, ts_set2, lb, ub):
        pts1 = create_ts_with_data(percent_change(ts_set1.data), ts_set1)
        pts2 = create_ts_with_data(percent_change(ts_set2.data), ts_set2)
        pts = concatenate_timeseries(pts1, pts2)

        #cts  = concatenate_timeseries(ts_set1, ts_set2)
        #pts  = tsu.percent_change    (cts.data)
        #pts  = create_ts_with_data   (pts, ts_set1)
        gc = nta.GrangerAnalyzer(pts, order=1)

        freq_idx_gc = np.where(
            (gc.frequencies > lb) * (gc.frequencies < ub))[0]

        mean_gc = np.mean(gc.causality_xy[:, :, freq_idx_gc], -1)
        return np.nanmean(mean_gc)
Esempio n. 2
0
def granger_causality_analysis(time_series, f_lb, f_ub, granger_order, roi_names,result_dir, s='', c=''):
    # initialize GrangerAnalyzer object
    G = nta.GrangerAnalyzer(time_series, order = granger_order)
    # initialize CoherenceAnalyzer 
    C = nta.CoherenceAnalyzer(time_series)
    # initialize CorrelationAnalyzer
    R = nta.CorrelationAnalyzer(time_series)
    # get the index of the frequency band of interest for different analyzer
    freq_idx_G = np.where((G.frequencies > f_lb) * (G.frequencies < f_ub))[0]
    freq_idx_C = np.where((C.frequencies> f_lb) * (C.frequencies < f_ub)) [0]
    # average the last dimension
    coh = np.mean(C.coherence[:, :, freq_idx_C], -1) 
    gl = np.mean(G.causality_xy[:, :, freq_idx_G], -1)
    # Difference in HRF between ROI may result misattriution of causality
    # examine diference between x-->y and y-->x
    g2 = np.mean(G.causality_xy[:,:,freq_idx_G] - G.causality_yx[:,:,freq_idx_G],-1)
    # Figure organization:
    # causality_xy: x-->y, or roi_names[0]-->roi_names[1], roi_names[0]-->roi_names[2], etc.
    # this makes: given a column, transverse through rows. Directionality is
    # from current column label to each row label
    # plot coherence from x to y, 
    drawmatrix_channels(coh, roi_names, size=[10., 10.], color_anchor=0)
    plt.title(('%s %s pair-wise Coherence' % (s, c)).replace('  ',' '))
    plt.savefig(os.path.join(result_dir,s,('%s_%s_pairwise_coherence.png' % (s, c)).replace('__','_')))
    # plot correlation from x to y
    #drawmatrix_channels(R.corrcoef, roi_names, size=[10., 10.], color_anchor=0)
    #plt.title(('%s %s pair-wise Correlation' % (s, c)).replace('  ',' '))
    #plt.savefig(os.path.join(result_dir,s,('%s_%s_pairwise_correlation.png' % (s, c)).replace('__','_')))
    # plot granger causality from x to y
    drawmatrix_channels(gl, roi_names, size=[10., 10.], color_anchor=0)
    plt.title(('%s %s pair-wise Granger Causality' % (s, c)).replace('  ',' '))
    plt.savefig(os.path.join(result_dir,s,('%s_%s_pairwise_granger_causality.png' % (s, c)).replace('__','_')))
    # plot granger causliaty forward-backward difference
    drawmatrix_channels(g2, roi_names, size=[10., 10.], color_anchor = 0)
    plt.title(('%s %s pair-wise Granger Causality Forward-Backward Difference' % (s, c)).replace('  ',' '))
    plt.savefig(os.path.join(result_dir,s,('%s_%s_granger_causality_forward_backward_diff.png' % (s, c)).replace('__','_')))
    # close all the figures
    plt.close("all")
    return(coh, gl, g2, G, C, R)
Esempio n. 3
0
def nitime_solution(animal, day, cutoff=True):
    # TODO: add support for custom freq bands
    import h5py
    import nitime
    import numpy as np
    import nitime.analysis as nta
    from utils_loading import encode_to_filename
    from nitime.viz import drawmatrix_channels
    import matplotlib.pyplot as plt
    folder = "/Volumes/DATA_01/NL/layerproject/processed/"
    hf = h5py.File(encode_to_filename(folder, animal, day), 'r')
    dff = np.array(hf['dff'])
    NEUR = 'ens'
    # Critical neuron pair vs general neuron gc
    if NEUR == 'ens':
        rois = dff[hf['ens_neur']]
    elif NEUR == 'neur':
        rois = dff[hf['nerden']]
    else:
        rois = dff[NEUR]

    rois_ts = nitime.TimeSeries(rois, sampling_interval=1 / hf.attrs['fr'])
    G = nta.GrangerAnalyzer(rois_ts)
    if cutoff:
        sel = np.where(G.frequencies < hf.attrs['fr'])[0]
        caus_xy = G.causality_xy[:, :, sel]
        caus_yx = G.causality_yx[:, :, sel]
        caus_sim = G.simultaneous_causality[:, :, sel]
    else:
        caus_xy = G.causality_xy
        caus_yx = G.causality_yx
        caus_sim = G.simultaneous_causality
    g1 = np.mean(caus_xy, -1)
    g2 = np.mean(caus_yx, -1)
    g3 = np.mean(caus_sim, -1)
    g4 = g1-g2
    fig03 = drawmatrix_channels(g1, ['E11', 'E12', 'E21', 'E22'], size=[10., 10.], color_anchor = 0)
    plt.colorbar()
def granger_scores(timeseries, order):
    timeseries = TimeSeries(timeseries, sampling_interval=1)
    g = nta.GrangerAnalyzer(timeseries, order=order)
    g_xy_mat = np.mean(g.causality_xy, axis=-1)
    g_yx_mat = np.mean(g.causality_yx, axis=-1)
    return np.concatenate([g_xy_mat[np.tril_indices(3,-1)], g_yx_mat.T[np.triu_indices(3,1)]])
Esempio n. 5
0
We normalize the data in each of the ROIs to be in units of % change and
initialize the TimeSeries object:

"""

pdata = tsu.percent_change(data)
time_series = ts.TimeSeries(pdata, sampling_interval=TR)
"""

We initialize the GrangerAnalyzer object, while specifying the order of the
autoregressive model to be 1 (predict the current behavior of the time-series
based on one time-point back).

"""

G = nta.GrangerAnalyzer(time_series, order=1)
"""

For comparison, we also initialize a CoherenceAnalyzer and a
CorrelationAnalyzer, with the same TimeSeries object:

"""

C1 = nta.CoherenceAnalyzer(time_series)
C2 = nta.CorrelationAnalyzer(time_series)
"""

We are only interested in the physiologically relevant frequency band
(approximately 0.02 to 0.15 Hz).

The spectral resolution is different in these two different analyzers. In the
Esempio n. 6
0
import matplotlib.pyplot as plt

A = np.vstack((a1, a2))
plt.plot(A.T);
plt.legend(['a', 'b'])
plt.show()
import h5py
import nitime
import numpy as np
import nitime.analysis as nta
from utils_loading import encode_to_filename
from nitime.viz import drawmatrix_channels

rois = A
rois_ts = nitime.TimeSeries(rois, sampling_interval=1 / 5)
G = nta.GrangerAnalyzer(rois_ts, order=1)
if cutoff:
    sel = np.where(G.frequencies < hf.attrs['fr'])[0]
    caus_xy = G.causality_xy[:, :, sel]
    caus_yx = G.causality_yx[:, :, sel]
else:
    caus_xy = G.causality_xy
    caus_yx = G.causality_yx
cutoff = False
G = nta.GrangerAnalyzer(rois_ts, order=1)
if cutoff:
    sel = np.where(G.frequencies < hf.attrs['fr'])[0]
    caus_xy = G.causality_xy[:, :, sel]
    caus_yx = G.causality_yx[:, :, sel]
else:
    caus_xy = G.causality_xy