Example #1
0
	def edit_actors(self, actors, **kwargs):
		"""
		edits a list of actors (e.g. render as wireframe or solid)
		:param actors: list of actors
		:param **kwargs:

		"""
		if not isinstance(actors, list):
			actors = [actors]

		for actor in actors:
			actors_funcs.edit_actor(actor, **kwargs)
Example #2
0
def test_edit_actors(scene):
    actor = scene.add_brain_regions("TH")

    edit_actor(
        actor,
        wireframe=True,
        color="red",
        line=True,
        line_kwargs={"lw": 2},
        upsample=True,
        downsample=False,
    )
    edit_actor(
        actor,
        solid=True,
        downsample=True,
        smooth=True,
    )
Example #3
0
    def get_brain_regions(
        self,
        brain_regions,
        add_labels=False,
        colors=None,
        use_original_color=True,
        alpha=None,
        hemisphere=None,
        verbose=False,
        **kwargs,
    ):
        """
                Gets brain regions meshes for rendering
                Many parameters can be passed to specify how the regions should be rendered.
                To treat a subset of the rendered regions, specify which regions are VIP. 
                Use the kwargs to specify more detailes on how the regins should be rendered (e.g. wireframe look)

                :param brain_regions: str list of acronyms of brain regions
                :param colors: str, color of rendered brian regions (Default value = None)
                :param use_original_color: bool, if True, the allen's default color for the region is used.  (Default value = False)
                :param alpha: float, transparency of the rendered brain regions (Default value = None)
                :param hemisphere: str (Default value = None)
                :param add_labels: bool (default False). If true a label is added to each regions' actor. The label is visible when hovering the mouse over the actor
                :param **kwargs: used to determine a bunch of thigs, including the look and location of lables from scene.add_labels
            """
        # Parse arguments
        if alpha is None:
            alpha = brainrender.DEFAULT_STRUCTURE_ALPHA

        # check that we have a list
        if not isinstance(brain_regions, list):
            brain_regions = [brain_regions]

        # check the colors input is correct
        if colors is not None:
            if isinstance(colors, (list, tuple)):
                if not len(colors) == len(brain_regions):
                    raise ValueError(
                        "when passing colors as a list, the number of colors must match the number of brain regions"
                    )
                for col in colors:
                    if not check_colors(col):
                        raise ValueError(
                            "Invalide colors in input: {}".format(col))
            else:
                if not check_colors(colors):
                    raise ValueError(
                        "Invalide colors in input: {}".format(colors))
                colors = [colors for i in range(len(brain_regions))]

        # loop over all brain regions
        actors = {}
        for i, region in enumerate(brain_regions):
            if verbose:
                print("Rendering: ({})".format(region))

            # get the structure and check if we need to download the object file
            if region not in self.lookup_df.acronym.values:
                print(
                    f"The region {region} doesn't seem to belong to the atlas being used: {self.atlas_name}. Skipping"
                )
                continue

            # Get path to .obj file
            obj_file = str(self.meshfile_from_structure(region))

            # check which color to assign to the brain region
            if use_original_color:
                color = [
                    x / 255
                    for x in self._get_from_structure(region, "rgb_triplet")
                ]
            else:
                if colors is None:
                    color = brainrender.DEFAULT_STRUCTURE_COLOR
                elif isinstance(colors, list):
                    color = colors[i]
                else:
                    color = colors

            # Load the object file as a mesh and store the actor
            if hemisphere is not None:
                if (hemisphere.lower() == "left"
                        or hemisphere.lower() == "right"):
                    obj = self.get_region_unilateral(region,
                                                     hemisphere=hemisphere,
                                                     color=color,
                                                     alpha=alpha)
                else:
                    raise ValueError(
                        f"Invalid hemisphere argument: {hemisphere}")
            else:
                obj = load_mesh_from_file(obj_file, color=color, alpha=alpha)

            if obj is not None:
                actors_funcs.edit_actor(obj, **kwargs)
                actors[region] = obj
            else:
                print(
                    f"Something went wrong while loading mesh data for {region}, skipping."
                )

        return actors
