def save_to_xml(self, file_name):

        from neuroml.writers import NeuroMLWriter

        NeuroMLWriter.write(self.nml_doc, file_name)

        print("Written to %s" % file_name)
def write_izhikevich(filename="./tmp/SingleIzhikevich_test.nml"):
    nml_doc = NeuroMLDocument(id="SingleIzhikevich")
    nml_filename = filename

    iz0 = IzhikevichCell(id="iz0", v0="-70mV", thresh="30mV", a="0.02", b="0.2", c="-65.0", d="6")

    nml_doc.izhikevich_cells.append(iz0)

    NeuroMLWriter.write(nml_doc, nml_filename)
    validate_neuroml2(nml_filename)
示例#3
0
 def test_convert_morphology(self):
     morph = converter.convert_morphology(self.neuron, positions='auto')
     cell = neuroml.Cell()
     cell.name = self.neuron.name
     cell.id = cell.name
     cell.morphology = morph
     doc = neuroml.NeuroMLDocument()
     doc.cells.append(cell)
     doc.id = 'TestNeuroMLDocument'
     fname = os.path.join(outdir, 'test_morphology_conversion.nml')
     NeuroMLWriter.write(doc, fname)
     print('Wrote', fname)
示例#4
0
 def test_convert_morphology(self):
     morph = converter.convert_morphology(self.neuron, positions='auto')
     cell = neuroml.Cell()
     cell.name = self.neuron.name
     cell.id = cell.name
     cell.morphology = morph
     doc = neuroml.NeuroMLDocument()
     doc.cells.append(cell)
     doc.id = 'TestNeuroMLDocument'
     fname = os.path.join(outdir, 'test_morphology_conversion.nml')        
     NeuroMLWriter.write(doc, fname)
     print 'Wrote', fname
def write_izhikevich(filename="./tmp/SingleIzhikevich_test.nml"):
    nml_doc = NeuroMLDocument(id="SingleIzhikevich")
    nml_filename = filename

    iz0 = IzhikevichCell(id="iz0",
                         v0="-70mV",
                         thresh="30mV",
                         a="0.02",
                         b="0.2",
                         c="-65.0",
                         d="6")

    nml_doc.izhikevich_cells.append(iz0)

    NeuroMLWriter.write(nml_doc, nml_filename)
    validate_neuroml2(nml_filename)
