-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_export.py
More file actions
170 lines (137 loc) · 6.39 KB
/
data_export.py
File metadata and controls
170 lines (137 loc) · 6.39 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
import json
import os
import pandas as pd
import traceback
def read_json_files(dataset, dataset_path, method):
"""
Read JSON files from the dataset directory and filter by method.
Args:
dataset: Dataset name
dataset_path: Path to dataset directory
method: Either "search" or "build"
Yields:
Tuple of (file_path, algorithm_name, dataframe)
"""
dir_path = os.path.join(dataset_path, dataset)
for file in os.listdir(dir_path):
if file.endswith(".json"):
# Filter files based on method
if method == "search" and "throughput" in file:
# Only process search files (containing throughput)
file_path = os.path.join(dir_path, file)
try:
with open(file_path, "r", encoding="ISO-8859-1") as f:
data = json.load(f)
df = pd.DataFrame(data["benchmarks"])
algo_name = tuple(file.split(",")[:2])
yield file_path, algo_name, df
except Exception as e:
print(f"Error processing search file {file_path}: {e}. Skipping...")
elif method == "build" and "throughput" not in file and "base.json" in file:
# Only process build files (base.json files without throughput)
file_path = os.path.join(dir_path, file)
try:
with open(file_path, "r", encoding="ISO-8859-1") as f:
data = json.load(f)
df = pd.DataFrame(data["benchmarks"])
algo_name = tuple(file.split(",")[:2])
yield file_path, algo_name, df
except Exception as e:
print(f"Error processing build file {file_path}: {e}. Skipping...")
def clean_algo_name(algo_name):
"""Clean and format the algorithm name."""
return "_".join(algo_name)
def write_csv(file, algo_name, df, extra_columns=None, skip_cols=None):
"""Write DataFrame to CSV file with optional extra columns."""
algo_name_clean = algo_name[0] if isinstance(algo_name, tuple) else algo_name
# Extract algo and index name from the full name (format: "algo/index")
split_names = df["name"].str.split("/", n=1, expand=True)
if len(split_names.columns) > 1:
index_from_name = split_names[1]
else:
index_from_name = split_names[0]
write_data = pd.DataFrame(
{
"algo_name": [algo_name_clean] * len(df),
"index_name": index_from_name.values,
"time": df["real_time"].values,
}
)
if extra_columns:
for col in extra_columns:
if col in df.columns:
write_data[col] = df[col].values
if skip_cols:
write_data = write_data.drop(columns=skip_cols)
write_data.to_csv(file, index=False)
def convert_json_to_csv_build(dataset, dataset_path):
"""Convert build JSON files to CSV format."""
for file, algo_name, df in read_json_files(dataset, dataset_path, "build"):
try:
algo_name_clean = algo_name[0] # Just the algorithm name, not the group
build_file = os.path.join(dataset_path, dataset, f"{algo_name_clean},base.csv")
write_csv(build_file, algo_name, df)
except Exception as e:
print(f"Error processing build file {file}: {e}. Skipping...")
def convert_json_to_csv_search(dataset, dataset_path):
"""Convert search JSON files to CSV format with Pareto frontier analysis."""
for file, algo_name, df in read_json_files(dataset, dataset_path, "search"):
try:
algo_name_clean = algo_name[0] # Just the algorithm name, not the group
# Extract index name from full name (format: "algo/index")
split_names = df["name"].str.split("/", n=1, expand=True)
if len(split_names.columns) > 1:
index_names = split_names[1]
else:
index_names = split_names[0]
write = pd.DataFrame(
{
"algo_name": [algo_name_clean] * len(df),
"index_name": index_names.values,
"recall": df["Recall"].values,
"throughput": df["items_per_second"].values,
"latency": df["Latency"].values,
}
)
# Write raw data
raw_file = os.path.join(dataset_path, dataset, f"{algo_name_clean},base,raw.csv")
write.to_csv(raw_file, index=False)
# Calculate Pareto frontier for throughput
throughput_frontier = get_frontier(write, "throughput")
throughput_file = os.path.join(dataset_path, dataset, f"{algo_name_clean},base,throughput.csv")
throughput_frontier.to_csv(throughput_file, index=False)
# Calculate Pareto frontier for latency
latency_frontier = get_frontier(write, "latency")
latency_file = os.path.join(dataset_path, dataset, f"{algo_name_clean},base,latency.csv")
latency_frontier.to_csv(latency_file, index=False)
except Exception as e:
print(f"Error processing search file {file}: {e}. Skipping...")
def create_pointset(data, xn, yn):
"""Create Pareto frontier point set."""
metrics = {
"k-nn": {"worst": float("-inf")},
"throughput": {"worst": float("-inf")},
"latency": {"worst": float("inf")},
}
xm, ym = metrics[xn], metrics[yn]
y_col = 4 if yn == "latency" else 3
rev_x = -1 if xm["worst"] < 0 else 1
rev_y = -1 if ym["worst"] < 0 else 1
data.sort(key=lambda t: (rev_y * t[y_col], rev_x * t[2]))
lines = []
last_x = xm["worst"]
comparator = (lambda xv, lx: xv > lx) if last_x < 0 else (lambda xv, lx: xv < lx)
for d in data:
if comparator(d[2], last_x):
last_x = d[2]
lines.append(d)
return lines
def get_frontier(df, metric):
"""Calculate Pareto frontier for given metric."""
lines = create_pointset(df.values.tolist(), "k-nn", metric)
return pd.DataFrame(lines, columns=df.columns)
def write_frontier(file, write_data, metric):
"""Write Pareto frontier data to CSV file."""
frontier_data = get_frontier(write_data, metric)
frontier_data.to_csv(file, index=False)