コード例 #1
0
def getMutualInfoFromPair(pair, exp_frame):
    first_response = exp_frame[exp_frame.cell_id == pair[0]]['num_spikes']
    second_response = exp_frame[exp_frame.cell_id == pair[1]]['num_spikes']
    first_response_dims = [1, 1 + first_response.max()]
    second_response_dims = [1, 1 + second_response.max()]
    discrete_system = DiscreteSystem(first_response, first_response_dims,
                                     second_response, second_response_dims)
    return calcInfo(discrete_system)
コード例 #2
0
ファイル: test_systems.py プロジェクト: w-klijn/pyentropy
def do_1d_check(method, qe_method):
    xc = x.copy()
    yc = y.copy()
    s = DiscreteSystem(xc, (1, 10), yc, (1, 10))
    # calculate all entropies
    s.calculate_entropies(method=method, calc=allcalc, qe_method=qe_method)
    # check output assinged
    assert_(s.H == getattr(s, 'H_%s' % method.replace('-', '')))
    v = np.array([s.H[k] for k in allcalc])
    assert_array_almost_equal(v, alltrue, decimal=2)
    # check didn't do something nasty to inputs
    assert_array_equal(x, xc)
    assert_array_equal(y, yc)
コード例 #3
0
ファイル: timeseries.py プロジェクト: shayezkarim/umcp
def mask_mutualinfo_matrix(nifti_file,
                           masks,
                           outfile,
                           mask_thresh=0,
                           nbins=10):
    """
    Calculates mutual information matrix for a set of mask mean timeseries'
    """
    from pyentropy import DiscreteSystem
    mutualinfo_mat = np.zeros((len(masks), len(masks)))
    input = nib.load(nifti_file)
    input_d = input.get_data()
    if len(input.shape) > 3:
        ts_length = input.shape[3]
    else:
        ts_length = 1
    mean_bin_ts_array = np.zeros((len(masks), ts_length), dtype='int')
    for count, mask in enumerate(masks):
        mask_coords = core.get_nonzero_coords(mask, mask_thresh)
        mask_array = [
            input_d[mask_coord[0], mask_coord[1], mask_coord[2], :]
            for mask_coord in mask_coords
        ]
        mean_ts = np.mean(mask_array, axis=0)
        l = np.linspace(min(mean_ts), max(mean_ts), nbins)
        mean_bin_ts_array[count, :] = np.digitize(
            mean_ts, l) - 1  # set range to start at 0
        if count > 0:
            for prev in range(count):
                sys = DiscreteSystem(mean_bin_ts_array[count], (1, nbins),
                                     mean_bin_ts_array[prev], (1, nbins))
                sys.calculate_entropies(method='qe',
                                        calc=['HX', 'HXY', 'HiXY', 'HshXY'])
                mutualinfo_mat[count, prev] = sys.I()
    mutualinfo_mat_sym = core.symmetrize_mat(mutualinfo_mat, 'bottom')
    np.savetxt('%s.txt' % outfile, mutualinfo_mat_sym)
    return mutualinfo_mat_sym
コード例 #4
0
ファイル: test_systems.py プロジェクト: w-klijn/pyentropy
def test_toy1():
    s = DiscreteSystem(x, (3, 3), y, (1, 2))
    s.calculate_entropies(method='plugin', calc=toycalc)
    v = np.array([s.H[t] for t in toycalc])
    assert_array_almost_equal(v, toytrue)
コード例 #5
0
#!/usr/bin/env python
import numpy as np
import pyentropy
from pyentropy import DiscreteSystem
import matplotlib.pyplot as plt

N = 10
Nq = 50
Nx = 1
noise = 1
ntrials = 10
for trial in xrange(ntrials):
    x = N * np.random.rand(Nx, 10000)
    y = np.zeros(10000)
    xq = np.empty((Nx, 10000), dtype=int)
    for n in range(Nx):
        xq[n, :] , _, _ = pyentropy.quantise(x[n, :], Nq)
        y += x[n, :]
    y += noise*np.random.randn(10000)
    yq, _, _ = pyentropy.quantise(y, Nq)

    s = DiscreteSystem(xq, (Nx, Nq), yq, (1, Nq))
    s.calculate_entropies(method='plugin', calc=['HX', 'HXY'])
    print(s.I())
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xq, yq, s=2)
    ax.set_title('MI: %s' % s.I())
    fig.savefig('mi_%d.png' % trial)
    plt.close()