Пример #1
0
def spike_statistics(idx, row):
    from elephant.statistics import mean_firing_rate, cv, isi
    from elephant.conversion import BinnedSpikeTrain
    from elephant.spike_train_correlation import corrcoef

    print(idx)
    results = {}

    # read spike trains from file
    io = get_io(row["output_file"])
    data_block = io.read()[0]
    spiketrains = data_block.segments[0].spiketrains

    # calculate mean firing rate
    results["spike_counts"] = sum(st.size for st in spiketrains)
    rates = [mean_firing_rate(st) for st in spiketrains]
    results["firing_rate"] = Quantity(rates, units=rates[0].units).rescale("1/s").mean()

    # calculate coefficient of variation of the inter-spike interval
    cvs = [cv(isi(st)) for st in spiketrains if st.size > 1]
    if len(cvs) > 0:
        results["cv_isi"] = sum(cvs)/len(cvs)
    else:
        results["cv_isi"] = 0

    # calculate global cross-correlation
    #cc_matrix = corrcoef(BinnedSpikeTrain(spiketrains, binsize=5*ms))
    #results["cc_min"] = cc_matrix.min()
    #results["cc_max"] = cc_matrix.max()
    #results["cc_mean"] = cc_matrix.mean()

    io.close()
    return results
Пример #2
0
    def test_statistics(self):
        # There is a statistical test and has a non-zero chance of failure during normal operation.
        # Re-run the test to see if the error persists.
        for rate in [123.0*Hz, 0.123*kHz]:
            for t_stop in [2345*ms, 2.345*second]:
                spiketrain = stgen.homogeneous_poisson_process(rate, t_stop=t_stop)
                intervals = isi(spiketrain)

                expected_spike_count = int((rate * t_stop).simplified)
                self.assertLess(pdiff(expected_spike_count, spiketrain.size), 0.2)  # should fail about 1 time in 1000

                expected_mean_isi = (1/rate)
                self.assertLess(pdiff(expected_mean_isi, intervals.mean()), 0.2)

                expected_first_spike = 0*ms
                self.assertLess(spiketrain[0] - expected_first_spike, 7*expected_mean_isi)

                expected_last_spike = t_stop
                self.assertLess(expected_last_spike - spiketrain[-1], 7*expected_mean_isi)

                # Kolmogorov-Smirnov test
                D, p = kstest(intervals.rescale(t_stop.units),
                              "expon",
                              args=(0, expected_mean_isi.rescale(t_stop.units)),  # args are (loc, scale)
                              alternative='two-sided')
                self.assertGreater(p, 0.001)
                self.assertLess(D, 0.12)
def analysis_quality(data, timestamp, **options):
    data_pop = data.segments[0].spiketrains
    g = open("%s.json" % BENCHMARK_NAME, 'r')
    d = json.load(g)
    N = d['param']['N']
    max_rate = d['param']['max_rate']
    delta_rate = max_rate/N
    tstop = d['param']['tstop']
    mean_intervals = 1000.0/numpy.linspace(delta_rate, max_rate, N)
    isi_distributions = []
    for spiketrain in data_pop:
        isi_distributions.append(isi(spiketrain))
        print spiketrain.annotations

    if options['plot_figure']:
         for i, (distr, expected_mean_interval) in enumerate(zip(isi_distributions, mean_intervals)[:8]):
            plt.subplot(4, 2, i + 1)
            counts, bins, _ = plt.hist(distr, bins=50)
            emi = expected_mean_interval
            plt.plot(bins, emi * distr.size * (numpy.exp(-bins[0]/emi) - numpy.exp(-bins[1]/emi)) * expon.pdf(bins, scale=emi), 'r-')
         plt.savefig("results/%s/spike_train_statistics.png" % timestamp)

    p_values = numpy.zeros((N,))
    for i, (distr, expected_mean_interval) in enumerate(zip(isi_distributions, mean_intervals)):
        D, p = kstest(distr, "expon", args=(0, expected_mean_interval),  # args are (loc, scale)
                      alternative='two-sided')
        p_values[i] = p
        print expected_mean_interval, distr.mean(), D, p, distr.size
    # Should we use the D statistic or the p-value as the benchmark?
    # note that D --> 0 is better, p --> 1 is better (but p > 0.01 should be ok, I guess?)
    # D is less variable, but depends on N.
    # taking the minimum p-value means we're more likely to get a false "significantly different" result.
    return {'type':'quality', 'name': 'kolmogorov_smirnov',
            'measure': 'min-p-value', 'value': p_values.min()}
