Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/Make 3d visualization compatibile with jupyter notebooks #399

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions pyneuroml/plot/PlotMorphologyVispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ def plot_interactive_3D(
if verbose:
logger.info(f"Visualising {nml_file}")

jupyter = False
if app.Application.is_interactive(app):
jupyter = True

# if it's a file, load it first
if isinstance(nml_file, str):
# load without optimization for older HDF5 API
Expand All @@ -473,22 +477,22 @@ def plot_interactive_3D(

# if it isn't a NeuroMLDocument, create one
if isinstance(nml_model, Cell):
logger.info("Got a cell")
logger.debug("Got a cell")
plottable_nml_model = NeuroMLDocument(id="newdoc")
plottable_nml_model.add(nml_model)
logger.info(f"plottable cell model is: {plottable_nml_model.cells[0]}")
logger.debug(f"plottable cell model is: {plottable_nml_model.cells[0]}")
if title is None:
title = f"{plottable_nml_model.cells[0].id}"

# if it's only a cell, add it to an empty cell in a document
elif isinstance(nml_model, Morphology):
logger.info("Received morph, adding to a dummy cell")
logger.debug("Received morph, adding to a dummy cell")
plottable_nml_model = NeuroMLDocument(id="newdoc")
nml_cell = plottable_nml_model.add(
Cell, id=nml_model.id, morphology=nml_model, validate=False
)
plottable_nml_model.add(nml_cell)
logger.info(f"plottable cell model is: {plottable_nml_model.cells[0]}")
logger.debug(f"plottable cell model is: {plottable_nml_model.cells[0]}")
if title is None:
title = f"{plottable_nml_model.cells[0].id}"
elif isinstance(nml_model, NeuroMLDocument):
Expand Down Expand Up @@ -520,10 +524,10 @@ def plot_interactive_3D(
except AttributeError:
total_segments += len(positions[pop_id])

logger.info(
logger.debug(
f"Visualising {total_segments} segments in {total_cells} cells in {len(pop_id_vs_cell)} populations"
)
logger.info(
logger.debug(
f"Grouping into mesh instances by diameters at {precision[0]} decimal places"
)
# not used later, clear up
Expand Down Expand Up @@ -616,7 +620,7 @@ def plot_interactive_3D(
pass

meshdata = {} # type: typing.Dict[typing.Any, typing.Any]
logger.info("Processing cells")
logger.debug("Processing cells")
pbar = progressbar.ProgressBar(
max_value=total_cells,
widgets=[progressbar.SimpleProgress(), progressbar.Bar(), progressbar.Timer()],
Expand Down Expand Up @@ -730,7 +734,7 @@ def plot_interactive_3D(
if (len(meshdata.keys()) > precision[1]) and (precision[0] > 0):
precision = (precision[0] - 1, precision[1])
pbar.finish(dirty=True)
logger.info(
logger.debug(
f"More meshes than threshold ({len(meshdata.keys())}/{precision[1]}), reducing precision to {precision[0]} and re-calculating."
)
plot_interactive_3D(
Expand All @@ -755,8 +759,13 @@ def plot_interactive_3D(
if not nogui:
pbar.finish()
create_instanced_meshes(meshdata, plot_type, current_view, min_width)
current_canvas.show()
app.run()
if jupyter:
from IPython.display import display

display(current_canvas)
else:
current_canvas.show()
app.run()


def make_cell_upright(
Expand Down Expand Up @@ -810,7 +819,7 @@ def make_cell_upright(
if z_angle < 0:
z_angle += numpy.pi

logger.info("Making cell upright for visualization")
logger.debug("Making cell upright for visualization")
cell = translate_cell_to_coords(cell, inplace=inplace, dest=[0, 0, 0])
cell = rotate_cell(
cell, 0, y_angle, z_angle, "yzx", relative_to_soma=False, inplace=inplace
Expand Down Expand Up @@ -950,6 +959,10 @@ def plot_3D_cell_morphology(
highlight_spec = {}
logging.debug("highlight_spec is " + str(highlight_spec))

jupyter = False
if app.Application.is_interactive(app):
jupyter = True

view_center = None
if upright:
cell = make_cell_upright(cell)
Expand Down Expand Up @@ -1073,8 +1086,13 @@ def plot_3D_cell_morphology(

if not nogui:
create_instanced_meshes(meshdata, plot_type, current_view, min_width)
current_canvas.show()
app.run()
if jupyter:
from IPython.display import display

display(current_canvas)
else:
current_canvas.show()
app.run()
return meshdata


Expand All @@ -1099,7 +1117,7 @@ def create_instanced_meshes(meshdata, plot_type, current_view, min_width):
total_mesh_instances = 0
for d, i in meshdata.items():
total_mesh_instances += len(i)
logger.info(
logger.debug(
f"Visualising {len(meshdata.keys())} meshes with {total_mesh_instances} instances"
)

Expand Down Expand Up @@ -1146,8 +1164,9 @@ def create_instanced_meshes(meshdata, plot_type, current_view, min_width):
instance_positions = []
instance_transforms = []
instance_colors = []
for im in i:
pbar.update(progress_ctr)
for num, im in enumerate(i):
if num % 2000 == 0:
pbar.update(progress_ctr)
progress_ctr += 1
prox = im[0]
dist = im[1]
Expand Down Expand Up @@ -1298,6 +1317,10 @@ def plot_3D_schematic(
if title == "":
title = f"3D schematic of segment groups from {cell.id}"

jupyter = False
if app.Application.is_interactive(app):
jupyter = True

view_center = None
if upright:
cell = make_cell_upright(cell)
Expand Down Expand Up @@ -1394,7 +1417,12 @@ def plot_3D_schematic(

if not nogui:
create_instanced_meshes(meshdata, "Detailed", current_view, width)
app.run()
if jupyter:
from IPython.display import display

display(current_canvas)
else:
app.run()


def create_cylindrical_mesh(
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ vispy =
pyNeuroML[vispy-common]
pyqt6

jupyter =
jupyter-rfb

plotly =
plotly

Expand Down
Loading