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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
| import numpy as np import matplotlib.pyplot as plt from scipy import signal, stats from sklearn.metrics import roc_curve, auc, precision_recall_curve, f1_score from sklearn.model_selection import cross_val_score from sklearn.ensemble import RandomForestClassifier import warnings warnings.filterwarnings('ignore')
np.random.seed(42)
def generate_variable_star_lightcurve(n_points=1000, period=50, amplitude=0.3, noise_level=0.05): """ Generate synthetic variable star light curve Parameters: ----------- n_points : int Number of time points period : float Period of variation in arbitrary time units amplitude : float Amplitude of variation noise_level : float Standard deviation of Gaussian noise Returns: -------- time : array Time points flux : array Flux measurements (brightness) """ time = np.linspace(0, 200, n_points) flux = 1.0 + amplitude * np.sin(2 * np.pi * time / period) flux += 0.1 * amplitude * np.sin(4 * np.pi * time / period) flux += noise_level * np.random.randn(n_points) return time, flux
def generate_constant_star_lightcurve(n_points=1000, noise_level=0.05): """Generate synthetic constant (non-variable) star light curve""" time = np.linspace(0, 200, n_points) flux = 1.0 + noise_level * np.random.randn(n_points) return time, flux
def generate_grb_timeseries(n_points=5000, n_bursts=5, baseline_rate=10, burst_intensity=100): """ Generate synthetic Gamma-Ray Burst time series Parameters: ----------- n_points : int Number of time bins n_bursts : int Number of GRB events to inject baseline_rate : float Background count rate burst_intensity : float Peak intensity of bursts Returns: -------- time : array Time bins counts : array Photon counts per bin burst_locations : array True burst locations (for validation) """ time = np.arange(n_points) counts = np.random.poisson(baseline_rate, n_points).astype(float) burst_locations = np.random.choice(np.arange(100, n_points-100), n_bursts, replace=False) for loc in burst_locations: burst_profile = np.zeros(n_points) decay_time = 20 for i in range(loc, min(loc + 100, n_points)): burst_profile[i] = burst_intensity * np.exp(-(i - loc) / decay_time) counts += np.random.poisson(burst_profile) return time, counts, burst_locations
def extract_variable_star_features(time, flux): """ Extract features for variable star classification Features: --------- 1. Amplitude: max - min 2. Standard deviation 3. Variability index: std/mean 4. Kurtosis: measure of "tailedness" 5. Skewness: measure of asymmetry 6. Power at dominant frequency (Lomb-Scargle periodogram) """ features = {} features['amplitude'] = np.max(flux) - np.min(flux) features['std'] = np.std(flux) features['mean'] = np.mean(flux) features['variability_index'] = features['std'] / features['mean'] if features['mean'] != 0 else 0 features['kurtosis'] = stats.kurtosis(flux) features['skewness'] = stats.skew(flux) from scipy.signal import lombscargle f_min = 0.01 f_max = 0.5 frequencies = np.linspace(f_min, f_max, 1000) angular_frequencies = 2 * np.pi * frequencies pgram = lombscargle(time, flux - np.mean(flux), angular_frequencies, normalize=True) features['max_power'] = np.max(pgram) features['peak_frequency'] = frequencies[np.argmax(pgram)] return features
def extract_grb_features(counts, window_size=50): """ Extract features for GRB detection using sliding window For each time point, calculate: 1. SNR in local window 2. Peak significance 3. Rise time 4. Duration above threshold """ n = len(counts) snr = np.zeros(n) for i in range(window_size, n - window_size): background_region = np.concatenate([ counts[i-window_size:i-window_size//2], counts[i+window_size//2:i+window_size] ]) background_mean = np.mean(background_region) background_std = np.std(background_region) signal_region = counts[i-window_size//4:i+window_size//4] signal_mean = np.mean(signal_region) if background_std > 0: snr[i] = (signal_mean - background_mean) / background_std else: snr[i] = 0 return snr
def detect_variable_stars_threshold(features_list, threshold_dict): """ Simple threshold-based detector for variable stars A star is classified as variable if: - variability_index > threshold AND - max_power > threshold """ predictions = [] for features in features_list: is_variable = ( features['variability_index'] > threshold_dict['variability_index'] and features['max_power'] > threshold_dict['max_power'] ) predictions.append(1 if is_variable else 0) return np.array(predictions)
def detect_grb_threshold(snr, threshold): """ Threshold-based GRB detector Detect burst when SNR exceeds threshold """ detections = snr > threshold detection_indices = [] in_burst = False burst_start = 0 for i in range(1, len(detections)-1): if detections[i] and not in_burst: in_burst = True burst_start = i elif not detections[i] and in_burst: burst_region = snr[burst_start:i] peak_loc = burst_start + np.argmax(burst_region) detection_indices.append(peak_loc) in_burst = False return np.array(detection_indices)
def optimize_variable_star_threshold(features_list, labels, param_name, param_range): """ Optimize single threshold parameter using ROC analysis """ tpr_list = [] fpr_list = [] f1_list = [] for threshold in param_range: threshold_dict = { 'variability_index': 0.03, 'max_power': 0.3, } threshold_dict[param_name] = threshold predictions = detect_variable_stars_threshold(features_list, threshold_dict) tp = np.sum((predictions == 1) & (labels == 1)) fp = np.sum((predictions == 1) & (labels == 0)) tn = np.sum((predictions == 0) & (labels == 0)) fn = np.sum((predictions == 0) & (labels == 1)) tpr = tp / (tp + fn) if (tp + fn) > 0 else 0 fpr = fp / (fp + tn) if (fp + tn) > 0 else 0 precision = tp / (tp + fp) if (tp + fp) > 0 else 0 recall = tpr f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0 tpr_list.append(tpr) fpr_list.append(fpr) f1_list.append(f1) return np.array(tpr_list), np.array(fpr_list), np.array(f1_list)
def optimize_grb_threshold(snr, true_burst_locations, threshold_range, tolerance=20): """ Optimize GRB detection threshold Parameters: ----------- tolerance : int Number of time bins within which a detection counts as correct """ tpr_list = [] fpr_list = [] f1_list = [] n_true_bursts = len(true_burst_locations) n_points = len(snr) for threshold in threshold_range: detections = detect_grb_threshold(snr, threshold) tp = 0 for true_loc in true_burst_locations: if np.any(np.abs(detections - true_loc) < tolerance): tp += 1 fp = len(detections) - tp fn = n_true_bursts - tp tn = max(0, n_points // 100 - fp) tpr = tp / n_true_bursts if n_true_bursts > 0 else 0 fpr = fp / max(1, fp + tn) precision = tp / len(detections) if len(detections) > 0 else 0 recall = tpr f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0 tpr_list.append(tpr) fpr_list.append(fpr) f1_list.append(f1) return np.array(tpr_list), np.array(fpr_list), np.array(f1_list)
print("=" * 80) print("ASTRONOMICAL EVENT DETECTION ALGORITHM OPTIMIZATION") print("=" * 80) print()
print("PART 1: VARIABLE STAR DETECTION") print("-" * 80)
n_variable = 100 n_constant = 100
features_list = [] labels = []
print(f"Generating {n_variable} variable stars and {n_constant} constant stars...")
for i in range(n_variable): period = np.random.uniform(30, 70) amplitude = np.random.uniform(0.2, 0.5) time, flux = generate_variable_star_lightcurve(period=period, amplitude=amplitude) features = extract_variable_star_features(time, flux) features_list.append(features) labels.append(1)
for i in range(n_constant): time, flux = generate_constant_star_lightcurve() features = extract_variable_star_features(time, flux) features_list.append(features) labels.append(0)
labels = np.array(labels)
print("\nOptimizing variability_index threshold...") vi_range = np.linspace(0.01, 0.15, 50) vi_tpr, vi_fpr, vi_f1 = optimize_variable_star_threshold( features_list, labels, 'variability_index', vi_range )
optimal_vi_idx = np.argmax(vi_f1) optimal_vi_threshold = vi_range[optimal_vi_idx] print(f"Optimal variability_index threshold: {optimal_vi_threshold:.4f}") print(f"Maximum F1 score: {vi_f1[optimal_vi_idx]:.4f}")
print("\nOptimizing max_power threshold...") mp_range = np.linspace(0.1, 0.8, 50) mp_tpr, mp_fpr, mp_f1 = optimize_variable_star_threshold( features_list, labels, 'max_power', mp_range )
optimal_mp_idx = np.argmax(mp_f1) optimal_mp_threshold = mp_range[optimal_mp_idx] print(f"Optimal max_power threshold: {optimal_mp_threshold:.4f}") print(f"Maximum F1 score: {mp_f1[optimal_mp_idx]:.4f}")
print("\n" + "=" * 80) print("PART 2: GAMMA-RAY BURST DETECTION") print("-" * 80)
time_grb, counts_grb, true_bursts = generate_grb_timeseries( n_points=5000, n_bursts=10, baseline_rate=10, burst_intensity=150 )
print(f"Generated time series with {len(true_bursts)} GRB events") print(f"True burst locations: {sorted(true_bursts)}")
print("\nExtracting SNR features...") snr_grb = extract_grb_features(counts_grb, window_size=50)
print("\nOptimizing SNR threshold...") threshold_range = np.linspace(2, 10, 50) grb_tpr, grb_fpr, grb_f1 = optimize_grb_threshold( snr_grb, true_bursts, threshold_range, tolerance=25 )
optimal_grb_idx = np.argmax(grb_f1) optimal_grb_threshold = threshold_range[optimal_grb_idx] print(f"Optimal SNR threshold: {optimal_grb_threshold:.4f}") print(f"Maximum F1 score: {grb_f1[optimal_grb_idx]:.4f}")
detections = detect_grb_threshold(snr_grb, optimal_grb_threshold) print(f"\nDetected {len(detections)} events at optimal threshold") print(f"Detection locations: {sorted(detections)}")
print("\n" + "=" * 80) print("GENERATING VISUALIZATIONS") print("-" * 80)
fig = plt.figure(figsize=(16, 12))
ax1 = plt.subplot(3, 3, 1) time_ex, flux_ex = generate_variable_star_lightcurve(period=50, amplitude=0.3) ax1.plot(time_ex, flux_ex, 'b-', linewidth=0.8, alpha=0.7) ax1.set_xlabel('Time', fontsize=10) ax1.set_ylabel('Normalized Flux', fontsize=10) ax1.set_title('Example: Variable Star Light Curve', fontsize=11, fontweight='bold') ax1.grid(True, alpha=0.3)
ax2 = plt.subplot(3, 3, 2) time_const, flux_const = generate_constant_star_lightcurve() ax2.plot(time_const, flux_const, 'r-', linewidth=0.8, alpha=0.7) ax2.set_xlabel('Time', fontsize=10) ax2.set_ylabel('Normalized Flux', fontsize=10) ax2.set_title('Example: Constant Star Light Curve', fontsize=11, fontweight='bold') ax2.grid(True, alpha=0.3)
ax3 = plt.subplot(3, 3, 3) vi_variable = [f['variability_index'] for f, l in zip(features_list, labels) if l == 1] vi_constant = [f['variability_index'] for f, l in zip(features_list, labels) if l == 0] ax3.hist(vi_variable, bins=20, alpha=0.6, label='Variable', color='blue', edgecolor='black') ax3.hist(vi_constant, bins=20, alpha=0.6, label='Constant', color='red', edgecolor='black') ax3.axvline(optimal_vi_threshold, color='green', linestyle='--', linewidth=2, label='Optimal Threshold') ax3.set_xlabel('Variability Index', fontsize=10) ax3.set_ylabel('Count', fontsize=10) ax3.set_title('Feature Distribution: Variability Index', fontsize=11, fontweight='bold') ax3.legend(fontsize=9) ax3.grid(True, alpha=0.3)
ax4 = plt.subplot(3, 3, 4) ax4.plot(vi_fpr, vi_tpr, 'b-', linewidth=2, label=f'AUC = {auc(vi_fpr, vi_tpr):.3f}') ax4.plot([0, 1], [0, 1], 'k--', linewidth=1, alpha=0.5, label='Random') ax4.scatter(vi_fpr[optimal_vi_idx], vi_tpr[optimal_vi_idx], s=100, c='red', marker='*', zorder=5, label='Optimal Point') ax4.set_xlabel('False Positive Rate', fontsize=10) ax4.set_ylabel('True Positive Rate', fontsize=10) ax4.set_title('ROC Curve: Variability Index Optimization', fontsize=11, fontweight='bold') ax4.legend(fontsize=9) ax4.grid(True, alpha=0.3)
ax5 = plt.subplot(3, 3, 5) ax5.plot(vi_range, vi_f1, 'b-', linewidth=2) ax5.axvline(optimal_vi_threshold, color='red', linestyle='--', linewidth=2, label='Optimal') ax5.scatter(optimal_vi_threshold, vi_f1[optimal_vi_idx], s=100, c='red', marker='*', zorder=5) ax5.set_xlabel('Variability Index Threshold', fontsize=10) ax5.set_ylabel('F1 Score', fontsize=10) ax5.set_title('F1 Score Optimization: Variability Index', fontsize=11, fontweight='bold') ax5.legend(fontsize=9) ax5.grid(True, alpha=0.3)
ax6 = plt.subplot(3, 3, 6) ax6.plot(mp_range, mp_f1, 'g-', linewidth=2) ax6.axvline(optimal_mp_threshold, color='red', linestyle='--', linewidth=2, label='Optimal') ax6.scatter(optimal_mp_threshold, mp_f1[optimal_mp_idx], s=100, c='red', marker='*', zorder=5) ax6.set_xlabel('Max Power Threshold', fontsize=10) ax6.set_ylabel('F1 Score', fontsize=10) ax6.set_title('F1 Score Optimization: Max Power', fontsize=11, fontweight='bold') ax6.legend(fontsize=9) ax6.grid(True, alpha=0.3)
ax7 = plt.subplot(3, 3, 7) ax7.plot(time_grb, counts_grb, 'k-', linewidth=0.5, alpha=0.6, label='Counts') for burst_loc in true_bursts: ax7.axvline(burst_loc, color='red', linestyle='--', alpha=0.5, linewidth=1) ax7.set_xlabel('Time Bin', fontsize=10) ax7.set_ylabel('Counts', fontsize=10) ax7.set_title('GRB Time Series (red lines = true bursts)', fontsize=11, fontweight='bold') ax7.grid(True, alpha=0.3) ax7.set_xlim([0, 1000])
ax8 = plt.subplot(3, 3, 8) ax8.plot(time_grb, snr_grb, 'b-', linewidth=0.8, label='SNR') ax8.axhline(optimal_grb_threshold, color='green', linestyle='--', linewidth=2, label='Optimal Threshold') for burst_loc in true_bursts: ax8.axvline(burst_loc, color='red', linestyle='--', alpha=0.3, linewidth=1) for det_loc in detections: ax8.plot(det_loc, snr_grb[det_loc], 'g^', markersize=8, alpha=0.7) ax8.set_xlabel('Time Bin', fontsize=10) ax8.set_ylabel('SNR', fontsize=10) ax8.set_title('GRB Detection: SNR Analysis', fontsize=11, fontweight='bold') ax8.legend(fontsize=9) ax8.grid(True, alpha=0.3) ax8.set_xlim([0, 1000])
ax9 = plt.subplot(3, 3, 9) ax9_twin = ax9.twinx() line1 = ax9.plot(threshold_range, grb_f1, 'b-', linewidth=2, label='F1 Score') ax9.axvline(optimal_grb_threshold, color='red', linestyle='--', linewidth=2, alpha=0.7) ax9.scatter(optimal_grb_threshold, grb_f1[optimal_grb_idx], s=100, c='red', marker='*', zorder=5) line2 = ax9_twin.plot(threshold_range, grb_tpr, 'g-', linewidth=2, label='True Positive Rate', alpha=0.7) ax9.set_xlabel('SNR Threshold', fontsize=10) ax9.set_ylabel('F1 Score', fontsize=10, color='b') ax9_twin.set_ylabel('True Positive Rate', fontsize=10, color='g') ax9.set_title('GRB Detection Optimization', fontsize=11, fontweight='bold') ax9.tick_params(axis='y', labelcolor='b') ax9_twin.tick_params(axis='y', labelcolor='g') lines = line1 + line2 labels = [l.get_label() for l in lines] ax9.legend(lines, labels, fontsize=9, loc='lower right') ax9.grid(True, alpha=0.3)
plt.tight_layout() plt.savefig('astronomical_detection_optimization.png', dpi=150, bbox_inches='tight') print("Visualization saved as 'astronomical_detection_optimization.png'") plt.show()
print("\n" + "=" * 80) print("SUMMARY STATISTICS") print("=" * 80)
print("\nVariable Star Detection:") print(f" Optimal Variability Index Threshold: {optimal_vi_threshold:.4f}") print(f" F1 Score at Optimal Point: {vi_f1[optimal_vi_idx]:.4f}") print(f" TPR at Optimal Point: {vi_tpr[optimal_vi_idx]:.4f}") print(f" FPR at Optimal Point: {vi_fpr[optimal_vi_idx]:.4f}") print(f" ROC AUC: {auc(vi_fpr, vi_tpr):.4f}")
print("\nGamma-Ray Burst Detection:") print(f" Optimal SNR Threshold: {optimal_grb_threshold:.4f}") print(f" F1 Score at Optimal Point: {grb_f1[optimal_grb_idx]:.4f}") print(f" TPR at Optimal Point: {grb_tpr[optimal_grb_idx]:.4f}") print(f" FPR at Optimal Point: {grb_fpr[optimal_grb_idx]:.4f}") print(f" Number of True Bursts: {len(true_bursts)}") print(f" Number of Detections: {len(detections)}")
print("\n" + "=" * 80) print("EXECUTION COMPLETE") print("=" * 80)
|