def __getitem__(self, index: Union[int, str]) -> Optional['MultiBlock']: """Get a block by its index or name. If the name is non-unique then returns the first occurrence. """ if isinstance(index, slice): stop = index.stop if index.stop >= 0 else self.n_blocks + index.stop + 1 step = index.step if isinstance(index.step, int) else 1 multi = MultiBlock() for i in range(index.start, stop, step): multi[-1, self.get_block_name(i)] = self[i] return multi elif isinstance(index, (list, tuple, np.ndarray)): multi = MultiBlock() for i in index: name = i if isinstance(i, str) else self.get_block_name(i) multi[-1, name] = self[i] # type: ignore return multi elif isinstance(index, str): index = self.get_index_by_name(index) ############################ if index < 0: index = self.n_blocks + index if index < 0 or index >= self.n_blocks: raise IndexError(f'index ({index}) out of range for this dataset.') data = self.GetBlock(index) if data is None: return data if data is not None and not is_pyvista_dataset(data): data = wrap(data) if data not in self.refs: self.refs.append(data) return data
def wrap_nested(self): """A helper to ensure all nested data structures are wrapped as PyVista datasets. This is perfrom inplace""" for i in range(self.n_blocks): block = self.GetBlock(i) if not is_pyvista_dataset(block): self.SetBlock(i, pyvista.wrap(block)) return
def wrap_nested(self): """Ensure that all nested data structures are wrapped as PyVista datasets. This is performed in place. """ for i in range(self.n_blocks): block = self.GetBlock(i) if not is_pyvista_dataset(block): self.SetBlock(i, pyvista.wrap(block))
def overwrite(self, mesh): """ Overwrites this mesh inplace with the new mesh's geometries and data Parameters ---------- mesh : vtk.vtkDataSet The overwriting mesh. """ self.deep_copy(mesh) if is_pyvista_dataset(mesh): self.copy_meta_from(mesh)
def plot_compare_four(data_a, data_b, data_c, data_d, disply_kwargs=None, plotter_kwargs=None, show_kwargs=None, screenshot=None, camera_position=None, outline=None, outline_color='k', labels=('A', 'B', 'C', 'D'), link=True, notebook=None): """Plot a 2 by 2 comparison of data objects. Plotting parameters and camera positions will all be the same. """ datasets = [[data_a, data_b], [data_c, data_d]] labels = [labels[0:2], labels[2:4]] if plotter_kwargs is None: plotter_kwargs = {} if disply_kwargs is None: disply_kwargs = {} if show_kwargs is None: show_kwargs = {} plotter_kwargs['notebook'] = notebook pl = pyvista.Plotter(shape=(2, 2), **plotter_kwargs) for i in range(2): for j in range(2): pl.subplot(i, j) pl.add_mesh(datasets[i][j], **disply_kwargs) pl.add_text(labels[i][j]) if is_pyvista_dataset(outline): pl.add_mesh(outline, color=outline_color) if camera_position is not None: pl.camera_position = camera_position if link: pl.link_views() # when linked, camera must be reset such that the view range # of all subrender windows matches pl.reset_camera() return pl.show(screenshot=screenshot, **show_kwargs)
def overwrite(self, mesh: _vtk.vtkDataSet): """Overwrite this mesh inplace with the new mesh's geometries and data. Parameters ---------- mesh : vtk.vtkDataSet The overwriting mesh. """ if not isinstance(mesh, type(self)): raise TypeError('The Input DataSet type must match ' f'the one being overwritten {type(self)}') self.deep_copy(mesh) if is_pyvista_dataset(mesh): self.copy_meta_from(mesh)
def __getitem__(self, index): """Get a block by its index or name (if the name is non-unique then returns the first occurence)""" if isinstance(index, str): index = self.get_index_by_name(index) if index < 0: index = self.n_blocks + index if index < 0 or index >= self.n_blocks: raise IndexError('index ({}) out of range for this dataset.'.format(index)) data = self.GetBlock(index) if data is None: return data if data is not None and not is_pyvista_dataset(data): data = wrap(data) if data not in self.refs: self.refs.append(data) return data
def __setitem__(self, index: Union[Tuple[int, Optional[str]], int, str], data: DataSet): """Set a block with a VTK data object. To set the name simultaneously, pass a string name as the 2nd index. Example ------- >>> import pyvista >>> multi = pyvista.MultiBlock() >>> multi[0] = pyvista.PolyData() >>> multi[1, 'foo'] = pyvista.UnstructuredGrid() >>> multi['bar'] = pyvista.PolyData() >>> multi.n_blocks 3 """ i: int = 0 name: Optional[str] = None if isinstance( index, (np.ndarray, collections.abc.Sequence)) and not isinstance(index, str): i, name = index[0], index[1] elif isinstance(index, str): try: i = self.get_index_by_name(index) except KeyError: i = -1 name = index else: i, name = cast(int, index), None if data is not None and not is_pyvista_dataset(data): data = wrap(data) if i == -1: self.append(data) i = self.n_blocks - 1 else: self.SetBlock(i, data) if name is None: name = f'Block-{i:02}' self.set_block_name(i, name) # Note that this calls self.Modified() if data not in self.refs: self.refs.append(data)
def __setitem__(self, index, data): """Set a block with a VTK data object. To set the name simultaneously, pass a string name as the 2nd index. Example ------- >>> import pyvista >>> multi = pyvista.MultiBlock() >>> multi[0] = pyvista.PolyData() >>> multi[1, 'foo'] = pyvista.UnstructuredGrid() >>> multi['bar'] = pyvista.PolyData() >>> multi.n_blocks 3 """ if isinstance(index, collections.Iterable) and not isinstance(index, str): i, name = index[0], index[1] elif isinstance(index, str): try: i = self.get_index_by_name(index) except KeyError: i = -1 name = index else: i, name = index, None if data is not None and not is_pyvista_dataset(data): data = wrap(data) if i == -1: self.append(data) i = self.n_blocks - 1 else: self.SetBlock(i, data) if name is None: name = 'Block-{0:02}'.format(i) self.set_block_name(i, name) # Note that this calls self.Modified() if data not in self.refs: self.refs.append(data)
def __init__(self, dataset, plotter=None, scalars=None, preference='cell', show_bounds=False, reset_camera=True, outline=None, display_params=None, default_params=None, continuous_update=False, clean=True, **kwargs): if not scooby.in_ipython() or not IPY_AVAILABLE: logging.warning( 'Interactive plotting tools require IPython and the ``ipywidgets`` package.' ) # Check the input dataset to make sure its compatible if not is_pyvista_dataset(dataset): dataset = wrap(dataset) if not is_pyvista_dataset(dataset): raise TypeError( 'Object type ({}) not supported for plotting in PyVista.'. format(type(dataset))) # Make the input/output of this tool available self.input_dataset = dataset self.output_dataset = None self.continuous_update = continuous_update if plotter is None: plotter = BackgroundPlotter(**kwargs) plotter.setWindowTitle(type(self).__name__) self._plotter = plotter self._loc = plotter.index_to_loc(plotter._active_renderer_index) # This is the actor that will be removed and re-added to the plotter self._data_to_update = None self._last_scalars = None # Intialize plotting parameters self.valid_range = self.input_dataset.get_data_range( arr=scalars, preference=preference) if display_params is None: display_params = {} if default_params is None: default_params = {} display_params.setdefault('rng', self.valid_range) display_params.setdefault('scalars', scalars) display_params.setdefault('preference', preference) display_params.setdefault('show_edges', False) # Make sure to remove the reset_camera parameter if present display_params.pop('reset_camera', None) # Make a name display_params.setdefault( 'name', '{}({})'.format(type(self).__name__, str(hex(id(self))))) self.display_params = display_params self.clean = clean # Set the tool status self._need_to_update = True # Add some intital plotting stuff to the scene self._initialize(show_bounds, reset_camera, outline) # Run the tool self.tool(default_params=default_params, **kwargs)