Пример #4
0
    def test_statistics(self):
        # This is a statistical test that has a non-zero chance of failure
        # during normal operation. Thus, we set the random seed to a value that
        # creates a realization passing the test.
        np.random.seed(seed=12345)

        for rate in [self.rate_profile, self.rate_profile.rescale(kHz)]:
            spiketrain = stgen.inhomogeneous_poisson_process(rate)
            intervals = isi(spiketrain)

            # Computing expected statistics and percentiles
            expected_spike_count = (np.sum(
                rate) * rate.sampling_period).simplified
            percentile_count = poisson.ppf(.999, expected_spike_count)
            expected_min_isi = (1 / np.min(rate))
            expected_max_isi = (1 / np.max(rate))
            percentile_min_isi = expon.ppf(.999, expected_min_isi)
            percentile_max_isi = expon.ppf(.999, expected_max_isi)

            # Testing (each should fail 1 every 1000 times)
            self.assertLess(spiketrain.size, percentile_count)
            self.assertLess(np.min(intervals), percentile_min_isi)
            self.assertLess(np.max(intervals), percentile_max_isi)

            # Testing t_start t_stop
            self.assertEqual(rate.t_stop, spiketrain.t_stop)
            self.assertEqual(rate.t_start, spiketrain.t_start)

        # Testing type
        spiketrain_as_array = stgen.inhomogeneous_poisson_process(
            rate, as_array=True)
        self.assertTrue(isinstance(spiketrain_as_array, np.ndarray))
        self.assertTrue(isinstance(spiketrain, neo.SpikeTrain))
Пример #5
0
    def test_statistics(self):
        # There is a statistical test and has a non-zero chance of failure during normal operation.
        # Re-run the test to see if the error persists.
        a = 3.0
        for b in (67.0*Hz, 0.067*kHz):
            for t_stop in (2345*ms, 2.345*second):
                spiketrain = stgen.homogeneous_gamma_process(a, b, t_stop=t_stop)
                intervals = isi(spiketrain)

                expected_spike_count = int((b/a * t_stop).simplified)
                self.assertLess(pdiff(expected_spike_count, spiketrain.size), 0.25)  # should fail about 1 time in 1000

                expected_mean_isi = (a/b).rescale(ms)
                self.assertLess(pdiff(expected_mean_isi, intervals.mean()), 0.3)

                expected_first_spike = 0*ms
                self.assertLess(spiketrain[0] - expected_first_spike, 4*expected_mean_isi)

                expected_last_spike = t_stop
                self.assertLess(expected_last_spike - spiketrain[-1], 4*expected_mean_isi)

                # Kolmogorov-Smirnov test
                D, p = kstest(intervals.rescale(t_stop.units),
                              "gamma",
                              args=(a, 0, (1/b).rescale(t_stop.units)),  # args are (a, loc, scale)
                              alternative='two-sided')
                self.assertGreater(p, 0.001)
                self.assertLess(D, 0.25)
Пример #6
0
 def generate_prediction(self, model, verbose=False):
     model.inject_square_current(observation['current']) 
     spike_train = model.get_spike_train()
     if len(spike_train) >= 3:
         isis = isi(spike_train)
         cv = np.std(isis) / np.mean(isis)
     else:
         cv = None
     return {'cv':cv}
