Esempio n. 1
0
 def test_get_start_stop(self):
     a = self.spiketrain_a
     b = neo.SpikeTrain([-0.1, -0.7, 1.2, 2.2, 4.3, 5.5, 8.0] * pq.s,
                        t_start=-1 * pq.s,
                        t_stop=8 * pq.s)
     start, stop = get_common_start_stop_times(a)
     self.assertEqual(start, a.t_start)
     self.assertEqual(stop, a.t_stop)
     start, stop = get_common_start_stop_times([a, b])
     self.assertEqual(start, a.t_start)
     self.assertEqual(stop, b.t_stop)
 def compute_binned_spikes_trains(self,
                                  spikes_trains,
                                  binsize=None,
                                  num_bins=None):
     """Method to compute a elephant.conversion.BinnedSpikeTrain data type
         from a sequence (array, list, tuple) of arrays of spikes" times.
        Arguments:
         - spikes_trains: a sequence (array, list, tuple) of arrays of spikes" times
         - binsize: the size (float, in ms) of the bin to be used. Default=None.
         - num_bins: the number (integer > 0) of bins to be used. Default=None.
         If none of binsize or num_bins if given, a bin size equal to the sampling period is used.
        Returns:
         - the elephant.conversion.BinnedSpikeTrain instance.
     """
     from quantities import ms
     from elephant.conversion import BinnedSpikeTrain
     from elephant.utils import get_common_start_stop_times
     for i_spike_train, spikes_train, in enumerate(spikes_trains):
         spikes_trains[i_spike_train] = self._assert_spike_train(
             spikes_train)
     t_start, t_stop = get_common_start_stop_times(spikes_trains)
     if binsize is not None:
         binsize = float(binsize) * ms
         num_bins = None
     elif num_bins is None:
         binsize = self.period * ms
     return BinnedSpikeTrain(spikes_trains,
                             binsize=binsize,
                             num_bins=num_bins,
                             t_start=t_start,
                             t_stop=t_stop)
Esempio n. 3
0
    def test_binned_spiketrain_neg_times_list(self):
        a = neo.SpikeTrain(
            [-6.5, 0.5, 0.7, 1.2, 3.1, 4.3, 5.5, 6.7] * pq.s,
            t_start=-7 * pq.s, t_stop=7 * pq.s)
        b = neo.SpikeTrain(
            [-0.1, -0.7, 1.2, 2.2, 4.3, 5.5, 8.0] * pq.s,
            t_start=-1 * pq.s, t_stop=8 * pq.s)
        spiketrains = [a, b]

        # not the same t_start and t_stop
        self.assertRaises(ValueError, cv.BinnedSpikeTrain,
                          spiketrains=spiketrains,
                          bin_size=self.bin_size)
        t_start, t_stop = get_common_start_stop_times(spiketrains)
        self.assertEqual(t_start, -1 * pq.s)
        self.assertEqual(t_stop, 7 * pq.s)
        x_bool = cv.BinnedSpikeTrain(spiketrains, bin_size=self.bin_size,
                                     t_start=t_start, t_stop=t_stop)
        y_bool = [[0, 1, 1, 0, 1, 1, 1, 1],
                  [1, 0, 1, 1, 0, 1, 1, 0]]

        assert_array_equal(x_bool.to_bool_array(), y_bool)
Esempio n. 4
0
    def _resolve_input_parameters(self, spiketrains):
        """
        Calculates `t_start`, `t_stop` from given spike trains.

        The start and stop points are calculated from given spike trains only
        if they are not calculable from given parameters or the number of
        parameters is less than three.

        Parameters
        ----------
        spiketrains : neo.SpikeTrain or list or np.ndarray of neo.SpikeTrain

        """
        def get_n_bins():
            n_bins = (self._t_stop - self._t_start) / self._bin_size
            if isinstance(n_bins, pq.Quantity):
                n_bins = n_bins.simplified.item()
            n_bins = round_binning_errors(n_bins, tolerance=self.tolerance)
            return n_bins

        def check_n_bins_consistency():
            if self.n_bins != get_n_bins():
                raise ValueError(
                    "Inconsistent arguments: t_start ({t_start}), "
                    "t_stop ({t_stop}), bin_size ({bin_size}), and "
                    "n_bins ({n_bins})".format(t_start=self.t_start,
                                               t_stop=self.t_stop,
                                               bin_size=self.bin_size,
                                               n_bins=self.n_bins))

        def check_consistency():
            if self.t_start >= self.t_stop:
                raise ValueError("t_start must be smaller than t_stop")
            if not isinstance(self.n_bins, int) or self.n_bins <= 0:
                raise TypeError("The number of bins ({}) must be a positive "
                                "integer".format(self.n_bins))

        if not _check_neo_spiketrain(spiketrains):
            # a binned numpy matrix
            self.__resolve_binned(spiketrains)
            self.units = self._bin_size.units
            check_n_bins_consistency()
            check_consistency()
            self._t_start = self._t_start.rescale(self.units).item()
            self._t_stop = self._t_stop.rescale(self.units).item()
            self._bin_size = self._bin_size.rescale(self.units).item()
            return

        if self._bin_size is None and self.n_bins is None:
            raise ValueError("Either 'bin_size' or 'n_bins' must be given")

        try:
            check_neo_consistency(spiketrains,
                                  object_type=neo.SpikeTrain,
                                  t_start=self._t_start,
                                  t_stop=self._t_stop,
                                  tolerance=self.tolerance)
        except ValueError as er:
            # different t_start/t_stop
            raise ValueError(
                er, "If you want to bin over the shared "
                "[t_start, t_stop] interval, provide "
                "shared t_start and t_stop explicitly, "
                "which can be obtained like so: "
                "t_start, t_stop = elephant.utils."
                "get_common_start_stop_times(spiketrains)")

        if self._t_start is None:
            self._t_start = spiketrains[0].t_start
        if self._t_stop is None:
            self._t_stop = spiketrains[0].t_stop
        # At this point, all spiketrains share the same units.
        self.units = spiketrains[0].units

        # t_start and t_stop are checked to be time quantities in the
        # check_neo_consistency call.
        self._t_start = self._t_start.rescale(self.units).item()
        self._t_stop = self._t_stop.rescale(self.units).item()

        start_shared, stop_shared = get_common_start_stop_times(spiketrains)
        start_shared = start_shared.rescale(self.units).item()
        stop_shared = stop_shared.rescale(self.units).item()

        tolerance = self.tolerance
        if tolerance is None:
            tolerance = 0
        if self._t_start < start_shared - tolerance \
                or self._t_stop > stop_shared + tolerance:
            raise ValueError("'t_start' ({t_start}) or 't_stop' ({t_stop}) is "
                             "outside of the shared [{start_shared}, "
                             "{stop_shared}] interval".format(
                                 t_start=self.t_start,
                                 t_stop=self.t_stop,
                                 start_shared=start_shared,
                                 stop_shared=stop_shared))

        if self.n_bins is None:
            # bin_size is provided
            self._bin_size = self._bin_size.rescale(self.units).item()
            self.n_bins = get_n_bins()
        elif self._bin_size is None:
            # n_bins is provided
            self._bin_size = (self._t_stop - self._t_start) / self.n_bins
        else:
            # both n_bins are bin_size are given
            self._bin_size = self._bin_size.rescale(self.units).item()
            check_n_bins_consistency()

        check_consistency()