-
Notifications
You must be signed in to change notification settings - Fork 6
/
visualutil.py
1046 lines (879 loc) · 36.3 KB
/
visualutil.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
def plotPDF(fitresults, tag, limits='', Ngood=5000, axes='auto'):
"""
Plot the PDF of each parameter of the model.
"""
import numpy
import matplotlib.pyplot as plt
from pylab import savefig
from matplotlib import rc
import modifypdf
# plotting parameters
rc('font',**{'family':'sans-serif', 'sans-serif':['Arial Narrow'],
'size':'12'})
# grab the last Ngood fits
fitresults = fitresults[-Ngood:]
#lnprobstring = "prior to pruning <Ln Prob>: {:f}"
#print(lnprobstring.format(fitresults['lnprob'].mean()))
# identify the good fits
fitresultsgood = modifypdf.prune(fitresults)
# determine dimensions of PDF plots
nparams = len(fitresultsgood[0])
ncol = 4
nrow = nparams / ncol + 1
j = 1
plt.figure(figsize=(12.0, 1.5 * nrow))
# set up the plotting window
plt.subplots_adjust(left=0.08, bottom=0.15, right=0.95, top=0.95,
wspace=0.4, hspace=0.65)
pnames = fitresultsgood.names
counter = 0
for pname in pnames:
# Position of primary lens
frg = fitresultsgood[pname]
rmsval = numpy.std(frg)
if rmsval > 1e-6:
avgval = numpy.mean(frg)
print(pname + ' = ' + str(avgval) + ' +/- ' + str(rmsval))
totalwidth = frg.max() - frg.min()
nbins = totalwidth / rmsval * 5
ax = plt.subplot(nrow, ncol, j)
j += 1
plt.hist(frg, nbins, edgecolor='blue')
plt.ylabel('N')
plt.xlabel(pname)
if axes == 'auto':
start, end = ax.get_xlim()
nticks = 5
stepsize = (end - start) / nticks
ax.xaxis.set_ticks(numpy.arange(start, end + 0.99*stepsize,
stepsize))
elif axes == 'initial':
oldaxis = plt.axis()
if pname[0:6] == 'lnprob':
xmin = frg.min()
xmax = frg.max()
elif pname[0:2] == 'mu':
xmin = 0
xmax = 30
else:
p_l = limits[0]
p_u = limits[1]
xmin = p_l[counter]
xmax = p_u[counter]
counter += 1
ymin = oldaxis[2]
ymax = oldaxis[3]
plt.axis([xmin, xmax, ymin, ymax])
savefile = tag + 'PDFs.png'
savefig(savefile)
def makeSBmap(config, fitresult):
"""
Make a surface brightness map of the lensed image for a given set of model
parameters.
"""
import lensutil
from astropy.io import fits
import os
import setuputil
import re
import numpy
# Loop over each region
# read the input parameters
paramData = setuputil.loadParams(config)
nlensedsource = paramData['nlensedsource']
nlensedregions = paramData['nlensedregions']
npar_previous = 0
configkeys = config.keys()
configkeystring = " ".join(configkeys)
regionlist = re.findall('Region.', configkeystring)
SBmap_all = 0
LensedSBmap_all = 0
nregion = len(regionlist)
for regioni in range(nregion):
regstring = 'Region' + str(regioni)
#indx = paramData['regionlist'].index(regstring)
cr = config[regstring]
nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)
if nmu > 0:
allparameters0 = list(fitresult)[1:-nmu]
else:
allparameters0 = list(fitresult)[1:]
# search poff_models for parameters fixed relative to other parameters
fixindx = setuputil.fixParams(paramData)
poff = paramData['poff']
ndim_total = len(poff)
fixed = (numpy.where(fixindx >= 0))[0]
nfixed = fixindx[fixed].size
parameters_offset = numpy.zeros(ndim_total)
for ifix in range(nfixed):
ifixed = fixed[ifix]
subindx = fixindx[ifixed]
par0 = 0
if fixindx[subindx] > 0:
par0 = fitresult[fixindx[subindx] + 1]
parameters_offset[ifixed] = fitresult[subindx + 1] + par0
allparameters = allparameters0 + parameters_offset
# count the number of lenses
configkeys = cr.keys()
configkeystring = " ".join(configkeys)
lenslist = re.findall('Lens.', configkeystring)
nlens = len(lenslist)
# count the number of sources
sourcelist = re.findall('Source.', configkeystring)
nsource = len(sourcelist)
nparperlens = 5
nparpersource = 6
nparlens = nparperlens * nlens
nparsource = nparpersource * nsource
npar = nparlens + nparsource + npar_previous
parameters = allparameters[npar_previous:npar]
npar_previous = npar
#nlens = paramData['nlens_regions'][indx]
#nsource = paramData['nsource_regions'][indx]
x = paramData['x'][regioni]
y = paramData['y'][regioni]
modelheader = paramData['modelheader'][regioni]
model_types = paramData['model_types'][regioni]
SBmap, LensedSBmap, Aperture, LensedAperture, mu_tot, mu_mask = \
lensutil.sbmap(x, y, nlens, nsource, parameters, model_types, \
computeamp=True)
caustics = False
if caustics:
deltapar = parameters[0:nparlens + nparpersource]
refine = 2
nx = x[:, 0].size * refine
ny = y[:, 0].size * refine
x1 = x[0, :].min()
x2 = x[0, :].max()
linspacex = numpy.linspace(x1, x2, nx)
y1 = y[:, 0].min()
y2 = y[:, 0].max()
linspacey = numpy.linspace(y1, y2, ny)
onex = numpy.ones(nx)
oney = numpy.ones(ny)
finex = numpy.outer(oney, linspacex)
finey = numpy.outer(linspacey, onex)
mumap = numpy.zeros([ny, nx])
for ix in range(nx):
for iy in range(ny):
deltapar[-nparpersource + 0] = finex[ix, iy]
deltapar[-nparpersource + 1] = finey[ix, iy]
xcell = paramData['celldata']
deltapar[-nparpersource + 2] = xcell
deltaunlensed, deltalensed, A1, A2, mu_xy, mu_xymask = \
lensutil.sbmap(finex, finey, nlens, 1, deltapar, ['Delta'])
mumap[ix, iy] = mu_xy[0]
import matplotlib.pyplot as plt
plt.imshow(mumap, origin='lower')
plt.contour(mumap, levels=[mumap.max()/1.1])
import pdb; pdb.set_trace()
SBmap_all += SBmap
LensedSBmap_all += LensedSBmap
LensedSBmapLoc = 'LensedSBmap.fits'
SBmapLoc = 'SBmap_Region.fits'
cmd = 'rm -rf ' + LensedSBmapLoc + ' ' + SBmapLoc
os.system(cmd)
fits.writeto(LensedSBmapLoc, LensedSBmap_all, modelheader)
fits.writeto(SBmapLoc, SBmap_all, modelheader)
return
def makeVis(config, miriad=False, idtag=''):
"""
Make simulated visibilities given a model image and observed visibilities.
Writes the visibilities to uvfits files.
"""
import uvmodel
import os
# get the uvfits files
visfile = config['UVData']
#----------------------------------------------------------------------
# Python version of UVMODEL
# "Observe" the lensed emission with the SMA and write to a new file
#----------------------------------------------------------------------
# Python version of UVMODEL's "replace" subroutine:
# is visfile a list of visibilities files?
if not type(visfile) is list:
visname, visext = os.path.splitext(visfile)
if miriad:
# We use miriad to do the imaging
tag = '.miriad'
DataMiriad = visname + tag
if not os.path.exists(DataMiriad):
print("Creating new miriad data file: " + DataMiriad)
os.system('rm -rf ' + DataMiriad)
command = 'fits op=uvin in=' + visfile + ' out=' + DataMiriad
os.system(command)
else:
# We use CASA to do the imaging
tag = '.ms'
# check to see if the CASA ms exists
try:
from taskinit import tb
tb.open(visname + tag)
tb.close()
print "Found an existing CASA ms file."
except RuntimeError:
print "No CASA ms file found, creating one from " + visname \
+ ".uvfits file."
from casa import importuvfits
infile = visname + '.uvfits'
outfile = visname + '.ms'
importuvfits(fitsfile=infile, vis=outfile)
visfile = visname + tag
SBmapLoc = 'LensedSBmap.fits'
modelvisfile = visname + '_model_' + idtag + tag
os.system('rm -rf ' + modelvisfile)
if miriad:
SBmapMiriad = 'LensedSBmap' + tag
os.system('rm -rf ' + SBmapMiriad)
command = 'fits op=xyin in=' + SBmapLoc + ' out=' \
+ SBmapMiriad
os.system(command)
command = 'uvmodel options=replace vis=' + visfile + \
' model=' + SBmapMiriad + ' out=' + modelvisfile
os.system(command + ' > uvmodeloutput.txt')
#command = 'cp ' + visfile + '/wflags ' + modelvisfile
#os.system(command)
else:
#print(visfile, modelvisfile)
uvmodel.replace(SBmapLoc, visfile, modelvisfile,
miriad=miriad)
#print(visfile, modelvisfile)
# Python version of UVMODEL's "subtract" subroutine:
modelvisfile = visname + '_residual_' + idtag + tag
os.system('rm -rf ' + modelvisfile)
if miriad:
SBmapMiriad = 'LensedSBmap' + tag
os.system('rm -rf ' + SBmapMiriad)
command = 'fits op=xyin in=' + SBmapLoc + ' out=' \
+ SBmapMiriad
os.system(command)
os.system('rm -rf ' + modelvisfile)
command = 'uvmodel options=subtract vis=' + visfile + \
' model=' + SBmapMiriad + ' out=' + modelvisfile
os.system(command)
#command = 'cp ' + visfile + '/wflags ' + modelvisfile
#os.system(command)
else:
#print(visfile, modelvisfile)
uvmodel.subtract(SBmapLoc, visfile, modelvisfile,
miriad=miriad)
else:
for i, ivisfile in enumerate(visfile):
visname, visext = os.path.splitext(ivisfile)
print(visname)
if miriad:
tag = '.uvfits'
else:
tag = '.ms'
# check to see if the CASA ms exists
try:
from taskinit import tb
tb.open(visname + tag)
tb.close()
print "Found an existing CASA ms file."
except RuntimeError:
print "No CASA ms file found, creating one from " + visname \
+ ".uvfits file."
from casa import importuvfits
infile = visname + '.uvfits'
outfile = visname + '.ms'
importuvfits(fitsfile=infile, vis=outfile)
SBmapLoc = 'LensedSBmap.fits'
if miriad:
SBmapMiriad = 'LensedSBmap.miriad'
os.system('rm -rf ' + SBmapMiriad)
command = 'fits op=xyin in=' + SBmapLoc + ' out=' \
+ SBmapMiriad
os.system(command)
ivisfile = visname + '.miriad'
modelivisfile = visname + '_model_' + idtag + '.miriad'
os.system('rm -rf ' + modelivisfile)
command = 'uvmodel options=replace vis=' + ivisfile + \
' model=' + SBmapMiriad + ' out=' + modelivisfile
os.system(command)
#command = 'cp ' + ivisfile + '/wflags ' + modelivisfile
#os.system(command)
else:
ivisfile = visname + tag
modelivisfile = visname + '_model_' + idtag + tag
os.system('rm -rf ' + modelivisfile)
uvmodel.replace(SBmapLoc, ivisfile, modelivisfile,
miriad=miriad)
# Python version of UVMODEL's "subtract" subroutine:
if miriad:
SBmapMiriad = 'LensedSBmap.miriad'
os.system('rm -rf ' + SBmapMiriad)
command = 'fits op=xyin in=' + SBmapLoc + ' out=' \
+ SBmapMiriad
os.system(command)
modelivisfile = visname + '_residual_' + idtag + '.miriad'
os.system('rm -rf ' + modelivisfile)
command = 'uvmodel options=subtract vis=' + ivisfile + \
' model=' + SBmapMiriad + ' out=' + modelivisfile
os.system(command)
#command = 'cp ' + ivisfile + '/wflags ' + modelivisfile
#os.system(command)
else:
modelivisfile = visname + '_residual_' + idtag + tag
os.system('rm -rf ' + modelivisfile)
uvmodel.subtract(SBmapLoc, ivisfile, modelivisfile,
miriad=miriad)
#except:
# msg = "Visibility datasets must be specified as either a string " \
# + "or a list of strings."
# print(msg)
# raise TypeError
def makeImage(config, interactive=True, miriad=False, idtag=''):
"""
Make an image of the model and the residual from simulated model
visibilities. Requires CASA or miriad.
"""
import os
from astropy.io import fits
import miriadutil
visfile = config['UVData']
target = config['ObjectName']
fitsim = config['ImageName']
fitshead = fits.getheader(fitsim)
imsize = [fitshead['NAXIS1'], fitshead['NAXIS2']]
cell = str(fitshead['CDELT2'] * 3600) + 'arcsec'
# search for an existing mask
index = fitsim.find('.fits')
maskname = fitsim[0:index] + '.mask'
try:
maskcheck = os.path.exists(maskname)
except:
maskcheck = False
if maskcheck:
mask = maskname
else:
mask = ''
# invert and clean the simulated model visibilities
if miriad:
try:
# use miriad for imaging
imsize = str(fitshead['NAXIS1'])
index = visfile.find('.uvfits')
name = visfile[0:index]
uvfitsloc = name + '_model_' + idtag + '.uvfits'
uvmirloc = name + '_model_' + idtag + '.miriad'
command = 'rm -rf ' + uvmirloc
#os.system(command)
command = 'fits op=uvin options=varwt in=' + uvfitsloc \
+ ' out=' + uvmirloc
#os.system(command + ' > fitsoutput.txt')
imloc = target + '_clean_model'
niter = '10000'
cell = str(fitshead['CDELT2'] * 3600)
cutoff = '2e-3'
cutoff2 = '4e-3'
robust = '+0.5'
region = 'quarter'
region2 = 'quarter'
gain = '0.1'
fwhm = '0'
sup = '0'
parameters = [uvmirloc, imloc, imsize, cell, niter, cutoff, \
cutoff2, robust, region, region2, gain, fwhm, sup]
miriadutil.makeScript(parameters)
command = 'csh image.csh'
os.system(command + ' > imageoutput.txt')
# the simulated residual visibilities
uvfitsloc = name + '_residual_' + idtag + '.uvfits'
uvmirloc = name + '_residual_' + idtag + '.miriad'
command = 'rm -rf ' + uvmirloc
#os.system(command)
command = 'fits op=uvin options=varwt in=' + uvfitsloc \
+ ' out=' + uvmirloc
#os.system(command + ' >> fitsoutput.txt')
imloc = target + '_clean_residual'
parameters = [uvmirloc, imloc, imsize, cell, niter, cutoff, \
cutoff2, robust, region, region2, gain, fwhm, sup]
miriadutil.makeScript(parameters)
command = 'csh image.csh'
os.system(command)# + ' >> imageoutput.txt')
except:
try:
modellist = []
residlist = []
for ivisfile in visfile:
# use miriad for imaging
imsize = str(fitshead['NAXIS1'])
index = ivisfile.find('.uvfits')
name = ivisfile[0:index]
uvfitsloc = name + '_model_' + idtag + '.uvfits'
uvmirloc = name + '_model_' + idtag + '.miriad'
modellist.append(uvmirloc)
command = 'rm -rf ' + uvmirloc
#os.system(command)
command = 'fits op=uvin in=' + uvfitsloc + ' out=' \
+ uvmirloc
#os.system(command + ' > fitsoutput.txt')
# the simulated residual visibilities
uvfitsloc = name + '_residual_' + idtag + '.uvfits'
uvmirloc = name + '_residual_' + idtag + '.miriad'
residlist.append(uvmirloc)
command = 'rm -rf ' + uvmirloc
#os.system(command)
command = 'fits op=uvin in=' + uvfitsloc + ' out=' \
+ uvmirloc
#os.system(command + ' >> fitsoutput.txt')
invisloc = ','.join(modellist)
imloc = target + '_model'
parameters = [invisloc, imloc, imsize, cell, niter, cutoff, \
cutoff2, robust, region, region2, gain, fwhm, sup]
miriadutil.makeScript(parameters)
command = 'csh image.csh'
os.system(command + ' > imageoutput.txt')
invisloc = ','.join(residlist)
imloc = target + '_residual'
parameters = [invisloc, imloc, imsize, cell, niter, cutoff, \
cutoff2, robust, region, region2, gain, fwhm, sup]
miriadutil.makeScript(parameters)
command = 'csh image.csh'
os.system(command)# + ' >> imageoutput.txt')
except:
msg = "Visibility datasets must be specified as either a "\
"string or a list of strings."
print(msg)
raise TypeError
else:
# use CASA for imaging
from clean import clean
from casa import exportfits
# ---------------------------------------
# invert and clean the model visibilities
# remove any existing clean products
imloc = target + '_clean_model'
os.system('rm -rf ' + imloc + '*')
# handle lists of visibility files
if type(visfile) is list:
modelvisloc = []
for i, ivisfile in enumerate(visfile):
visname, visext = os.path.splitext(ivisfile)
modelvisloc.append(visname + '_model_' + idtag + '.ms')
# handle single visibility files
else:
visname, visext = os.path.splitext(visfile)
modelvisloc = visname + '_model_' + idtag + '.ms'
# use CASA's clean task to make the images
clean(vis=modelvisloc, imagename=imloc, mode='mfs', niter=10000,
threshold='0.2mJy', interactive=interactive, mask=mask,
imsize=imsize, cell=cell, weighting='briggs', robust=0.5)
# export the cleaned image to a fits file
os.system('rm -rf ' + imloc + '.fits')
exportfits(imagename=imloc + '.image', fitsimage=imloc + '.fits')
# ---------------------------------------
# invert and clean the residual visibilities
# remove any existing clean products
imloc = target + '_clean_residual'
os.system('rm -rf ' + imloc + '*')
# handle lists of visibility files
if type(visfile) is list:
modelvisloc = []
for i, ivisfile in enumerate(visfile):
visname, visext = os.path.splitext(ivisfile)
modelvisloc.append(visname + '_residual_' + idtag + '.ms')
# handle single visibility files
else:
visname, visext = os.path.splitext(visfile)
modelvisloc = visname + '_residual_' + idtag + '.ms'
# use CASA's clean task to make the images
clean(vis=modelvisloc, imagename=imloc, mode='mfs', niter=10000,
threshold='0.2mJy', interactive=interactive, mask=mask,
imsize=imsize, cell=cell, weighting='briggs', robust=0.5)
# export the cleaned image to a fits file
os.system('rm -rf ' + imloc + '.fits')
exportfits(imagename=imloc + '.image', fitsimage=imloc + '.fits')
return
def plotImage(model, data, config, modeltype, fitresult, tag=''):
"""
Make a surface brightness map of a given model image. Overlay with red
contours a surface brightness map of the data image to which the model was
fit.
"""
import numpy
from astropy import wcs
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
from pylab import savefig
import setuputil
import re
# set font properties
font = {'family' : 'Arial Narrow',
'weight' : 'bold',
'size' : 10}
matplotlib.rc('font', **font)
matplotlib.rcParams['axes.linewidth'] = 1.5
fig = plt.figure(figsize=(3.0, 3.0))
ax = fig.add_subplot(1, 1, 1)
plt.subplots_adjust(left=0.08, right=0.97, top=0.97,
bottom=0.08, wspace=0.35)
paramData = setuputil.loadParams(config)
nlensedsource = paramData['nlensedsource']
nlensedregions = paramData['nlensedregions']
npar_previous = 0
configkeys = config.keys()
configkeystring = " ".join(configkeys)
regionlist = re.findall('Region.', configkeystring)
nregion = len(regionlist)
for iregion in range(nregion):
region = 'Region' + str(iregion)
cr = config[region]
ra_centroid = cr['RACentroid']
dec_centroid = cr['DecCentroid']
radialextent = cr['RadialExtent']
nmu = 2 * (numpy.array(nlensedsource).sum() + nlensedregions)
if nmu > 0:
allparameters0 = list(fitresult)[1:-nmu]
else:
allparameters0 = list(fitresult)[1:]
# search poff_models for parameters fixed relative to other parameters
fixindx = setuputil.fixParams(paramData)
poff = paramData['poff']
ndim_total = len(poff)
fixed = (numpy.where(fixindx >= 0))[0]
nfixed = fixindx[fixed].size
parameters_offset = numpy.zeros(ndim_total)
for ifix in range(nfixed):
ifixed = fixed[ifix]
subindx = fixindx[ifixed]
par0 = 0
if fixindx[subindx] > 0:
par0 = fitresult[fixindx[subindx] + 1]
parameters_offset[ifixed] = fitresult[subindx + 1] + par0
allparameters = allparameters0 + parameters_offset
# count the number of lenses
configkeys = cr.keys()
configkeystring = " ".join(configkeys)
lenslist = re.findall('Lens.', configkeystring)
nlens = len(lenslist)
# count the number of sources
sourcelist = re.findall('Source.', configkeystring)
nsource = len(sourcelist)
nparlens = 5 * nlens
nparsource = 6 * nsource
npar = nparlens + nparsource + npar_previous
parameters = allparameters[npar_previous:npar]
npar_previous = npar
for i in range(nsource):
i6 = i * 6
xxx = parameters[i6 + 2 + nparlens]
yyy = parameters[i6 + 3 + nparlens]
source_pa = 90 - parameters[i6 + 5 + nparlens]
#model_type = model_types[i]
#if model_type == 'gaussian':
norm = 2.35
#if model_type == 'cylinder':
# norm = numpy.sqrt(2)
meansize = norm * parameters[i6 + 1 + nparlens]
source_bmaj = meansize / numpy.sqrt(parameters[i6 + 4 + nparlens])
source_bmin = meansize * numpy.sqrt(parameters[i6 + 4 + nparlens])
e = Ellipse((xxx, yyy), source_bmaj, source_bmin, \
angle=source_pa, ec='white', lw=0.5, fc='magenta', \
zorder=2, fill=True, alpha=0.5)
ax.add_artist(e)
if config.keys().count('xtextoff') > 0:
xytext = (xxx + config['xtextoff'][i],
yyy + config['ytextoff'][i])
if iregion == 0:
if modeltype != 'optical':
plt.annotate(str(i), xy=(xxx, yyy), xytext=xytext,
fontsize='xx-large')
else:
xytext = (xxx + config['xtextoff'][i+2],
yyy + config['ytextoff'][i+2])
if modeltype != 'optical':
plt.annotate(str(i + 2), xy=(xxx, yyy), xytext=xytext,
fontsize='xx-large')
for i in range(nlens):
i5 = i * 5
xxx = numpy.array([parameters[i5 + 1]])
yyy = numpy.array([parameters[i5 + 2]])
plt.plot(xxx, yyy, 'o', ms=5., mfc='black', mec='white', mew=0.5, \
label='Lens Position', zorder=20)
lens_pa = 90 - parameters[i5 + 4]
meansize = 2 * parameters[i5]
lens_bmaj = meansize / numpy.sqrt(parameters[i5 + 3])
lens_bmin = meansize * numpy.sqrt(parameters[i5 + 3])
elens = Ellipse((xxx, yyy), lens_bmaj, lens_bmin, \
angle=lens_pa, ec='orange', lw=1.0, \
zorder=20, fill=False)
ax.add_artist(elens)
# get the image centroid in model pixel coordinates
headim = data[0].header
headmod = model[0].header
im = data[0].data
im = im[0, 0, :, :]
# good region is where mask is zero
mask = setuputil.makeMask(config)
goodregion = mask == 0
# compute sigma image from cutout of SMA flux image
# dv = 1.
# bunit = headim['BUNIT']
# if bunit == 'JY/BEAM.KM/S':
# dv = 500.
rms = im[goodregion].std()# * dv
# Obtain measurements of beamsize and image min/max
bmaj = headim['BMAJ'] * 3600
bmin = headim['BMIN'] * 3600
bpa = headim['BPA']
cdelt1 = headim['CDELT1'] * 3600
cdelt2 = headim['CDELT2'] * 3600
cell = numpy.sqrt( abs(cdelt1) * abs(cdelt2) )
im_model = model[0].data
if im_model.ndim == 4:
im_model = im_model[0, 0, :, :]
#nx_model = im_model[0, :].size
pixextent = radialextent / cell
datawcs = wcs.WCS(headim, naxis=2)
pix = datawcs.wcs_world2pix(ra_centroid, dec_centroid, 1)
x0 = numpy.round(pix[0])
y0 = numpy.round(pix[1])
imrady = numpy.round(radialextent / cell)# nymod / 2.
imradx = numpy.round(radialextent / cell)# nxmod / 2.
# make data cutout
totdx1 = x0 - imradx
totdx2 = x0 + imradx
totdy1 = y0 - imrady
totdy2 = y0 + imrady
datacut = im[totdy1:totdy2,totdx1:totdx2]
# make cleaned model cutout
headerkeys = headmod.keys()
cd1_1 = headerkeys.count('CD1_1')
cd1_2 = headerkeys.count('CD1_2')
if cd1_1 == 0:
cdelt1_model = numpy.abs(headmod['CDELT1'] * 3600)
cdelt2_model = numpy.abs(headmod['CDELT2'] * 3600)
else:
cdelt1_model = numpy.abs(headmod['CD1_1'] * 3600)
cdelt2_model = numpy.abs(headmod['CD2_2'] * 3600)
cd11 = headmod['CD1_1']
if cd1_2 == 0:
cd12 = 0
cd21 = 0
else:
cd12 = headmod['CD1_2']
cd21 = headmod['CD2_1']
cd22 = headmod['CD2_2']
cdelt1_model = numpy.sqrt(cd11 ** 2 + cd12 ** 2) * 3600
cdelt2_model = numpy.sqrt(cd21 ** 2 + cd22 ** 2) * 3600
if cd12 == 0:
cd12 = cd11 / 1e8
cdratio = numpy.abs(cd11 / cd12)
if cdratio < 1:
cdratio = 1 / cdratio
cellmod = numpy.sqrt( abs(cdelt1_model) * abs(cdelt2_model) )
modelwcs = wcs.WCS(headmod, naxis=2)
pix = modelwcs.wcs_world2pix(ra_centroid, dec_centroid, 1)
x0 = numpy.round(pix[0])
y0 = numpy.round(pix[1])
modrady = numpy.round(radialextent / cellmod)
modradx = numpy.round(radialextent / cellmod)
totdx1 = x0 - modradx
totdx2 = x0 + modradx
totdy1 = y0 - modrady
totdy2 = y0 + modrady
modelcut = im_model[totdy1:totdy2,totdx1:totdx2]
#cellp = cell * (2 * pixextent + 1.1) / (2 * pixextent)
xlo = -radialextent
xhi = radialextent
ylo = -radialextent
yhi = radialextent
ncell = modelcut[:, 0].size#(xhi - xlo) / cell
modx = -numpy.linspace(xlo, xhi, ncell)
mody = numpy.linspace(ylo, yhi, ncell)
#modx = -(numpy.arange(2 * pixextent) - pixextent) * cellp - cell/2.
#mody = (numpy.arange(2 * pixextent) - pixextent) * cellp + cell/2.
cornerextent = [modx[0], modx[-1], mody[0], mody[-1] ]
if modeltype == 'residual':
grayscalename = 'Residual'
pcolor = 'white'
ncolor = 'black'
vmax = 5 * rms
vmin = -5 * rms
elif modeltype == 'model':
grayscalename = 'Model'
pcolor = 'red'
ncolor = 'red'
vmax = modelcut.max()
vmin = modelcut.min()
else:
grayscalename = config['OpticalTag']
filtindx = grayscalename.find(' ')
filtname = grayscalename[filtindx + 1:]
if filtname == 'F110W':
modelcut = numpy.log10(modelcut - modelcut.min() + 1)
pcolor = 'red'
ncolor = 'red'
vmax = modelcut.max()
vmin = modelcut.min()
plt.imshow(modelcut, cmap='gray_r', interpolation='nearest', \
extent=cornerextent, origin='lower', vmax=vmax, vmin=vmin)
plevs = 3*rms * 2**(numpy.arange(10))
nlevs = sorted(-3 * rms * 2**(numpy.arange(4)))
pcline = 'solid'
ncline = 'dashed'
#nx_contour = datacut[0, :].size
#ny_contour = datacut[:, 0].size
#cmodx = -(numpy.arange(nx_contour) - pixextent) * cellp - cell/2.
#cmody = (numpy.arange(ny_contour) - pixextent) * cellp + cell/2.
ncell = datacut[:, 0].size#(xhi - xlo) / cell
cmodx = -numpy.linspace(xlo, xhi, ncell)
cmody = numpy.linspace(ylo, yhi, ncell)
plt.contour(cmodx, cmody, datacut, colors=pcolor, levels=plevs, \
linestyles=pcline, linewidths=1.5)
plt.contour(cmodx, cmody, datacut, colors=ncolor, levels=nlevs, \
linestyles=ncline, linewidths=1.5)
# plot the critical curve
#plt.contour(cmodx, cmody, dmu, colors='orange', levels=[100])
#axisrange = plt.axis()
axisrange = numpy.array([xhi,xlo,ylo,yhi]).astype(float)
plt.axis(axisrange)
plt.minorticks_on()
plt.tick_params(width=1.5, which='both')
plt.tick_params(length=2, which='minor')
plt.tick_params(length=4, which='major')
#plt.xlabel(r'$\Delta$RA (arcsec)', fontsize='x-large')
#plt.ylabel(r'$\Delta$Dec (arcsec)', fontsize='x-large')
bparad = bpa / 180 * numpy.pi
beamx = numpy.abs(numpy.sin(bparad) * bmaj) + \
numpy.abs(numpy.cos(bparad) * bmin)
beamy = numpy.abs(numpy.cos(bparad) * bmaj) + \
numpy.abs(numpy.sin(bparad) * bmin)
beamxhi = 2 * pixextent / cell
beamxlo = -2 * pixextent / cell
beamyhi = 2 * pixextent / cell
beamylo = -2 * pixextent / cell
beamdx = numpy.float(beamxhi) - numpy.float(beamxlo)
beamdy = numpy.float(beamyhi) - numpy.float(beamylo)
bufferx = 0.03 * beamdx / 6.0
buffery = 0.03 * beamdx / 6.0
xpos = 1 - beamx/beamdx/2 - bufferx
ypos = beamy/beamdy/2 + buffery
#beamx = bmaj * numpy.abs(numpy.cos(bparad))
#beamy = bmaj * numpy.abs(numpy.sin(bparad))
xpos = 0.95 * axisrange[1] + 0.95 * beamx / 2.
ypos = 0.95 * axisrange[2] + 0.95 * beamy / 2.
e = Ellipse((xpos,ypos), bmaj, bmin, angle=90 - bpa, ec='black', \
hatch='//////', lw=1.0, fc='None', zorder=10, fill=True)
ax.add_artist(e)
plt.text(0.92, 0.88, grayscalename, transform=ax.transAxes,
fontsize='xx-large', ha='right')
try:
from astropy.table import Table
tloc = '../../../Papers/Bussmann_2015a/Bussmann2015/Data/targetlist.dat'
hackstep = Table.read(tloc, format='ascii')
objname = config['ObjectName']
match = hackstep['dataname'] == objname
shortname = hackstep['shortname'][match][0]
plt.text(0.08, 0.88, shortname, transform=ax.transAxes,
fontsize='xx-large')
except:
pass
bigtag = '.' + modeltype + '.' + tag
savefig('LensedSBmap' + bigtag + '.pdf')
#plt.clf()
def removeTempFiles():
"""
Remove files created along the way by visualutil routines.
"""
import os
cmd = 'rm -rf *SBmap*fits *_model* *_residual* *output.txt'
os.system(cmd)
def plotFit(config, fitresult, tag='', cleanup=True, showOptical=False,
interactive=True, plotonly=False):
"""
Plot a particular model fit.
"""
from astropy.io import fits
if not plotonly:
# make the lensed image
makeSBmap(config, fitresult)
# are we using miriad to image the best-fit model?
if config.keys().count('UseMiriad') > 0:
miriad = config['UseMiriad']
if miriad == 'Visualize':
miriad = True
interactive = False
else:
miriad = False
# make the simulated visibilities
makeVis(config, miriad=miriad, idtag=tag)
# image the simulated visibilities
makeImage(config, miriad=miriad, interactive=interactive, idtag=tag)
# read in the images of the simulated visibilities
objectname = config['ObjectName']
simimloc = objectname + '_clean_model.fits'
model = fits.open(simimloc)
simimloc = objectname + '_clean_residual.fits'
residual = fits.open(simimloc)
# read in the data
data = fits.open(config['ImageName'])
if showOptical:
# read in the data
optical = fits.open(config['OpticalImage'])
# plot the images
plotImage(optical, data, config, 'optical', fitresult, tag=tag)
# plot the images
plotImage(model, data, config, 'model', fitresult, tag=tag)
# plot the residual
plotImage(residual, residual, config, 'residual', fitresult, tag=tag)
# remove the intermediate files
if cleanup:
removeTempFiles()
def preProcess(config, paramData, fitresult, tag='', cleanup=True,
showOptical=False, interactive=True):
"""
Cycle through each region and run plotFit, selecting parameters
appropriately.
"""
import setuputil
import numpy
import re
# Loop over each region
nlensedsource = paramData['nlensedsource']
nlensedregions = paramData['nlensedregions']
npar_previous = 0
configkeys = config.keys()