Beispiel #1
0
    def test_randomise_spikes_output_decimals(self):
        st = neo.SpikeTrain([90, 150, 180, 350] * pq.ms, t_stop=500 * pq.ms)

        nr_surr = 2
        surrs = surr.randomise_spikes(st, n=nr_surr, decimals=3)

        for surrog in surrs:
            for i in range(len(surrog)):
                self.assertNotEqual(surrog[i] - int(surrog[i]) * pq.ms,
                                    surrog[i] - surrog[i])
Beispiel #2
0
    def test_randomise_spikes_output_decimals(self):
        spiketrain = neo.SpikeTrain([90, 150, 180, 350] * pq.ms,
                                    t_stop=500 * pq.ms)

        n_surrogates = 2
        surrogate_trains = surr.randomise_spikes(spiketrain,
                                                 n_surrogates=n_surrogates,
                                                 decimals=3)

        for surrogate_train in surrogate_trains:
            for i in range(len(surrogate_train)):
                self.assertNotEqual(
                    surrogate_train[i] - int(surrogate_train[i]) * pq.ms,
                    surrogate_train[i] - surrogate_train[i])
Beispiel #3
0
    def test_randomise_spikes_output_format(self):

        st = neo.SpikeTrain([90, 150, 180, 350] * pq.ms, t_stop=500 * pq.ms)

        nr_surr = 2
        surrs = surr.randomise_spikes(st, n=nr_surr)

        self.assertIsInstance(surrs, list)
        self.assertEqual(len(surrs), nr_surr)

        for surrog in surrs:
            self.assertIsInstance(surrs[0], neo.SpikeTrain)
            self.assertEqual(surrog.units, st.units)
            self.assertEqual(surrog.t_start, st.t_start)
            self.assertEqual(surrog.t_stop, st.t_stop)
            self.assertEqual(len(surrog), len(st))
    def test_randomise_spikes_output_format(self):

        spiketrain = neo.SpikeTrain([90, 150, 180, 350] * pq.ms,
                                    t_stop=500 * pq.ms)

        n_surrogates = 2
        surrogate_trains = surr.randomise_spikes(spiketrain, n=n_surrogates)

        self.assertIsInstance(surrogate_trains, list)
        self.assertEqual(len(surrogate_trains), n_surrogates)

        self.assertIsInstance(surrogate_trains[0], neo.SpikeTrain)
        for surrogate_train in surrogate_trains:
            self.assertEqual(surrogate_train.units, spiketrain.units)
            self.assertEqual(surrogate_train.t_start, spiketrain.t_start)
            self.assertEqual(surrogate_train.t_stop, spiketrain.t_stop)
            self.assertEqual(len(surrogate_train), len(spiketrain))
Beispiel #5
0
    def test_randomise_spikes_empty_train(self):

        st = neo.SpikeTrain([] * pq.ms, t_stop=500 * pq.ms)

        surrog = surr.randomise_spikes(st, n=1)[0]
        self.assertEqual(len(surrog), 0)
Beispiel #6
0
    def test_randomise_spikes_empty_train(self):

        spiketrain = neo.SpikeTrain([] * pq.ms, t_stop=500 * pq.ms)

        surrogate_train = surr.randomise_spikes(spiketrain, n_surrogates=1)[0]
        self.assertEqual(len(surrogate_train), 0)
