def create_big_neo_block(name='a block'):
    t1 = time.time()
    
    print 'b', b
    bl = neo.Block(name = name)
    
    rcg = neo.RecordingChannelGroup(name = 'big group')
    bl.recordingchannelgroups.append(rcg)
    for a in range(nb_sig):
        rc = neo.RecordingChannel(name = 'rec channel {}'.format(a), index = a)
        rcg.recordingchannels.append(rc)
    
    for s in range(nb_seg):
        print ' s', s
        seg = neo.Segment(name = 'seg {}'.format(s))
        bl.segments.append(seg)
        for a in range(nb_sig):
            #~ print '  a', a
            #~ ana = neo.AnalogSignal(signal = np.empty(sig_size)*pq.mV, sampling_rate = 10*pq.kHz, t_start = -2.*pq.s,
                                #~ name = 'signal {}'.format(a), description = 'this is a big signal')
            ana = neo.AnalogSignal(signal = np.random.rand(sig_size)*pq.mV, sampling_rate = 10*pq.kHz, t_start = -2.*pq.s,
                                name = 'signal {}'.format(a), description = 'this is a big signal')
            seg.analogsignals.append(ana)
            rcg.recordingchannels[a].analogsignals.append(ana)
        
    t2 = time.time()
    print 'time for creating neo block', t2 - t1
    
    return bl
Esempio n. 2
0
    def create_hierarchy(cls, many_to_many):
        b = neo.Block()

        for ns in range(cls.SEGMENTS):
            b.segments.append(neo.Segment())

        channels = []
        if many_to_many:
            channels = [
                neo.RecordingChannel(name='Shared %d' % i,
                                     index=i + cls.CHANNELS)
                for i in range(cls.CHANNELS / 2)
            ]

        for ng in range(cls.CHANNEL_GROUPS):
            rcg = neo.RecordingChannelGroup()
            for nu in range(cls.UNITS):
                unit = neo.Unit()
                for ns in range(cls.SEGMENTS):
                    spike = neo.Spike(0 * pq.s)
                    unit.spikes.append(spike)
                    b.segments[ns].spikes.append(spike)

                    st = neo.SpikeTrain([] * pq.s, 0 * pq.s)
                    unit.spiketrains.append(st)
                    b.segments[ns].spiketrains.append(st)

                rcg.units.append(unit)

            if not many_to_many:
                for nc in range(cls.CHANNELS):
                    rc = neo.RecordingChannel(name='Single %d' % nc, index=nc)
                    rc.recordingchannelgroups.append(rcg)
                    rcg.recordingchannels.append(rc)
            else:
                for nc in range(cls.CHANNELS):
                    if nc % 2 == 0:
                        rc = neo.RecordingChannel(name='Single %d' % (nc / 2),
                                                  index=nc / 2)
                    else:
                        rc = channels[nc / 2]
                    rc.recordingchannelgroups.append(rcg)
                    rcg.recordingchannels.append(rc)
            rcg.channel_indexes = sp.array(
                [c.index for c in rcg.recordingchannels])
            rcg.channel_names = sp.array(
                [c.name for c in rcg.recordingchannels])

            b.recordingchannelgroups.append(rcg)

        try:
            neo.io.tools.create_many_to_one_relationship(b)
        except AttributeError:
            b.create_many_to_one_relationship()
        return b
Esempio n. 3
0
def generate_block(n_segments=3,
                   n_channels=8,
                   n_units=3,
                   data_samples=1000,
                   feature_samples=100):
    """
    Generate a block with a single recording channel group and a number of
    segments, recording channels and units with associated analog signals
    and spike trains.
    """
    feature_len = feature_samples / data_samples

    # Create container and grouping objects
    segments = [neo.Segment(index=i) for i in range(n_segments)]

    rcg = neo.RecordingChannelGroup(name='T0')
    for i in range(n_channels):
        rc = neo.RecordingChannel(name='C%d' % i, index=i)
        rc.recordingchannelgroups = [rcg]
        rcg.recordingchannels.append(rc)

    units = [neo.Unit('U%d' % i) for i in range(n_units)]
    rcg.units = units

    block = neo.Block()
    block.segments = segments
    block.recordingchannelgroups = [rcg]

    # Create synthetic data
    for seg in segments:
        feature_pos = np.random.randint(0, data_samples - feature_samples)

        # Analog signals: Noise with a single sinewave feature
        wave = 3 * np.sin(np.linspace(0, 2 * np.pi, feature_samples))
        for rc in rcg.recordingchannels:
            sig = np.random.randn(data_samples)
            sig[feature_pos:feature_pos + feature_samples] += wave

            signal = neo.AnalogSignal(sig * pq.mV, sampling_rate=1 * pq.kHz)
            seg.analogsignals.append(signal)
            rc.analogsignals.append(signal)

        # Spike trains: Random spike times with elevated rate in short period
        feature_time = feature_pos / data_samples
        for u in units:
            random_spikes = np.random.rand(20)
            feature_spikes = np.random.rand(5) * feature_len + feature_time
            spikes = np.hstack([random_spikes, feature_spikes])

            train = neo.SpikeTrain(spikes * pq.s, 1 * pq.s)
            seg.spiketrains.append(train)
            u.spiketrains.append(train)

    block.create_many_to_one_relationship()
    return block
