From 4381272d1283a06a1524b7f4c0e8054e89cb73df Mon Sep 17 00:00:00 2001 From: Guillaume Favelier Date: Wed, 25 Nov 2020 11:16:35 +0100 Subject: [PATCH 1/4] Add reset camera parameter --- mne/viz/_brain/_brain.py | 6 +++--- mne/viz/backends/_pysurfer_mayavi.py | 3 ++- mne/viz/backends/_pyvista.py | 11 +++++++---- mne/viz/backends/base_renderer.py | 6 +++++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/mne/viz/_brain/_brain.py b/mne/viz/_brain/_brain.py index ede259cdc03..26e1e548b58 100644 --- a/mne/viz/_brain/_brain.py +++ b/mne/viz/_brain/_brain.py @@ -2199,8 +2199,7 @@ def show_view(self, view=None, roll=None, distance=None, row=0, col=0, if distance is not None: view.update(distance=distance) self._renderer.subplot(row, col) - self._renderer.set_camera(**view) - self._renderer.reset_camera() + self._renderer.set_camera(**view, reset_camera=False) self._update() def reset_view(self): @@ -2208,7 +2207,8 @@ def reset_view(self): for h in self._hemis: for ri, ci, v in self._iter_views(h): self._renderer.subplot(ri, ci) - self._renderer.set_camera(**views_dicts[h][v]) + self._renderer.set_camera(**views_dicts[h][v], + reset_camera=False) def save_image(self, filename, mode='rgb'): """Save view from all panels to disk. diff --git a/mne/viz/backends/_pysurfer_mayavi.py b/mne/viz/backends/_pysurfer_mayavi.py index 5077a81dee5..0f7c53157ec 100644 --- a/mne/viz/backends/_pysurfer_mayavi.py +++ b/mne/viz/backends/_pysurfer_mayavi.py @@ -450,7 +450,8 @@ def _close_all(): mlab.close(all=True) -def _set_3d_view(figure, azimuth, elevation, focalpoint, distance, roll=None): +def _set_3d_view(figure, azimuth, elevation, focalpoint, distance, roll=None, + reset_camera=True): from mayavi import mlab with warnings.catch_warnings(record=True): # traits with SilenceStdout(): diff --git a/mne/viz/backends/_pyvista.py b/mne/viz/backends/_pyvista.py index 1896213b1ca..a29d9213b3f 100644 --- a/mne/viz/backends/_pyvista.py +++ b/mne/viz/backends/_pyvista.py @@ -609,9 +609,10 @@ def close(self): _close_3d_figure(figure=self.figure) def set_camera(self, azimuth=None, elevation=None, distance=None, - focalpoint=None, roll=None): + focalpoint=None, roll=None, reset_camera=True): _set_3d_view(self.figure, azimuth=azimuth, elevation=elevation, - distance=distance, focalpoint=focalpoint, roll=roll) + distance=distance, focalpoint=focalpoint, roll=roll, + reset_camera=reset_camera) def reset_camera(self): self.plotter.reset_camera() @@ -759,9 +760,11 @@ def _get_camera_direction(focalpoint, position): return r, theta, phi, focalpoint -def _set_3d_view(figure, azimuth, elevation, focalpoint, distance, roll=None): +def _set_3d_view(figure, azimuth, elevation, focalpoint, distance, roll=None, + reset_camera=True): position = np.array(figure.plotter.camera_position[0]) - figure.plotter.reset_camera() + if reset_camera: + figure.plotter.reset_camera() if focalpoint is None: focalpoint = np.array(figure.plotter.camera_position[1]) r, theta, phi, fp = _get_camera_direction(focalpoint, position) diff --git a/mne/viz/backends/base_renderer.py b/mne/viz/backends/base_renderer.py index f79f81352bf..a0ab69682c8 100644 --- a/mne/viz/backends/base_renderer.py +++ b/mne/viz/backends/base_renderer.py @@ -376,7 +376,7 @@ def close(self): @abstractclassmethod def set_camera(self, azimuth=None, elevation=None, distance=None, - focalpoint=None): + focalpoint=None, roll=None, reset_camera=True): """Configure the camera of the scene. Parameters @@ -389,6 +389,10 @@ def set_camera(self, azimuth=None, elevation=None, distance=None, The distance to the focal point. focalpoint: tuple The focal point of the camera: (x, y, z). + roll: float + The rotation of the camera along its axis. + reset_camera: bool + If True, reset the camera properties beforehand. """ pass From cae25554ddb3965082b3506666a0d4acb2a21e4c Mon Sep 17 00:00:00 2001 From: Guillaume Favelier Date: Wed, 25 Nov 2020 11:18:05 +0100 Subject: [PATCH 2/4] Update set_3d_view --- mne/viz/backends/renderer.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mne/viz/backends/renderer.py b/mne/viz/backends/renderer.py index 7631fd05497..c795ebd1243 100644 --- a/mne/viz/backends/renderer.py +++ b/mne/viz/backends/renderer.py @@ -201,7 +201,8 @@ def _use_test_3d_backend(backend_name, interactive=False): def set_3d_view(figure, azimuth=None, elevation=None, - focalpoint=None, distance=None, roll=None): + focalpoint=None, distance=None, roll=None, + reset_camera=True): """Configure the view of the given scene. Parameters @@ -218,10 +219,13 @@ def set_3d_view(figure, azimuth=None, elevation=None, The distance to the focal point. roll : float The view roll. + reset_camera: bool + If True, reset the camera properties beforehand. """ backend._set_3d_view(figure=figure, azimuth=azimuth, elevation=elevation, focalpoint=focalpoint, - distance=distance, roll=roll) + distance=distance, roll=roll, + reset_camera=reset_camera) def set_3d_title(figure, title, size=40): From b3828ae4f601b4bf23f33fe0ce4a6fdcff950736 Mon Sep 17 00:00:00 2001 From: Guillaume Favelier Date: Wed, 25 Nov 2020 11:36:13 +0100 Subject: [PATCH 3/4] Fix style --- mne/viz/backends/base_renderer.py | 210 +++++++++++++++--------------- mne/viz/backends/renderer.py | 2 +- 2 files changed, 106 insertions(+), 106 deletions(-) diff --git a/mne/viz/backends/base_renderer.py b/mne/viz/backends/base_renderer.py index a0ab69682c8..377243f8827 100644 --- a/mne/viz/backends/base_renderer.py +++ b/mne/viz/backends/base_renderer.py @@ -42,52 +42,52 @@ def mesh(self, x, y, z, triangles, color, opacity=1.0, shading=False, Parameters ---------- - x: array, shape (n_vertices,) + x : array, shape (n_vertices,) The array containing the X component of the vertices. - y: array, shape (n_vertices,) + y : array, shape (n_vertices,) The array containing the Y component of the vertices. - z: array, shape (n_vertices,) + z : array, shape (n_vertices,) The array containing the Z component of the vertices. - triangles: array, shape (n_polygons, 3) + triangles : array, shape (n_polygons, 3) The array containing the indices of the polygons. - color: tuple | str + color : tuple | str The color of the mesh as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). - opacity: float + opacity : float The opacity of the mesh. - shading: bool + shading : bool If True, enable the mesh shading. - backface_culling: bool + backface_culling : bool If True, enable backface culling on the mesh. - scalars: ndarray, shape (n_vertices,) + scalars : ndarray, shape (n_vertices,) The scalar valued associated to the vertices. - vmin: float | None + vmin : float | None vmin is used to scale the colormap. If None, the min of the data will be used - vmax: float | None + vmax : float | None vmax is used to scale the colormap. If None, the max of the data will be used - colormap: + colormap : The colormap to use. - interpolate_before_map: + interpolate_before_map : Enabling makes for a smoother scalars display. Default is True. When False, OpenGL will interpolate the mapped colors which can result is showing colors that are not present in the color map. - representation: str + representation : str The representation of the mesh: either 'surface' or 'wireframe'. - line_width: int + line_width : int The width of the lines when representation='wireframe'. - normals: array, shape (n_vertices, 3) + normals : array, shape (n_vertices, 3) The array containing the normal of each vertex. - polygon_offset: float + polygon_offset : float If not None, the factor used to resolve coincident topology. - kwargs: args + kwargs : args The arguments to pass to triangular_mesh Returns ------- - surface: + surface : Handle of the mesh in the scene. """ pass @@ -100,29 +100,29 @@ def contour(self, surface, scalars, contours, width=1.0, opacity=1.0, Parameters ---------- - surface: surface object + surface : surface object The mesh to use as support for contour. - scalars: ndarray, shape (n_vertices,) + scalars : ndarray, shape (n_vertices,) The scalar valued associated to the vertices. - contours: int | list + contours : int | list Specifying a list of values will only give the requested contours. - width: float + width : float The width of the lines or radius of the tubes. - opacity: float + opacity : float The opacity of the contour. - vmin: float | None + vmin : float | None vmin is used to scale the colormap. If None, the min of the data will be used - vmax: float | None + vmax : float | None vmax is used to scale the colormap. If None, the max of the data will be used - colormap: + colormap : The colormap to use. - normalized_colormap: bool + normalized_colormap : bool Specify if the values of the colormap are between 0 and 1. - kind: 'line' | 'tube' + kind : 'line' | 'tube' The type of the primitives to use to display the contours. - color: + color : The color of the mesh as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). @@ -138,27 +138,27 @@ def surface(self, surface, color=None, opacity=1.0, Parameters ---------- - surface: surface object + surface : surface object The information describing the surface. - color: tuple | str + color : tuple | str The color of the surface as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). - opacity: float + opacity : float The opacity of the surface. - vmin: float | None + vmin : float | None vmin is used to scale the colormap. If None, the min of the data will be used - vmax: float | None + vmax : float | None vmax is used to scale the colormap. If None, the max of the data will be used - colormap: + colormap : The colormap to use. - scalars: ndarray, shape (n_vertices,) + scalars : ndarray, shape (n_vertices,) The scalar valued associated to the vertices. - backface_culling: bool + backface_culling : bool If True, enable backface culling on the surface. - polygon_offset: float + polygon_offset : float If not None, the factor used to resolve coincident topology. """ pass @@ -171,23 +171,23 @@ def sphere(self, center, color, scale, opacity=1.0, Parameters ---------- - center: ndarray, shape(n_center, 3) + center : ndarray, shape(n_center, 3) The list of centers to use for the sphere(s). - color: tuple | str + color : tuple | str The color of the sphere as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). - scale: float + scale : float The scaling applied to the spheres. The given value specifies the maximum size in drawing units. - opacity: float + opacity : float The opacity of the sphere(s). - resolution: int + resolution : int The resolution of the sphere created. This is the number of divisions along theta and phi. - backface_culling: bool + backface_culling : bool If True, enable backface culling on the sphere(s). - radius: float | None + radius : float | None Replace the glyph scaling by a fixed radius value for each sphere (not supported by mayavi). """ @@ -201,36 +201,36 @@ def tube(self, origin, destination, radius=0.001, color='white', Parameters ---------- - origin: array, shape(n_lines, 3) + origin : array, shape(n_lines, 3) The coordinates of the first end of the tube(s). - destination: array, shape(n_lines, 3) + destination : array, shape(n_lines, 3) The coordinates of the other end of the tube(s). - radius: float + radius : float The radius of the tube(s). - color: tuple | str + color : tuple | str The color of the tube as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). - scalars: array, shape (n_quivers,) | None + scalars : array, shape (n_quivers,) | None The optional scalar data to use. - vmin: float | None + vmin : float | None vmin is used to scale the colormap. If None, the min of the data will be used - vmax: float | None + vmax : float | None vmax is used to scale the colormap. If None, the max of the data will be used - colormap: + colormap : The colormap to use. - opacity: float + opacity : float The opacity of the tube(s). - backface_culling: bool + backface_culling : bool If True, enable backface culling on the tube(s). - reverse_lut: bool + reverse_lut : bool If True, reverse the lookup table. Returns ------- - surface: + surface : Handle of the tube in the scene. """ pass @@ -245,55 +245,55 @@ def quiver3d(self, x, y, z, u, v, w, color, scale, mode, resolution=8, Parameters ---------- - x: array, shape (n_quivers,) + x : array, shape (n_quivers,) The X component of the position of the quiver. - y: array, shape (n_quivers,) + y : array, shape (n_quivers,) The Y component of the position of the quiver. - z: array, shape (n_quivers,) + z : array, shape (n_quivers,) The Z component of the position of the quiver. - u: array, shape (n_quivers,) + u : array, shape (n_quivers,) The last X component of the quiver. - v: array, shape (n_quivers,) + v : array, shape (n_quivers,) The last Y component of the quiver. - w: array, shape (n_quivers,) + w : array, shape (n_quivers,) The last Z component of the quiver. - color: tuple | str + color : tuple | str The color of the quiver as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). - scale: float + scale : float The scaling applied to the glyphs. The size of the glyph is by default calculated from the inter-glyph spacing. The given value specifies the maximum glyph size in drawing units. - mode: 'arrow', 'cone' or 'cylinder' + mode : 'arrow', 'cone' or 'cylinder' The type of the quiver. - resolution: int + resolution : int The resolution of the glyph created. Depending on the type of glyph, it represents the number of divisions in its geometric representation. - glyph_height: float + glyph_height : float The height of the glyph used with the quiver. - glyph_center: tuple + glyph_center : tuple The center of the glyph used with the quiver: (x, y, z). - glyph_resolution: float + glyph_resolution : float The resolution of the glyph used with the quiver. - opacity: float + opacity : float The opacity of the quiver. - scale_mode: 'vector', 'scalar' or 'none' + scale_mode : 'vector', 'scalar' or 'none' The scaling mode for the glyph. - scalars: array, shape (n_quivers,) | None + scalars : array, shape (n_quivers,) | None The optional scalar data to use. - backface_culling: bool + backface_culling : bool If True, enable backface culling on the quiver. - colormap: + colormap : The colormap to use. - vmin: float | None + vmin : float | None vmin is used to scale the colormap. If None, the min of the data will be used - vmax: float | None + vmax : float | None vmax is used to scale the colormap. If None, the max of the data will be used - line_width: float + line_width : float The width of the 2d arrows. """ pass @@ -304,17 +304,17 @@ def text2d(self, x_window, y_window, text, size=14, color='white'): Parameters ---------- - x: float + x : float The X component to use as position of the text in the window coordinates system (window_width, window_height). - y: float + y : float The Y component to use as position of the text in the window coordinates system (window_width, window_height). - text: str + text : str The content of the text. - size: int + size : int The size of the font. - color: tuple | str + color : tuple | str The color of the text as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). @@ -327,17 +327,17 @@ def text3d(self, x, y, z, text, width, color='white'): Parameters ---------- - x: float + x : float The X component to use as position of the text. - y: float + y : float The Y component to use as position of the text. - z: float + z : float The Z component to use as position of the text. - text: str + text : str The content of the text. - width: float + width : float The width of the text. - color: tuple | str + color : tuple | str The color of the text as a tuple (red, green, blue) of float values between 0 and 1 or a valid color name (i.e. 'white' or 'w'). @@ -351,15 +351,15 @@ def scalarbar(self, source, color="white", title=None, n_labels=4, Parameters ---------- - source: + source : The object of the scene used for the colormap. - color: + color : The color of the label text. - title: str | None + title : str | None The title of the scalar bar. - n_labels: int | None + n_labels : int | None The number of labels to display on the scalar bar. - bgcolor: + bgcolor : The color of the background when there is transparency. """ pass @@ -381,17 +381,17 @@ def set_camera(self, azimuth=None, elevation=None, distance=None, Parameters ---------- - azimuth: float + azimuth : float The azimuthal angle of the camera. - elevation: float + elevation : float The zenith angle of the camera. - distance: float + distance : float The distance to the focal point. - focalpoint: tuple + focalpoint : tuple The focal point of the camera: (x, y, z). - roll: float + roll : float The rotation of the camera along its axis. - reset_camera: bool + reset_camera : bool If True, reset the camera properties beforehand. """ pass @@ -407,10 +407,10 @@ def screenshot(self, mode='rgb', filename=None): Parameters ---------- - mode: str + mode : str Either 'rgb' or 'rgba' for values to return. Default is 'rgb'. - filename: str | None + filename : str | None If not None, save the figure to the disk. """ pass @@ -421,9 +421,9 @@ def project(self, xyz, ch_names): Parameters ---------- - xyz: array, shape(n_points, 3) + xyz : array, shape(n_points, 3) The points to project. - ch_names: array, shape(_n_points,) + ch_names : array, shape(_n_points,) Names of the channels. """ pass diff --git a/mne/viz/backends/renderer.py b/mne/viz/backends/renderer.py index c795ebd1243..680a0574a55 100644 --- a/mne/viz/backends/renderer.py +++ b/mne/viz/backends/renderer.py @@ -219,7 +219,7 @@ def set_3d_view(figure, azimuth=None, elevation=None, The distance to the focal point. roll : float The view roll. - reset_camera: bool + reset_camera : bool If True, reset the camera properties beforehand. """ backend._set_3d_view(figure=figure, azimuth=azimuth, From be0364a9fb3f657baab34ca87a8adca7c2fc6735 Mon Sep 17 00:00:00 2001 From: Guillaume Favelier Date: Wed, 25 Nov 2020 12:33:00 +0100 Subject: [PATCH 4/4] Add compatibility with mayavi --- mne/viz/backends/_pysurfer_mayavi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mne/viz/backends/_pysurfer_mayavi.py b/mne/viz/backends/_pysurfer_mayavi.py index 0f7c53157ec..39abd16977b 100644 --- a/mne/viz/backends/_pysurfer_mayavi.py +++ b/mne/viz/backends/_pysurfer_mayavi.py @@ -313,7 +313,7 @@ def close(self): _close_3d_figure(figure=self.fig) def set_camera(self, azimuth=None, elevation=None, distance=None, - focalpoint=None, roll=None): + focalpoint=None, roll=None, reset_camera=None): _set_3d_view(figure=self.fig, azimuth=azimuth, elevation=elevation, distance=distance, focalpoint=focalpoint, roll=roll)