-
Notifications
You must be signed in to change notification settings - Fork 95
/
test_streamdf.py
1655 lines (1584 loc) · 94.2 KB
/
test_streamdf.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
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function, division
import pytest
import numpy
from scipy import interpolate, integrate
from galpy.util import bovy_coords
sdf_bovy14= None #so we can set this up and then use in other tests
sdft_bovy14= None #so we can set this up and then use in other tests, trailing
def test_progenitor_coordtransformparams():
#Test related to #189: test that the streamdf setup throws a warning when the given coordinate transformation parameters differ from those of the given progenitor orbit
from galpy.df import streamdf
from galpy.orbit import Orbit
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleIsochroneApprox
from galpy.util import bovy_conversion #for unit conversions
from galpy.util import galpyWarning
lp= LogarithmicHaloPotential(normalize=1.,q=0.9)
#odeint to make sure that the C integration warning isn't thrown
aAI= actionAngleIsochroneApprox(pot=lp,b=0.8,integrate_method='odeint')
obs= Orbit([1.56148083,0.35081535,-1.15481504,
0.88719443,-0.47713334,0.12019596],
ro=8.5,vo=235.,zo=0.1,solarmotion=[0.,-10.,0.])
sigv= 0.365 #km/s
#Turn warnings into errors to test for them
import warnings
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always",galpyWarning)
#Test w/ diff Rnorm
sdf_bovy14= streamdf(sigv/220.,progenitor=obs,pot=lp,aA=aAI,
leading=True,
nTrackChunks=11,
tdisrupt=4.5/bovy_conversion.time_in_Gyr(220.,8.),
nosetup=True, #won't look at track
Rnorm=10.)
# Should raise warning bc of Rnorm, might raise others
raisedWarning= False
for wa in w:
raisedWarning= (str(wa.message) == "Warning: progenitor's ro does not agree with streamdf's ro and R0; this may have unexpected consequences when projecting into observables")
if raisedWarning: break
assert raisedWarning, "streamdf setup does not raise warning when progenitor's ro is different from ro"
#Test w/ diff R0
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always",galpyWarning)
sdf_bovy14= streamdf(sigv/220.,progenitor=obs,pot=lp,aA=aAI,
leading=True,
nTrackChunks=11,
tdisrupt=4.5/bovy_conversion.time_in_Gyr(220.,8.),
nosetup=True, #won't look at track
R0=10.)
# Should raise warning bc of R0, might raise others
raisedWarning= False
for wa in w:
raisedWarning= (str(wa.message) == "Warning: progenitor's ro does not agree with streamdf's ro and R0; this may have unexpected consequences when projecting into observables")
if raisedWarning: break
assert raisedWarning, "streamdf setup does not raise warning when progenitor's ro is different from R0"
#Test w/ diff Vnorm
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always",galpyWarning)
sdf_bovy14= streamdf(sigv/220.,progenitor=obs,pot=lp,aA=aAI,
leading=True,
nTrackChunks=11,
tdisrupt=4.5/bovy_conversion.time_in_Gyr(220.,8.),
nosetup=True, #won't look at track
Rnorm=8.5,R0=8.5,Vnorm=220.)
# Should raise warning bc of Vnorm, might raise others
raisedWarning= False
for wa in w:
raisedWarning= (str(wa.message) == "Warning: progenitor's vo does not agree with streamdf's vo; this may have unexpected consequences when projecting into observables")
if raisedWarning: break
assert raisedWarning, "streamdf setup does not raise warning when progenitor's vo is different from vo"
#Test w/ diff zo
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always",galpyWarning)
sdf_bovy14= streamdf(sigv/220.,progenitor=obs,pot=lp,aA=aAI,
leading=True,
nTrackChunks=11,
tdisrupt=4.5/bovy_conversion.time_in_Gyr(220.,8.),
nosetup=True, #won't look at track
Rnorm=8.5,R0=8.5,Vnorm=235.,Zsun=0.025)
# Should raise warning bc of zo, might raise others
raisedWarning= False
for wa in w:
raisedWarning= (str(wa.message) == "Warning: progenitor's zo does not agree with streamdf's Zsun; this may have unexpected consequences when projecting into observables")
if raisedWarning: break
assert raisedWarning, "streamdf setup does not raise warning when progenitor's zo is different from Zsun"
#Test w/ diff vsun
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always",galpyWarning)
sdf_bovy14= streamdf(sigv/220.,progenitor=obs,pot=lp,aA=aAI,
leading=True,
nTrackChunks=11,
tdisrupt=4.5/bovy_conversion.time_in_Gyr(220.,8.),
nosetup=True, #won't look at track
Rnorm=8.5,R0=8.5,Vnorm=235.,Zsun=0.1,
vsun=[0.,220.,0.])
# Should raise warning bc of vsun, might raise others
raisedWarning= False
for wa in w:
raisedWarning= (str(wa.message) == "Warning: progenitor's solarmotion does not agree with streamdf's vsun (after accounting for vo); this may have unexpected consequences when projecting into observables")
if raisedWarning: break
assert raisedWarning, "streamdf setup does not raise warning when progenitor's solarmotion is different from vsun"
return None
#Exact setup from Bovy (2014); should reproduce those results (which have been
# sanity checked
def test_bovy14_setup():
#Imports
from galpy.df import streamdf
from galpy.orbit import Orbit
from galpy.potential import LogarithmicHaloPotential
from galpy.actionAngle import actionAngleIsochroneApprox
from galpy.util import bovy_conversion #for unit conversions
lp= LogarithmicHaloPotential(normalize=1.,q=0.9)
aAI= actionAngleIsochroneApprox(pot=lp,b=0.8)
obs= Orbit([1.56148083,0.35081535,-1.15481504,
0.88719443,-0.47713334,0.12019596])
sigv= 0.365 #km/s
# For custom_transform
theta,dec_ngp,ra_ngp= bovy_coords.get_epoch_angles(2000.)
T= numpy.dot(numpy.array([[numpy.cos(ra_ngp),-numpy.sin(ra_ngp),0.],
[numpy.sin(ra_ngp),numpy.cos(ra_ngp),0.],
[0.,0.,1.]]),
numpy.dot(numpy.array([[-numpy.sin(dec_ngp),0.,
numpy.cos(dec_ngp)],
[0.,1.,0.],
[numpy.cos(dec_ngp),0.,
numpy.sin(dec_ngp)]]),
numpy.array([[numpy.cos(theta),numpy.sin(theta),0.],
[numpy.sin(theta),-numpy.cos(theta),0.],
[0.,0.,1.]]))).T
global sdf_bovy14
sdf_bovy14= streamdf(sigv/220.,progenitor=obs,pot=lp,aA=aAI,
leading=True,
nTrackChunks=11,
tdisrupt=4.5/bovy_conversion.time_in_Gyr(220.,8.),
custom_transform=T)
assert not sdf_bovy14 is None, 'bovy14 streamdf setup did not work'
return None
def test_bovy14_freqratio():
#Test the frequency ratio
assert (sdf_bovy14.freqEigvalRatio()-30.)**2. < 10.**0., 'streamdf model from Bovy (2014) does not give a frequency ratio of about 30'
assert (sdf_bovy14.freqEigvalRatio(isotropic=True)-34.)**2. < 10.**0., 'streamdf model from Bovy (2014) does not give an isotropic frequency ratio of about 34'
return None
def test_bovy14_misalignment():
#Test the misalignment
assert (sdf_bovy14.misalignment()/numpy.pi*180.+0.5)**2. <10.**-2., 'streamdf model from Bovy (2014) does not give a misalighment of about -0.5 degree'
assert (sdf_bovy14.misalignment(isotropic=True)/numpy.pi*180.-1.3)**2. <10.**-2., 'streamdf model from Bovy (2014) does not give an isotropic misalighment of about 1.3 degree'
return None
def test_bovy14_track_prog_diff():
#Test that the stream and the progenitor are close together, for both leading and trailing
check_track_prog_diff(sdf_bovy14,'R','Z',0.1)
check_track_prog_diff(sdf_bovy14,'R','Z',0.8,phys=True) #do 1 with phys
check_track_prog_diff(sdf_bovy14,'R','X',0.1)
check_track_prog_diff(sdf_bovy14,'R','Y',0.1)
check_track_prog_diff(sdf_bovy14,'R','vZ',0.03)
check_track_prog_diff(sdf_bovy14,'R','vZ',6.6,phys=True) #do 1 with phys
check_track_prog_diff(sdf_bovy14,'R','vX',0.05)
check_track_prog_diff(sdf_bovy14,'R','vY',0.05)
check_track_prog_diff(sdf_bovy14,'R','vT',0.05)
check_track_prog_diff(sdf_bovy14,'R','vR',0.05)
check_track_prog_diff(sdf_bovy14,'ll','bb',0.3)
check_track_prog_diff(sdf_bovy14,'ll','dist',0.5)
check_track_prog_diff(sdf_bovy14,'ll','vlos',4.)
check_track_prog_diff(sdf_bovy14,'ll','pmll',0.3)
check_track_prog_diff(sdf_bovy14,'ll','pmbb',0.25)
return None
def test_bovy14_track_spread():
#Test that the spreads are small
check_track_spread(sdf_bovy14,'R','Z',0.01,0.005)
check_track_spread(sdf_bovy14,'R','Z',0.08,0.04,phys=True) #do 1 with phys
check_track_spread(sdf_bovy14,'R','Z',0.01,0.005,interp=False) #do 1 with interp
check_track_spread(sdf_bovy14,'X','Y',0.01,0.005)
check_track_spread(sdf_bovy14,'X','Y',0.08,0.04,phys=True) #do 1 with phys
check_track_spread(sdf_bovy14,'R','phi',0.01,0.005)
check_track_spread(sdf_bovy14,'vR','vT',0.005,0.005)
check_track_spread(sdf_bovy14,'vR','vT',1.1,1.1,phys=True) #do 1 with phys
check_track_spread(sdf_bovy14,'vR','vZ',0.005,0.005)
check_track_spread(sdf_bovy14,'vX','vY',0.005,0.005)
delattr(sdf_bovy14,'_allErrCovs') #to test that this is re-generated
check_track_spread(sdf_bovy14,'ll','bb',0.5,0.5)
check_track_spread(sdf_bovy14,'dist','vlos',0.5,5.)
check_track_spread(sdf_bovy14,'pmll','pmbb',0.5,0.5)
#These should all exist, so return None
assert sdf_bovy14._interpolate_stream_track() is None, '_interpolate_stream_track does not return None, even though it should be set up'
assert sdf_bovy14._interpolate_stream_track_aA() is None, '_interpolate_stream_track_aA does not return None, even though it should be set up'
delattr(sdf_bovy14,'_interpolatedObsTrackAA')
delattr(sdf_bovy14,'_interpolatedThetasTrack')
#Re-build
assert sdf_bovy14._interpolate_stream_track_aA() is None, 'Re-building interpolated AA track does not return None'
return None
def test_closest_trackpoint():
#Check that we can find the closest trackpoint properly
check_closest_trackpoint(sdf_bovy14,50)
check_closest_trackpoint(sdf_bovy14,230,usev=True)
check_closest_trackpoint(sdf_bovy14,330,usev=True,xy=False)
check_closest_trackpoint(sdf_bovy14,40,xy=False)
check_closest_trackpoint(sdf_bovy14,4,interp=False)
check_closest_trackpoint(sdf_bovy14,6,interp=False,usev=True,xy=False)
return None
def test_closest_trackpointLB():
#Check that we can find the closest trackpoint properly in LB
check_closest_trackpointLB(sdf_bovy14,50)
check_closest_trackpointLB(sdf_bovy14,230,usev=True)
check_closest_trackpointLB(sdf_bovy14,4,interp=False)
check_closest_trackpointLB(sdf_bovy14,8,interp=False,usev=True)
check_closest_trackpointLB(sdf_bovy14,-1,interp=False,usev=False)
check_closest_trackpointLB(sdf_bovy14,-2,interp=False,usev=True)
check_closest_trackpointLB(sdf_bovy14,-3,interp=False,usev=True)
return None
def test_closest_trackpointaA():
#Check that we can find the closest trackpoint properly in AA
check_closest_trackpointaA(sdf_bovy14,50)
check_closest_trackpointaA(sdf_bovy14,4,interp=False)
return None
def test_pOparapar():
#Test that integrating pOparapar gives density_par
dens_frompOpar_close=\
integrate.quad(lambda x: sdf_bovy14.pOparapar(x,0.1),
sdf_bovy14._meandO\
-4.*numpy.sqrt(sdf_bovy14._sortedSigOEig[2]),
sdf_bovy14._meandO\
+4.*numpy.sqrt(sdf_bovy14._sortedSigOEig[2]))[0]
dens_fromOpar_half=\
integrate.quad(lambda x: sdf_bovy14.pOparapar(x,1.1),
sdf_bovy14._meandO\
-4.*numpy.sqrt(sdf_bovy14._sortedSigOEig[2]),
sdf_bovy14._meandO\
+4.*numpy.sqrt(sdf_bovy14._sortedSigOEig[2]))[0]
assert numpy.fabs(dens_fromOpar_half/dens_frompOpar_close-sdf_bovy14.density_par(1.1)) < 10.**-4., 'density from integrating pOparapar not equal to that from density_par for Bovy14 stream'
return None
def test_density_par_valueerror():
# Test that the code throws a ValueError if coord is not understood
with pytest.raises(ValueError) as excinfo:
sdf_bovy14.density_par(0.1,coord='xi')
return None
def test_density_par():
#Test that the density is close to 1 close to the progenitor and close to zero far from the progenitor
assert numpy.fabs(sdf_bovy14.density_par(0.1)-1.) < 10.**-2., 'density near progenitor not close to 1 for Bovy14 stream'
assert numpy.fabs(sdf_bovy14.density_par(0.5)-1.) < 10.**-2., 'density near progenitor not close to 1 for Bovy14 stream'
assert numpy.fabs(sdf_bovy14.density_par(1.8)-0.) < 10.**-2., 'density far progenitor not close to 0 for Bovy14 stream'
return None
def test_density_phi():
#Test that the density in phi is correctly computed, by doing this by hand
def dens_phi(apar):
dapar= 10.**-9.
X,Y,Z= sdf_bovy14._interpTrackX(apar), sdf_bovy14._interpTrackY(apar),\
sdf_bovy14._interpTrackZ(apar)
R,phi,z= bovy_coords.rect_to_cyl(X,Y,Z)
dX,dY,dZ= sdf_bovy14._interpTrackX(apar+dapar),\
sdf_bovy14._interpTrackY(apar+dapar),\
sdf_bovy14._interpTrackZ(apar+dapar)
dR,dphi,dz= bovy_coords.rect_to_cyl(dX,dY,dZ)
jac= numpy.fabs((dphi-phi)/dapar)
return sdf_bovy14.density_par(apar)/jac
apar= 0.1
assert numpy.fabs(dens_phi(apar)/sdf_bovy14.density_par(apar,coord='phi')-1.) < 10.**-2., \
'density near progenitor in phi is incorrect'
apar= 0.5
assert numpy.fabs(dens_phi(apar)/sdf_bovy14.density_par(apar,coord='phi')-1.) < 10.**-2., \
'density near progenitor in phi is incorrect'
apar= 1.8
assert numpy.fabs(dens_phi(apar)/sdf_bovy14.density_par(apar,coord='phi')-1.) < 10.**-2., \
'density far from progenitor in phi is incorrect'
return None
def test_density_ll_and_customra():
#Test that the density in ll is correctly computed, by doing this by hand
# custom should be the same for this setup (see above)
def dens_ll(apar):
dapar= 10.**-9.
X,Y,Z= sdf_bovy14._interpTrackX(apar)*sdf_bovy14._ro, \
sdf_bovy14._interpTrackY(apar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar)*sdf_bovy14._ro
X,Y,Z= bovy_coords.galcenrect_to_XYZ(X,Y,Z,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
l,b,d= bovy_coords.XYZ_to_lbd(X,Y,Z,degree=True)
dX,dY,dZ= sdf_bovy14._interpTrackX(apar+dapar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackY(apar+dapar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar+dapar)*sdf_bovy14._ro
dX,dY,dZ= bovy_coords.galcenrect_to_XYZ(dX,dY,dZ,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
dl,db,dd= bovy_coords.XYZ_to_lbd(dX,dY,dZ,degree=True)
jac= numpy.fabs((dl-l)/dapar)
return sdf_bovy14.density_par(apar)/jac
apar= 0.1
assert numpy.fabs(dens_ll(apar)/sdf_bovy14.density_par(apar,coord='ll')-1.) < 10.**-2., \
'density near progenitor in ll is incorrect'
assert numpy.fabs(dens_ll(apar)/sdf_bovy14.density_par(apar,coord='customra')-1.) < 10.**-2., \
'density near progenitor in ll is incorrect'
apar= 0.5
assert numpy.fabs(dens_ll(apar)/sdf_bovy14.density_par(apar,coord='ll')-1.) < 10.**-2., \
'density near progenitor in ll is incorrect'
assert numpy.fabs(dens_ll(apar)/sdf_bovy14.density_par(apar,coord='customra')-1.) < 10.**-2., \
'density near progenitor in ll is incorrect'
apar= 1.8
assert numpy.fabs(dens_ll(apar)/sdf_bovy14.density_par(apar,coord='ll')-1.) < 10.**-2., \
'density far from progenitor in ll is incorrect'
assert numpy.fabs(dens_ll(apar)/sdf_bovy14.density_par(apar,coord='customra')-1.) < 10.**-2., \
'density far from progenitor in ll is incorrect'
return None
def test_density_ra():
#Test that the density in ra is correctly computed, by doing this by hand
def dens_ra(apar):
dapar= 10.**-9.
X,Y,Z= sdf_bovy14._interpTrackX(apar)*sdf_bovy14._ro, \
sdf_bovy14._interpTrackY(apar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar)*sdf_bovy14._ro
X,Y,Z= bovy_coords.galcenrect_to_XYZ(X,Y,Z,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
l,b,d= bovy_coords.XYZ_to_lbd(X,Y,Z,degree=True)
ra,dec= bovy_coords.lb_to_radec(l,b,degree=True)
dX,dY,dZ= sdf_bovy14._interpTrackX(apar+dapar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackY(apar+dapar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar+dapar)*sdf_bovy14._ro
dX,dY,dZ= bovy_coords.galcenrect_to_XYZ(dX,dY,dZ,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
dl,db,dd= bovy_coords.XYZ_to_lbd(dX,dY,dZ,degree=True)
dra,ddec= bovy_coords.lb_to_radec(dl,db,degree=True)
jac= numpy.fabs((dra-ra)/dapar)
return sdf_bovy14.density_par(apar)/jac
apar= 0.1
assert numpy.fabs(dens_ra(apar)/sdf_bovy14.density_par(apar,coord='ra')-1.) < 10.**-2., \
'density near progenitor in ra is incorrect'
apar= 0.5
assert numpy.fabs(dens_ra(apar)/sdf_bovy14.density_par(apar,coord='ra')-1.) < 10.**-2., \
'density near progenitor in ra is incorrect'
apar= 1.8
assert numpy.fabs(dens_ra(apar)/sdf_bovy14.density_par(apar,coord='ra')-1.) < 10.**-2., \
'density far from progenitor in ra is incorrect'
return None
def test_density_ll_wsampling():
# Test that the density computed using density_par is correct using a
# random sample
numpy.random.seed(1)
def ll(apar):
"""Quick function that returns l for a given apar"""
X,Y,Z= sdf_bovy14._interpTrackX(apar)*sdf_bovy14._ro, \
sdf_bovy14._interpTrackY(apar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar)*sdf_bovy14._ro
X,Y,Z= bovy_coords.galcenrect_to_XYZ(X,Y,Z,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
l,b,d= bovy_coords.XYZ_to_lbd(X,Y,Z,degree=True)
return l
LB= sdf_bovy14.sample(n=10000,lb=True)
apar1, apar2= 0.1, 0.6
dens1= float(numpy.sum((LB[0] > ll(apar1))*(LB[0] < ll(apar1)+2.)))
dens2= float(numpy.sum((LB[0] > ll(apar2))*(LB[0] < ll(apar2)+2.)))
dens1_calc= sdf_bovy14.density_par(apar1,coord='ll')
dens2_calc= sdf_bovy14.density_par(apar2,coord='ll')
assert numpy.fabs(dens1/dens2-dens1_calc/dens2_calc) < 0.1, 'density in ll computed using density_par does not agree with density from random sample'
return None
def test_length():
# Test that the length is correct according to its definition
thresh= 0.2
assert numpy.fabs(sdf_bovy14.density_par(\
sdf_bovy14.length(threshold=thresh))/sdf_bovy14.density_par(0.1)-thresh) < 10.**-3., 'Stream length does not conform to its definition'
thresh= 0.05
assert numpy.fabs(sdf_bovy14.density_par(\
sdf_bovy14.length(threshold=thresh))/sdf_bovy14.density_par(0.1)-thresh) < 10.**-3., 'Stream length does not conform to its definition'
return None
def test_length_valueerror():
thresh= 0.00001
with pytest.raises(ValueError) as excinfo:
assert numpy.fabs(sdf_bovy14.density_par(\
sdf_bovy14.length(threshold=thresh))/sdf_bovy14.density_par(0.1)-thresh) < 10.**-3., 'Stream length does not conform to its definition'
return None
def test_length_ang():
# Test that this is roughly correct
def dphidapar(apar):
dapar= 10.**-9.
X,Y,Z= sdf_bovy14._interpTrackX(apar)*sdf_bovy14._ro, \
sdf_bovy14._interpTrackY(apar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar)*sdf_bovy14._ro
X,Y,Z= bovy_coords.galcenrect_to_XYZ(X,Y,Z,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
l,b,d= bovy_coords.XYZ_to_lbd(X,Y,Z,degree=True)
dX,dY,dZ= sdf_bovy14._interpTrackX(apar+dapar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackY(apar+dapar)*sdf_bovy14._ro,\
sdf_bovy14._interpTrackZ(apar+dapar)*sdf_bovy14._ro
dX,dY,dZ= bovy_coords.galcenrect_to_XYZ(dX,dY,dZ,
Xsun=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun)
dl,db,dd= bovy_coords.XYZ_to_lbd(dX,dY,dZ,degree=True)
jac= numpy.fabs((dl-l)/dapar)
return jac
thresh= 0.2
assert numpy.fabs(sdf_bovy14.length(threshold=thresh)*dphidapar(0.3)
-sdf_bovy14.length(threshold=thresh,ang=True)) < 10., 'Length in angular coordinates does not conform to rough expectation'
# Dangerous hack to test case where l decreases along the stream
sdf_bovy14._interpolatedObsTrackLB[:,:2]*= -1.
thresh= 0.2
assert numpy.fabs(sdf_bovy14.length(threshold=thresh)*dphidapar(0.3)
-sdf_bovy14.length(threshold=thresh,ang=True)) < 10., 'Length in angular coordinates does not conform to rough expectation'
# Go back
sdf_bovy14._interpolatedObsTrackLB[:,:2]*= -1.
return None
def test_length_phys():
# Test that this is roughly correct
def dxdapar(apar):
dapar= 10.**-9.
X,Y,Z= sdf_bovy14._interpTrackX(apar),\
sdf_bovy14._interpTrackY(apar),\
sdf_bovy14._interpTrackZ(apar)
dX,dY,dZ= sdf_bovy14._interpTrackX(apar+dapar),\
sdf_bovy14._interpTrackY(apar+dapar),\
sdf_bovy14._interpTrackZ(apar+dapar)
jac= numpy.sqrt(((dX-X)/dapar)**2.\
+((dY-Y)/dapar)**2.\
+((dZ-Z)/dapar)**2.)
return jac*sdf_bovy14._ro
thresh= 0.2
assert numpy.fabs(sdf_bovy14.length(threshold=thresh)*dxdapar(0.3)
-sdf_bovy14.length(threshold=thresh,phys=True)) < 1., 'Length in physical coordinates does not conform to rough expectation'
return None
def test_meanOmega():
#Test that meanOmega is close to constant and the mean Omega close to the progenitor
assert numpy.all(numpy.fabs(sdf_bovy14.meanOmega(0.1)-sdf_bovy14._progenitor_Omega) < 10.**-2.), 'meanOmega near progenitor not close to mean Omega for Bovy14 stream'
assert numpy.all(numpy.fabs(sdf_bovy14.meanOmega(0.5)-sdf_bovy14._progenitor_Omega) < 10.**-2.), 'meanOmega near progenitor not close to mean Omega for Bovy14 stream'
return None
def test_meanOmega_oned():
#Test that meanOmega is close to constant and the mean Omega close to the progenitor
assert numpy.fabs(sdf_bovy14.meanOmega(0.1,oned=True)) < 10.**-2., 'One-dimensional meanOmega near progenitor not close to zero for Bovy14 stream'
assert numpy.fabs(sdf_bovy14.meanOmega(0.5,oned=True)) < 10.**-2., 'Oned-dimensional meanOmega near progenitor not close to zero for Bovy14 stream'
return None
def test_sigOmega_constant():
#Test that sigOmega is close to constant close to the progenitor
assert numpy.fabs(sdf_bovy14.sigOmega(0.1)-sdf_bovy14.sigOmega(0.5)) < 10.**-4., 'sigOmega near progenitor not close to constant for Bovy14 stream'
return None
def test_sigOmega_small():
#Test that sigOmega is smaller than the total spread
assert sdf_bovy14.sigOmega(0.1) < numpy.sqrt(sdf_bovy14._sortedSigOEig[2]), 'sigOmega near progenitor not smaller than the total Omega spread'
assert sdf_bovy14.sigOmega(0.5) < numpy.sqrt(sdf_bovy14._sortedSigOEig[2]), 'sigOmega near progenitor not smaller than the total Omega spread'
assert sdf_bovy14.sigOmega(1.2) < numpy.sqrt(sdf_bovy14._sortedSigOEig[2]), 'sigOmega near progenitor not smaller than the total Omega spread'
return None
def test_meantdAngle():
#Test that the mean td for a given angle is close to what's expected
assert numpy.fabs((sdf_bovy14.meantdAngle(0.1)-0.1/sdf_bovy14._meandO)/sdf_bovy14.meantdAngle(0.1)) < 10.**-1.5, 'mean td close to the progenitor is not dangle/dO'
assert numpy.fabs((sdf_bovy14.meantdAngle(0.4)-0.4/sdf_bovy14._meandO)/sdf_bovy14.meantdAngle(0.1)) < 10.**-0.9, 'mean td close to the progenitor is not dangle/dO'
return None
def test_sigtdAngle():
#Test that the sigma of td for a given angle is small
assert sdf_bovy14.sigtdAngle(0.1) < 0.2*0.1/sdf_bovy14._meandO, 'sigma of td close to the progenitor is not small'
assert sdf_bovy14.sigtdAngle(0.5) > 0.2*0.1/sdf_bovy14._meandO, 'sigma of td in the middle of the stream is not large'
return None
def test_ptdAngle():
#Test that the probability distribution for p(td|angle) is reasonable
#at 0.1
da= 0.1
expected_max= da/sdf_bovy14._meandO
assert sdf_bovy14.ptdAngle(expected_max,da) > \
sdf_bovy14.ptdAngle(2.*expected_max,da), 'ptdAngle does not peak close to where it is expected to peak'
assert sdf_bovy14.ptdAngle(expected_max,da) > \
sdf_bovy14.ptdAngle(0.5*expected_max,da), 'ptdAngle does not peak close to where it is expected to peak'
#at 0.6
da= 0.6
expected_max= da/sdf_bovy14._meandO
assert sdf_bovy14.ptdAngle(expected_max,da) > \
sdf_bovy14.ptdAngle(2.*expected_max,da), 'ptdAngle does not peak close to where it is expected to peak'
assert sdf_bovy14.ptdAngle(expected_max,da) > \
sdf_bovy14.ptdAngle(0.5*expected_max,da), 'ptdAngle does not peak close to where it is expected to peak'
#Now test that the mean and sigma calculated with a simple Riemann sum agrees with meantdAngle
da= 0.2
ts= numpy.linspace(0.,100.,1001)
pts= numpy.array([sdf_bovy14.ptdAngle(t,da) for t in ts])
assert numpy.fabs((numpy.sum(ts*pts)/numpy.sum(pts)\
-sdf_bovy14.meantdAngle(da))/sdf_bovy14.meantdAngle(da)) < 10.**-2., 'mean td at angle 0.2 calculated with Riemann sum does not agree with that calculated by meantdAngle'
assert numpy.fabs((numpy.sqrt(numpy.sum(ts**2.*pts)/numpy.sum(pts)-(numpy.sum(ts*pts)/numpy.sum(pts))**2.)\
-sdf_bovy14.sigtdAngle(da))/sdf_bovy14.sigtdAngle(da)) < 10.**-1.5, 'sig td at angle 0.2 calculated with Riemann sum does not agree with that calculated by meantdAngle'
return None
def test_meanangledAngle():
#Test that the mean perpendicular angle at a given angle is zero
da= 0.1
assert numpy.fabs(sdf_bovy14.meanangledAngle(da,smallest=False)) < 10.**-2, 'mean perpendicular angle not zero'
assert numpy.fabs(sdf_bovy14.meanangledAngle(da,smallest=True)) < 10.**-2, 'mean perpendicular angle not zero'
da= 1.1
assert numpy.fabs(sdf_bovy14.meanangledAngle(da,smallest=False)) < 10.**-2, 'mean perpendicular angle not zero'
assert numpy.fabs(sdf_bovy14.meanangledAngle(da,smallest=True)) < 10.**-2, 'mean perpendicular angle not zero'
return None
def test_sigangledAngle():
#Test that the spread in perpendicular angle is much smaller than 1 (the typical spread in the parallel angle)
da= 0.1
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=True,smallest=False,
simple=False) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=True,smallest=True,
simple=False) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
da= 1.1
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=True,smallest=False,
simple=False) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=True,smallest=True,
simple=False) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
#w/o assuming zeroMean
da= 0.1
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=False,smallest=False,
simple=False) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=False,smallest=True,
simple=False) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
#simple estimate
da= 0.1
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=False,smallest=False,
simple=True) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
assert sdf_bovy14.sigangledAngle(da,assumeZeroMean=False,smallest=True,
simple=True) \
< 1./sdf_bovy14.freqEigvalRatio(), \
'spread in perpendicular angle is not small'
return None
def test_pangledAngle():
#Sanity check pangledAngle, does it peak near zero? Does the mean agree with meandAngle, does the sigma agree with sigdAngle?
da= 0.1
assert sdf_bovy14.pangledAngle(0.,da,smallest=False) > \
sdf_bovy14.pangledAngle(0.1,da,smallest=False), 'pangledAngle does not peak near zero'
assert sdf_bovy14.pangledAngle(0.,da,smallest=False) > \
sdf_bovy14.pangledAngle(-0.1,da,smallest=False), 'pangledAngle does not peak near zero'
#also for smallest
assert sdf_bovy14.pangledAngle(0.,da,smallest=True) > \
sdf_bovy14.pangledAngle(0.1,da,smallest=False), 'pangledAngle does not peak near zero'
assert sdf_bovy14.pangledAngle(0.,da,smallest=True) > \
sdf_bovy14.pangledAngle(-0.1,da,smallest=False), 'pangledAngle does not peak near zero'
dangles= numpy.linspace(-0.01,0.01,201)
pdangles= (numpy.array([sdf_bovy14.pangledAngle(pda,da,smallest=False) for pda in dangles])).flatten()
assert numpy.fabs(numpy.sum(dangles*pdangles)/numpy.sum(pdangles)) < 10.**-2., 'mean calculated using Riemann sum of pangledAngle does not agree with actual mean'
acsig= sdf_bovy14.sigangledAngle(da,assumeZeroMean=True,smallest=False,simple=False)
assert numpy.fabs((numpy.sqrt(numpy.sum(dangles**2.*pdangles)/numpy.sum(pdangles))-acsig)/acsig) < 10.**-2., 'sigma calculated using Riemann sum of pangledAngle does not agree with actual sigma'
#also for smallest
pdangles= (numpy.array([sdf_bovy14.pangledAngle(pda,da,smallest=True) for pda in dangles])).flatten()
assert numpy.fabs(numpy.sum(dangles*pdangles)/numpy.sum(pdangles)) < 10.**-2., 'mean calculated using Riemann sum of pangledAngle does not agree with actual mean'
acsig= sdf_bovy14.sigangledAngle(da,assumeZeroMean=True,smallest=True,simple=False)
assert numpy.fabs((numpy.sqrt(numpy.sum(dangles**2.*pdangles)/numpy.sum(pdangles))-acsig)/acsig) < 10.**-1.95, 'sigma calculated using Riemann sum of pangledAngle does not agree with actual sigma'
return None
def test_bovy14_approxaA_inv():
#Test that the approximate action-angle conversion near the track works, ie, that the inverse gives the initial point
#Point on track, interpolated
RvR= sdf_bovy14._interpolatedObsTrack[22,:]
check_approxaA_inv(sdf_bovy14,-5.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=True)
#Point on track, not interpolated
RvR= sdf_bovy14._interpolatedObsTrack[152,:]
check_approxaA_inv(sdf_bovy14,-3.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=False)
#Point near track, interpolated
RvR= sdf_bovy14._interpolatedObsTrack[22,:]*(1.+10.**-2.)
check_approxaA_inv(sdf_bovy14,-2.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=True)
#Point near track, not interpolated
RvR= sdf_bovy14._interpolatedObsTrack[152,:]*(1.+10.**-2.)
check_approxaA_inv(sdf_bovy14,-2.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=False)
#Point near end of track, interpolated
RvR= sdf_bovy14._interpolatedObsTrack[-23,:]
check_approxaA_inv(sdf_bovy14,-2.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=True)
#Point near end of track, not interpolated
RvR= sdf_bovy14._interpolatedObsTrack[-23,:]
check_approxaA_inv(sdf_bovy14,-2.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=False)
#Now find some trackpoints close to where angles wrap, to test that wrapping is covered properly everywhere
dphi= numpy.roll(sdf_bovy14._interpolatedObsTrack[:,5],-1)-\
sdf_bovy14._interpolatedObsTrack[:,5]
indx= dphi < 0.
RvR= sdf_bovy14._interpolatedObsTrack[indx,:][0,:]*(1.+10.**-2.)
check_approxaA_inv(sdf_bovy14,-2.,
RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=False)
return None
def test_bovy14_gaussApprox_onemissing():
#Test the Gaussian approximation
#First, test near an interpolated point, without using interpolation (non-trivial)
tol= -3.
trackp= 110
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
# X
XvX[0]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,0]) < 10.**tol, 'gaussApprox along track does not work for X'
# Y
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[1]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,1]) < 10.**tol, 'gaussApprox along track does not work for Y'
# Z
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[2]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for Z'
# vX
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[3]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vX'
# vY
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[4]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for vY'
# vZ
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[5]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for vZ'
return None
def test_bovy14_gaussApprox_threemissing():
#Test the Gaussian approximation
#First, test near an interpolated point, without using interpolation (non-trivial)
tol= -3.
trackp= 110
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
# X,vX,vY
XvX[0]= None
XvX[3]= None
XvX[4]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,0]) < 10.**tol, 'gaussApprox along track does not work for X'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackXY[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vX'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackXY[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for vY'
# Y,Z,vZ
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[1]= None
XvX[2]= None
XvX[5]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,1]) < 10.**tol, 'gaussApprox along track does not work for Y'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackXY[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for Z'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackXY[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for vZ'
return None
def test_bovy14_gaussApprox_fivemissing():
#Test the Gaussian approximation
#Test near an interpolation point
tol= -3.
trackp= 110
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
# X,Z,vX,vY,vZ
XvX[0]= None
XvX[2]= None
XvX[3]= None
XvX[4]= None
XvX[5]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False,cindx=1)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,0]) < 10.**tol, 'gaussApprox along track does not work for X'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackXY[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for Z'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackXY[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vX'
assert numpy.fabs(meanp[3]-sdf_bovy14._interpolatedObsTrackXY[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for vY'
assert numpy.fabs(meanp[4]-sdf_bovy14._interpolatedObsTrackXY[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for vZ'
# Y,Z,vX,vY,vZ
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[1]= None
XvX[2]= None
XvX[3]= None
XvX[4]= None
XvX[5]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=False,cindx=1)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,1]) < 10.**tol, 'gaussApprox along track does not work for Y'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackXY[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for Z'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackXY[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vX'
assert numpy.fabs(meanp[3]-sdf_bovy14._interpolatedObsTrackXY[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for vY'
assert numpy.fabs(meanp[4]-sdf_bovy14._interpolatedObsTrackXY[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for vZ'
return None
def test_bovy14_gaussApprox_interp():
#Tests of Gaussian approximation when using interpolation
tol= -10.
trackp= 234
XvX= list(sdf_bovy14._interpolatedObsTrackXY[trackp,:].flatten())
XvX[1]= None
XvX[2]= None
meanp, varp= sdf_bovy14.gaussApprox(XvX,interp=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,1]) < 10.**tol, 'Gaussian approximation when using interpolation does not work as expected for Y'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackXY[trackp,2]) < 10.**tol, 'Gaussian approximation when using interpolation does not work as expected for Y'
#also w/ default (which should be interp=True)
meanp, varp= sdf_bovy14.gaussApprox(XvX)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackXY[trackp,1]) < 10.**tol, 'Gaussian approximation when using interpolation does not work as expected for Y'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackXY[trackp,2]) < 10.**tol, 'Gaussian approximation when using interpolation does not work as expected for Y'
return None
def test_bovy14_gaussApproxLB_onemissing():
#Test the Gaussian approximation
#First, test near an interpolated point, without using interpolation (non-trivial)
tol= -2.
trackp= 102
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
# l
LB[0]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,0]) < 10.**tol, 'gaussApprox along track does not work for l'
# b
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[1]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,1]) < 10.**tol, 'gaussApprox along track does not work for b'
# d
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[2]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for d'
# vlos
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[3]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vlos'
# pmll
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[4]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for pmll'
# pmbb
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[5]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for pmbb'
return None
def test_bovy14_gaussApproxLB_threemissing():
#Test the Gaussian approximation
#First, test near an interpolated point, without using interpolation (non-trivial)
tol= -1.8
trackp= 102
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
# l,vlos,pmll
LB[0]= None
LB[3]= None
LB[4]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,0]) < 10.**tol, 'gaussApprox along track does not work for l'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackLB[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vlos'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackLB[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for pmll'
# b,d,pmbb
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[1]= None
LB[2]= None
LB[5]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,1]) < 10.**tol, 'gaussApprox along track does not work for b'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackLB[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for d'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackLB[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for pmbb'
return None
def test_bovy14_gaussApproxLB_fivemissing():
#Test the Gaussian approximation
#Test near an interpolation point
tol= -1.98 #vlos just doesn't make -2.
trackp= 102
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
# X,Z,vX,vY,vZ
LB[0]= None
LB[2]= None
LB[3]= None
LB[4]= None
LB[5]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,cindx=1,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,0]) < 10.**tol, 'gaussApprox along track does not work for l'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackLB[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for d'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackLB[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vlos'
assert numpy.fabs(meanp[3]-sdf_bovy14._interpolatedObsTrackLB[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for pmll'
assert numpy.fabs(meanp[4]-sdf_bovy14._interpolatedObsTrackLB[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for pmbb'
# b,d,vlos,pmll,pmbb
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[1]= None
LB[2]= None
LB[3]= None
LB[4]= None
LB[5]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=False,cindx=1,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,1]) < 10.**tol, 'gaussApprox along track does not work for b'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackLB[trackp,2]) < 10.**tol, 'gaussApprox along track does not work for d'
assert numpy.fabs(meanp[2]-sdf_bovy14._interpolatedObsTrackLB[trackp,3]) < 10.**tol, 'gaussApprox along track does not work for vlos'
assert numpy.fabs(meanp[3]-sdf_bovy14._interpolatedObsTrackLB[trackp,4]) < 10.**tol, 'gaussApprox along track does not work for pmll'
assert numpy.fabs(meanp[4]-sdf_bovy14._interpolatedObsTrackLB[trackp,5]) < 10.**tol, 'gaussApprox along track does not work for pmbb'
return None
def test_bovy14_gaussApproxLB_interp():
#Tests of Gaussian approximation when using interpolation
tol= -10.
trackp= 234
LB= list(sdf_bovy14._interpolatedObsTrackLB[trackp,:].flatten())
LB[1]= None
LB[2]= None
meanp, varp= sdf_bovy14.gaussApprox(LB,interp=True,lb=True)
assert numpy.fabs(meanp[0]-sdf_bovy14._interpolatedObsTrackLB[trackp,1]) < 10.**tol, 'Gaussian approximation when using interpolation does not work as expected for b'
assert numpy.fabs(meanp[1]-sdf_bovy14._interpolatedObsTrackLB[trackp,2]) < 10.**tol, 'Gaussian approximation when using interpolation does not work as expected for d'
return None
def test_bovy14_callMargXZ():
#Example from the tutorial and paper
meanp, varp= sdf_bovy14.gaussApprox([None,None,2./8.,None,None,None])
xs= numpy.linspace(-3.*numpy.sqrt(varp[0,0]),3.*numpy.sqrt(varp[0,0]),
11)+meanp[0]
logps= numpy.array([sdf_bovy14.callMarg([x,None,2./8.,None,None,None])
for x in xs])
ps= numpy.exp(logps)
ps/= numpy.sum(ps)*(xs[1]-xs[0])*8.
#Test that the mean is close to the approximation
assert numpy.fabs(numpy.sum(xs*ps)/numpy.sum(ps)-meanp[0]) < 10.**-2., 'mean of full PDF calculation does not agree with Gaussian approximation to the level at which this is expected for p(X|Z)'
assert numpy.fabs(numpy.sqrt(numpy.sum(xs**2.*ps)/numpy.sum(ps)-(numpy.sum(xs*ps)/numpy.sum(ps))**2.)-numpy.sqrt(varp[0,0])) < 10.**-2., 'sigma of full PDF calculation does not agree with Gaussian approximation to the level at which this is expected for p(X|Z)'
#Test that the mean is close to the approximation, when explicitly setting sigma and ngl
logps= numpy.array([sdf_bovy14.callMarg([x,None,2./8.,None,None,None],
ngl=6,nsigma=3.1)
for x in xs])
ps= numpy.exp(logps)
ps/= numpy.sum(ps)*(xs[1]-xs[0])*8.
assert numpy.fabs(numpy.sum(xs*ps)/numpy.sum(ps)-meanp[0]) < 10.**-2., 'mean of full PDF calculation does not agree with Gaussian approximation to the level at which this is expected for p(X|Z)'
assert numpy.fabs(numpy.sqrt(numpy.sum(xs**2.*ps)/numpy.sum(ps)-(numpy.sum(xs*ps)/numpy.sum(ps))**2.)-numpy.sqrt(varp[0,0])) < 10.**-2., 'sigma of full PDF calculation does not agree with Gaussian approximation to the level at which this is expected for p(X|Z)'
return None
def test_bovy14_callMargDPMLL():
#p(D|pmll)
meanp, varp= sdf_bovy14.gaussApprox([None,None,None,None,8.,None],lb=True)
xs= numpy.linspace(-3.*numpy.sqrt(varp[1,1]),3.*numpy.sqrt(varp[1,1]),
11)+meanp[1]
logps= numpy.array([sdf_bovy14.callMarg([None,x,None,None,8.,None],
lb=True)
for x in xs])
ps= numpy.exp(logps)
ps/= numpy.sum(ps)*(xs[1]-xs[0])
#Test that the mean is close to the approximation
assert numpy.fabs(numpy.sum(xs*ps)/numpy.sum(ps)-meanp[1]) < 10.**-2., 'mean of full PDF calculation does not agree with Gaussian approximation to the level at which this is expected for p(D|pmll)'
assert numpy.fabs(numpy.sqrt(numpy.sum(xs**2.*ps)/numpy.sum(ps)-(numpy.sum(xs*ps)/numpy.sum(ps))**2.)-numpy.sqrt(varp[1,1])) < 10.**-1., 'sigma of full PDF calculation does not agree with Gaussian approximation to the level at which this is expected for p(D|pmll)'
#Test options
assert numpy.fabs(sdf_bovy14.callMarg([None,meanp[1],None,None,8.,None],
lb=True)-
sdf_bovy14.callMarg([None,meanp[1],None,None,8.,None],
lb=True,
ro=sdf_bovy14._ro,
vo=sdf_bovy14._vo,
R0=sdf_bovy14._R0,
Zsun=sdf_bovy14._Zsun,
vsun=sdf_bovy14._vsun)) < 10.**-10., 'callMarg with ro, etc. options set to default does not agree with default'
cindx= sdf_bovy14.find_closest_trackpointLB(None,meanp[1],None,
None,8.,None,
usev=True)
assert numpy.fabs(sdf_bovy14.callMarg([None,meanp[1],None,None,8.,None],
lb=True)-
sdf_bovy14.callMarg([None,meanp[1],None,None,8.,None],
lb=True,
cindx=cindx,interp=True)) < 10.**10., 'callMarg with cindx set does not agree with it set to default'
if cindx % 100 > 50: cindx= cindx//100+1
else: cindx= cindx//100
assert numpy.fabs(sdf_bovy14.callMarg([None,meanp[1],None,None,8.,None],
lb=True,interp=False)-
sdf_bovy14.callMarg([None,meanp[1],None,None,8.,None],
lb=True,interp=False,
cindx=cindx)) < 10.**10., 'callMarg with cindx set does not agree with it set to default'
#Same w/o interpolation
return None
def test_callArgs():
#Tests of _parse_call_args
from galpy.orbit import Orbit
#Just checking that different types of inputs give the same result
trackp= 191
RvR= sdf_bovy14._interpolatedObsTrack[trackp,:].flatten()
OA= sdf_bovy14._interpolatedObsTrackAA[trackp,:].flatten()
#RvR vs. array of OA
s= numpy.ones(2)
assert numpy.all(numpy.fabs(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5])\
-sdf_bovy14(OA[0]*s,OA[1]*s,OA[2]*s,OA[3]*s,OA[4]*s,OA[5]*s,aAInput=True)) < 10.**-8.), '__call__ w/ R,vR,... and equivalent O,theta,... does not give the same result'
#RvR vs. OA
assert numpy.fabs(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5])\
-sdf_bovy14(OA[0],OA[1],OA[2],OA[3],OA[4],OA[5],aAInput=True)) < 10.**-8., '__call__ w/ R,vR,... and equivalent O,theta,... does not give the same result'
#RvR vs. orbit
assert numpy.fabs(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5])\
-sdf_bovy14(Orbit([RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5]]))) < 10.**-8., '__call__ w/ R,vR,... and equivalent orbit does not give the same result'
#RvR vs. list of orbit
assert numpy.fabs(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5])\
-sdf_bovy14([Orbit([RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5]])])) < 10.**-8., '__call__ w/ R,vR,... and equivalent list of orbit does not give the same result'
#RvR w/ and w/o log
assert numpy.fabs(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5])\
-numpy.log(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],log=False))) < 10.**-8., '__call__ w/ R,vR,... log and not log does not give the same result'
#RvR w/ explicit interp
assert numpy.fabs(sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5])\
-sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4],RvR[5],interp=True)) < 10.**-8., '__call__ w/ R,vR,... w/ explicit interp does not give the same result as w/o'
#RvR w/o phi should raise error
try:
sdf_bovy14(RvR[0],RvR[1],RvR[2],RvR[3],RvR[4])
except IOError: pass
else: raise AssertionError('__call__ w/o phi does not raise IOError')
return None
def test_bovy14_sample():
numpy.random.seed(1)
RvR= sdf_bovy14.sample(n=1000)
#Sanity checks
# Range in Z
indx= (RvR[3] > 4./8.)*(RvR[3] < 5./8.)
meanp, varp= sdf_bovy14.gaussApprox([None,None,4.5/8.,None,None,None])
#mean
assert numpy.fabs(numpy.sqrt(meanp[0]**2.+meanp[1]**2.)\
-numpy.mean(RvR[0][indx])) < 10.**-2., 'Sample track does not lie in the same location as the track'
assert numpy.fabs(meanp[4]-numpy.mean(RvR[4][indx])) < 10.**-2., 'Sample track does not lie in the same location as the track'
#variance, use smaller range
RvR= sdf_bovy14.sample(n=10000)
indx= (RvR[3] > 4.4/8.)*(RvR[3] < 4.6/8.)
assert numpy.fabs(numpy.sqrt(varp[4,4])/numpy.std(RvR[4][indx])-1.) < 10.**0., 'Sample spread not similar to track spread'
# Test that t is returned
RvRdt= sdf_bovy14.sample(n=10,returndt=True)
assert len(RvRdt) == 7, 'dt not returned with returndt in sample'
return None
def test_bovy14_sampleXY():
XvX= sdf_bovy14.sample(n=1000,xy=True)
#Sanity checks
# Range in Z
indx= (XvX[2] > 4./8.)*(XvX[2] < 5./8.)
meanp, varp= sdf_bovy14.gaussApprox([None,None,4.5/8.,None,None,None])
#mean
assert numpy.fabs(meanp[0]-numpy.mean(XvX[0][indx])) < 10.**-2., 'Sample track does not lie in the same location as the track'
assert numpy.fabs(meanp[1]-numpy.mean(XvX[1][indx])) < 10.**-2., 'Sample track does not lie in the same location as the track'
assert numpy.fabs(meanp[3]-numpy.mean(XvX[4][indx])) < 10.**-2., 'Sample track does not lie in the same location as the track'
#variance, use smaller range
XvX= sdf_bovy14.sample(n=10000)
indx= (XvX[3] > 4.4/8.)*(XvX[3] < 4.6/8.)
assert numpy.fabs(numpy.sqrt(varp[0,0])/numpy.std(XvX[0][indx])-1.) < 10.**0., 'Sample spread not similar to track spread'
# Test that t is returned
XvXdt= sdf_bovy14.sample(n=10,returndt=True,xy=True)
assert len(XvXdt) == 7, 'dt not returned with returndt in sample'
return None
def test_bovy14_sampleLB():
LB= sdf_bovy14.sample(n=1000,lb=True)
#Sanity checks
# Range in l
indx= (LB[0] > 212.5)*(LB[0] < 217.5)
meanp, varp= sdf_bovy14.gaussApprox([215,None,None,None,None,None],lb=True)
#mean
assert numpy.fabs((meanp[0]-numpy.mean(LB[1][indx]))/meanp[0]) < 10.**-2., 'Sample track does not lie in the same location as the track'
assert numpy.fabs((meanp[1]-numpy.mean(LB[2][indx]))/meanp[1]) < 10.**-2., 'Sample track does not lie in the same location as the track'
assert numpy.fabs((meanp[3]-numpy.mean(LB[4][indx]))/meanp[3]) < 10.**-2., 'Sample track does not lie in the same location as the track'
#variance, use smaller range
LB= sdf_bovy14.sample(n=10000,lb=True)
indx= (LB[0] > 214.)*(LB[0] < 216.)
assert numpy.fabs(numpy.sqrt(varp[0,0])/numpy.std(LB[1][indx])-1.) < 10.**0., 'Sample spread not similar to track spread'
# Test that t is returned
LBdt= sdf_bovy14.sample(n=10,returndt=True,lb=True)
assert len(LBdt) == 7, 'dt not returned with returndt in sample'
return None
def test_bovy14_sampleA():
AA= sdf_bovy14.sample(n=1000,returnaAdt=True)
#Sanity checks
indx= (AA[0][0] > 0.5625)*(AA[0][0] < 0.563)
assert numpy.fabs(numpy.mean(AA[0][2][indx])-0.42525) < 10.**-1., "Sample's vertical frequency at given radial frequency is not as expected"
#Sanity check w/ time
AA= sdf_bovy14.sample(n=10000,returnaAdt=True)
daperp= numpy.sqrt(numpy.sum((AA[1]
-numpy.tile(sdf_bovy14._progenitor_angle,(10000,1)).T)**2.,axis=0))
indx= (daperp > 0.24)*(daperp < 0.26)
assert numpy.fabs((numpy.mean(AA[2][indx])-sdf_bovy14.meantdAngle(0.25))/numpy.mean(AA[2][indx])) < 10.**-2., 'mean stripping time along sample not as expected'
return None
def test_subhalo_encounters():
# Test that subhalo_encounters acts as expected
# linear in sigma
assert numpy.fabs(sdf_bovy14.subhalo_encounters(sigma=300./220.)\