Skip to content

graphchem.data.MoleculeDataset

Bases: Dataset

A custom dataset class for molecular graphs.

This class extends the Dataset class from PyTorch Geometric to create a dataset of molecular graphs. Each graph in the dataset is an instance of MoleculeGraph.

Attributes

_graphs : List[MoleculeGraph] A list containing all the molecule graphs in the dataset.

Source code in graphchem/data/structs.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class MoleculeDataset(Dataset):
    """
    A custom dataset class for molecular graphs.

    This class extends the `Dataset` class from PyTorch Geometric to create a
    dataset of molecular graphs. Each graph in the dataset is an instance of
    `MoleculeGraph`.

    Attributes
    ----------
    _graphs : List[MoleculeGraph]
        A list containing all the molecule graphs in the dataset.
    """

    def __init__(self, graphs: Iterable[MoleculeGraph]):
        """
        Initialize the MoleculeDataset object.

        Parameters
        ----------
        graphs : Iterable[MoleculeGraph]
            An iterable of `MoleculeGraph` instances representing the
            molecules in the dataset.
        """

        super().__init__()
        self._graphs = list(graphs)

    def len(self) -> int:
        """
        Returns the number of molecules in the dataset.

        Returns
        -------
        int
            The number of molecule graphs in the dataset.
        """
        return len(self._graphs)

    def get(self, idx: int) -> MoleculeGraph:
        """
        Retrieves a molecule graph from the dataset by index.

        Returns
        -------
        MoleculeGraph
            The molecule graph at the specified index.
        """
        return self._graphs[idx]

__init__(graphs)

Initialize the MoleculeDataset object.

Parameters

graphs : Iterable[MoleculeGraph] An iterable of MoleculeGraph instances representing the molecules in the dataset.

Source code in graphchem/data/structs.py
83
84
85
86
87
88
89
90
91
92
93
94
95
def __init__(self, graphs: Iterable[MoleculeGraph]):
    """
    Initialize the MoleculeDataset object.

    Parameters
    ----------
    graphs : Iterable[MoleculeGraph]
        An iterable of `MoleculeGraph` instances representing the
        molecules in the dataset.
    """

    super().__init__()
    self._graphs = list(graphs)

get(idx)

Retrieves a molecule graph from the dataset by index.

Returns

MoleculeGraph The molecule graph at the specified index.

Source code in graphchem/data/structs.py
108
109
110
111
112
113
114
115
116
117
def get(self, idx: int) -> MoleculeGraph:
    """
    Retrieves a molecule graph from the dataset by index.

    Returns
    -------
    MoleculeGraph
        The molecule graph at the specified index.
    """
    return self._graphs[idx]

len()

Returns the number of molecules in the dataset.

Returns

int The number of molecule graphs in the dataset.

Source code in graphchem/data/structs.py
 97
 98
 99
100
101
102
103
104
105
106
def len(self) -> int:
    """
    Returns the number of molecules in the dataset.

    Returns
    -------
    int
        The number of molecule graphs in the dataset.
    """
    return len(self._graphs)