Skip to content

networks

Helpers for building pyvis network graphs from presentation-type and pattern-matching results, used in the network visualization notebooks.

add_communities

add_communities(G)

Detect communities in a graph and store them as a node attribute.

Runs Louvain community detection on a copy of G and stores each node's community id in a "group" attribute, which pyvis/networkx can use to color nodes by community.

Parameters:

Name Type Description Default
G Graph

The graph to analyze. Not modified in place; a copy is returned.

required

Returns:

Type Description
Graph

A copy of G with a "group" attribute added to every node.

Source code in crim_intervals/networks.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def add_communities(G):
    """
    Detect communities in a graph and store them as a node attribute.

    Runs Louvain community detection on a copy of `G` and stores each node's
    community id in a "group" attribute, which pyvis/networkx can use to
    color nodes by community.

    Parameters
    ----------
    G : networkx.Graph
        The graph to analyze. Not modified in place; a copy is returned.

    Returns
    -------
    networkx.Graph
        A copy of `G` with a "group" attribute added to every node.
    """
    G = deepcopy(G)
    partition = community_louvain.best_partition(G)
    nx.set_node_attributes(G, partition, "group")
    return G

add_nodes_from_edgelist

add_nodes_from_edgelist(edge_list: list, source_df: DataFrame, graph: Graph, node_col: str)

Add every unique node found in an edge list to a graph, with tooltips.

Flattens edge_list (a list of node pairs) to its unique node values and adds each as a node in a copy of graph, using create_node_html to build a hover-tooltip title for each node from source_df.

Parameters:

Name Type Description Default
edge_list list

A list of node pairs (e.g. 2-tuples) defining the edges to draw nodes from.

required
source_df DataFrame

A dataframe with "Composer", "Title", "First_Offset", and node_col columns, passed through to create_node_html for each node.

required
graph Graph

The graph to add nodes to. Not modified in place; a copy is returned.

required
node_col str

The column in source_df that node values correspond to.

required

Returns:

Type Description
Graph

A copy of graph with a node (and HTML tooltip) added for every unique value found in edge_list.

Source code in crim_intervals/networks.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def add_nodes_from_edgelist(edge_list: list,
                               source_df: pd.DataFrame,
                               graph: nx.Graph,
                               node_col: str):
    """
    Add every unique node found in an edge list to a graph, with tooltips.

    Flattens `edge_list` (a list of node pairs) to its unique node values and
    adds each as a node in a copy of `graph`, using `create_node_html` to
    build a hover-tooltip title for each node from `source_df`.

    Parameters
    ----------
    edge_list : list
        A list of node pairs (e.g. 2-tuples) defining the edges to draw nodes
        from.
    source_df : pandas.DataFrame
        A dataframe with "Composer", "Title", "First_Offset", and `node_col`
        columns, passed through to `create_node_html` for each node.
    graph : networkx.Graph
        The graph to add nodes to. Not modified in place; a copy is returned.
    node_col : str
        The column in `source_df` that node values correspond to.

    Returns
    -------
    networkx.Graph
        A copy of `graph` with a node (and HTML tooltip) added for every
        unique value found in `edge_list`.
    """
    graph = deepcopy(graph)

    node_list = pd.Series(edge_list).apply(pd.Series).stack().unique()

    for n in node_list:
        graph.add_node(n, title=create_node_html(n, source_df, node_col))

    return graph

create_node_html

create_node_html(node: str, source_df: DataFrame, node_col: str)

Build an HTML tooltip listing the pieces/offsets where a node occurs.

Filters source_df for rows where node_col equals node, and renders an HTML <ul> with one <li> per matching row showing Composer, Title, and First_Offset. Intended to be passed as the title of a pyvis node so it displays as a hover tooltip.

Parameters:

Name Type Description Default
node str

The value to match against node_col in source_df.

required
source_df DataFrame

A dataframe with "Composer", "Title", "First_Offset", and node_col columns.

required
node_col str

The name of the column in source_df to match node against.

required

Returns:

Type Description
str

An HTML string (<ul>...</ul>) suitable for use as a pyvis node title.

Source code in crim_intervals/networks.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def create_node_html(node: str, source_df: pd.DataFrame, node_col: str):
    """
    Build an HTML tooltip listing the pieces/offsets where a node occurs.

    Filters `source_df` for rows where `node_col` equals `node`, and renders
    an HTML `<ul>` with one `<li>` per matching row showing Composer, Title,
    and First_Offset. Intended to be passed as the `title` of a pyvis node
    so it displays as a hover tooltip.

    Parameters
    ----------
    node : str
        The value to match against `node_col` in `source_df`.
    source_df : pandas.DataFrame
        A dataframe with "Composer", "Title", "First_Offset", and `node_col`
        columns.
    node_col : str
        The name of the column in `source_df` to match `node` against.

    Returns
    -------
    str
        An HTML string (`<ul>...</ul>`) suitable for use as a pyvis node title.
    """
    rows = source_df.loc[source_df[node_col] == node].itertuples()

    html_lis = []

    for r in rows:
        html_lis.append(f"""<li>Composer: {r.Composer}<br>
                                Title: {r.Title}<br>
                                Offset: {r.First_Offset}</li>"""
                       )

    html_ul = f"""<ul>{''.join(html_lis)}</ul>"""

    return html_ul

