Example #1
0
    def _append_previous_segment(self, block, segment_number, variables,
                                 view_indexes):
        if segment_number not in self._data_cache:
            logger.warning("No Data available for Segment {}", segment_number)
            segment = neo.Segment(name="segment{}".format(segment_number),
                                  description="Empty",
                                  rec_datetime=datetime.now())
            block.segments.append(segment)
            return

        data_cache = self._data_cache[segment_number]

        # sort out variables
        variables = self._clean_variables(variables)

        # build segment for the previous data to be gathered in
        segment = neo.Segment(name="segment{}".format(segment_number),
                              description=data_cache.description,
                              rec_datetime=data_cache.rec_datetime)

        for variable in variables:
            if variable not in data_cache.variables:
                logger.warning("No Data available for Segment {} variable {}",
                               segment_number, variable)
                continue
            variable_cache = data_cache.get_data(variable)
            if variable == SPIKES:
                self.__read_in_spikes(
                    segment=segment,
                    spikes=variable_cache.data,
                    t=data_cache.t,
                    n_neurons=variable_cache.n_neurons,
                    recording_start_time=data_cache.recording_start_time,
                    sampling_interval=variable_cache.sampling_interval,
                    indexes=view_indexes,
                    label=data_cache.label)
            elif variable == REWIRING:
                self.__read_in_event(
                    segment=segment,
                    event_array=variable_cache.data,
                    variable=variable,
                    recording_start_time=data_cache.recording_start_time)
            else:
                self.__read_in_signal(
                    segment=segment,
                    block=block,
                    signal_array=variable_cache.data,
                    data_indexes=variable_cache.indexes,
                    view_indexes=view_indexes,
                    variable=variable,
                    recording_start_time=data_cache.recording_start_time,
                    sampling_interval=variable_cache.sampling_interval,
                    units=variable_cache.units,
                    label=data_cache.label)

        block.segments.append(segment)
Example #2
0
    def _get_current_segment(self,
                             filter_ids=None,
                             variables='all',
                             clear=False):
        segment = neo.Segment(
            name="segment%03d" % self._simulator.state.segment_counter,
            description=self.population.describe(),
            rec_datetime=datetime.now()
        )  # would be nice to get the time at the start of the recording, not the end
        variables_to_include = set(self.recorded.keys())
        if variables is not 'all':
            variables_to_include = variables_to_include.intersection(
                set(variables))
        for variable in variables_to_include:
            if variable == 'spikes':
                t_stop = self._simulator.state.t * pq.ms  # must run on all MPI nodes
                sids = sorted(self.filter_recorded('spikes', filter_ids))
                data = self._get_spiketimes(sids)

                segment.spiketrains = [
                    neo.SpikeTrain(data.get(int(id), []),
                                   t_start=self._recording_start_time,
                                   t_stop=t_stop,
                                   units='ms',
                                   source_population=self.population.label,
                                   source_id=int(id),
                                   source_index=self.population.id_to_index(
                                       int(id))) for id in sids
                ]
            else:
                ids = sorted(self.filter_recorded(variable, filter_ids))
                signal_array = self._get_all_signals(variable,
                                                     ids,
                                                     clear=clear)
                t_start = self._recording_start_time
                sampling_period = self.sampling_interval * pq.ms
                current_time = self._simulator.state.t * pq.ms
                mpi_node = self._simulator.state.mpi_rank  # for debugging
                if signal_array.size > 0:  # may be empty if none of the recorded cells are on this MPI node
                    units = self.population.find_units(variable)
                    source_ids = numpy.fromiter(ids, dtype=int)
                    signal = neo.AnalogSignal(
                        signal_array,
                        units=units,
                        t_start=t_start,
                        sampling_period=sampling_period,
                        name=variable,
                        source_population=self.population.label,
                        source_ids=source_ids)
                    signal.channel_index = neo.ChannelIndex(
                        index=numpy.arange(source_ids.size),
                        channel_ids=numpy.array(
                            [self.population.id_to_index(id) for id in ids]))
                    segment.analogsignals.append(signal)
                    logger.debug("%d **** ids=%s, channels=%s", mpi_node,
                                 source_ids, signal.channel_index)
                    assert segment.analogsignals[
                        0].t_stop - current_time - 2 * sampling_period < 1e-10
                    # need to add `Unit` and `RecordingChannelGroup` objects
        return segment
