Skip to content

Commit

Permalink
feat(morph-plot): also allow plotting of swc files
Browse files Browse the repository at this point in the history
Note that we first export the SWC files to NeuroML and then visualise
them, we don't visualise them "natively". That will require the
implementation of new methods to create meshes from SWC points,
effectively re-doing the SWC to NeuroML conversion logic.
  • Loading branch information
sanjayankur31 committed Sep 6, 2024
1 parent 0a378df commit 3d9a27f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
38 changes: 31 additions & 7 deletions pyneuroml/plot/PlotMorphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import os
import random
import sys
import textwrap
import typing
from typing import Optional

Expand All @@ -23,6 +24,7 @@
from neuroml.utils import fix_external_morphs_biophys_in_cell

from pyneuroml.pynml import read_neuroml2_file
from pyneuroml.swc.ExportNML import convert_swc_to_neuroml
from pyneuroml.utils import extract_position_info
from pyneuroml.utils.cli import build_namespace
from pyneuroml.utils.plot import (
Expand All @@ -46,14 +48,25 @@ def process_args():
Parse command-line arguments.
"""
parser = argparse.ArgumentParser(
description=("A script which can generate plots of morphologies in NeuroML 2")
description=(
textwrap.dedent(
"""
A script which can generate plots of morphologies in NeuroML 2.
It can generate plots of single cells (either detailed or point
cells), and networks that include these cells.
It can also visualise single cell morphologies from SWC files,
although it will first convert these to NeuroML.
"""
)
)
)

parser.add_argument(
"nmlFile",
type=str,
metavar="<NeuroML 2 file>",
help="Name of the NeuroML 2 file",
metavar="<NeuroML 2 file/SWC cell file>",
help="Name of the NeuroML 2 file/SWC cell file",
)

parser.add_argument(
Expand Down Expand Up @@ -215,17 +228,20 @@ def plot_2D(
If a file with a network containing multiple cells is provided, it will
plot all the cells. For detailed neuroml.Cell types, it will plot their
complete morphology. For point neurons, we only plot the points (locations)
where they are.
where they are. For single cell SWC files, it will first convert them to
NeuroML and then plot them.
This method uses matplotlib.
.. versionadded:: 1.1.12
The hightlight_spec parameter
:param nml_file: path to NeuroML cell file, or a NeuroMLDocument object
:type nml_file: str or :py:class:`neuroml.NeuroMLDocument` or
:py:class:`neuroml.Cell`
:param nml_file: path to NeuroML cell file or single cell SWC file or
:py:class:`neuroml.NeuroMLDocument` or :py:class:`neuroml.Cell`
or :py:class:`neuroml.Morphology` object
:type nml_file: str or neuroml.NeuroMLDocument or neuroml.Cell or
neuroml.Morphology
:param plane2d: what plane to plot (xy/yx/yz/zy/zx/xz)
:type plane2d: str
:param min_width: minimum width for segments (useful for visualising very
Expand Down Expand Up @@ -317,6 +333,14 @@ def plot_2D(
# TODO: check if this is required: must for MultiscaleISN
if nml_file.endswith(".h5"):
nml_model = read_neuroml2_file(nml_file)
elif nml_file.endswith(".swc"):
nml_model_doc = convert_swc_to_neuroml(
nml_file,
neuroml_file=None,
standalone_morphology=False,
unbranched_segment_groups=False,
)
nml_model = nml_model_doc.cells[0]
else:
nml_model = read_neuroml2_file(
nml_file,
Expand Down
24 changes: 18 additions & 6 deletions pyneuroml/plot/PlotMorphologyVispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from scipy.spatial.transform import Rotation

from pyneuroml.pynml import read_neuroml2_file
from pyneuroml.swc.ExportNML import convert_swc_to_neuroml
from pyneuroml.utils import extract_position_info, make_cell_upright
from pyneuroml.utils.plot import (
DEFAULTS,
Expand Down Expand Up @@ -361,8 +362,13 @@ def plot_interactive_3D(
.. versionadded:: 1.1.12
The highlight_spec parameter
If a file with a network containing multiple cells is provided, it will
plot all the cells. For detailed neuroml.Cell types, it will plot their
complete morphology. For point neurons, we only plot the points (locations)
where they are as spheres. For single cell SWC files, it will first convert
them to NeuroML and then plot them.
:param nml_file: path to NeuroML cell file or
:param nml_file: path to NeuroML cell file or single cell SWC file or
:py:class:`neuroml.NeuroMLDocument` or :py:class:`neuroml.Cell`
or :py:class:`neuroml.Morphology` object
:type nml_file: str or neuroml.NeuroMLDocument or neuroml.Cell or
Expand Down Expand Up @@ -502,6 +508,14 @@ def plot_interactive_3D(
# TODO: check if this is required: must for MultiscaleISN
if nml_file.endswith(".h5"):
nml_model = read_neuroml2_file(nml_file)
elif nml_file.endswith(".swc"):
nml_model_doc = convert_swc_to_neuroml(
nml_file,
neuroml_file=None,
standalone_morphology=False,
unbranched_segment_groups=False,
)
nml_model = nml_model_doc.cells[0]
else:
# do not fix external morphs here, we do it later below
nml_model = read_neuroml2_file(
Expand Down Expand Up @@ -742,6 +756,7 @@ def plot_interactive_3D(
except KeyError:
# if single cell only, use default groups
if total_cells == 1:
logger.debug("Only one detailed cell, using default color groups")
color = "default groups"
# if multiple cells, use different colors for each cell
else:
Expand Down Expand Up @@ -917,15 +932,12 @@ def plot_3D_cell_morphology(
.. seealso::
:py:func:`plot_2D`
:py:func:`plot_interactive_3D`
general function for plotting
:py:func:`plot_2D_schematic`
:py:func:`plot_3D_schematic`
for plotting only segmeng groups with their labels
:py:func:`plot_2D_point_cells`
for plotting point cells
:param offset: offset for cell
Note that this is only used in creating the meshdata. If set to None,
this is ommitted from the meshdata, and the mesh creator will set it to
Expand Down

0 comments on commit 3d9a27f

Please sign in to comment.