Example #1
0
def test_prepare_data_dict(query):
    """
    Test prepare_data_dict method.
    """
    res = prepare_data_dict(query[0], query[1])
    res = prepare_data_dict(res, query[2])
    assert res is not None
Example #2
0
def merge_edge(g: BaseGraph,
               u: str,
               v: str,
               key: str,
               data: dict,
               preserve: bool = True) -> dict:
    """
    Merge edge ``u`` -> ``v`` into graph ``g``.

    Parameters
    ----------
    g: kgx.graph.base_graph.BaseGraph
        The target graph
    u: str
        Subject node id
    v: str
        Object node id
    key: str
        Edge key
    data: dict
        Node properties
    preserve: bool
        Whether or not to preserve conflicting properties

    Returns
    -------
    dict
        The merged edge

    """
    existing_edge = g.get_edge(u, v, key)
    new_data = prepare_data_dict(copy.deepcopy(existing_edge),
                                 copy.deepcopy(data), preserve)
    g.add_edge(u, v, edge_key=key, **new_data)
    return existing_edge
Example #3
0
def merge_node(g: BaseGraph,
               n: str,
               data: dict,
               preserve: bool = True) -> dict:
    """
    Merge node ``n`` into graph ``g``.

    Parameters
    ----------
    g: kgx.graph.base_graph.BaseGraph
        The target graph
    n: str
        Node id
    data: dict
        Node properties
    preserve: bool
        Whether or not to preserve conflicting properties

    Returns
    -------
    dict
        The merged node

    """
    existing_node = g.nodes()[n]
    new_data = prepare_data_dict(copy.deepcopy(existing_node),
                                 copy.deepcopy(data), preserve)
    g.add_node(n, **new_data)
    return existing_node
Example #4
0
    def update_node_attribute(self,
                              node: str,
                              attr_key: str,
                              attr_value: Any,
                              preserve: bool = False) -> Dict:
        """
        Update an attribute of a given node.

        Parameters
        ----------
        node: str
            The node identifier
        attr_key: str
            The key for an attribute
        attr_value: Any
            The value corresponding to the key
        preserve: bool
            Whether or not to preserve existing values for the given attr_key

        Returns
        -------
        Dict
            A dictionary corresponding to the updated node properties

        """
        node_data = self.graph.nodes[node]
        updated = prepare_data_dict(node_data, {attr_key: attr_value},
                                    preserve=preserve)
        self.graph.add_node(node, **updated)
        return updated
Example #5
0
    def update_edge_attribute(
        self,
        subject_node: str,
        object_node: str,
        edge_key: Optional[str],
        attr_key: str,
        attr_value: Any,
        preserve: bool = False,
    ) -> Dict:
        """
        Update an attribute of a given edge.

        Parameters
        ----------
        subject_node: str
            The subject (source) node
        object_node: str
            The object (target) node
        edge_key: Optional[str]
            The edge key
        attr_key: str
            The attribute key
        attr_value: Any
            The attribute value
        preserve: bool
            Whether or not to preserve existing values for the given attr_key

        Returns
        -------
        Dict
            A dictionary corresponding to the updated edge properties

        """
        e = self.graph.edges((subject_node, object_node, edge_key),
                             keys=True,
                             data=True)
        edge_data = list(e)[0][3]
        updated = prepare_data_dict(edge_data, {attr_key: attr_value},
                                    preserve)
        self.graph.add_edge(subject_node, object_node, key=edge_key, **updated)
        return updated
Example #6
0
    def add_node_attribute(
        self, iri: Union[URIRef, str], key: str, value: Union[str, List]
    ) -> None:
        """
        Add an attribute to a node in cache, while taking into account whether the attribute
        should be multi-valued.

        The ``key`` may be a rdflib.URIRef or an URI string that maps onto a
        property name as defined in ``rdf_utils.property_mapping``.

        Parameters
        ----------
        iri: Union[rdflib.URIRef, str]
            The IRI of a node in the rdflib.Graph
        key: str
            The name of the attribute. Can be a rdflib.URIRef or URI string
        value: Union[str, List]
            The value of the attribute

        Returns
        -------
        Dict
            The node data

        """
        if self.prefix_manager.is_iri(key):
            key_curie = self.prefix_manager.contract(key)
        else:
            key_curie = key
        c = curie_lookup(key_curie)
        if c:
            key_curie = c

        if self.prefix_manager.is_curie(key_curie):
            # property names will always be just the reference
            mapped_key = self.prefix_manager.get_reference(key_curie)
        else:
            mapped_key = key_curie

        if isinstance(value, rdflib.term.Identifier):
            if isinstance(value, rdflib.term.URIRef):
                value_curie = self.prefix_manager.contract(value)
                # if self.prefix_manager.get_prefix(value_curie) not in {'biolink'} \
                #         and mapped_key not in {'type', 'category', 'predicate', 'relation', 'predicate'}:
                #     d = self.add_node(value)
                #     value = d['id']
                # else:
                #     value = value_curie
                value = value_curie
            else:
                value = value.toPython()
        if mapped_key in is_property_multivalued and is_property_multivalued[mapped_key]:
            value = [value]
        if mapped_key in self.node_record:
            if isinstance(self.node_record[mapped_key], str):
                _ = self.node_record[mapped_key]
                self.node_record[mapped_key] = [_]
            self.node_record[mapped_key].append(value)
        else:
            self.node_record[mapped_key] = [value]
        curie = self.prefix_manager.contract(iri)
        if curie in self.node_cache:
            if mapped_key in self.node_cache[curie]:
                node = self.node_cache[curie]
                updated_node = prepare_data_dict(node, {mapped_key: value})
                self.node_cache[curie] = updated_node
            else:
                self.node_cache[curie][mapped_key] = value
        else:
            self.node_cache[curie] = {'id': curie, mapped_key: value}