pyneuroml.swc package#
pyneuroml.swc.LoadSWC module#
Module for loading SWC files
Added in version 1.3.9.
Copyright 2024 NeuroML contributors
- class pyneuroml.swc.LoadSWC.SWCGraph#
Bases:
object
Graph data structure holding SWCNode objects
- HEADER_FIELDS = ['ORIGINAL_SOURCE', 'CREATURE', 'REGION', 'FIELD/LAYER', 'TYPE', 'CONTRIBUTOR', 'REFERENCE', 'RAW', 'EXTRAS', 'SOMA_AREA', 'SHRINKAGE_CORRECTION', 'VERSION_NUMBER', 'VERSION_DATE', 'SCALE']#
- add_metadata(key: str, value: str)#
Add metadata to the SWC graph.
- Parameters:
key (str) – The key for the metadata
value (str) – The value for the metadata
- add_node(node: SWCNode)#
Add a node to the SWC graph.
- Parameters:
node (SWCNode) – The node to be added
- Raises:
ValueError – If a node with the same ID already exists in the graph or if multiple root nodes are detected
- export_to_swc_file(filename: str) None #
Export the SWCGraph to a new SWC file.
- Parameters:
filename (str) – The path to the output SWC file
- get_branch_points(types: List[int] | None) List[SWCNode] | Dict[int, List[SWCNode]] #
Get all branch points (nodes with multiple children) of the given types.
- Parameters:
types (int) – One or more node type IDs to filter the branch points by
- Returns:
if node types are given, a dictionary with keys as the node type and lists of nodes as values; otherwise a list of all nodes
- Return type:
list or dict
- get_children(node_id: int) List[SWCNode] #
Get a list of child nodes for a given node.
- Parameters:
node_id (int) – The ID of the node for which to get the children
- Returns:
A list of SWCNode objects representing the children of the given node
- Return type:
list
- Raises:
ValueError – If the provided node_id is not found in the graph
- get_node(node_id: int) SWCNode #
Get a node from the graph by its ID.
- Parameters:
node_id (int) – The ID of the node to retrieve
- Returns:
The node with the specified ID
- Return type:
- Raises:
ValueError – If the specified node_id is not found in the SWC tree
- get_nodes_by_type(type_id: int) List[SWCNode] #
Get a list of nodes of a specific type.
- Parameters:
type_id (int) – The type ID of the nodes to retrieve
- Returns:
A list of SWCNode objects that have the specified type ID
- Return type:
list
- get_nodes_with_multiple_children(type_id: int | None = None) List[SWCNode] #
Get nodes with multiple children, optionally filtered by type.
- Parameters:
type_id (int or None) – The type ID to filter nodes by (optional)
- Returns:
A list of SWCNode objects that have multiple children and match the specified type (if provided)
- Return type:
list
- get_parent(node_id: int) SWCNode | None #
Get the parent node of a given node in the SWC tree.
- Parameters:
node_id (int) – The ID of the node for which to retrieve the parent
- Returns:
The parent node if the node has a parent, otherwise None
- Return type:
SWCNode or None
- Raises:
ValueError – If the specified node_id is not found in the SWC tree
- class pyneuroml.swc.LoadSWC.SWCNode(node_id: str | int, type_id: str | int, x: str | float, y: str | float, z: str | float, radius: str | float, parent_id: str | int)#
Bases:
object
Represents a single node in an SWC (Standardized Morphology Data Format) file.
The SWC format is a widely used standard for representing neuronal morphology data. It consists of a series of lines, each representing a single node or sample point along the neuronal structure. For more information on the SWC format, see: https://swc-specification.readthedocs.io/en/latest/swc.html
- Parameters:
UNDEFINED (int) – ID representing an undefined node type
SOMA (int) – ID representing a soma node
AXON (int) – ID representing an axon node
BASAL_DENDRITE (int) – ID representing a basal dendrite node
APICAL_DENDRITE (int) – ID representing an apical dendrite node
CUSTOM (int) – ID representing a custom node type
UNSPECIFIED_NEURITE (int) – ID representing an unspecified neurite node
GLIA_PROCESSES (int) – ID representing a glia process node
TYPE_NAMES (dict) – A mapping of node type IDs to their string representations
- APICAL_DENDRITE = 4#
- AXON = 2#
- BASAL_DENDRITE = 3#
- CUSTOM = 5#
- GLIA_PROCESSES = 7#
- SOMA = 1#
- TYPE_NAMES = {0: 'Undefined', 1: 'Soma', 2: 'Axon', 3: 'Basal Dendrite', 4: 'Apical Dendrite', 5: 'Custom', 6: 'Unspecified Neurite', 7: 'Glia Processes'}#
- UNDEFINED = 0#
- UNSPECIFIED_NEURITE = 6#
- pyneuroml.swc.LoadSWC.load_swc(filename: str) SWCGraph #
Load an SWC file and create an SWCGraph object.
- Parameters:
filename (str) – The path to the SWC file to be loaded
- Returns:
An SWCGraph object representing the loaded SWC file
- Return type:
- Raises:
ValueError – If a non header line with more than the required number of fields is found
- pyneuroml.swc.LoadSWC.parse_header(line_number: int, line: str) Tuple[str, str] | None #
Parse a header line from an SWC file.
- Parameters:
line_number (int) – line number, for logging purposes
line (str) – A single line from the SWC file header
- Returns:
A tuple containing the matched header field name and corresponding value (or None if no match)
- Return type:
tuple
pyneuroml.swc.ExportSWC module#
A script to export SWC files from NeuroML <cell>s
- pyneuroml.swc.ExportSWC.convert_to_swc(nml_file_name, add_comments=False, target_dir=None)#
Find all <cell> elements and create one SWC file for each
pyneuroml.swc.ExportNML module#
Module for exporting NeuroML from SWC files.
Added in version 1.3.9.
Copyright 2024 NeuroML contributors
- class pyneuroml.swc.ExportNML.NeuroMLWriter(swc_graph: SWCGraph)#
Bases:
object
A class to convert SWC graph data to NeuroML format.
This class takes an SWC graph and converts it into a NeuroML representation, handling different neuron segment types and creating appropriate segment groups.
- export_to_nml_file(filename: str, standalone_morphology: bool = True, unbranched_segment_groups: bool = True) None #
Export the NeuroML representation to a file.
- Parameters:
filename (str) – The name of the file to export to.
standalone_morphology (bool) – export morphology as standalone object (not as part of a Cell object)
unbranched_segment_groups (bool) – toggle creation of unbranched segment groups
- generate_neuroml(standalone_morphology: bool = True, unbranched_segment_groups: bool = True) NeuroMLDocument #
Generate NeuroML representation.
Main worker function
- Parameters:
standalone_morphology (bool) – export morphology as standalone object (not as part of a Cell object)
unbranched_segment_groups (bool) – toggle creation of unbranched segment groups
- Returns:
the NeuroML document
- Return type:
NeuroMLDocument
- pyneuroml.swc.ExportNML.convert_swc_to_neuroml(swc_file: str, neuroml_file: str | None = None, standalone_morphology: bool = True, unbranched_segment_groups: bool = True) NeuroMLDocument #
Convert an SWC file to NeuroML.
If neuroml_file is provided, will also write to file.
- Parameters:
swc_file (str) – SWC input file
neuroml_file (str) – output NeuroML file (optional)
standalone_morphology (bool) – export morphology as standalone object (not as part of a Cell object)
- Returns:
NeuroML document
- Return type:
NeuroMLDocument