Пример #7
0
 def generate_prediction(self, model = None):
     st = model.get_spike_train()
     isis = isi(st)
     value = float(np.mean(isis))*1000.0*pq.ms
     prediction = {'value':value} 
     return prediction
Пример #8
0
plt.show()

os.chdir('Analysis/PSTH')  # /PSTH')
plt.savefig('Noise_PSTH_' + file_name + '.png', bbox_inches='tight')
plt.close()
os.chdir('../..')
print 'PSTH for ' + file_name + 'saved'

#%% CV-ISI
"""
The below calculates CV-ISI from only the latter 2.5 s current step, output = half_mean_CV
Method = drop values from neospiketrain_list that occur before 1000 ms, then calculating ISI and CV from that
"""
from elephant.statistics import isi, cv

half_isi_list = [isi(spiketrain) for spiketrain in half_spiketime_list]
half_cv_list = [cv(item) for item in half_isi_list]

half_isi_list = [
    isis for i, isis in enumerate(half_isi_list) if i not in depolarized_sweeps
]

#excluding depolarized sweeps - can't use del_half_spiketime_list bc
#need to sort by current injection later

for item in exclude:
    half_cv_list[item] = np.nan

half_mean_CV = np.nanmean(half_cv_list)

#%% Plot ISI histogram for second 1.5 s half of sweep
Пример #9
0
 def test_cv_isi_regular_spiketrain_is_zero(self):
     st = neo.SpikeTrain(self.test_array_regular,  units='ms', t_stop=10.0)
     targ = 0.0
     res = es.cv(es.isi(st))
     self.assertEqual(res, targ)
Пример #10
0
 def test_isi_with_quantities_1d(self):
     st = pq.Quantity(self.test_array_1d, units='ms')
     target = pq.Quantity(self.targ_array_1d, 'ms')
     res = es.isi(st)
     assert_array_almost_equal(res, target, decimal=9)
 def test_cv_isi_regular_spiketrain_is_zero(self):
     st = neo.SpikeTrain(self.test_array_regular,  units='ms', t_stop=10.0)
     targ = 0.0
     res = es.cv(es.isi(st))
     self.assertEqual(res, targ)
 def test_isi_with_plain_array_2d_1(self):
     st = self.test_array_2d
     target = self.targ_array_2d_1
     res = es.isi(st, axis=1)
     assert not isinstance(res, pq.Quantity)
     assert_array_almost_equal(res, target, decimal=9)
 def test_isi_with_quantities_1d(self):
     st = pq.Quantity(self.test_array_1d, units='ms')
     target = pq.Quantity(self.targ_array_1d, 'ms')
     res = es.isi(st)
     assert_array_almost_equal(res, target, decimal=9)
 def test_isi_with_spiketrain(self):
     st = neo.SpikeTrain(
         self.test_array_1d, units='ms', t_stop=10.0, t_start=0.29)
     target = pq.Quantity(self.targ_array_1d, 'ms')
     res = es.isi(st)
     assert_array_almost_equal(res, target, decimal=9)
Пример #15
0
    for i in range (156):
    
    
        unit=block.list_units[i]
        sp=unit.spiketrains
        
        # Defining an ISI for all trials of a neuron
        ISI_trials = []
        ISI_no=0
        
        # defining matrix to store them
        
        for k in sp:
            
            # appending all trials
            ISI_trials.extend(stats.isi(k))
            
            
        # calculating the average of the ISIs and numbers of the ISIs
        his_bin_raw=np.arange(0,t_raw,bin_raw)
        his_bin_mean=np.arange(0,t_mean,bin_mean)
        ISI_mean = np.mean(ISI_trials)
        ISI_no=len(ISI_trials)
        ISI_trials_final=ISI_trials/(ISI_mean)
        # hsitogram
        
        # the histogram of the data
