From cc7d74761cf1e01b903894b6a3947bb57636575d Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Tue, 7 Nov 2023 16:59:14 +0000 Subject: [PATCH 1/3] feat(connectivity-plots): move matrix plot to new sub-module --- pyneuroml/plot/Connectivity.py | 35 ++++++++++++++++++++++++++++++++++ pyneuroml/pynml.py | 17 ++--------------- 2 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 pyneuroml/plot/Connectivity.py diff --git a/pyneuroml/plot/Connectivity.py b/pyneuroml/plot/Connectivity.py new file mode 100644 index 00000000..e2878c88 --- /dev/null +++ b/pyneuroml/plot/Connectivity.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +""" +Connectivity plotting methods + +File: pyneuroml/plot/Connectivity.py + +Copyright 2023 NeuroML contributors +""" + +import logging + +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...") diff --git a/pyneuroml/pynml.py b/pyneuroml/pynml.py index 703e5402..82603364 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: From f8c4fe4ec87bdc289f06e55d0808a5a0c72b74a6 Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Tue, 7 Nov 2023 17:03:52 +0000 Subject: [PATCH 2/3] wip: chord-diagram implementation using mne https://mne.tools/mne-connectivity/stable/auto_examples/mne_inverse_label_connectivity.html#make-a-connectivity-plot --- pyneuroml/plot/Connectivity.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pyneuroml/plot/Connectivity.py b/pyneuroml/plot/Connectivity.py index e2878c88..a70cad9a 100644 --- a/pyneuroml/plot/Connectivity.py +++ b/pyneuroml/plot/Connectivity.py @@ -33,3 +33,12 @@ def plot_connectivity_matrix(filename, level): 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 + """ + pass From 088fa8a6028e7ebbdf71dba92e6da4c35da50e4c Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Tue, 7 Nov 2023 18:04:47 +0000 Subject: [PATCH 3/3] wip: chord diagram --- pyneuroml/plot/Connectivity.py | 15 ++++++++++++++- tests/plot/test_plot.py | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pyneuroml/plot/Connectivity.py b/pyneuroml/plot/Connectivity.py index a70cad9a..4be44abc 100644 --- a/pyneuroml/plot/Connectivity.py +++ b/pyneuroml/plot/Connectivity.py @@ -8,6 +8,7 @@ """ import logging +import numpy as np logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -41,4 +42,16 @@ def plot_chord_diagram(filename): :param filename: name of NeuroML file :type filename: str """ - pass + 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/tests/plot/test_plot.py b/tests/plot/test_plot.py index b14a8181..02fda544 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()