示例#1
0
def hh_compartment_with_channels():
    filename = 'hh_compartment_with_channels.h5'
    compartment = nsdf.ModelComponent('compartment', uid=uuid1().hex)
    na_channel = nsdf.ModelComponent('NaChannel', uid=uuid1().hex,
                                     parent=compartment)
    k_channel =  nsdf.ModelComponent('KChannel', uid=uuid1().hex,
                                     parent=compartment)
    writer = nsdf.NSDFWriter(filename, mode='w')
    writer.add_modeltree(compartment)
    source_ds = writer.add_static_ds(compartment.name, [compartment.uid])
    # Add membrane resitance
    data_obj = nsdf.StaticData('Rm', unit='Mohm')
    data_obj.put_data(compartment.uid, 1e3)
    writer.add_static_data(source_ds, data_obj)
    # Add membrane capacitance
    data_obj = nsdf.StaticData('Cm', unit='pF')
    data_obj.put_data(compartment.uid, 0.9)
    writer.add_static_data(source_ds, data_obj)
    # Add leak reversal potential
    data_obj = nsdf.StaticData('Em', unit='mV')
    data_obj.put_data(compartment.uid, -65.0)
    writer.add_static_data(source_ds, data_obj)
    # Add membrane potential
    source_ds = writer.add_uniform_ds(compartment.name, [compartment.uid])
    data_obj = nsdf.UniformData('Vm', unit='mV')
    data_obj.put_data(compartment.uid,
                      np.random.uniform(low=-67.0, high=-63.0, size=100))
    data_obj.set_dt(0.1, unit='ms')
    writer.add_uniform_data(source_ds, data_obj)
    source_ds = writer.add_uniform_ds('channel', [na_channel.uid,
                                                  k_channel.uid])
    data_obj = nsdf.UniformData('Ik', unit='nA', dt=0.1, tunit='ms')
    data_obj.update_source_data_dict([(na_channel.uid, ina),
                                      (k_channel.uid, ik)])
    writer.add_uniform_data(source_ds, data_obj)
示例#2
0
def create_example():
    # First create the model tree
    model = nsdf.ModelComponent('LeisureCenter', uid=uuid1().hex)
    poolroom = nsdf.ModelComponent('PoolRoom', uid=uuid1().hex, parent=model)
    tables = []
    balls = []
    for ii in range(2):
        tables.append(
            nsdf.ModelComponent('table_{}'.format(ii),
                                uid=uuid1().hex,
                                parent=poolroom))
        for jj in range(3):
            balls.append(
                nsdf.ModelComponent('ball_{}'.format(jj),
                                    uid=uuid1().hex,
                                    parent=tables[-1]))
    id_path_dict = model.get_id_path_dict()
    path_id_dict = {value: key for key, value in id_path_dict.items()}

    # Create the NSDF writer object
    writer = nsdf.NSDFWriter('poolroom.h5', mode='w')
    writer.add_modeltree(model)
    dataobj = nsdf.StaticData('area', unit='m^2')
    dataobj.put_data(poolroom.uid, [100.0])
    source_ds = writer.add_static_ds('rooms', [poolroom.uid])
    writer.add_static_data(source_ds, dataobj)
    source_ds = writer.add_static_ds('tables', [tab.uid for tab in tables])
    dataobj = nsdf.StaticData('height', unit='m')
    for tab in tables:
        dataobj.put_data(tab.uid, 1.2)
    writer.add_static_data(source_ds, dataobj)
    source_ds = writer.add_nonuniform_ds_1d('tables', 'players',
                                            [tab.uid for tab in tables])
    dataobj = nsdf.NonuniformData('players',
                                  unit='item',
                                  tunit='hour',
                                  dtype=np.int32)
    for tab in tables:
        times = np.cumsum(np.random.exponential(1 / 10.0, size=10))
        dataobj.put_data(tab.uid, (np.random.randint(10, size=10), times))
    writer.add_nonuniform_1d(source_ds, dataobj,
                             {tab.uid: tab.name
                              for tab in tables})
    source_ds = writer.add_uniform_ds('balls', [ball.uid for ball in balls])
    dataobj = nsdf.UniformData('x', unit='cm')
    for ball in balls:
        dataobj.put_data(ball.uid, np.random.rand(10) * 10)
    dataobj.set_dt(1e-2, 's')
    writer.add_uniform_data(source_ds, dataobj)

    source_ds = writer.add_event_ds_1d('balls', 'hit',
                                       [ball.uid for ball in balls])
    dataobj = nsdf.EventData('hit', unit='s')
    source_name_dict = {}
    for ball in balls:
        source_name_dict[ball.uid] = '{}_{}'.format(ball.name, ball.uid)
        data = np.cumsum(np.random.exponential(1 / 100.0, size=10))
        dataobj.put_data(ball.uid, data)
    writer.add_event_1d(source_ds, dataobj, source_name_dict)