Example #3
0
    def __init__(self, RecordFile=None, UnitGain=1e6, Seg=None):

        self.UnitGain = UnitGain

        if not RecordFile:
            self.Seg = Seg
            if self.Seg:
                self.UpdateEventDict()
                self.UpdateSignalsDict()
            else:
                self.Seg = neo.Segment('New Seg')
            return

        ftype = RecordFile.split('.')[-1]
        if ftype == 'h5':
            self.RecFile = neo.io.NixIO(filename=RecordFile, mode='ro')
            Block = self.RecFile.read_block()
        elif ftype == 'smr':
            self.RecFile = neo.io.Spike2IO(filename=RecordFile)
            Block = self.RecFile.read()[0]

        self.Seg = Block.segments[0]

        self.UpdateSignalsDict()
        self.UpdateEventDict()
Example #4
0
    def write_data_to_hdf5(self, filename='data'):
        """This is a function to call after simulation that writes the 
        data of all probes to filename using the Neo HDF5 IO module.
    
        :param string filename: the name of the file to write out to
        """
        import neo
        from neo import hdf5io

        # get list of probes
        probe_list = [
            self.nodes[node] for node in self.nodes if node[:5] == 'Probe'
        ]

        # if no probes then just return
        if len(probe_list) == 0: return

        # open up hdf5 file
        if not filename.endswith('.hd5'): filename += '.hd5'
        iom = hdf5io.NeoHdf5IO(filename=filename)

        #TODO: set up to write multiple trials/segments to same block
        #      for trials run at different points
        # create the all encompassing block structure
        block = neo.Block()
        # create the segment, representing a trial
        segment = neo.Segment()
        # put the segment in the block
        block.segments.append(segment)

        # create the appropriate Neo structures from the Probes data
        #TODO: pair any analog signals and spike trains from the same
        #      population together into a RecordingChannel
        for probe in probe_list:
            # decoded signals become AnalogSignals
            if probe.target_name.endswith('decoded'):
                segment.analogsignals.append(
                    neo.AnalogSignal(
                        probe.get_data() * quantities.dimensionless,
                        sampling_period=probe.dt_sample * quantities.s,
                        target_name=probe.target_name))
            # spikes become spike trains
            elif probe.target_name.endswith('spikes'):
                # have to change spike train of 0s and 1s to list of times
                for neuron in probe.get_data().T:
                    segment.spiketrains.append(
                        neo.SpikeTrain([
                            t * probe.dt_sample
                            for t, val in enumerate(neuron[0]) if val > 0
                        ] * quantities.s,
                                       t_stop=len(probe.data),
                                       target_name=probe.target_name))
            else:
                print 'Do not know how to write %s to NeoHDF5 file' % probe.target_name
                assert False

        # write block to file
        iom.save(block)
        # close up hdf5 file
        iom.close()
Example #5
0
    def test_spiketrains_findable(self):

        # same test as `test_spread_0` with the addition of
        # a neo structure: we must not overwrite the spiketrain
        # list of the segment before determining the index

        sampling_rate = 1 / pq.s

        segment = neo.Segment()

        segment.spiketrains = [
            neo.SpikeTrain([1, 5, 9, 11, 16, 19] * pq.s, t_stop=20 * pq.s),
            neo.SpikeTrain([1, 4, 8, 12, 16, 18] * pq.s, t_stop=20 * pq.s)
        ]

        segment.create_relationship()

        correct_annotations = np.array([[2, 1, 1, 1, 2, 1], [2, 1, 1, 1, 2,
                                                             1]])

        self._test_template(segment.spiketrains,
                            correct_annotations,
                            sampling_rate,
                            spread=0,
                            mode='delete',
                            in_place=True,
                            deletion_threshold=2)
