From 99b739742639e0bbcd1f24e8e0d7053b4a150127 Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Wed, 6 Nov 2024 17:55:31 +0000 Subject: [PATCH 1/2] wip: improve `pynml-summary` Adds an intermediate `-v`, but needs more work to properly replicate earlier behaviour. --- pyneuroml/utils/info.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/pyneuroml/utils/info.py b/pyneuroml/utils/info.py index 927f1d7a..e01b2d5d 100644 --- a/pyneuroml/utils/info.py +++ b/pyneuroml/utils/info.py @@ -12,8 +12,10 @@ import sys import textwrap import typing +from io import StringIO from neuroml import Cell, NeuroMLDocument + from pyneuroml.io import read_neuroml2_file from pyneuroml.utils.units import convert_to_units, get_value_in_si @@ -64,7 +66,7 @@ def summary( """ Usage: - pynml-summary [-vh] + pynml-summary [-vVh] Required arguments: NeuroML file: name of file to summarise @@ -72,10 +74,14 @@ def summary( Optional arguments: -v/--verbose: enable verbose mode + -V/--very-verbose: enable *very* verbose mode -h/--help: print this help text and exit """ ) + verbose = False + very_verbose = False + if len(sys.argv) < 2: print("Argument required.") print(usage) @@ -85,20 +91,41 @@ def summary( print(usage) return - if "-v" in sys.argv or "--verbose" in sys.argv: + if "-v" in sys.argv: verbose = True sys.argv.remove("-v") + if "--verbose" in sys.argv: + verbose = True + sys.argv.remove("--verbose") + + if "-V" in sys.argv: + very_verbose = True + sys.argv.remove("-V") + + if "--very-verbose" in sys.argv: + very_verbose = True + sys.argv.remove("-very-verbose") + if nml2_doc is None: nml2_file_name = sys.argv[1] - nml2_doc = read_neuroml2_file(nml2_file_name, include_includes=verbose, fix_external_morphs_biophys=True) + nml2_doc = read_neuroml2_file( + nml2_file_name, include_includes=verbose, fix_external_morphs_biophys=True + ) info = nml2_doc.summary(show_includes=False) - if verbose: + if verbose or very_verbose: cell_info_str = "" - for cell in nml2_doc.cells: - cell_info_str += cell_info(cell) + "*\n" + if very_verbose: + for cell in nml2_doc.cells: + cell_info_str += cell_info(cell) + "*\n" + else: + string_buffer = StringIO() + for cell in nml2_doc.cells: + cell.summary(morph=True, biophys=True, string_buffer=string_buffer) + cell_info_str += string_buffer.getvalue() + lines = info.split("\n") info = "" still_to_add = False From 76958b44d42a7e0021fe7685d01ce28319e3ccdb Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Fri, 8 Nov 2024 17:00:33 +0000 Subject: [PATCH 2/2] feat(summary): generalise to all NML elements --- pyneuroml/utils/info.py | 113 ++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 44 deletions(-) diff --git a/pyneuroml/utils/info.py b/pyneuroml/utils/info.py index e01b2d5d..54b41528 100644 --- a/pyneuroml/utils/info.py +++ b/pyneuroml/utils/info.py @@ -66,7 +66,7 @@ def summary( """ Usage: - pynml-summary [-vVh] + pynml-summary [-vh] Required arguments: NeuroML file: name of file to summarise @@ -74,14 +74,10 @@ def summary( Optional arguments: -v/--verbose: enable verbose mode - -V/--very-verbose: enable *very* verbose mode -h/--help: print this help text and exit """ ) - verbose = False - very_verbose = False - if len(sys.argv) < 2: print("Argument required.") print(usage) @@ -91,58 +87,87 @@ def summary( print(usage) return - if "-v" in sys.argv: + if "-v" in sys.argv or "--verbose" in sys.argv: verbose = True sys.argv.remove("-v") - if "--verbose" in sys.argv: - verbose = True - sys.argv.remove("--verbose") - - if "-V" in sys.argv: - very_verbose = True - sys.argv.remove("-V") - - if "--very-verbose" in sys.argv: - very_verbose = True - sys.argv.remove("-very-verbose") - if nml2_doc is None: nml2_file_name = sys.argv[1] nml2_doc = read_neuroml2_file( nml2_file_name, include_includes=verbose, fix_external_morphs_biophys=True ) - info = nml2_doc.summary(show_includes=False) - - if verbose or very_verbose: - cell_info_str = "" - if very_verbose: + info = "" + # network, show full + if len(nml2_doc.networks) > 0: + info = nml2_doc.summary(show_includes=False) + if verbose: + cell_info_str = "" for cell in nml2_doc.cells: cell_info_str += cell_info(cell) + "*\n" - else: - string_buffer = StringIO() - for cell in nml2_doc.cells: - cell.summary(morph=True, biophys=True, string_buffer=string_buffer) - cell_info_str += string_buffer.getvalue() - - lines = info.split("\n") - info = "" - still_to_add = False - for line in lines: - if "Cell: " in line: - still_to_add = True - pass - elif "Network: " in line: - still_to_add = False - if len(cell_info_str) > 0: - info += "%s" % cell_info_str - info += "%s\n" % line - else: - if still_to_add and "******" in line: + lines = info.split("\n") + info = "" + still_to_add = False + for line in lines: + if "Cell: " in line: + still_to_add = True + pass + elif "Network: " in line: + still_to_add = False if len(cell_info_str) > 0: info += "%s" % cell_info_str - info += "%s\n" % line + info += "%s\n" % line + else: + if still_to_add and "******" in line: + if len(cell_info_str) > 0: + info += "%s" % cell_info_str + info += "%s\n" % line + else: + # not a full network + # cell file + if len(nml2_doc.cells) > 0: + for cell in nml2_doc.cells: + if verbose: + info += cell_info(cell) + "*\n" + else: + cell_info_buffer = StringIO() + cell.summary(string_buffer=cell_info_buffer) + info += cell_info_buffer.getvalue() + # other things, just use info + else: + nml2_doc_info = nml2_doc.info(show_contents=True, return_format="dict") + + doc_id = nml2_doc_info.pop("id", None) + try: + info += f"** Summary: id: {doc_id['members']} **\n\n" + except (KeyError, AttributeError): + info += f"** Summary: id: {nml2_file_name} **\n\n" + + notes = nml2_doc_info.pop("notes", None) + if notes: + try: + info += f"{notes['members']}\n\n" + except KeyError: + pass + + for attr, membs in nml2_doc_info.items(): + if "members" in membs.keys(): + amemb_info = "" + num_members = len(membs["members"]) + ctr = 0 + for amemb in membs["members"]: + ctr += 1 + if isinstance(amemb, str): + amemb_info += amemb + else: + amemb_info += amemb.info( + show_contents="set", return_format="string" + ) + if len(amemb_info) > 0: + info += f"*** {attr} ({ctr}/{num_members}) ***\n\n{amemb_info}\n*** {attr} ({ctr}/{num_members}) ***\n\n" + else: + info += f"*** {attr} ***\n\n{membs}\n*** {attr} ***\n\n" + print(info)