示例#3
0
 def _prepare_model_tree(self):
     neo_model = nsdf.ModelComponent('neo', uid=uuid1().hex)
     blocks_model = nsdf.ModelComponent('blocks',
                                        uid=uuid1().hex,
                                        parent=neo_model)
     segments_model = nsdf.ModelComponent('segments',
                                          uid=uuid1().hex,
                                          parent=neo_model)
     return neo_model, blocks_model, segments_model
示例#4
0
    def _prepare_model_tree(self, writer):
        neo_model = nsdf.ModelComponent('neo', uid=uuid1().hex)
        self._write_model_component(neo_model, writer)

        blocks_model = nsdf.ModelComponent('blocks', uid=uuid1().hex, parent=neo_model)
        self._write_model_component(blocks_model, writer)

        segments_model = nsdf.ModelComponent('segments', uid=uuid1().hex, parent=neo_model)
        self._write_model_component(segments_model, writer)

        return neo_model, blocks_model, segments_model
示例#5
0
    def _create_signal_data_sources(self, model, r_signal, uid, writer):
        channels = []
        channels_model = nsdf.ModelComponent(name='channels', uid=uuid1().hex, parent=model)
        name_pattern = '{{:0{}d}}'.format(self._number_of_digits(max(len(r_signal) - 1, 0)))
        for i in range(len(r_signal)):
            channels.append(nsdf.ModelComponent(name_pattern.format(i),
                                                uid=uuid1().hex,
                                                parent=channels_model))

        source_ds = writer.add_uniform_ds(uid, [channel.uid for channel in channels])
        return channels_model, channels, source_ds
示例#6
0
    def _write_block_children(self, block, block_model, writer):
        segments_model = nsdf.ModelComponent(name='segments', uid=uuid1().hex, parent=block_model)
        self._write_model_component(segments_model, writer)
        name_pattern = self._name_pattern(len(block.segments))
        for i, segment in enumerate(block.segments):
            self.write_segment(segment=segment, name=name_pattern.format(i),
                               writer=writer, parent=segments_model)

        channel_indexes_model = nsdf.ModelComponent(
            name='channel_indexes', uid=uuid1().hex, parent=block_model)
        self._write_model_component(channel_indexes_model, writer)
        name_pattern = self._name_pattern(len(block.channel_indexes))
        for i, channelindex in enumerate(block.channel_indexes):
            self.write_channelindex(channelindex=channelindex, name=name_pattern.format(i),
                                    writer=writer, parent=channel_indexes_model)
示例#7
0
    def write_block(self, block=None, name='0', writer=None, parent=None):
        """
        Write a Block to the file

        :param block: Block to be written
        :param name: Name for block representation in NSDF model tree (optional)
        :param writer: NSDFWriter instance (optional)
        :param parent: NSDF ModelComponent which will be the parent of block NSDF representation (optional)
        """
        if not isinstance(block, Block):
            raise ValueError("Must provide a Block to write.")

        if writer is None:
            writer = self._init_writing()

        single_block = False
        if parent is None:
            neo_model, parent, segments_model = self._prepare_model_tree()
            single_block = True

        block_model = nsdf.ModelComponent(name, uid=uuid1().hex, parent=parent)

        self._write_block_children(block, block_model, writer)
        self._write_container_metadata(block, block_model)

        if single_block:
            writer.add_modeltree(neo_model)
