-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.py
More file actions
executable file
·821 lines (572 loc) · 24.5 KB
/
detect.py
File metadata and controls
executable file
·821 lines (572 loc) · 24.5 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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#! venv/bin/python
"""
Gravitational Wave Pulsar Timing Array Correlation Detector
===========================================================
Implements correlation-based detection methods for GW signals in PTA data.
Key Features:
- Calculates correlation patterns between pulsar pairs
- Implements point-source and imaging detection methods
- Uses astropy units throughout for physical consistency
- Optimized with numpy vectorization for performance
"""
import numpy as np
import scipy as sp
import astropy.units as u
from typing import List, Union, Tuple
from dataclasses import dataclass
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
from generate import Pulsar, GWSource
from pathlib import Path
from scipy.optimize import curve_fit
from scipy.ndimage import gaussian_filter
from scipy.signal import convolve2d
from scipy import linalg
import copy
from myplot import *
set_tex()
class PTACorrelationDetector:
"""A detector that calculates correlation patterns between pulsars in a PTA.
This class implements the mathematical framework for detecting gravitational waves
by correlating signals from multiple pulsars in a timing array. The implementation
follows the theoretical framework described in the accompanying paper.
Attributes
----------
pulsars : List[Pulsar]
List of pulsars in the timing array
_phases : astropy.Quantity
Array of optimal phase differences for each pulsar pair (in radians)
"""
def __init__(self, pulsars: List['Pulsar']):
"""Initialize the correlation detector with a set of pulsars.
Parameters
----------
pulsars : List[Pulsar]
List of pulsar objects that form the timing array
"""
self.pulsars = pulsars
# Initialize phase differences for all pulsar pairs
n_pul = len(pulsars)
self._phases = np.zeros(n_pul * (n_pul+1) //2) * u.rad
self.sinle_beams = None
self.pair_beams = None
self.correlations = None
def pair_beam_pattern(self,
i: int,
j: int,
gw_source1: GWSource,
gw_source2: GWSource = None,
phase_diff: u.Quantity = None) -> Union[float, np.ndarray]:
"""Calculate the correlation beam pattern ρ(Ω₁, Ω₂) for a pulsar pair.
This implements calculating the response of a pulsar pair to GW sources at
given sky positions, following equation (2.31) from the paper.
Parameters
----------
i, j : int
Indices of the pulsar pair in the array
gw_source1 : GWSource
First GW source parameters (direction Ω₁)
phase_diff : astropy.Quantity, optional
Phase difference Δφ between pulsars in radians
gw_source2 : GWSource, optional
Second GW source parameters (direction Ω₂). If None, uses gw_source1.
Returns
-------
Union[float, np.ndarray]
Correlation value(s) ρ(Ω₁, Ω₂) for the given pulsar pair
"""
# Use gw_source1 for both if gw_source2 is not provided
gw_source2 = gw_source1 if gw_source2 is None else gw_source2
# Convert inputs to numpy arrays
theta1 = np.asarray(gw_source1.theta.to(u.rad).value, dtype=np.float64)
phi1 = np.asarray(gw_source1.phi.to(u.rad).value, dtype=np.float64)
theta2 = np.asarray(gw_source2.theta.to(u.rad).value, dtype=np.float64)
phi2 = np.asarray(gw_source2.phi.to(u.rad).value, dtype=np.float64)
# Angular frequency of GW and phase difference between pulsars
omega = gw_source1.frequency.to(u.rad/u.s).value
if phase_diff is None:
phase_diff = self.phase(i, j)
delta_phi = phase_diff.to(u.rad).value
# Get the two pulsars
p1 = self.pulsars[i]
p2 = self.pulsars[j]
# Get unit vectors
p1_vec = p1.get_unit_vector()
p2_vec = p2.get_unit_vector()
# Calculate antenna pattern components for both sources
F1_plus, F1_cross = self._calculate_antenna_components(gw_source1, p1_vec)
F2_plus, F2_cross = self._calculate_antenna_components(gw_source2, p2_vec)
# Calculate distance terms ℓ (in seconds)
omega_dir1 = gw_source1.unit_vector
omega_dir2 = gw_source2.unit_vector
dot1 = np.einsum('i,i...->...', p1_vec, omega_dir1) # p1·Ω₁
dot2 = np.einsum('i,i...->...', p2_vec, omega_dir2) # p2·Ω₂
ell1 = p1.distance.to(u.s).value * (1 + dot1)
ell2 = p2.distance.to(u.s).value * (1 + dot2)
# Calculate the static component S (equation 2.18)
sin_term1 = np.sin(omega * ell1 / 2)
sin_term2 = np.sin(omega * ell2 / 2)
S = 2 * sin_term1 * sin_term2
# Duration of the experiment
#T = (p1.mjd.max() - p1.mjd.min()) # in days
#delta_t = delta_phi / omega / 86400 # convert phase diff to days
#S *= (T - delta_t)
# Combine antenna patterns (equations 2.19-2.20)
FF_plus = F1_plus * F2_plus + F1_cross * F2_cross # F1+F2+ + F1×F2×
FF_cross = F1_plus * F2_cross - F1_cross * F2_plus # F1+F2× - F1×F2+
# Calculate the angle difference term
angle_diff = omega * (ell1 - ell2) / 2
# Calculate the dynamical components D1 and D2 (equation 2.21)
D1 = (np.cos(angle_diff) * FF_plus +
np.sin(angle_diff) * FF_cross)
D2 = (np.cos(angle_diff) * FF_cross -
np.sin(angle_diff) * FF_plus)
# Final correlation (equation 2.17)
beam = S * (np.cos(delta_phi) * D1 + np.sin(delta_phi) * D2)
return beam
def single_beam_pattern(self,
i: int,
grid: GWSource) -> Union[float, np.ndarray]:
"""Calculate the correlation beam pattern ρ(Ω₁, Ω₂) for a pulsar pair.
This implements calculating the response of a pulsar pair to GW sources at
given sky positions, following equation (2.31) from the paper.
Parameters
----------
i, j : int
Indices of the pulsar pair in the array
grid : GWSource
First GW source parameters (direction Ω₁)
phase_diff : astropy.Quantity, optional
Phase difference Δφ between pulsars in radians
Returns
-------
Union[float, np.ndarray]
Correlation value(s) ρ(Ω₁, Ω₂) for the given pulsar pair
"""
# Convert inputs to numpy arrays
theta = np.asarray(grid.theta.to(u.rad).value, dtype=np.float64)
phi = np.asarray(grid.phi.to(u.rad).value, dtype=np.float64)
# Angular frequency of GW and phase difference between pulsars
omega = grid.frequency.to(u.rad/u.s).value
# Get the two pulsars
p = self.pulsars[i]
# Get unit vectors
p_vec = p.get_unit_vector()
# Calculate antenna pattern components for both sources
F_plus, F_cross = self._calculate_antenna_components(grid, p_vec)
F = F_plus - 1j*F_cross
# Calculate distance terms ℓ (in seconds)
omega_dir = grid.unit_vector
dot = np.einsum('i,i...->...', p_vec, omega_dir) # p1·Ω₁
ell = p.distance.to(u.s).value * (1 + dot)
# Calculate the static component S (equation 2.18)
sin_term = np.sin(omega * ell / 2)
exp_term = np.exp(-1j * omega*ell/2)
# Final correlation (equation 2.17)
beam = np.sqrt(2) * sin_term * exp_term * F
return beam
def _calculate_antenna_components(self,
gw_source: GWSource,
p_vec: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""Calculate the antenna pattern components F+ and F× for a pulsar.
Parameters
----------
gw_source : GWSource
GW source parameters including direction and polarization
p_vec : np.ndarray
Unit vector pointing to the pulsar
Returns
-------
Tuple[np.ndarray, np.ndarray]
The (F+, F×) antenna pattern components
"""
# GW direction unit vector
omega_dir = gw_source.unit_vector
# Polarization tensors
e_plus = gw_source.e_plus
e_cross = gw_source.e_cross
denom = 1 + np.einsum('i,i...->...', p_vec, omega_dir)
# Antenna pattern calculation (general form)
F_plus = 0.5 * np.einsum('i...,j...,ij...->...', p_vec, p_vec, e_plus) / denom
F_cross = 0.5 * np.einsum('i...,j...,ij...->...', p_vec, p_vec, e_cross) / denom
return F_plus, F_cross
def _get_arg(self,
i: int,
j: int) -> int:
"""Calculate the linearized index in the _phases array for pulsar pair (i,j).
Uses triangular numbering to store only unique pairs (i ≤ j).
Parameters
----------
i, j : int
Indices of the pulsar pair
Returns
-------
int
Index in the _phases array for this pair
"""
N = len(self.pulsars)
return i*N + j - i*(i+1)//2
def fix_phases(self, phases: np.ndarray):
"""Set the phase differences for all pulsar pairs.
Parameters
----------
phases : np.ndarray
Array of phase differences in radians
"""
self._phases = phases
def phase(self,
i: int,
j: int) -> u.Quantity:
"""Get the optimal phase difference for a pulsar pair.
Parameters
----------
i, j : int
Indices of the pulsar pair
Returns
-------
astropy.Quantity
Optimal phase difference in radians
"""
arg = self._get_arg(i, j)
return self._phases[arg]
def observable_correlation(self,
i: int,
j: int,
dt: u.Quantity = 0*u.s) -> np.float64:
"""Calculate observed correlations between two pulsars for a given time lag.
Parameters
----------
i, j : int
Indices of the pulsar pair
dt : astropy.Quantity
The time lags for correlation calculation
Returns
-------
np.float64
Correlation value for the given time lag
"""
if not np.isscalar(dt.value):
print("Correlation is only possible for a single time lag")
psr1 = self.pulsars[i]
psr2 = self.pulsars[j]
# Convert MJDs to consistent units (days)
mjd1 = psr1.mjd * u.day
mjd2 = psr2.mjd * u.day
# Get redshift data
z1 = psr1.redshifts
z2 = psr2.redshifts
# Determine common time range
t0 = mjd1.min()
t1 = mjd1.max() - dt
T = mjd1.max() - mjd1.min()
# Create evaluation points (same for all time lags)
t = np.linspace(t0, t1, len(mjd1))
# Interpolate the redshift data to common time points
int_z1 = np.interp(t, mjd1, z1)
int_z2 = np.interp(t+dt, mjd2, z2)
# Integration factor
denom = (t[1]-t[0]).to_value(u.d) / (T-dt).to_value(u.d)
# Calculate and return the correlation
return np.einsum("i...,i...->...", int_z1, int_z2) * denom
def point_detector(self,
gw_source: GWSource,
n_phases: int = 100) -> None:
"""Determine optimal phase differences for maximum response to a GW source.
This method finds the phase differences that maximize the correlation response
to a GW source at a given position using a quadratic optimization
Parameters
----------
gw_source : GWSource
The target GW source to point at (must be a single point)
n_phases : int, optional
Number of phase test points (default: 100)
Raises
------
AttributeError
If gw_source contains multiple points or has no frequency
"""
# Input validation
if not np.isscalar(gw_source.frequency.value):
raise AttributeError("GW Source must contain only one point: center of the FoV")
if gw_source.frequency.value <= 0:
raise AttributeError("GW Source frequency must be positive")
n_pul = len(self.pulsars)
optimal_phases = np.zeros(n_pul*(n_pul+1)//2) * u.rad
omega = gw_source.frequency.to(u.rad/u.day)
# For each pulsar pair, find the phase difference that maximizes correlation
for i in range(n_pul):
for j in range(i, n_pul):
D1 = self.pair_beam_pattern(i, j,
gw_source,
phase_diff = 0 * u.rad)
D2 = self.pair_beam_pattern(i, j, gw_source,
phase_diff = np.pi/2 * u.rad)
dphi = np.arctan(D2/D1)
dphi = np.mod(dphi, 2*np.pi)
dphi = dphi * u.rad
corr = self.observable_correlation(i, j, dphi/omega)
if corr < 0:
dphi += np.pi * u.rad
corr = self.observable_correlation(i, j, dphi/omega)
# Find the phase that gives maximum correlation
arg = self._get_arg(i, j)
optimal_phases[arg] = dphi
self.fix_phases(optimal_phases)
def fill_beams(self,
grid: GWSource,
phase_diff: float = None):
n, k = grid.phi.shape
n_pul = len(self.pulsars)
n_pair = n_pul*(n_pul+1)//2
# Initialize matrices for the linear system
B = np.empty(shape = (n_pair, n, n))
c = np.empty(shape = (n_pair))
self.single_beams = np.empty((n_pul, n, k), dtype = np.complex128)
self.pair_beams = np.empty((n_pair, n, k))
for i in range(n_pul):
self.single_beams[i] = self.single_beam_pattern(i, grid)
# Calculate beam patterns and correlations for all pulsar pairs
for i in range(n_pul):
for j in range(i, n_pul):
arg = self._get_arg(i, j)
print(f"\rBeam patterns found: {arg+1} / {n_pair}", end = "")
self.pair_beams[arg] = (self.single_beams[i] * self.single_beams[j].conj()).real
print("")
def fill_correlations(self):
n_pul = len(self.pulsars)
n_pair = n_pul*(n_pul+1)//2
self.correlations = np.empty(n_pair)
for i in range(n_pul):
for j in range(i, n_pul):
arg = self._get_arg(i, j)
print(f"\rCorrelations found: {arg+1} / {n_pair}", end = "")
self.correlations[arg] = self.observable_correlation(i, j)
print("")
def image_point(self,
grid: GWSource,
refine_beams: bool = True) -> np.ndarray:
"""Reconstruct a sky image of GW sources using the PTA data.
This implements the imaging method in the point-like source
Parameters
----------
grid : GWSource
Grid of sky positions to evaluate (must be square)
Returns
-------
np.ndarray
Reconstructed sky image showing GW source locations
"""
n, k = grid.phi.shape
if n != k:
raise AttributeError("Please provide a square grid")
n_pul = len(self.pulsars)
n_pair = n_pul*(n_pul+1)//2
# Calculate pixel sizes in the grid
#dphi = grid.phi[0, 1] - grid.phi[0, 0]
#dtheta = grid.theta[1, 0] - grid.theta[0, 0]
#solid_angle = np.sin(grid.theta) * dtheta * dphi
#solid_angle = solid_angle.to_value(u.rad**2)
# Initialize matrices for the linear system
if refine_beams or self.pair_beams is None:
self.fill_beams(grid)
self.fill_correlations()
R = self.pair_beams.reshape((n_pair, -1))
#inv = 1/np.sum(R*R, axis = 0)
#asq = (R.T @ self.correlations) * inv
inv = np.linalg.inv(R.T @ R + 1e0 * np.eye(n*n))
asq = inv @ R.T @ self.correlations
a = np.sqrt(np.abs(asq)).reshape((n, n))
return a
def image_clean(self,
grid: GWSource,
gain: np.float64 = 1e-1,
n_iter: int = 50,
) -> np.ndarray:
phi = grid.phi
theta = grid.theta
omega = grid.frequency
n, _ = phi.shape
n_pul = len(self.pulsars)
n_pair = n_pul * (n_pul + 1) // 2
# Precompute correlations
pta = copy.deepcopy(self)
# Initialize the image
img = np.zeros(shape = (n, n))
DI = pta.image_point(grid, refine_beams = True)
# CLEAN loop
for i in range(n_iter):
# Find new set of correlations and construct a dirty image
DI = pta.image_point(grid, refine_beams = False)
# Find amplitude and position of the next point in the image
argmax1d = np.argmax(DI)
argmax = np.unravel_index(argmax1d, DI.shape)
amplitude = gain * DI[argmax]
# Save this point
img[argmax] += amplitude
#plt.figure(figsize = (4, 4)) # view format
plt.figure(figsize = a4(.6, .6/np.sqrt(2))) # view format
plt.title(f"Dirty Map {i}")
plt.xlabel(r"pix")
plt.ylabel(r"pix")
plt.imshow(DI,
origin = "lower",
cmap = "hot",
)
#plt.colorbar()
#plt.plot(argmax[1], argmax[0], "ko")
# Remove the point from observations
source = GWSource(theta = theta[argmax],
phi = phi[argmax],
C02E
frequency = omega,
strain = -amplitude)
for psr in pta.pulsars:
psr.add_redshift(source)
return img
def image_svd(self,
grid: GWSource,
lam: np.float64 = 1e-1,
) -> np.ndarray:
n, k = grid.phi.shape
if n != k:
raise AttributeError("Please provide a square grid")
n_pul = len(self.pulsars)
n_pair = n_pul*(n_pul+1)//2
# Calculate pixel sizes in the grid
#dphi = grid.phi[0, 1] - grid.phi[0, 0]
#dtheta = grid.theta[1, 0] - grid.theta[0, 0]
#solid_angle = np.sin(grid.theta) * dtheta * dphi
#solid_angle = solid_angle.to_value(u.rad**2)
# Initialize matrices for the linear system
self.fill_beams(grid)
self.fill_correlations()
n = grid.phi.shape[0]
R = self.pair_beams.reshape((n_pair, -1))
U, S, Vh = linalg.svd(R)
rho = self.correlations
img = np.zeros((n, n))
for i in range(8*len(self.pulsars)):
new = np.dot(U[:, i], rho) * Vh[i, :] / S[i]
new = new.reshape((n, n))
img += new
fig, ax = plt.subplots(1, 2)
fig.suptitle(f"{S[i]}")
ax[0].imshow(new, origin = "lower", cmap = "hot")
ax[1].imshow(img, origin = "lower", cmap = "hot")
print(S[0])
print(S[2*len(self.pulsars)])
save_image("123.pdf", tight = True)
exit()
def image_psf(self,
grid: GWSource,
crop: int = None,
) -> (np.ndarray, np.ndarray):
def gaussian_2d(xy, amplitude, x0, y0, sigma_x, sigma_y, theta, const = 0):
x, y = xy
a = np.cos(theta)**2/(2*sigma_x**2) + np.sin(theta)**2/(2*sigma_y**2)
b = -np.sin(2*theta)/(4*sigma_x**2) + np.sin(2*theta)/(4*sigma_y**2)
c = np.sin(theta)**2/(2*sigma_x**2) + np.cos(theta)**2/(2*sigma_y**2)
img = amplitude * np.exp(-(a*(x-x0)**2 + 2*b*(x-x0)*(y-y0) + c*(y-y0)**2)) + const
return img.ravel()
phi = grid.phi
theta = grid.theta
omega = grid.frequency
n, _ = phi.shape
nh = n//2
n_pul = len(self.pulsars)
n_pair = n_pul * (n_pul + 1)//2
center = [theta[nh, nh], phi[nh, nh]]
# Add a point source in observations
source = GWSource(theta = center[0],
phi = center[1],
frequency = omega,
strain = 1)
pulsars = Pulsar.load_collection("pulsars")
for i, psr in enumerate(pulsars):
psr.redshifts[:] = 0.0
psr.add_redshift(source)
pta = PTACorrelationDetector(pulsars)
a = pta.image_point(grid, refine_beams = True)
if crop is not None:
phi = phi[nh - crop : nh + crop, nh - crop : nh + crop]
theta = theta[nh - crop : nh + crop, nh - crop : nh + crop]
a_cr = a[nh - crop : nh + crop, nh - crop : nh + crop]
n, _ = phi.shape
nh = n//2
else:
a_cr = a
x = np.linspace(0, 1, n)
y = np.linspace(0, 1, n)
X, Y = np.meshgrid(x, y)
i0 = [1, .5, .5, 1e-2, 1e-2, 0, 0]
popt, pcov = curve_fit(gaussian_2d, (X, Y), a_cr.ravel(), p0 = i0,
bounds = ([0, .4, .4, 0, 0, 0, -np.pi],
[2, .6, .6, .3, .3, np.pi, 2]))
x = np.linspace(0, 1, grid.phi.shape[0])
y = np.linspace(0, 1, grid.phi.shape[0])
X, Y = np.meshgrid(x, y)
popt[-1] = 0
popt[1] = .5
popt[2] = .5
popt[3] = popt[3] * n / grid.phi.shape[0]
popt[4] = popt[4] * n / grid.phi.shape[0]
img = gaussian_2d((X, Y), *popt).reshape(grid.phi.shape)
return a, img / np.sum(img)
if __name__ == "__main__":
# Example usage with default parameters
n = 50 # Grid size
center = [60, 30] * u.deg # Center of field of view
n_pul = 60 # Number of pulsars to use
# Field of view width
width = [10, 10] * u.arcmin
# Create a grid of sky positions to evaluates
phi = center[0] + np.linspace(-width[0]/2, width[0]/2, n)
theta = center[1] + np.linspace(-width[1]/2, width[1]/2, n)
phi, theta = np.meshgrid(phi, theta)
# Create GW source objects for the grid and center position
grid = GWSource(theta = theta,
phi = phi,
frequency = 1e-8 * u.Hz,
strain = 1
)
center = GWSource(theta = center[1],
phi = center[0],
frequency = 1e-8 * u.Hz,
strain = 1)
# Load pulsars and create detector
pulsars = Pulsar.load_collection("pulsars")[:n_pul]
pta = PTACorrelationDetector(pulsars)
#img = pta.image_svd(grid)
# Reconstruct the sky image
#img = pta.image_point(grid, refine_beams = True)
dirty_beam, psf = pta.image_psf(grid, crop = 15)
img = pta.image_clean(grid, gain = 3e-1, n_iter = 30)
img = convolve2d(img, psf, mode = "same") / np.max(psf)
#plt.figure(figsize = (4, 4)) # view format
plt.figure(figsize = a4(.6, .6/np.sqrt(2))) # view format
plt.title(f"Clean Map $N = {n_pul}$")
plt.xlabel(r"arcmin")
plt.ylabel(r"arcmin")
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('$%.0f$'))
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('$%.0f$'))
extent = [-width[0].to(u.arcmin).value/2,
width[0].to(u.arcmin).value/2,
-width[1].to(u.arcmin).value/2,
width[1].to(u.arcmin).value/2]
plt.imshow(img,
origin = "lower",
cmap = "hot",
extent = extent,
)
#plt.colorbar()
# # Plot PSF
# shifted = np.roll(psf/psf.max(), shift = (-n//3, n//3), axis = (0, 1))
#
# plt.contour(shifted,
# levels = [.5], # adjust number of contour levels as needed
# colors = "white", # or any color that contrasts well
# linewidths = .7,
# origin = "lower",
# extent = extent,
# )
#save_eps("pta_image", tight = True)
save_image("pta_image.pdf", tight = True)