How to use the brainrender.scene.Scene function in brainrender

To help you get started, we’ve selected a few brainrender examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BrancoLab / BrainRender / tests / test_scene_io.py View on Github external
def test_custom_video():
    from brainrender.animation.video import CustomVideoMaker

    # --------------------------------- Variables -------------------------------- #
    N_FRAMES = 20

    # Variables to specify camera position at each frame
    zoom = np.linspace(1, 1.35, N_FRAMES)
    frac = np.zeros_like(
        zoom
    )  # for camera transition, interpolation value between cameras
    frac[:10] = np.linspace(0, 1, 10)
    frac[10:] = np.linspace(1, 0, len(frac[10:]))

    # ------------------------------- Create scene ------------------------------- #
    scene = Scene(display_inset=True, use_default_key_bindings=True)

    filepaths, data = scene.atlas.download_streamlines_for_region("TH")
    scene.add_brain_regions(["TH"], alpha=0.2)

    # Create new cameras
    cam1 = buildcam(sagittal_camera)
    cam2 = buildcam(top_camera)
    cam3 = buildcam(
        dict(
            position=[1862.135, -4020.792, -36292.348],
            focal=[6587.835, 3849.085, 5688.164],
            viewup=[0.185, -0.97, 0.161],
            distance=42972.44,
            clipping=[29629.503, 59872.10],
        )
    )
github BrancoLab / BrainRender / Examples / basic / adding_labels.py View on Github external
"""
    This tutorial shows how to use flags (strings of text that appear when you mose hoover over an actor)
    to add more information about the actors in your rendering. 
"""


import brainrender

brainrender.SHADER_STYLE = "cartoon"
from brainrender.scene import Scene

# Create a scene
scene = Scene()

# add_brain_regions can be used to add labels directly
scene.add_brain_regions("VAL", add_labels=True)

# you can also use scene.add_actor_label
mos = scene.add_brain_regions("MOs")

# Add another label, this time make it gray and shift it slightly
scene.add_actor_label(mos, "MOs", size=400, color="blackboard", xoffset=250)

scene.render()
github BrancoLab / BrainRender / Examples / example_scenes.py View on Github external
def ConnectivityScene():
    scene = Scene()
    p0 = scene.get_region_CenterOfMass("ZI")

    # Then we se these coordinates to get tractography data, note: any set of X,Y,Z coordinates would do. 
    tract = aba.get_projection_tracts_to_target(p0=p0)

    scene.add_tractography(tract, display_injection_structure=False, color_by="region", 
                        display_injection_volume=True, others_alpha=.25)
    scene.add_brain_regions(['ZI'], colors="ivory", alpha=1)

    scene.render()
github BrancoLab / BrainRender / Examples / mouse / tractography_vipregions.py View on Github external
""" 
    This tutorial shows how download and rendered afferent mesoscale projection data
    using the AllenBrainAtlas (ABA) and Scene classes and coloring a subset of the 
    data differently based on the location of virus injection
"""
import brainrender

brainrender.SHADER_STYLE = "cartoon"
from brainrender.scene import Scene


# Create a scene
scene = Scene(title="tractography")

# Get the center of mass of the region of interest
p0 = scene.atlas.get_region_CenterOfMass("ZI")

# Get projections to that point
tract = scene.atlas.get_projection_tracts_to_target(p0=p0)

# Add the brain regions and the projections to it
scene.add_brain_regions(["ZI"], alpha=0.4, use_original_color=True)
scene.add_tractography(
    tract, color_by="target_region", VIP_regions=["SCm"], VIP_color="green"
)

scene.render()
github BrancoLab / BrainRender / Examples / mouse / colored_neurons.py View on Github external
filterby="soma", filter_regions=["MOs"]
)

# Then we can download the files and save them as a .json file
neurons = mlapi.download_neurons(
    neurons_metadata[:50]
)  # 50 neurons, might take a while the first time


# ----------------------------- Rendering neurons ---------------------------- #

# Create a custom colormap between 3 colors
colors = makePalette(len(neurons), "salmon", "lightgreen")

# Create scene
scene = Scene(add_root=True, display_inset=False)

# Add each neuron with it's color
scene.add_neurons(neurons, alpha=0.8, neurite_radius=8, color=colors)

