brainglobe / brainrender

a python based software for visualization of neuroanatomical and morphological data.
https://brainglobe.info/documentation/brainrender/index.html
BSD 3-Clause "New" or "Revised" License
538 stars 75 forks source link

Add to scene scene.add(cells) is causing jupyter to crash [BUG] #355

Closed AngCamp closed 3 months ago

AngCamp commented 3 months ago

Describe the bug When running the tutorial code this function: # Add to scene scene.add(cells) leads to a crash

Note scene.render() also leads to the same issue.

To Reproduce Run the tutorial code from the readme in a jupyter notebook.

Expected behaviour I expect the code from the tutorial to execute labelling some points in visual coretx in the allen map.

Log file Jupyter failure:

17:07:06.987 [info] Handle Execution of Cells 13 for ~/NeuropixelsLFPOnRamp/TechnicalValidation/ABI_validation.ipynb
17:07:08.866 [info] Cell 13 completed in 1.847s (start: 1715904427019, end: 1715904428866)
17:07:17.697 [info] Handle Execution of Cells 14 for ~/NeuropixelsLFPOnRamp/TechnicalValidation/ABI_validation.ipynb
17:07:18.867 [info] Handle Execution of Cells 15 for ~/NeuropixelsLFPOnRamp/TechnicalValidation/ABI_validation.ipynb
17:07:19.294 [info] Cell 14 completed in 1.576s (start: 1715904437718, end: 1715904439294)
17:07:19.347 [info] Cell 15 completed in 0.042s (start: 1715904439305, end: 1715904439347)
17:07:20.006 [info] Handle Execution of Cells 16 for ~/NeuropixelsLFPOnRamp/TechnicalValidation/ABI_validation.ipynb
17:07:20.165 [info] Cell 16 completed in 0.142s (start: 1715904440022, end: 1715904440164)
17:07:24.266 [info] Handle Execution of Cells 17 for ~/NeuropixelsLFPOnRamp/TechnicalValidation/ABI_validation.ipynb
17:07:24.367 [info] Cell 17 completed in 0.083s (start: 1715904444283, end: 1715904444366)
17:07:28.181 [info] Handle Execution of Cells 18 for ~/NeuropixelsLFPOnRamp/TechnicalValidation/ABI_validation.ipynb
17:07:30.952 [error] Error in waiting for cell to complete Error: Canceled future for execute_request message before replies were done
    at Qr.dispose (/home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:22379)
    at /home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:41058
    at Map.forEach (<anonymous>)
    at n._clearKernelState (/home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:41044)
    at /home/~/.vscode-server/extensions/ms-toolsai.jupyter-2024.4.0-linux-x64/dist/node_modules/@jupyterlab/services.js:11:43455
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
17:07:30.953 [warn] Cell completed with errors {
  message: 'Canceled future for execute_request message before replies were done'
}
17:07:30.953 [info] Cell 18 completed in 2.741s (start: 1715904448212, end: 1715904450953)
17:07:30.954 [error] Error in execution {
  message: 'Canceled future for execute_request message before replies were done'
}
17:07:30.954 [error] Error in execution (get message for cell) {
  message: 'Canceled future for execute_request message before replies were done'
}
17:07:31.062 [info] Cell 18 completed in 0s (start: undefined, end: undefined)

Screenshots If applicable, add screenshots to help explain your problem.

Computer used (please complete the following information):

OS: Linux OS version: 3.10.0-1160.95.1.el7.x86_64 Processor architecture: x86_64 Processor name: x86_64

Additional context Add any other context about the problem here.

adamltyson commented 3 months ago

Hi @AngCamp, using brainrender in a notebook is slightly different to using it in a Python script. Could I check that you've set the backend etc as per the docs? I just tested this in a notebook on macOS and it worked ok.

AngCamp commented 3 months ago

I am now using the script you provided for notebooks. I've provided it below. It was working last night but this morning for some reason the cell is also crashing. I'm running this on a remote server through VS code. Was wondering if you have any advice on interacting with brain render from a remote ssh tunnel? It doesn't seem set up for that, but I would love to be corrected. All my data is on a remote server my lab uses.

import random

import numpy as np

from brainrender import Scene
from brainrender.actors import Points
from vedo import Plotter  # <- this will be used to render an embedded scene 

# Display the Allen Brain mouse atlas.
scene = Scene(atlas_name="allen_mouse_25um", title="Cells in primary visual cortex")
# Make sure it gets embedded in the window
scene.jupyter = True

def get_n_random_points_in_region(region, N):
    """
    Gets N random points inside (or on the surface) of a mes
    """

    region_bounds = region.mesh.bounds()
    X = np.random.randint(region_bounds[0], region_bounds[1], size=10000)
    Y = np.random.randint(region_bounds[2], region_bounds[3], size=10000)
    Z = np.random.randint(region_bounds[4], region_bounds[5], size=10000)
    pts = [[x, y, z] for x, y, z in zip(X, Y, Z)]

    ipts = region.mesh.inside_points(pts).coordinates
    return np.vstack(random.choices(ipts, k=N))

scene = Scene(title="Labelled cells")

