-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsort_plot.py
executable file
·82 lines (58 loc) · 2.37 KB
/
sort_plot.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
import plotly.plotly as py
import plotly.graph_objs as go
from os import path
from glob import glob
import pandas as pd
import argparse
from itertools import groupby
# Setup command line arguments and returns it
def setup_args():
# Description
parser = argparse.ArgumentParser(description="Generates plots in plotly based in CSV files from the project Sort Algorithms Compare.")
parser.add_argument('csv_path', metavar="CSV files folder path", type=str, help="The folder which the csv files generated by sortalgs executable are stored")
return parser.parse_args()
def get_data_type_from_csv(fileName):
return path.basename(fileName).split('.')[1]
def get_alg_name_from_csv(fileName):
return path.basename(fileName).split('.')[0]
def get_alg_name_from_data_frame(df):
for row in df.iterrows():
return row[1]["Algorithm Name"]
# Plot the graphs
def plot_graph(args):
csv_path = path.abspath(args.csv_path) + "/"
# Sort the files by data type to be grouped later
csv_files = sorted(glob(csv_path + "*.csv"), key=lambda str: get_data_type_from_csv(str))
# Group the files by data type
for data_type, grouped_files in groupby(csv_files, lambda str: get_data_type_from_csv(str)):
# Scatter plot lines
lines = []
# Sort the files by algorithm name
grouped_files = sorted(grouped_files, key=lambda str: get_alg_name_from_csv(str))
for file in grouped_files:
# Read from the csv
df = pd.read_csv(file)
# Create the plot lines
line = go.Scatter(
x = df["N"],
y = df["Nano Seconds"],
name = get_alg_name_from_data_frame(df) + " Sort",
mode = "lines"
)
lines.append(line)
# Create the layout
layout = go.Layout(
title = "Sort Algorithms Comparison with " + data_type.replace("_", " ") + "s",
xaxis = dict(title="N"),
yaxis = dict(title="Nanoseconds")
)
# Create the figure
fig = go.Figure(data=lines, layout=layout)
# Generates the lot in plotly
plotName = "Sort Algorithms Comparison: " + data_type
print("Creating {} plot...".format(plotName))
py.iplot(fig, filename = plotName)
if __name__ == '__main__':
args = setup_args()
plot_graph(args)