Example #4
0
    def get_brain_regions(self, brain_regions, VIP_regions=None, VIP_color=None,
                        add_labels=False,
                        colors=None, use_original_color=True, 
                        alpha=None, hemisphere=None, verbose=False, **kwargs):

        """
            Gets brain regions meshes for rendering
            Many parameters can be passed to specify how the regions should be rendered.
            To treat a subset of the rendered regions, specify which regions are VIP. 
            Use the kwargs to specify more detailes on how the regins should be rendered (e.g. wireframe look)

            :param brain_regions: str list of acronyms of brain regions
            :param VIP_regions: if a list of brain regions are passed, these are rendered differently compared to those in brain_regions (Default value = None)
            :param VIP_color: if passed, this color is used for the VIP regions (Default value = None)
            :param colors: str, color of rendered brian regions (Default value = None)
            :param use_original_color: bool, if True, the allen's default color for the region is used.  (Default value = False)
            :param alpha: float, transparency of the rendered brain regions (Default value = None)
            :param hemisphere: str (Default value = None)
            :param add_labels: bool (default False). If true a label is added to each regions' actor. The label is visible when hovering the mouse over the actor
            :param **kwargs: used to determine a bunch of thigs, including the look and location of lables from scene.add_labels
        """
        # Check that the atlas has brain regions data
        if self.region_acronyms is None:
            print(f"The atlas {self.atlas_name} has no brain regions data")
            return

        # Parse arguments
        if VIP_regions is None:
            VIP_regions = brainrender.DEFAULT_VIP_REGIONS
        if VIP_color is None:
            VIP_color = brainrender.DEFAULT_VIP_COLOR
        if alpha is None:
            _alpha = brainrender.DEFAULT_STRUCTURE_ALPHA
        else: _alpha = alpha

        # check that we have a list
        if not isinstance(brain_regions, list):
            brain_regions = [brain_regions]

        # check the colors input is correct
        if colors is not None:
            if isinstance(colors[0], (list, tuple)):
                if not len(colors) == len(brain_regions): 
                    raise ValueError("when passing colors as a list, the number of colors must match the number of brain regions")
                for col in colors:
                    if not check_colors(col): raise ValueError("Invalide colors in input: {}".format(col))
            else:
                if not check_colors(colors): raise ValueError("Invalide colors in input: {}".format(colors))
                colors = [colors for i in range(len(brain_regions))]

        # loop over all brain regions
        actors = {}
        for i, region in enumerate(brain_regions):
            self._check_valid_region_arg(region)

            if region in self.ignore_regions: continue
            if verbose: print("Rendering: ({})".format(region))

            # get the structure and check if we need to download the object file
            if region not in self.region_acronyms:
                print(f"The region {region} doesn't seem to belong to the atlas being used: {self.atlas_name}. Skipping")
                continue

            obj_file = os.path.join(self.meshes_folder, "{}.{}".format(region, self.mesh_format))
            if not self._check_obj_file(region, obj_file):
                print("Could not render {}, maybe we couldn't get the mesh?".format(region))
                continue

            # check which color to assign to the brain region
            if use_original_color:
                color = [x/255 for x in self.get_region_color(region)]
            else:
                if region in VIP_regions:
                    color = VIP_color
                else:
                    if colors is None:
                        color = brainrender.DEFAULT_STRUCTURE_COLOR
                    elif isinstance(colors, list):
                        color = colors[i]
                    else: 
                        color = colors

            if region in VIP_regions:
                alpha = 1
            else:
                alpha = _alpha

            # Load the object file as a mesh and store the actor
            if hemisphere is not None:
                if hemisphere.lower() == "left" or hemisphere.lower() == "right":
                    obj = self.get_region_unilateral(region, hemisphere=hemisphere, color=color, alpha=alpha)
                else:
                    raise ValueError(f'Invalid hemisphere argument: {hemisphere}')
            else:
                obj = load(obj_file, c=color, alpha=alpha)

            if obj is not None:
                actors_funcs.edit_actor(obj, **kwargs)

                actors[region] = obj
            else:
                print(f"Something went wrong while loading mesh data for {region}")

        return actors