cfs-energy / cfspopcon

POPCONs (Plasma OPerating CONtours)
https://cfspopcon.readthedocs.io/en/latest/
MIT License
22 stars 14 forks source link

DAG representation of algorithm dependencies #92

Open tbody-cfs opened 2 months ago

tbody-cfs commented 2 months ago

Might be a useful way of representing interdependencies of algorithms

https://wimyedema.medium.com/drawing-dags-5cadcb452115 https://wimyedema.github.io/dagviz/

tbody-cfs commented 1 month ago

This seems to be more of a visualisation challenge than a compute challenge. For instance, both of the following work, but the result is very difficult to understand

image

Option 1 — using pyvis

from importlib.resources import as_file, files
from cfspopcon import Algorithm
import yaml

with as_file(files("cfspopcon").joinpath("variables.yaml")) as filepath:
    with open(filepath) as f:
        variables_dict = yaml.safe_load(f)

from pyvis.network import Network

net = Network()

for var_key in variables_dict.keys():
    net.add_node(var_key, color="blue")

for alg_key in Algorithm.instances.keys():
    net.add_node(alg_key, color="red")

for alg_key, alg in Algorithm.instances.items():
    for var_key in alg.input_keys:
        net.add_edge(var_key, alg_key)

    for var_key in alg.return_keys:
        net.add_edge(alg_key, var_key)

net.save_graph("example.html")

Option 2 — using NetworkX

import networkx as nx

from importlib.resources import as_file, files
from cfspopcon import Algorithm
import yaml

with as_file(files("cfspopcon").joinpath("variables.yaml")) as filepath:
    with open(filepath) as f:
        variables_dict = yaml.safe_load(f)

G = nx.DiGraph()

for var_key in variables_dict.keys():
    G.add_node(var_key)

for alg_key in Algorithm.instances.keys():
    G.add_node(alg_key)

for alg_key, alg in Algorithm.instances.items():

    for var_key in alg.input_keys:
        G.add_edge(var_key, alg_key)

    for var_key in alg.return_keys:
        G.add_edge(alg_key, var_key)

nx.draw(G)

image

tbody-cfs commented 1 month ago

There are a lot of drawing options available at https://networkx.org/documentation/stable/auto_examples/drawing/index.html. Someone could experiment with these