mercury.graph.viz
mercury.graph.viz.Moebius(G)
Bases: MoebiusAnywidget
Moebius class for visualizing graphs using an anywidget.
Important: This class requires: anywidget, traitlets and IPython.display!! These packages are not mandatory for the rest of the library, since you may not be intereseted in visualizing graphs. If you want to use this class, you need to install these packages.
Usage
from mercury.graph.viz import Moebius
G = ... # A graph object
moebius = Moebius(G)
moebius.show()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
Graph
|
The graph to be visualized. |
required |
Source code in mercury/graph/viz/moebius.py
522 523 524 525 526 | |
__getitem__(item)
Add support for the [] operator.
Source code in mercury/graph/viz/moebius.py
129 130 131 132 133 134 | |
__str__()
Convert the object via str()
Source code in mercury/graph/viz/moebius.py
121 122 123 124 125 126 | |
_get_adjacent_nodes_moebius(node_id, limit=20, depth=1)
This is the callback function used to interact with the Moebius JS library. Each time you deploy a node in the graph, this function is called to get the adjacent nodes and edges.
The expected return is a JSON string with the following format:
json = { 'nodes': [ {'id' : ..., 'count' : ..., '_int_id' : ...}, {'id' : ..., 'count' : ..., '_int_id' : ...} ], 'links': [ {'source' : ..., 'target' : ..., '_int_id' : ...}, {'source' : ..., 'target' : ..., '_int_id' : ...} ] }, where 'count' is the degree of the given node, and '_int_id' is a unique integer identifier for the given node or edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The id of the node to get the adjacent nodes. |
required |
limit
|
int
|
The maximum number of nodes to be returned. |
20
|
depth
|
int
|
The depth of the graph to be returned. |
1
|
Returns:
| Type | Description |
|---|---|
str
|
A JSON string with the adjacent nodes and edges. |
Source code in mercury/graph/viz/moebius.py
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 | |
_get_one_level_subgraph_graphframes(node_id, _testing=False)
Get the one-level subgraph for a given node ID using GraphFrames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The ID of the node for which to get the one-level subgraph. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing two Spark DataFrames: - nodes_df: DataFrame with columns 'id', 'count', '_int_id', and any other node attributes. - edges_df: DataFrame with columns 'source', 'target', '_int_id' the edges connecting the nodes in the subgraph. |
Source code in mercury/graph/viz/moebius.py
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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
_get_one_level_subgraph_networkx(node_id)
Get the one-level subgraph for a given node ID using Networkx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The ID of the node for which to get the one-level subgraph. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing two Pandas DataFrames: - nodes_df: DataFrame with columns 'id', 'count', '_int_id', and any other node attributes. - edges_df: DataFrame with columns 'source', 'target', '_int_id' the edges connecting the nodes in the subgraph. |
Source code in mercury/graph/viz/moebius.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
_hsl_to_rgb(h, s, l)
Convert HSL (Hue, Saturation, Lightness) color space to RGB (Red, Green, Blue) color space.
Parameters: h (float): Hue value, should be between 0 and 1. s (float): Saturation value, should be between 0 and 1. l (float): Lightness value, should be between 0 and 1.
Returns: tuple: A tuple containing the RGB values (r, g, b), each ranging from 0 to 255.
Source code in mercury/graph/viz/moebius.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
_pd_to_json_format(df)
A utility to produced the flavor of JSON expected by the JS library from a pandas DataFrame.
Usage
# This simple snippet to convert the whole graph to the expected JSON format.
edges = self._pd_to_json_format(self.G.edges_as_pandas().rename({'src' : 'source', 'dst' : 'target'}, axis = 1))
nodes = self._pd_to_json_format(self.G.nodes_as_pandas())
json_final = {'nodes' : nodes, 'links' : edges}
return json.dumps(json_final, ensure_ascii = False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The DataFrame to be converted to JSON. |
required |
Source code in mercury/graph/viz/moebius.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
_repr_mimebundle_(include=None, exclude=None)
Override display() method to control its execution and avoid errors.
Source code in mercury/graph/viz/moebius.py
529 530 531 532 533 | |
generate_color_palette(cats, hue=0, sat=0.7, light=0.5)
Generates a color palette for the given categories. This can be used in combination with node_or_edge_config to generate
the dictionary expected by the colors argument with colors that cover the whole 0..1 hue range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cats
|
iterable
|
An iterable of categories for which the color palette is to be generated. |
required |
hue
|
float
|
The base hue that is added to all the colors in the color palette. It must be in range, 0..1, all the resulting hue values will be kept modulo 1.0. Default is 0 (no shift). |
0
|
sat
|
float
|
The saturation level for the colors. Default is 0.7. Range is 0..1. |
0.7
|
light
|
float
|
The lightness level for the colors. Default is 0.5. Range is 0..1. |
0.5
|
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary where keys are categories and values are hex color codes. |
Source code in mercury/graph/viz/moebius.py
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 | |
node_or_edge_config(text_is=None, color_is=None, colors=None, size_is=None, size_range=None, size_scale='linear')
Create a node_config or edge_config configuration dictionary for show() in an understandable way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text_is
|
str
|
The node/edge attribute to be displayed as text. Use the string |
None
|
color_is
|
str
|
A categorical node/edge attribute that can be represented as a color. This will also enable a legend interface where categories can be individually shown or hidden. |
None
|
colors
|
dict
|
The colors for each category defined as a dictionary. The keys are possible outcomes of category. The values are html RGB strings. E.g., .draw(category = 'size', colors = {'big' : '#c0a080', 'small' : '#a0c080'}) where 'big' and 'small' are possible values of the category 'size'. |
None
|
size_is
|
str
|
The node attribute to be displayed as the size of the nodes. Use the string |
None
|
size_range
|
List of two numbers
|
Combined with edge_label, this parameter controls the values in the variable that correspond to the minimum and maximum displayed sizes. The values below or equal the first value will be displayed with the base radius (that depends on the zoom) and the values above or equal to the second value will be shown with the maximum radius. |
None
|
size_scale
|
(linear, power, sqrt or log)
|
Combined with edge_label, the scale used to convert the value in the variable to the displayed radius. |
'linear'
|
Returns:
| Type | Description |
|---|---|
dict
|
The node configuration dictionary |
Source code in mercury/graph/viz/moebius.py
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 | |
show(initial_id=None, initial_depth=1, node_config=None, edge_config=None)
Start the interactive graph visualization using an anywidget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_id
|
str
|
The id of the node to start the visualization. |
None
|
initial_depth
|
int
|
The initial depth of the graph (starting with |
1
|
node_config
|
dict
|
A node configuration dictionary created by |
None
|
edge_config
|
dict
|
An edge configuration dictionary created by |
None
|
Source code in mercury/graph/viz/moebius.py
536 537 538 539 540 541 542 543 544 545 546 | |