Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various (small) improvements #291

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions brainrender/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def make_actor_label(
default_offset = np.array([0, -200, 100])

new_actors = []
for n, (actor, label) in enumerate(zip(listify(actors), listify(labels))):
for _, (actor, label) in enumerate(zip(listify(actors), listify(labels))):
# Get label color
if color is None:
color = [0.2, 0.2, 0.2]
Expand Down Expand Up @@ -74,7 +74,7 @@ def make_actor_label(
return new_actors


class Actor(object):
class Actor:
_needs_label = False # needs to make a label
_needs_silhouette = False # needs to make a silhouette
_is_transformed = False # has been transformed to correct axes orientation
Expand Down
15 changes: 10 additions & 5 deletions brainrender/actors/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ def __init__(

def _from_numpy(self, griddata, voxel_size, color, **volume_kwargs):
"""
Creates a vedo.Volume actor from a 3D numpy array
with volume data
Creates a vedo.Volume actor from a 3D numpy array with volume data.
"""

return VedoVolume(
vvol = VedoVolume(
griddata,
spacing=[voxel_size, voxel_size, voxel_size],
**volume_kwargs,
).cmap(color)
)
vvol.cmap(color)
# THIS IS DONE IN render.py
alessandrofelder marked this conversation as resolved.
Show resolved Hide resolved
# Flip volume so that it's oriented as in the atlas
# vvol.permute_axes(2, 1, 0)
# mtx = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]
# vvol.apply_transform(mtx)
return vvol

def _from_file(self, filepath, voxel_size, color, **volume_kwargs):
"""
Expand Down
23 changes: 11 additions & 12 deletions brainrender/camera.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import vtk
from loguru import logger
from vtkmodules.vtkRenderingCore import vtkCamera

from brainrender.cameras import cameras

Expand All @@ -24,14 +24,14 @@ def check_camera_param(camera):
if isinstance(camera, str):
return get_camera(camera)
else:
params = ["pos", "viewup", "clippingRange"]
params = ["pos", "viewup", "clipping_range"]
for param in params:
if param not in list(camera.keys()):
raise ValueError(
f"Camera parameters dict should include the following keys: {params}, missing: {param}"
)
if "focalPoint" not in camera.keys():
camera["focalPoint"] = None
if "focal_point" not in camera.keys():
camera["focal_point"] = None
return camera


Expand All @@ -45,10 +45,10 @@ def set_camera_params(camera, params):
# Apply camera parameters
camera.SetPosition(params["pos"])
camera.SetViewUp(params["viewup"])
camera.SetClippingRange(params["clippingRange"])
camera.SetClippingRange(params["clipping_range"])

if "focalPoint" in params.keys() and params["focalPoint"] is not None:
camera.SetFocalPoint(params["focalPoint"])
if "focal_point" in params.keys() and params["focal_point"] is not None:
camera.SetFocalPoint(params["focal_point"])
if "distance" in params.keys():
camera.SetDistance(params["distance"])

Expand All @@ -60,12 +60,11 @@ def set_camera(scene, camera):
:param scene: instance of Scene
:param camera: either a string with the name of one of the pre-defined cameras, or
a dictionary of camera parameters.

"""
if camera is None:
return
return None

if not isinstance(camera, vtk.vtkCamera):
if not isinstance(camera, vtkCamera):
# Get camera params
camera = check_camera_param(camera)

Expand Down Expand Up @@ -103,10 +102,10 @@ def clean(val):

params = dict(
pos=clean(cam.GetPosition()),
focalPoint=clean(cam.GetFocalPoint()),
focal_point=clean(cam.GetFocalPoint()),
viewup=clean(cam.GetViewUp()),
distance=clean(cam.GetDistance()),
clippingRange=clean(cam.GetClippingRange()),
clipping_range=clean(cam.GetClippingRange()),
# orientation=clean(cam.GetOrientation()),
)
return params
12 changes: 6 additions & 6 deletions brainrender/cameras.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
sagittal_camera = {
"pos": (6514, -34, 36854),
"viewup": (0, -1, 0),
"clippingRange": (24098, 49971),
"clipping_range": (24098, 49971),
}

sagittal_camera2 = {
"pos": (9782, 1795, -40999),
"viewup": (0, -1, 0),
"clippingRange": (23256, 51031),
"clipping_range": (23256, 51031),
}


frontal_camera = {
"pos": (-19199, -1428, -5763),
"viewup": (0, -1, 0),
"clippingRange": (19531, 40903),
"clipping_range": (19531, 40903),
}

top_camera = {
"pos": (7760, -31645, -5943),
"viewup": (-1, 0, 0),
"clippingRange": (27262, 45988),
"clipping_range": (27262, 45988),
}

top_side_camera = {
"pos": (4405, -31597, -5411),
"viewup": (0, 0, -1),
"clippingRange": (26892, 46454),
"clipping_range": (26892, 46454),
}

three_quarters_camera = {
"pos": (-20169, -7298, 14832),
"viewup": (0, -1, 0),
"clippingRange": (16955, 58963),
"clipping_range": (16955, 58963),
}

cameras = dict(
Expand Down
21 changes: 11 additions & 10 deletions brainrender/render.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
from loguru import logger
from myterial import amber, deep_purple_light, orange, teal
Expand Down Expand Up @@ -55,7 +54,7 @@ def _get_plotter(self):
title="brainrender",
bg=settings.BACKGROUND_COLOR,
offscreen=settings.OFFSCREEN,
size="full" if settings.WHOLE_SCREEN else "auto",
size="full" if settings.WHOLE_SCREEN else (1600, 1200),
)

self.plotter.keyPressFunction = self.keypress
Expand Down Expand Up @@ -134,7 +133,10 @@ def _prepare_actor(self, actor):

if isinstance(actor._mesh, VedoVolume):
actor._mesh.permute_axes(2, 1, 0)
actor._mesh.apply_transform(mtx, True)
actor._mesh.apply_transform(mtx)
actor._mesh.transform = (
None # otherwise it gets applied twice
)
elif actor.br_class in ["None", "Gene Data"]:
actor._mesh.apply_transform(mtx_swap_x_z)
actor._mesh.apply_transform(mtx)
Expand All @@ -153,7 +155,6 @@ def _prepare_actor(self, actor):
logger.debug(
f'Failed to reverse actor: "{actor.name} (type: {actor.br_class})"'
)
pass
actor._is_transformed = True

# Add silhouette and labels
Expand Down Expand Up @@ -224,8 +225,8 @@ def render(
else:
camera = check_camera_param(camera)

if "focalPoint" not in camera.keys() or camera["focalPoint"] is None:
camera["focalPoint"] = self.root._mesh.center_of_mass()
if "focal_point" not in camera.keys() or camera["focal_point"] is None:
camera["focal_point"] = self.root._mesh.center_of_mass()

if not self.backend and camera is not None:
camera = set_camera(self, camera)
Expand Down Expand Up @@ -290,7 +291,7 @@ def render(
)

def close(self):
plt.close()
self.plotter.close()

def export(self, savepath):
"""
Expand Down Expand Up @@ -362,7 +363,7 @@ def screenshot(self, name=None, scale=None):
def _print_camera(self):
pms = get_camera_params(scene=self)

focal = pms.pop("focalPoint", None)
focal = pms.pop("focal_point", None)
dst = pms.pop("distance", None)

names = [
Expand All @@ -375,7 +376,7 @@ def _print_camera(self):
*names,
f"[{orange}] }}",
f"[{deep_purple_light}]Additional, (optional) parameters:",
f"[green bold] 'focalPoint'[/green bold]: [{amber}]{focal},",
f"[green bold] 'focal_point'[/green bold]: [{amber}]{focal},",
f"[green bold] 'distance'[/green bold]: [{amber}]{dst},",
sep="\n",
)
Expand All @@ -390,7 +391,7 @@ def keypress(self, key): # pragma: no cover
if key == "s":
self.screenshot()

elif key == "q" or key == "Esc":
elif key in ("q", "Esc"):
self.close()

elif key == "c":
Expand Down
8 changes: 3 additions & 5 deletions brainrender/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def __init__(
"root",
alpha=settings.ROOT_ALPHA,
color=settings.ROOT_COLOR,
silhouette=True
if root and settings.SHADER_STYLE == "cartoon"
else False,
silhouette=bool(root and settings.SHADER_STYLE == "cartoon"),
)
self.atlas.root = self.root # give atlas access to root
self._root_mesh = self.root.mesh.clone()
Expand Down Expand Up @@ -108,7 +106,7 @@ def _get_inset(self):
Creates a small inset showing the brain's orientation
"""
if settings.OFFSCREEN:
return None
return

inset = self._root_mesh.clone()
inset.alpha(1) # scale(0.5)
Expand Down Expand Up @@ -401,7 +399,7 @@ def content(self):
f"[bold][{amber}]- {act.name}[/bold][{orange_darker}] (type: [{orange}]{act.br_class}[/{orange}])"
)

if "win32" != sys.platform:
if sys.platform != "win32":
actors.print()
else:
print(pi.utils.stringify(actors, maxlen=-1))
Expand Down
4 changes: 1 addition & 3 deletions brainrender/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
vsettings.occlusion_ratio = 0.1
vsettings.multi_samples = 0 if sys.platform == "darwin" else 8

# vsettings.useSSAO = True

# For transparent background with screenshots
vsettings.screenshot_transparent_background = False # vedo for transparent bg
vsettings.use_fxaa = False
Expand All @@ -36,5 +34,5 @@
SCREENSHOT_SCALE = 1
SHADER_STYLE = "cartoon" # affects the look of rendered brain regions: [metallic, plastic, shiny, glossy]
SHOW_AXES = True
WHOLE_SCREEN = True # If true render window is full screen
WHOLE_SCREEN = False # If true render window is full screen
OFFSCREEN = False
4 changes: 4 additions & 0 deletions brainrender/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def __init__(self, scene, save_fld, name, fmt="mp4", size="1620x1050"):
self.keyframes[0] = dict( # make sure first frame is a keyframe
zoom=None, camera=None, callback=None
)
self.keyframes_numbers = 0
self.nframes = 0
self.last_keyframe = 0
self.segment_fact = 0

def add_keyframe(
self,
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
custom_camera = {
"pos": (41381, -16104, 27222),
"viewup": (0, -1, 0),
"clippingRange": (31983, 76783),
"clipping_range": (31983, 76783),
}


Expand Down
6 changes: 3 additions & 3 deletions examples/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Explicitly initialise a scene with the screenshot folder set
# If the screenshot folder is not set, by default screenshots
# Will save to the current working directory
screenshot_folder = "./examples/screenshots"
screenshot_folder = "."
scene = Scene(
title=f"Screenshots will be saved to {screenshot_folder}",
inset=True,
Expand All @@ -28,8 +28,8 @@
camera = {
"pos": (8777, 1878, -44032),
"viewup": (0, -1, 0),
"clippingRange": (24852, 54844),
"focalPoint": (7718, 4290, -3507),
"clipping_range": (24852, 54844),
"focal_point": (7718, 4290, -3507),
"distance": 40610,
}
zoom = 2.5
Expand Down
2 changes: 1 addition & 1 deletion examples/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def make_frame(scene, frame_number, *args, **kwargs):
# Now make a video with our custom function
scene = Scene("my video2")
scene.add_brain_region("TH")
vm = VideoMaker(scene, "./examples", "vid2", make_frame_func=make_frame)
vm = VideoMaker(scene, ".", "vid2", make_frame_func=make_frame)
vm.make_video(duration=1, fps=15)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies = [
"msgpack",
"pyyaml>=5.3",
"pooch",
"morphapi>=0.1.3.0",
"morphapi>=0.2",
alessandrofelder marked this conversation as resolved.
Show resolved Hide resolved
"requests",
"bg-atlasapi>=1.0.0",
"tables",
Expand Down
5 changes: 1 addition & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,11 @@ def test_animation(scene, pytestconfig):

anim.make_video(duration=5, fps=15)

scene.render(interactive=False)
scene.close()

vid_path = Path(root_path / "tests" / "examples" / "vid3.mp4")

assert vid_path.exists()
vid_path.unlink()
Path.rmdir(vid_directory)
assert not vid_path.exists()


def test_adding_multiple_brain_regions(scene):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_scene_render():
-0.9965615097647067,
-0.08270172139591858,
),
clippingRange=(30461.81976236306, 58824.38622122339),
clipping_range=(30461.81976236306, 58824.38622122339),
),
)

Expand Down
Loading