dbbs-lab / bsb-core

The Brain Scaffold Builder
https://bsb.readthedocs.io
GNU General Public License v3.0
21 stars 16 forks source link

How to Visualize the Geometries #897

Open utkuoguzman opened 5 days ago

utkuoguzman commented 5 days ago

Hi, In documentation I couldn't see a recommendation on how to visualize the morphologies of neurons so that one can see the relative distances between the parts of the neurons. What is your recommended method? What if I want to see all of them together?

drodarie commented 5 days ago

Hi, thank you for reaching us!

Unfortunately, the bsb visualization package (bsb-plotting) has been deprecated for a while (as we have focused on our core functions). There are however several tools that you can use to visualize morphologies. Among them, you can use:

Here is a small code snippet showing how you can extract and display points of the morphology in 3D:

import numpy as np
from matplotlib.pylab import plt
from bsb import parse_morphology_file, from_storage

# Load morphology from file
morpho = parse_morphology_file("path/to/file.swc") 
offset_position = [10, 20, 30]  # position of the soma
rotation = [0, 90, 0]  # rotation in degrees

# otherwise load from compiled network
scaffold = from_storage("my_circuit.hdf5")
cell_type_name = "my_cell_type"
cell_id = 14
ps = scaffold.get_placement_set(cell_type_name)
morpho = ps.load_morphologies().get(cell_id)
rotation = ps.load_rotations()[cell_id]
offset_position = ps.load_positions()[cell_id]

# Rotate, translate morphology
morpho.rotate(rotation)
morpho.translate(offset_position)

# Example of 3D display with matplotlib
fig = plt.figure(figsize=(10, 10))
ax1 = fig.add_subplot(111, projection='3d')
for branch in morpho.branches:
    x, z, y = (branch.points.T)
    # filter labels to use a different color for dendrites and axons
    is_axon = np.array([np.isin(list(branch.labelsets[branch.labels[i]]), ["axon", "tag_18"]).any() for i in range(len(branch.points))])
    ax1.plot(x[~is_axon], y[~is_axon], z[~is_axon], c="blue")
    ax1.plot(x[is_axon], y[is_axon], z[is_axon], c="red")
mins = np.min(morpho.points, axis=0)
maxs = np.max(morpho.points, axis=0)
ax1.plot([mins + max(maxs - mins)]

plt.show()