-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (63 loc) · 2.32 KB
/
Copy pathmain.py
File metadata and controls
83 lines (63 loc) · 2.32 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
#!/usr/bin/env python3
"""
PET Image Reconstruction using L-BFGS-B optimisation - Main Script
This is the main entry point for PET reconstruction using the modular
components.
Author: Sam Porter
Date: 30/06/25
Dependencies: SIRF, CIL, scipy, numpy
"""
import os
import sys
from sirf.STIR import MessageRedirector
from src.config import Config
from src.cli_parser import parse_arguments, validate_optimisation_params
from src.data_loader import load_pet_data
from src.bfgs_optimiser import run_bfgs_reconstruction
from src.optimisation_tracker import print_optimisation_summary
def main():
"""Main function to run PET reconstruction."""
# Send stdout and stderr to log files
msg = MessageRedirector(
info="info.log",
warn='stdout.log',
errr='stderr.log',
)
# Parse command line arguments
args = parse_arguments()
# Set verbosity from command line
Config.VERBOSITY = args.verbose
try:
# Validate parameters
validate_optimisation_params(args)
# Load PET data
if Config.VERBOSITY > 0:
print("Loading PET data...")
data = load_pet_data(args.data_path, args.suffix)
# Run reconstruction
if Config.VERBOSITY > 0:
print("Starting reconstruction...")
reconstructed_image, optimisation_result, tracker = run_bfgs_reconstruction(data, args)
# Save reconstructed image
output_path = os.path.join(args.output_dir, args.output)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
reconstructed_image.write(output_path)
if Config.VERBOSITY >= 0: # Always show this message
print(f"\nReconstructed image saved to: {output_path}")
# Print optimisation summary
print_optimisation_summary(optimisation_result, tracker)
if Config.VERBOSITY > 0:
print("\nReconstruction completed successfully!")
except KeyboardInterrupt:
print("\nReconstruction interrupted by user.")
return 1
except Exception as e:
print(f"Error during reconstruction: {e}")
if Config.VERBOSITY > 1:
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
sys.exit(main())