def __getitem__(self, idx: int) -> Union[Graph, List[Graph]]: r""" Takes in an integer (or a list of integers) returns a single Graph object (a subset of graphs). Args: idx: index to be selected from graphs. Returns: Union[:class:`deepsnap.graph.Graph`, List[:class:`deepsnap.graph.Graph`]]: A single :class:`deepsnap.graph.Graph` object or subset of :class:`deepsnap.graph.Graph` objects. """ if self.task == 'link_pred' and self._resample_negatives: # resample negative examples for graph in self.graphs: if type(graph) == Graph: graph._create_neg_sampling( self.edge_negative_sampling_ratio, resample=True) elif type(graph) == HeteroGraph: graph._create_neg_sampling( self.edge_negative_sampling_ratio, split_types=self._split_types, resample=True) # TODO: add the hetero graph equivalent of these functions ? if self.graphs is None: graph = self.generator.generate() if not isinstance(graph, Graph): graph = Graph(graph) # generated an networkx graph if self.otf_device is not None: graph.to(self.otf_device) return graph elif isinstance(idx, int): return self.graphs[idx] else: return self._index_select(idx)
def __getitem__(self, idx: int) -> Union[Graph, List[Graph]]: r""" Takes in an integer (or a list of integers) returns a single Graph object (a subset of graphs). Args: idx: index to be selected from graphs. Returns: Union[:class:`deepsnap.graph.Graph`, List[:class:`deepsnap.graph.Graph`]]: A single :class:`deepsnap.graph.Graph` object or subset of :class:`deepsnap.graph.Graph` objects. """ # TODO: add the hetero graph equivalent of these functions ? if self.graphs is None: graph = self.generator.generate() if not isinstance(graph, Graph): graph = Graph(graph) # generated an networkx graph if self.otf_device is not None: graph.to(self.otf_device) # return graph elif isinstance(idx, int): graph = self.graphs[idx] else: graph = self._index_select(idx) if self.task == "link_pred" and self._resample_negatives: # resample negative examples if isinstance(graph, Graph): if isinstance(graph, HeteroGraph): if self.negative_edges_mode == "random": graph._create_neg_sampling( self.edge_negative_sampling_ratio, split_types=self._split_types, resample=True) elif self.negative_edges_mode == "custom": raise NotImplementedError() else: if self.negative_edges_mode == "random": graph._create_neg_sampling( self.edge_negative_sampling_ratio, resample=True) elif self.negative_edges_mode == "custom": graph._custom_create_neg_sampling( self.edge_negative_sampling_ratio, resample=True) else: raise TypeError("element in self.graphs of unexpected type.") return graph
def __getitem__(self, idx: int) -> Union[Graph, List[Graph]]: r""" Takes in an integer (or a list of integers) returns a single Graph object (a subset of graphs). Args: idx: index to be selected from graphs. Returns: Union[:class:`deepsnap.graph.Graph`, List[:class:`deepsnap.graph.Graph`]]: A single :class:`deepsnap.graph.Graph` object or subset of :class:`deepsnap.graph.Graph` objects. """ # TODO: add the hetero graph equivalent of these functions ? if self.graphs is None: graph = self.generator.generate() if not isinstance(graph, Graph): graph = Graph(graph) # generated an networkx graph if self.otf_device is not None: graph.to(self.otf_device) elif isinstance(idx, int): graph = self.graphs[idx] else: # sliceing of dataset dataset = self._index_select(idx) return dataset # TODO: remove self.task check by add corresponding checker in the __init__ if (self.task == "link_pred" and self.edge_train_mode == "disjoint" and self.resample_disjoint and graph.is_train): if not hasattr(graph, "resample_disjoint_period"): graph.resample_disjoint_period = self.resample_disjoint_period if isinstance(graph, Graph): if isinstance(graph, HeteroGraph): graph = graph.resample_disjoint( split_types=self._split_types, message_ratio=self.edge_message_ratio) else: graph = graph.resample_disjoint(self.edge_message_ratio) else: raise TypeError("element in self.graphs of unexpected type.") if self.task == "link_pred" and self.resample_negatives: resample_negative_flag = True # after graph just resampled disjoint training data # graph.edge_label is reset to original state, # the negative sampling process needs to utilize # resample=False mode. if (hasattr(graph, "_resample_disjoint_flag") and graph._resample_disjoint_flag): resample_negative_flag = False # resample negative examples if isinstance(graph, Graph): if isinstance(graph, HeteroGraph): if self.negative_edges_mode == "random": graph._create_neg_sampling( self.edge_negative_sampling_ratio, split_types=self._split_types, resample=resample_negative_flag) elif self.negative_edges_mode == "custom": graph._custom_create_neg_sampling( self.edge_negative_sampling_ratio, split_types=self._split_types, resample=resample_negative_flag) else: if self.negative_edges_mode == "random": graph._create_neg_sampling( self.edge_negative_sampling_ratio, resample=resample_negative_flag) elif self.negative_edges_mode == "custom": graph._custom_create_neg_sampling( self.edge_negative_sampling_ratio, resample=resample_negative_flag) else: raise TypeError("element in self.graphs of unexpected type.") if self.graphs is not None and isinstance(idx, int): self.graphs[idx] = graph return graph