def __nml2_doc(cls,file_name,optimized=False): import sys import logging logging.basicConfig(level=logging.INFO, format="%(name)-19s %(levelname)-5s - %(message)s") from neuroml.hdf5.NeuroMLHdf5Parser import NeuroMLHdf5Parser if optimized: currParser = NeuroMLHdf5Parser(None,optimized=True) currParser.parse(file_name) return currParser.get_nml_doc() else: from neuroml.hdf5.NetworkBuilder import NetworkBuilder nmlHandler = NetworkBuilder() currParser = NeuroMLHdf5Parser(nmlHandler) currParser.parse(file_name) nml2_doc = nmlHandler.get_nml_doc() if currParser.nml_doc_extra_elements: add_all_to_document(currParser.nml_doc_extra_elements,nml2_doc) return nml2_doc
def _read_neuroml2(nml2_file_name_or_string, include_includes=False, verbose=False, already_included=[], print_method=print_, optimized=False, base_path=None): #print("................ Loading: %s"%nml2_file_name_or_string[:7]) base_path_to_use = os.path.dirname(os.path.realpath(nml2_file_name_or_string)) if base_path == None else base_path if supressGeneratedsWarnings: warnings.simplefilter("ignore") if not isinstance(nml2_file_name_or_string, str) or nml2_file_name_or_string.startswith('<'): nml2_doc = nmlparsestring(nml2_file_name_or_string) base_path_to_use = './' if base_path == None else base_path elif nml2_file_name_or_string.endswith('.h5') or nml2_file_name_or_string.endswith('.hdf5'): nml2_doc = NeuroMLHdf5Loader.load(nml2_file_name_or_string,optimized=optimized) else: nml2_doc = NeuroMLLoader.load(nml2_file_name_or_string) if supressGeneratedsWarnings: warnings.resetwarnings() if include_includes: print_method('Including included files (included already: %s)' \ % already_included, verbose) for include in nml2_doc.include: incl_loc = os.path.abspath(os.path.join(base_path_to_use, include.href)) if incl_loc not in already_included: print_method("Loading included NeuroML2 file: %s (base: %s, resolved: %s)" % (include.href, base_path_to_use, incl_loc), verbose) if incl_loc.endswith('.nml') or incl_loc.endswith('.xml'): nml2_sub_doc = read_neuroml2_file(incl_loc, True, verbose=verbose, already_included=already_included) already_included.append(incl_loc) add_all_to_document(nml2_sub_doc,nml2_doc) elif incl_loc.endswith('.nml.h5'): nml2_sub_doc = NeuroMLHdf5Loader.load(incl_loc) already_included.append(incl_loc) add_all_to_document(nml2_sub_doc,nml2_doc) else: raise Exception("Unrecognised extension on file: %s"%incl_loc) nml2_doc.include = [] else: if len(nml2_doc.include)>0: print_method('NOT including included files, even though %s are included!'%len(nml2_doc.include), verbose) nml2_doc.include = [] return nml2_doc
def get_nml_doc(self): if not self.optimized: nml2_doc = nmlHandler.get_nml_doc() if self.nml_doc_extra_elements: add_all_to_document(self.nml_doc_extra_elements, nml2_doc) return nml2_doc else: nml_doc = neuroml.NeuroMLDocument(id=self.doc_id, notes=self.doc_notes) if self.nml_doc_extra_elements: add_all_to_document(self.nml_doc_extra_elements, nml_doc) nml_doc.networks.append(self.optimizedNetwork) return nml_doc