def test_ruler(): s = Scene(title="BR") th = s.add_brain_region("TH", hemisphere="left") mos = s.add_brain_region("MOs", hemisphere="right") s.add( ruler(th.centerOfMass(), mos.centerOfMass(), unit_scale=0.01, units="mm")) s.render(interactive=False) del s
def test_cylinder(): s = Scene(title="BR") th = s.add_brain_region("TH") s.add(Cylinder(th, s.root)) s.add(Cylinder(th.centerOfMass(), s.root)) del s
def test_points_density(): s = Scene(title="BR") mos = s.add_brain_region("MOs", alpha=0.0) coordinates = get_n_random_points_in_region(mos, 2000) pd = s.add(PointsDensity(coordinates)) assert isinstance(pd, Actor) del s
def test_actor_removal(): s = Scene() th = s.add_brain_region("TH") assert len(s.actors) == 2 s.remove(th) assert len(s.actors) == 1 s.remove("no actor") assert len(s.actors) == 1
def test_scene_slice(): s = Scene() s.add_brain_region("TH") s.slice("frontal") ret = s.slice("frontal",) assert ret is None s.slice("sagittal", close_actors=True) s = Scene() th = s.add_brain_region("TH") plane = s.atlas.get_plane(pos=[1999, 1312, 3421], norm=[1, -1, 2]) s.slice(plane, actors=th) ret = s.slice(plane, actors=[th, s.root],) s.render(interactive=False) del s
def test_brain_regions(): scene = Scene() th = scene.add_brain_region("TH") assert scene.actors[-1] == th assert isinstance(th, Actor) regs = scene.add_brain_region("MOs", "CA1") assert isinstance(regs, list) assert len(regs) == 2 noone = scene.add_brain_region("what is this") assert noone is None a1 = scene.add_brain_region("TH", hemisphere="left") a2 = scene.add_brain_region("CA1", hemisphere="right") assert isinstance(a1, Actor) assert isinstance(a2, Actor) scene.render(interactive=False) del scene
def test_scene_render(): scene = Scene() scene.add_brain_region("TH") scene.render(interactive=False, zoom=1.4) scene.render( interactive=False, camera=dict( pos=(10705.845660949382, 7435.678067378925, -36936.3695486442,), viewup=( -0.0050579179155257475, -0.9965615097647067, -0.08270172139591858, ), clippingRange=(30461.81976236306, 58824.38622122339), ), ) scene.render(interactive=False, camera="sagittal") del scene
def test_get_actors(): s = Scene() th = s.add_brain_region("TH") found1 = s.get_actors(name="TH") assert len(found1) == 1 assert th in found1 found2 = s.get_actors(br_class="brain region") assert len(found2) == 2 assert th in found2 assert s.root in found2
def test_ruler_from_surface(): s = Scene(title="BR") th = s.add_brain_region("TH", hemisphere="left") s.add( ruler_from_surface(th.centerOfMass(), s.root, unit_scale=0.01, units="mm")) s.render(interactive=False) del s
def test_brain_regions(): scene = Scene() th = scene.add_brain_region("TH") assert scene.actors[-1] == th assert isinstance(th, Actor) regs = scene.add_brain_region("MOs", "CA1") assert isinstance(regs, list) assert len(regs) == 2 nan = scene.add_brain_region("MOs", "CA1") assert nan is None noone = scene.add_brain_region("what is this") assert noone is None scene.add_brain_region("TH", hemisphere="left") scene.add_brain_region("CA1", hemisphere="right") scene.add_brain_region("STN", hemisphere="right")
def test_export_for_web(): s = Scene(title="BR") th = s.add_brain_region("TH") s.add_label(th, "TH") path = s.export("test.html") assert path == "test.html" path = Path(path) assert path.exists() path.unlink() with pytest.raises(ValueError): path = s.export("test.py")
# camera settings cam = { "pos": (-16954, 2456, -3961), "viewup": (0, -1, 0), "clippingRange": (22401, 34813), "focalPoint": (7265, 2199, -5258), "distance": 24256, } # create scene scene = Scene(inset=False, screenshots_folder="paper/screenshots", root=True) scene.root._needs_silhouette = False scene.root.alpha(0.5) # add brain regions pag = scene.add_brain_region("PAG", alpha=0.4, silhouette=False, color=pagcol) scm = scene.add_brain_region("SCm", alpha=0.3, silhouette=False, color=scmcol) # add neuron mesh neuron = scene.add("paper/data/yulins_neuron.stl") neuron.c(neuroncol) # add sphere at soma location soma_pos = [9350.51912036, 2344.33986638, 5311.18297796] point = scene.add(Point(soma_pos, color=neuroncol, radius=25)) scene.add_silhouette(point, lw=1, color="k") scene.add_silhouette(neuron, lw=1, color="k") # slice scene repeatedly to cut out region of interest p = [9700, 1, 800] plane = scene.atlas.get_plane(pos=p, plane="frontal")
def test_labels(): scene = Scene() th = scene.add_brain_region("TH") scene.add_label(th, "TH")
import napari from benchmark.timer import SimpleTimer from brainrender import Scene """ Compare performance on napari """ # get a couple meshes with brainrender scene = Scene() print("Getting mesh data") regions = scene.atlas.get_structure_descendants("root") for reg in regions[:400]: try: scene.add_brain_region(reg) except FileNotFoundError: pass surfaces = [] for act in scene.clean_actors: surfaces.append( (act.points(), act.faces(), np.ones(len(act.points())) * 0.5)) # render stuff in napar timer = SimpleTimer("napari") with napari.gui_qt(): print("creating viewer") viewer = napari.Viewer(ndisplay=3) print("Adding surfaces")
from brainrender import Scene, Animation from benchmark.timer import Timer # Create a brainrender scene scene = Scene(title="brain regions", inset=False) with Timer(scene, name="Animation"): # Add brain regions for br in ("TH", "MOs", "MOp", "CA1", "CB", "MB"): scene.add_brain_region(br, silhouette=False) anim = Animation(scene, "examples", "vid3") # Specify camera position and zoom at some key frames # each key frame defines the scene's state after n seconds have passed anim.add_keyframe(0, camera="top", zoom=1.3) anim.add_keyframe(1, camera="sagittal", zoom=2.1) anim.add_keyframe(2, camera="frontal", zoom=3) anim.add_keyframe(3, camera="frontal", zoom=2) # Make videos anim.make_video(duration=3, fps=10)
from pathlib import Path print(f"[{orange}]Running example: {Path(__file__).name}") # Explicitly initiliase a scene with the screenshot folder set # If the screenshot folder is not set, by default screenshots # Will save to the current working directory screenshot_folder = "./examples/screenshots" scene = Scene( title=f"Screenshots will be saved to {screenshot_folder}", inset=True, screenshots_folder=screenshot_folder, ) # Add some actors to the scene scene.add_brain_region("TH", alpha=0.2, silhouette=True, color=salmon) scene.add_brain_region("VISp", alpha=0.4, silhouette=False, color=[50, 2, 155]) scene.slice("sagittal") # Set up a camera. Can use string, such as "sagittal". # During render runtime, press "c" to print the current camera parameters. camera = { "pos": (8777, 1878, -44032), "viewup": (0, -1, 0), "clippingRange": (24852, 54844), "focalPoint": (7718, 4290, -3507), "distance": 40610, } zoom = 1.5
save_folder = Path( "/Users/federicoclaudi/Dropbox (UCL)/Rotation_vte/Presentations/Presentations/Fiete lab" ) settings.SHOW_AXES = False BREGMA = np.array([5400, 0, 0]) # AP # DV # ML top = np.array([4.136, -2.106, 0]) * 1000 + BREGMA # AP ML DV tip = np.array([5.507, -0.584, 6.489]) * 1000 + BREGMA scene = Scene( inset=False, screenshots_folder=save_folder, ) cun, grn, mos = scene.add_brain_region("CUN", "GRN", "MOs", alpha=0.7) # CUN/GRN probe tip[1] = tip[1] + scene.root.centerOfMass()[2] top[1] = top[1] + scene.root.centerOfMass()[2] top = top[[0, 2, 1]] tip = tip[[0, 2, 1]] scene.add(shapes.Cylinder(pos=[top, tip], c="k", r=50, alpha=1)) # MOs probe mos_center = mos.centerOfMass() + np.array([1000, 0, -800]) for x in [-500, -1000, -1500, -2000]: scene.add( Cylinder(mos_center + np.array([x, 0, 0]),
from brainrender import Scene from brainrender.actors import ruler, ruler_from_surface from rich import print from myterial import orange from pathlib import Path print(f"[{orange}]Running example: {Path(__file__).name}") scene = Scene(title="rulers") th, mos = scene.add_brain_region("TH", "MOs", alpha=0.3) # Get a ruler between the two regions p1 = th.centerOfMass() p2 = mos.centerOfMass() rul1 = ruler(p1, p2, unit_scale=0.01, units="mm") # Get a ruler between thalamus and brian surface rul2 = ruler_from_surface(p1, scene.root, unit_scale=0.01, units="mm") scene.add(rul1, rul2) scene.render()
from brainrender import Scene from brainrender.atlas_specific import get_streamlines_for_region from brainrender.actors.streamlines import make_streamlines # Create a brainrender scene scene = Scene() # Add brain regions scene.add_brain_region("TH") # Get stramlines data and add streams = get_streamlines_for_region("TH") scene.add(*make_streamlines(*streams, color="salmon", alpha=0.5)) # Render! scene.render()
for act in streams: alpha = act.alpha() act.alpha(alpha + 0.02) # ------------------------------- create scene ------------------------------- # scene = Scene(inset=INSET) scene.root._needs_silhouette = SILHOUETTE coords = np.load("paper/data/cell-detect-paper-cells.npy") cells = scene.add(Points(coords, radius=30, colors=salmon)) rsp = scene.add_brain_region( "RSP", alpha=0.5, silhouette=SILHOUETTE, ) scene.add_brain_region( "TH", alpha=0.15, color=[0.6, 0.6, 0.6], silhouette=SILHOUETTE, ) root_box(scene) # get streamlines data streams = get_streamlines_for_region("RSP") # ----------------------------- create animation ----------------------------- #
from brainrender import Scene from brainrender import settings from brainrender.atlas_specific import GeneExpressionAPI settings.SHOW_AXES = False scene = Scene(inset=False) gene = "Gpr161" geapi = GeneExpressionAPI() expids = geapi.get_gene_experiments(gene) data = geapi.get_gene_data(gene, expids[1]) gene_actor = geapi.griddata_to_volume(data, min_quantile=99, cmap="inferno") act = scene.add(gene_actor) ca1 = scene.add_brain_region("CA1", alpha=0.2, color="skyblue") ca3 = scene.add_brain_region("CA3", alpha=0.5, color="salmon") scene.add_silhouette(act) scene.render(zoom=1.6)
print(f"[{orange}]Running example: {Path(__file__).name}") 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.insidePoints(pts).points() return np.vstack(random.choices(ipts, k=N)) scene = Scene(title="Labelled cells") # Get a numpy array with (fake) coordinates of some labelled cells mos = scene.add_brain_region("MOs", alpha=0.15) coordinates = get_n_random_points_in_region(mos, 2000) # Add to scene scene.add(Points(coordinates, name="CELLS", colors="steelblue")) # render scene.content scene.render()
"Isocortex", "HPF", "STR", "PAL", "CB", "MB", "TH", "HY", "P", "MY", "CTXsp", "OLF", "VISC", ) for main in mains: subs = scene.atlas.get_structure_descendants(main) for sub in subs: try: reg = scene.add_brain_region(sub, silhouette=SILHOUETTE) except FileNotFoundError: pass # ----------------------------- create animation ----------------------------- # anim = Animation(scene, "videos", "regions", size=None) anim.add_keyframe(0, camera="frontal", zoom=1.5, callback=slc, duration=10) anim.make_video(duration=10, fps=30)
from brainrender import Scene from rich import print from myterial import orange from pathlib import Path print(f"[{orange}]Running example: {Path(__file__).name}") # Create a brainrender scene scene = Scene(title="Injection in SCm") # Add brain SCm scene.add_brain_region("SCm", alpha=0.2) # Add from file scene.add("examples/data/CC_134_1_ch1inj.obj", color="tomato") # Render! scene.render()
from brainrender import Scene scene = Scene(atlas_name="allen_mouse_10um") scene.add_brain_region("ZI", "SCm") scene.render()
def test_labels(): scene = Scene() th = scene.add_brain_region("TH") scene.add_label(th, "TH") scene.render(interactive=False) del scene
from rich import print from pathlib import Path from brainrender import Scene sys.path.append("./") from paper.figures import INSET, SILHOUETTE print("[bold red]Running: ", Path(__file__).name) # camera settings cam = { "pos": (-20268, -6818, 14964), "viewup": (0, -1, 0), "clippingRange": (16954, 58963), "focalPoint": (6489, 4329, -5556), "distance": 35514, } # create scene scene = Scene(inset=INSET, screenshots_folder="paper/screenshots") scene.root._needs_silhouette = SILHOUETTE # add brain regions for reg, col in zip(("SCm", "SCs", "ZI"), (scmcol, scscol, zicol)): scene.add_brain_region(reg, color=col, silhouette=SILHOUETTE) # render scene.render(zoom=1.75, camera=cam) scene.screenshot(name="mouse_regions", scale=1)
from brainrender.actors import ruler, Point import numpy as np # To plan probe placement # planner = Planner('plan.yaml', 'myprobe.yaml') # planner.plan() # To measure distances and angles BREGMA = np.array([5400, 0, 0]) # AP # DV # ML top = np.array([4.136, -2.106, 0]) * 1000 + BREGMA # AP ML DV tip = np.array([5.507, -0.584, 6.489]) * 1000 + BREGMA scene = Scene() cun, grn = scene.add_brain_region("CUN", "GRN", alpha=0.4) tip[1] = tip[1] + scene.root.centerOfMass()[2] top[1] = top[1] + scene.root.centerOfMass()[2] top = top[[0, 2, 1]] tip = tip[[0, 2, 1]] print(f"Distance between tip and top: {np.linalg.norm(top-tip):.3f}") scene.add(Point(top)) scene.add(Point(tip)) p = cun.centerOfMass() p[2] -= 1400 scene.add(Point(p))
""" This example shows how to add a label to a renderend actor """ from brainrender import Scene from rich import print from myterial import orange from pathlib import Path print(f"[{orange}]Running example: {Path(__file__).name}") # crate a scene and add brain regions scene = Scene() th, mos = scene.add_brain_region("TH", "MOs") scene.add_label(th, "TH") scene.add_label(mos, "My region") # render scene.render()
""" from brainrender import Scene from brainrender import settings from brainrender.actors import Cylinder settings.SHOW_AXES = False settings.WHOLE_SCREEN = False from rich import print from myterial import orange from pathlib import Path print(f"[{orange}]Running example: {Path(__file__).name}") scene = Scene(inset=False, title="optic canula") th = scene.add_brain_region( "TH", alpha=0.4, ) # create and add a cylinder actor actor = Cylinder( th, # center the cylinder at the center of mass of th scene.root, # the cylinder actor needs information about the root mesh ) scene.add(actor) scene.render(zoom=1.6)