def NeuronsScene3(): scene = Scene() neurons_metadata = mouselight_fetch_neurons_metadata( filterby='soma', filter_regions=['VAL']) neurons_files = mlapi.download_neurons(neurons_metadata[2:6]) scene.add_neurons(neurons_files, soma_color='deepskyblue', force_to_hemisphere="right") scene.add_brain_regions(['VAL'], use_original_color=False, colors='palegreen', alpha=.9) mos = scene.actors['regions']['VAL'] scene.edit_actors([mos], wireframe=True) streamlines_files, data = streamlines_api.download_streamlines_for_region( "VAL") scene.add_streamlines(data[:1], color="palegreen", show_injection_site=False, alpha=.2, radius=10) scene.render()
def test_mouselight(): from brainrender.Utils.MouseLightAPI.mouselight_api import MouseLightAPI from brainrender.Utils.MouseLightAPI.mouselight_info import mouselight_api_info, mouselight_fetch_neurons_metadata # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mouselight_fetch_neurons_metadata( filterby='soma', filter_regions=['MOs']) # Then we can download the files and save them as a .json file ml_api = MouseLightAPI() neurons_files = ml_api.download_neurons( neurons_metadata[:2] ) # just saving the first couple neurons to speed things up # Show neurons and ZI in the same scene: scene = Scene() scene.add_neurons( neurons_files, soma_color='orangered', dendrites_color='orangered', axon_color='darkseagreen', neurite_radius=8 ) # add_neurons takes a lot of arguments to specify how the neurons should look # make sure to check the source code to see all available optionsq scene.add_brain_regions(['MOs'], alpha=0.15) scene.render(camera='coronal')
def _make_root(self, rootpath): """ Creates a root mesh by merging the mesh corresponding to each neuron, then saves it as an obj file at rootpath """ raise NotImplementedError( "Create root method not supported yet, sorry") print(f"Creating root mesh for atlas {self.atlas_name}") temp_scene = Scene( atlas=Celegans, add_root=False, display_inset=False, atlas_kwargs=dict(data_folder=self.data_folder), ) temp_scene.add_neurons(self.neurons_names) temp_scene.render(interactive=False) temp_scene.close() root = merge(*temp_scene.actors["neurons"]).clean().cap() # root = mesh2Volume(root, spacing=(0.02, 0.02, 0.02)).isosurface() points = Points(root.points()).smoothMLS2D(f=0.8).clean(tol=0.005) root = recoSurface(points, dims=100, radius=0.2) # Save write(root, rootpath) del temp_scene return root
def NeuronsScene2(): scene = Scene() neurons_metadata = mouselight_fetch_neurons_metadata(filterby='soma', filter_regions=['MOp5']) neurons_files = mlapi.download_neurons(neurons_metadata[2:6]) scene.add_neurons(neurons_files, soma_color='deepskyblue', force_to_hemisphere="right") streamlines_files, data = streamlines_api.download_streamlines_for_region("MOp") scene.add_streamlines(data[:1], color="palegreen", show_injection_site=False, alpha=.2, radius=10) set_camera(scene) scene.render()
def test_neurons(): scene = Scene() mlapi = MouseLightAPI() neurons_metadata = mouselight_fetch_neurons_metadata(filterby='soma', filter_regions=['MOs']) neurons_files = mlapi.download_neurons(neurons_metadata[:2]) parser = NeuronsParser(scene=scene, color_neurites=True, axon_color="antiquewhite", soma_color="darkgoldenrod", dendrites_color="firebrick") neurons, regions = parser.render_neurons(neurons_files) scene.add_neurons(neurons_files, color_neurites=False, random_color="jet", display_axon_regions=False)
def NeuronsScene(show_regions = False): scene = Scene() fl = 'Examples/example_files/one_neuron.json' scene.add_neurons(fl, soma_color='darkseagreen', force_to_hemisphere="right",) if show_regions: scene.add_brain_regions(['ZI', 'PAG', 'MRN', 'NPC', "VTA", "STN", "PPT", "SCm", "HY"], use_original_color=True, alpha=.5) set_camera(scene) scene.render()
def test_fish_neurons(): api = MpinMorphologyAPI() # api.download_dataset() neurons_ids = api.get_neurons_by_structure(837)[:5] neurons = api.load_neurons(neurons_ids) neurons = [ neuron.create_mesh(soma_radius=1, neurite_radius=1)[1] for neuron in neurons ] scene = Scene(atlas="mpin_zfish_1um", add_root=True, camera="sagittal2") scene.add_neurons(neurons) scene.render(interactive=False) scene.close()
def test_neurons(): scene = Scene() mlapi = MouseLightAPI() # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mlapi.fetch_neurons_metadata(filterby='soma', filter_regions=['MOs']) # Then we can download the files and save them as a .json file neurons = mlapi.download_neurons(neurons_metadata[:5]) scene = Scene(title='One color') scene.add_neurons(neurons, color='salmon', display_axon=True, neurite_radius=6) scene.render(interactive=False) scene.close()
screenshot_params = dict( folder = './screenshots/single_cell_views', name='top_{0}'.format(cell), ) # scene = Scene(display_inset = False, screenshot_kwargs=screenshot_params) # scene.add_neurons(filepath, soma_color='black', dendrites_color='black', # axon_color='red', neurite_radius=8) for cell,fp in cell_filepaths.items(): screenshot_params = dict( folder = './screenshots/single_cell_views', name='top_{0}'.format(cell), ) scene = Scene(display_inset=False, screenshot_kwargs=screenshot_params) scene.add_neurons(fp, soma_color='black', dendrites_color='black', axon_color='red', neurite_radius=18) scene.render(interactive=False,camera='top', zoom=1) scene.take_screenshot() # for streamline,fp in sl_filepaths.items(): # screenshot_params = dict( # folder = './screenshots/single_cell_views', # name='top_{0}'.format(streamline), # ) # scene = Scene(display_inset=False, screenshot_kwargs=screenshot_params) # scene.add_streamlines(sl_filepaths[streamline], color=sl_colors[streamline], show_injection_site=True) # scene.render(interactive=False,camera='top', zoom=1) # scene.take_screenshot()
import brainrender brainrender.WHOLE_SCREEN = True brainrender.SHADER_STYLE = 'cartoon' from brainrender.scene import Scene from brainrender.animation.video import BasicVideoMaker scene = Scene(display_inset=False, camera="sagittal") scene.add_brain_regions(['MOs']) scene.add_neurons("Examples/example_files/neuron1.swc", color="black") vmkr = BasicVideoMaker(scene) vmkr.make_video(azimuth=1, niters=36, duration=30, save_name="test")
with open(fp) as f: temp = json.load(f) print(len(temp['neurons'])) x = temp['neurons'][0]['soma']['x'] y = temp['neurons'][0]['soma']['y'] z = temp['neurons'][0]['soma']['z'] return (x,y,z) for k,vlist in groups.items(): if k not in [1,3,2,12]: screenshot_params = dict( folder = './screenshots/single_cell_views', name='top_cluster_{0}'.format(k), ) scene = Scene(display_inset=False, screenshot_kwargs=screenshot_params) for i,neuron in enumerate(vlist): if neuron not in left_neurons: fp = cell_filepaths[neuron] else: fp = cell_filepaths[neuron].split('.json')[0] + '_reversed.json' scene.add_neurons(fp, soma_color=cmap[i], render_axons=True, render_dendrites=True, dendrites_color=cmap[i], axon_color=cmap[i], neurite_radius=18) # pos = get_point(fp) # print(pos) # scene.add_sphere_at_point(pos=pos, radius= 75, color=cmap[i]) scene.render(interactive=False,camera='top', zoom=1) scene.take_screenshot()
# ---------------------------- Downloading neurons --------------------------- # mlapi = MouseLightAPI() # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mlapi.fetch_neurons_metadata(filterby="soma", filter_regions=["MOs"]) # Then we can download the files and save them as a .json file neurons = mlapi.download_neurons(neurons_metadata[:5]) # ----------------------------- Rendering neurons ---------------------------- # # 1 color all neurons of the same color, don't show axons scene = Scene(title="One color") scene.add_neurons(neurons, color="salmon", display_axon=False, neurite_radius=6) scene.render() scene.close() # 2 color each neuron of a different color using a colormap scene = Scene(title="Colormap") 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"],
add_root=False, atlas=Celegans, # Pass the custom atlas class to scene. display_inset=False, atlas_kwargs=dict( data_folder=data_folder ), # use this to pass keyword arguments to the Atlas class title="Whole connectome", ) # Exclude some neurons we don't want to render metadata = scene.atlas.neurons_metadata neurons = metadata.loc[(metadata.type != "nonvalid") & (metadata.type != "other")] # Add each neuron with the corresponding outline scene.add_neurons(list(neurons.neuron.values)) for neuron in scene.actors["neurons"]: scene.add_actor(neuron.silhouette().lw(3).c("k")) # Render scene.render() scene.close() # ------------------------- Scene 2: showing synapses ------------------------ # scene = Scene( add_root=False, atlas=Celegans, # Pass the custom atlas class to scene. display_inset=False, atlas_kwargs=dict( data_folder=data_folder
frac[150:] = np.linspace(1, 0, len(frac[150:])) # -------------------------------- Fetch data -------------------------------- # # Then we can download the files and save them as a .json file ml_api = MouseLightAPI() # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = ml_api.fetch_neurons_metadata(filterby="soma", filter_regions=["MOs"]) neurons_files = ml_api.download_neurons(neurons_metadata[:N_neurons]) # ------------------------------- Create scene ------------------------------- # scene = Scene(display_inset=False, use_default_key_bindings=True) neurons = scene.add_neurons(neurons_files, neurite_radius=12, alpha=0) # Make all neurons background for neuron in neurons: for mesh in neuron.values(): mesh.alpha(minalpha) mesh.color(darkcolor) # Create new cameras cam1 = buildcam(sagittal_camera) cam2 = buildcam( dict( position=[-16624.081, -33431.408, 33527.412], focal=[6587.835, 3849.085, 5688.164], viewup=[0.634, -0.676, -0.376],
# ---------------------------- Downloading neurons --------------------------- # mlapi = MouseLightAPI() # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mlapi.fetch_neurons_metadata(filterby='soma', filter_regions=['MOs']) # Then we can download the files and save them as a .json file neurons = mlapi.download_neurons(neurons_metadata[:5]) # ----------------------------- Rendering neurons ---------------------------- # # 1 color all neurons of the same color, don't show axons scene = Scene(title='One color') scene.add_neurons(neurons, color='salmon', display_axon=False, neurite_radius=6) scene.render() scene.close() # 2 color each neuron of a different color using a colormap scene = Scene(title='Colormap') scene.add_neurons(neurons, color='Reds', alpha=.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'],
# Start by creating a scene scene = Scene(screenshot_kwargs=screenshot_params) # Add single cells for cell in fig['cells']: if cell == 'AA0271' or cell == 'AA0876' or cell.startswith( '17781') or cell == 'AA0649' or cell == 'AA0898': cfp = cell + '.json' else: cfp = cell + '_reversed.json' nfp = os.path.join(cells_base_dir, cfp) scene.add_neurons(nfp, soma_color=cell_colors[cell], dendrites_color='black', axon_color=cell_colors[cell], neurite_radius=18) # Add streamlines for streamline in fig['streamlines']: scene.add_streamlines(sl_filepaths[streamline], color=sl_colors[streamline], show_injection_site=True) # # angle 1 # bespoke_camera = dict( # position = [-1, -.3, .7] , # focal = [0, 0, 0], # viewup = [0, -1, 0], # distance = 9522.144,
from brainrender.colors import makePalette from morphapi.morphology.morphology import Neuron from morphapi.api.mouselight import MouseLightAPI, mouselight_structures_identifiers # ---------------------------- Downloading neurons --------------------------- # mlapi = MouseLightAPI() # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mlapi.fetch_neurons_metadata(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=.8, neurite_radius=8, color=colors) # Cut all actors to expose neurons scene.cut_actors_with_plane('sagittal') scene.render()
passing the downloaded files to `scene.add_neurons`. """ import brainrender brainrender.USE_MORPHOLOGY_CACHE = True from brainrender.scene import Scene from brainrender.Utils.MouseLightAPI.mouselight_api import MouseLightAPI from brainrender.Utils.MouseLightAPI.mouselight_info import mouselight_api_info, mouselight_fetch_neurons_metadata # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mouselight_fetch_neurons_metadata(filterby='soma', filter_regions=['MOs']) # Then we can download the files and save them as a .json file ml_api = MouseLightAPI() neurons_files = ml_api.download_neurons( neurons_metadata[:2] ) # just saving the first couple neurons to speed things up # Show neurons and ZI in the same scene: scene = Scene() scene.add_neurons( neurons_files, soma_color='orangered', dendrites_color='orangered', axon_color='darkseagreen', neurite_radius=8 ) # add_neurons takes a lot of arguments to specify how the neurons should look # make sure to check the source code to see all available optionsq scene.add_brain_regions(['MOs'], alpha=0.15) scene.render(camera='coronal')
reconstructions aligned to the zebra fish atlas. """ # Use morphapi's API to download the neurons api = MpinMorphologyAPI() # the first time you use this dataset you'll have to download the data # api.download_dataset() # ----------------------------- Download dataset ----------------------------- # """ If it's the first time using this API, you'll have to download the dataset with all of the neurons' data. """ # Select a few neurons for a specific brain region neurons_ids = api.get_neurons_by_structure(837) neurons = api.load_neurons(neurons_ids) # Create meshes for each neuron neurons = [ neuron.create_mesh(soma_radius=1, neurite_radius=1)[1] for neuron in neurons ] colors = makePalette(len(neurons), "salmon", "powderblue") # ------------------------------- Visualisation ------------------------------ # scene = Scene(atlas="mpin_zfish_1um", add_root=True, camera="sagittal2") scene.add_neurons(neurons, color=colors) scene.render()
frac[150:] = np.linspace(1, 0, len(frac[150:])) # -------------------------------- Fetch data -------------------------------- # # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = mouselight_fetch_neurons_metadata(filterby='soma', filter_regions=['MOs']) # Then we can download the files and save them as a .json file ml_api = MouseLightAPI() neurons_files = ml_api.download_neurons(neurons_metadata[:N_neurons]) # ------------------------------- Create scene ------------------------------- # scene = Scene(display_inset=False, use_default_key_bindings=True) root = scene.actors['root'] scene.add_neurons(neurons_files, random_color=True, neurite_radius=12, alpha=0) # Make all neurons background for neuron in scene.actors['neurons']: for mesh in neuron.values(): mesh.alpha(minalpha) mesh.color(darkcolor) # Create new cameras cam1 = buildcam(sagittal_camera) cam2 = buildcam( dict( position=[-16624.081, -33431.408, 33527.412], focal=[6587.835, 3849.085, 5688.164], viewup=[0.634, -0.676, -0.376],
def test_animated_scene(): # --------------------------------- Variables -------------------------------- # minalpha = 0.01 # transparency of background neurons darkcolor = "lightgray" # background neurons color N_FRAMES = 50 N_neurons = 4 # number of neurons to show in total, if -1 all neurons are shown but it might take a while to render them at first N_neurons_in_frame = ( 2 # number of neurons to be highlighted in a given frame ) N_frames_for_change = 15 # every N frames which neurons are shown changes # Variables to specify camera position at each frame zoom = np.linspace(1, 1.5, 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:])) # -------------------------------- Fetch data -------------------------------- # # Then we can download the files and save them as a .json file ml_api = MouseLightAPI() # Fetch metadata for neurons with some in the secondary motor cortex neurons_metadata = ml_api.fetch_neurons_metadata(filterby="soma", filter_regions=["MOs"]) neurons_files = ml_api.download_neurons(neurons_metadata[:N_neurons]) # ------------------------------- Create scene ------------------------------- # scene = Scene(display_inset=False, use_default_key_bindings=True) neurons_actors = scene.add_neurons(neurons_files, neurite_radius=12, alpha=0) # Create new cameras cam1 = buildcam(sagittal_camera) cam2 = buildcam( dict( position=[-16624.081, -33431.408, 33527.412], focal=[6587.835, 3849.085, 5688.164], viewup=[0.634, -0.676, -0.376], distance=51996.653, clipping=[34765.671, 73812.327], )) 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], )) # ------------------------------- Create frames ------------------------------ # # Create frames prev_neurons = [] for step in track(np.arange(N_FRAMES), total=N_FRAMES, description="Generating frames..."): if step % N_frames_for_change == 0: # change neurons every N framse # reset neurons from previous set of neurons for neuron in prev_neurons: for component, actor in neuron.items(): actor.alpha(minalpha) actor.color(darkcolor) prev_neurons = [] # highlight new neurons neurons = choices(neurons_actors, k=N_neurons_in_frame) for n, neuron in enumerate(neurons): color = colorMap(n, "Greens_r", vmin=-2, vmax=N_neurons_in_frame + 3) for component, actor in neuron.items(): actor.alpha(1) actor.color(color) prev_neurons.append(neuron) # Move scene camera between 3 cameras scene.plotter.moveCamera(cam1, cam2, frac[step]) if frac[step] == 1: cam1 = cam3 # Update rendered window time.sleep(0.1) scene.render(zoom=zoom[step], interactive=False, video=True) scene.close()
import brainrender brainrender.WHOLE_SCREEN = True brainrender.SHADER_STYLE = 'cartoon' from brainrender.scene import Scene from brainrender.animation.video import BasicVideoMaker scene = Scene(display_inset=False, camera="sagittal") scene.add_brain_regions(['MOs']) scene.add_neurons("Examples/example_files/one_neuron.json", soma_color="black") vmkr = BasicVideoMaker(scene) vmkr.make_video(azimuth=1, niters=360, duration=30, save_name="test")