docs: 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