Selectors And Serialization
Selectors make graph queries explicit. They can be built from query strings, lists of ids, or neighborhood operations, and they are useful for analysis, filtering, exporting subsets, and handoff into visualization workflows.
Serialization preserves the graph and its attributes so data workflows can move between browser sessions, Node scripts, Python, native code, and Helios Web. For the complete format matrix and cross-language examples, see the data formats guide.
Select Nodes And Edges With Queries
This example writes a numeric score attribute with the chainable writer API,
then selects nodes and edges with query expressions. Query expressions can refer
to node attributes, edge attributes, and endpoint attributes.
Round Trip A Network Snapshot
ZXNet is the compact Helios network format. This example saves a graph to a byte array, restores it, and then exports Node-Link JSON for interoperability.
Handoff Into Helios Web
The Network and Web packages meet at the Helios constructor. A Network example
becomes visual when the graph object is passed to Helios Web.
import HeliosNetwork from "helios-network";
import { Helios } from "helios-web";
const network = await HeliosNetwork.fromZXNet(file);
const helios = new Helios(network, {
container: document.querySelector('#app'),
mode: '2d',
});
await helios.ready;
See Helios Web basic rendering for the rendered version of this workflow.
{"importMap": {"helios-web": "../vendor/helios/helios-web.es.js", "helios-network": "../vendor/helios/helios-network.js"}, "examples": [{"id": "examples--helios-network--selectors-serialization.md--example-1", "title": "Select Nodes And Edges With Queries", "render": false, "console": true, "autorun": true, "layout": "split", "width": null, "height": "440px", "headerName": null, "headerCode": "", "bodyName": "helios_network_selectors", "bodyCode": "import HeliosNetwork from \"helios-network\";\n\nconst network = await HeliosNetwork.create({ directed: true });\nconst nodes = network.addNodes(5);\n\nnetwork.addEdges([\n [nodes[0], nodes[1]],\n [nodes[1], nodes[2]],\n [nodes[2], nodes[3]],\n [nodes[3], nodes[4]],\n [nodes[0], nodes[4]],\n]);\n\nnetwork.nodeAttribute('score', [0.1, 0.4, 0.8, 0.7, 0.2]);\n\nconst highScoreNodes = network.selectNodes('score >= 0.5');\nconst outgoingFromHighScore = network.selectEdges('$src.score >= 0.5');\nconst seed = network.createNodeSelector([nodes[1]]);\nconst neighbors = seed.neighbors({ mode: 'out', includeEdges: true });\n\nconsole.log('score >= 0.5', Array.from(highScoreNodes));\nconsole.log('$src.score >= 0.5', Array.from(outgoingFromHighScore));\nconsole.log('neighbors from node 1', {\n nodes: Array.from(neighbors.nodes),\n edges: Array.from(neighbors.edges),\n});\nseed.dispose();", "headerRefs": [], "bodyRefs": []}, {"id": "examples--helios-network--selectors-serialization.md--example-2", "title": "Round Trip A Network Snapshot", "render": false, "console": true, "autorun": true, "layout": "split", "width": null, "height": "420px", "headerName": null, "headerCode": "", "bodyName": "helios_network_serialization", "bodyCode": "import HeliosNetwork from \"helios-network\";\n\nconst network = await HeliosNetwork.create({ directed: false });\nconst nodes = network.addNodes(6);\n\nnetwork.addEdges([\n [nodes[0], nodes[1]], [nodes[1], nodes[2]], [nodes[2], nodes[3]],\n [nodes[3], nodes[4]], [nodes[4], nodes[5]], [nodes[5], nodes[0]],\n]);\nnetwork.nodeAttribute('label', (_current, _id, ordinal) => `node-${ordinal}`);\n\nconst bytes = await network.saveZXNet({ format: 'uint8array', compressionLevel: 3 });\nconst restored = await HeliosNetwork.fromZXNet(bytes);\nconst nodeLink = await restored.saveNodeLinkJSON({ format: 'string' });\nconst parsed = JSON.parse(nodeLink);\n\nconsole.log(`zxnet bytes: ${bytes.byteLength}`);\nconsole.log({\n restoredNodes: restored.nodeCount,\n restoredEdges: restored.edgeCount,\n nodeLinkNodes: parsed.nodes.length,\n nodeLinkLinks: parsed.links.length,\n});", "headerRefs": [], "bodyRefs": []}]}