Beispiel #7
0
def complexity(all_data, color, trial, bin_size, save_figures=True):
    """
    Give all_data, color as a string, trial as a scalar, bins as a scalar, default is save_figures = True.
    Plots: raster plot, spike counts per bin (in ms), the complexity distribution for that bin size,
    complexity distribution for data vs. surrogates, then substracts the two from each other. Finally, three plots
    are created of complexity distributions for data, surrogates, and their difference, scanning across bin sizes
    from 0 to 30 ms.
    """

    #     if color=='blue':
    #         color_map = 'Blues'
    #     if color=='green':
    #         color_map = 'Greens'
    #     if color=='grey':
    #         color_map = 'Greys'
    #     if color=='orange':
    #         color_map = 'Oranges'
    #     if color=='purple':
    #         color_map = 'Purples'
    #     if color=='red':
    #         color_map = 'Reds'

    # block = ut.load_dataset(color)
    block = all_data[color]['neo_block']
    train = block.segments[trial].spiketrains  # segments is trials

    binsize = bin_size * pq.ms
    pophist = stats.time_histogram(train, binsize, binary=True)
    complexity_cpp = stats.complexity_pdf(train, binsize)
    # Plot the results
    # plt.xkcd()
    fig = plt.figure(figsize=(12, 5))
    fig.subplots_adjust(top=.92, bottom=.05, hspace=.5, wspace=.2)
    misc.add_raster(fig,
                    2,
                    2,
                    1,
                    train,
                    ms=1,
                    xlabel='time',
                    ylabel='neuron id')
    misc.add_hist(fig,
                  2,
                  2,
                  3,
                  pophist.times,
                  pophist,
                  pophist.sampling_period,
                  xlabel='time',
                  ylabel='counts')
    ylim = plt.gca().get_ylim()
    plt.subplot(1, 2, 2)
    # plt.bar(complexity_cpp.times, list(A) + [0]*(n-assembly_size), color='r', label='amplitude distrib.')
    #plt.bar(range(len(A)), A, color='r', label='amplitude distrib.')
    plt.plot(complexity_cpp.times,
             complexity_cpp,
             label='complexity distrib.',
             color=color)
    plt.ylim([0, 0.25])
    plt.xlim([0, 30])
    plt.xlabel('complexity', size=12)
    plt.ylabel('probability', size=12)
    plt.suptitle('train', size=14)
    plt.legend()
    if save_figures:
        complexity_png = 'ATplots/complexity_bin' + str(
            bin_size) + '_color_' + str(color) + '.png'
        complexity_pdf = 'ATplots/complexity_bin' + str(
            bin_size) + '_color_' + str(color) + '.pdf'
        plt.savefig(complexity_png)
        plt.savefig(complexity_pdf)

    # generation of surrogates
    surr_sts = []

    for st in train:
        surr_sts.append(surr.randomise_spikes(st)[0])

    # Computation of the Complexity Distributions
    complexity_surr = stats.complexity_pdf(surr_sts, binsize)
    diff_complexity = complexity_cpp - complexity_surr

    # Plot the difference of the complexity distributions of the correlated and independent CPP
    plt.figure(figsize=(12, 4))
    plt.subplot(1, 2, 1)
    plt.plot(complexity_cpp.times, complexity_cpp, color=color, label="corr'd")
    plt.plot(complexity_surr.times,
             complexity_surr,
             color=color,
             label="surrogate",
             ls=':')
    plt.xlabel('complexity')
    plt.xlim([0, 30])
    plt.ylabel('probability')
    plt.legend()

    plt.subplot(1, 2, 2)
    plt.plot(complexity_cpp.times, diff_complexity, color=color)
    plt.xlabel('complexity')
    plt.xlim([0, 30])
    plt.ylabel('probability diff.')
    if save_figures:
        complexity_surr_png = 'ATplots/complexity_surr' + str(
            bin_size) + '_color_' + str(color) + '.png'
        complexity_surr_pdf = 'ATplots/complexity_surr' + str(
            bin_size) + '_color_' + str(color) + '.pdf'
        plt.savefig(complexity_surr_png)
        plt.savefig(complexity_surr_pdf)

    plt.show()

    # computation of the complexity distributions of CPP and surrogates for different binsizes
    # and storing the result in matrices
    binsizes = range(1, 31) * pq.ms
    max2 = []
    complexity_cpp_matrix = stats.complexity_pdf(train, binsizes[0]).magnitude
    complexity_surr_matrix = stats.complexity_pdf(surr_sts,
                                                  binsizes[0]).magnitude
    diff_complexity_matrix = complexity_cpp_matrix - complexity_surr_matrix
    max2.append(
        numpy.argmax(diff_complexity.magnitude[numpy.argmin(diff_complexity.
                                                            magnitude):]) +
        numpy.argmin(diff_complexity.magnitude))

    for i, h in enumerate(binsizes[1:]):
        complexity_cpp = stats.complexity_pdf(train, h)
        complexity_surr = stats.complexity_pdf(surr_sts, h)
        diff_complexity = complexity_cpp - complexity_surr
        max2.append(
            numpy.argmax(diff_complexity.
                         magnitude[numpy.argmin(diff_complexity.magnitude):]) +
            numpy.argmin(diff_complexity.magnitude))

        complexity_cpp_matrix = numpy.hstack(
            (complexity_cpp_matrix, complexity_cpp.magnitude))
        complexity_surr_matrix = numpy.hstack(
            (complexity_surr_matrix, complexity_surr.magnitude))
        diff_complexity_matrix = numpy.hstack(
            (diff_complexity_matrix, diff_complexity.magnitude))

    # Plot the complexity matrices
    inch2cm = 0.3937
    scale = 2  # figure scale; '1' creates a 8x10 cm figure (half a page width)
    label_size = 6 + 1 * scale
    text_size = 8 + 1 * scale
    tick_size = 4 + 1 * scale
    plt.figure(figsize=(8 * scale * inch2cm, 12 * scale * inch2cm),
               dpi=100 * scale)
    plt.subplots_adjust(right=.97,
                        top=.95,
                        bottom=.2 - .08 * scale,
                        hspace=.55)

    plt.subplot(3, 1, 1)
    plt.title('CPP complexity')
    plt.pcolor(complexity_cpp_matrix.T)
    plt.colorbar()
    plt.tick_params(length=2, direction='out', pad=0)
    plt.yticks(binsizes[0:-1:3].magnitude)
    plt.ylabel('Binsize')
    plt.xlabel('Complexity')
    #plt.xlim([0,complexity_cpp_matrix.T.shape[1]])
    plt.xlim([0, 30])
    plt.ylim([0, complexity_cpp_matrix.T.shape[0]])
    plt.ylim([binsizes[0], binsizes[-1]])

    plt.subplot(3, 1, 2)
    plt.title('Surrogate complexity')
    plt.pcolor(complexity_surr_matrix.T)
    plt.colorbar()
    plt.ylabel('Binsize')
    plt.xlabel('Complexity')
    plt.tick_params(length=2, direction='out', pad=0)
    plt.yticks(binsizes[0:-1:3].magnitude)
    #plt.xlim([0,complexity_cpp_matrix.T.shape[1]])
    plt.xlim([0, 30])
    plt.ylim([0, complexity_cpp_matrix.T.shape[0]])
    plt.ylim([binsizes[0], binsizes[-1]])

    plt.subplot(3, 1, 3)
    plt.title('Difference of complexities')
    plt.pcolor(diff_complexity_matrix.T)
    plt.colorbar()
    plt.plot(max2, binsizes, 'm')
    plt.ylabel('Binsize')
    plt.xlabel('Complexity')
    #plt.yticks(binsizes[0:-1:3].magnitude)
    plt.tick_params(length=2, direction='out', pad=0)
    #plt.xlim([0,complexity_cpp_matrix.T.shape[1]])
    plt.xlim([0, 30])
    plt.ylim([0, complexity_cpp_matrix.T.shape[0]])
    plt.ylim([binsizes[0], binsizes[-1]])

    if save_figures:
        sliding_bin_png = 'ATplots/complexity_sliding_bin_' + str(
            color) + '.png'
        sliding_bin_pdf = 'ATplots/complexity_sliding_bin_' + str(
            color) + '.pdf'
        plt.savefig(sliding_bin_png)
        plt.savefig(sliding_bin_pdf)

    plt.show()