Example #6
0
    def test_unidirectional_uplinks(self):

        # same test as `test_spiketrains_findable` but the spiketrains
        # are rescaled first
        # the rescaled spiketrains have a unidirectional uplink to segment
        # check that this does not cause an error
        # check that a UserWarning is issued in this case

        sampling_rate = 1 / pq.s

        segment = neo.Segment()

        segment.spiketrains = [
            neo.SpikeTrain([1, 5, 9, 11, 16, 19] * pq.s, t_stop=20 * pq.s),
            neo.SpikeTrain([1, 4, 8, 12, 16, 18] * pq.s, t_stop=20 * pq.s)
        ]

        segment.create_relationship()

        spiketrains = [st.rescale(pq.s) for st in segment.spiketrains]

        correct_annotations = np.array([[2, 1, 1, 1, 2, 1], [2, 1, 1, 1, 2,
                                                             1]])

        with self.assertWarns(UserWarning):
            self._test_template(spiketrains,
                                correct_annotations,
                                sampling_rate,
                                spread=0,
                                mode='delete',
                                in_place=True,
                                deletion_threshold=2)
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
Example #8
0
    def recording(self, port_name, t_start=None):
        """
        Returns the recorded data for the given port name

        Parameters
        ----------
        port_name : str
            The name of the port (or state-variable) to retrieve the recorded
            data for

        Returns
        -------
        recording : neo.Segment
            The recorded data in a neo.Segment
        """

        pyNN_data = self.get_data().segments[0]
        recording = neo.Segment()
        communicates, _ = self._get_port_details(port_name)
        if communicates == 'event':
            for st in pyNN_data.spiketrains:
                # FIXME: At some point we need to be able to specify multiple
                # event outputs
                if st.annotations:
                    if t_start is not None:
                        st = st[st > t_start]
                    recording.spiketrains.append(st)
        else:
            for asig in pyNN_data.analogsignals:
                # FIXME: Not sure if this will work
                if asig.annotations['name'] == port_name:
                    recording.analogsignals.append(asig)
        return recording
def save_sessions(fname, corr_coef, n_sessions=5):

    for i in range(n_sessions):
        r = neo.AsciiSpikeTrainIO(filename=fname.format(i))
        spiketrains = generate_correlated_spike_trains(corr_coef, 50 * Hz, 1000 * ms, 10)
        seg = neo.Segment()
        seg.spiketrains = spiketrains
        r.write_segment(seg)
Example #10
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)]

    chx = neo.ChannelIndex(index=1, name='T0')

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

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

    block = neo.Block()
    block.segments = segments
    block.channel_indexes = [chx]

    # 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 chx.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
 def test_get_fp_start_ttl(self, mock_tdt):
     event = neo.Event(times=[1,2,3]*pq.s, name='A')
     segment = neo.Segment()
     segment.events.append(event)
     segment.events.append(event)
     block = neo.Block()
     block.segments.append(segment)
     mock_tdt.return_value = block
     self.assertEqual(1, GetBehavioralEvents().get_fp_start_ttl('/path/to/data/', event_name='A'))
     self.assertEqual(1, GetBehavioralEvents().get_fp_start_ttl('/path/to/data/', event_idx=0))
Example #12
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
Example #13
0
def segment_from_recording_device(devices,
                                  variables_to_include,
                                  id_lists,
                                  t_stop,
                                  name="segment00"):
    """
    Extract data from a NEST recording device and return it as a Neo Segment object.    
    """
    def get_data(device, variable, id_list):
        events = nest.GetStatus(device, 'events')[0]
        ids = events['senders']
        values = events[variable]
        data = {}
        for id in id_list:
            data[id] = values[ids == id]
        assert len(data) > 0
        return data

    segment = neo.Segment(name=name, rec_datetime=datetime.now())

    for device, variable, id_list in zip(devices, variables_to_include,
                                         id_lists):
        print(name, device, variable)
        data = get_data(device, variable, id_list)
        if variable == 'times':
            print("  adding spiketrain")
            id0 = min(id_list)
            segment.spiketrains = [
                neo.SpikeTrain(spiketrain,
                               t_start=0.0,
                               t_stop=t_stop,
                               units='ms',
                               source_id=id,
                               source_index=id - id0)
                for id, spiketrain in data.items()
            ]
        else:
            print("  adding signal")
            source_ids = np.array(id_list)
            channel_indices = source_ids - source_ids.min()
            signal_array = np.vstack(data.values()).T
            segment.analogsignalarrays = [
                neo.AnalogSignalArray(signal_array,
                                      units='mV',
                                      t_start=0.0 * ms,
                                      sampling_period=0.1 * ms,
                                      name=variable,
                                      channel_index=channel_indices,
                                      source_ids=source_ids)
            ]
    return segment
