Example #1
0
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)
Example #2
0
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)
Example #3
0
def minf_pyent(isp, osp, meth):
    x, xdim = _pyentcond(osp)
    y, ydim = _pyentcond(isp)
    ds = DiscreteSystem(x, xdim, y, ydim)
    calc = ["HX", "HXY", "HY"]
    if meth.endswith('_sh'):
        meth = meth[:-3]
        calc.extend(['HiXY', 'HshXY'])
    ds.calculate_entropies(meth, calc=calc)
    if len(calc) == 3:
        return (ds.I(), ds.H['HY'], ds.H['HX'])
    else:
        return (ds.Ish(), ds.H['HY'], ds.H["HX"])
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)
Example #5
0
def plot_calc(x,y,ax=None, xlim=[-4, 4], ylim=[-4,4]):
    if ax is None:
        f = plt.figure()
        ax = f.add_subplot(111)

    # correlation
    cor = np.corrcoef(x,y)[0,1]

    # information
    m = 8 
    qx = quantise(x, m, uniform='sampling')[0]
    qy = quantise(y, m, uniform='sampling')[0]
    s = DiscreteSystem(qx, (1,m), qy, (1,m))
    s.calculate_entropies(method='plugin', calc=['HX','HXY'])
    I = s.I()

    # p-values?

    Nplot = 500
    plotidx = np.random.permutation(len(x))[:Nplot]
    ax.scatter(x[plotidx],y[plotidx], s=5, edgecolors='none')
    #ax.set_title("r=%.1f  I=%.2f" % (cor, I))
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    for sp in ['left','right','bottom','top']:
        ax.spines[sp].set_color('none')
    ax.xaxis.set_ticks_position('none')
    ax.yaxis.set_ticks_position('none')
    ax.set_xticklabels([])
    if np.abs(cor)<1e-3:
        cor = 0.0
    if np.isnan(cor):
        cor = '-'
    else:
        cor = "%.2f"%cor
    ax.text(0.2, 1, cor, horizontalalignment='center', verticalalignment='bottom', 
            transform = ax.transAxes, color='#663300', fontsize=12, fontweight='bold')
    ax.text(0.8, 1, "%.2f"%I, horizontalalignment='center', verticalalignment='bottom', 
            transform = ax.transAxes, color='#b20000', fontsize=12, fontweight='bold')
    return ax
Example #6
0
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
Example #7
0
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)
Example #8
0
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)
Example #9
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()