-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiskfit_mcmc.py
1541 lines (1231 loc) · 60.3 KB
/
diskfit_mcmc.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
# pylint: disable=C0103
"""
MCMC code for fitting a disk
author: Johan Mazoyer
"""
import os
import copy
import argparse
# careful on Python 3.8 mac multiprocessing switched to spawn so the global varialbe do not work
basedir = os.environ["EXCHANGE_PATH"] # the base directory where is
# your data (using OS environnement variable allow to use same code on
# different computer without changing this).
# default_parameter_file = 'FakeHr4796bright_MCMC_ADI.yaml' # name of the parameter file
# default_parameter_file = 'GPI_Hband_MCMC_ADI.yaml' # name of the parameter file
default_parameter_file = 'SPHERE_Hband_MCMC_ADI.yaml' # name of the parameter file
# you can also call it with the python function argument -p
MPI = False ## by default the MCMC is not mpi. you can change it
## in the the python function argument --mpi
parser = argparse.ArgumentParser(description='run diskFM MCMC')
parser.add_argument('-p',
'--param_file',
required=False,
help='parameter file name')
parser.add_argument("--mpi", help="run in mpi mode", action="store_true")
args = parser.parse_args()
import sys
import glob
import distutils.dir_util
import warnings
if args.mpi: # MPI or not for parallelization.
from schwimmbad import MPIPool as MultiPool
else:
import multiprocessing as mp
MultiPool = mp.get_context('fork').Pool
# from multiprocessing import Pool as MultiPool
from multiprocessing import cpu_count
from datetime import datetime
import math as mt
import numpy as np
import scipy.ndimage as snd
import astropy.io.fits as fits
from astropy.convolution import convolve
from astropy.wcs import FITSFixedWarning
import yaml
from emcee import EnsembleSampler
from emcee import backends
from numba.core.errors import NumbaWarning
import pyklip.instruments.GPI as GPI
import pyklip.instruments.SPHERE as SPHERE
import pyklip.instruments.Instrument as Instrument
import pyklip.parallelized as parallelized
from pyklip.fmlib.diskfm import DiskFM, _load_dict_from_hdf5
import pyklip.fm as fm
import pyklip.rdi as rdi
from anadisk_model.anadisk_sum_mask import phase_function_spline, generate_disk
from disk_models import hg_1g, hg_2g, hg_3g
import make_gpi_psf_for_disks as gpidiskpsf
import astro_unit_conversion as convert
# recommended by emcee https://emcee.readthedocs.io/en/stable/tutorials/parallel/
# and by PyKLIPto avoid that NumPy automatically parallelizes some operations,
# which kill the speed
os.environ["OMP_NUM_THREADS"] = "1"
def sigma_filter(image, box_width, n_sigma=3, ignore_edges=False, monitor=False):
# NAME:
# SIGMA_FILTER
# PURPOSE:
# Replace pixels more than a specified pixels deviant from its neighbors
# EXPLANATION:
# Computes the mean and standard deviation of pixels in a box centered at
# each pixel of the image, but excluding the center pixel. If the center
# pixel value exceeds some # of standard deviations from the mean, it is
# replaced by the mean in box. Note option to process pixels on the edges.
# CALLING SEQUENCE:
# Result = sigma_filter( image, box_width, n_sigma=(#), /ALL,/MON )
# INPUTS:
# image = 2-D image (matrix)
# box_width = width of square filter box, in # pixels (default = 3)
# n_sigma = # standard deviations to define outliers, floating point,
# recommend > 2, default = 3. For gaussian statistics:
# n_sigma = 1 smooths 35% of pixels, 2 = 5%, 3 = 1%.
# ignore_edges: if False, we also apply the sigma filter to the edges.
# If true, they're left untouched.
# monitor: prints information about % pixels replaced.
#
# CALLS:
# function filter_image( )
# PROCEDURE:
# Compute mean over moving box-cars using smooth, subtract center values,
# compute variance using smooth on deviations from mean,
# check where pixel deviation from mean is within variance of box,
# replace those pixels in smoothed image (mean) with orignal values,
# return the resulting partial mean image.
# MODIFICATION HISTORY:
# Written, 1991, Frank Varosi and Dan Gezari NASA/GSFC
# F.V.1992, added optional keywords /ITER,/MON,VAR=,DEV=,N_CHANGE=.
# Converted to IDL V5.0 W. Landsman September 1997
# Translated to python with chat GPT by Johan
#-
if box_width <3:
raise ValueError("box_width must be an odd integer > 2")
if box_width % 2 == 0:
raise ValueError("box_width must be an odd integer > 2")
bw2 = box_width**2
smooth = np.ones((box_width, box_width))
smooth[1:-1, 1:-1] = 0
if ignore_edges:
mean = (snd.generic_filter(image, np.mean, footprint=smooth, mode='constant', cval=np.nan) * bw2 - image) / (bw2 - 1)
wh_nan = np.isnan(mean)
mean[wh_nan] = 0
else:
mean = (snd.generic_filter(image, np.mean, footprint=smooth, mode='mirror') * bw2 - image) / (bw2 -1)
# mean = (generic_filter(image, np.nanmean, footprint=smooth, mode='constant', cval=np.nan) * bw2 - image) / (bw2 -1)
imdev = (image - mean)**2
fact = float(n_sigma**2) / (bw2 - 2)
if ignore_edges:
imvar = fact * (snd.generic_filter(imdev, np.mean, footprint=smooth, mode='constant', cval=np.nan) * bw2 - imdev)
imdev[np.isnan(imvar)] = 0
imvar[np.isnan(imvar)] = 0
else:
imvar = fact * (snd.generic_filter(imdev, np.nanmean, footprint=smooth, mode='mirror') * bw2 - imdev)
# imvar = fact * (generic_filter(imdev, np.nanmean, footprint=smooth, mode='constant', cval=np.nan) * bw2 - imdev)
# chek which pixels are ok
wok = np.where(imdev <= imvar)
nok = wok[0].size
npix = image.size
nchange = npix - nok
if monitor:
if ignore_edges:
print(f"{(nchange)*100./npix:.2f}% of pixels replaced (edges ignored), n_sigma={n_sigma:.1f}")
else:
print(f"{nchange*100./npix:.2f}% of pixels replaced, n_sigma={n_sigma:.1f}")
if nok == npix:
return image
if nok > 0:
mean[wok] = image[wok]
if ignore_edges:
mean[wh_nan] = image[wh_nan]
return mean
def from_theta_to_params(theta):
param_disk = {}
if (SPF_MODEL == 'spf_fix'):
# param_disk['a_r'] = 0.01 # we can fix the aspect ratio
param_disk['offset'] = 0. # no vertical offset in KLIP
param_disk['r1'] = mt.exp(theta[0])
param_disk['r2'] = mt.exp(theta[1])
param_disk['beta_in'] = theta[2]
param_disk['beta_out'] = theta[3]
param_disk['a_r'] = theta[4]
param_disk['inc'] = np.degrees(np.arccos(theta[5]))
param_disk['PA'] = theta[6]
param_disk['dx'] = theta[7]
param_disk['dy'] = theta[8]
param_disk['Norm'] = mt.exp(theta[9])
vector_param = [
param_disk['r1'], param_disk['r2'], param_disk['beta_in'],
param_disk['beta_out'], param_disk['a_r'], param_disk['inc'],
param_disk['PA'], param_disk['dx'], param_disk['dy'],
param_disk['Norm']
]
elif (SPF_MODEL == 'hg_1g') or (SPF_MODEL == 'hg_2g') or (SPF_MODEL
== 'hg_3g'):
param_disk['a_r'] = 0.01 # we fix the aspect ratio
param_disk['offset'] = 0. # no vertical offset in KLIP
param_disk['beta_in'] = -100 # we fix the inner power law
param_disk['r1'] = mt.exp(theta[0])
param_disk['r2'] = mt.exp(theta[1])
param_disk['beta_out'] = theta[2]
param_disk['inc'] = np.degrees(np.arccos(theta[3]))
param_disk['PA'] = theta[4]
param_disk['dx'] = theta[5]
param_disk['dy'] = theta[6]
param_disk['Norm'] = mt.exp(theta[7])
param_disk['g1'] = theta[8]
vector_param = [
param_disk['r1'], param_disk['r2'], param_disk['beta_out'],
param_disk['inc'], param_disk['PA'], param_disk['dx'],
param_disk['dy'], param_disk['Norm'], param_disk['g1']
]
if (SPF_MODEL == 'hg_2g') or (SPF_MODEL == 'hg_3g'):
param_disk['g2'] = theta[9]
param_disk['alpha1'] = theta[10]
vector_param = np.concatenate(
(vector_param, (param_disk['g2'], param_disk['alpha1'])))
if SPF_MODEL == 'hg_3g':
param_disk['g3'] = theta[11]
param_disk['alpha2'] = theta[12]
vector_param = np.concatenate(
(vector_param, (param_disk['g3'], param_disk['alpha2'])))
return param_disk, vector_param
#######################################################
def call_gen_disk(theta):
""" call the disk model from a set of parameters.
use SPF_MODEL, DIMENSION, PIXSCALE_INS, DISTANCE_STAR
ALIGNED_CENTER and WHEREMASK2GENERATEDISK
as global variables
Args:
theta: list of parameters of the MCMC
Returns:
a 2d model
"""
param_disk, _ = from_theta_to_params(theta)
if (SPF_MODEL == 'spf_fix'):
spf = F_SPF
elif (SPF_MODEL == 'hg_1g'):
# we fix the SPF using a HG parametrization with parameters in the init file
n_points = 21 # odd number to ensure that scattangl=pi/2 is in the list for normalization
scatt_angles = np.linspace(0, np.pi, n_points)
# 1g henyey greenstein, normalized at 1 at 90 degrees
spf_norm90 = hg_1g(np.degrees(scatt_angles), param_disk['g1'], 1)
#measure fo the spline and param_disk
spf = phase_function_spline(scatt_angles, spf_norm90)
elif SPF_MODEL == 'hg_2g':
# we fix the SPF using a HG parametrization with parameters in the init file
# starttime = datetime.now()
n_points = 21 # odd number to ensure that scattangl=pi/2 is in the list for normalization
scatt_angles = np.linspace(0, np.pi, n_points)
# 2g henyey greenstein, normalized at 1 at 90 degrees
spf_norm90 = hg_2g(np.degrees(scatt_angles), param_disk['g1'],
param_disk['g2'], param_disk['alpha1'], 1)
#measure fo the spline and param_disk
spf = phase_function_spline(scatt_angles, spf_norm90)
# print("spf_time: ", datetime.now() - starttime)
elif SPF_MODEL == 'hg_3g':
# we fix the SPF using a HG parametrization with parameters in the init file
n_points = 21 # odd number to ensure that scattangl=pi/2 is in the list for normalization
scatt_angles = np.linspace(0, np.pi, n_points)
# 3g henyey greenstein, normalized at 1 at 90 degrees
spf_norm90 = hg_3g(np.degrees(scatt_angles), param_disk['g1'],
param_disk['g2'], param_disk['g3'],
param_disk['alpha1'], param_disk['alpha2'], 1)
#measure fo the spline and param_disk
spf = phase_function_spline(scatt_angles, spf_norm90)
#generate the model
model = generate_disk(scattering_function_list=[spf],
R1=param_disk['r1'],
R2=param_disk['r2'],
beta_in=param_disk['beta_in'],
beta_out=param_disk['beta_out'],
aspect_ratio=param_disk['a_r'],
inc=param_disk['inc'],
pa=param_disk['PA'],
dx=param_disk['dx'],
dy=param_disk['dy'],
psfcenx=ALIGNED_CENTER[0],
psfceny=ALIGNED_CENTER[1],
sampling=1,
los_factor=2,
dim=DIMENSION,
mask=WHEREMASK2GENERATEDISK,
pixscale=PIXSCALE_INS,
distance=DISTANCE_STAR)[:, :, 0]
# remove the nans to avoid problems when convolving
model[model != model] = 0
# I normalize by value of a_r to avoid degenerascies between a_r and Normalization
model = param_disk['Norm'] * model / param_disk['a_r']
return model
########################################################
def logl(theta):
""" measure the Chisquare (log of the likelyhood) of the parameter set.
create disk
convolve by the PSF (psf is global)
do the forward modeling (diskFM obj is global)
nan out when it is out of the zone (zone mask is global)
subctract from data and divide by noise (data and noise are global)
Args:
theta: list of parameters of the MCMC
Returns:
Chisquare
"""
model = call_gen_disk(theta)
modelconvolved = convolve(model, PSF, boundary='wrap')
# DISKOBJ = DiskFM(None,
# None,
# None,
# modelconvolved,
# basis_filename=os.path.join(KLIPDIR,
# FILE_PREFIX + '_klbasis.h5'),
# load_from_basis=True)
DISKOBJ.update_disk(modelconvolved)
model_fm = DISKOBJ.fm_parallelized()[0]
# reduced data have already been naned outside of the minimization
# zone, so we don't need to do it also for model_fm
res = (REDUCED_DATA - model_fm) / NOISE
Chisquare = np.nansum(-0.5 * (res * res))
return Chisquare
########################################################
def logp(theta):
""" measure the log of the priors of the parameter set.
This function still have a lot of parameters hard coded here
Also you can change the prior shape directly here.
Args:
theta: list of parameters of the MCMC
Returns:
log of priors
"""
param_disk, _ = from_theta_to_params(theta)
prior_rout = 1.
# define the prior values
if (param_disk['r1'] < 60 or param_disk['r1'] > 80):
print('r1 out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
# - rout = Logistic We cut the prior at r2 = xx
# because this parameter is very limited by the ADI
if (param_disk['r2'] < 82 or param_disk['r2'] > 102):
print('r2 out of prior')
return -np.inf
else:
prior_rout = prior_rout / (1. + np.exp(40. * (param_disk['r2'] - 100)))
# prior_rout = prior_rout * 1. # or we can just use a flat prior
if (param_disk['inc'] < 10 or param_disk['inc'] > 90):
print('inc out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['PA'] < 10 or param_disk['PA'] > 90):
print('PA out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['dx'] < -90) or (param_disk['dx'] > 90): #The x offset
print('dx out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['dy'] < -90) or (param_disk['dy'] > 90): #The y offset
print('dy out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['Norm'] < 0.5 or param_disk['Norm'] > 50000):
print('Norm out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['beta_out'] < 1 or param_disk['beta_out'] > 30):
print('beta_out out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (SPF_MODEL == 'hg_1g') or (SPF_MODEL == 'hg_2g') or (SPF_MODEL
== 'hg_3g'):
if (param_disk['g1'] < 0.001 or param_disk['g1'] > 0.9999):
print('g1 out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (SPF_MODEL == 'hg_2g') or (SPF_MODEL == 'hg_3g'):
if (param_disk['g2'] < -0.9999 or param_disk['g2'] > -0.0001):
print('g2 out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['alpha1'] < 0.0001
or param_disk['alpha1'] > 0.9999):
print('alpha1 out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if SPF_MODEL == 'hg_3g':
if (param_disk['g3'] < -1 or param_disk['g3'] > 1):
print('g3 out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['alpha2'] < -1 or param_disk['alpha2'] > 1):
print('alpha2 out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
elif (SPF_MODEL == 'spf_fix'):
if (param_disk['beta_in'] < -30 or param_disk['beta_in'] > -1):
print('beta_in out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
if (param_disk['a_r'] < 0.0001
or param_disk['a_r'] > 0.5): #The aspect ratio
print('a_r out of prior')
return -np.inf
else:
prior_rout = prior_rout * 1.
# otherwise ...
return np.log(prior_rout)
########################################################
def lnpb(theta):
""" sum the logs of the priors (return of the logp funciton)
and of the likelyhood (return of the logl function)
Args:
theta: list of parameters of the MCMC
Returns:
log of priors + log of likelyhood
"""
# from datetime import datetime
starttime = datetime.now()
lp = logp(theta)
if not np.isfinite(lp):
return -np.inf
ll = logl(theta)
# print("Running time model + FM: ", datetime.now() - starttime)
return lp + ll
########################################################
def make_noise_map_rings(nodisk_data,
aligned_center=[140., 140.],
delta_raddii=3):
""" create a noise map from a image using concentring rings
and measuring the standard deviation on them
Args:
nodisk_data: [dim dim] data array containing speckle without disk
aligned_center: [pixel,pixel], position of the star in the mask
delta_raddii: pixel, widht of the small concentric rings
Returns:
a [dim,dim] array where each concentric rings is at a constant value
of the standard deviation of the reduced_data
"""
dim = nodisk_data.shape[1]
# create rho2D for the rings
x = np.arange(dim, dtype=np.float64)[None, :] - aligned_center[0]
y = np.arange(dim, dtype=np.float64)[:, None] - aligned_center[1]
rho2d = np.sqrt(x**2 + y**2)
noise_map = np.zeros((dim, dim))
for i_ring in range(0,
int(np.floor(aligned_center[0] / delta_raddii)) - 2):
wh_rings = np.where((rho2d >= i_ring * delta_raddii)
& (rho2d < (i_ring + 1) * delta_raddii))
noise_map[wh_rings] = np.nanstd(nodisk_data[wh_rings])
return noise_map
def create_uncertainty_map(dataset, params_mcmc_yaml, psflib=None):
""" measure the uncertainty map using the counter rotation trick
described in Sec4 of Gerard&Marois SPIE 2016 and probabaly elsewhere
Args:
dataset: a pyklip instance of Instrument.Data containing the data
params_mcmc_yaml: dic, all the parameters of the MCMC and klip
read from yaml file
delta_raddii: pixel, widht of the small concentric rings
psflib: a PSF librairy if RDI
Returns:
a [dim,dim] array containing only speckles and the disk has been removed
"""
file_prefix = params_mcmc_yaml['FILE_PREFIX']
move_here = params_mcmc_yaml['MOVE_HERE']
numbasis = [params_mcmc_yaml['KLMODE_NUMBER']]
mode = params_mcmc_yaml['MODE']
annuli = params_mcmc_yaml['ANNULI']
aligned_center = params_mcmc_yaml['ALIGNED_CENTER']
noise_multiplication_factor = params_mcmc_yaml[
'NOISE_MULTIPLICATION_FACTOR']
datadir = os.path.join(basedir, params_mcmc_yaml['BAND_DIR'])
klipdir = os.path.join(datadir, 'klip_fm_files')
dataset.PAs = -dataset.PAs
maxnumbasis = dataset.input.shape[0]
parallelized.klip_dataset(dataset,
numbasis=numbasis,
maxnumbasis=maxnumbasis,
annuli=annuli,
subsections=1,
mode=mode,
outputdir=klipdir,
fileprefix=file_prefix + '_couter_rotate_trick',
aligned_center=aligned_center,
highpass=False,
minrot=move_here,
calibrate_flux=False,
psf_library=psflib)
reduced_data_nodisk = fits.getdata(
os.path.join(klipdir,
file_prefix + '_couter_rotate_trick-KLmodes-all.fits'))[0]
noise = make_noise_map_rings(reduced_data_nodisk,
aligned_center=aligned_center,
delta_raddii=3)
noise[np.where(noise == 0)] = np.nan #we are going to divide by this noise
#### We know our noise is too small so we multiply by a given factor
noise = noise_multiplication_factor * noise
dataset.PAs = -dataset.PAs
os.remove(
os.path.join(klipdir,
file_prefix + '_couter_rotate_trick-KLmodes-all.fits'))
return noise
########################################################
def initialize_mask_psf_noise(params_mcmc_yaml, quietklip=True):
""" initialize the MCMC by preparing the useful things to measure the
likelyhood (measure the data, the psf, the uncertainty map, the masks).
Args:
params_mcmc_yaml: dic, all the parameters of the MCMC and klip
read from yaml file
quietklip : if True, pyklip and DiskFM are quiet
Returns:
a dataset a pyklip instance of Instrument.Data
"""
instrument = params_mcmc_yaml['INSTRUMENT']
# if first_time=True, all the masks, reduced data, noise map, and KL vectors
# are recalculated. be careful, for some reason the KL vectors are slightly
# different on different machines. if you see weird stuff in the FM models
# (for example in plotting the results), just remake them
first_time = params_mcmc_yaml['FIRST_TIME']
datadir = os.path.join(basedir, params_mcmc_yaml['BAND_DIR'])
klipdir = os.path.join(datadir, 'klip_fm_files')
distutils.dir_util.mkpath(klipdir)
file_prefix = params_mcmc_yaml['FILE_PREFIX']
#The PSF centers
aligned_center = params_mcmc_yaml['ALIGNED_CENTER']
### This is the only part of the code different for GPI IFS anf SPHERE
# For SPHERE We load and crop the PSF and the parangs
# For GPI, we load the raw data, emasure hte PSF from sat spots and
# collaspe the data
if first_time:
if instrument == 'SPHERE-IRDIS':
# only for SPHERE.
# using the SPHERE data frame developed by Vigan in pyklip
# data_files_str = params_mcmc_yaml['DATA_FILES_STR']
# psf_files_str = params_mcmc_yaml['PSF_FILES_STR']
# angles_str = params_mcmc_yaml['ANGLES_STR']
# band_name = params_mcmc_yaml['BAND_NAME']
# dataset = SPHERE.Irdis(data_files_str,
# psf_files_str,
# angles_str,
# band_name,
# psf_cube_size=31)
# #collapse the data spectrally
# dataset.spectral_collapse(align_frames=True,
# aligned_center=aligned_center)
# fits.writeto(os.path.join(klipdir, file_prefix + '_SmallPSF.fits'),
# dataset.psfs,
# overwrite='True')
psf_init = fits.getdata(os.path.join(datadir, "psf_sphere_h2.fits"))
size_init = psf_init.shape[1]
size_small = 31
small_psf = psf_init[size_init // 2 - size_small // 2:size_init // 2 +
size_small // 2 + 1,
size_init // 2 - size_small // 2:size_init // 2 +
size_small // 2 + 1]
small_psf = small_psf / np.max(small_psf)
small_psf[np.where(small_psf < 0.005)] = 0.
fits.writeto(os.path.join(klipdir,
file_prefix + '_SmallPSF.fits'),
small_psf,
overwrite='True')
# load the raw data
datacube_sphere_init = fits.getdata(
os.path.join(datadir, "cube_H2.fits")
)
parangs = fits.getdata(os.path.join(datadir, "parang.fits"))
parangs = parangs - 135.99 + 90 ## true north Maire et al. 2016
datacube_sphere_init = np.delete(datacube_sphere_init, (72, 81),
0) ## 2 slices are bad
parangs = np.delete(parangs, (72, 81), 0) ## 2 slices are bad
# we keep only 1 slide out of N to run test on my laptop
datacube_sphere_init = datacube_sphere_init[0::2]
# we keep only 1 slide out of N to run test on my laptop
parangs = parangs[0::2]
olddim = datacube_sphere_init.shape[1]
# we resize the SPHERE data to the same size as GPI (281)
# to avoid a problem of centering
newdim = 241
datacube_sphere_newdim = np.zeros(
(datacube_sphere_init.shape[0], newdim, newdim))
for i in range(datacube_sphere_init.shape[0]):
# datacube_sphere_newdim[i, :, :] = sigma_filter(datacube_sphere_init[
# i, olddim // 2 - newdim // 2:olddim // 2 + newdim // 2 + 1,
# olddim // 2 - newdim // 2:olddim // 2 + newdim // 2 + 1],3,n_sigma=5, monitor=True)
datacube_sphere_newdim[i, :, :] = datacube_sphere_init[
i, olddim // 2 - newdim // 2:olddim // 2 + newdim // 2 + 1,
olddim // 2 - newdim // 2:olddim // 2 + newdim // 2 + 1]
# we flip the dataset (and therefore inverse the parangs) to obtain
# the good PA after pyklip reduction
parangs = -parangs
for i in range(datacube_sphere_newdim.shape[0]):
datacube_sphere_newdim[i] = np.flip(datacube_sphere_newdim[i],
axis=0)
datacube_sphere = datacube_sphere_newdim
fits.writeto(os.path.join(datadir, file_prefix + '_true_parangs.fits'),
parangs,
overwrite='True')
fits.writeto(os.path.join(datadir, file_prefix + '_true_dataset.fits'),
datacube_sphere,
overwrite='True')
datacube_sphere = fits.getdata(
os.path.join(datadir, file_prefix + '_true_dataset.fits'))
parangs_sphere = fits.getdata(
os.path.join(datadir, file_prefix + '_true_parangs.fits'))
size_datacube = datacube_sphere.shape
centers_sphere = np.zeros((size_datacube[0], 2)) + [aligned_center[0], aligned_center[1]]
dataset = Instrument.GenericData(datacube_sphere,
centers_sphere,
parangs=parangs_sphere,
flipx=True,
wvs=None)
elif instrument == 'GPI':
# only for GPI
filelist = sorted(glob.glob(os.path.join(datadir, "*.fits")))
#check that these are GPI files
non_GPI_files = []
filetype_here = None
for filename in filelist:
headerfile = fits.getheader(filename)
if ('FILETYPE' in headerfile.keys()) and (
headerfile['FILETYPE'] == 'Stokes Cube'
or headerfile['FILETYPE']
== 'Spectral Cube'): # This is a GPI file
if filetype_here is not None:
if filetype_here != headerfile[
'FILETYPE']: # check if Spec of Pol
raise ValueError(
""" There are simultaneously Pol and Spec
type files in this folder. Code only works
with a single kind of obs""")
filetype_here = headerfile['FILETYPE']
else: # This is not a GPI file (maybe PSF file). Ignore when loading.
non_GPI_files.append(filename)
for nonGPIfilename in non_GPI_files:
filelist.remove(nonGPIfilename)
pol_or_spec = filetype_here
print(pol_or_spec)
if len(filelist) == 0:
raise ValueError("Could not find files in the dir")
if pol_or_spec == 'Spectral Cube': # GPI spec mode
filelist4psf = copy.copy(filelist)
dataset4psf = GPI.GPIData(filelist4psf, quiet=True)
print("\n Create a PSF from the sat spots")
# identify angles where the
# disk intersect the satspots
excluded_files = gpidiskpsf.check_satspots_disk_intersection(
dataset4psf, params_mcmc_yaml, quiet=True)
# exclude those angles for the PSF measurement
for excluded_filesi in excluded_files:
if excluded_filesi in filelist4psf:
filelist4psf.remove(excluded_filesi)
# create the data this time wihtout the bad files
dataset4psf = GPI.GPIData(filelist4psf, quiet=True)
# Find the IFS slices for which the satspots are too faint
# if SNR time_mean(sat spot) <3 they are removed
# Mostly for K2 and sometime K1
excluded_slices = gpidiskpsf.check_satspots_snr(
dataset4psf, params_mcmc_yaml, quiet=True)
# extract the data this time wihtout the bad files nor slices
dataset4psf = GPI.GPIData(filelist4psf,
quiet=True,
skipslices=excluded_slices)
# finally measure the good psf
instrument_psf = gpidiskpsf.make_collapsed_psf(
dataset4psf, params_mcmc_yaml, boxrad=11)[0]
# monocrhomatic here
# save the excluded_slices in the psf header (SNR too low)
hdr_psf = fits.Header()
hdr_psf['N_BADSLI'] = len(excluded_slices)
for badslice_i, excluded_slices_num in enumerate(
excluded_slices):
hdr_psf['BADSLI' +
str(badslice_i).zfill(2)] = excluded_slices_num
# save the excluded_files in the psf header (disk on satspots)
hdr_psf['N_BADFIL'] = len(excluded_files)
for badfile_i, badfilestr in enumerate(excluded_files):
hdr_psf['BADFIL' + str(badfile_i).zfill(2)] = badfilestr
else: # GPI Pol mode
instrument_psf = fits.getdata(
os.path.join(datadir, params_mcmc_yaml['PSF_FILES_STR']))
hdr_psf = fits.Header()
hdr_psf['N_BADSLI'] = 0
#save the psf
fits.writeto(os.path.join(klipdir, file_prefix + '_SmallPSF.fits'),
instrument_psf,
header=hdr_psf,
overwrite=True)
if pol_or_spec == 'Spectral Cube': # GPI spec mode
# load the bad slices and bad files in the psf header
hdr_psf = fits.getheader(
os.path.join(klipdir, file_prefix + '_SmallPSF.fits'))
# We can choose to remove completely from the correction
# the angles where the disk intersect the disk (they are exlcuded
# from the PSF measurement by defaut).
# We can removed those if rm_file_disk_cross_satspots=True
if params_mcmc_yaml['RM_FILE_DISK_CROSS_SATSPOTS']:
excluded_files = []
if hdr_psf['N_BADFIL'] > 0:
for badfile_i in range(hdr_psf['N_BADFIL']):
excluded_files.append(
hdr_psf['BADFIL' + str(badfile_i).zfill(2)])
for excluded_filesi in excluded_files:
if excluded_filesi in filelist:
filelist.remove(excluded_filesi)
# in IFS mode, we always exclude the IFS slices with too much noise. We
# chose the criteria as "SNR(mean of sat spot)< 3""
excluded_slices = []
if hdr_psf['N_BADSLI'] > 0:
for badslice_i in range(hdr_psf['N_BADSLI']):
excluded_slices.append(
hdr_psf['BADSLI' + str(badslice_i).zfill(2)])
# load the raw data without the bad slices
dataset = GPI.GPIData(filelist,
quiet=True,
skipslices=excluded_slices)
else: # pol mode
dataset = GPI.GPIData(filelist, quiet=True)
#collapse the data spectrally and center
dataset.spectral_collapse(align_frames=True,
aligned_center=aligned_center)
#After this, this is for both GPI and SPHERE
#define the outer working angle
dataset.OWA = params_mcmc_yaml['OWA']
if dataset.input.shape[1] != dataset.input.shape[2]:
raise ValueError(""" Data slices are not square (dimx!=dimy),
please make them square""")
#create the masks
#create the mask where the non convoluted disk is going to be generated.
# To gain time, it is ~tightely adjusted to the expected models BEFORE
# convolution. Inded, the models are generated pixel by pixels. 0.1 s
# gained on every model is a day of calculation gain on one million model,
# so adjust your mask tightly to your model. You can change the harcoded parameter
# here if you neet to go faster (reduced it) or it the slope beta is very slow (increase it)
print(
"\n Create the binary masks to define model zone and chisquare zone"
)
mask_disk_zeros = gpidiskpsf.make_disk_mask(
dataset.input.shape[1],
params_mcmc_yaml['pa_init'],
params_mcmc_yaml['inc_init'],
convert.au_to_pix(params_mcmc_yaml['r1_init'],
params_mcmc_yaml['PIXSCALE_INS'],
params_mcmc_yaml['DISTANCE_STAR']) -
18 / np.cos(np.radians(params_mcmc_yaml['inc_init'])),
convert.au_to_pix(params_mcmc_yaml['r2_init'],
params_mcmc_yaml['PIXSCALE_INS'],
params_mcmc_yaml['DISTANCE_STAR']) +
18 / np.cos(np.radians(params_mcmc_yaml['inc_init'])),
aligned_center=aligned_center)
mask2generatedisk = 1 - mask_disk_zeros
fits.writeto(os.path.join(klipdir,
file_prefix + '_mask2generatedisk.fits'),
mask2generatedisk,
overwrite='True')
# we create a second mask for the minimization a little bit larger
# (because model expect to grow with the PSF convolution and the FM)
# and we can also exclude the center region where there are too much speckles
mask_disk_zeros = gpidiskpsf.make_disk_mask(
dataset.input.shape[1],
params_mcmc_yaml['pa_init'],
params_mcmc_yaml['inc_init'],
convert.au_to_pix(params_mcmc_yaml['r1_init'],
params_mcmc_yaml['PIXSCALE_INS'],
params_mcmc_yaml['DISTANCE_STAR']) -
20 / np.cos(np.radians(params_mcmc_yaml['inc_init'])),
convert.au_to_pix(params_mcmc_yaml['r2_init'],
params_mcmc_yaml['PIXSCALE_INS'],
params_mcmc_yaml['DISTANCE_STAR']) +
20 / np.cos(np.radians(params_mcmc_yaml['inc_init'])),
aligned_center=aligned_center)
mask2minimize = (1 - mask_disk_zeros)
### a few lines to create a circular central mask to hide center regions with a lot
### of speckles. Currently not using it but it's there
# mask_speckle_region = np.ones((dataset.input.shape[1], dataset.input.shape[2]))
# x = np.arange(dataset.input.shape[1], dtype=np.float)[None,:] - aligned_center[0]
# y = np.arange(dataset.input.shape[2], dtype=np.float)[:,None] - aligned_center[1]
# rho2d = np.sqrt(x**2 + y**2)
# mask_speckle_region[np.where(rho2d < 21)] = 0.
# mask2minimize = mask2minimize*mask_speckle_region
fits.writeto(os.path.join(klipdir,
file_prefix + '_mask2minimize.fits'),
mask2minimize,
overwrite='True')
# RDI case, if it's not the first time, we do not even need to load the correlation
# psflib, all needed information is already loaded in the KL modes
if params_mcmc_yaml['MODE'] == 'RDI':
psflib = initialize_rdi(dataset, params_mcmc_yaml)
else:
psflib = None
print("\n Create the uncertainty map")
# Disable print for pyklip
if quietklip:
sys.stdout = open(os.devnull, 'w')
noise = create_uncertainty_map(dataset,
params_mcmc_yaml,
psflib=psflib)
fits.writeto(os.path.join(klipdir, file_prefix + '_noisemap.fits'),
noise,
overwrite='True')
sys.stdout = sys.__stdout__
else:
dataset = None
psflib = None
return dataset, psflib
########################################################
def initialize_rdi(dataset, params_mcmc_yaml):
""" initialize the rdi librairy. This should maybe not be done in this
code since it can be extremely time consuming. Feel free to run this
routine elsewhere. Currently very GPI oriented.
Args:
dataset: a pyklip instance of Instrument.Data containing the data
params_mcmc_yaml: dic, all the parameters of the MCMC and klip
read from yaml file
Returns:
a psflib object
"""
print("\n Initialize RDI")
do_rdi_correlation = params_mcmc_yaml['DO_RDI_CORRELATION']
aligned_center = params_mcmc_yaml['ALIGNED_CENTER']