In [1]:
import numpy as np
import os
import pandas as pd
import itertools
from PIL import Image
import seaborn as sns
import fitsio
import skimage.io
import galsim
from astrometry.util.fits import fits_table, merge_tables
# to make this notebook's output stable across runs
np.random.seed(7)
# To plot pretty figures
#%matplotlib inline
%matplotlib notebook
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
%load_ext autoreload
%autoreload 2
In [2]:
from obiwan.qa.visual import readImage,sliceImage,plotImage, flux2mag
In [3]:
# Testcase functions not in py/obiwan/ directory
a=os.path.join(os.getcwd(),
"../../tests/end_to_end")
!ls $a
import sys
sys.path.append(a)
In [5]:
print('hello from docker image')
In [17]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
add_noise=False,all_blobs=True)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
t.simcat_xy()
#t.numeric_tests()
isim,itrac,ireal= t.match_simcat_tractor()
In [13]:
fig,ax=plt.subplots(figsize=(3,3))
plotImage().imshow(t.img_fits['z'],ax,qs=None)
plotImage().circles(t.simcat.x,t.simcat.y,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.obitractor[ireal].bx,t.obitractor[ireal].by,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=4./0.262,color='m')
In [18]:
import photutils
apers= photutils.CircularAperture((t.simcat.x,t.simcat.y),
r=3.5/t.brickwcs.pixel_scale())
apy_table = photutils.aperture_photometry(t.img_fits['z'], apers)
img_apflux= np.array(apy_table['aperture_sum'])
apy_table = photutils.aperture_photometry(t.sims_fits['z'], apers)
sims_apflux= np.array(apy_table['aperture_sum'])
sky_apflux= img_apflux - sims_apflux
#sky_meas= t.obitractor[itrac].flux_z - t.simcat.zflux
print('simcat id',t.simcat.id)
print('sky_apflux:',sky_apflux)
#print('sky_meas:',sky_meas)
print('sims_apflux:',sims_apflux)
print('simcat_flux:',t.simcat.zflux)
print('obitrac_flux:',t.obitractor[itrac].flux_z)
print('img_apflux:',img_apflux)
print('obitrac_apflux:',t.obitractor[itrac].apflux_z[:,5])
print(sky_apflux + sims_apflux - t.obitractor[itrac].apflux_z[:,5])
print(t.obitractor[itrac].flux_z - t.simcat.zflux)
In [24]:
np.max([t.obitractor[itrac].shapeexp_r,t.obitractor[itrac].shapeexp_r],axis=1)
Out[24]:
In [19]:
t.obitractor[ireal].apflux_z[:,5], t.obitractor[ireal].flux_z
Out[19]:
In [20]:
t.simcat.rhalf,t.simcat.e1,t.simcat.e2,t.simcat.n
Out[20]:
In [21]:
t.obitractor[itrac].type,t.obitractor[itrac].shapeexp_r
Out[21]:
In [22]:
t.obitractor[itrac].shapeexp_r-t.simcat.rhalf
Out[22]:
In [11]:
t.obitractor[itrac].shapeexp_r-t.simcat.rhalf
Out[11]:
In [6]:
def flux2mag(flux):
return -2.5*np.log10(1e-9 * flux)
def mag2flux(mag):
return 10**(9-mag/2.5)
t.obitractor[ireal].type,t.obitractor[ireal].flux_z, flux2mag(t.obitractor[ireal].flux_z)
Out[6]:
In [50]:
t.obitractor[ireal].shapedev_r,t.obitractor[ireal].shapedev_e1,t.obitractor[ireal].shapedev_e2
Out[50]:
In [51]:
print(t.obitractor[itrac].type,t.obitractor[itrac].flux_z, flux2mag(t.obitractor[itrac].flux_z))
t.obitractor[itrac].shapeexp_r,t.obitractor[itrac].shapeexp_e1,t.obitractor[itrac].shapeexp_e2
Out[51]:
In [31]:
x=np.array([198,2,100,150]) + t.zoom[0]
y=np.array([2,198,2,198]) + t.zoom[2]
t.brickwcs.pixelxy2radec(x,y)
Out[31]:
In [47]:
t.simcat.ra
Out[47]:
In [49]:
fig,ax=plt.subplots(3,3,figsize=(7,6))
for i,b in enumerate(t.bands) :
plotImage().imshow(t.img_fits[b],ax[0,i])
plotImage().imshow(t.sims_fits[b],ax[1,i],qs=None)
plotImage().circles(t.obitractor.bx,t.obitractor.by,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.simcat.x,t.simcat.y,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=4./0.262,color='m')
plotImage().imshow(t.img_fits[b] - t.sims_fits[b],ax[2,i])
In [12]:
fig,ax=plt.subplots(2,2,figsize=(6,6))
plotImage().imshow(t.blobs,ax[0,0],qs=None)
plotImage().imshow(t.img_jpg,ax[0,1],qs=None)
plotImage().imshow(t.model_jpg,ax[1,0],qs=None)
plotImage().imshow(t.resid_jpg,ax[1,1],qs=None)
In [15]:
plot3d(t.ivar_fits['z'])
In [50]:
len(isim),len(itrac),len(ireal)
Out[50]:
In [51]:
t.simcat.zflux, t.obitractor[itrac].flux_z
Out[51]:
In [52]:
import photutils
apers= photutils.CircularAperture((t.simcat.x,t.simcat.y),
r=3.5/t.brickwcs.pixel_scale())
apy_table = photutils.aperture_photometry(t.img_fits['z'], apers)
img_apflux= np.array(apy_table['aperture_sum'])
apy_table = photutils.aperture_photometry(t.sims_fits['z'], apers)
sims_apflux= np.array(apy_table['aperture_sum'])
sky_apflux= img_apflux - sims_apflux
#sky_meas= t.obitractor[itrac].flux_z - t.simcat.zflux
print('sky_apflux:',sky_apflux)
#print('sky_meas:',sky_meas)
print('sims_apflux:',sims_apflux)
print('simcat_flux:',t.simcat.zflux)
print('obitrac_flux:',t.obitractor[itrac].flux_z)
print('obitrac_apflux:',t.obitractor[itrac].apflux_z[:,5])
# apers= photutils.CircularAperture(([100],[100]),
# r=3.5/brickwcs.pixel_scale())
# apy_table = photutils.aperture_photometry(img_fits['g'], apers)
# real_apflux= np.array(apy_table['aperture_sum'])
# print(simcat.gflux,
# sim_apflux,
# real_apflux)
# print(flux2mag(simcat.gflux) - flux2mag(sim_apflux),flux2mag(simcat.gflux[0]),flux2mag(real_apflux))
In [53]:
print(sky_apflux + sims_apflux - t.obitractor[itrac].apflux_z[:,5])
In [54]:
print(t.simcat.zflux + sky_apflux - t.obitractor[itrac].apflux_z[:,5])
In [55]:
apers= photutils.CircularAperture((t.obitractor[ireal].bx,t.obitractor[ireal].by),
r=3.5/t.brickwcs.pixel_scale())
apy_table = photutils.aperture_photometry(t.img_fits['z'], apers)
img_apflux= np.array(apy_table['aperture_sum'])
print('real galaxy apflux:',img_apflux)
print('real galaxy obitrac flux:',t.obitractor[ireal].flux_z)
print('real galaxy obitrac apflux:',t.obitractor[ireal].apflux_z[0][5])
print(flux2mag(img_apflux) - flux2mag(t.obitractor[ireal].flux_z))
In [9]:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
def plot3d(image,zlim=None):
data = [
go.Surface(
z=image
)
]
layout=go.Layout()
if zlim:
layout = go.Layout(
scene= dict(
zaxis = dict(
nticks=10, range = [zlim[0],zlim[1]]),
)
)
fig = go.Figure(data=data, layout=layout)
#fig = go.Figure(data=data)
iplot(fig, filename='image')
In [ ]:
plot3d(img_fits)
In [ ]:
plot3d(sims_fits)
In [24]:
plot3d(img_fits - sims_fits)
In [4]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_grz',dataset='DR5',
all_blobs=True,early_coadds=True)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
In [5]:
fig,ax=plt.subplots(3,3,figsize=(7,6))
for i,b in enumerate(t.bands) :
plotImage().imshow(t.img_fits[b],ax[0,i])
plotImage().imshow(t.sims_fits[b],ax[1,i],qs=None)
plotImage().circles(t.simcat.x,t.simcat.y,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=4./0.262,color='m')
plotImage().imshow(t.img_fits[b] - t.sims_fits[b],ax[2,i])
In [60]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
obj='star',
onedge=False,add_noise=False,all_blobs=True)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
t.simcat_xy()
#t.numeric_tests()
isim,itrac,ireal= t.match_simcat_tractor()
In [61]:
fig,ax=plt.subplots(figsize=(3,3))
plotImage().imshow(t.img_fits['z'],ax,qs=None)
plotImage().circles(t.simcat.x,t.simcat.y,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.obitractor[ireal].bx,t.obitractor[ireal].by,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=4./0.262,color='m')
In [62]:
import photutils
apers= photutils.CircularAperture((t.simcat.x,t.simcat.y),
r=3.5/t.brickwcs.pixel_scale())
apy_table = photutils.aperture_photometry(t.img_fits['z'], apers)
img_apflux= np.array(apy_table['aperture_sum'])
apy_table = photutils.aperture_photometry(t.sims_fits['z'], apers)
sims_apflux= np.array(apy_table['aperture_sum'])
sky_apflux= img_apflux - sims_apflux
#sky_meas= t.obitractor[itrac].flux_z - t.simcat.zflux
print(t.simcat.id)
print('sky_apflux:',sky_apflux)
#print('sky_meas:',sky_meas)
print('sims_apflux:',sims_apflux)
print('simcat_flux:',t.simcat.zflux)
print('obitrac_flux:',t.obitractor[itrac].flux_z)
print('obitrac_apflux:',t.obitractor[itrac].apflux_z[:,5])
added_flux= np.array([39.135,39.102,39.122,39.115])
print('added_flux/1.02',added_flux/1.02)
In [8]:
t.obitractor[itrac].type
Out[8]:
In [12]:
dflux= t.simcat.zflux + \
sky_apflux - t.obitractor[itrac].apflux_z[:,5]
print('diff flux: ',dflux)
dmag= flux2mag(t.simcat.zflux + sky_apflux) - flux2mag(t.obitractor[itrac].apflux_z[:,5])
print('diff mag: ',dmag)
In [19]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
onedge=True,add_noise=False,all_blobs=False)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
t.simcat_xy()
t.numeric_tests()
isim,itrac,ireal= t.match_simcat_tractor()
print(t.simcat.id[isim])
print([(x,y) for x,y in zip(t.simcat.x[isim],t.simcat.y[isim])])
In [20]:
fig,ax=plt.subplots(figsize=(3,3))
plotImage().imshow(t.img_fits['z'],ax,qs=None)
plotImage().circles(t.simcat.x,t.simcat.y,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.obitractor[itrac].bx,t.obitractor[itrac].by,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=4./0.262,color='m')
In [18]:
rhalf= np.max((t.obitractor[itrac].shapeexp_r,t.obitractor[itrac].shapedev_r),
axis=0)
e1= np.max((t.obitractor[itrac].shapeexp_e1,t.obitractor[itrac].shapedev_e1),
axis=0)
e2= np.max((t.obitractor[itrac].shapeexp_e2,t.obitractor[itrac].shapedev_e2),
axis=0)
print(t.simcat[isim].id)
print(t.obitractor[itrac].type,rhalf,e1,e2)
print(t.simcat.n[isim],t.simcat.rhalf[isim],t.simcat.e1[isim],t.simcat.e2[isim])
print(t.simcat.zflux[isim] - t.obitractor.flux_z[itrac])
In [24]:
import galsim
a=galsim.PositionD(10,10)
a.x
Out[24]:
In [39]:
a=galsim.Image(np.zeros((10,10)))
a.shift(dx=0.5,dy=0.5)
a.setCenter(10,10)
In [40]:
a.trueCenter(),a.center()
Out[40]:
In [22]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
add_noise=False,all_blobs=False)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
t.simcat_xy()
#t.numeric_tests()
isim,itrac,ireal= t.match_simcat_tractor()
In [25]:
fig,ax=plt.subplots(figsize=(3,3))
plotImage().imshow(t.img_fits['z'],ax,qs=None)
plotImage().circles(t.simcat.x,t.simcat.y,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=7./0.25,color='y')
plotImage().circles(t.obitractor[itrac].bx,t.obitractor[itrac].by,ax,
img_shape=t.img_fits['z'].shape,
r_pixels=4./0.25,color='m')
In [23]:
import photutils
apers= photutils.CircularAperture((t.simcat.x,t.simcat.y),
r=3.5/t.brickwcs.pixel_scale())
apy_table = photutils.aperture_photometry(t.img_fits['z'], apers)
img_apflux= np.array(apy_table['aperture_sum'])
apy_table = photutils.aperture_photometry(t.sims_fits['z'], apers)
sims_apflux= np.array(apy_table['aperture_sum'])
sky_apflux= img_apflux - sims_apflux
#sky_meas= t.obitractor[itrac].flux_z - t.simcat.zflux
print('sky_apflux:',sky_apflux)
#print('sky_meas:',sky_meas)
print('sims_apflux:',sims_apflux)
print('simcat_flux:',t.simcat.zflux)
print('obitrac_flux:',t.obitractor[itrac].flux_z)
print('obitrac_apflux:',t.obitractor[itrac].apflux_z[:,5])
In [24]:
print(t.simcat.rhalf[isim],t.simcat.e1[isim],t.simcat.e2[isim])
print(t.obitractor.type[itrac],t.obitractor.shapeexp_r[itrac],t.obitractor.shapeexp_e1[itrac],t.obitractor.shapeexp_e2[itrac])
In [26]:
fig,ax=plt.subplots(3,3,figsize=(7,6))
for i,b in enumerate(t.bands) :
plotImage().imshow(t.img_fits[b],ax[0,i])
plotImage().imshow(t.sims_fits[b],ax[1,i],qs=None)
plotImage().circles(t.obitractor.bx,t.obitractor.by,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.simcat.x,t.simcat.y,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=4./0.262,color='m')
plotImage().imshow(t.img_fits[b] - t.sims_fits[b],ax[2,i])
In [11]:
plot3d(t.img_fits['z'])
In [ ]:
all_blobs == False
In [23]:
### No noise
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
add_noise=False,all_blobs=False)
no= AnalyzeTestcase(**kwargs)
no.load_outputs()
isim,itrac,ireal= no.match_simcat_tractor()
print(no.obitractor[itrac].apflux_z)[:,5]
plot3d(no.sims_fits['z'])
In [24]:
### w/noise original formula
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
add_noise=True,all_blobs=False)
noise= AnalyzeTestcase(**kwargs)
noise.load_outputs()
isim,itrac,ireal= noise.match_simcat_tractor()
print(noise.obitractor[itrac].apflux_z)[:,5]
plot3d(noise.sims_fits['z'])
In [40]:
### w/noise mine
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_z',dataset='DR5',
add_noise=True,all_blobs=False)
my= AnalyzeTestcase(**kwargs)
my.load_outputs()
isim,itrac,ireal= my.match_simcat_tractor()
print(my.obitractor[itrac].apflux_z)[:,5]
plot3d(my.img_fits['z'])
In [31]:
sns.distplot(np.random.randn(100,100).flatten())
Out[31]:
In [8]:
print(no.obitractor[itrac].apflux_z[:,5] - my.obitractor[itrac].apflux_z[:,5])
print(no.obitractor[itrac].apflux_z[:,5] - noise.obitractor[itrac].apflux_z[:,5])
In [62]:
fig,ax=plt.subplots(2,2,figsize=(6,6))
plotImage().imshow(t.blobs,ax[0,0],qs=None)
plotImage().imshow(t.img_jpg,ax[0,1],qs=None)
plotImage().imshow(t.model_jpg,ax[1,0],qs=None)
plotImage().imshow(t.resid_jpg,ax[1,1],qs=None)
In [83]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_grz',dataset='DR5',
obj='elg',
add_noise=False,all_blobs=True)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
t.simcat_xy()
#t.numeric_tests()
isim,itrac,ireal= t.match_simcat_tractor()
In [84]:
fig,ax=plt.subplots(4,3,figsize=(7,8))
for i,b in enumerate(t.bands) :
plotImage().imshow(t.img_fits[b],ax[0,i])
plotImage().imshow(t.sims_fits[b],ax[1,i],qs=None)
plotImage().circles(t.obitractor.bx,t.obitractor.by,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.simcat.x,t.simcat.y,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=4./0.262,color='m')
plotImage().imshow(t.img_fits[b] - t.sims_fits[b],ax[2,i])
plotImage().imshow(t.ivar_fits[b],ax[3,i])
In [ ]:
In [23]:
plot3d(t.ivar_fits['r'])
In [85]:
fig,ax=plt.subplots(2,2,figsize=(6,6))
plotImage().imshow(t.blobs,ax[0,0],qs=None)
plotImage().imshow(t.img_jpg,ax[0,1],qs=None)
plotImage().imshow(t.model_jpg,ax[1,0],qs=None)
plotImage().imshow(t.resid_jpg,ax[1,1],qs=None)
In [71]:
len(isim),len(itrac),len(ireal)
Out[71]:
In [72]:
t.simcat.zflux, t.obitractor[itrac].flux_z
Out[72]:
In [78]:
import photutils
apers= photutils.CircularAperture((t.simcat.x,t.simcat.y),
r=3.5/t.brickwcs.pixel_scale())
for band in t.bands:
print('band=',band)
apy_table = photutils.aperture_photometry(t.img_fits[b], apers)
img_apflux= np.array(apy_table['aperture_sum'])
apy_table = photutils.aperture_photometry(t.sims_fits[b], apers)
sims_apflux= np.array(apy_table['aperture_sum'])
sky_apflux= img_apflux - sims_apflux
#sky_meas= t.obitractor[itrac].flux_z - t.simcat.zflux
simcat_flux= t.simcat.get(b+'flux')
trac_flux= t.obitractor[itrac].get('flux_'+b)
trac_apflux=t.obitractor[itrac].get('apflux_'+b)[:,5]
print('sky_apflux:',sky_apflux)
#print('sky_meas:',sky_meas)
print('sims_apflux:',sims_apflux)
print('simcat_flux:',simcat_flux)
print('obitrac_flux:',trac_flux)
print('obitrac_apflux:',trac_apflux)
print(sky_apflux + sims_apflux - trac_apflux)
print(simcat_flux + sky_apflux - trac_apflux)
In [74]:
t.img_fits['g'].shape
Out[74]:
In [79]:
t.obitractor[ireal].type,t.obitractor[ireal].shapeexp_r,t.obitractor[ireal].shapedev_r,
Out[79]:
In [80]:
for mod in ['shapedev','shapeexp']:
print(t.obitractor[ireal].get(mod+'_e1'),t.obitractor[ireal].get(mod+'_e2'))
In [81]:
for b in 'grz':
print(b,flux2mag(t.obitractor[ireal].get('flux_'+b)))
In [86]:
t.obitractor[itrac].type,t.obitractor[itrac].shapeexp_r,t.obitractor[itrac].shapedev_r,
Out[86]:
In [87]:
t.obitractor[itrac].shapeexp_e1,t.obitractor[itrac].shapeexp_e2
Out[87]:
In [88]:
t.obitractor[itrac].shapedev_e1,t.obitractor[itrac].shapedev_e2
Out[88]:
In [90]:
for b in 'grz':
print(b,flux2mag(t.obitractor[itrac].get('flux_'+b)))
In [66]:
apers= photutils.CircularAperture((t.obitractor[ireal].bx,t.obitractor[ireal].by),
r=3.5/t.brickwcs.pixel_scale())
apy_table = photutils.aperture_photometry(t.img_fits['z'], apers)
img_apflux= np.array(apy_table['aperture_sum'])
print('real galaxy apflux:',img_apflux)
print('real galaxy obitrac flux:',t.obitractor[ireal].flux_z)
print('real galaxy obitrac apflux:',t.obitractor[ireal].apflux_z[0][5])
In [41]:
from test_datasets import Testcase,AnalyzeTestcase
kwargs= dict(name='testcase_DR5_grz',dataset='DR5',
obj='elg',
add_noise=False,all_blobs=False)
t= AnalyzeTestcase(**kwargs)
t.load_outputs()
t.simcat_xy()
#t.numeric_tests()
isim,itrac,ireal= t.match_simcat_tractor()
In [42]:
fig,ax=plt.subplots(4,3,figsize=(7,8))
for i,b in enumerate(t.bands) :
plotImage().imshow(t.img_fits[b],ax[0,i])
plotImage().imshow(t.sims_fits[b],ax[1,i],qs=None)
plotImage().circles(t.obitractor.bx,t.obitractor.by,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=5./0.262,color='y')
plotImage().circles(t.simcat.x,t.simcat.y,ax[1,i],
img_shape=t.sims_fits[b].shape,
r_pixels=4./0.262,color='m')
plotImage().imshow(t.img_fits[b] - t.sims_fits[b],ax[2,i])
plotImage().imshow(t.ivar_fits[b],ax[3,i])
In [44]:
print(t.obitractor[itrac].type,t.obitractor[itrac].shapeexp_r,t.obitractor[itrac].shapedev_r)
print(t.simcat[isim].rhalf)
In [45]:
print(t.obitractor[itrac].shapeexp_e1,t.obitractor[itrac].shapedev_e1)
print(t.simcat[isim].e1)
In [46]:
print(t.obitractor[itrac].shapeexp_e2,t.obitractor[itrac].shapedev_e2)
print(t.simcat[isim].e2)
In [104]:
for b in 'grz':
print(b,flux2mag(t.obitractor[itrac].get('flux_'+b)))
In [28]:
plot3d(t.ivar_fits['g'])
In [115]:
fig,ax=plt.subplots(2,2,figsize=(6,6))
plotImage().imshow(t.blobs,ax[0,0],qs=None)
plotImage().imshow(t.img_jpg,ax[0,1],qs=None)
plotImage().imshow(t.model_jpg,ax[1,0],qs=None)
plotImage().imshow(t.resid_jpg,ax[1,1],qs=None)
In [90]:
apers= photutils.CircularAperture((t.simcat.x,t.simcat.y),
r=3.5/t.brickwcs.pixel_scale())
for band in t.bands:
print('band=',band)
apy_table = photutils.aperture_photometry(t.img_fits[b], apers)
img_apflux= np.array(apy_table['aperture_sum'])
apy_table = photutils.aperture_photometry(t.sims_fits[b], apers)
sims_apflux= np.array(apy_table['aperture_sum'])
sky_apflux= img_apflux - sims_apflux
#sky_meas= t.obitractor[itrac].flux_z - t.simcat.zflux
simcat_flux= t.simcat.get(b+'flux')
trac_flux= t.obitractor[itrac].get('flux_'+b)
trac_apflux=t.obitractor[itrac].get('apflux_'+b)[:,5]
print('sky_apflux:',sky_apflux)
#print('sky_meas:',sky_meas)
print('sims_apflux:',sims_apflux)
print('simcat_flux:',simcat_flux)
print('obitrac_flux:',trac_flux)
print('obitrac_apflux:',trac_apflux)
print(sky_apflux + sims_apflux - trac_apflux)
print(simcat_flux + sky_apflux - trac_apflux)
In [137]:
a=np.ones((10,10))+34
a /= a.sum()
a.sum()
Out[137]:
In [148]:
assert(len(itrac) == 4)
print(simcat[isim].zflux-obitractor[itrac].flux_z)
print(np.sqrt(1/obitractor[itrac].flux_ivar_z))
print(flux2mag(simcat[isim].zflux)-flux2mag(obitractor[itrac].flux_z))
In [102]:
# sizes?
print(simcat[isim].rhalf-obitractor[itrac].shapeexp_r)
In [103]:
# Does extinction vary by this much? I doubt it...
mags= flux2mag(obitractor[itrac].flux_z)
print('measured mag variation=',mags - np.median(mags))
mags= flux2mag(obitractor[itrac].flux_z/obitractor[itrac].mw_transmission_z)
print('extinction variation=',mags - np.median(mags))
In [104]:
np.std(obitractor.mw_transmission_z)
Out[104]:
In [105]:
# Diff b/w fake truth and measure real galaxy identical
print(simcat[isim].zflux-obitractor[ireal].flux_z)
print(flux2mag(simcat[isim].zflux)-flux2mag(obitractor[ireal].flux_z))
In [106]:
# But diff b/w measure fake and measure real galaxy are large
print(obitractor[itrac].flux_z-obitractor[ireal].flux_z)
print(flux2mag(obitractor[itrac].flux_z)-flux2mag(obitractor[ireal].flux_z))
In [110]:
obitractor.flux_z
Out[110]:
In [107]:
In [113]:
plot3d(img_jpg[:,:,0])
In [114]:
plot3d(model_jpg[:,:,0])
In [22]:
fig,ax=plt.subplots()
plotImage().imshow(blobs,ax,qs=None)
In [27]:
ra=[174.24714700910047, 174.24715333044438, 174.24915898792247, 174.2451413528916]
dec=[24.326224120319477, 24.32988191327332, 24.328050866811363, 24.32805515572893]
np.mean(ra),np.mean(dec)
Out[27]:
In [41]:
os.environ["LEGACY_SURVEY_DIR"]= IN_DIR
from legacypipe.survey import LegacySurveyData, wcs_for_brick
survey = LegacySurveyData()
brickinfo = survey.get_brick_by_name('1741p242')
brickwcs = wcs_for_brick(brickinfo)
In [28]:
brickwcs.radec2pixelxy(174.24715,24.32805)
Out[28]:
In [38]:
_,x,y= brickwcs.radec2pixelxy(174.2471,24.3262)
x,y= int(x),int(y)+25
hw=100
fig,ax=plt.subplots()
xslc=slice(x-hw,x+hw)
yslc=slice(y-hw,y+hw)
plotImage().imshow(sliceImage(blobs,xslice=xslc,yslice=yslc),
ax,qs=None)
print(xslc,yslc)
1) use viewer to find good source, write down brickname, ra,dec of source and 3 good ccds touching that ra,dec by clicking on the source in the viewer
2) grab the coadd/ccds file for that brick and cut to just those three ccds
3) create a initial dir structure by running create_testcase on NERSC using the 3-ccd ccd file. This is NOT the final product yet, but is need for #4.
4) You need to read each of the ccds as tim objects and print each of their x,y pixel positions for the ra,dec above. This requires the files output by #3 and the original images (on project at NERSC). scp -r the output dir from #3 to a dir on laptop. Keep the subdirs in images/ but remove the images you scp'd over. Copy the actual oo[idw] images from project to each band's dir. Then run the "Correct Region?" and "TIM object for each CCD" and "x,y pixels for each ccd" cells below. This uses the tim.wcs.wcs.radec2pixelxy func
6) replace the ccds table with these new values for ccd_x0,_x1,_y0,_y1 (round to nearest integer) then copy the ccds table back to NERSC and rerun create_testcase but using the new ccds table.
7) Done!
In [27]:
# DR3: Correct Region?
from legacypipe.survey import LegacySurveyData, wcs_for_brick
dr= os.path.join(os.environ['HOME'],
'myrepo/legacypipe/dr3_testcase')
os.environ["LEGACY_SURVEY_DIR"]= dr
survey = LegacySurveyData()
brick='2357p087'
brickinfo = survey.get_brick_by_name(brick)
brickwcs = wcs_for_brick(brickinfo)
ra,dec= 235.6799,8.8597
print(brickwcs.radec2pixelxy(ra,dec))
# Plot
_,x,y= brickwcs.radec2pixelxy(ra,dec)
x,y= int(x),int(y)
hw=100
fig,ax=plt.subplots()
xslc=slice(x-hw,x+hw)
yslc=slice(y-hw,y+hw)
img_jpg= readImage(os.path.join(dr,'legacysurvey-%s-image.jpg' % brick),
jpeg=True)
plotImage().imshow(sliceImage(img_jpg,xslice=xslc,yslice=yslc),
ax,qs=None)
print(x,y)
print(x-hw,x+hw,y-hw,y+hw)
for x,y in [(x-hw,y),(x+hw,y),(x,y+hw),(x,y-hw)]:
print(brickwcs.pixelxy2radec(x,y))
In [15]:
# TIM objects for each CCD
from legacypipe.decam import DecamImage
from legacypipe.survey import LegacySurveyData
ccds= fits_table(os.path.join(dr,'dr3_testcase_ccds.fits'))
kwargs = dict(pixPsf=True, splinesky=True, subsky=True, hybridPsf=True,
pixels=True, dq=True, invvar=True)
survey = LegacySurveyData(ccds=ccds)
tims={}
#for col in ccds.get_columns():
# if 'S' in str(ccds.get(col).dtype):
# ccds.set(col,np.char.strip(ccds.get(col)))
for ccd in ccds:
im = survey.get_image_object(ccd)
tims[ccd.filter] = im.get_tractor_image(**kwargs)
In [24]:
# x,y pixels for each ccd
hw=100
for b in ccds.filter:
_,x,y= tims[b].wcs.wcs.radec2pixelxy(ra,dec)
print('%s: ' % b,x-hw,x+hw,y-hw,y+hw)
In [43]:
# brick 0285m165, RA,Dec = (28.4194, -16.4362) in viewer looks like good 3 colors source
IN_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'testcase_DR5_grz/'))
os.environ["LEGACY_SURVEY_DIR"]= IN_DIR
survey = LegacySurveyData()
brickinfo = survey.get_brick_by_name('0285m165')
brickwcs = wcs_for_brick(brickinfo)
ra,dec= 28.4194, -16.4362
brickwcs.radec2pixelxy(ra,dec)
Out[43]:
In [67]:
brick='0285m165'
DATA_DIR= os.path.join(os.environ['HOME'],
'mydata',brick,'dr5')
dr5tractor= fits_table(os.path.join(DATA_DIR,
'tractor-%s.fits' % brick))
#ccds= fits_table(os.path.join(DATA_DIR,
# 'dr5_3ccds.fits'))
#ccds= fits_table(os.path.join(DATA_DIR,
# 'ccds_0285m165_dr5coadddir.fits'))
ccds= fits_table(os.path.join(IN_DIR,'survey-ccds-1.fits.gz'))
img_jpg= readImage(os.path.join(DATA_DIR,'legacysurvey-0285m165-image.jpg'),
jpeg=True)
In [46]:
_,x,y= brickwcs.radec2pixelxy(ra,dec)
x,y= int(x),int(y)
hw=100
fig,ax=plt.subplots()
xslc=slice(x-hw,x+hw)
yslc=slice(y-hw,y+hw)
plotImage().imshow(sliceImage(img_jpg,xslice=xslc,yslice=yslc),
ax,qs=None)
print(xslc,yslc)
In [4]:
# DR6: Correct Region?
from legacypipe.survey import LegacySurveyData, wcs_for_brick
dr= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end/testcase_bassmzls_grz')
os.environ["LEGACY_SURVEY_DIR"]= dr
survey = LegacySurveyData()
brick='2176p330'
brickinfo = survey.get_brick_by_name(brick)
brickwcs = wcs_for_brick(brickinfo)
ra,dec= 217.5429, 33.0873
_,x,y= brickwcs.radec2pixelxy(ra,dec)
x,y= int(x),int(y)
hw=100
print('brick box pixels: x=[%d,%d],y=[%d,%d]' % \
(x-hw,x+hw,y-hw,y+hw))
for x,y in [(x-hw,y),(x+hw,y),(x,y+hw),(x,y-hw)]:
print(brickwcs.pixelxy2radec(x,y))
fig,ax=plt.subplots()
xslc=slice(x-hw,x+hw)
yslc=slice(y-hw,y+hw)
img_jpg= readImage(os.path.join(dr,'legacysurvey-%s-image.jpg' % brick),
jpeg=True)
plotImage().imshow(sliceImage(img_jpg,xslice=xslc,yslice=yslc),
ax,qs=None)
In [10]:
survey = LegacySurveyData(ccds=ccds)
im = survey.get_image_object(ccds[0])
kwargs = dict(pixPsf=True, splinesky=True, subsky=True, hybridPsf=True,
pixels=True, dq=True, invvar=True)
im.get_tractor_image(**kwargs)
In [11]:
# TIM objects for each CCD
from legacypipe.mosaic import MosaicImage
from legacypipe.bok import BokImage
from legacypipe.survey import LegacySurveyData
ccds= fits_table(os.path.join(dr,'survey-ccds-1.fits.gz'))
kwargs = dict(pixPsf=True, splinesky=True, subsky=True, hybridPsf=True,
pixels=True, dq=True, invvar=True)
survey = LegacySurveyData(ccds=ccds)
tims={}
#for col in ccds.get_columns():
# if 'S' in str(ccds.get(col).dtype):
# ccds.set(col,np.char.strip(ccds.get(col)))
for ccd in ccds:
im = survey.get_image_object(ccd)
tims[ccd.filter] = im.get_tractor_image(**kwargs)
In [24]:
# x,y pixels for each ccd
hw=100
for b in ccds.filter:
_,x,y= tims[b].wcs.wcs.radec2pixelxy(ra,dec)
print('%s: ' % b,x-hw,x+hw,y-hw,y+hw)
In [43]:
# brick 0285m165, RA,Dec = (28.4194, -16.4362) in viewer looks like good 3 colors source
IN_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'testcase_DR5_grz/'))
os.environ["LEGACY_SURVEY_DIR"]= IN_DIR
survey = LegacySurveyData()
brickinfo = survey.get_brick_by_name('0285m165')
brickwcs = wcs_for_brick(brickinfo)
ra,dec= 28.4194, -16.4362
brickwcs.radec2pixelxy(ra,dec)
Out[43]:
In [67]:
brick='0285m165'
DATA_DIR= os.path.join(os.environ['HOME'],
'mydata',brick,'dr5')
dr5tractor= fits_table(os.path.join(DATA_DIR,
'tractor-%s.fits' % brick))
#ccds= fits_table(os.path.join(DATA_DIR,
# 'dr5_3ccds.fits'))
#ccds= fits_table(os.path.join(DATA_DIR,
# 'ccds_0285m165_dr5coadddir.fits'))
ccds= fits_table(os.path.join(IN_DIR,'survey-ccds-1.fits.gz'))
img_jpg= readImage(os.path.join(DATA_DIR,'legacysurvey-0285m165-image.jpg'),
jpeg=True)
In [46]:
_,x,y= brickwcs.radec2pixelxy(ra,dec)
x,y= int(x),int(y)
hw=100
fig,ax=plt.subplots()
xslc=slice(x-hw,x+hw)
yslc=slice(y-hw,y+hw)
plotImage().imshow(sliceImage(img_jpg,xslice=xslc,yslice=yslc),
ax,qs=None)
print(xslc,yslc)
In [16]:
from legacypipe.decam import DecamImage
from legacypipe.survey import LegacySurveyData
In [80]:
kwargs = dict(pixPsf=True, splinesky=True, subsky=True, hybridPsf=True,
pixels=True, dq=True, invvar=True)
survey = LegacySurveyData(ccds=ccds)
tims={}
#for col in ccds.get_columns():
# if 'S' in str(ccds.get(col).dtype):
# ccds.set(col,np.char.strip(ccds.get(col)))
for ccd in ccds:
im = survey.get_image_object(ccd)
tims[ccd.filter] = im.get_tractor_image(**kwargs)
In [81]:
tim=tims['g']
tim.wcs.wcs.radec2pixelxy(ra,dec)
Out[81]:
In [82]:
_,x,y= tim.wcs.wcs.radec2pixelxy(ra,dec)
x,y= int(x),int(y)
hw=100
xslc,yslc= slice(x-hw,x+hw),slice(y-hw,y+hw)
fig,ax=plt.subplots(1,3,figsize=(7,4))
for i,band in enumerate(['g','r','z']):
plotImage().imshow(sliceImage(tims[band].getImage(),xslice=xslc,yslice=yslc).T,
ax[i])
In [87]:
def flux2mag(flux):
return -2.5*np.log10(1e-9 * flux)
hw=30
xslc,yslc= slice(x-hw,x+hw),slice(y-hw,y+hw)
fig,ax=plt.subplots(1,3,figsize=(7,4))
for i,band in enumerate(['g','r','z']):
img= sliceImage(tims[band].getImage(),xslice=xslc,yslice=yslc).T
plotImage().imshow(img,ax[i])
# Extract the nanomaggie flux for each band that we will add to the images
print(band,'nanomags=',img.sum(),'mags=',flux2mag(img.sum()))
In [72]:
# flux of this galaxy
from astrometry.libkd.spherematch import match_radec
_,itrac,d= match_radec(np.array([ra]), np.array([dec]), dr5tractor.ra, dr5tractor.dec,
5./3600.0,nearest=True)
itrac
Out[72]:
In [74]:
trac=dr5tractor.copy()
trac.cut(itrac)
len(trac)
Out[74]:
In [76]:
trac.ra,trac.dec, ra,dec
Out[76]:
In [88]:
# Tractor measurements on many imags should be close to above values for the three images
for band in ['g','r','z']:
flux=trac.get('flux_'+band)
print(band,'nanomags=',flux,'mags=',flux2mag(flux))
In [89]:
trac.ra,trac.dec,trac.type,flux2mag(trac.flux_g),flux2mag(trac.flux_r),flux2mag(trac.flux_z)
Out[89]:
In [90]:
trac.shapeexp_r,trac.shapeexp_e1,trac.shapeexp_e2
Out[90]:
In [104]:
# Lets circle ra,dec where we'll add galaxies to the r-band image
print(ra,dec)
tim= tims['r']
dd= 60 * 0.262 /3600 # pix*as/pix converted to deg
newra,newdec=[],[]
for dx,dy in zip([dd,-dd,0,0],[0,0,dd,-dd]):
#newx,newy=x+dx,y+dy
#ra,dec= tim.wcs.wcs.pixelxy2radec(newx,newy)
#print(newx,newy,ra,dec)
newra.append(ra + dx)
newdec.append(dec + dy)
print(newra)
print(newdec)
_,newx,newy= tim.wcs.wcs.radec2pixelxy(newra,newdec)
print(newx)
print(newy)
_,x,y= tim.wcs.wcs.radec2pixelxy(ra,dec)
x,y= int(x),int(y)
hw=100
xslc,yslc= slice(x-hw,x+hw),slice(y-hw,y+hw)
fig,ax=plt.subplots(figsize=(3,3))
plotImage().imshow(sliceImage(tims['r'].getImage(),xslice=xslc,yslice=yslc),
ax)
# cirlces
plotImage().circles(newx,newy,ax,
xslice=xslc,yslice=yslc,
r_pixels=5./0.262,color='y')
print('IMAGE SLICE:',xslc,yslc)
In [125]:
# start with coadd/ccds
ccds= fits_table(os.path.join(DATA_DIR,
'ccds_0285m165_dr5coadddir.fits'))
In [111]:
kwargs = dict(pixPsf=True, splinesky=True, subsky=True, hybridPsf=True,
pixels=True, dq=False, invvar=False)
survey = LegacySurveyData(ccds=ccds)
vals=[]
for band in ['g','r','z']:
t= ccds[ccds.filter == band]
for ccd in t:
im = survey.get_image_object(ccd)
tim = im.get_tractor_image(**kwargs)
_,x,y= tim.wcs.wcs.radec2pixelxy(ra,dec)
print(band,int(x),int(y))
vals.append( (band,int(x),int(y)) )
In [114]:
print(vals)
dx=100
from collections import defaultdict
x0,x1,y0,y1= defaultdict(list),defaultdict(list),defaultdict(list),defaultdict(list)
for b,xc,yc in vals:
x0[b] += [=xc-100,xc+100,yc-100,yc+100)
In [122]:
IN_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'testcase_DR5_grz_200x200/')
g=fitsio.FITS(os.path.join(IN_DIR,'images/decam/NonDECaLS/CP20141215/',
'c4d_141223_020302_ooi_g_a1-S10.fits'))[1].read()
r=fitsio.FITS(os.path.join(IN_DIR,'images/decam/NonDECaLS/CP20141215/',
'c4d_141224_033245_ooi_r_a1-S10.fits'))[1].read()
z=fitsio.FITS(os.path.join(IN_DIR,'images/decam/NonDECaLS/CP20141215/',
'c4d_141206_043719_ooi_z_a1-S10.fits'))[1].read()
g.shape
Out[122]:
In [123]:
fig,ax=plt.subplots(1,3,figsize=(6,3))
plotImage().imshow(g,ax[0])
plotImage().imshow(r,ax[1])
plotImage().imshow(z,ax[2])
It worked!
In [5]:
OUT_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'out_testcase_DR5_z/elg/174/1741p242/rs0/')
IN_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'testcase_DR5_z/')
simcat= fits_table(os.path.join(OUT_DIR,'obiwan/simcat-elg-1741p242.fits'))
obitractor= fits_table(os.path.join(OUT_DIR,'tractor/tractor-1741p242.fits'))
blobs= fitsio.FITS(os.path.join(OUT_DIR,'metrics/blobs-1741p242.fits.gz'))[0].read()
img_jpg= readImage(os.path.join(OUT_DIR,'coadd/legacysurvey-1741p242-image.jpg'),
jpeg=True)
model_jpg= readImage(os.path.join(OUT_DIR,'coadd/legacysurvey-1741p242-model.jpg'),
jpeg=True)
resid_jpg= readImage(os.path.join(OUT_DIR,'coadd/legacysurvey-1741p242-resid.jpg'),
jpeg=True)
In [6]:
len(simcat),len(obitractor),blobs.shape
Out[6]:
In [7]:
fig,ax=plt.subplots(2,2,figsize=(6,6))
plotImage().imshow(blobs,ax[0,0],qs=None)
plotImage().imshow(img_jpg,ax[0,1],qs=None)
plotImage().imshow(model_jpg,ax[1,0],qs=None)
plotImage().imshow(resid_jpg,ax[1,1],qs=None)
In [8]:
OUT_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'out_testcase_DR5_z_allblobs/elg/174/1741p242/rs0/')
IN_DIR= os.path.join(os.environ['HOME'],
'myrepo/obiwan/tests/end_to_end',
'testcase_DR5_z_allblobs/')
simcat= fits_table(os.path.join(OUT_DIR,'obiwan/simcat-elg-1741p242.fits'))
obitractor= fits_table(os.path.join(OUT_DIR,'tractor/tractor-1741p242.fits'))
blobs= fitsio.FITS(os.path.join(OUT_DIR,'metrics/blobs-1741p242.fits.gz'))[0].read()
img_jpg= readImage(os.path.join(OUT_DIR,'coadd/legacysurvey-1741p242-image.jpg'),
jpeg=True)
model_jpg= readImage(os.path.join(OUT_DIR,'coadd/legacysurvey-1741p242-model.jpg'),
jpeg=True)
resid_jpg= readImage(os.path.join(OUT_DIR,'coadd/legacysurvey-1741p242-resid.jpg'),
jpeg=True)
In [9]:
len(simcat),len(obitractor),blobs.shape
Out[9]:
In [10]:
fig,ax=plt.subplots(2,2,figsize=(6,6))
plotImage().imshow(blobs,ax[0,0],qs=None)
plotImage().imshow(img_jpg,ax[0,1],qs=None)
plotImage().imshow(model_jpg,ax[1,0],qs=None)
plotImage().imshow(resid_jpg,ax[1,1],qs=None)