#        plt.subplot(3,1,1)
#        his=np.histogram(ISI_trials_final,bins=his_bin_mean)
#        y=his[0]/(ISI_no)
#        plt.plot(his[1][:-1],y)
Пример #16
0
        t = spiketrain.rescale(pq.ms)
        raster_list.append(t)
        plt.plot(t, (i + 1) * np.ones_like(t), 'k.', markersize=2)
        plt.axis('tight')
        plt.xlim(0, 3100)
        plt.xlabel('Time (ms)', fontsize=16)
        plt.ylim(0, 51)
        plt.ylabel('Trial Number', fontsize=16)
        plt.gca().tick_params(axis='both', which='major', labelsize=14)
        plt.show()

#%%
#CV isi NEVER CONVERTED FOR MULTIPLE FILES###
from elephant.statistics import isi, cv

isi_list = [isi(spiketrain) for spiketrain in st_list]
cv_list = [cv(item) for item in isi_list]
plt.figure()
plt.hist(cv_list)
plt.xlabel('CV', fontsize=16)
plt.ylabel('count', fontsize=16)
plt.show()
plt.figure()
plt.plot(cv_list)
#%%
"""
28Sep2017
NOTE: Work on conversion of discrete spike time lists to binary spike counts.
Use this conversion for PSTH and for spike-time correlations.
"""
##ADDED bstc_df to cut the first 500 ms of data out for correlations
Пример #17
0
 def generate_prediction(self, model=None):
     st = model.get_spike_train()
     isis = isi(st)
     value = float(np.mean(isis))*1000.0*pq.ms
     prediction = {'value': value}
     return prediction
Пример #18
0
def shuffle_isis(spiketrain, n=1, decimals=None):
    """
    Generates surrogates of a neo.SpikeTrain object by inter-spike-interval
    (ISI) shuffling.

    The surrogates are obtained by randomly sorting the ISIs of the given input
    :attr:`spiketrain`. This generates independent `SpikeTrain` object(s) with
    same ISI distribution and spike count as in :attr:`spiketrain`, while
    destroying temporal dependencies and firing rate profile.

    Parameters
    ----------
    spiketrain :  neo.SpikeTrain
        The spike train from which to generate the surrogates
    n : int (optional)
        Number of surrogates to be generated.
        Default: 1
    decimals : int or None (optional)
        Number of decimal points for every spike time in the surrogates
        If None, machine precision is used.
        Default: None

    Returns
    -------
    list of SpikeTrain
      A list of spike trains, each obtained from `spiketrain` by random ISI
      shuffling. The range of the surrogate `neo.SpikeTrain` objects is the
      same as :attr:`spiketrain`.

    Example
    -------
    >>> import quantities as pq
    >>> import neo
    >>>
    >>> st = neo.SpikeTrain([100, 250, 600, 800]*pq.ms, t_stop=1*pq.s)
    >>> print shuffle_isis(st)
        [<SpikeTrain(array([ 200.,  350.,  700.,  800.]) * ms,
                 [0.0 ms, 1000.0 ms])>]
    >>> print shuffle_isis(st, n=2)
        [<SpikeTrain(array([ 100.,  300.,  450.,  800.]) * ms,
              [0.0 ms, 1000.0 ms])>,
         <SpikeTrain(array([ 200.,  350.,  700.,  800.]) * ms,
              [0.0 ms, 1000.0 ms])>]

    """

    if len(spiketrain) > 0:
        isi0 = spiketrain[0] - spiketrain.t_start
        ISIs = np.hstack([isi0, es.isi(spiketrain)])

        # Round the ISIs to decimal position, if requested
        if decimals is not None:
            ISIs = ISIs.round(decimals)

        # Create list of surrogate spike trains by random ISI permutation
        sts = []
        for i in range(n):
            surr_times = np.cumsum(np.random.permutation(ISIs)) *\
                spiketrain.units + spiketrain.t_start
            sts.append(neo.SpikeTrain(
                surr_times, t_start=spiketrain.t_start,
                t_stop=spiketrain.t_stop))

    else:
        sts = []
        empty_train = neo.SpikeTrain([]*spiketrain.units,
                                     t_start=spiketrain.t_start,
                                     t_stop=spiketrain.t_stop)
        for i in range(n):
            sts.append(empty_train)

    return sts
