<commit_before># -*- coding: utf-8 -*-

import networkx as nx

from .utils import update_metadata, update_node_helper

__all__ = [
    'subgraph',
    'left_full_join',
    'left_outer_join',
    'union',
    'left_node_intersection_join',
    'node_intersection',
]


def subgraph(graph, nodes):
    subgraph = graph.subgraph(nodes)

    # see implementation for .copy()
    result = graph.fresh_copy()
    result.graph.update(subgraph.graph)

    for node, data in subgraph.nodes(data=True):
        result.add_node(node, **data)

    result.add_edges_from(
        (u, v, key, datadict.copy())
        for u, v, key, datadict in subgraph.edges(keys=True, data=True)
    )

    return result


def left_full_join(g, h):
    """Add all nodes and edges from ``h`` to ``g``, in-place for ``g``
<commit_msg>Refactor subgraph function<commit_after># -*- coding: utf-8 -*-

import networkx as nx

from .utils import update_metadata, update_node_helper

__all__ = [
    'subgraph',
    'left_full_join',
    'left_outer_join',
    'union',
    'left_node_intersection_join',
    'node_intersection',
]


def subgraph(graph, nodes):
    """Induce a sub-graph over the given nodes.

    :param BELGraph graph:
    :param set[BaseEntity] nodes:
    :rtype: BELGraph
    """
    sg = graph.subgraph(nodes)

    # see implementation for .copy()
    result = graph.fresh_copy()
    result.graph.update(sg.graph)

    for node, data in sg.nodes(data=True):
        result.add_node(node, **data)

    result.add_edges_from(
        (u, v, key, datadict.copy())
        for u, v, key, datadict in sg.edges(keys=True, data=True)
    )

    return result


def left_full_join(g, h):
    """Add all nodes and edges from ``h`` to ``g``, in-place for ``g``

{'fraction_non_alphanumeric': 0.13463751438434982, 'fraction_numerical': 0.0011507479861910242, 'mean_word_length': 3.1208530805687205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1759720', 'n_tokens_mistral': 617, 'n_tokens_neox': 597, 'n_words': 153}