Commit 76891462 authored by nd-02110114's avatar nd-02110114
Browse files

🐛 fix small bug

parent 13050dc2
Loading
Loading
Loading
Loading
+25 −35
Original line number Diff line number Diff line
@@ -85,13 +85,8 @@ class GraphData:
    if self.node_features is not None:
      self.num_edge_features = self.edge_features.shape[1]

  def to_pyg_data(self, target: np.ndarray):
    """Convert to PyTorch Geometric Data instance

    Parameters
    ----------
    target: np.ndarray
      Graph or node targets with arbitrary shape
  def to_pyg_graph(self):
    """Convert to PyTorch Geometric graph data instance

    Returns
    -------
@@ -110,9 +105,31 @@ class GraphData:
      edge_index=torch.from_numpy(self.edge_index),
      edge_attr=None if self.edge_features is None \
        else torch.from_numpy(self.edge_features),
      y=torch.from_numpy(target),
    )

  def to_dgl_graph(self):
    """Convert to DGL graph data instance

    Returns
    -------
    dgl.DGLGraph
      Graph data for PyTorch Geometric
    """
    try:
      from dgl import DGLGraph
    except ModuleNotFoundError:
      raise ValueError("This function requires DGL to be installed.")

    g = DGLGraph()
    g.add_nodes(self.num_nodes)
    g.add_edges(self.edge_index[0], self.edge_index[1])
    g.ndata['x'] = torch.from_numpy(self.node_features)

    if self.edge_features is not None:
      g.edata['edge_attr'] = torch.from_numpy(self.edge_features)

    return g


class BatchGraphData(GraphData):
  """Batch GraphData class
@@ -177,30 +194,3 @@ class BatchGraphData(GraphData):
        edge_features=batch_edge_features,
        graph_features=batch_graph_features,
    )

    @staticmethod  # type: ignore
    def to_pyg_data(graphs: Sequence[GraphData], targets: Sequence[np.ndarray]):
      """Convert to PyTorch Geometric Batch instance

      Parameters
      ----------
      graphs: Sequence[GraphData]
        List of GraphData
      targets: Sequence[np.ndarray]
        List of graph or node targets with arbitrary shape

      Returns
      -------
      torch_geometric.data.Batch
        Batch data of graphs for PyTorch Geometric
      """
      try:
        from torch_geometric.data import Batch
      except ModuleNotFoundError:
        raise ValueError(
            "This function requires PyTorch Geometric to be installed.")

      data_list = [
          graph.to_pyg_data(target) for graph, target in zip(graphs, targets)
      ]
      return Batch.from_data_list(data_list=data_list)
+0 −1
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ from deepchem.utils.typing import PymatgenStructure
from deepchem.feat import MaterialStructureFeaturizer
from deepchem.feat.graph_data import GraphData


ATOM_INIT_JSON_URL = 'https://deepchemdata.s3-us-west-1.amazonaws.com/datasets/atom_init.json'


+6 −9
Original line number Diff line number Diff line
@@ -28,12 +28,15 @@ class TestGraph(unittest.TestCase):
    assert graph.num_edges == num_edges
    assert graph.num_edge_features == num_edge_features

    # check to_pyg_data function
    target = np.array([1], dtype=np.float)
    pyg_graph = graph.to_pyg_data(target)
    # check convert function
    pyg_graph = graph.to_pyg_graph()
    from torch_geometric.data import Data
    assert isinstance(pyg_graph, Data)

    dgl_graph = graph.to_pyg_graph()
    from dgl import DGLGraph
    assert isinstance(dgl_graph, DGLGraph)

  def test_invalid_graph_data(self):
    with pytest.raises(ValueError):
      invalid_node_features_type = list(np.random.random_sample((5, 5)))
@@ -87,9 +90,3 @@ class TestGraph(unittest.TestCase):
    assert batch.num_edges == sum(num_edge_list)
    assert batch.num_edge_features == num_edge_features
    assert batch.graph_index.shape == (sum(num_nodes_list),)

    # check to_pyg_data function
    targets = np.array([1, 2, 3], dtype=np.float)
    batch = BatchGraphData.to_pyg_data(graphs=graphs, targets=targets)
    from torch_geometric.data import Batch
    assert isinstance(pyg_graph, Batch)
+1 −0
Original line number Diff line number Diff line
biopython==1.77
dgl==0.4.3.post2
matminer==0.6.3
mdtraj==1.9.4
networkx==2.4