Example #14
0
 def test_GetImagingDataTTL(self, mock_tdt):
     event1 = neo.Event(times=[1, 2, 3] * pq.s, name='A')
     event2 = neo.Event(times=[4, 5, 6] * pq.s, name='B')
     event3 = neo.Event(times=[7, 8, 9] * pq.s, name='C')
     segment = neo.Segment()
     segment.events.append(event1)
     segment.events.append(event2)
     segment.events.append(event3)
     block = neo.Block()
     block.segments.append(segment)
     mock_tdt.return_value = block
     self.assertEqual(
         3, GetImagingDataTTL('/dpath/', time_idx=-1, event_name='A'))
     self.assertEqual(
         6, GetImagingDataTTL('/dpath/', time_idx=-1, event_idx=1))
Example #15
0
 def recordings(self, t_start=None):
     seg = neo.Segment(description="Simulation of '{}' cell".format(
         self._nineml.name,
         ('from {}'.format(t_start) if t_start is not None else '')))
     for port_name in self._recorders:
         if port_name == self.code_generator.REGIME_VARNAME:
             continue
         sig = self.recording(port_name, t_start=t_start)
         if isinstance(sig, neo.AnalogSignal):
             seg.analogsignals.append(sig)
         else:
             seg.spiketrains.append(sig)
     try:
         seg.epochs.append(self.regime_epochs())
     except Pype9RegimeTransitionsNotRecordedError:
         pass
     return seg
Example #16
0
    def __append_current_segment(self, block, variables, view_indexes, clear):
        # build segment for the current data to be gathered in
        segment = neo.Segment(name="segment{}".format(
            get_simulator().segment_counter),
                              description=self.__population.describe(),
                              rec_datetime=datetime.now())

        # sort out variables for using
        variables = self._clean_variables(variables)

        for variable in variables:
            if variable == SPIKES:
                self.__read_in_spikes(
                    segment=segment,
                    spikes=self.get_spikes(),
                    t=get_simulator().get_current_time(),
                    n_neurons=self.__population.size,
                    recording_start_time=self._recording_start_time,
                    sampling_interval=self.__spike_sampling_interval,
                    indexes=view_indexes,
                    label=self.__population.label)
            elif variable == REWIRING:
                self.__read_in_event(
                    segment=segment,
                    event_array=self.get_events(variable),
                    variable=variable,
                    recording_start_time=self._recording_start_time)
            else:
                (data, data_indexes, sampling_interval) = \
                    self.get_recorded_matrix(variable)
                self.__read_in_signal(
                    segment=segment,
                    block=block,
                    signal_array=data,
                    data_indexes=data_indexes,
                    view_indexes=view_indexes,
                    variable=variable,
                    recording_start_time=self._recording_start_time,
                    sampling_interval=sampling_interval,
                    units=self._get_units(variable),
                    label=self.__population.label)
        block.segments.append(segment)

        if clear:
            self._clear_recording(variables)
def spike_array_to_neo(spike_array, population, t_stop):
    """
    Convert the spike array produced by PyNN 0.7 to a Neo Block
    (the data format used by PyNN 0.8)
    """
    from datetime import datetime
    segment = neo.Segment(name="I-F curve data", rec_datetime=datetime.now())
    segment.spiketrains = []
    for index in range(len(population)):
        segment.spiketrains.append(
            neo.SpikeTrain(spike_array[:, 1][spike_array[:, 0] == index],
                           t_start=0.0,
                           t_stop=t_stop,
                           units='ms',
                           source_index=index))
    data = neo.Block(name="I-F curve data")
    data.segments.append(segment)
    return data
Example #18
0
    def test_remove_spiketrain(self):
        unit = neo.Unit()
        segment = neo.Segment()

        s = neo.Spike(0 * pq.s)
        unit.spikes.append(s)
        segment.spikes.append(s)
        s.unit = unit
        s.segment = segment

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

        tools.remove_from_hierarchy(st)
        self.assertTrue(s in unit.spikes)
        self.assertTrue(s in segment.spikes)
        self.assertFalse(st in unit.spiketrains)
        self.assertFalse(st in segment.spiketrains)
    def _run_simulations(self, model):
        """For each step in the protocol, run simulation and store recordings"""
        recordings = neo.Block()
        print("Total protocols: {}".format(len(self.protocol)))
        for idx, item in enumerate(self.protocol.items()):
            step_name = item[0]
            step = item[1]
            segment = neo.Segment(name=step_name)
            recordings.segments.append(segment)
            segment.block = recordings

            print("{}. Current protocol: {}".format(idx+1, step_name))
            model.inject_current(step["stimuli"])
            model.run(tstop=step["total_duration"])
            signal = model.get_membrane_potential()
            stimulus_on =  neo.Epoch(times=step["stimuli"]["delay"]*ms,
                                     durations=step["stimuli"]["duration"]*ms,
                                     labels="stimulus")
            segment.analogsignals.append(signal)
            segment.epochs.append(stimulus_on)
        return recordings
