-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopscan.py
More file actions
305 lines (253 loc) · 11.9 KB
/
loopscan.py
File metadata and controls
305 lines (253 loc) · 11.9 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
#!/usr/bin/env python3
"""
LoopScan: Pattern Echo Detection in the CMB
Main CLI interface for the Flat Loop Universe analysis tool
"""
import argparse
import logging
import sys
from pathlib import Path
import json
import numpy as np
# Add src to path
sys.path.append('src')
from data_loader import CMBDataLoader
from echo_detector import EchoDetector
from visualizer import CMBVisualizer
from synthetic_data import SyntheticCMBGenerator
def setup_logging(verbose: bool = False):
"""Configure logging"""
level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(
level=level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def run_synthetic_test(args):
"""Run synthetic data test"""
print("🌀 LoopScan: Synthetic Echo Detection Test")
print("=" * 50)
# Generate synthetic data
generator = SyntheticCMBGenerator(nside=args.nside)
synthetic_map, echo_locations = generator.create_toroidal_test_map(
n_echo_pairs=args.n_echoes,
pattern_strength=args.strength,
seed=42
)
print(f"Generated synthetic CMB with {len(echo_locations)} echo pairs")
# Run detection
detector = EchoDetector(nside=args.nside)
# Antipodal search
antipodal_matches = detector.detect_antipodal_echoes(
synthetic_map,
patch_radius=np.radians(args.patch_size),
min_correlation=args.min_corr,
n_samples=args.n_samples
)
# Toroidal search
shift_angles = [np.radians(angle) for angle in args.shift_angles]
toroidal_matches = detector.detect_toroidal_echoes(
synthetic_map,
shift_angles=shift_angles,
patch_radius=np.radians(args.patch_size),
min_correlation=args.min_corr,
n_samples=args.n_samples // 2
)
all_matches = antipodal_matches + toroidal_matches
print(f"\nDetection Results:")
print(f" Antipodal matches: {len(antipodal_matches)}")
print(f" Toroidal matches: {len(toroidal_matches)}")
print(f" Total matches: {len(all_matches)}")
if all_matches:
# Sort by correlation
all_matches.sort(key=lambda x: abs(x.correlation_score), reverse=True)
print(f"\nTop 5 Detections:")
for i, match in enumerate(all_matches[:5]):
print(f" {i+1}. Correlation: {match.correlation_score:.3f}, "
f"Separation: {np.degrees(match.angular_separation):.1f}°")
# Visualization
if args.plot:
viz = CMBVisualizer()
viz.plot_mollweide(synthetic_map, "Synthetic CMB with Echoes",
save_path="synthetic_cmb.png")
if all_matches:
viz.plot_echo_matches(synthetic_map, all_matches,
save_path="synthetic_detections.png")
viz.plot_correlation_histogram(all_matches,
save_path="synthetic_correlations.png")
# Save results
if args.output:
results = {
'n_echo_pairs_planted': len(echo_locations),
'n_matches_found': len(all_matches),
'detection_parameters': {
'nside': args.nside,
'patch_size_deg': args.patch_size,
'min_correlation': args.min_corr,
'n_samples': args.n_samples
},
'matches': []
}
for match in all_matches:
results['matches'].append({
'region1_center_deg': [np.degrees(match.region1_center[0]),
np.degrees(match.region1_center[1])],
'region2_center_deg': [np.degrees(match.region2_center[0]),
np.degrees(match.region2_center[1])],
'angular_separation_deg': np.degrees(match.angular_separation),
'correlation_score': match.correlation_score,
'method': match.method
})
with open(args.output, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nResults saved to {args.output}")
def run_real_data_analysis(args):
"""Run analysis on real CMB data"""
print("🌀 LoopScan: Real CMB Data Analysis")
print("=" * 50)
# Load CMB data
loader = CMBDataLoader()
try:
cmb_map = loader.load_planck_map(args.data_file)
print(f"Loaded CMB map: {loader.get_map_statistics(cmb_map)}")
# Downsample if requested
import healpy as hp
if args.nside < hp.npix2nside(len(cmb_map)):
cmb_map = loader.downsample_map(cmb_map, args.nside)
# Clean map
cmb_map = loader.remove_monopole_dipole(cmb_map)
# Run detection
detector = EchoDetector(nside=args.nside)
print("Searching for echo patterns...")
# Antipodal search
antipodal_matches = detector.detect_antipodal_echoes(
cmb_map,
patch_radius=np.radians(args.patch_size),
min_correlation=args.min_corr,
n_samples=args.n_samples
)
# Toroidal search
shift_angles = [np.radians(90), np.radians(180), np.radians(120)]
toroidal_matches = detector.detect_toroidal_echoes(
cmb_map,
shift_angles=shift_angles,
patch_radius=np.radians(args.patch_size),
min_correlation=args.min_corr,
n_samples=args.n_samples // 2
)
all_matches = antipodal_matches + toroidal_matches
print(f"\nDetection Results:")
print(f" Antipodal matches: {len(antipodal_matches)}")
print(f" Toroidal matches: {len(toroidal_matches)}")
print(f" Total matches: {len(all_matches)}")
if all_matches:
all_matches.sort(key=lambda x: abs(x.correlation_score), reverse=True)
print(f"\nTop 5 Detections:")
for i, match in enumerate(all_matches[:5]):
print(f" {i+1}. Correlation: {match.correlation_score:.3f}, "
f"Separation: {np.degrees(match.angular_separation):.1f}°")
# Visualization
if args.plot:
viz = CMBVisualizer()
viz.plot_mollweide(cmb_map, f"CMB Map - {args.data_file}",
save_path="real_cmb.png")
if all_matches:
viz.plot_echo_matches(cmb_map, all_matches,
save_path="real_detections.png")
viz.plot_correlation_histogram(all_matches,
save_path="real_correlations.png")
# Save results
if args.output:
results = {
'data_file': args.data_file,
'n_matches_found': len(all_matches),
'detection_parameters': {
'nside': args.nside,
'patch_size_deg': args.patch_size,
'min_correlation': args.min_corr,
'n_samples': args.n_samples
},
'matches': []
}
for match in all_matches:
results['matches'].append({
'region1_center_deg': [np.degrees(match.region1_center[0]),
np.degrees(match.region1_center[1])],
'region2_center_deg': [np.degrees(match.region2_center[0]),
np.degrees(match.region2_center[1])],
'angular_separation_deg': np.degrees(match.angular_separation),
'correlation_score': match.correlation_score,
'method': match.method
})
with open(args.output, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nResults saved to {args.output}")
print("Real data analysis complete!")
except FileNotFoundError:
print(f"Error: CMB data file '{args.data_file}' not found in data/ directory")
print("Please download CMB data files first. See data/README.md for instructions.")
def main():
parser = argparse.ArgumentParser(
description="LoopScan: Search for echo patterns in CMB data",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Run synthetic test with default parameters
python loopscan.py synthetic --plot
# Run with custom parameters
python loopscan.py synthetic --nside 512 --n-echoes 5 --strength 200 --plot
# Analyze real Planck data
python loopscan.py real --data-file planck_cmb_commander.fits --nside 256
"""
)
parser.add_argument('-v', '--verbose', action='store_true',
help='Enable verbose logging')
subparsers = parser.add_subparsers(dest='mode', help='Analysis mode')
# Synthetic data mode
synthetic_parser = subparsers.add_parser('synthetic',
help='Test with synthetic data')
synthetic_parser.add_argument('--nside', type=int, default=256,
help='HEALPix resolution parameter')
synthetic_parser.add_argument('--n-echoes', type=int, default=3,
help='Number of echo pairs to plant')
synthetic_parser.add_argument('--strength', type=float, default=100.0,
help='Echo pattern strength (μK)')
synthetic_parser.add_argument('--patch-size', type=float, default=10.0,
help='Patch radius in degrees')
synthetic_parser.add_argument('--min-corr', type=float, default=0.2,
help='Minimum correlation threshold')
synthetic_parser.add_argument('--n-samples', type=int, default=2000,
help='Number of sample points')
synthetic_parser.add_argument('--shift-angles', nargs='+', type=float,
default=[90.0, 180.0, 120.0],
help='Angular shifts to test (degrees)')
synthetic_parser.add_argument('--plot', action='store_true',
help='Generate visualization plots')
synthetic_parser.add_argument('--output', type=str,
help='Save results to JSON file')
# Real data mode
real_parser = subparsers.add_parser('real', help='Analyze real CMB data')
real_parser.add_argument('--data-file', type=str, required=True,
help='CMB .fits file in data/ directory')
real_parser.add_argument('--nside', type=int, default=512,
help='Target resolution (will downsample if needed)')
real_parser.add_argument('--patch-size', type=float, default=10.0,
help='Patch radius in degrees')
real_parser.add_argument('--min-corr', type=float, default=0.3,
help='Minimum correlation threshold')
real_parser.add_argument('--n-samples', type=int, default=5000,
help='Number of sample points')
real_parser.add_argument('--plot', action='store_true',
help='Generate visualization plots')
real_parser.add_argument('--output', type=str,
help='Save results to JSON file')
args = parser.parse_args()
if not args.mode:
parser.print_help()
return
setup_logging(args.verbose)
if args.mode == 'synthetic':
run_synthetic_test(args)
elif args.mode == 'real':
run_real_data_analysis(args)
if __name__ == '__main__':
main()