|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Collections in PyGraphistry\n", |
| 8 | + "\n", |
| 9 | + "Collections define labeled subsets of a graph (nodes, edges, or subgraphs) using full GFQL. They enable advanced, layered styling that overrides base encodings when you need precise highlights.\n", |
| 10 | + "\n", |
| 11 | + "Use collections when you want:\n", |
| 12 | + "- baseline encodings (for example, by entity type) plus overlays for alerts or critical paths\n", |
| 13 | + "- multiple overlapping highlights with a priority order\n", |
| 14 | + "- a UI panel for toggling focused subsets on and off\n", |
| 15 | + "\n", |
| 16 | + "Collections are evaluated in priority order, with higher priority collections overriding lower ones for styling.\n" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "In this notebook, we build sets using GFQL AST helpers, combine them with intersections, and apply node and edge colors. Collections can be based on nodes, edges, or multi-step graph traversals (Chain).\n" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "metadata": {}, |
| 29 | + "execution_count": null, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "from pathlib import Path\n", |
| 33 | + "import pandas as pd\n", |
| 34 | + "import graphistry\n", |
| 35 | + "from graphistry import collection_set, collection_intersection, n, e_forward, Chain\n", |
| 36 | + "\n", |
| 37 | + "edges = pd.read_csv(Path('demos/data/honeypot.csv'))\n", |
| 38 | + "g = graphistry.edges(edges, \"attackerIP\", \"victimIP\")\n" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "metadata": {}, |
| 44 | + "execution_count": null, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "# Use Chain to select subgraphs (nodes + edges) by edge attributes\n", |
| 48 | + "collections = [\n", |
| 49 | + " collection_set(\n", |
| 50 | + " expr=Chain([n(), e_forward({\"vulnName\": \"MS08067 (NetAPI)\"}), n()]),\n", |
| 51 | + " id='netapi',\n", |
| 52 | + " name='MS08067 (NetAPI)',\n", |
| 53 | + " node_color='#00BFFF',\n", |
| 54 | + " edge_color='#00BFFF',\n", |
| 55 | + " ),\n", |
| 56 | + " collection_set(\n", |
| 57 | + " expr=Chain([n(), e_forward({\"victimPort\": 445.0}), n()]),\n", |
| 58 | + " id='port445',\n", |
| 59 | + " name='Port 445',\n", |
| 60 | + " node_color='#32CD32',\n", |
| 61 | + " edge_color='#32CD32',\n", |
| 62 | + " ),\n", |
| 63 | + " collection_intersection(\n", |
| 64 | + " sets=['netapi', 'port445'],\n", |
| 65 | + " name='NetAPI + 445',\n", |
| 66 | + " node_color='#AABBCC',\n", |
| 67 | + " edge_color='#AABBCC',\n", |
| 68 | + " ),\n", |
| 69 | + "]\n", |
| 70 | + "\n", |
| 71 | + "g2 = g.collections(\n", |
| 72 | + " collections=collections,\n", |
| 73 | + " show_collections=True,\n", |
| 74 | + " collections_global_node_color='CCCCCC',\n", |
| 75 | + " collections_global_edge_color='CCCCCC',\n", |
| 76 | + ")\n", |
| 77 | + "\n", |
| 78 | + "g2._url_params\n" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "metadata": {}, |
| 84 | + "execution_count": null, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "# Render (requires graphistry.register(...))\n", |
| 88 | + "g2.plot()\n" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Notes and validation\n", |
| 96 | + "\n", |
| 97 | + "- Order matters: earlier collections override later ones.\n", |
| 98 | + "- Use collections for priority-based subsets and overlaps; use `encode_*` for simple column-driven colors.\n", |
| 99 | + "- Helper constructors: `graphistry.collection_set(...)` and `graphistry.collection_intersection(...)` return JSON-friendly dicts (AST inputs wrap to `gfql_chain`).\n", |
| 100 | + "- Provide `id` for sets used by intersections.\n", |
| 101 | + "- Global colors apply to nodes/edges not in any collection; `#` is optional.\n", |
| 102 | + "- Use `validate='strict'` to raise, or `warn=False` to silence warnings.\n", |
| 103 | + "\n", |
| 104 | + "Wire protocol and pre-encoded strings:\n", |
| 105 | + "\n", |
| 106 | + "```python\n", |
| 107 | + "collections_wire = [\n", |
| 108 | + " {\n", |
| 109 | + " \"type\": \"set\",\n", |
| 110 | + " \"name\": \"Wire Protocol Example\",\n", |
| 111 | + " \"node_color\": \"#AA00AA\",\n", |
| 112 | + " \"expr\": {\n", |
| 113 | + " \"type\": \"gfql_chain\",\n", |
| 114 | + " \"gfql\": [\n", |
| 115 | + " {\"type\": \"Node\", \"filter_dict\": {\"status\": \"purchased\"}}\n", |
| 116 | + " ]\n", |
| 117 | + " }\n", |
| 118 | + " }\n", |
| 119 | + "]\n", |
| 120 | + "g.collections(collections=collections_wire)\n", |
| 121 | + "\n", |
| 122 | + "g.collections(collections=encoded_collections, encode=False)\n", |
| 123 | + "```\n" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "Run `g2.plot()` in a notebook session with valid credentials to render inline.\n" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": {}, |
| 136 | + "source": [ |
| 137 | + "## Overlap priority example\n", |
| 138 | + "\n", |
| 139 | + "Earlier collections override later ones when they overlap.\n", |
| 140 | + "\n", |
| 141 | + "```python\n", |
| 142 | + "collections_priority = [\n", |
| 143 | + " collection_set(\n", |
| 144 | + " expr=Chain([n(), e_forward({\"vulnName\": \"MS08067 (NetAPI)\"}), n()]),\n", |
| 145 | + " id=\"netapi\",\n", |
| 146 | + " name=\"MS08067 (NetAPI)\",\n", |
| 147 | + " node_color=\"#FFAA00\",\n", |
| 148 | + " edge_color=\"#FFAA00\",\n", |
| 149 | + " ),\n", |
| 150 | + " collection_set(\n", |
| 151 | + " expr=Chain([n(), e_forward({\"victimPort\": 445.0}), n()]),\n", |
| 152 | + " id=\"port445\",\n", |
| 153 | + " name=\"Port 445\",\n", |
| 154 | + " node_color=\"#00BFFF\",\n", |
| 155 | + " edge_color=\"#00BFFF\",\n", |
| 156 | + " ),\n", |
| 157 | + "]\n", |
| 158 | + "g.collections(collections=collections_priority)\n", |
| 159 | + "```\n" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "metadata": {}, |
| 165 | + "source": [ |
| 166 | + "For more on color encodings, see the [Color encodings notebook](encodings-colors.ipynb).\n" |
| 167 | + ] |
| 168 | + } |
| 169 | + ], |
| 170 | + "metadata": { |
| 171 | + "kernelspec": { |
| 172 | + "display_name": "Python 3", |
| 173 | + "language": "python", |
| 174 | + "name": "python3" |
| 175 | + }, |
| 176 | + "language_info": { |
| 177 | + "name": "python", |
| 178 | + "version": "3.x" |
| 179 | + } |
| 180 | + }, |
| 181 | + "nbformat": 4, |
| 182 | + "nbformat_minor": 5 |
| 183 | +} |
0 commit comments