Example #20
0
def spike_detector_to_neo(spike_detector, t_stop, label=""):
    """
    Convert the spikes recorded by NEST to a Neo Block
    """
    from datetime import datetime

    segment = neo.Segment(name=label, rec_datetime=datetime.now())
    segment.spiketrains = []
    events = nest.GetStatus(spike_detector, 'events')[0]
    ids = events['senders']
    values = events['times']
    for id in np.unique(ids):
        spike_times = values[ids == id]
        segment.spiketrains.append(
            neo.SpikeTrain(spike_times,
                           t_start=0.0,
                           t_stop=t_stop,
                           units='ms',
                           source_id=int(id)))
    data = neo.Block(name=label)
    data.segments.append(segment)
    return data
Example #21
0
def test_load_save():
    n_channels = 5
    n_samples = 20
    n_spikes = 50
    fname = '/tmp/test_phy.exdir'
    if os.path.exists(fname):
        shutil.rmtree(fname)
    wf = np.random.random((n_spikes, n_channels, n_samples))
    ts = np.sort(np.random.random(n_spikes))
    t_stop = np.ceil(ts[-1])
    sptr = neo.SpikeTrain(times=ts, units='s', waveforms=wf * pq.V,
                          t_stop=t_stop, **{'group_id': 0})
    blk = neo.Block()
    seg = neo.Segment()
    seg.duration = t_stop
    blk.segments.append(seg)
    chx = neo.ChannelIndex(index=range(n_channels), **{'group_id': 0})
    blk.channel_indexes.append(chx)
    sptr.channel_index = chx
    unit = neo.Unit()
    unit.spiketrains.append(sptr)
    chx.units.append(unit)
    seg.spiketrains.append(sptr)
    epo = neo.Epoch()
    if os.path.exists(fname):
        shutil.rmtree(fname)
    io = neo.ExdirIO(fname)
    io.write_block(blk)
    wfswap = wf.swapaxes(1, 2)
    m = NeoModel(fname, overwrite=True)
    assert np.array_equal(m.spike_times, ts)
    assert np.array_equal(m.waveforms, wfswap)
    m.save()
    m2 = NeoModel(fname, overwrite=True)
    assert np.array_equal(m2.spike_times, ts)
    assert np.array_equal(m2.waveforms, wfswap)
    assert np.array_equal(m2.features, m.features)
    assert np.array_equal(m2.amplitudes, m.amplitudes)
    assert np.array_equal(m2.spike_clusters, m.spike_clusters)
Example #22
0
    def ButSaveViewClick(self):  # TODO finish save

        if self.ChkSaveRec1.isChecked():
            RecordFile, _ = QFileDialog.getSaveFileName(
                self, "Rec1 file", "", "NixIO (*.h5)")
            if RecordFile:
                out_f = neo.io.NixIO(filename=RecordFile)
                out_f.write_block(self.rec.Block)
                out_f.close()

        if self.ChkSaveRec2.isChecked():
            RecordFile, _ = QFileDialog.getSaveFileName(
                self, "Rec2 file", "", "NixIO (*.h5)")
            if RecordFile:
                out_f = neo.io.NixIO(filename=RecordFile)
                out_f.write_block(self.rec2.Block)
                out_f.close()

        if self.ChkSaveView.isChecked():
            RecordFile, _ = QFileDialog.getSaveFileName(
                self, "Current View file", "", "NixIO (*.h5)")
            if RecordFile:
                Tstart = self.SLTStart.value() * pq.s
                Tstop = self.SLTStop.value() * pq.s

                out_seg = neo.Segment(name='NewSeg')

                for sl in self.pltrec.Slots:
                    SName = sl.DispName
                    print SName
                    sig = sl.rec.Signal(SName)
                    out_seg.analogsignals.append(sig.time_slice(Tstart, Tstop))

                out_bl = neo.Block(name='NewBlock')
                out_bl.segments.append(out_seg)
                out_f = neo.io.NixIO(filename='Test_out.h5')
                out_f.write_block(out_bl)
                out_f.close()