示例#8
0
    def write_block(self, block=None, name='0', writer=None, parent=None):
        """
        Write a Block to the file

        :param block: Block to be written
        :param name: Name for block representation in NSDF model tree (optional)
        :param writer: NSDFWriter instance (optional)
        :param parent: NSDF ModelComponent which will be the parent of block NSDF representation (optional)
        """
        raise NotImplementedError("Implementation not yet updated for Neo 0.9")
        if not isinstance(block, Block):
            raise ValueError("Must provide a Block to write.")

        if writer is None:
            writer = self._init_writing()

        if parent is None:
            neo_model, parent, segments_model = self._prepare_model_tree(writer)

        block_model = nsdf.ModelComponent(name, uid=uuid1().hex, parent=parent)
        self._write_container_metadata(block, block_model)
        self._write_model_component(block_model, writer)

        self._write_block_children(block, block_model, writer)

        self._clean_nsdfio_annotations(block)
示例#9
0
    def write_analogsignal(self, signal, name, writer, parent):
        """
        Write an AnalogSignal to the file

        :param signal: AnalogSignal to be written
        :param name: Name for signal representation in NSDF model tree
        :param writer: NSDFWriter instance
        :param parent: NSDF ModelComponent which will be the parent of signal NSDF representation
        """
        raise NotImplementedError("Implementation not yet updated for Neo 0.9")
        uid = uuid1().hex
        model = nsdf.ModelComponent(name, uid=uid, parent=parent)

        if signal.annotations.get('nsdfio_uid') is not None:
            model.attrs['reference_to'] = signal.annotations['nsdfio_uid']
            self._write_model_component(model, writer)
            return

        self._write_basic_metadata(model, signal)
        signal.annotations['nsdfio_uid'] = uid

        r_signal = np.swapaxes(signal, 0, 1)
        channels_model, channels, source_ds = self._create_signal_data_sources(
            model, r_signal, uid, writer)
        self._write_signal_data(model, channels, r_signal, signal, source_ds, writer)

        self._write_model_component(model, writer)
        self._write_model_component(channels_model, writer)
        for channel_model in channels:
            self._write_model_component(channel_model, writer)
示例#10
0
    def write_segment(self, segment=None, name='0', writer=None, parent=None):
        """
        Write a Segment to the file

        :param segment: Segment to be written
        :param name: Name for segment representation in NSDF model tree (optional)
        :param writer: NSDFWriter instance (optional)
        :param parent: NSDF ModelComponent which will be the parent of segment NSDF representation (optional)
        """
        raise NotImplementedError("Implementation not yet updated for Neo 0.9")
        if not isinstance(segment, Segment):
            raise ValueError("Must provide a Segment to write.")

        if writer is None:
            writer = self._init_writing()

        single_segment = False
        if parent is None:
            neo_model, blocks_model, parent = self._prepare_model_tree(writer)
            single_segment = True

        model = nsdf.ModelComponent(name, uid=uuid1().hex, parent=parent)
        self._write_container_metadata(segment, model)
        self._write_model_component(model, writer)

        self._write_segment_children(model, segment, writer)

        if single_segment:
            self._clean_nsdfio_annotations(segment)
示例#11
0
 def _write_channelindex_children(self, channelindex, model, writer):
     analogsignals_model = nsdf.ModelComponent(
         name='analogsignals', uid=uuid1().hex, parent=model)
     self._write_model_component(analogsignals_model, writer)
     name_pattern = self._name_pattern(len(channelindex.analogsignals))
     for i, signal in enumerate(channelindex.analogsignals):
         self.write_analogsignal(signal=signal, name=name_pattern.format(i),
                                 parent=analogsignals_model, writer=writer)
示例#12
0
 def _write_block_children(self, block, block_model, writer):
     segments_model = nsdf.ModelComponent(name='segments',
                                          uid=uuid1().hex,
                                          parent=block_model)
     name_pattern = '{{:0{}d}}'.format(
         self._number_of_digits(max(len(block.segments) - 1, 0)))
     for i, segment in enumerate(block.segments):
         self.write_segment(segment=segment,
                            name=name_pattern.format(i),
                            writer=writer,
                            parent=segments_model)
