Skip to content

Bader

Bases: GridPlotter

Source code in src/baderkit/plotting/core/plotter.py
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
class BaderPlotter(GridPlotter):
    def __init__(
        self,
        bader: Bader,
        **grid_kwargs,
    ):
        """
        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.

        Returns
        -------
        None.

        """
        # apply StructurePlotter kwargs
        grid = bader.charge_grid
        super().__init__(grid=grid, **grid_kwargs)
        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(
            [i for i, ai in enumerate(bader.basin_atoms) if ai == 0]
        )
        self._visible_atom_basins = set()
        self.visible_bader_basins = [
            i for i, ai in enumerate(bader.basin_atoms) if ai == 0
        ]
        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, **grid_kwargs)

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

Returns:

Type Description
None.
Source code in src/baderkit/plotting/core/plotter.py
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
def __init__(
    self,
    bader: Bader,
    **grid_kwargs,
):
    """
    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.

    Returns
    -------
    None.

    """
    # apply StructurePlotter kwargs
    grid = bader.charge_grid
    super().__init__(grid=grid, **grid_kwargs)
    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(
        [i for i, ai in enumerate(bader.basin_atoms) if ai == 0]
    )
    self._visible_atom_basins = set()
    self.visible_bader_basins = [
        i for i, ai in enumerate(bader.basin_atoms) if ai == 0
    ]
    self.visible_atom_basins = []
    self._hidden_mask = np.zeros(len(self.flat_bader_basins), dtype=bool)