Example #23
0
def build_block(data_file):
    """(plaintextfile_path) -> neo.core.block.Block
    Thie function reads a plain text file (data_file) with the data exported (per waveform) from Plexon Offline Sorter after sorting and returns a neo.Block with spiketrains ordered in any number of 10 minute segments.

    For practicality and Plexon management of channels names, units and channels have been ignored in the block structure."""

    raw_data = pd.read_csv(data_file, sep=',', header=0, usecols=[0, 1, 2])
    ord_times = raw_data.groupby(['Channel Name', 'Unit'])['Timestamp']
    new_block = neo.Block()
    chx = neo.ChannelIndex(index=None, name='MEA_60')
    new_block.channel_indexes.append(chx)
    # Next line will not work properly if last spike happens
    # exactly at the end of the recording
    num_segments = range(int(raw_data['Timestamp'].max() // 600 + 1))
    for ind in num_segments:
        seg = neo.Segment(name='segment {}'.format(ind), index=ind)
        new_block.segments.append(seg)

    for name, group in ord_times:
        time_stamps = ord_times.get_group(name).values
        inter = 600  # Number of seconds in 10 minutes
        first_seg = neo.SpikeTrain(
            time_stamps[time_stamps < inter], units='sec', t_start=0,  t_stop=inter)
        new_block.segments[0].spiketrains.append(first_seg)
        new_unit = neo.Unit(name=name)
        sptrs = [first_seg]
        for seg in num_segments[1:-1]:
            seg_train = neo.SpikeTrain(time_stamps[(time_stamps > seg * inter) & (time_stamps < ((seg + 1) * inter))],
                                       units='sec', t_start=(seg * inter), t_stop=((seg + 1) * inter))
            new_block.segments[seg].spiketrains.append(seg_train)
            sptrs.append(seg_train)
        last_seg = neo.SpikeTrain(time_stamps[time_stamps > (num_segments[-1] * inter)], units='sec',
                                  t_start=(num_segments[-1]) * inter, t_stop=((num_segments[-1] + 1) * inter))
        new_block.segments[num_segments[-1]].spiketrains.append(last_seg)
        sptrs.append(last_seg)
        new_unit.spiketrains = sptrs
        chx.units.append(new_unit)
    return new_block
Example #24
0
    def InitContMeas(self, Vin, Fs, Refresh, RecDC=True, GenTestSig=False):
        #  Init Neo record
        out_seg = neo.Segment(name='NewSeg')

        if RecDC:
            self.EventContDcDone = self.ContDcDoneCallback
            for chk, chi, in sorted(self.DCChannelIndex.iteritems()):
                name = chk
                sig = neo.AnalogSignal(signal=np.empty((0, 1), float),
                                       units=pq.V,
                                       t_start=0*pq.s,
                                       sampling_rate=Fs*pq.Hz,
                                       name=name)
                out_seg.analogsignals.append(sig)

        self.ContRecord = NeoRecord(Seg=out_seg, UnitGain=1)

        #  Lauch adquisition
        self.SetBias(Vsig=Vin)
        self.GetContinuousCurrent(Fs=Fs,
                                  Refresh=Refresh,
                                  GenTestSig=GenTestSig)
        self.CharactRunning = True
Example #25
0
def runtest_neo(io, N):
    times = []
    blk = neo.Block()
    seg = neo.Segment()
    blk.segments.append(seg)
    step = 1
    if N >= 10:
        step = N // 10
    Ns = list()
    for n in range(0, N + step, step):
        seg.analogsignals = []
        for ni in range(n):
            seg.analogsignals.append(
                neo.AnalogSignal(signal=[0],
                                 units="V",
                                 sampling_rate=1 * pq.Hz))
        t0 = time()
        io.write_block(blk)
        times.append(time() - t0)
        Ns.append(n)
        print(f" :: {n}/{N} {int(n/N*100):3d}%", end="\r")

    print(f" :: Last write time: {times[-1]:7.05f} s")

    print("Verifying neo-nix file")
    assert len(io.nix_file.blocks) == 1
    blk = io.nix_file.blocks[0]
    assert blk.type == "neo.block"

    assert len(blk.groups) == 1
    grp = blk.groups[0]
    assert grp.type == "neo.segment"

    assert len(blk.data_arrays) == N
    assert len(grp.data_arrays) == N

    return Ns, times
Example #26
0
File: sonata.py Project: wau/PyNN
    def read(self):
        """
        Read all data* from a SONATA dataset directory.

        Returns a list of Blocks.

        (*Currently only spike data supported)
        """
        file_path = join(self.base_dir, self.spike_file)
        block = neo.Block(file_origin=file_path)
        segment = neo.Segment(file_origin=file_path)
        spikes_file = h5py.File(file_path, 'r')
        for gid in np.unique(spikes_file['spikes']['gids']):
            index = spikes_file['spikes']['gids'].value == gid
            spike_times = spikes_file['spikes']['timestamps'][index]
            segment.spiketrains.append(
                neo.SpikeTrain(spike_times,
                               t_stop = spike_times.max() + 1.0,
                               t_start=0.0,
                               units='ms',
                               source_id=gid)
            )
        block.segments.append(segment)
        return [block]
Example #27
0
    def __init__(self, RecordFile=None, Seg=None):
        self.SigNames = {}
        self.EventNames = {}
        if RecordFile is None:
            if Seg is None:
                self.Seg = neo.Segment('New Seg')
            else:
                self.Seg = Seg
                self.UpdateEventDict()
                self.UpdateSignalsDict()
            return

        ftype = RecordFile.split('.')[-1]
        if ftype == 'h5':
            self.RecFile = neo.io.NixIO(filename=RecordFile, mode='ro')
            Block = self.RecFile.read_block()
        elif ftype == 'smr':
            self.RecFile = neo.io.Spike2IO(filename=RecordFile)
            Block = self.RecFile.read()[0]

        self.Seg = Block.segments[0]

        self.UpdateSignalsDict()
        self.UpdateEventDict()
    def test1(self):
        """Create clu and fet files based on spiketrains in a block.

        Checks that
            Files are created
            Converted to samples correctly
            Missing sampling rate are taken from IO reader default
            Spiketrains without cluster info are assigned to cluster 0
            Spiketrains across segments are concatenated
        """
        block = neo.Block()
        segment = neo.Segment()
        segment2 = neo.Segment()
        block.segments.append(segment)
        block.segments.append(segment2)

        # Fake spiketrain 1
        st1 = neo.SpikeTrain(times=[.002, .004, .006], units='s', t_stop=1.)
        st1.annotations['cluster'] = 0
        st1.annotations['group'] = 0
        wff = np.array([[11.3, 0.2], [-0.3, 12.3], [3.0, -2.5]])
        st1.annotations['waveform_features'] = wff
        segment.spiketrains.append(st1)

        # Create empty directory for writing
        if not os.path.exists(self.dirname):
            os.mkdir(self.dirname)
        delete_test_session(self.dirname)

        # Create writer
        kio = KlustaKwikIO(filename=os.path.join(self.dirname, 'base2'),
                           sampling_rate=1000.)
        kio.write_block(block)

        # Check files were created
        for fn in ['.fet.0', '.clu.0']:
            self.assertTrue(
                os.path.exists(os.path.join(self.dirname, 'base2' + fn)))

        # Check files contain correct content
        fi = file(os.path.join(self.dirname, 'base2.fet.0'))

        # first line is nbFeatures
        self.assertEqual(fi.readline(), '2\n')

        # Now check waveforms and times are same
        data = fi.readlines()
        new_wff = []
        new_times = []
        for line in data:
            line_split = line.split()
            new_wff.append([float(val) for val in line_split[:-1]])
            new_times.append(int(line_split[-1]))
        self.assertEqual(new_times, [2, 4, 6])
        assert_arrays_almost_equal(wff, np.array(new_wff), .00001)

        # Clusters on group 0
        data = file(os.path.join(self.dirname, 'base2.clu.0')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [1, 0, 0, 0])

        # Now read the features and test same
        block = kio.read_block()
        train = block.segments[0].spiketrains[0]
        assert_arrays_almost_equal(wff, train.annotations['waveform_features'],
                                   .00001)

        # Empty out test session again
        delete_test_session(self.dirname)
    def test1(self):
        """Create clu and fet files based on spiketrains in a block.

        Checks that
            Files are created
            Converted to samples correctly
            Missing sampling rate are taken from IO reader default
            Spiketrains without cluster info are assigned to cluster 0
            Spiketrains across segments are concatenated
        """
        block = neo.Block()
        segment = neo.Segment()
        segment2 = neo.Segment()
        block.segments.append(segment)
        block.segments.append(segment2)

        # Fake spiketrain 1, will be sorted
        st1 = neo.SpikeTrain(times=[.002, .004, .006], units='s', t_stop=1.)
        st1.annotations['cluster'] = 0
        st1.annotations['group'] = 0
        segment.spiketrains.append(st1)

        # Fake spiketrain 1B, on another segment. No group specified,
        # default is 0.
        st1B = neo.SpikeTrain(times=[.106], units='s', t_stop=1.)
        st1B.annotations['cluster'] = 0
        segment2.spiketrains.append(st1B)

        # Fake spiketrain 2 on same group, no sampling rate specified
        st2 = neo.SpikeTrain(times=[.001, .003, .011], units='s', t_stop=1.)
        st2.annotations['cluster'] = 1
        st2.annotations['group'] = 0
        segment.spiketrains.append(st2)

        # Fake spiketrain 3 on new group, with different sampling rate
        st3 = neo.SpikeTrain(times=[.05, .09, .10], units='s', t_stop=1.)
        st3.annotations['cluster'] = -1
        st3.annotations['group'] = 1
        segment.spiketrains.append(st3)

        # Fake spiketrain 4 on new group, without cluster info
        st4 = neo.SpikeTrain(times=[.005, .009], units='s', t_stop=1.)
        st4.annotations['group'] = 2
        segment.spiketrains.append(st4)

        # Create empty directory for writing
        delete_test_session()

        # Create writer with default sampling rate
        kio = KlustaKwikIO(filename=os.path.join(self.dirname, 'base1'),
                           sampling_rate=1000.)
        kio.write_block(block)

        # Check files were created
        for fn in ['.fet.0', '.fet.1', '.clu.0', '.clu.1']:
            self.assertTrue(
                os.path.exists(os.path.join(self.dirname, 'base1' + fn)))

        # Check files contain correct content
        # Spike times on group 0
        data = file(os.path.join(self.dirname, 'base1.fet.0')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [0, 2, 4, 6, 1, 3, 11, 106])

        # Clusters on group 0
        data = file(os.path.join(self.dirname, 'base1.clu.0')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [2, 0, 0, 0, 1, 1, 1, 0])

        # Spike times on group 1
        data = file(os.path.join(self.dirname, 'base1.fet.1')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [0, 50, 90, 100])

        # Clusters on group 1
        data = file(os.path.join(self.dirname, 'base1.clu.1')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [1, -1, -1, -1])

        # Spike times on group 2
        data = file(os.path.join(self.dirname, 'base1.fet.2')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [0, 5, 9])

        # Clusters on group 2
        data = file(os.path.join(self.dirname, 'base1.clu.2')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [1, 0, 0])

        # Empty out test session again
        delete_test_session()
Example #30
0
import neo
import quantities as pq
import numpy as np
import nixio as nix
from neo.io import NixIO

block = neo.Block()
chn_index = neo.ChannelIndex([0, 1, 2],
                             channel_names=["a", "b", "c"],
                             channel_ids=[1, 2, 3])
block.channel_indexes.append(chn_index)
unit = neo.Unit(name="x", description="contain1st")
chn_index.units.append(unit)

seg = neo.Segment()
asig = neo.AnalogSignal(name="signal",
                        signal=[1.1, 1.2, 1.5],
                        units="mV",
                        sampling_rate=1 * pq.Hz)
seg.analogsignals.append(asig)
asig2 = neo.AnalogSignal(name="signal2",
                         signal=[1.1, 1.2, 2.5],
                         units="mV",
                         sampling_rate=1 * pq.Hz)
seg.analogsignals.append(asig2)
irasig = neo.IrregularlySampledSignal(name="irsignal",
                                      signal=np.random.random((100, 2)),
                                      units="mV",
                                      times=np.cumsum(
                                          np.random.random(100) * pq.s))
seg.irregularlysampledsignals.append(irasig)