Skip to content

Tips and Tricks

Temporary storage of textsearches or graphs

In the research process it can make sense to temporarily store text searches or the creation of complicated graphs. This way, one can focus on the creation of images, without having to repeat the search process. The graph data and the search terms have to stay the same for this to work.

This can be done in a few lines:

  1. Make sure that there is a graph file at the specified path. In this case, this is a .gexf file, which makes sense for the process.
  2. If there is no graph file at the path, create a graph with all its data and write it to the graph file at the specified path.
  3. If there is a graph file at the path, read in the file.
from pathlib import Path
import networkx as nx

pth = Path("./my_graph.gexf")
if pth.exists():
    graph = nx.read_gexf(pth)
else:
    graph = import_your_very_important_graph("snapshot_1234.csv")
    txts = TextSearch("my_slug", token=ENV["MY_TOKEN"], timeout=280)
    txts.search(graph, vocab, summarize=True)
    nx.write_gexf(graph, pth)

The two functions nx.read_gexf() and nx.write_gexf() are central. They read / write the file with the path pth. If pth already exists, the file is read in, otherwise the graph is created.

If you want to force a new calculation, manually delete the graph file from the folder (at the path pth).

Setting color of edges in relation to the degree of the node they are pointing to

If you want to highlight certain edges or make edges visible, you can use the following approach:

import dsslab.net_bench as dnb
# ... import your graph

ig = dnb.ImageGenerator(graph)
# ... set positions
ig.edges.set_colors("grey").set_alphas(
    dnb.from_node(sequential("degree", "lin", out_range=(0, 1)), "incoming")
)

In this example we set the colors of all edges to grey and vary the alpha value. This makes a edge more transparent (value closer to 0) the lower the degree of the node it is pointing towards (incoming).

While a similar result can be produced through mapping color values directly on the edge with a colormap this might result overlaps where a lower valued edge is above a higher valued one and thus interrupts it. Try it out for your usecase:

ig = dnb.ImageGenerator(graph)
# ... set positions
ig.edges.set_colors(
    dnb.from_node(sequential("degree", "lin", "incoming", cmap="binary",)
)

In this example we use the binary colormap, traversing from white to black. Higher values of degree will thus create a darker edge.