Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for idx, bp in branching_points.iteritems():
# get neurites after the branching point
bp = neurites.loc[neurites.sampleNumber == idx]
post_bp = neurites.loc[neurites.parentNumber == idx]
parent = neurites.loc[neurites.sampleNumber == bp.parentNumber.values[0]]
# loop on each branch after the branching point
for bi, branch in post_bp.iterrows():
# Start coordinates in a list, including parent and branch point
if len(parent):
branch_points = [get_coords(parent, mirror=self.mirror_coord, mirror_ax=self.mirror_ax)]
else:
branch_points = []
branch_points.extend([get_coords(bp, mirror=self.mirror_coord, mirror_ax=self.mirror_ax),
get_coords(branch, mirror=self.mirror_coord, mirror_ax=self.mirror_ax)])
# loop over all following points along the branch, until you meet either a terminal or another branching point. store the points
idx = branch.sampleNumber
while True:
nxt = neurites.loc[neurites.parentNumber == idx]
if len(nxt) != 1:
break
else:
branch_points.append(get_coords(nxt, mirror=self.mirror_coord, mirror_ax=self.mirror_ax))
idx = nxt.sampleNumber.values[0]
# if the branch is too short for a tube, create a sphere instead
if len(branch_points) < 2: # plot either a line between two branch_points or a spheere
actors.append(shapes.Sphere(branch_points[0], c="g", r=100))
continue
branching_points = parent_counts.loc[parent_counts > 1]
# loop over each branching point
actors = []
for idx, bp in branching_points.iteritems():
# get neurites after the branching point
bp = neurites.loc[neurites.sampleNumber == idx]
post_bp = neurites.loc[neurites.parentNumber == idx]
parent = neurites.loc[neurites.sampleNumber == bp.parentNumber.values[0]]
# loop on each branch after the branching point
for bi, branch in post_bp.iterrows():
# Start coordinates in a list, including parent and branch point
if len(parent):
branch_points = [get_coords(parent, mirror=self.mirror_coord, mirror_ax=self.mirror_ax)]
else:
branch_points = []
branch_points.extend([get_coords(bp, mirror=self.mirror_coord, mirror_ax=self.mirror_ax),
get_coords(branch, mirror=self.mirror_coord, mirror_ax=self.mirror_ax)])
# loop over all following points along the branch, until you meet either a terminal or another branching point. store the points
idx = branch.sampleNumber
while True:
nxt = neurites.loc[neurites.parentNumber == idx]
if len(nxt) != 1:
break
else:
branch_points.append(get_coords(nxt, mirror=self.mirror_coord, mirror_ax=self.mirror_ax))
idx = nxt.sampleNumber.values[0]
# if the branch is too short for a tube, create a sphere instead
neuron_actors = self._load_cached_neuron(neuron_name, params)
if neuron_actors is not None:
# Color the loaded neuron
for component, color in zip(['soma', 'dendrites', 'axon'], [soma_color, dendrites_color, axon_color]):
if component in list(neuron_actors.keys()):
if color is not None:
if len(color) != 1 and len(color) != 3:
color = color[neuron_number]
neuron_actors[component].color(color)
return neuron_actors, {'soma':soma_region, 'dendrites':None, 'axons':None}
if not USE_MORPHOLOGY_CACHE or neuron_actors is None:
print("Parsing neuron: " + neuron_name)
neuron_actors = {}
self.soma_coords = get_coords(neuron["soma"], mirror=self.mirror_coord, mirror_ax=self.mirror_ax)
neuron_actors['soma'] = shapes.Sphere(pos=self.soma_coords, c=soma_color, r=SOMA_RADIUS)
# Draw dendrites and axons
neuron_actors['dendrites'], dendrites_regions = [], None
neuron_actors['axon'], axon_regions = [], None
if self.render_neurites:
if self.is_json:
if self.render_dendrites:
neuron_actors['dendrites'], dendrites_regions = self.neurites_parser(pd.DataFrame(neuron["dendrite"]), dendrites_color)
if self.render_axons:
neuron_actors['axon'], axon_regions = self.neurites_parser(pd.DataFrame(neuron["axon"]), axon_color)
else:
if self.render_dendrites:
neuron_actors['dendrites'], dendrites_regions = self.neurites_parser_swc(pd.DataFrame(neuron["dendrite"]), dendrites_color)
if self.render_axons:
neuron_actors['axon'], axon_regions = self.neurites_parser_swc(pd.DataFrame(neuron["axon"]), axon_color)
if isinstance(soma_color, list):
if isinstance(soma_color[0], str) or isinstance(soma_color[0], list):
soma_color = soma_color[neuron_number]
if isinstance(dendrites_color, list):
if isinstance(dendrites_color[0], str) or isinstance(dendrites_color[0], list):
dendrites_color = dendrites_color[neuron_number]
if isinstance(axon_color, list):
if isinstance(axon_color[0], str) or isinstance(axon_color[0], list):
axon_color = axon_color[neuron_number]
# get allen info: it containes the allenID of each brain region
# each sample has the corresponding allen ID so we can recontruct in which brain region it is
if neuron is not None:
if isinstance(neuron, dict):
self.alleninfo = None
soma_region = self.scene.get_structure_from_coordinates(get_coords(neuron['soma']))
else:
self.alleninfo = None
soma_region = None
elif soma_region is None:
self.alleninfo = None
if soma is not None:
soma_region = self.scene.get_structure_from_coordinates(get_coords(soma))
else:
raise ValueError("You need to pass either a neuron, or a soma region or a soma")
else:
self.alleninfo = None
if soma_region is not None:
soma_region = self.scene.get_structure_parent(soma_region)['acronym']
else:
soma_region = "root"
if isinstance(axon_color[0], str) or isinstance(axon_color[0], list):
axon_color = axon_color[neuron_number]
# get allen info: it containes the allenID of each brain region
# each sample has the corresponding allen ID so we can recontruct in which brain region it is
if neuron is not None:
if isinstance(neuron, dict):
self.alleninfo = None
soma_region = self.scene.get_structure_from_coordinates(get_coords(neuron['soma']))
else:
self.alleninfo = None
soma_region = None
elif soma_region is None:
self.alleninfo = None
if soma is not None:
soma_region = self.scene.get_structure_from_coordinates(get_coords(soma))
else:
raise ValueError("You need to pass either a neuron, or a soma region or a soma")
else:
self.alleninfo = None
if soma_region is not None:
soma_region = self.scene.get_structure_parent(soma_region)['acronym']
else:
soma_region = "root"
if self.color_by_region:
try:
region_color = self.scene.structure_tree.get_structures_by_acronym([soma_region])[0]['rgb_triplet']
except:
print("could not find default color for region: {}. Using random color instead".format(soma_region))
region_color = get_random_colors(n_colors=1)
def neurites_parser_swc(self, neurites, color):
"""
Parses neuron's neurites when the data are provided as .swc
:param neurites: datafarme with neurites samples
:param color: color for vtk actor
"""
coords = [self.soma_coords]
coords.extend([get_coords(sample, mirror=self.mirror_coord, mirror_ax=self.mirror_ax) for i, sample in neurites.iterrows()])
lines = shapes.Spheres(coords, r=38, c=color, res=4)
regions = []
return lines, regions
# Start coordinates in a list, including parent and branch point
if len(parent):
branch_points = [get_coords(parent, mirror=self.mirror_coord, mirror_ax=self.mirror_ax)]
else:
branch_points = []
branch_points.extend([get_coords(bp, mirror=self.mirror_coord, mirror_ax=self.mirror_ax),
get_coords(branch, mirror=self.mirror_coord, mirror_ax=self.mirror_ax)])
# loop over all following points along the branch, until you meet either a terminal or another branching point. store the points
idx = branch.sampleNumber
while True:
nxt = neurites.loc[neurites.parentNumber == idx]
if len(nxt) != 1:
break
else:
branch_points.append(get_coords(nxt, mirror=self.mirror_coord, mirror_ax=self.mirror_ax))
idx = nxt.sampleNumber.values[0]
# if the branch is too short for a tube, create a sphere instead
if len(branch_points) < 2: # plot either a line between two branch_points or a spheere
actors.append(shapes.Sphere(branch_points[0], c="g", r=100))
continue
# create tube actor
actors.append(shapes.Tube(branch_points, r=neurite_radius, c=color, alpha=1, res=NEURON_RESOLUTION))
# merge actors' meshes to make rendering faster
merged = merge(*actors)
if merged is None:
return None, None
merged.color(color)