# Display a brain region
cornu_Ammonis_1 = scene.add_brain_region("CA1", alpha=0.2)

ca1_coordinates = []

base_dir = '/space/scratch/allen_visbehave_swr_data/allen_visbehave_swr_murphylab2024'

# Iterate over all subdirectories in the base directory
for dir_name in os.listdir(base_dir):
    if 'swrs_session_' in dir_name:
        # Extract the session ID from the directory name and get the session data
        session_id = int(dir_name.split('swrs_session_')[1])
        session = cache.get_session_data(session_id)

        # Iterate over all files in the directory
        for file_name in os.listdir(os.path.join(base_dir, dir_name))[0:2]:
            ca1_coordinates = []
            if 'karlsson' in file_name:
                # Extract probe_id from the filename
                probe_id = int(re.search('probe_(.*?)_', file_name).group(1))

                # Get a list of all CA1 channel ids specific to the probe_id
                ca1index = session.channels[(session.channels['ecephys_structure_acronym'] == 'CA1') & (session.channels['probe_id'] == probe_id)].index

                # Get the coordinates for all CA1 channels specific to the probe_id
                ca1_coordinates = session.channels.loc[ca1index, ['anterior_posterior_ccf_coordinate', 'dorsal_ventral_ccf_coordinate', 'left_right_ccf_coordinate']].values.astype(np.float32)
                print(ca1_coordinates.shape)
                scene.add(
                    Points(
                        ca1_coordinates,
                        name="probe_1",
                        colors="darkred",
                        radius=50,
                    )
                )

# Add label to the brain region
scene.add_label(cornu_Ammonis_1, "CA1")

# render
scene.content
scene.render()

plt = Plotter()
plt.show(*scene.renderables)
AngCamp commented 3 months ago

Ah I see now I forgot to add

import vedo
vedo.settings.default_backend= 'k3d'

So my full corrected code that runs in the notebook looks like....

import random

import numpy as np

from brainrender import Scene
from brainrender.actors import Points
import vedo
vedo.settings.default_backend= 'k3d'

from vedo import Plotter  # <- this will be used to render an embedded scene 

# Display the Allen Brain mouse atlas.
scene = Scene(atlas_name="allen_mouse_25um", title="Cells in primary visual cortex")
# Make sure it gets embedded in the window
scene.jupyter = True

def get_n_random_points_in_region(region, N):
    """
    Gets N random points inside (or on the surface) of a mes
    """

    region_bounds = region.mesh.bounds()
    X = np.random.randint(region_bounds[0], region_bounds[1], size=10000)
    Y = np.random.randint(region_bounds[2], region_bounds[3], size=10000)
    Z = np.random.randint(region_bounds[4], region_bounds[5], size=10000)
    pts = [[x, y, z] for x, y, z in zip(X, Y, Z)]

    ipts = region.mesh.inside_points(pts).coordinates
    return np.vstack(random.choices(ipts, k=N))

scene = Scene(title="Labelled cells")

# Display a brain region
cornu_Ammonis_1 = scene.add_brain_region("CA1", alpha=0.2)

ca1_coordinates = []

base_dir = '/space/scratch/allen_visbehave_swr_data/allen_visbehave_swr_murphylab2024'

# Iterate over all subdirectories in the base directory
for dir_name in os.listdir(base_dir):
    if 'swrs_session_' in dir_name:
        # Extract the session ID from the directory name and get the session data
        session_id = int(dir_name.split('swrs_session_')[1])
        session = cache.get_session_data(session_id)

        # Iterate over all files in the directory
        for file_name in os.listdir(os.path.join(base_dir, dir_name))[0:2]:
            ca1_coordinates = []
            if 'karlsson' in file_name:
                # Extract probe_id from the filename
                probe_id = int(re.search('probe_(.*?)_', file_name).group(1))

                # Get a list of all CA1 channel ids specific to the probe_id
                ca1index = session.channels[(session.channels['ecephys_structure_acronym'] == 'CA1') & (session.channels['probe_id'] == probe_id)].index

                # Get the coordinates for all CA1 channels specific to the probe_id
                ca1_coordinates = session.channels.loc[ca1index, ['anterior_posterior_ccf_coordinate', 'dorsal_ventral_ccf_coordinate', 'left_right_ccf_coordinate']].values.astype(np.float32)
                print(ca1_coordinates.shape)
                scene.add(
                    Points(
                        ca1_coordinates,
                        name="probe_1",
                        colors="darkred",
                        radius=50,
                    )
                )

# Add label to the brain region
scene.add_label(cornu_Ammonis_1, "CA1")

# render
scene.content
scene.render()

plt = Plotter()
plt.show(*scene.renderables)
adamltyson commented 3 months ago

@AngCamp glad this working for you. Out of curiosity, does the above script work on a remote server? It's not something the dev team use much, but we'd like to be able to support it.

AngCamp commented 2 months ago

Hi I'm very sorry for the slow reply. Yes I am running this notebook in VS code on a remote server, local instillation of VS code connecting through an SSH tunnel if that matters. Do you need any more information?

adamltyson commented 2 months ago

No, that's great. Thanks!