duypro247 commited on
Commit
8bdbf0c
·
1 Parent(s): 7aa497a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import networkx as nx
3
+ import pydot
4
+ import pandas as pd
5
+ from io import BytesIO
6
+
7
+ def calculate_parameters(file):
8
+ # Parse dot file using pydot
9
+ graphs = pydot.graph_from_dot_file(file.name)
10
+ G = nx.nx_pydot.from_pydot(graphs[0])
11
+
12
+ # Initialize the list of lengths and the node-to-index map
13
+ all_lengths = [0] * len(G.nodes())
14
+ node_to_index = {node: i for i, node in enumerate(G.nodes())}
15
+
16
+ # Calculate absolute depth (Dabs) and depth of each node
17
+ for node in nx.topological_sort(G):
18
+ if G.in_degree(node) > 0: # This node has a predecessor
19
+ all_lengths[node_to_index[node]] = max(all_lengths[node_to_index[n]]+1 for n in G.predecessors(node))
20
+ Dabs = sum(all_lengths)
21
+
22
+ # Create node depth dictionary
23
+ node_depth = {node: all_lengths[node_to_index[node]] for node in G.nodes()}
24
+
25
+ # Calculate maximum depth (Dmax)
26
+ Dmax = max(all_lengths)
27
+
28
+ # Calculate average depth (Davg)
29
+ Davg = Dabs / len(all_lengths)
30
+
31
+ # Calculate absolute width (Wabs)
32
+ Wabs = len(G.nodes())
33
+
34
+ # Calculate maximum width (Wmax)
35
+ level_count = [all_lengths.count(i) for i in set(all_lengths)]
36
+ Wmax = max(level_count)
37
+
38
+ # Calculate average width (Wavg)
39
+ Wavg = Wabs / len(set(all_lengths))
40
+
41
+ # Create a DataFrame for node depths
42
+ df = pd.DataFrame.from_dict(node_depth, orient='index', columns=['Depth'])
43
+ node_depth_str = df.to_string()
44
+
45
+ result = f"Node Depths:\n{node_depth_str}\n\nFinal Calculations:\n"
46
+ result += f"Dabs = {Dabs}, Dmax = {Dmax}, Davg = {Davg:.3f}\n"
47
+ result += f"Wabs = {Wabs}, Wmax = {Wmax}, Wavg = {Wavg:.3f}"
48
+
49
+ return result
50
+
51
+ iface = gr.Interface(fn=calculate_parameters, inputs="file", outputs="text")
52
+ iface.launch()