Importance Network & Visualization
ImportanceNetwork
A class for building and analyzing a directed graph representing the importance network of a system.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
importance_df |
DataFrame
|
A dataframe with columns for the source node, target node, value flow between nodes, and layer for each node. This dataframe should represent the complete importance network of the system. |
required |
val_col |
str
|
The name of the column in the DataFrame that represents the value flow between nodes. Defaults to "value". |
'value'
|
norm_method |
str
|
Method to normalie the value column with. Options are 'subgraph' and 'fan'. If 'subgraph', normalizes on the log(nodes in subgraph) from each node. If 'fan', normalizes on the log(fan in + fan out) for each node. |
'subgraph'
|
Attributes:
Name | Type | Description |
---|---|---|
importance_df |
DataFrame
|
The dataframe used for downstream/upstream subgraph construction and plotting. |
val_col |
str
|
The name of the column in the DataFrame that represents the value flow between nodes. |
G |
DiGraph
|
A directed graph object representing the importance network of the system. |
G_reverse |
DiGraph
|
A directed graph object representing the importance network of the system in reverse. |
norm_method |
str
|
The normalization method. |
Source code in binn/importance_network.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 68 69 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 108 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 155 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
add_normalization(method='subgraph')
Adds normalization to the importance values based on the specified method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method |
str
|
The normalization method to use. Options are "fan" and "subgraph". "fan" normalizes based on fan-in and fan-out values. "subgraph" normalizes based on the number of nodes in the upstream and downstream subgraphs. |
'subgraph'
|
Returns:
Type | Description |
---|---|
pd.DataFrame: The importance dataframe with the normalized values added. |
Source code in binn/importance_network.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
create_graph()
Create a directed graph (DiGraph) from the source and target nodes in the input dataframe.
Returns:
Name | Type | Description |
---|---|---|
importance_graph |
a directed graph (DiGraph) object |
Source code in binn/importance_network.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
get_complete_subgraph(query_node, depth_limit=None)
Get a subgraph that contains all nodes both upstream and downstream of the query node up to the given depth limit (if provided).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node from which the complete subgraph is constructed |
required |
depth_limit |
an integer representing the maximum depth to which the subgraph is constructed (optional) |
None
|
Returns:
Name | Type | Description |
---|---|---|
subgraph |
a directed graph (DiGraph) object containing all nodes both upstream and downstream of the query node, up to the given depth limit |
Source code in binn/importance_network.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
get_downstream_subgraph(graph, query_node, depth_limit=None)
Get a subgraph that contains all nodes downstream of the query node up to the given depth limit (if provided).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node from which the downstream subgraph is constructed |
required |
depth_limit |
an integer representing the maximum depth to which the subgraph is constructed (optional) |
None
|
Returns:
Name | Type | Description |
---|---|---|
subgraph |
a directed graph (DiGraph) object containing all nodes downstream of the query node, up to the given depth limit |
Source code in binn/importance_network.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
get_fan_in(query_node)
Get the number of incoming edges (fan-in) for the query node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node |
required |
Returns:
Type | Description |
---|---|
the number of incoming edges (fan-in) for the query node |
Source code in binn/importance_network.py
275 276 277 278 279 280 281 282 283 284 285 |
|
get_fan_out(query_node)
Get the number of outgoing edges (fan-out) for the query node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node |
required |
Returns:
Type | Description |
---|---|
the number of outgoing edges (fan-out) for the query node |
Source code in binn/importance_network.py
287 288 289 290 291 292 293 294 295 296 297 |
|
get_nr_nodes_in_downstream_subgraph(query_node)
Get the number of nodes in the downstream subgraph of the query node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node from which the downstream subgraph is constructed |
required |
Returns:
Type | Description |
---|---|
the number of nodes in the downstream subgraph of the query node |
Source code in binn/importance_network.py
262 263 264 265 266 267 268 269 270 271 272 273 |
|
get_nr_nodes_in_upstream_subgraph(query_node)
Get the number of nodes in the upstream subgraph of the query node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node from which the upstream subgraph is constructed |
required |
Returns:
Type | Description |
---|---|
the number of nodes in the upstream subgraph of the query node |
Source code in binn/importance_network.py
248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
get_upstream_subgraph(query_node, depth_limit=None)
Get a subgraph that contains all nodes upstream of the query node up to the given depth limit (if provided).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
a string representing the name of the node from which the upstream subgraph is constructed |
required |
depth_limit |
an integer representing the maximum depth to which the subgraph is constructed (optional) |
None
|
Returns:
Name | Type | Description |
---|---|---|
subgraph |
a directed graph (DiGraph) object containing all nodes upstream of the query node, up to the given depth limit |
Source code in binn/importance_network.py
201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
plot_complete_sankey(multiclass=False, show_top_n=10, node_cmap='Reds', edge_cmap='Reds', savename='sankey.png', width=1900, scale=2, height=800)
Plot a complete Sankey diagram for the importance network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multiclass |
bool, optional If True, plot multiclass Sankey diagram. Defaults to False. |
False
|
|
show_top_n |
int, optional Show only the top N nodes in the Sankey diagram. Defaults to 10. |
10
|
|
node_cmap |
str, optional The color map for the nodes. Defaults to "Reds". |
'Reds'
|
|
edge_cmap |
str or list, optional The color map for the edges. Defaults to "Reds". |
'Reds'
|
|
savename |
str, optional The filename to save the plot. Defaults to "sankey.png". |
'sankey.png'
|
Returns:
Type | Description |
---|---|
plotly.graph_objs._figure.Figure: The plotly Figure object representing the Sankey diagram. |
Source code in binn/importance_network.py
102 103 104 105 106 107 108 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 |
|
plot_subgraph_sankey(query_node, upstream=False, savename='sankey.png', val_col='value', cmap='coolwarm', width=1200, scale=2.5, height=500)
Generate a Sankey diagram using the provided query node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_node |
str
|
The node to use as the starting point for the Sankey diagram. |
required |
upstream |
bool
|
If True, the Sankey diagram will show the upstream flow of the query_node. If False (default), the Sankey diagram will show the downstream flow of the query_node. |
False
|
savename |
str
|
The file name to save the Sankey diagram as. Defaults to "sankey.png". |
'sankey.png'
|
val_col |
str
|
The column in the DataFrame that represents the value flow between nodes. Defaults to "value". |
'value'
|
cmap_name |
str
|
The name of the color map to use for the Sankey diagram. Defaults to "coolwarm". |
required |
Returns:
Type | Description |
---|---|
plotly.graph_objs._figure.Figure: The plotly Figure object representing the Sankey diagram. |
Source code in binn/importance_network.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 |
|