File size: 1,626 Bytes
5e952bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
import os
import json
import networkx as nx

def export_graph_to_cytoscape_json(G: nx.MultiDiGraph, path: str) -> None:
    """Export the NetworkX DiGraph to Cytoscape.js compatible JSON format."""
    # Ensure parent directory exists
    os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
    
    # Generate cytoscape-compatible structure
    data = nx.readwrite.json_graph.cytoscape_data(G)
    
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2)

def export_graph_to_gexf(G: nx.MultiDiGraph, path: str) -> None:
    """Export the NetworkX DiGraph to GEXF format (for Gephi, visualization debugging).
    
    Converts list-valued attributes (like authors) to comma-separated strings to avoid
    GEXF writer validation errors.
    """
    # Ensure parent directory exists
    os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
    
    # Copy graph to avoid modifying original graph data
    G_export = G.copy()
    
    # Convert list attributes to strings, and None values to empty strings
    for _, attrs in G_export.nodes(data=True):
        for key, val in list(attrs.items()):
            if val is None:
                attrs[key] = ""
            elif isinstance(val, list):
                attrs[key] = ", ".join(map(str, val))
                
    for _, _, attrs in G_export.edges(data=True):
        for key, val in list(attrs.items()):
            if val is None:
                attrs[key] = ""
            elif isinstance(val, list):
                attrs[key] = ", ".join(map(str, val))
                
    nx.write_gexf(G_export, path)