Пример #19
0
 def test_isi_with_spiketrain(self):
     st = neo.SpikeTrain(self.test_array_1d, units='ms', t_stop=10.0)
     target = pq.Quantity(self.targ_array_1d, 'ms')
     res = es.isi(st)
     assert_array_almost_equal(res, target, decimal=9)
 def test_cv_isi_regular_array_is_zero(self):
     st = self.test_array_regular
     targ = 0.0
     res = es.cv(es.isi(st))
     self.assertEqual(res, targ)
Пример #21
0
 def test_isi_with_plain_array_2d_1(self):
     st = self.test_array_2d
     target = self.targ_array_2d_1
     res = es.isi(st, axis=1)
     assert not isinstance(res, pq.Quantity)
     assert_array_almost_equal(res, target, decimal=9)
Пример #22
0
    psth_spikes = non_zero_neuron_times[:]
    # psth_plot_8(plt, numpy.arange(len(psth_spikes)), psth_spikes, bin_width=1e-3,
    #             duration=duration/1000.,title=str(d_ds[i]),subplots=(len(d_data), 1, i + 1),ylim=500)
    #
    plt.figure("v_mem")
    mem_v = cd_data.segments[0].filter(name='v')
    cell_voltage_plot_8(mem_v, plt, duration, [],id=int(len(non_zero_neuron_times)/2),scale_factor=0.0001,title="",subplots=(len(d_data), 1, i + 1))
    # plt.ylabel("membrane voltage (mV)")

    plt.figure("g_syn")
    g_syn = cd_data.segments[0].filter(name='gsyn_exc')
    cell_voltage_plot_8(g_syn, plt, duration, [],id=int(len(non_zero_neuron_times)/2),scale_factor=0.0001,title="",subplots=(len(d_data), 1, i + 1))


    plt.figure("isi")
    t_isi = [isi(spike_train) for spike_train in psth_spikes]
    hist_isi = []
    for neuron in t_isi:
        for interval in neuron:
            if interval.item()<20:
                hist_isi.append(interval.item())
    plt.subplot(len(d_data), 1, i + 1)
    plt.hist(hist_isi,bins=100)
    plt.xlim((0,20))
    # plt.figure("CV")
    # cvs = [cv(interval) for interval in t_isi if len(interval) > 0]
    # plt.subplot(len(d_data), 1, i + 1)
    # plt.hist(cvs)  # ,bins=100)
    # plt.xlim((0, 2))
#
# spike_raster_plot_8(an_spikes, plt, duration/1000., len(an_spikes) + 1, 0.001,
Пример #23
0
 def test_cv_isi_regular_array_is_zero(self):
     st = self.test_array_regular
     targ = 0.0
     res = es.cv(es.isi(st))
     self.assertEqual(res, targ)