示例#6
0
def generate_neuroml2_from_network(nl_model,
                                   nml_file_name=None,
                                   print_summary=True,
                                   seed=1234,
                                   format='xml',
                                   base_dir=None,
                                   copy_included_elements=False,
                                   target_dir=None,
                                   validate=False):
    """
    Generate and save NeuroML2 file (in either XML or HDF5 format) from the 
    NeuroMLlite description
    """

    print_v("Generating NeuroML2 for %s%s..." %
            (nl_model.id, ' (base dir: %s; target dir: %s)' %
             (base_dir, target_dir) if base_dir or target_dir else ''))

    import neuroml
    from neuroml.hdf5.NetworkBuilder import NetworkBuilder

    neuroml_handler = NetworkBuilder()

    generate_network(nl_model, neuroml_handler, seed=seed, base_dir=base_dir)

    nml_doc = neuroml_handler.get_nml_doc()

    for i in nl_model.input_sources:

        if nml_doc.get_by_id(i.id) == None:
            if i.neuroml2_source_file:

                incl = neuroml.IncludeType(
                    _locate_file(i.neuroml2_source_file, base_dir))
                if not incl in nml_doc.includes:
                    nml_doc.includes.append(incl)

            if i.neuroml2_input:
                input_params = i.parameters if i.parameters else {}

                # TODO make more generic...

                if i.neuroml2_input.lower() == 'pulsegenerator':
                    input = neuroml.PulseGenerator(id=i.id)
                    nml_doc.pulse_generators.append(input)

                elif i.neuroml2_input.lower() == 'pulsegeneratordl':
                    input = neuroml.PulseGeneratorDL(id=i.id)
                    nml_doc.pulse_generator_dls.append(input)

                elif i.neuroml2_input.lower() == 'poissonfiringsynapse':
                    input = neuroml.PoissonFiringSynapse(id=i.id)
                    nml_doc.poisson_firing_synapses.append(input)

                for p in input_params:
                    exec('input.%s = "%s"' %
                         (p, evaluate(input_params[p], nl_model.parameters)))

    for c in nl_model.cells:
        if c.neuroml2_source_file:

            incl = neuroml.IncludeType(
                _locate_file(c.neuroml2_source_file, base_dir))
            found_cell = False
            for cell in nml_doc.cells:
                if cell.id == c.id:
                    nml_doc.cells.remove(
                        cell
                    )  # Better to use imported cell file; will have channels, etc.
                    nml_doc.includes.append(incl)
                    found_cell = True

            if not found_cell:
                for p in nl_model.populations:
                    if p.component == c.id:
                        pass

            if not incl in nml_doc.includes:
                nml_doc.includes.append(incl)
        '''  Needed???
        if c.lems_source_file:      
            incl = neuroml.IncludeType(_locate_file(c.lems_source_file, base_dir))
            if not incl in nml_doc.includes:
                nml_doc.includes.append(incl)'''

        if c.neuroml2_cell:

            cell_params = c.parameters if c.parameters else {}
            # TODO make more generic...
            if c.neuroml2_cell.lower() == 'spikegenerator':
                cell = neuroml.SpikeGenerator(id=c.id)
                nml_doc.spike_generators.append(cell)
            elif c.neuroml2_cell.lower() == 'spikegeneratorpoisson':
                cell = neuroml.SpikeGeneratorPoisson(id=c.id)
                nml_doc.spike_generator_poissons.append(cell)
            elif c.neuroml2_cell.lower() == 'spikegeneratorrefpoisson':
                cell = neuroml.SpikeGeneratorRefPoisson(id=c.id)
                nml_doc.spike_generator_ref_poissons.append(cell)
            else:
                raise Exception(
                    'The neuroml2_cell: %s is not yet supported...' %
                    c.neuroml2_cell)

            for p in cell_params:
                exec('cell.%s = "%s"' %
                     (p, evaluate(cell_params[p], nl_model.parameters)))

    for s in nl_model.synapses:
        if nml_doc.get_by_id(s.id) == None:
            if s.neuroml2_source_file:
                incl = neuroml.IncludeType(
                    _locate_file(s.neuroml2_source_file, base_dir))
                if not incl in nml_doc.includes:
                    nml_doc.includes.append(incl)

    # Look for and add the PyNN based elements to the NeuroMLDocument
    _extract_pynn_components_to_neuroml(nl_model, nml_doc)

    if print_summary:
        # Print info
        print_v(nml_doc.summary())

    # Save to file
    if target_dir == None:
        target_dir = base_dir
    if format == 'xml':
        if not nml_file_name:
            nml_file_name = _locate_file('%s.net.nml' % nml_doc.id, target_dir)
        from neuroml.writers import NeuroMLWriter
        NeuroMLWriter.write(nml_doc, nml_file_name)

    if format == 'hdf5':
        if not nml_file_name:
            nml_file_name = _locate_file('%s.net.nml.h5' % nml_doc.id,
                                         target_dir)
        from neuroml.writers import NeuroMLHdf5Writer
        NeuroMLHdf5Writer.write(nml_doc, nml_file_name)

    print_v("Written NeuroML to %s" % nml_file_name)

    if validate and format == 'xml':

        from pyneuroml import pynml
        success = pynml.validate_neuroml2(nml_file_name,
                                          verbose_validate=False)
        if success:
            print_v('Generated file is valid NeuroML2!')
        else:
            print_v('Generated file is NOT valid NeuroML2!')

    return nml_file_name, nml_doc
示例#7
0
net.populations[1].random_layout.append(
    RandomLayout(id='test2', x=1000, y=1000, z=1000))

print net.to_json()

###   Use a handler which just prints info on positions, etc.

def_handler = DefaultNetworkHandler()

generate_network(net, def_handler)

###   Use a handler which builds a NeuroML 2 representation

from neuroml.hdf5.NetworkBuilder import NetworkBuilder

neuroml_handler = NetworkBuilder()

generate_network(net, neuroml_handler)

nml_doc = neuroml_handler.get_nml_doc()

# Print info
print(nml_doc.summary())

# Save to file
file_name = '%s.net.nml' % nml_doc.id
from neuroml.writers import NeuroMLWriter
NeuroMLWriter.write(nml_doc, file_name)

print("Written NeuroML to %s" % file_name)
示例#8
0
    from neuromllite.DefaultNetworkHandler import DefaultNetworkHandler
    def_handler = DefaultNetworkHandler()

    bbp.parse(def_handler)

    from neuroml.hdf5.NetworkBuilder import NetworkBuilder

    neuroml_handler = NetworkBuilder()

    bbp = BBPConnectomeReader(
        filename=filename,
        percentage_cells_per_pop=percentage_cells_per_pop,
        DEFAULT_CELL_ID='hhcell')
    bbp.parse(neuroml_handler)

    nml_file_name = 'BBP.net.nml'

    from neuroml.writers import NeuroMLWriter
    NeuroMLWriter.write(neuroml_handler.get_nml_doc(), nml_file_name)
    '''
    from neuromllite.NeuronHandler import NeuronHandler


    nrn_handler = NeuronHandler()
    
    nrn_handler.executeHoc('load_file("hhcell.hoc")')
    bbp = BBPConnectomeReader()
    bbp.parse(file_name, nrn_handler)  
    
    #bbp.save_to_hdf5(nml_h5_file_name)'''