Esempio n. 4
0
class DataProvider(object):
    """ Defines all methods that should be implemented by a
    selection/data provider class.

    A `DataProvider` encapsulates
    access to a selection of data. It can be used by plugins to
    acesss data currently selected in the GUI or in saved selections.
    It also contains an attribute `progress`, a
    :class:`spykeutils.progress_indicator.ProgressIndicator` that
    can be used to report the progress of an operation (and is used
    by methods of this class if they can lead to processing times
    of half a second or more).

    This class serves as an abstract base class and should not be
    instantiated."""
    _factories = {}
    no_unit = neo.Unit(name='No Unit')
    no_segment = neo.Segment(name='No segment')
    no_channel = neo.RecordingChannel(name='No recording channel')
    no_channelgroup = neo.RecordingChannelGroup(
        name='No recording channel group')
    no_unit.annotate(unique_id=-1)
    no_segment.annotate(unique_id=-1)
    no_channel.annotate(unique_id=-1)
    no_channelgroup.annotate(unique_id=-1)

    def __init__(self, name, progress):
        self.name = name
        self.progress = progress

    def _invert_indices(self, dictionary):
        """ Invert the indices of a dictionary of dictionaries.
        """
        dict_type = type(dictionary)
        ret = dict_type()
        for i1 in dictionary:
            for i2 in dictionary[i1]:
                if not i2 in ret:
                    ret[i2] = dict_type()
                ret[i2][i1] = dictionary[i1][i2]
        return ret

    def blocks(self):
        """ Return a list of selected Block objects.

        The returned objects will contain all regular references, not just to
        selected objects.
        """
        return []

    def segments(self):
        """ Return a list of selected Segment objects.

        The returned objects will contain all regular references, not just to
        selected objects.
        """
        return []

    def recording_channel_groups(self):
        """ Return a list of selected RecordingChannelGroup objects.

        The returned objects will contain all regular references, not just to
        selected objects.
        """
        return []

    def recording_channels(self):
        """ Return a list of selected RecordingChannel objects.

        The returned objects will contain all regular references, not just to
        selected objects.
        """
        return []

    def units(self):
        """ Return a list of selected Unit objects.

        The returned objects will contain all regular references, not just to
        selected objects.
        """
        return []

    def selection_blocks(self):
        """ Return a list of selected blocks.

        The returned blocks will contain references to all other selected
        elements further down in the object hierarchy, but no references to
        elements which are not selected. The returned hierarchy is a copy,
        so changes made to it will not persist. The main purpose
        of this function is to provide an object hierarchy that can be
        saved to a neo file. It is not recommended to use it for data
        processing, the respective functions that return objects lower
        in the hierarchy are better suited for that purpose.
        """
        return []

    def spike_trains(self):
        """ Return a list of :class:`neo.core.SpikeTrain` objects.
        """
        return []

    def spike_trains_by_unit(self):
        """ Return a dictionary (indexed by Unit) of lists of
        :class:`neo.core.SpikeTrain` objects.

        If spike trains not attached to a Unit are selected, their
        dicionary key will be ``DataProvider.no_unit``.
        """
        return {}

    def spike_trains_by_segment(self):
        """ Return a dictionary (indexed by Segment) of lists of
        :class:`neo.core.SpikeTrain` objects.

        If spike trains not attached to a Segment are selected, their
        dictionary key will be ``DataProvider.no_segment``.
        """
        return {}

    def spike_trains_by_unit_and_segment(self):
        """ Return a dictionary (indexed by Unit) of dictionaries
        (indexed by Segment) of :class:`neo.core.SpikeTrain` objects.

        If there are multiple spike trains in one Segment for the same Unit,
        only the first will be contained in the returned dictionary. If spike
        trains not attached to a Unit or Segment are selected, their
        dictionary key will be ``DataProvider.no_unit`` or
        ``DataProvider.no_segment``, respectively.
        """
        return {}

    def spike_trains_by_segment_and_unit(self):
        """ Return a dictionary (indexed by Unit) of dictionaries
        (indexed by Segment) of :class:`neo.core.SpikeTrain` objects.

        If there are multiple spike trains in one Segment for the same Unit,
        only the first will be contained in the returned dictionary. If spike
        trains not attached to a Unit or Segment are selected, their
        dictionary key will be ``DataProvider.no_unit`` or
        ``DataProvider.no_segment``, respectively.
        """
        return self._invert_indices(self.spike_trains_by_unit_and_segment())

    def spikes(self):
        """ Return a list of :class:`neo.core.Spike` objects.
        """
        return []

    def spikes_by_unit(self):
        """ Return a dictionary (indexed by Unit) of lists of
        :class:`neo.core.Spike` objects.

        If spikes not attached to a Unit are selected, their
        dicionary key will be ``DataProvider.no_unit``.
        """
        return {}

    def spikes_by_segment(self):
        """ Return a dictionary (indexed by Segment) of lists of
        :class:`neo.core.Spike` objects.

        If spikes not attached to a Segment are selected, their
        dictionary key will be ``DataProvider.no_segment``.
        """
        return {}

    def spikes_by_unit_and_segment(self):
        """ Return a dictionary (indexed by Unit) of dictionaries
        (indexed by Segment) of :class:`neo.core.Spike` lists.

        If there are multiple spikes in one Segment for the same Unit,
        only the first will be contained in the returned dictionary. If
        spikes not attached to a Unit or Segment are selected, their
        dictionary key will be ``DataProvider.no_unit`` or
        ``DataProvider.no_segment``, respectively.
        """
        return {}

    def spikes_by_segment_and_unit(self):
        """ Return a dictionary (indexed by Segment) of dictionaries
        (indexed by Unit) of lists of :class:`neo.core.Spike` lists.

        If spikes not attached to a Unit or Segment are selected, their
        dictionary key will be ``DataProvider.no_unit`` or
        ``DataProvider.no_segment``, respectively.
        """
        return self._invert_indices(self.spikes_by_unit_and_segment())

    def events(self, include_array_events=True):
        """ Return a dictionary (indexed by Segment) of lists of
        Event objects.

        :param bool include_array_events: Determines if EventArray objects
            should be converted to Event objects and included in the returned
            list.
        """
        return {}

    def labeled_events(self, label, include_array_events=True):
        """ Return a dictionary (indexed by Segment) of lists of Event
        objects with the given label.

        :param str label: The name of the Event objects to be returnded
        :param bool include_array_events: Determines if EventArray objects
            should be converted to Event objects and included in the returned
            list.
        """
        return []

    def event_arrays(self):
        """ Return a dictionary (indexed by Segment) of lists of
        EventArray objects.
        """
        return {}

    def epochs(self, include_array_epochs=True):
        """ Return a dictionary (indexed by Segment) of lists of
        Epoch objects.

        :param bool include_array_epochs: Determines if EpochArray objects
            should be converted to Epoch objects and included in the returned
            list.
        """
        return {}

    def labeled_epochs(self, label, include_array_epochs=True):
        """ Return a dictionary (indexed by Segment) of lists of Epoch
        objects with the given label.

        :param str label: The name of the Epoch objects to be returnded
        :param bool include_array_epochs: Determines if EpochArray objects
            should be converted to Epoch objects and included in the returned
            list.
        """
        return []

    def epoch_arrays(self):
        """ Return a dictionary (indexed by Segment) of lists of
        EpochArray objects.
        """
        return {}

    def analog_signals(self, conversion_mode=1):
        """ Return a list of :class:`neo.core.AnalogSignal` objects.

        :param int conversion_mode: Determines what signals are returned:

            1. AnalogSignal objects only
            2. AnalogSignal objects extracted from AnalogSignalArrays only
            3. Both AnalogSignal objects and extracted AnalogSignalArrays
        """
        return []

    def analog_signals_by_segment(self, conversion_mode=1):
        """ Return a dictionary (indexed by Segment) of lists of
        :class:`neo.core.AnalogSignal` objects.

        If analog signals not attached to a Segment are selected, their
        dictionary key will be ``DataProvider.no_segment``.

        :param int conversion_mode: Determines what signals are returned:

            1. AnalogSignal objects only
            2. AnalogSignal objects extracted from AnalogSignalArrays only
            3. Both AnalogSignal objects and extracted AnalogSignalArrays
        """
        return {}

    def analog_signals_by_channel(self, conversion_mode=1):
        """ Return a dictionary (indexed by RecordingChannel) of lists
        of :class:`neo.core.AnalogSignal` objects.

        If analog signals not attached to a RecordingChannel are selected,
        their dictionary key will be ``DataProvider.no_channel``.

        :param int conversion_mode: Determines what signals are returned:

            1. AnalogSignal objects only
            2. AnalogSignal objects extracted from AnalogSignalArrays only
            3. Both AnalogSignal objects and extracted AnalogSignalArrays
        """
        return {}

    def analog_signals_by_channel_and_segment(self, conversion_mode=1):
        """ Return a dictionary (indexed by RecordingChannel) of
        dictionaries (indexed by Segment) of :class:`neo.core.AnalogSignal`
        lists.

        If analog signals not attached to a Segment or
        RecordingChannel are selected, their dictionary key will be
        ``DataProvider.no_segment`` or ``DataProvider.no_channel``,
        respectively.

        :param int conversion_mode: Determines what signals are returned:

            1. AnalogSignal objects only
            2. AnalogSignal objects extracted from AnalogSignalArrays only
            3. Both AnalogSignal objects and extracted AnalogSignalArrays
        """
        return {}

    def analog_signals_by_segment_and_channel(self, conversion_mode=1):
        """ Return a dictionary (indexed by Segment) of
        dictionaries (indexed by RecordingChannel) of :class:`neo.core.AnalogSignal`
        lists.

        If analog signals not attached to a Segment or
        RecordingChannel are selected, their dictionary key will be
        ``DataProvider.no_segment`` or ``DataProvider.no_channel``,
        respectively.

        :param int conversion_mode: Determines what signals are returned:

            1. AnalogSignal objects only
            2. AnalogSignal objects extracted from AnalogSignalArrays only
            3. Both AnalogSignal objects and extracted AnalogSignalArrays
        """
        return self._invert_indices(
            self.analog_signals_by_channel_and_segment(conversion_mode))

    def analog_signal_arrays(self):
        """ Return a list of :class:`neo.core.AnalogSignalArray` objects.
        """
        return []

    def analog_signal_arrays_by_segment(self):
        """ Return a dictionary (indexed by Segment) of lists of
        :class:`neo.core.AnalogSignalArray` objects.

        If analog signals arrays not attached to a Segment are selected,
        their dictionary key will be ``DataProvider.no_segment``.
        """
        return {}

    def analog_signal_arrays_by_channelgroup(self):
        """ Return a dictionary (indexed by RecordingChannelGroup) of
        lists of :class:`neo.core.AnalogSignalArray` objects.

        If analog signals arrays not attached to a RecordingChannel are
        selected, their dictionary key will be
        ``DataProvider.no_channelgroup``.
        """
        return {}

    def analog_signal_arrays_by_channelgroup_and_segment(self):
        """ Return a dictionary (indexed by RecordingChannelGroup) of
        dictionaries (indexed by Segment) of
        :class:`neo.core.AnalogSignalArray` objects.

        If there are multiple analog signals in one RecordingChannel for
        the same Segment, only the first will be contained in the returned
        dictionary. If analog signal arrays not attached to a Segment or
        RecordingChannelGroup are selected, their dictionary key will be
        ``DataProvider.no_segment`` or ``DataProvider.no_channelgroup``,
        respectively.
        """
        return {}

    def analog_signal_arrays_by_segment_and_channelgroup(self):
        """ Return a dictionary (indexed by RecordingChannelGroup) of
        dictionaries (indexed by Segment) of
        :class:`neo.core.AnalogSignalArray` objects.

        If there are multiple analog signals in one RecordingChannel for
        the same Segment, only the first will be contained in the returned
        dictionary. If analog signal arrays not attached to a Segment or
        RecordingChannelGroup are selected, their dictionary key will be
        ``DataProvider.no_segment`` or ``DataProvider.no_channelgroup``,
        respectively.
        """
        return self._invert_indices(
            self.analog_signal_arrays_by_channelgroup_and_segment())

    def refresh_view(self):
        """ Refresh associated views of the data.

        Use this method if when you change the neo hierarchy on which the
        selection is based (e.g. adding or removing objects). It will ensure
        that all current views on the data are updated, for example in
        Spyke Viewer.
        """
        pass

    def data_dict(self):
        """ Return a dictionary with all information to serialize the
        object.
        """
        return {}

    @classmethod
    def from_data(cls, data, progress=None):
        """ Create a new `DataProvider` object from a dictionary. This
        method is mostly for internal use.

        The respective type of `DataProvider` (e.g.
        :class:`spykeviewer.plugin_framework.data_provider_neo.DataProviderNeo`
        has to be imported in the environment where this function is
        called.

        :param dict data: A dictionary containing data from a `DataProvider`
            object, as returned by :func:`data_dict`.
        :param ProgressIndicator progress: The object where loading progress
            will be indicated.
        """
        if progress:
            return cls._factories[data['type']](data, progress)
        return cls._factories[data['type']](data)