Пример #24
0
		# generate raster-plot
		for i, spiketrain in enumerate(snglnrn_spikes_neo):
				t = spiketrain.rescale(q.ms)
				plt.plot(t, i * np.ones_like(t), 'k.', markersize=2)
		plt.axis('tight')
		plt.xlim(0, runtime)
		plt.xlabel('Time (ms)', fontsize=16)
		plt.ylabel('Spike Train Index', fontsize=16)
		plt.gca().tick_params(axis='both', which='major', labelsize=14)
		plt.savefig('decorr_rasterplot_w{}_k{}.png'.format(w, numInhPerNeuron))
		'''

        # calculate ISIs and coefficient of variation (CV)

        isi_list = [
            np.nanmean(isi(spiketrain)) for spiketrain in snglnrn_spikes_neo
        ]
        rate_list = [(np.size(spiketrain) / runtime * 1e3)
                     for spiketrain in snglnrn_spikes]
        cv_list = [cv(isi(spiketrain)) for spiketrain in snglnrn_spikes_neo]

        train = BinnedSpikeTrain(snglnrn_spikes_neo, binsize=5 * q.ms)
        cc_matrix = corrcoef(train, binary=False)

        # Matrix zwischenspeichern
        #np.savetxt('cc_matrix.txt', cc_matrix)
        #print(np.shape(cc_matrix)) # (192, 192)
        #print(cc_matrix)
        #plt.plot(cc_matrix)

        diagonalwerte = []
Пример #25
0
def shuffle_isis(spiketrain, n=1, decimals=None):
    """
    Generates surrogates of a spike train by inter-spike-interval (ISI)
    shuffling.

    The surrogates are obtained by randomly sorting the ISIs of the given input
    `spiketrain`. This generates independent `neo.SpikeTrain` object(s) with
    same ISI distribution and spike count as in `spiketrain`, while
    destroying temporal dependencies and firing rate profile.

    Parameters
    ----------
    spiketrain :  neo.SpikeTrain
        The spike train from which to generate the surrogates.
    n : int, optional
        Number of surrogates to be generated.
        Default: 1.
    decimals : int or None, optional
        Number of decimal points for every spike time in the surrogates.
        If None, machine precision is used.
        Default: None.

    Returns
    -------
    list of neo.SpikeTrain
        Each surrogate spike train obtained independently from `spiketrain` by
        random ISI shuffling. The time range of the surrogate spike trains is
        the same as in `spiketrain`.

    Examples
    --------
    >>> import quantities as pq
    >>> import neo
    ...
    >>> st = neo.SpikeTrain([100, 250, 600, 800] * pq.ms, t_stop=1 * pq.s)
    >>> print(shuffle_isis(st))  # doctest: +SKIP
        [<SpikeTrain(array([ 200.,  350.,  700.,  800.]) * ms,
                 [0.0 ms, 1000.0 ms])>]
    >>> print(shuffle_isis(st, n=2))  # doctest: +SKIP
        [<SpikeTrain(array([ 100.,  300.,  450.,  800.]) * ms,
              [0.0 ms, 1000.0 ms])>,
         <SpikeTrain(array([ 200.,  350.,  700.,  800.]) * ms,
              [0.0 ms, 1000.0 ms])>]

    """
    if len(spiketrain) > 0:
        isi0 = spiketrain[0] - spiketrain.t_start
        ISIs = np.hstack([isi0, isi(spiketrain)])

        # Round the isis to decimal position, if requested
        if decimals is not None:
            ISIs = ISIs.round(decimals)

        # Create list of surrogate spike trains by random ISI permutation
        sts = []
        for surrogate_id in range(n):
            surr_times = np.cumsum(np.random.permutation(ISIs)) * \
                spiketrain.units + spiketrain.t_start
            sts.append(
                neo.SpikeTrain(surr_times,
                               t_start=spiketrain.t_start,
                               t_stop=spiketrain.t_stop))

    else:
        sts = [
            neo.SpikeTrain([] * spiketrain.units,
                           t_start=spiketrain.t_start,
                           t_stop=spiketrain.t_stop)
        ] * n

    return sts
Пример #26
0
 def test_isi_with_plain_array_2d_default(self):
     st = self.test_array_2d
     target = self.targ_array_2d_default
     res = statistics.isi(st)
     assert not isinstance(res, pq.Quantity)
     assert_array_almost_equal(res, target, decimal=9)
Пример #27
0
    def statistic_isi(self, spiketrain, axis=-1):

        isi(spiketrain, axis)
Пример #28
0
 def test_unsorted_array(self):
     np.random.seed(0)
     array = np.random.rand(100)
     with self.assertWarns(UserWarning):
         isi = statistics.isi(array)