-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_processing.py
More file actions
362 lines (277 loc) · 11.7 KB
/
batch_processing.py
File metadata and controls
362 lines (277 loc) · 11.7 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
355
356
357
358
359
360
361
362
#!/usr/bin/env python3
"""
Example: Batch Processing Multiple NGB Files
This example demonstrates how to efficiently process multiple NGB files
using pyngb's batch processing capabilities.
Requirements:
- pyngb
- polars (automatically installed with pyngb)
Usage:
python batch_processing.py --input-dir ./data/ --output-dir ./results/
python batch_processing.py --files file1.ngb-ss3 file2.ngb-ss3
"""
import argparse
import sys
import tempfile
from collections.abc import Sequence
from pathlib import Path
from typing import Any
import polars as pl
from pyngb import BatchProcessor, NGBDataset, process_directory
def demonstrate_batch_processor() -> bool:
"""Demonstrate BatchProcessor class usage."""
print("\n🔧 Method 1: BatchProcessor Class")
print("-" * 50)
# Initialize batch processor
processor = BatchProcessor(
max_workers=2, # Use 2 cores for processing
verbose=True, # Show progress information
)
# Process files
results = processor.process_files(
list(files),
output_format="both", # Create both Parquet and CSV files
output_dir=output_dir,
skip_errors=True, # Continue processing if individual files fail
)
# Analyze results
successful = [r for r in results if r["status"] == "success"]
failed = [r for r in results if r["status"] == "error"]
print("\n📊 Processing Results:")
print(f" ✅ Successful: {len(successful)}")
print(f" ❌ Failed: {len(failed)}")
if successful:
total_rows = int(sum(float(r.get("rows") or 0) for r in successful))
avg_time = (
sum(float(r.get("processing_time") or 0.0) for r in successful)
/ len(successful)
if successful
else 0.0
)
print(f" 📈 Total data points: {total_rows:,}")
print(f" ⏱️ Average processing time: {avg_time:.2f} seconds")
print("\n📁 Output Files Created:")
for result in successful:
file_val = result.get("file")
if not isinstance(file_val, str):
continue
input_path = Path(file_val)
base_name = input_path.stem
parquet_file = Path(output_dir) / f"{base_name}.parquet"
csv_file = Path(output_dir) / f"{base_name}.csv"
metadata_file = Path(output_dir) / f"{base_name}_metadata.json"
if parquet_file.exists():
print(f" ✅ {parquet_file}")
if csv_file.exists():
print(f" ✅ {csv_file}")
if metadata_file.exists():
print(f" ✅ {metadata_file}")
if failed:
print("\n❌ Failed Files:")
for result in failed:
print(f" {result['file']}: {result.get('error', 'Unknown error')}")
return results
def demonstrate_convenience_function() -> bool:
"""Demonstrate process_directory convenience function."""
print("\n⚡ Method 2: Convenience Function")
print("-" * 50)
# Process entire directory
results = process_directory(
directory=input_dir, pattern="*.ngb-ss3", output_format="parquet", max_workers=2
)
print(f"📊 Processed {len(results)} files from directory")
successful = [r for r in results if r["status"] == "success"]
if successful:
print(f" ✅ {len(successful)} files processed successfully")
# Show sample information
for result in successful[:3]: # Show first 3
file_val = result.get("file")
name = Path(file_val).name if isinstance(file_val, str) else "<unknown>"
print(f" {name}: {result.get('rows', 0)} rows")
if len(successful) > 3:
print(f" ... and {len(successful) - 3} more files")
return results
def demonstrate_dataset_management(files: Sequence[str | Path], out_dir: Path) -> None:
"""Demonstrate NGBDataset for dataset management."""
print("\n📚 Method 3: Dataset Management")
print("-" * 50)
# Create dataset
dataset = NGBDataset([Path(f) for f in files])
print("📊 Dataset Overview:")
print(f" Files: {len(dataset)}")
# Get summary statistics
try:
summary = dataset.summary()
print(f" Loadable files: {summary.get('loadable_files', 0)}")
if "unique_instruments" in summary:
instruments = summary["unique_instruments"]
print(f" Instruments: {instruments}")
if "unique_operators" in summary:
operators = summary["unique_operators"]
print(f" Operators: {operators}")
rng = summary.get("sample_mass_range")
if (
isinstance(rng, tuple)
and len(rng) == 2
and all(isinstance(x, (int, float)) for x in rng)
):
print(f" Sample mass range: {rng[0]:.1f} to {rng[1]:.1f} mg")
if summary.get("avg_sample_mass"):
avg_mass = summary["avg_sample_mass"]
print(f" Average sample mass: {avg_mass:.1f} mg")
except Exception as e:
print(f" ⚠️ Could not generate summary: {e}")
# Export metadata
try:
metadata_file = out_dir / "dataset_metadata.csv"
dataset.export_metadata(metadata_file, format="csv")
if metadata_file.exists():
print(f" 📁 Metadata exported to: {metadata_file}")
# Show metadata preview
metadata_df = pl.read_csv(metadata_file)
print(" 📋 Metadata preview:")
print(f" Columns: {metadata_df.columns}")
print(f" Rows: {metadata_df.height}")
except Exception as e:
print(f" ⚠️ Could not export metadata: {e}")
def create_processing_summary(results: list[dict], output_dir: str | Path) -> None:
"""Create a summary report of batch processing results."""
print("\n📋 Creating Processing Summary")
print("-" * 50)
# Compile summary data
summary_data = []
for result in results:
file_path = Path(result["file"])
summary_row = {
"file_name": file_path.name,
"file_path": str(file_path),
"status": result["status"],
"rows": result.get("rows", 0),
"columns": result.get("columns", 0),
"sample_name": result.get("sample_name", "Unknown"),
"processing_time": result.get("processing_time", 0),
"error": result.get("error", ""),
}
summary_data.append(summary_row)
# Create summary DataFrame
summary_df = pl.DataFrame(summary_data)
# Save summary
summary_file = Path(output_dir) / "processing_summary.csv"
summary_df.write_csv(summary_file)
print(f"📁 Summary saved to: {summary_file}")
# Display statistics
total_files = len(results)
successful_files = len([r for r in results if r["status"] == "success"])
total_rows = sum(r.get("rows", 0) for r in results if r["status"] == "success")
total_time = sum(r.get("processing_time", 0) for r in results)
print("\n📊 Final Statistics:")
print(f" Total files processed: {total_files}")
print(
f" Successful: {successful_files} ({successful_files / total_files * 100:.1f}%)"
)
print(f" Total data points: {total_rows:,}")
print(f" Total processing time: {total_time:.2f} seconds")
if successful_files > 0:
avg_time = total_time / successful_files
print(f" Average time per file: {avg_time:.2f} seconds")
def main() -> bool:
"""Main function to run the batch processing example."""
parser = argparse.ArgumentParser(
description="Demonstrate pyngb batch processing capabilities"
)
parser.add_argument("--input-dir", type=str, help="Directory containing NGB files")
parser.add_argument(
"--output-dir",
type=str,
default=None,
help="Output directory for processed files (defaults to a temporary directory)",
)
parser.add_argument("--files", nargs="+", help="Specific files to process")
args = parser.parse_args()
print("🚀 pyngb Batch Processing Example")
print("=" * 60)
print("This example demonstrates efficient processing of multiple NGB files.")
print()
# Determine files to process
files_to_process = []
if args.files:
files_to_process = args.files
print(f"📁 Processing {len(files_to_process)} specified files")
elif args.input_dir:
input_path = Path(args.input_dir)
if not input_path.exists():
print(f"❌ Input directory not found: {args.input_dir}")
return 1
files_to_process = list(input_path.glob("*.ngb-ss3"))
print(f"📁 Found {len(files_to_process)} NGB files in {args.input_dir}")
else:
# Look for test files
possible_dirs = ["../tests/test_files/", "tests/test_files/", "./test_files/"]
for test_dir in possible_dirs:
test_path = Path(test_dir)
if test_path.exists():
test_files = list(test_path.glob("*.ngb-ss3"))
if test_files:
files_to_process = [str(f) for f in test_files]
print(f"📁 Using test files from {test_dir}")
break
if not files_to_process:
print("❌ No NGB files found!")
print()
print("Usage:")
print(" python batch_processing.py --input-dir ./data/")
print(" python batch_processing.py --files file1.ngb-ss3 file2.ngb-ss3")
return 1
# Check that files exist
valid_files = []
for file_path in files_to_process:
if Path(file_path).exists():
valid_files.append(str(file_path))
else:
print(f"⚠️ File not found: {file_path}")
if not valid_files:
print("❌ No valid files to process!")
return 1
files_to_process = valid_files
print(f"✅ Will process {len(files_to_process)} files")
# Create output directory (temporary if not provided)
if args.output_dir:
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
print(f"📁 Output directory: {output_dir}")
# Run example normally, leave artifacts when user chose path
cleanup = False
else:
tmp = tempfile.TemporaryDirectory(prefix="pyngb_batch_out_")
output_dir = Path(tmp.name)
print(f"📁 Output directory (temporary): {output_dir}")
cleanup = True
# Demonstrate different batch processing methods
all_results = []
# Method 1: BatchProcessor class
results1 = demonstrate_batch_processor(files_to_process, output_dir)
all_results.extend(results1)
# Method 2: Convenience function (if input directory provided)
if args.input_dir:
_ = demonstrate_convenience_function(args.input_dir, output_dir)
# Method 3: Dataset management
demonstrate_dataset_management(files_to_process, output_dir)
# Create processing summary
create_processing_summary(all_results, output_dir)
print("\n✅ Batch processing demonstration completed!")
print("\n💡 What you learned:")
print(" - How to use BatchProcessor for parallel processing")
print(" - How to handle processing errors gracefully")
print(" - How to manage datasets with NGBDataset")
print(" - How to export data in multiple formats")
print(" - How to create processing summaries")
print("\n📁 Check the output directory for processed files:")
print(f" {output_dir.absolute()}")
# Cleanup temporary directory if we created one
if cleanup:
print("\n🧹 Cleaning up temporary output directory...")
# TemporaryDirectory will clean up automatically when going out of scope
return 0
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)