-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstrometricData.py
731 lines (602 loc) · 20.2 KB
/
AstrometricData.py
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
import numpy as np
import sys
import itertools
from scipy.stats import truncnorm
from scipy.linalg import cholesky
import pandas as pd
import CoordinateTransformations as CT
import vsh
import Utils as U
import model
class AstrometricDataframe:
def __init__(self):
"""
Initialise Class
"""
self.N_obj = 0
self.Lmax = None
self.positions = np.array([])
self.positions_Cartesian = np.array([])
self.proper_motions = np.array([])
self.inv_proper_motion_error_matrix = np.array([])
self.almQ_names = dict()
self.YlmQ_names = dict()
self.basis = dict()
self.which_basis = None
self.overlap_matrix = np.array([])
self.overlap_matrix_Cholesky = np.array([])
def generate_names(self) -> None:
U.logger("Generating names")
self.almQ_names = {
(l, m, Q): f"a^{Q}_{l},{m}"
for l in range(1, self.Lmax+1)
for m in range(-l, l+1) for Q in ['E', 'B']
}
self.YlmQ_names = {
(l, m, Q): f"Y^{Q}_{l},{m}"
for l in range(1, self.Lmax+1)
for m in range(-l, l+1) for Q in ['E', 'B']
}
self.lmQ_ordered = [
(l, m, Q)
for l in range(1, self.Lmax+1)
for m in range(-l, l+1) for Q in ['E', 'B']
]
def generate_positions(
self,
random_seed: int,
method_polar: str,
method_azimuthal: str,
bunch_size_polar: float,
bunch_size_azimuthal: float,
) -> None:
"""
Generate random positions
INPUTS
------
method: string
switches between a uniform distribution
and a bunched (biased) distribution
bunch_size_polar: float
controls the distribution in the polar direction;
0. activates the uniform regime,
while a small number (e.g. 0.1) is severely non-uniform
bunch_size_azimuthal: float
controls the distribution in the azimuthal direction;
0. activates the uniform regime,
while a small number (e.g. 0.1) is severely non-uniform
seed: int
random seed
"""
U.logger("Generating QSO positions")
if random_seed is not None and random_seed > 0:
U.logger(f"Using random seed {random_seed}")
np.random.seed(random_seed)
if (
method_polar == "uniform"
or (method_polar == "bunched" and bunch_size_polar == 0)
):
U.logger("Using method \"uniform\" for the declination")
dec = (
0.5 * np.pi
- np.arccos(np.random.uniform(-1, 1, size=self.N_obj))
)
elif method_polar == "bunched" and bunch_size_polar > 0:
U.logger("Using method \"bunched\" for the declination")
dec = (
0.5 * np.pi
- np.arccos(truncnorm.rvs(
-1/bunch_size_polar,
1/bunch_size_polar,
scale=bunch_size_polar,
size=self.N_obj)
)
)
if (
method_azimuthal == "uniform"
or (method_azimuthal == "bunched" and bunch_size_azimuthal == 0)
):
U.logger("Using method \"uniform\" for the right ascension")
ra = 2 * np.pi * np.random.uniform(0, 1, size=self.N_obj)
elif method_azimuthal == "bunched" and bunch_size_azimuthal > 0:
U.logger("Using method \"bunched\" for the right ascension")
ra = (
2 * np.pi * (truncnorm.rvs(
-0.5/bunch_size_azimuthal,
0.5/bunch_size_azimuthal,
scale=bunch_size_azimuthal,
size=self.N_obj
)
+ 0.5)
)
self.positions = np.array(list(zip(ra, dec)))
def load_Gaia_positions(
self,
dataset: pd.DataFrame,
) -> None:
"""
Load the positions from Gaia file
"""
U.logger("Loading Gaia QSO positions")
self.positions = dataset[['ra', 'dec']].values
self.positions = U.deg_to_rad(self.positions)
def load_TD_positions(
self,
dataset: pd.DataFrame,
) -> None:
"""
Load the positions from Truebenbach-Darling file
"""
U.logger("Loading Truebenbach & Darling QSO positions")
hours = 360. / 24.
mins = hours / 60.
secs = mins / 60.
deg = 1.
arcmin = deg / 60.
arcsec = arcmin / 60.
RAh = dataset['RAh'].values
RAm = dataset['RAm'].values
RAs = dataset['RAs'].values
DEd = dataset['DEd'].values
DEm = dataset['DEm'].values
DEs = dataset['DEs'].values
ra = RAh*hours + RAm*mins + RAs*secs
dec = DEd*deg + DEm*arcmin + DEs*arcsec
self.positions = np.transpose([ra, dec])
self.positions = U.deg_to_rad(self.positions)
def generate_proper_motions(
self,
injection: dict,
random_seed: int,
) -> None:
U.logger("Generating QSO proper motions")
if random_seed > 0:
U.logger(f"Using random seed {random_seed}")
np.random.seed(random_seed)
if len(injection) == 0:
U.logger("Injecting no proper motions")
self.proper_motions = np.zeros((self.N_obj, 2))
else:
U.logger("Injecting multipoles with amplitudes:")
almQ = {}
for l in range(1, self.Lmax+1):
for m in range(-l, l+1):
for Q in ['E', 'B']:
key = f"{l},{m},{Q}"
if key in injection.keys():
almQ[(l, m, Q)] = injection[key]
U.logger(f"{key}: {injection[key]}")
else:
almQ[(l, m, Q)] = 0
self.proper_motions = model.generate_model(almQ, self.basis)
def load_Gaia_proper_motions(
self,
dataset: pd.DataFrame,
) -> None:
"""
Load the proper motions from Gaia file
"""
U.logger("Loading Gaia QSO proper motions")
self.proper_motions = dataset[['pmra', 'pmdec']].values
def load_TD_proper_motions(
self,
dataset: pd.DataFrame,
) -> None:
"""
Load the proper motions from Truebenbach-Darling file
"""
U.logger("Loading Truebenbach & Darling QSO proper motions")
self.proper_motions = dataset[['pmRA', 'pmDE']].values
def generate_proper_motion_errors(
self,
method: str,
std: float,
corr: float,
) -> None:
U.logger("Generating QSO proper motion errors")
if method == "flat":
U.logger("Using method \"flat\"")
scale = std
elif method == "adaptive":
U.logger("Using method \"adaptive\"")
scale = std * np.abs(self.proper_motions)
proper_motion_errors = scale * np.ones(self.proper_motions.shape)
# Scale the pm_ra_err by sin(theta) = cos(dec)
proper_motion_errors[:, 0] = (
proper_motion_errors[:, 0] / np.cos(self.positions[:, 1])
)
proper_motion_errors_corr = corr * np.ones(self.N_obj)
covariance = U.covariant_matrix(
proper_motion_errors,
proper_motion_errors_corr
)
self.inv_proper_motion_error_matrix = np.linalg.inv(covariance)
def load_Gaia_proper_motion_errors(
self,
dataset: pd.DataFrame,
) -> None:
"""
Load the proper motion errors from Gaia file
"""
U.logger("Loading Gaia QSO proper motion errors")
proper_motions_errors = dataset[['pmra_error', 'pmdec_error']].values
proper_motions_errors[:,0] = np.true_divide(
proper_motions_errors[:,0],
np.cos(self.positions[:,1])
)
proper_motions_err_corr = dataset[['pmra_pmdec_corr']].values
covariance = U.covariant_matrix(
proper_motions_errors,
proper_motions_err_corr
)
self.inv_proper_motion_error_matrix = np.linalg.inv(covariance)
def load_TD_proper_motion_errors(
self,
dataset: pd.DataFrame,
) -> None:
"""
Load the proper motion errors from Truebenbach-Darling file
TO DO: Use chi2 statistics for correlation
"""
U.logger("Loading Truebenbach & Darling QSO proper motion errors")
proper_motions_errors = dataset[['e_pmRA', 'e_pmDE']].values
proper_motions_errors[:,0] = np.true_divide(
proper_motions_errors[:,0],
np.cos(self.positions[:,1])
)
proper_motions_err_corr = np.zeros(self.N_obj)
covariance = U.covariant_matrix(
proper_motions_errors,
proper_motions_err_corr
)
self.inv_proper_motion_error_matrix = np.linalg.inv(covariance)
def add_proper_motion_noise(
self,
std: float,
random_seed: int,
) -> None:
U.logger("Adding proper motion noise")
if random_seed is not None and random_seed > 0:
U.logger(f"Using random seed {random_seed}")
np.random.seed(random_seed)
proper_motion_noise = np.random.normal(
loc = 0.,
scale = std,
size = self.proper_motions.shape,
)
self.proper_motions += proper_motion_noise
# def _compute_VSHs(self, l: int, m: int, Q: str) -> np.ndarray:
# if Q == "E":
# return CT.Cartesian_to_geographic_vector(
# self.positions_Cartesian,
# vsh.real_vector_spherical_harmonic_E(
# l,
# m,
# self.positions_Cartesian,
# )
# )
# elif Q == "B":
# return CT.Cartesian_to_geographic_vector(
# self.positions_Cartesian,
# vsh.real_vector_spherical_harmonic_B(
# l,
# m,
# self.positions_Cartesian,
# )
# )
def _compute_VSHs(self, l: int, m: int, Q: str) -> np.ndarray:
if Q == 'E':
return CT.Cartesian_to_geographic_vector(
self.positions_Cartesian,
vsh.real_vector_spherical_harmonic_E(
l,
m,
self.positions_Cartesian,
)
)
elif Q == 'B':
return CT.Cartesian_to_geographic_vector(
self.positions_Cartesian,
vsh.real_vector_spherical_harmonic_B(
l,
m,
self.positions_Cartesian,
)
)
def generate_VSHs(self) -> None:
"""
Precompute VSH functions at QSO locations
"""
U.logger("Generating Vector Spherical Harmonics basis")
self.basis = {
(l, m, Q): self._compute_VSHs(l, m, Q)
for l in range(1, self.Lmax+1)
for m in range(-l, l+1) for Q in ['E', 'B']
}
self.which_basis = "vsh"
def remove_outliers(
self,
R_threshold: float,
) -> None:
"""
Remove outliers from dataset
Define the dimensionless proper motion for each object as
R = proper_motions^T . inverse_error_matrix . proper_motions
We will remove outliers with R greater than a given threshold
INPUTS
------
R_threshold: float
remove outliers with R>R_threshold
"""
if R_threshold is not None:
R = np.sqrt(np.einsum(
'...i, ...ij, ...j -> ...',
self.proper_motions,
self.inv_proper_motion_error_matrix,
self.proper_motions)
)
remove_mask = np.asarray(R > R_threshold)
remove_indices = remove_mask.nonzero()[0]
self.positions = np.delete(
self.positions,
remove_indices,
axis=0,
)
self.positions_Cartesian = np.delete(
self.positions_Cartesian,
remove_indices,
axis=0,
)
self.proper_motions = np.delete(
self.proper_motions,
remove_indices,
axis=0,
)
self.inv_proper_motion_error_matrix = np.delete(
self.inv_proper_motion_error_matrix,
remove_indices,
axis=0,
)
N_removed_outliers = remove_indices.shape[0]
if N_removed_outliers == 1:
U.logger(f"Removing 1 outlier.")
else:
U.logger(f"Removing {N_removed_outliers} outliers.")
def compute_overlap_matrix(
self,
weighted_overlaps: bool = True,
) -> None:
"""
Calculate the overlap matrix (and its Cholesky decomposition)
between VSH basis functions
weighted_overlaps: bool
whether or not to use the error weighted overlap sums
"""
U.logger("Computing the overlap matrix")
prefactor = 4 * np.pi / self.N_obj
overlap_matrix_size = 2 * self.Lmax * (self.Lmax+2)
self.overlap_matrix = np.zeros(
(overlap_matrix_size, overlap_matrix_size)
)
if weighted_overlaps == False:
metric = np.zeros((self.N_obj, 2, 2))
metric[:, 0, 0] = np.cos(self.positions[:, 1].copy())**2
metric[:, 1, 1] = 1
basis_values = np.array(list(self.basis.values()))
for ((i, vsh_i), (j, vsh_j)) in itertools.product(
enumerate(basis_values),
repeat=2,
):
if weighted_overlaps == True:
self.overlap_matrix[i,j] = prefactor * np.einsum(
"...i,...ij,...j->...",
vsh_i,
self.inv_proper_motion_error_matrix,
vsh_j
).sum()
else:
self.overlap_matrix[i,j] = prefactor * np.einsum(
"...i,...ij,...j->...",
vsh_i,
metric,
vsh_i
).sum()
self.overlap_matrix = U.normalize_matrix(
self.overlap_matrix,
L=self.Lmax,
)
def change_basis(self) -> None:
"""
Method to change from VSH basis to orthogonal basis
"""
U.logger("Changing Vector Spherical Harmonics basis to \
orthogonal basis")
self.overlap_matrix_Cholesky = cholesky(self.overlap_matrix)
invL = np.linalg.inv(self.overlap_matrix_Cholesky)
vsh_basis_values = np.array(list(self.basis.values()))
orthogonal_basis_values = np.einsum(
"i...j,ik->k...j",
vsh_basis_values,
invL
)
for i, key in enumerate(self.basis):
self.basis[key] = orthogonal_basis_values[i]
self.which_basis = "orthogonal"
def load_astrometric_data(
ADf: AstrometricDataframe,
params: dict,
) -> None:
ADf.Lmax = params["Lmax"]
ADf.generate_names()
positions = params["positions"]
proper_motions = params["proper_motions"]
proper_motion_errors = params["proper_motion_errors"]
dataset_dict = {
2: {"cat": "Gaia", "file_name": "data/type2.csv"},
3: {"cat": "Gaia", "file_name": "data/type3.csv"},
4: {"cat": "Gaia", "file_name": "data/type2and3.csv"},
5: {"cat": "TD", "file_name": "data/TD6.dat"}
}
which_dataset = set([
positions,
proper_motions,
proper_motion_errors,
]).intersection(set(dataset_dict.keys()))
if len(which_dataset) > 1:
sys.exit("Conflicting datasets cannot be combined.")
elif len(which_dataset) == 1:
chosen_dataset = next(iter(which_dataset))
if dataset_dict[chosen_dataset]['cat'] == "Gaia":
dataset = import_Gaia_dataset(
dataset_dict[chosen_dataset]['file_name']
)
elif dataset_dict[chosen_dataset]['cat'] == "TD":
dataset = import_TD_dataset(
dataset_dict[chosen_dataset]['file_name']
)
else:
dataset = None
if dataset is None:
ADf.N_obj = params['N_obj']
else:
ADf.N_obj = dataset.shape[0]
if positions == 1:
ADf.generate_positions(
random_seed = params['positions_seed'],
method_polar = params['positions_method_polar'],
method_azimuthal = params['positions_method_azimuthal'],
bunch_size_polar = params['bunch_size_polar'],
bunch_size_azimuthal = params['bunch_size_azimuthal'],
)
elif positions in [2, 3, 4]:
ADf.load_Gaia_positions(dataset)
elif positions == 5:
ADf.load_TD_positions(dataset)
ADf.positions_Cartesian = CT.geographic_to_Cartesian_point(
ADf.positions
)
ADf.generate_VSHs()
if proper_motions == 1:
ADf.generate_proper_motions(
injection = params['injection'],
random_seed = params['proper_motions_seed']
)
elif proper_motions in [2, 3, 4]:
ADf.load_Gaia_proper_motions(dataset)
elif proper_motions == 5:
ADf.load_TD_proper_motions(dataset)
if proper_motion_errors == 1:
ADf.generate_proper_motion_errors(
method = params['proper_motion_errors_method'],
std = params['proper_motion_errors_std'],
corr = params['proper_motion_errors_corr'],
)
elif proper_motion_errors in [2, 3, 4]:
ADf.load_Gaia_proper_motion_errors(dataset)
elif proper_motion_errors == 5:
ADf.load_TD_proper_motion_errors(dataset)
if 'proper_motion_noise' in params:
ADf.add_proper_motion_noise(
std = params['proper_motion_noise'],
random_seed = params['proper_motion_noise_seed'],
)
ADf.remove_outliers(
params['dimensionless_proper_motion_threshold']
)
ADf.compute_overlap_matrix()
if params['basis'] == "orthogonal":
ADf.change_basis()
ADf.compute_overlap_matrix()
def import_Gaia_dataset(path: str) -> pd.DataFrame:
"""
Import Gaia dataset
"""
dataset = pd.read_csv(
path,
sep=',',
header='infer',
squeeze=False,
mangle_dupe_cols=True,
engine='c',
skipinitialspace=False,
skipfooter=0,
keep_default_na=True,
na_filter=True,
verbose=False,
skip_blank_lines=True,
parse_dates=False,
infer_datetime_format=False,
keep_date_col=False,
dayfirst=False,
iterator=False,
decimal=b'.',
doublequote=True,
error_bad_lines=True,
warn_bad_lines=True,
delim_whitespace=False,
low_memory=False,
memory_map=False
)
dropna_columns = [
'ra',
'dec',
'pmra',
'pmdec',
'pmra_error',
'pmdec_error',
'pmra_pmdec_corr'
]
dataset.dropna(
axis=0,
how='any',
thresh=None,
subset=dropna_columns,
inplace=True
)
return dataset
def import_TD_dataset(path: str) -> pd.DataFrame:
"""
Import TD dataset
"""
col_names = [
'Name',
'RAh', 'RAm', 'RAs',
'e_RAs',
'DEd', 'DEm', 'DEs',
'e_DEs',
'pmRA',
'e_pmRA',
'o_pmRA',
'chi2a',
'pmDE',
'e_pmDE',
'o_pmDE',
'chi2d',
'Length',
'MJD',
'Flag',
'z',
'f_z',
'r_z'
]
dataset = pd.read_fwf(
path,
colspecs='infer',
names=col_names,
widths=None,
comment = '#',
infer_nrows=500,
)
dropna_columns = [
'RAh', 'RAm', 'RAs', 'e_RAs', 'DEd', 'DEm', 'DEs', 'e_DEs',
'pmRA', 'e_pmRA', 'o_pmRA', 'pmDE', 'e_pmDE', 'o_pmDE',
'chi2a', 'chi2d',
]
dataset.dropna(
axis=0,
how='any',
thresh=None,
subset=dropna_columns,
inplace=True,
)
return dataset