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 |
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 | |
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 |
required |
graph
|
Graph
|
The graph to add nodes to. Not modified in place; a copy is returned. |
required |
node_col
|
str
|
The column in |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
A copy of |
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 | |
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 |
required |
source_df
|
DataFrame
|
A dataframe with "Composer", "Title", "First_Offset", and |
required |
node_col
|
str
|
The name of the column in |
required |
Returns:
| Type | Description |
|---|---|
str
|
An HTML string ( |
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 | |
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 |
required |
chosen_word
|
str
|
The column in |
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 |
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 | |
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 |
required |
p_type
|
str
|
The presentation type to filter for (matched against "Presentation_Type"). |
required |
chosen_word
|
str
|
The column in |
required |
corpus_name
|
str
|
A label used (with |
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 | |