# Graph Export Module - Complete Guide ## Overview The Graph Export module provides powerful tools for exporting vector similarity graphs to multiple formats for visualization, analysis, and graph database integration. ## Supported Formats | Format | Description | Use Cases | |--------|-------------|-----------| | **GraphML** | XML-based graph format | Gephi, yEd, NetworkX, igraph, Cytoscape | | **GEXF** | Graph Exchange XML Format | Gephi visualization (recommended) | | **Neo4j** | Cypher queries | Graph database import and queries | | **D3.js** | JSON for web visualization | Interactive web-based force graphs | | **NetworkX** | Python graph library format | Network analysis in Python | ## Quick Examples ### 1. Basic Export to All Formats ```typescript import { buildGraphFromEntries, exportGraph } from 'ruvector-extensions'; const entries = [ { id: 'doc1', vector: [0.1, 0.2, 0.3], metadata: { title: 'AI' } }, { id: 'doc2', vector: [0.15, 0.25, 0.35], metadata: { title: 'ML' } }, { id: 'doc3', vector: [0.8, 0.1, 0.05], metadata: { title: 'History' } } ]; const graph = buildGraphFromEntries(entries, { maxNeighbors: 5, threshold: 0.7 }); // Export to different formats const graphml = exportGraph(graph, 'graphml'); const gexf = exportGraph(graph, 'gexf'); const neo4j = exportGraph(graph, 'neo4j'); const d3 = exportGraph(graph, 'd3'); const networkx = exportGraph(graph, 'networkx'); ``` ### 2. GraphML Export for Gephi ```typescript import { exportToGraphML } from 'ruvector-extensions'; import { writeFile } from 'fs/promises'; const graphml = exportToGraphML(graph, { graphName: 'Document Similarity Network', includeMetadata: true, includeVectors: false }); await writeFile('network.graphml', graphml); ``` **Import into Gephi:** 1. Open Gephi 2. File → Open → Select `network.graphml` 3. Choose "Undirected" or "Directed" graph 4. Apply layout (ForceAtlas2 recommended) 5. Analyze with built-in metrics ### 3. GEXF Export for Advanced Gephi Features ```typescript import { exportToGEXF } from 'ruvector-extensions'; const gexf = exportToGEXF(graph, { graphName: 'Knowledge Graph', graphDescription: 'Vector embeddings similarity network', includeMetadata: true }); await writeFile('network.gexf', gexf); ``` **Gephi Workflow:** - Import the GEXF file - Use Statistics panel for centrality measures - Apply community detection (Modularity) - Color nodes by cluster - Size nodes by degree centrality - Export as PNG/SVG for publications ### 4. Neo4j Graph Database ```typescript import { exportToNeo4j } from 'ruvector-extensions'; const cypher = exportToNeo4j(graph, { includeVectors: true, includeMetadata: true }); await writeFile('import.cypher', cypher); ``` **Import into Neo4j:** ```bash # Option 1: Neo4j Browser # Copy and paste the Cypher queries # Option 2: cypher-shell cypher-shell -f import.cypher # Option 3: Node.js driver import neo4j from 'neo4j-driver'; const driver = neo4j.driver('bolt://localhost:7687'); const session = driver.session(); await session.run(cypher); ``` **Query Examples:** ```cypher // Find most similar vectors MATCH (v:Vector)-[r:SIMILAR_TO]->(other:Vector) WHERE v.id = 'doc1' RETURN other.label, r.weight ORDER BY r.weight DESC LIMIT 5; // Find communities CALL gds.louvain.stream('myGraph') YIELD nodeId, communityId RETURN gds.util.asNode(nodeId).label AS node, communityId; // Path finding MATCH path = shortestPath( (a:Vector {id: 'doc1'})-[*]-(b:Vector {id: 'doc10'}) ) RETURN path; ``` ### 5. D3.js Web Visualization ```typescript import { exportToD3 } from 'ruvector-extensions'; const d3Data = exportToD3(graph, { includeMetadata: true }); // Save for web app await writeFile('public/graph-data.json', JSON.stringify(d3Data)); ``` **HTML Visualization:** ```html
``` ### 6. NetworkX Python Analysis ```typescript import { exportToNetworkX } from 'ruvector-extensions'; const nxData = exportToNetworkX(graph); await writeFile('graph.json', JSON.stringify(nxData, null, 2)); ``` **Python Analysis:** ```python import json import networkx as nx import matplotlib.pyplot as plt import numpy as np # Load graph with open('graph.json', 'r') as f: data = json.load(f) G = nx.node_link_graph(data) print(f"Nodes: {G.number_of_nodes()}") print(f"Edges: {G.number_of_edges()}") print(f"Density: {nx.density(G):.4f}") # Centrality analysis degree_cent = nx.degree_centrality(G) between_cent = nx.betweenness_centrality(G) close_cent = nx.closeness_centrality(G) eigen_cent = nx.eigenvector_centrality(G) # Community detection communities = nx.community.louvain_communities(G) print(f"\nFound {len(communities)} communities") # Visualize plt.figure(figsize=(12, 8)) pos = nx.spring_layout(G, k=0.5, iterations=50) # Color by community color_map = [] for node in G: for i, comm in enumerate(communities): if node in comm: color_map.append(i) break nx.draw(G, pos, node_color=color_map, node_size=[v * 1000 for v in degree_cent.values()], cmap=plt.cm.rainbow, with_labels=True, font_size=8, edge_color='gray', alpha=0.7) plt.title('Network Graph with Communities') plt.savefig('network.png', dpi=300, bbox_inches='tight') # Export metrics metrics = { 'node': list(G.nodes()), 'degree_centrality': [degree_cent[n] for n in G.nodes()], 'betweenness_centrality': [between_cent[n] for n in G.nodes()], 'closeness_centrality': [close_cent[n] for n in G.nodes()], 'eigenvector_centrality': [eigen_cent[n] for n in G.nodes()] } import pandas as pd df = pd.DataFrame(metrics) df.to_csv('network_metrics.csv', index=False) print("\nMetrics exported to network_metrics.csv") ``` ## Streaming Exports for Large Graphs When dealing with millions of nodes, use streaming exporters: ### GraphML Streaming ```typescript import { GraphMLStreamExporter } from 'ruvector-extensions'; import { createWriteStream } from 'fs'; const stream = createWriteStream('large-graph.graphml'); const exporter = new GraphMLStreamExporter(stream, { graphName: 'Large Network' }); await exporter.start(); // Add nodes in batches for (const batch of nodeBatches) { for (const node of batch) { await exporter.addNode(node); } console.log(`Processed ${batch.length} nodes`); } // Add edges for (const batch of edgeBatches) { for (const edge of batch) { await exporter.addEdge(edge); } } await exporter.end(); stream.close(); ``` ### D3.js Streaming ```typescript import { D3StreamExporter } from 'ruvector-extensions'; const stream = createWriteStream('large-d3-graph.json'); const exporter = new D3StreamExporter(stream); await exporter.start(); // Process in chunks for await (const node of nodeIterator) { await exporter.addNode(node); } for await (const edge of edgeIterator) { await exporter.addEdge(edge); } await exporter.end(); ``` ## Configuration Options ### Export Options ```typescript interface ExportOptions { includeVectors?: boolean; // Include embeddings (default: false) includeMetadata?: boolean; // Include node attributes (default: true) maxNeighbors?: number; // Max edges per node (default: 10) threshold?: number; // Min similarity (default: 0.0) graphName?: string; // Graph title graphDescription?: string; // Graph description streaming?: boolean; // Enable streaming mode attributeMapping?: Record