def __enter__(self): """Set this figure as the current in the pylab API. Example: >>> f1 = ipv.figure(1) >>> f2 = ipv.figure(2) >>> with f1: >>> ipv.scatter(x, y, z) >>> assert ipv.gcf() is f2 """ self._previous_figure = ipv.gcf() ipv.figure(self)
def test_context(): f1 = ipv.figure(1) f2 = ipv.figure(2) f3 = ipv.figure(2) assert ipv.gcf() is f3 with f2: assert ipv.gcf() is f2 assert ipv.gcf() is f3 # test nested with f2: assert ipv.gcf() is f2 with f1: assert ipv.gcf() is f1 assert ipv.gcf() is f2 assert ipv.gcf() is f3
def test_context(): f1 = ipv.figure(1) f2 = ipv.figure(2) f3 = ipv.figure(2) assert ipv.gcf() is f3 with f2: # pylint: disable=not-context-manager assert ipv.gcf() is f2 assert ipv.gcf() is f3 # test nested with f2: # pylint: disable=not-context-manager assert ipv.gcf() is f2 with f1: # pylint: disable=not-context-manager assert ipv.gcf() is f1 assert ipv.gcf() is f2 assert ipv.gcf() is f3
def _update_data(self): if self.data_original is None: return if all([k <= self.data_max_shape for k in self.data_original.shape]): self.data = self.data_original self.extent = self.extent_original return current_figure = ipv.gcf() xlim = current_figure.xlim ylim = current_figure.ylim zlim = current_figure.zlim shape = self.data_original.shape ex = self.extent_original viewx, xt = grid_slice(ex[0][0], ex[0][1], shape[2], *xlim) viewy, yt = grid_slice(ex[1][0], ex[1][1], shape[1], *ylim) viewz, zt = grid_slice(ex[2][0], ex[2][1], shape[0], *zlim) view = [slice(*viewz), slice(*viewy), slice(*viewx)] data_view = self.data_original[view] extent = [xt, yt, zt] data_view, extent = reduce_size(data_view, self.data_max_shape, extent) self.data = np.array(data_view) self.extent = extent
def sheet_view(sheet, coords=["x", "y", "z"], **draw_specs_kw): """ Creates a javascript renderer of the edge lines to be displayed in Jupyter Notebooks Returns ------- fig: a :class:`ipyvolume.widgets.Figure` widget mesh: a :class:`ipyvolume.widgets.Mesh` mesh widget """ ipv.style.use(["dark", "minimal"]) draw_specs = sheet_spec() spec_updater(draw_specs, draw_specs_kw) fig = ipv.gcf() edge_spec = draw_specs["edge"] if edge_spec["visible"]: edges = edge_mesh(sheet, coords, **edge_spec) fig.meshes = fig.meshes + [edges] else: edges = None face_spec = draw_specs["face"] if face_spec["visible"]: faces = face_mesh(sheet, coords, **face_spec) fig.meshes = fig.meshes + [faces] else: faces = None box_size = max(*(np.ptp(sheet.vert_df[u]) for u in sheet.coords)) border = 0.05 * box_size lim_inf = sheet.vert_df[sheet.coords].min().min() - border lim_sup = sheet.vert_df[sheet.coords].max().max() + border ipv.xyzlim(lim_inf, lim_sup) return fig, (edges, faces)
def view_ipv(sheet, coords=["x", "y", "z"], **edge_specs): """ Creates a javascript renderer of the edge lines to be displayed in Jupyter Notebooks Returns ------- fig: a :class:`ipyvolume.widgets.Figure` widget mesh: a :class:`ipyvolume.widgets.Mesh` mesh widget """ warnings.warn("`view_ipv` is deprecated, use the more generic `sheet_view`") mesh = edge_mesh(sheet, coords, **edge_specs) fig = ipv.gcf() fig.meshes = fig.meshes + [mesh] box_size = max(*(np.ptp(sheet.vert_df[u]) for u in sheet.coords)) border = 0.05 * box_size lim_inf = sheet.vert_df[sheet.coords].min().min() - border lim_sup = sheet.vert_df[sheet.coords].max().max() + border ipv.xyzlim(lim_inf, lim_sup) return fig, mesh
def sheet_view(sheet, coords=["x", "y", "z"], **draw_specs_kw): """ Creates a javascript renderer of the edge lines to be displayed in Jupyter Notebooks Returns ------- fig: a :class:`ipyvolume.widgets.Figure` widget mesh: a :class:`ipyvolume.widgets.Mesh` mesh widget """ # ipv.style.use(["dark", "minimal"]) draw_specs = sheet_spec() spec_updater(draw_specs, draw_specs_kw) fig = ipv.gcf() fig.meshes = fig.meshes + _get_meshes(sheet, coords, draw_specs) box_size = max(*(np.ptp(sheet.vert_df[u]) for u in sheet.coords)) border = 0.05 * box_size lim_inf = sheet.vert_df[sheet.coords].min().min() - border lim_sup = sheet.vert_df[sheet.coords].max().max() + border ipv.xyzlim(lim_inf, lim_sup) return fig, fig.meshes
def show_saliency_volume( saliency_maps, tasks=[], show_ct=True, show_pet=False, colormap=None ): colors = plt.get_cmap("Set1").colors widgets = [] def add_controls(volume, name, color=None): """ """ level = FloatLogSlider( base=10, min=-0.5, max=0, step=0.0002, description=f"{name} level:" ) opacity = FloatLogSlider( base=10, min=-2, max=1.0, step=0.01, description=f"{name} opacity:" ) jslink((volume.tf, "level1"), (level, "value")) jslink((volume.tf, "level2"), (level, "value")) jslink((volume.tf, "level3"), (level, "value")) jslink((volume, "opacity_scale"), (opacity, "value")) button = Button(description=name) if color is not None: button.style.button_color = color controls = HBox([button, level, opacity]) widgets.append(controls) ipv.clear() ipv.figure() data = {} for idx, task in enumerate(tasks): saliency_map = saliency_maps[task] scan_grad = saliency_map["scan_grad"] scan_grad = torch.max(scan_grad, dim=4)[0].unsqueeze(0) scan_grad = ( scan_grad.squeeze() ) # torch.nn.functional.max_pool3d(scan_grad, kernel_size=(7, 21, 21)).squeeze() scan_grad = scan_grad.numpy() scan_grad = scan_grad - scan_grad.min() scan_grad /= scan_grad.max() saliency_tensor = scan_grad saliency_tensor -= saliency_tensor.min() saliency_tensor /= saliency_tensor.max() if colormap is None: color = np.array(colors[idx % len(colors)]) else: color = np.array(colormap[task]) / 256 opacity = color * 0.2 color = "#%02x%02x%02x" % tuple(map(int, color * 256)) saliency_vol = ipv.volshow( saliency_tensor, downscale=100, level=(0.5, 0.5, 0.5), opacity=opacity, controls=True, ) data[task] = saliency_tensor add_controls(saliency_vol, name=task, color=color) scan = saliency_map["scan"].numpy() if show_ct: ct = scan[:, :, :, :, 0].squeeze() data["ct"] = ct ct_vol = ipv.volshow( ct, downscale=100, level=(1.0, 1.0, 1.0), opacity=(0.2, 0.2, 0.2) ) add_controls(ct_vol, name="ct") if show_pet: pet = scan[:, :, :, :, 1].squeeze() data["pet"] = pet opacity = (np.array((228, 26, 28)) / 256) * 0.2 pet_vol = ipv.volshow( pet, downscale=100, level=(0.7, 0.7, 0.7), opacity=opacity ) add_controls(pet_vol, name="pet") ipv.style.use("minimal") widgets.append(ipv.gcf()) return VBox(widgets), data
def show_attention_volume( scan, attention_maps, tasks=[], show_ct=True, show_pet=False, downscale=1, exam_id="", movie_dir=None, colormap=None, flip_axes=None, ): colors = plt.get_cmap("Set1").colors widgets = [] def add_controls(volume, name, color=None): """ """ level = FloatLogSlider( base=10, min=-0.5, max=0, step=0.01, description=f"{name} level:" ) opacity = FloatLogSlider( base=10, min=-2, max=0.6, step=0.01, description=f"{name} opacity:" ) jslink((volume.tf, "level1"), (level, "value")) jslink((volume.tf, "level2"), (level, "value")) jslink((volume.tf, "level3"), (level, "value")) jslink((volume, "opacity_scale"), (opacity, "value")) button = Button(description=name) if color is not None: button.style.button_color = color controls = HBox([button, level, opacity]) widgets.append(controls) ipv.clear() f = ipv.figure() for idx, task in enumerate(tasks): attention_map = attention_maps[task] attention_map = attention_map.cpu().detach().numpy() attention_map = np.mean(attention_map, axis=1) attention_map = attention_map[0, :, ::].squeeze() # scale volume up scan_shape = scan[:, :, :, :, 0].squeeze().shape for dim_idx, scan_dim in enumerate(scan_shape): repeat = np.round(scan_dim / attention_map.shape[dim_idx]) attention_map = np.repeat(attention_map, repeat, axis=dim_idx) # set color if colormap is None: color = np.array(colors[idx % len(colors)]) else: color = np.array(colormap[task]) / 256 opacity = color * 0.2 color = "#%02x%02x%02x" % tuple(map(int, color * 255)) attention_map = attention_map[::downscale, ::downscale, ::downscale] if flip_axes is not None: for flip_axis in flip_axes: attention_map = np.flip(attention_map, axis=flip_axis) saliency_vol = ipv.volshow( attention_map, level=(1.0, 1.0, 1.0), opacity=opacity, controls=True, extent=[ [0, attention_map.shape[0]], [0, attention_map.shape[1]], [0, attention_map.shape[2]], ], ) add_controls(saliency_vol, name=task, color=color) if show_ct: ct = scan[:, :, :, :, 0].squeeze() ct = ct[::downscale, ::downscale, ::downscale] if flip_axes is not None: for flip_axis in flip_axes: ct = np.flip(ct, axis=flip_axis) ct_vol = ipv.volshow( ct, downscale=100, level=(0.7, 0.7, 0.7), opacity=(0.2, 0.2, 0.2), extent=[[0, ct.shape[0]], [0, ct.shape[1]], [0, ct.shape[2]]], ) add_controls(ct_vol, name="ct") if show_pet: pet = scan[:, :, :, :, 1].squeeze() pet = pet[::downscale, ::downscale, ::downscale] if flip_axes is not None: for flip_axis in flip_axes: pet = np.flip(pet, axis=flip_axis) color = np.array(colors[0 % len(colors)]) opacity = color * 0.2 pet_vol = ipv.volshow( pet, downscale=100, level=(0.7, 0.7, 0.7), opacity=opacity ) add_controls(pet_vol, name="pet") # ipv.xlim(0,attention_map.shape[0]) # ipv.ylim(0,attention_map.shape[1]) # ipv.zlim(0,attention_map.shape[2]) # ipv.squarelim() # f.camera.up = (-1, -1, -1) # f.camera.lookAt = (0, 0, 0) ipv.pylab.view(0, 0, 0) if movie_dir is not None: ipv.pylab.movie(f=os.path.join(movie_dir, f"{exam_id}.gif")) ipv.style.use("minimal") widgets.append(ipv.gcf()) return VBox(widgets)