divelab / DIG

A library for graph deep learning research
https://diveintographs.readthedocs.io/
GNU General Public License v3.0
1.82k stars 281 forks source link

TypeError: Can't instantiate abstract class MarginalSubgraphDataset with abstract methods get, len #193

Closed w41039 closed 1 year ago

w41039 commented 1 year ago

Hi DIG team, Thanks a lot for this amazing work.

As a beginner, I tried the ipynb file "DIG/examples/xgraph/subgraphx.ipynb", and found the error as the title stated when I call the SubgraphX explainer (see more about the error message in the attached zip). It is very strange in the sense that the code worked perfectly yesterday but it can't work today, and I am pretty sure that I didnt change anythings........

I guess the self defined len(self) and getitem(self, idx) are not working, but I have no ideas how to solve it.....Could you pls suggest a way to correct it? Thank you~ subgraphx.zip

wzr0108 commented 10 months ago

I encountered the same problem, did you solve it?

DrChandraMohanD commented 7 months ago

Hi DIG team, Thanks a lot for this amazing work.

As a beginner, I tried the ipynb file "DIG/examples/xgraph/subgraphx.ipynb", and found the error as the title stated when I call the SubgraphX explainer (see more about the error message in the attached zip). It is very strange in the sense that the code worked perfectly yesterday but it can't work today, and I am pretty sure that I didnt change anythings........

I guess the self defined len(self) and getitem(self, idx) are not working, but I have no ideas how to solve it.....Could you pls suggest a way to correct it? Thank you~ subgraphx.zip

Hello, I also facing the same error. Have you resolved the issue? Could you please let us know how to resolve "TypeError: Can't instantiate abstract class MarginalSubgraphDataset with abstract methods get, len"

TheSpeky commented 4 months ago

Hello, I found a work around, but I am now encountering new errors in other parts (not directly related to MarginalSubgraphDataset).

I found the definition of the class MaginalSubgraphDataset and copied the len and getitem method and gave them the names len and get.

class MarginalSubgraphDataset(Dataset):
    def __init__(self, data, exclude_mask, include_mask, subgraph_build_func):
        self.num_nodes = data.num_nodes
        self.X = data.x
        self.edge_index = data.edge_index
        self.device = self.X.device

        self.label = data.y
        self.exclude_mask = torch.tensor(exclude_mask).type(torch.float32).to(self.device)
        self.include_mask = torch.tensor(include_mask).type(torch.float32).to(self.device)
        self.subgraph_build_func = subgraph_build_func

    def __len__(self):
        return self.exclude_mask.shape[0]

    def __getitem__(self, idx):
        exclude_graph_X, exclude_graph_edge_index = self.subgraph_build_func(self.X, self.edge_index, self.exclude_mask[idx])
        include_graph_X, include_graph_edge_index = self.subgraph_build_func(self.X, self.edge_index, self.include_mask[idx])
        exclude_data = Data(x=exclude_graph_X, edge_index=exclude_graph_edge_index)
        include_data = Data(x=include_graph_X, edge_index=include_graph_edge_index)
        return exclude_data, include_data

    #added by me
    def len(self):
        return self.exclude_mask.shape[0]

    def get(self, idx):
        exclude_graph_X, exclude_graph_edge_index = self.subgraph_build_func(self.X, self.edge_index, self.exclude_mask[idx])
        include_graph_X, include_graph_edge_index = self.subgraph_build_func(self.X, self.edge_index, self.include_mask[idx])
        exclude_data = Data(x=exclude_graph_X, edge_index=exclude_graph_edge_index)
        include_data = Data(x=include_graph_X, edge_index=include_graph_edge_index)
        return exclude_data, include_data