Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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()
"""
N = len(neurons)
colors = dict(soma=None, axon=None, dendrites=None,)
# If no color is passed, get random colors
if color is None:
cols = [self.default_neuron_color for n in np.arange(N)]
colors = dict(
soma=cols.copy(), axon=cols.copy(), dendrites=cols.copy(),
)
else:
if isinstance(color, str):
# Deal with a a cmap being passed
if color in _mapscales_cmaps:
cols = [
colorMap(n, name=color, vmin=-2, vmax=N + 2)
for n in np.arange(N)
]
colors = dict(
soma=cols.copy(),
axon=cols.copy(),
dendrites=cols.copy(),
)
else:
# Deal with a single color being passed
cols = [getColor(color) for n in np.arange(N)]
colors = dict(
soma=cols.copy(),
axon=cols.copy(),
dendrites=cols.copy(),
)
"""
Modify neurons actors after they have been created, at render time.
neurons should be a list of dictionaries with soma, dendrite and axon actors of each neuron.
:param neurons: list of dictionaries with vtk actors for each neuron
:param **kwargs:
"""
soma_color, axon_color, dendrites_color = None, None, None
for neuron in neurons:
if "random_color" in kwargs:
if kwargs["random_color"]:
if not isinstance(kwargs["random_color"], str):
color = get_random_colors(n_colors=1)
else: # random_color is a colormap
color = colorMap(np.random.randint(1000), name=kwargs["random_color"], vmin=0, vmax=1000)
axon_color = soma_color = dendrites_color = color
elif "color_neurites" in kwargs:
soma_color = neuron["soma"].color()
if not kwargs["color_neurites"]:
axon_color = dendrites_color = soma_color
else:
if not "axon_color" in kwargs:
# print("no axon color provided, using somacolor")
axon_color = soma_color
else:
axon_color = kwargs["axon_color"]
if not "dendrites_color" in kwargs:
# print("no dendrites color provided, using somacolor")
dendrites_color = soma_color
else:
N = len(neurons)
colors = dict(soma=None, axon=None, dendrites=None,)
# If no color is passed, get random colors
if color is None:
cols = get_random_colors(N)
if not isinstance(cols, list):
cols = [cols]
colors = dict(soma=cols, axon=cols, dendrites=cols,)
else:
if isinstance(color, str):
# Deal with a a cmap being passed
if color in _mapscales_cmaps:
cols = [
colorMap(n, name=color, vmin=-2, vmax=N + 2)
for n in np.arange(N)
]
colors = dict(
soma=cols.copy(), axon=cols.copy(), dendrites=cols.copy(),
)
else:
# Deal with a single color being passed
cols = [getColor(color) for n in np.arange(N)]
colors = dict(
soma=cols.copy(), axon=cols.copy(), dendrites=cols.copy(),
)
elif isinstance(color, dict):
# Deal with a dictionary with color for each component
if "soma" not in color.keys():
raise ValueError(
"""
Makes sure that all the parameters to specify how neurons should be rendered.
:param neuron_number: number of the neuron being rendered
:param neuron: neuron's metadata (Default value = None)
:param soma_region: str with the acronym of the region the soma is in (Default value = None)
:param soma: list with XYZ coordinates of the neuron's soma. (Default value = None)
"""
# Define colors of different components
if not self.color_by_region:
if self.random_color:
if not isinstance(self.random_color, str):
color = get_random_colors(n_colors=1)
else: # random_color is a colormap
color = colorMap(neuron_number, name=self.random_color, vmin=0, vmax=self.n_neurons)
axon_color = soma_color = dendrites_color = color
else:
if self.soma_color is None:
soma_color = get_random_colors(n_colors=1)
if not self.color_neurites:
axon_color = dendrites_color = soma_color = self.soma_color
else:
soma_color = self.soma_color
if self.axon_color is None:
axon_color = soma_color
else:
axon_color = self.axon_color
if self.dendrites_color is None:
dendrites_color = soma_color
else:
Modify neurons actors after they have been created, at render time.
neurons should be a list of dictionaries with soma, dendrite and axon actors of each neuron.
:param neurons: list of dictionaries with vtk actors for each neuron
:param **kwargs:
"""
if not isinstance(neurons, (list, tuple)):
neurons = [neurons]
soma_color, axon_color, dendrites_color = None, None, None
for neuron in neurons:
if "random_color" in kwargs:
if kwargs["random_color"]:
if not isinstance(kwargs["random_color"], str):
color = get_random_colors(n_colors=1)
else: # random_color is a colormap
color = colorMap(
np.random.randint(1000),
name=kwargs["random_color"],
vmin=0,
vmax=1000,
)
axon_color = soma_color = dendrites_color = color
elif "color_neurites" in kwargs:
soma_color = neuron["soma"].color()
if not kwargs["color_neurites"]:
axon_color = dendrites_color = soma_color
else:
if "axon_color" not in kwargs:
# print("no axon color provided, using somacolor")
axon_color = soma_color
else:
axon_color = kwargs["axon_color"]
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(scene.actors["neurons"], 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)