def _create_label(self, kg: KG, vertex: Vertex, n: int) -> str: """Creates a label according to a vertex and its neighbors. kg: The Knowledge Graph. The graph from which the neighborhoods are extracted for the provided entities. vertex: The vertex to get its neighbors to create the suffix. n: The index of the neighbor Returns: the label created for the vertex. """ if len(self._label_map) == 0: self._weisfeiler_lehman(kg) suffix = "-".join( sorted( set( [ self._label_map[neighbor][n - 1] for neighbor in kg.get_neighbors( vertex, is_reverse=True ) ] ) ) ) return f"{self._label_map[vertex][n - 1]}-{suffix}"
def _community_detection(self, kg: KG) -> None: """Converts the knowledge graph to a networkX graph. Note: You can create a `graph.KnowledgeGraph` object from an `rdflib.Graph` object by using a converter method. Args: kg: The Knowledge Graph. The graph from which the neighborhoods are extracted for the provided instances. """ nx_graph = nx.Graph() for v in kg._vertices: if not v.predicate: name = str(v) nx_graph.add_node(name, vertex=v) for v in kg._vertices: if not v.predicate: v_name = str(v) # Neighbors are predicates for pred in kg.get_neighbors(v): pred_name = str(pred) for obj in kg.get_neighbors(pred): obj_name = str(obj) nx_graph.add_edge(v_name, obj_name, name=pred_name) # This will create a dictionary that maps the URI on a community partition = community.best_partition(nx_graph, resolution=self.resolution) self.labels_per_community = defaultdict(list) self.communities = {} vertices = nx.get_node_attributes(nx_graph, "vertex") for node in partition: if node in vertices: self.communities[vertices[node]] = partition[node] for node in self.communities: self.labels_per_community[self.communities[node]].append(node)
def fit(self, kg: KG) -> None: """Fits the embedding network based on provided Knowledge Graph. Args: kg: The Knowledge Graph. """ super().fit(kg) nx_graph = nx.DiGraph() for vertex in kg._vertices: if not vertex.predicate: nx_graph.add_node(vertex.name, vertex=vertex) for predicate in kg.get_neighbors(vertex): for obj in kg.get_neighbors(predicate): nx_graph.add_edge(vertex.name, obj.name, name=predicate.name) self.pageranks = nx.pagerank(nx_graph, alpha=self.alpha)
def fit(self, kg: KG) -> None: """Fits the sampling strategy by running PageRank on a provided KG according to the specified damping. Args: kg: The Knowledge Graph. """ super().fit(kg) nx_graph = nx.DiGraph() for vertex in kg._vertices: if not vertex.predicate: nx_graph.add_node(vertex.name, vertex=vertex) for predicate in kg.get_neighbors(vertex): for obj in kg.get_neighbors(predicate): nx_graph.add_edge(vertex.name, obj.name, name=predicate.name) self._pageranks = nx.pagerank(nx_graph, alpha=self.alpha)
def _community_detection(self, kg: KG) -> None: """Converts the knowledge graph to a networkX graph. Note: You can create a `graph.KnowledgeGraph` object from an `rdflib.Graph` object by using a converter method. Args: kg: The Knowledge Graph. """ nx_graph = nx.Graph() for vertex in kg._vertices: if not vertex.predicate: nx_graph.add_node(str(vertex), vertex=vertex) for vertex in kg._vertices: if not vertex.predicate: # Neighbors are predicates for pred in kg.get_neighbors(vertex): for obj in kg.get_neighbors(pred): nx_graph.add_edge(str(vertex), str(obj), name=str(pred)) # Create a dictionary that maps the URI on a community partition = community.best_partition(nx_graph, resolution=self.resolution) self.labels_per_community = defaultdict(list) self.communities = {} vertices = nx.get_node_attributes(nx_graph, "vertex") for node in partition: if node in vertices: self.communities[vertices[node]] = partition[node] for node in self.communities: self.labels_per_community[self.communities[node]].append(node)
def fit(self, kg: KG) -> None: """Fits the sampling strategy by counting the number of available neighbors for each vertex. Args: kg: The Knowledge Graph. """ super().fit(kg) for vertex in kg._vertices: if not vertex.predicate: self._counts[vertex.name] = len( kg.get_neighbors(vertex, is_reverse=True))
def fit(self, kg: KG) -> None: """Fits the embedding network based on provided Knowledge Graph. Args: kg: The Knowledge Graph. """ super().fit(kg) self.counts: DefaultDict[Any, Any] = defaultdict(int) for vertex in kg._vertices: if vertex.predicate: # Always one object associated with this predicate obj = list(kg.get_neighbors(vertex))[0] self.counts[(vertex.name, obj.name)] += 1
def fit(self, kg: KG) -> None: """Fits the sampling strategy by counting the number of occurrences of an object belonging to a subject. Args: kg: The Knowledge Graph. """ super().fit(kg) for vertex in kg._vertices: if vertex.predicate: objs = list(kg.get_neighbors(vertex)) if objs: obj = objs[0] if (vertex.name, obj.name) in self._counts: self._counts[(vertex.name, obj.name)] += 1 else: self._counts[(vertex.name, obj.name)] = 1
def fit(self, kg: KG) -> None: """Fits the sampling strategy by counting the number of occurance of having two neighboring vertices. Args: kg: The Knowledge Graph. """ super().fit(kg) for vertex in kg._vertices: if vertex.predicate: neighbors = list(kg.get_neighbors(vertex)) if len(neighbors) > 0: obj = neighbors[0] if (vertex.name, obj.name) in self._counts: self._counts[(vertex.name, obj.name)] += 1 else: self._counts[(vertex.name, obj.name)] = 1
def fit(self, kg: KG) -> None: """Fits the sampling strategy. Args: kg: The Knowledge Graph. Raises: SamplerNotSupported: If there is an attempt to use an invalid sampling strategy to a remote Knowledge Graph. """ if kg._is_remote and not self._is_support_remote: raise SamplerNotSupported( "Invalid sampling strategy. Please, choose a sampling strategy" + " that can fetch walks via a SPARQL endpoint server.") if self.split: for vertex in kg._vertices: if not vertex.predicate: self._vertices_deg[vertex.name] = len( kg.get_neighbors(vertex, is_reverse=True))
def fit(self, kg: KG) -> None: """Fits the sampling strategy by couting the number of available neighbors for each vertex, but also by counting the number of occurrence that a predicate and an object appears in the Knowledge Graph. Args: kg: The Knowledge Graph. """ super().fit(kg) for vertex in kg._vertices: is_reverse = True if vertex.predicate else False counter = self._pred_degs if vertex.predicate else self._obj_degs self._neighbor_counts[vertex.name] = len( kg.get_neighbors(vertex, is_reverse=is_reverse)) if vertex.name in counter: counter[vertex.name] += 1 else: counter[vertex.name] = 1