# Cut all actors to expose neurons
scene.cut_actors_with_plane("sagittal")
scene.render()
github BrancoLab / BrainRender / Examples / user_data / add_object_to_scene.py View on Github external
In this case the object is a .stl file of the mouse skull, 
    but it could be anything, including experimental and recording devices.

    Note that the mouse skull and brain meshes come from different sources
    so that's why they don't match perfectly.
"""


import brainrender

brainrender.SHADER_STYLE = "cartoon"
brainrender.ROOT_ALPHA = 1

from brainrender.scene import Scene

scene = Scene()

# Load skull from file
skull = scene.add_from_file("Examples/example_files/skull.stl")
skull.c("ivory").alpha(1)

# Align skull and brain (scene.root)
skull_com = skull.centerOfMass()
root_com = scene.root.centerOfMass()

skull.origin(skull.centerOfMass())
skull.rotateY(90).rotateX(180)
skull.x(root_com[0] - skull_com[0])
skull.y(root_com[1] - skull_com[1])
skull.z(root_com[2] - skull_com[2])
skull.x(3500)
skull.rotateZ(-25)
github BrancoLab / BrainRender / Examples / example_scenes.py View on Github external
def StreamlinesScene2():
    scene = Scene()

    streamlines_files, data = streamlines_api.download_streamlines_for_region("VAL") 
    scene.add_streamlines(data, color="palegreen", show_injection_site=False, alpha=.3, radius=10)

    streamlines_files, data = streamlines_api.download_streamlines_for_region("VM") 
    scene.add_streamlines(data, color="palevioletred", show_injection_site=False, alpha=.3, radius=10)

    
    scene.add_brain_regions(['VAL'], use_original_color=False, colors='palegreen', alpha=.9, hemisphere='right')
    mos = scene.actors['regions']['VAL']
    scene.edit_actors([mos], wireframe=True) 

    scene.add_brain_regions(['VM'], use_original_color=False, colors='palevioletred', alpha=.9, hemisphere='right')
    mos = scene.actors['regions']['VM']
    scene.edit_actors([mos], wireframe=True)
github BrancoLab / BrainRender / brainrender / Utils / AllenMorphologyAPI / visualize.py View on Github external
from brainrender.scene import Scene
from brainrender.Utils.parsers.mouselight import NeuronsParser
from brainrender.Utils.AllenMorphologyAPI.AllenMorphology import AllenMorphology


class AllenMorphologyVisualizer(Scene):
    def __init__(self):
        self.morphology = AllenMorphology()
        self.parser = NeuronsParser(Scene(), neurite_radius=1.5)

    def add_neurons(self, neurons, color=None):
        for neuron in neurons:
            actors, regions = self.parser.parse_neurons_swc_allen(neurons[0], 9999)

            for name, actor in actors.items():
                if actor is not None:
                    scene.add_vtkactor(actor)
            scene.render()
github BrancoLab / BrainRender / Examples / basic / labelled_cells2.py View on Github external
""" 
    This tutorial shows how to show the position of labelled cells in brainrender
    and color them based on the region they are in. 
"""
import pandas as pd

import brainrender

brainrender.SHADER_STYLE = "cartoon"
from brainrender.scene import Scene
from brainrender.Utils.scene_utils import get_n_random_points_in_region

# Create a scene
scene = Scene(
    title="labelled cells"
)  # specify that you want a view from the top


# Gerate the coordinates of N cells across 3 regions
_regions = ["MOs", "VISp", "ZI"]
N = 1000  # getting 1k cells per region, but brainrender can deal with >1M cells easily.

# Get fake cell coordinates
cells, regions = [], []  # to store x,y,z coordinates
for region in _regions:
    region_cells = get_n_random_points_in_region(
        scene.atlas, region=region, N=N
    )
    cells.extend(region_cells)
    regions.extend([region for i in region_cells])
github BrancoLab / BrainRender / Examples / mouse / mouselight.py View on Github external
scene.add_neurons(neurons, color="Reds", alpha=0.8)
scene.render()
scene.close()

# 3 specify a color for each neuron
scene = Scene(title="Color each")
scene.add_neurons(
    neurons,
    color=["salmon", "darkseagreeb", "skyblue", "chocolate", "darkgoldenrod"],
    alpha=0.8,
)
scene.render()
scene.close()

# 4 specify a color for each neuronal component
scene = Scene(title="Color components")
scene.add_neurons(
    neurons, color=dict(soma="red", dendrites="orange", axon="blackboard")
)
scene.render()
scene.close()


""" 
    For more options check the add_neurons function docstring (atlases.aba.Aba -> add_neurons).