diff --git a/pyneuroml/plot/Connectivity.py b/pyneuroml/plot/Connectivity.py new file mode 100644 index 000000000..4be44abc7 --- /dev/null +++ b/pyneuroml/plot/Connectivity.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +""" +Connectivity plotting methods + +File: pyneuroml/plot/Connectivity.py + +Copyright 2023 NeuroML contributors +""" + +import logging +import numpy as np + +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) + + +def plot_connectivity_matrix(filename, level): + """Plot a connectivity matrix + + :param filename: name of NeuroML file + :type filename: str + :param level: level at which to plot + :type level: int + """ + from neuromllite.MatrixHandler import MatrixHandler + from neuroml.hdf5.NeuroMLXMLParser import NeuroMLXMLParser + + logger.info("Converting %s to matrix form, level %i" % (filename, level)) + + handler = MatrixHandler(level=level, nl_network=None) + + currParser = NeuroMLXMLParser(handler) + currParser.parse(filename) + handler.finalise_document() + + logger.info("Done with MatrixHandler...") + + +def plot_chord_diagram(filename): + """Plot a chord connectivity diagram + + :param filename: name of NeuroML file + :type filename: str + """ + from pyneuroml.pynml import read_neuroml2_file + from mne_connectivity.viz import plot_connectivity_circle + + doc = read_neuroml2_file(filename) + + nodes = [p.id for p in doc.networks[0].populations] + print(f"Nodes for chord diagram are: {nodes}") + + con = [] + # TODO: logic to get connectivity information + print(f"Connecitivity is {con}") + + fix, axes = plot_connectivity_circle(con, nodes) diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index 703e54022..82603364f 100644 --- a/pyneuroml/pynml.py +++ b/pyneuroml/pynml.py @@ -2312,23 +2312,10 @@ def evaluate_arguments(args): exit_on_fail = True elif args.matrix: confirm_neuroml_file(f) - from neuromllite.MatrixHandler import MatrixHandler - level = int(args.matrix[0]) + from pyneuroml.plot.Connectivity import plot_connectivity_matrix - logger.info("Converting %s to matrix form, level %i" % (f, level)) - - from neuroml.hdf5.NeuroMLXMLParser import NeuroMLXMLParser - - handler = MatrixHandler(level=level, nl_network=None) - - currParser = NeuroMLXMLParser(handler) - - currParser.parse(f) - - handler.finalise_document() - - logger.info("Done with MatrixHandler...") + plot_connectivity_matrix(f, level) exit(0) elif args.validate: diff --git a/tests/plot/test_plot.py b/tests/plot/test_plot.py index b14a8181f..02fda5445 100644 --- a/tests/plot/test_plot.py +++ b/tests/plot/test_plot.py @@ -12,6 +12,7 @@ import pathlib as pl from pyneuroml.plot import generate_plot, generate_interactive_plot +from pyneuroml.plot.Connectivity import plot_chord_diagram from .. import BaseTestCase logger = logging.getLogger(__name__) @@ -86,6 +87,17 @@ def test_generate_interactive_plot(self): self.assertIsFile(filename) pl.Path(filename).unlink() + def test_chord_diagram(self): + """Test the chord diagram plotter""" + filename = "tests/plot/test_chord_diagram.png" + + # remove the file first + try: + pl.Path(filename).unlink() + except FileNotFoundError: + pass + plot_chord_diagram("./Izh2007Cells.net.nml") + if __name__ == "__main__": unittest.main()