Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (
not isinstance(data, np.ndarray)
or not isinstance(data2, np.ndarray)
or not isinstance(data3, np.ndarray)
):
raise ValueError
if not np.array_equal(data, data3):
raise ValueError
gene_actor = geapi.griddata_to_volume(
data, min_quantile=90, cmap="inferno"
)
gene_actor2 = geapi.griddata_to_volume(data2, min_value=0.2)
if not isinstance(gene_actor, Mesh) or not isinstance(gene_actor2, Mesh):
raise ValueError
Get the center of mass of the 3d mesh of one or multiple brain regions.
:param regions: str, list of brain regions acronyms
:param unilateral: bool, if True, the CoM is relative to one hemisphere (Default value = True)
:param hemisphere: str, if unilteral=True, specifies which hemisphere to use ['left' or 'right'] (Default value = "right")
:returns: coms = {list, dict} -- [if only one regions is passed, then just returns the CoM coordinates for that region.
If a list is passed then a dictionary is returned. ]
"""
if not isinstance(regions, list):
regions = [regions]
coms = {}
for region in regions:
# Check if input is an actor or if we need to load one
if isinstance(region, Mesh):
mesh = region
else:
# load mesh corresponding to brain region
if unilateral:
mesh = self.get_region_unilateral(
region, hemisphere="left"
)
else:
mesh = self._get_structure_mesh(region)
com = mesh.centerOfMass()
# if using right hemisphere, mirror COM
if unilateral and hemisphere.lower() == "right":
com = self.mirror_point_across_hemispheres(com)
coms[region] = com
def get_n_random_points_in_region(atlas, region, N, hemisphere=None):
"""
Gets N random points inside (or on the surface) of the mesh defining a brain region.
:param region: str, acronym of the brain region.
:param N: int, number of points to return.
"""
if isinstance(region, Mesh):
region_mesh = region
else:
if hemisphere is None:
region_mesh = atlas._get_structure_mesh(region)
else:
region_mesh = atlas.get_region_unilateral(
region, hemisphere=hemisphere
)
if region_mesh is None:
return None
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)
swcfile=neuron,
neurite_radius=neurite_radius,
soma_radius=soma_radius,
use_cache=use_cache,
)
else:
raise NotImplementedError(
"Currently we can only parse morphological reconstructions from swc files"
)
else:
raise ValueError(
f"Passed neruon {neuron} is not a valid input. Maybe the file doesn't exist?"
)
# Deal with neuron as single actor
elif isinstance(neuron, Mesh):
# A single actor was passed, maybe it's the entire neuron
neuron_actors["soma"] = neuron # store it as soma
pass
# Deal with neuron as dictionary of actor
elif isinstance(neuron, dict):
neuron_actors["soma"] = neuron.pop("soma", None)
neuron_actors["axon"] = neuron.pop("axon", None)
# Get dendrites actors
if (
"apical_dendrites" in neuron.keys()
or "basal_dendrites" in neuron.keys()
):
if "apical_dendrites" not in neuron.keys():
neuron_actors["dendrites"] = neuron["basal_dendrites"]
elif isinstance(neuron, Neuron):
neuron_actors, _ = get_neuron_actors_with_morphapi(
neuron=neuron,
neurite_radius=neurite_radius,
use_cache=use_cache,
)
# Deal with other inputs
else:
raise ValueError(
f"Passed neuron {neuron} is not a valid input"
)
# Check that we don't have anything weird in neuron_actors
for key, act in neuron_actors.items():
if act is not None:
if not isinstance(act, Mesh):
raise ValueError(
f"Neuron actor {key} is {type(act)} but should be a vedo Mesh. Not: {act}"
)
if not display_axon:
neuron_actors["axon"] = None
if not display_dendrites:
neuron_actors["dendrites"] = None
_neurons_actors.append(neuron_actors)
# Color actors
colors = parse_neurons_colors(neurons, color)
for n, neuron in enumerate(_neurons_actors):
if neuron["axon"] is not None:
neuron["axon"].c(colors["axon"][n])
neuron["axon"].name = "neuron-axon"
Get the center of mass of the 3d mesh of one or multiple brain regions.
:param regions: str, list of brain regions acronyms
:param unilateral: bool, if True, the CoM is relative to one hemisphere (Default value = True)
:param hemisphere: str, if unilteral=True, specifies which hemisphere to use ['left' or 'right'] (Default value = "right")
:returns: coms = {list, dict} -- [if only one regions is passed, then just returns the CoM coordinates for that region.
If a list is passed then a dictionary is returned. ]
"""
if not isinstance(regions, list):
regions = [regions]
coms = {}
for region in regions:
# Check if input is an actor or if we need to load one
if isinstance(region, Mesh):
mesh = region
else:
# load mesh corresponding to brain region
if unilateral:
mesh = self.get_region_unilateral(
region, hemisphere="left"
)
else:
mesh = self._get_structure_mesh(region)
com = mesh.centerOfMass()
# if using right hemisphere, mirror COM
if unilateral and hemisphere.lower() == "right":
com = self.mirror_point_across_hemispheres(com)
coms[region] = com
swcfile=neuron,
neurite_radius=neurite_radius,
soma_radius=soma_radius,
use_cache=use_cache,
)
else:
raise NotImplementedError(
"Currently we can only parse morphological reconstructions from swc files"
)
else:
raise ValueError(
f"Passed neruon {neuron} is not a valid input. Maybe the file doesn't exist?"
)
# Deal with neuron as single actor
elif isinstance(neuron, Mesh):
# A single actor was passed, maybe it's the entire neuron
neuron_actors["soma"] = neuron # store it as soma
pass
# Deal with neuron as dictionary of actor
elif isinstance(neuron, dict):
neuron_actors["soma"] = neuron.pop("soma", None)
neuron_actors["axon"] = neuron.pop("axon", None)
# Get dendrites actors
if (
"apical_dendrites" in neuron.keys()
or "basal_dendrites" in neuron.keys()
):
if "apical_dendrites" not in neuron.keys():
neuron_actors["dendrites"] = neuron["basal_dendrites"]
def add_brain_regions(self, *args, **kwargs):
"""
Adds brain regions meshes to scene.
Check the atlas' method to know how it works
"""
add_labels = kwargs.pop("add_labels", False)
allactors = self.atlas.get_brain_regions(
*args, verbose=self.verbose, **kwargs
)
actors = []
for region, actor in allactors.items():
if region in [a.name for a in self.actors if isinstance(a, Mesh)]:
# Avoid inserting again
continue
if add_labels:
self.add_actor_label(actor, region, **kwargs)
actor.name = region
actors.append(actor)
self.actors.extend(actors)
return return_list_smart(actors)
# Check args
if not isinstance(actors, (tuple, list)):
actors = [actors]
if not isinstance(labels, (tuple, list)):
labels = [labels]
if atlas.atlas_name == "ABA":
offset = [-yoffset, -zoffset, xoffset]
default_offset = np.array([0, -200, 100])
else:
offset = [xoffset, yoffset, zoffset]
default_offset = np.array([100, 0, -200])
new_actors = []
for n, (actor, label) in enumerate(zip(actors, labels)):
if not isinstance(actor, Mesh):
raise ValueError(
f"Mesh must be an instance of Mesh, not {type(actor)}"
)
if not isinstance(label, str):
raise ValueError(f"Label must be a string, not {type(label)}")
# Get label color
if color is None:
color = actor.c()
elif isinstance(color, (list, tuple)):
color = color[n]
# Get mesh's highest point
points = actor.points().copy()
point = points[np.argmin(points[:, 1]), :]
point += np.array(offset) + default_offset