Skip to content

Commit

Permalink
Merge pull request #258 from NeuroML/feat/hybrid-plots
Browse files Browse the repository at this point in the history
Reduce memory usage when generating morph plots and a few minor fixes
  • Loading branch information
sanjayankur31 authored Sep 21, 2023
2 parents 9bdb814 + 286940c commit 5d3b17a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
28 changes: 19 additions & 9 deletions pyneuroml/plot/PlotMorphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,24 @@ def plot_2D(
verbose=False,
optimized=True,
)

if title is None:
try:
title = f"{nml_model.networks[0].id} from {nml_file}"
except IndexError:
title = f"{nml_model.cells[0].id} from {nml_file}"
elif isinstance(nml_file, Cell):
nml_model = NeuroMLDocument(id="newdoc")
nml_model.add(nml_file)
if title is None:
title = f"{nml_model.cells[0].id}"

elif isinstance(nml_file, NeuroMLDocument):
nml_model = nml_file
if title is None:
try:
title = f"{nml_model.networks[0].id} from {nml_file.id}"
except IndexError:
title = f"{nml_model.cells[0].id} from {nml_file.id}"
else:
raise TypeError(
"Passed model is not a NeuroML file path, nor a neuroml.Cell, nor a neuroml.NeuroMLDocument"
Expand All @@ -283,19 +294,16 @@ def plot_2D(
nml_model, verbose, nml_file if type(nml_file) is str else ""
)

if title is None:
if len(nml_model.networks) > 0:
title = "2D plot of %s from %s" % (nml_model.networks[0].id, nml_file)
else:
title = "2D plot of %s" % (nml_model.cells[0].id)

if verbose:
logger.debug(f"positions: {positions}")
logger.debug(f"pop_id_vs_cell: {pop_id_vs_cell}")
logger.debug(f"cell_id_vs_cell: {cell_id_vs_cell}")
logger.debug(f"pop_id_vs_color: {pop_id_vs_color}")
logger.debug(f"pop_id_vs_radii: {pop_id_vs_radii}")

# not used, clear up
del cell_id_vs_cell

fig, ax = get_new_matplotlib_morph_plot(title, plane2d)
axis_min_max = [float("inf"), -1 * float("inf")]

Expand All @@ -322,7 +330,8 @@ def plot_2D(
except KeyError:
pass

for pop_id, cell in pop_id_vs_cell.items():
while pop_id_vs_cell:
pop_id, cell = pop_id_vs_cell.popitem()
pos_pop = positions[pop_id] # type: typing.Dict[typing.Any, typing.List[float]]

# reinit point_cells for each loop
Expand All @@ -337,7 +346,8 @@ def plot_2D(
except KeyError:
pass

for cell_index, pos in pos_pop.items():
while pos_pop:
cell_index, pos = pos_pop.popitem()
radius = pop_id_vs_radii[pop_id] if pop_id in pop_id_vs_radii else 10
color = pop_id_vs_color[pop_id] if pop_id in pop_id_vs_color else None

Expand Down
35 changes: 25 additions & 10 deletions pyneuroml/plot/PlotMorphologyVispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,25 @@ def plot_interactive_3D(
verbose=False,
optimized=True,
)
if title is None:
try:
title = f"{nml_model.networks[0].id} from {nml_file}"
except IndexError:
title = f"{nml_model.cells[0].id} from {nml_file}"

elif isinstance(nml_file, Cell):
nml_model = NeuroMLDocument(id="newdoc")
nml_model.add(nml_file)
if title is None:
title = f"{nml_model.cells[0].id}"

elif isinstance(nml_file, NeuroMLDocument):
nml_model = nml_file
if title is None:
try:
title = f"{nml_model.networks[0].id} from {nml_file.id}"
except IndexError:
title = f"{nml_model.cells[0].id} from {nml_file.id}"
else:
raise TypeError(
"Passed model is not a NeuroML file path, nor a neuroml.Cell, nor a neuroml.NeuroMLDocument"
Expand All @@ -386,19 +400,17 @@ def plot_interactive_3D(
marker_points = []
marker_colors = []

if title is None:
try:
title = f"{nml_model.networks[0].id} from {nml_file}"
except IndexError:
title = f"{nml_model.cells[0].id} from {nml_file}"

logger.debug(f"positions: {positions}")
logger.debug(f"pop_id_vs_cell: {pop_id_vs_cell}")
logger.debug(f"cell_id_vs_cell: {cell_id_vs_cell}")
logger.debug(f"pop_id_vs_color: {pop_id_vs_color}")
logger.debug(f"pop_id_vs_radii: {pop_id_vs_radii}")

if len(positions.keys()) > 1:
# not used, clear up
print(f"Plotting {len(cell_id_vs_cell)} cells")
del cell_id_vs_cell

if len(positions) > 1:
only_pos = []
for posdict in positions.values():
for poss in posdict.values():
Expand Down Expand Up @@ -467,8 +479,9 @@ def plot_interactive_3D(
except KeyError:
pass

for pop_id, cell in pop_id_vs_cell.items():
pos_pop = positions[pop_id] # type: typing.Dict[typing.Any, typing.List[float]]
while pop_id_vs_cell:
pop_id, cell = pop_id_vs_cell.popitem()
pos_pop = positions[pop_id]

# reinit point_cells for each loop
point_cells_pop = []
Expand All @@ -482,7 +495,8 @@ def plot_interactive_3D(
except KeyError:
pass

for cell_index, pos in pos_pop.items():
while pos_pop:
cell_index, pos = pos_pop.popitem()
radius = pop_id_vs_radii[pop_id] if pop_id in pop_id_vs_radii else 10
color = pop_id_vs_color[pop_id] if pop_id in pop_id_vs_color else None

Expand Down Expand Up @@ -530,6 +544,7 @@ def plot_interactive_3D(
or plot_type == "constant"
or cell.id in constant_cells
):
logger.debug(f"Cell for 3d is: {cell.id}")
pts, sizes, colors = plot_3D_cell_morphology(
offset=pos,
cell=cell,
Expand Down
20 changes: 11 additions & 9 deletions pyneuroml/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ def extract_position_info(
# document
for inc in nml_model_copy.includes:
incl_loc = os.path.abspath(os.path.join(base_path, inc.href))
inc = read_neuroml2_file(incl_loc)
for acell in inc.cells:
if acell.id in required_cell_types:
acell.biophysical_properties = None
nml_model_copy.add(acell)
if os.path.isfile(incl_loc):
inc = read_neuroml2_file(incl_loc)
for acell in inc.cells:
if acell.id in required_cell_types:
acell.biophysical_properties = None
nml_model_copy.add(acell)

cell_elements.extend(nml_model_copy.cells)
cell_elements.extend(nml_model_copy.cell2_ca_poolses)
Expand All @@ -111,10 +112,11 @@ def extract_position_info(
# add any included cells to the main document
for inc in nml_model_copy.includes:
incl_loc = os.path.abspath(os.path.join(base_path, inc.href))
inc = read_neuroml2_file(incl_loc)
for acell in inc.cells:
acell.biophysical_properties = None
nml_model_copy.add(acell)
if os.path.isfile(incl_loc):
inc = read_neuroml2_file(incl_loc)
for acell in inc.cells:
acell.biophysical_properties = None
nml_model_copy.add(acell)

cell_elements.extend(nml_model_copy.cells)
cell_elements.extend(nml_model_copy.cell2_ca_poolses)
Expand Down

0 comments on commit 5d3b17a

Please sign in to comment.