Skip to content

Commit 3d9a27f

Browse files
committed
feat(morph-plot): also allow plotting of swc files
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.
1 parent 0a378df commit 3d9a27f

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

pyneuroml/plot/PlotMorphology.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313
import random
1414
import sys
15+
import textwrap
1516
import typing
1617
from typing import Optional
1718

@@ -23,6 +24,7 @@
2324
from neuroml.utils import fix_external_morphs_biophys_in_cell
2425

2526
from pyneuroml.pynml import read_neuroml2_file
27+
from pyneuroml.swc.ExportNML import convert_swc_to_neuroml
2628
from pyneuroml.utils import extract_position_info
2729
from pyneuroml.utils.cli import build_namespace
2830
from pyneuroml.utils.plot import (
@@ -46,14 +48,25 @@ def process_args():
4648
Parse command-line arguments.
4749
"""
4850
parser = argparse.ArgumentParser(
49-
description=("A script which can generate plots of morphologies in NeuroML 2")
51+
description=(
52+
textwrap.dedent(
53+
"""
54+
A script which can generate plots of morphologies in NeuroML 2.
55+
It can generate plots of single cells (either detailed or point
56+
cells), and networks that include these cells.
57+
58+
It can also visualise single cell morphologies from SWC files,
59+
although it will first convert these to NeuroML.
60+
"""
61+
)
62+
)
5063
)
5164

5265
parser.add_argument(
5366
"nmlFile",
5467
type=str,
55-
metavar="<NeuroML 2 file>",
56-
help="Name of the NeuroML 2 file",
68+
metavar="<NeuroML 2 file/SWC cell file>",
69+
help="Name of the NeuroML 2 file/SWC cell file",
5770
)
5871

5972
parser.add_argument(
@@ -215,17 +228,20 @@ def plot_2D(
215228
If a file with a network containing multiple cells is provided, it will
216229
plot all the cells. For detailed neuroml.Cell types, it will plot their
217230
complete morphology. For point neurons, we only plot the points (locations)
218-
where they are.
231+
where they are. For single cell SWC files, it will first convert them to
232+
NeuroML and then plot them.
219233
220234
This method uses matplotlib.
221235
222236
.. versionadded:: 1.1.12
223237
The hightlight_spec parameter
224238
225239
226-
:param nml_file: path to NeuroML cell file, or a NeuroMLDocument object
227-
:type nml_file: str or :py:class:`neuroml.NeuroMLDocument` or
228-
:py:class:`neuroml.Cell`
240+
:param nml_file: path to NeuroML cell file or single cell SWC file or
241+
:py:class:`neuroml.NeuroMLDocument` or :py:class:`neuroml.Cell`
242+
or :py:class:`neuroml.Morphology` object
243+
:type nml_file: str or neuroml.NeuroMLDocument or neuroml.Cell or
244+
neuroml.Morphology
229245
:param plane2d: what plane to plot (xy/yx/yz/zy/zx/xz)
230246
:type plane2d: str
231247
:param min_width: minimum width for segments (useful for visualising very
@@ -317,6 +333,14 @@ def plot_2D(
317333
# TODO: check if this is required: must for MultiscaleISN
318334
if nml_file.endswith(".h5"):
319335
nml_model = read_neuroml2_file(nml_file)
336+
elif nml_file.endswith(".swc"):
337+
nml_model_doc = convert_swc_to_neuroml(
338+
nml_file,
339+
neuroml_file=None,
340+
standalone_morphology=False,
341+
unbranched_segment_groups=False,
342+
)
343+
nml_model = nml_model_doc.cells[0]
320344
else:
321345
nml_model = read_neuroml2_file(
322346
nml_file,

pyneuroml/plot/PlotMorphologyVispy.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from scipy.spatial.transform import Rotation
3535

3636
from pyneuroml.pynml import read_neuroml2_file
37+
from pyneuroml.swc.ExportNML import convert_swc_to_neuroml
3738
from pyneuroml.utils import extract_position_info, make_cell_upright
3839
from pyneuroml.utils.plot import (
3940
DEFAULTS,
@@ -361,8 +362,13 @@ def plot_interactive_3D(
361362
.. versionadded:: 1.1.12
362363
The highlight_spec parameter
363364
365+
If a file with a network containing multiple cells is provided, it will
366+
plot all the cells. For detailed neuroml.Cell types, it will plot their
367+
complete morphology. For point neurons, we only plot the points (locations)
368+
where they are as spheres. For single cell SWC files, it will first convert
369+
them to NeuroML and then plot them.
364370
365-
:param nml_file: path to NeuroML cell file or
371+
:param nml_file: path to NeuroML cell file or single cell SWC file or
366372
:py:class:`neuroml.NeuroMLDocument` or :py:class:`neuroml.Cell`
367373
or :py:class:`neuroml.Morphology` object
368374
:type nml_file: str or neuroml.NeuroMLDocument or neuroml.Cell or
@@ -502,6 +508,14 @@ def plot_interactive_3D(
502508
# TODO: check if this is required: must for MultiscaleISN
503509
if nml_file.endswith(".h5"):
504510
nml_model = read_neuroml2_file(nml_file)
511+
elif nml_file.endswith(".swc"):
512+
nml_model_doc = convert_swc_to_neuroml(
513+
nml_file,
514+
neuroml_file=None,
515+
standalone_morphology=False,
516+
unbranched_segment_groups=False,
517+
)
518+
nml_model = nml_model_doc.cells[0]
505519
else:
506520
# do not fix external morphs here, we do it later below
507521
nml_model = read_neuroml2_file(
@@ -742,6 +756,7 @@ def plot_interactive_3D(
742756
except KeyError:
743757
# if single cell only, use default groups
744758
if total_cells == 1:
759+
logger.debug("Only one detailed cell, using default color groups")
745760
color = "default groups"
746761
# if multiple cells, use different colors for each cell
747762
else:
@@ -917,15 +932,12 @@ def plot_3D_cell_morphology(
917932
918933
.. seealso::
919934
920-
:py:func:`plot_2D`
935+
:py:func:`plot_interactive_3D`
921936
general function for plotting
922937
923-
:py:func:`plot_2D_schematic`
938+
:py:func:`plot_3D_schematic`
924939
for plotting only segmeng groups with their labels
925940
926-
:py:func:`plot_2D_point_cells`
927-
for plotting point cells
928-
929941
:param offset: offset for cell
930942
Note that this is only used in creating the meshdata. If set to None,
931943
this is ommitted from the meshdata, and the mesh creator will set it to

0 commit comments

Comments
 (0)