Closed AngCamp closed 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.
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)
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)
@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.
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?
No, that's great. Thanks!
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:
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.