File size: 1,738 Bytes
8bdbf0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
import gradio as gr
import networkx as nx
import pydot
import pandas as pd
from io import BytesIO

def calculate_parameters(file):
    # Parse dot file using pydot
    graphs = pydot.graph_from_dot_file(file.name)
    G = nx.nx_pydot.from_pydot(graphs[0])

    # Initialize the list of lengths and the node-to-index map
    all_lengths = [0] * len(G.nodes())
    node_to_index = {node: i for i, node in enumerate(G.nodes())}

    # Calculate absolute depth (Dabs) and depth of each node
    for node in nx.topological_sort(G):
        if G.in_degree(node) > 0:  # This node has a predecessor
            all_lengths[node_to_index[node]] = max(all_lengths[node_to_index[n]]+1 for n in G.predecessors(node))
    Dabs = sum(all_lengths)

    # Create node depth dictionary
    node_depth = {node: all_lengths[node_to_index[node]] for node in G.nodes()}

    # Calculate maximum depth (Dmax)
    Dmax = max(all_lengths)

    # Calculate average depth (Davg)
    Davg = Dabs / len(all_lengths)

    # Calculate absolute width (Wabs)
    Wabs = len(G.nodes())

    # Calculate maximum width (Wmax)
    level_count = [all_lengths.count(i) for i in set(all_lengths)]
    Wmax = max(level_count)

    # Calculate average width (Wavg)
    Wavg = Wabs / len(set(all_lengths))

    # Create a DataFrame for node depths
    df = pd.DataFrame.from_dict(node_depth, orient='index', columns=['Depth'])
    node_depth_str = df.to_string()

    result = f"Node Depths:\n{node_depth_str}\n\nFinal Calculations:\n"
    result += f"Dabs = {Dabs}, Dmax = {Dmax}, Davg = {Davg:.3f}\n"
    result += f"Wabs = {Wabs}, Wmax = {Wmax}, Wavg = {Wavg:.3f}"

    return result

iface = gr.Interface(fn=calculate_parameters, inputs="file", outputs="text")
iface.launch()