示例#13
0
 def _write_segment_children(self, model, segment, writer):
     analogsignals_model = nsdf.ModelComponent(name='analogsignals',
                                               uid=uuid1().hex,
                                               parent=model)
     name_pattern = '{{:0{}d}}'.format(
         self._number_of_digits(max(len(segment.analogsignals) - 1, 0)))
     for i, signal in enumerate(segment.analogsignals):
         self.write_analogsignal(signal=signal,
                                 name=name_pattern.format(i),
                                 parent=analogsignals_model,
                                 writer=writer)
示例#14
0
    def write_channelindex(self, channelindex, name, writer, parent):
        """
        Write a ChannelIndex to the file

        :param channelindex: ChannelIndex to be written
        :param name: Name for channelindex representation in NSDF model tree
        :param writer: NSDFWriter instance
        :param parent: NSDF ModelComponent which will be the parent of channelindex NSDF representation
        """
        uid = uuid1().hex
        model = nsdf.ModelComponent(name, uid=uid, parent=parent)

        self._write_basic_metadata(model, channelindex)
        self._write_model_component(model, writer)

        self._write_channelindex_arrays(model, channelindex, writer)

        self._write_channelindex_children(channelindex, model, writer)
示例#15
0
    def write_analogsignal(self, signal, name='0', writer=None, parent=None):
        """
        Write an AnalogSignal to the file

        :param signal: AnalogSignal to be written
        :param name: Name for signal representation in NSDF model tree
        :param writer: NSDFWriter instance
        :param parent: NSDF ModelComponent which will be the parent of signal NSDF representation
        """
        uid = uuid1().hex
        model = nsdf.ModelComponent(name, uid=uid, parent=parent)

        r_signal = np.swapaxes(signal, 0, 1)
        channels, source_ds = self._create_signal_data_sources(
            model, r_signal, uid, writer)
        self._write_signal_data(model, channels, r_signal, signal, source_ds,
                                writer)

        self._write_basic_metadata(model, signal)
示例#16
0
def hh_vm():
    model = nsdf.ModelComponent('compartment')
    data_container = nsdf.UniformData('Vm', unit='mV', dt=0.1, tunit='ms')
    data_container.put_data(model.uid, vm_array)                            
    writer = nsdf.NSDFWriter('hh_vm.h5', mode='w')
    writer.set_properties({'title': 'Example Vm recording',
                           'creator': 'user',
                           'software': ['python2.7', 'python-nsdf-0.1'],
                           'method': ['np.random.rand'],
                           'description': 'Randomly generated Vm for a single' \
                                          'compartment',
                           'rights': 'CC-BY-SA',
                           'tstart': datetime.now(),
                           'tend': (datetime.now() + timedelta(seconds=3)),
                           'contributor': ['Chaitanya Chintaluri',
                                           'Daniel Wocjcik',
                                           'Upinder Bhalla']})
                           
    writer.add_modeltree(model)
    source_ds = writer.add_uniform_ds('compartment_population', [model.uid])
    writer.add_uniform_data(source_ds, data_container)
示例#17
0
def hh_compartment():
    filename = 'hh_compartment.h5'
    model = nsdf.ModelComponent('compartment', uid=uuid1().hex)
    writer = nsdf.NSDFWriter(filename, mode='w')
    writer.add_modeltree(model)
    source_ds = writer.add_static_ds(model.name, [model.uid])
    # Add membrane resitance
    data_obj = nsdf.StaticData('Rm', unit='Mohm')
    data_obj.put_data(model.uid, 1e3)
    writer.add_static_data(source_ds, data_obj)
    # Add membrane capacitance
    data_obj = nsdf.StaticData('Cm', unit='pF')
    data_obj.put_data(model.uid, 0.9)
    writer.add_static_data(source_ds, data_obj)
    # Add leak reversal potential
    data_obj = nsdf.StaticData('Em', unit='mV')
    data_obj.put_data(model.uid, -65.0)
    writer.add_static_data(source_ds, data_obj)
    # Add membrane potential
    source_ds = writer.add_uniform_ds(model.name, [model.uid])
    data_obj = nsdf.UniformData('Vm', unit='mV', dt=0.1, tunit='ms')
    data_obj.put_data(model.uid, vm_array)
    writer.add_uniform_data(source_ds, data_obj)