choose_network

choose_network(df, chosen_word, file_name)

Build and render a pyvis network of co-occurring values within pieces.

Groups df by "Title" and, within each piece, forms all pairwise combinations of the values in the chosen_word column (e.g. soggetti or presentation-type labels that co-occur in the same piece). Each unique pair becomes an edge; nodes are colored by detected community (add_communities) and annotated with hover tooltips (add_nodes_from_edgelist). The resulting graph is written to an HTML file via pyvis.

Parameters:

Name Type Description Default
df DataFrame

A dataframe with a "Title" column and a chosen_word column.

required
chosen_word str

The column in df whose values become the network's nodes.

required
file_name str

The path/filename pyvis will write the rendered HTML graph to.

required

Returns:

Type Description
None

The graph is written to file_name as a side effect; nothing is returned.

Source code in crim_intervals/networks.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def choose_network(df, chosen_word, file_name):
    """
    Build and render a pyvis network of co-occurring values within pieces.

    Groups `df` by "Title" and, within each piece, forms all pairwise
    combinations of the values in the `chosen_word` column (e.g. soggetti or
    presentation-type labels that co-occur in the same piece). Each unique
    pair becomes an edge; nodes are colored by detected community
    (`add_communities`) and annotated with hover tooltips
    (`add_nodes_from_edgelist`). The resulting graph is written to an HTML
    file via pyvis.

    Parameters
    ----------
    df : pandas.DataFrame
        A dataframe with a "Title" column and a `chosen_word` column.
    chosen_word : str
        The column in `df` whose values become the network's nodes.
    file_name : str
        The path/filename pyvis will write the rendered HTML graph to.

    Returns
    -------
    None
        The graph is written to `file_name` as a side effect; nothing is
        returned.
    """
    output_grouped = df.groupby(['Title'])[chosen_word].apply(list).reset_index()
    pairs = output_grouped[chosen_word].apply(lambda x: list(combinations(x, 2)))
    pairs2 = pairs.explode().dropna()
    unique_pairs = pairs.explode().dropna().unique()


    pyvis_graph = Network(notebook=True, width="1800", height="1400", bgcolor="black", font_color="white")
    G = nx.Graph()

    try:
        G = add_nodes_from_edgelist(edge_list=unique_pairs, source_df=df, graph=G, node_col=chosen_word)
    except Exception as e:
        print(e)


    G.add_edges_from(unique_pairs)
    G = add_communities(G)
    pyvis_graph.from_nx(G)
    pyvis_graph.show(file_name)

create_ptype_network

create_ptype_network(df, p_type, chosen_word, corpus_name)

Build a choose_network graph limited to one presentation type.

Filters df to rows where "Presentation_Type" equals p_type, then calls choose_network on the filtered result, writing the output to a file named "{corpus_name}_{p_type}_{chosen_word}.html".

Parameters:

Name Type Description Default
df DataFrame

A dataframe with "Title", "Presentation_Type", and chosen_word columns.

required
p_type str

The presentation type to filter for (matched against "Presentation_Type").

required
chosen_word str

The column in df whose values become the network's nodes.

required
corpus_name str

A label used (with p_type and chosen_word) to build the output HTML filename.

required

Returns:

Type Description
None

The graph is written to a file as a side effect; nothing is returned.

Source code in crim_intervals/networks.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def create_ptype_network(df, p_type, chosen_word, corpus_name):
    """
    Build a `choose_network` graph limited to one presentation type.

    Filters `df` to rows where "Presentation_Type" equals `p_type`, then
    calls `choose_network` on the filtered result, writing the output to a
    file named `"{corpus_name}_{p_type}_{chosen_word}.html"`.

    Parameters
    ----------
    df : pandas.DataFrame
        A dataframe with "Title", "Presentation_Type", and `chosen_word`
        columns.
    p_type : str
        The presentation type to filter for (matched against
        "Presentation_Type").
    chosen_word : str
        The column in `df` whose values become the network's nodes.
    corpus_name : str
        A label used (with `p_type` and `chosen_word`) to build the output
        HTML filename.

    Returns
    -------
    None
        The graph is written to a file as a side effect; nothing is
        returned.
    """
    output_file_name = f"{corpus_name}_{p_type}_{chosen_word}.html"
    choose_network(df.query(f"Presentation_Type == '{p_type}'"), chosen_word, output_file_name)