pyneuroml.io module#

Methods related to IO.

File: pyneuroml/io.py

Copyright 2024 NeuroML contributors

pyneuroml.io.confirm_file_exists(filename: str) None#

Check if a file exists, exit if it does not.

Parameters:

filename (str) – the filename to check

pyneuroml.io.confirm_file_type(filename: str, file_exts: List[str], root_tag: str | None = None, error_str: str | None = None, sys_error: bool = False) None#

Confirm that a file exists and is of the provided type.

First we rely on file extensions to test for type, since this is the simplest way. If this test fails, we read the full file and test the root tag if one has been provided.

Parameters:
  • filename (str) – filename to confirm

  • file_exts (list of strings) – list of valid file extensions, without the leading dot

  • root_tag (str) – root tag for file, used if extensions do not match

  • error_str (string (optional)) – an optional error string to print along with the thrown exception

Raises:

NMLFileTypeError – if file does not have one of the provided extensions

pyneuroml.io.confirm_lems_file(filename: str, sys_error: bool = False) None#

Confirm that file exists and is a LEMS file before proceeding with processing.

Parameters:
  • filename (list of strings) – Names of files to check

  • sys_error (bool) – toggle whether function should exit or raise exception

pyneuroml.io.confirm_neuroml_file(filename: str, sys_error: bool = False) None#

Confirm that file exists and is a NeuroML file before proceeding with processing.

Parameters:
  • filename (str) – Names of files to check

  • sys_error (bool) – toggle whether function should exit or raise exception

pyneuroml.io.read_lems_file(lems_file_name: str, include_includes: bool = False, fail_on_missing_includes: bool = False, debug: bool = False) Model#

Read LEMS file using PyLEMS. See WARNING below.

WARNING: this is a general function that uses PyLEMS to read any files that are valid LEMS even if they are not valid NeuroML. Therefore, this function is not aware of the standard NeuroML LEMS definitions.

To validate NeuroML LEMS files which need to be aware of the NeuroML standard LEMS definitions, please use the validate_neuroml2_lems_file function instead.

pyneuroml.io.read_neuroml2_file(nml2_file_name: str, include_includes: bool = False, verbose: bool = False, already_included: list | None = None, optimized: bool = False, check_validity_pre_include: bool = False, fix_external_morphs_biophys: bool = False) NeuroMLDocument#

Read a NeuroML2 file into a nml.NeuroMLDocument

Parameters:
  • nml2_file_name (str) – file of NeuroML 2 file to read

  • include_includes (bool) – toggle whether files included in NML file should also be included/read

  • verbose (bool) – toggle verbosity

  • already_included (list) – list of files already included

  • optimized (bool) – toggle whether the HDF5 loader should optimise the document

  • check_validity_pre_include (bool) – check each file for validity before including

  • fix_external_morphs_biophys (bool) – if a cell element has a morphology (or biophysicalProperties) attribute, as opposed to a subelement morphology/biophysicalProperties, substitute the external element into the cell element for ease of access

Returns:

nml.NeuroMLDocument object containing the read NeuroML file(s)

pyneuroml.io.write_lems_file(lems_model: Model, lems_file_name: str, validate: bool = False) None#

Write a lems_model.Model to file using pyLEMS.

Parameters:
  • lems_model (lems_model.Model) – LEMS model to write to file

  • lems_file_name (str) – name of file to write to

  • validate (bool) – toggle whether written file should be validated

pyneuroml.io.write_neuroml2_file(nml2_doc: NeuroMLDocument, nml2_file_name: str, validate: bool = True, verbose_validate: bool = False, hdf5: bool = False) bool | Tuple[bool, str] | None#

Write a NeuroMLDocument object to a file using libNeuroML.

Parameters:
  • nml2_doc (NeuroMLDocument) – NeuroMLDocument object to write to file

  • nml2_file_name (str) – name of file to write to

  • validate (bool) – toggle whether the written file should be validated

  • verbose_validate (bool) – toggle whether the validation should be verbose

  • hdf5 (bool) – write to HDF5 file