Exemplo n.º 1
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()
Exemplo n.º 2
0
Bp = net.make_probe('B', dt_sample=dt_step)
BpSpikes = net.make_probe('B', data_type='spikes', dt_sample=dt_step)

build_time_end = time.time()

print "starting simulation"
net.run(timesteps * dt_step)

sim_time_end = time.time()
print "\nBuild time: %0.10fs" % (build_time_end - build_time_start)
print "Sim time: %0.10fs" % (sim_time_end - build_time_end)

net.write_data_to_hdf5()

# open up hdf5 file
iom = hdf5io.NeoHdf5IO(filename='data.hd5')

print iom.get_info()
# wtf i know right?
block_as = iom.read_analogsignal()
segment_as = block_as.segments[0]
block_st = iom.read_spiketrain()
segment_st = block_st.segments[0]

import matplotlib.pyplot as plt

plt.clf()
plt.subplot(211)
plt.title('analog signal')
plt.hold(1)
legend = []