Example #1
0
    def get_data_range(self, arr=None, preference='cell'):
        """Get the non-NaN min and max of a named scalar array

        Parameters
        ----------
        arr : str, np.ndarray, optional
            The name of the array to get the range. If None, the active scalar
            is used

        preference : str, optional
            When scalars is specified, this is the perfered scalar type to
            search for in the dataset.  Must be either ``'point'``, ``'cell'``,
            or ``'field'``.

        """
        if arr is None:
            # use active scalar array
            _, arr = self.active_scalar_info
        if isinstance(arr, str):
            arr = get_scalar(self, arr, preference=preference)
        # If array has no tuples return a NaN range
        if arr is None or arr.size == 0 or not np.issubdtype(
                arr.dtype, np.number):
            return (np.nan, np.nan)
        # Use the array range
        return np.nanmin(arr), np.nanmax(arr)
Example #2
0
 def set_active_vectors(self, name, preference='cell'):
     """Finds the vectors by name and appropriately sets it as active"""
     _, field = get_scalar(self, name, preference=preference, info=True)
     if field == POINT_DATA_FIELD:
         self.GetPointData().SetActiveVectors(name)
     elif field == CELL_DATA_FIELD:
         self.GetCellData().SetActiveVectors(name)
     else:
         raise RuntimeError('Data field ({}) not useable'.format(field))
     self._active_vectors_info = [field, name]
Example #3
0
 def rename_scalar(self, old_name, new_name, preference='cell'):
     """Changes array name by searching for the array then renaming it"""
     _, field = get_scalar(self, old_name, preference=preference, info=True)
     if field == POINT_DATA_FIELD:
         self.point_arrays[new_name] = self.point_arrays.pop(old_name)
     elif field == CELL_DATA_FIELD:
         self.cell_arrays[new_name] = self.cell_arrays.pop(old_name)
     elif field == FIELD_DATA_FIELD:
         self.field_arrays[new_name] = self.field_arrays.pop(old_name)
     else:
         raise RuntimeError('Array not found.')
     if self.active_scalar_info[1] == old_name:
         self.set_active_scalar(new_name, preference=field)
Example #4
0
 def format_array(key, field):
     """internal helper to foramt array information for printing"""
     arr = get_scalar(self, key, preference=field)
     dl, dh = self.get_data_range(key)
     dl = pyvista.FLOAT_FORMAT.format(dl)
     dh = pyvista.FLOAT_FORMAT.format(dh)
     if key == self.active_scalar_info[1]:
         key = '<b>{}</b>'.format(key)
     if arr.ndim > 1:
         ncomp = arr.shape[1]
     else:
         ncomp = 1
     return row.format(key, field, arr.dtype, ncomp, dl, dh)
Example #5
0
 def get_data_range(self, name):
     """Gets the min/max of a scalar given its name across all blocks"""
     mini, maxi = np.inf, -np.inf
     for i in range(self.n_blocks):
         data = self[i]
         if data is None:
             continue
         # get the scalar if availble
         arr = get_scalar(data, name)
         if arr is None or not np.issubdtype(arr.dtype, np.number):
             continue
         tmi, tma = np.nanmin(arr), np.nanmax(arr)
         if tmi < mini:
             mini = tmi
         if tma > maxi:
             maxi = tma
     return mini, maxi
Example #6
0
def test_get_scalar():
    grid = pyvista.UnstructuredGrid(ex.hexbeamfile)
    # add array to both point/cell data with same name
    carr = np.random.rand(grid.n_cells)
    grid._add_cell_scalar(carr, 'test_data')
    parr = np.random.rand(grid.n_points)
    grid._add_point_scalar(parr, 'test_data')
    # add other data
    oarr = np.random.rand(grid.n_points)
    grid._add_point_scalar(oarr, 'other')
    farr = np.random.rand(grid.n_points * grid.n_cells)
    grid._add_field_scalar(farr, 'field_data')
    assert np.allclose(carr, utilities.get_scalar(grid, 'test_data', preference='cell'))
    assert np.allclose(parr, utilities.get_scalar(grid, 'test_data', preference='point'))
    assert np.allclose(oarr, utilities.get_scalar(grid, 'other'))
    assert None == utilities.get_scalar(grid, 'foo')
    assert utilities.get_scalar(grid, 'test_data', preference='field') is None
    assert np.allclose(farr, utilities.get_scalar(grid, 'field_data', preference='field'))
Example #7
0
 def get_scalar(self, name, preference='cell', info=False):
     """ Searches both point, cell and field data for an array """
     return get_scalar(self, name, preference=preference, info=info)