-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_all_compression.py
More file actions
354 lines (277 loc) · 11.8 KB
/
Copy pathrun_all_compression.py
File metadata and controls
354 lines (277 loc) · 11.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
Run All Compression Methods and Generate CSV Report
This script runs all implemented compression algorithms on all test files
and generates a comprehensive CSV report comparing their performance.
The report includes:
- Original file sizes
- Compressed sizes for each method
- Compression ratios
- Compression times
- Best performing method for each file
"""
from compression_methods.burrows_wheeler import BWTCompressor
from compression_methods.arithmetic_compression import ArithmeticCompressor
from compression_methods.lzw_compression import LZWCompressor
from compression_methods.lz77_compression import LZ77Compressor
from compression_methods.huffman_compression import HuffmanCompressor
from compression_methods.rle_compression import RLECompressor
from utils.file_utils import (
get_all_test_files, get_file_size, format_size,
CompressionStats, calculate_compression_ratio
)
import os
import sys
import csv
import time
from typing import List, Dict, Any
# Add utils to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Import all compression methods
class CompressionBenchmark:
"""Benchmark suite for all compression methods."""
def __init__(self):
self.compressors = {
'RLE': RLECompressor(),
'Huffman': HuffmanCompressor(),
'LZ77': LZ77Compressor(),
'LZW': LZWCompressor(),
'Arithmetic': ArithmeticCompressor(),
'BWT': BWTCompressor()
}
self.output_dirs = {
'RLE': 'compressed_output/rle',
'Huffman': 'compressed_output/huffman',
'LZ77': 'compressed_output/lz77',
'LZW': 'compressed_output/lzw',
'Arithmetic': 'compressed_output/arithmetic',
'BWT': 'compressed_output/bwt'
}
self.file_extensions = {
'RLE': '.rle',
'Huffman': '.huff',
'LZ77': '.lz77',
'LZW': '.lzw',
'Arithmetic': '.arith',
'BWT': '.bwt'
}
def setup_output_directories(self):
"""Create output directories for all compression methods."""
for output_dir in self.output_dirs.values():
os.makedirs(output_dir, exist_ok=True)
def compress_file_with_method(self, input_file: str, method_name: str) -> CompressionStats:
"""
Compress a single file with a specific method.
Args:
input_file: Path to input file
method_name: Name of compression method
Returns:
CompressionStats object with results
"""
compressor = self.compressors[method_name]
output_dir = self.output_dirs[method_name]
file_ext = self.file_extensions[method_name]
filename = os.path.basename(input_file)
output_file = os.path.join(output_dir, f"{filename}{file_ext}")
try:
original_size = get_file_size(input_file)
# Compress file and measure time
start_time = time.time()
compressed_size = compressor.compress_file(
input_file, output_file)[0]
end_time = time.time()
compression_time = end_time - start_time
return CompressionStats(method_name, original_size, compressed_size, compression_time)
except Exception as e:
print(f"Error compressing {filename} with {method_name}: {e}")
original_size = get_file_size(input_file)
return CompressionStats(method_name, original_size, original_size, 0.0)
def run_comprehensive_benchmark(self, test_dir: str = "files_to_compress") -> List[Dict[str, Any]]:
"""
Run all compression methods on all test files.
Args:
test_dir: Directory containing test files
Returns:
List of dictionaries with comprehensive results
"""
if not os.path.exists(test_dir):
print(f"Error: Test directory '{test_dir}' not found!")
return []
self.setup_output_directories()
test_files = get_all_test_files(test_dir)
if not test_files:
print(f"No test files found in {test_dir}")
return []
print(f"Starting comprehensive compression benchmark")
print(f"Test files: {len(test_files)}")
print(f"Compression methods: {len(self.compressors)}")
print(f"Total operations: {len(test_files) * len(self.compressors)}")
print("=" * 80)
results = []
for file_idx, input_file in enumerate(test_files):
filename = os.path.basename(input_file)
original_size = get_file_size(input_file)
print(f"\nFile {file_idx + 1}/{len(test_files)}: {filename}")
print(f"Original size: {format_size(original_size)}")
print("-" * 60)
file_results = {
'filename': filename,
'original_size': original_size,
'original_size_formatted': format_size(original_size)
}
method_stats = {}
# Test each compression method
for method_name in self.compressors.keys():
print(f" Testing {method_name}... ", end="", flush=True)
stats = self.compress_file_with_method(input_file, method_name)
method_stats[method_name] = stats
print(f"{format_size(stats.compressed_size)} "
f"({stats.compression_ratio:.1f}%) "
f"in {stats.compression_time:.2f}s")
# Add method-specific data to results
file_results[f'{method_name}_size'] = stats.compressed_size
file_results[f'{method_name}_size_formatted'] = format_size(
stats.compressed_size)
file_results[f'{method_name}_ratio'] = stats.compression_ratio
file_results[f'{method_name}_time'] = stats.compression_time
# Find best method for this file
best_method = max(method_stats.keys(),
key=lambda m: method_stats[m].compression_ratio)
best_stats = method_stats[best_method]
file_results['best_method'] = best_method
file_results['best_ratio'] = best_stats.compression_ratio
file_results['best_size'] = best_stats.compressed_size
print(
f" Best: {best_method} ({best_stats.compression_ratio:.1f}%)")
results.append(file_results)
return results
def generate_csv_report(self, results: List[Dict[str, Any]], output_file: str = "compression_report.csv"):
"""
Generate comprehensive CSV report.
Args:
results: Benchmark results
output_file: Output CSV filename
"""
if not results:
print("No results to write to CSV")
return
print(f"\nGenerating CSV report: {output_file}")
# Define CSV columns
columns = [
'filename',
'original_size',
'original_size_formatted'
]
# Add columns for each method
for method in self.compressors.keys():
columns.extend([
f'{method}_size',
f'{method}_size_formatted',
f'{method}_ratio',
f'{method}_time'
])
columns.extend([
'best_method',
'best_ratio',
'best_size'
])
# Write CSV file
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=columns)
writer.writeheader()
writer.writerows(results)
print(f"CSV report saved: {output_file}")
def print_summary_statistics(self, results: List[Dict[str, Any]]):
"""
Print summary statistics for all methods.
Args:
results: Benchmark results
"""
if not results:
return
print("\n" + "="*80)
print("COMPRESSION BENCHMARK SUMMARY")
print("="*80)
total_original = sum(r['original_size'] for r in results)
print(f"Files processed: {len(results)}")
print(f"Total original size: {format_size(total_original)}")
print()
# Summary for each method
print("Method Performance Summary:")
print("-" * 50)
method_summaries = {}
for method in self.compressors.keys():
total_compressed = sum(r[f'{method}_size'] for r in results)
total_time = sum(r[f'{method}_time'] for r in results)
overall_ratio = calculate_compression_ratio(
total_original, total_compressed)
# Count wins (best method for file)
wins = sum(1 for r in results if r['best_method'] == method)
method_summaries[method] = {
'total_compressed': total_compressed,
'overall_ratio': overall_ratio,
'total_time': total_time,
'wins': wins
}
print(f"{method:12}: {format_size(total_compressed):>10} "
f"({overall_ratio:>6.1f}%) "
f"{total_time:>8.2f}s "
f"{wins:>3} wins")
# Find overall best method
best_overall = max(method_summaries.keys(),
key=lambda m: method_summaries[m]['overall_ratio'])
print()
print(f"Best overall method: {best_overall} "
f"({method_summaries[best_overall]['overall_ratio']:.1f}% compression)")
# File type analysis
print("\nFile Type Analysis:")
print("-" * 50)
file_types = {}
for result in results:
filename = result['filename']
ext = os.path.splitext(filename)[1].lower()
if ext not in file_types:
file_types[ext] = []
file_types[ext].append(result)
for file_type, type_results in file_types.items():
if not file_type:
file_type = "(no extension)"
type_original = sum(r['original_size'] for r in type_results)
# Find best method for this file type
type_best_ratios = {}
for method in self.compressors.keys():
type_compressed = sum(r[f'{method}_size']
for r in type_results)
type_ratio = calculate_compression_ratio(
type_original, type_compressed)
type_best_ratios[method] = type_ratio
best_for_type = max(type_best_ratios.keys(),
key=lambda m: type_best_ratios[m])
best_ratio = type_best_ratios[best_for_type]
print(f"{file_type:12}: {len(type_results):>2} files, "
f"best method: {best_for_type} ({best_ratio:.1f}%)")
def main():
"""Main function to run all compression benchmarks."""
print("Compression Algorithms Benchmark Suite")
print("=" * 50)
print()
print("This will run all compression methods on all test files")
print("and generate a comprehensive CSV report.")
print()
benchmark = CompressionBenchmark()
# Run comprehensive benchmark
start_time = time.time()
results = benchmark.run_comprehensive_benchmark()
end_time = time.time()
if results:
# Generate CSV report
benchmark.generate_csv_report(results)
# Print summary statistics
benchmark.print_summary_statistics(results)
print(f"\nTotal benchmark time: {end_time - start_time:.2f} seconds")
print("\nBenchmark complete! Check 'compression_report.csv' for detailed results.")
print("\nCompressed files are saved in 'compressed_output/' directory,")
print("organized by compression method.")
else:
print("No results generated. Please check that test files exist.")
if __name__ == "__main__":
main()