1+ import json
2+ import pandas as pd
3+ import matplotlib .pyplot as plt
4+ import numpy as np
5+
6+
7+ def label_selector (file_path :str ):
8+ if file_path == 'erc20' :
9+ return "ERC20"
10+ else :
11+ return "SoK"
12+
13+
14+ def RQ1 (file_path ):
15+
16+ # Load data from JSON file
17+ with open (file_path , 'r' ) as f :
18+ data = json .load (f )
19+
20+ file_path = file_path .removeprefix ("./" )
21+ file_path = file_path .removesuffix (".json" )
22+ # Flatten nested JSON structure
23+ flat_data = []
24+ for item in data :
25+ if isinstance (item , list ): # Handle list of lists
26+ flat_data .extend (item )
27+ else :
28+ flat_data .append (item )
29+
30+ # Convert to DataFrame
31+ df = pd .DataFrame (flat_data )
32+
33+ # Calculate total and average times
34+ total_parsing_time = df ['total_parsing_time' ].sum ()
35+ total_reduction_time = df ['total_reduction_time' ].sum ()
36+ average_parsing_time = df ['total_parsing_time' ].mean ()
37+ average_reduction_time = df ['total_reduction_time' ].mean ()
38+ average_reduction_ratio = round (df ['reduction_ratio' ].mean (),2 )
39+ predicates_removed = df ["predicates_before_reduction" ].sum () - df ["predicates_after_reduction" ].sum ()
40+ average_predicates_removed = predicates_removed / len (df )
41+
42+ # Print calculated values
43+ print (f"Total Parsing Time: { total_parsing_time } " )
44+ print (f"Total Reduction Time: { total_reduction_time } " )
45+ print (f"Average Parsing Time: { average_parsing_time } " )
46+ print (f"Average Reduction Time: { average_reduction_time } " )
47+ print (f"Average Reduction Ratio: { average_reduction_ratio } " )
48+ print (f"Average predicates removed: { average_predicates_removed } " )
49+ print (f"Total predicates removed: { predicates_removed } " )
50+
51+ # Generate histogram for reduction ratio with custom ticks and no grid
52+ num_bins = 4
53+ counts , bins , patches = plt .hist (df ['reduction_ratio' ], bins = num_bins , edgecolor = 'black' , alpha = 0.8 , align = 'mid' )
54+
55+ # Set the x-ticks to be at the center of each bin
56+ bin_centers = 0.5 * (bins [1 :] + bins [:- 1 ])
57+ plt .xticks (bin_centers , [f"{ center :.2f} " for center in bin_centers ])
58+
59+
60+
61+ # Set the x-ticks to be at the center of each bin, with 0.65 instead of 0.66
62+ bin_centers = 0.5 * (bins [1 :] + bins [:- 1 ])
63+ custom_ticks = [f"{ center :.2f} " for center in bin_centers ]
64+ if file_path == 'sok' :
65+ custom_ticks [0 ] = '0.65' # Change the first tick to 0.65
66+
67+ plt .xticks (bin_centers , custom_ticks )
68+
69+ # Set labels and title
70+ plt .xlabel ('Reduction Ratio' , fontsize = 14 )
71+ plt .ylabel ('Frequency' ,fontsize = 14 )
72+ # plt.title(f'Histogram of Reduction Ratios {label_selector(file_path)} for Smart Contract', fontsize=16)
73+
74+ # Turn off the grid
75+ plt .grid (False )
76+
77+ # Save and show the plot
78+ plt .savefig (f'reduction_ratio_histogram_{ file_path } .pdf' , format = 'pdf' , dpi = 300 )
79+ plt .clf ()
80+
81+
82+ if __name__ == "__main__" :
83+ RQ1 ("./erc20.json" )
84+ # print("_______________________________________________________________")
85+ RQ1 ("./sok.json" )
0 commit comments