Skip to content

Bader

Bases: GridPlotter

Source code in src/baderkit/plotting/core/plotter.py
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
class BaderPlotter(GridPlotter):
    def __init__(
        self,
        bader: Bader,
        off_screen: bool = False,
    ):
        """
        A convenience class for creating plots of individual Bader basins
        using pyvista's package for VTK.

        Parameters
        ----------
        bader : Bader
            The Bader object to use for isolating basins and creating isosurfaces.
            The structure will be pulled from the charge grid.
        off_screen : bool, optional
            Whether or not the plotter should be in offline mode. The default is False.

        Returns
        -------
        None.

        """
        # apply StructurePlotter kwargs
        grid = bader.charge_grid
        super().__init__(grid=grid, off_screen=off_screen)
        self.bader = bader

        # pad the label arrays then flatten them
        padded_basins = np.pad(
            bader.basin_labels, pad_width=((0, 1), (0, 1), (0, 1)), mode="wrap"
        )
        padded_atoms = np.pad(
            bader.atom_labels, pad_width=((0, 1), (0, 1), (0, 1)), mode="wrap"
        )
        # padded_basins = bader.basin_labels
        # padded_atoms = bader.atom_labels
        self.flat_bader_basins = padded_basins.ravel(order="F")
        self.flat_atom_basins = padded_atoms.ravel(order="F")

        # get the initial empty list of visible atom labels and visible basin labels
        self._visible_bader_basins = set()
        self._visible_atom_basins = set()
        self.visible_bader_basins = []
        self.visible_atom_basins = []
        self._hidden_mask = np.zeros(len(self.flat_bader_basins), dtype=bool)

    @property
    def visible_bader_basins(self) -> list[int]:
        """

        Returns
        -------
        list[int]
            A list of bader basin indices that are currently visible.

        """
        return self._visible_bader_basins

    @visible_bader_basins.setter
    def visible_bader_basins(self, visible_bader_basins: set[int]):
        # make sure input is set
        visible_bader_basins = set(visible_bader_basins)
        # set visible basins
        self._visible_bader_basins = visible_bader_basins
        # update plotter
        self._update_plotter_mask()

    @property
    def visible_atom_basins(self) -> list[int]:
        """

        Returns
        -------
        list[int]
            A list of atom indices whose basins are currently visible.

        """
        return self._visible_atom_basins

    @visible_atom_basins.setter
    def visible_atom_basins(self, visible_atom_basins: set[int]):
        # make sure input is set
        visible_atom_basins = set(visible_atom_basins)
        # update visible basins set
        self._visible_atom_basins = visible_atom_basins
        # update plotter
        self._update_plotter_mask()

    def _update_plotter_mask(self):
        """
        Updates the mask indicating which areas of the grid should not be shown
        then sets the regions to -1.

        Returns
        -------
        None.

        """
        hidden_mask = ~(
            np.isin(self.flat_bader_basins, list(self._visible_bader_basins))
            | np.isin(self.flat_atom_basins, list(self._visible_atom_basins))
        )
        self._hidden_mask = hidden_mask
        # NOTE: using hide_cells works, but results in some funky artifacting.
        # Maybe there's a way to get it to work, but for now I'm replacing it
        # for visual quality
        # self.structured_grid.hide_cells(self.hidden_mask, inplace=True)
        # update structured_grid
        temp_values = self.values.copy()
        temp_values[hidden_mask] = -1
        self.structured_grid = self._make_structured_grid(temp_values)
        # update the surface
        self.surface = self.structured_grid.extract_surface()
        # update plotter
        self.iso_val = self.iso_val

visible_atom_basins property writable

Returns:

Type Description
list[int]

A list of atom indices whose basins are currently visible.

visible_bader_basins property writable

Returns:

Type Description
list[int]

A list of bader basin indices that are currently visible.

__init__(bader, off_screen=False)

A convenience class for creating plots of individual Bader basins using pyvista's package for VTK.

Parameters:

Name Type Description Default
bader Bader

The Bader object to use for isolating basins and creating isosurfaces. The structure will be pulled from the charge grid.

required
off_screen bool

Whether or not the plotter should be in offline mode. The default is False.

False

Returns:

Type Description
None.
Source code in src/baderkit/plotting/core/plotter.py
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
def __init__(
    self,
    bader: Bader,
    off_screen: bool = False,
):
    """
    A convenience class for creating plots of individual Bader basins
    using pyvista's package for VTK.

    Parameters
    ----------
    bader : Bader
        The Bader object to use for isolating basins and creating isosurfaces.
        The structure will be pulled from the charge grid.
    off_screen : bool, optional
        Whether or not the plotter should be in offline mode. The default is False.

    Returns
    -------
    None.

    """
    # apply StructurePlotter kwargs
    grid = bader.charge_grid
    super().__init__(grid=grid, off_screen=off_screen)
    self.bader = bader

    # pad the label arrays then flatten them
    padded_basins = np.pad(
        bader.basin_labels, pad_width=((0, 1), (0, 1), (0, 1)), mode="wrap"
    )
    padded_atoms = np.pad(
        bader.atom_labels, pad_width=((0, 1), (0, 1), (0, 1)), mode="wrap"
    )
    # padded_basins = bader.basin_labels
    # padded_atoms = bader.atom_labels
    self.flat_bader_basins = padded_basins.ravel(order="F")
    self.flat_atom_basins = padded_atoms.ravel(order="F")

    # get the initial empty list of visible atom labels and visible basin labels
    self._visible_bader_basins = set()
    self._visible_atom_basins = set()
    self.visible_bader_basins = []
    self.visible_atom_basins = []
    self._hidden_mask = np.zeros(len(self.flat_bader_basins), dtype=bool)