Esempio n. 5
0
def generate_block_for_sorting(
    nb_segment=5,
    nb_recordingchannel=4,
    sampling_rate=10.e3 * pq.Hz,
    duration=6. * pq.s,
    nb_unit=6,
    spikerate_range=[.5 * pq.Hz, 12 * pq.Hz],
    t_start=0. * pq.s,
    noise_ratio=0.2,
    use_memmap_path=None,
):

    bl = neo.Block()

    for s in range(nb_segment):
        bl.segments.append(neo.Segment(name='Segment {}'.format(s)))

    rcg = neo.RecordingChannelGroup(name='polytrode', )
    rcg.block = bl
    bl.recordingchannelgroups.append(rcg)
    for r in range(nb_recordingchannel):
        rc = neo.RecordingChannel(name='RC {}'.format(r), index=r)
        rcg.recordingchannels.append(rc)
        rc.recordingchannelgroups.append(rcg)

    # generate spiketrain
    for u in range(nb_unit):
        unit = neo.Unit(name='Unit #{}'.format(u))
        rcg.units.append(unit)
        spikerate = np.random.rand() * np.diff(
            spikerate_range) + spikerate_range[0].magnitude
        spike_times = []
        for seg in bl.segments:
            spike_times.append(
                np.random.rand(int((spikerate *
                                    (duration)).simplified)) * duration +
                t_start)
        n_total = np.sum(times.size for times in spike_times)
        all_waveforms = stupid_waveform_generator(n_total, nb_recordingchannel,
                                                  sampling_rate) * pq.mV

        n = 0
        for s, seg in enumerate(bl.segments):
            #~ print u, 's', s, spike_times[s].size
            sptr = neo.SpikeTrain(spike_times[s],
                                  t_start=t_start,
                                  t_stop=t_start + duration)
            sptr.waveforms = all_waveforms[n:n + spike_times[s].size, :, :]
            sptr.sampling_rate = sampling_rate
            sptr.left_sweep = (sptr.waveforms.shape[2] /
                               sampling_rate).rescale('ms') / 2.
            seg.spiketrains.append(sptr)
            unit.spiketrains.append(sptr)
            n += spike_times[s].size

    # generate signal = noise + spike waveform
    for i, seg in enumerate(bl.segments):
        for j, rc in enumerate(rcg.recordingchannels):
            sig_size = int((duration * sampling_rate).simplified)

            if use_memmap_path:
                filename = os.path.join(use_memmap_path,
                                        'signal {} {}'.format(i, j))
                signal = np.memmap(
                    filename,
                    dtype=float,
                    mode='w+',
                    offset=0,
                    shape=sig_size,
                )
                signal[:] = noise_ratio * np.random.randn(sig_size)
            else:
                signal = noise_ratio * np.random.randn(sig_size)

            #~ anasig = neo.AnalogSignal(signal = signal, units = 'mV', sampling_rate = sampling_rate, t_start = t_start, channel_index = j)

            for sptr in seg.spiketrains:
                for k, time in enumerate(sptr):
                    wf = sptr.waveforms[k, j, :]
                    pos = int(((time - t_start) * sampling_rate).simplified)
                    if pos + wf.size < signal.size:
                        signal[pos:pos + wf.size] += wf
            anasig = neo.AnalogSignal(signal=signal,
                                      units='mV',
                                      sampling_rate=sampling_rate,
                                      t_start=t_start,
                                      channel_index=j)
            seg.analogsignals.append(anasig)
            rc.analogsignals.append(anasig)

    try:
        neo.io.tools.create_many_to_one_relationship(bl)
    except:
        bl.create_many_to_one_relationship()

    return bl