<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Comparing 2 Stacks of Catalogues
Environment
Step1:  Need more packages?
Step2:  Main()
Run legacy-zeropoints-qa.py like this "python legacy-zeropoints-qa.py" to analyze everything. 
See below to walk through it step by step.
Step3:  MZLS v2, v3 tractor cats
Step4:  Cosmos comparison
<ASSISTANT_TASK:>
Python Code:
import sys
print sys.executable
# Hack!, this avoids messing with NERSC's config file for jupyter hub
sys.path.append('/global/homes/k/kaylanb/repos/astrometry.net')
sys.path.append('/global/homes/k/kaylanb/repos/tractor')
sys.path
print sys.path
Explanation: Comparing 2 Stacks of Catalogues
Environment: anaconda-2.7
End of explanation
# Easy if pip, conda installable
#!/anaconda2/bin/pip install ...
#!/anaconda2/bin/conda install ...
Explanation: Need more packages?
End of explanation
%matplotlib inline
%load_ext autoreload
%autoreload 2
import numpy as np
import matplotlib.pyplot as plt
import fitsio
import glob
import os
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
from astropy import units
from astropy.coordinates import SkyCoord
from astrometry.util.fits import fits_table, merge_tables
from tractor.brightness import NanoMaggies
import catalogues as cat
Explanation: Main()
Run legacy-zeropoints-qa.py like this "python legacy-zeropoints-qa.py" to analyze everything. 
See below to walk through it step by step.
End of explanation
cut= np.all((np.all(v2.get('decam_nobs')[:,[4]] == 1,axis=1),\
             ),axis=0)
v2.get('decam_nobs')[:,4][cut]
np.where(v2.get('decam_nobs')[:,4] == 1)
fits_funcs= cat.CatalogueFuncs()
v2=fits_funcs.stack('v2_cats.txt')
v3=fits_funcs.stack('v3_cats.txt')
cut= np.all((np.all(v2.get('decam_nobs')[:,[4]] == 1,axis=1),\
             ),axis=0)
v2.cut(cut)
cut= np.all((np.all(v3.get('decam_nobs')[:,[4]] == 1,axis=1),\
             ),axis=0)
v3.cut(cut)
mat=cat.Matcher()
imatch,imiss,d2d= mat.match_within(v2,v3) #,dist=1./3600)
fits_funcs.set_mags(v2)
fits_funcs.set_mags(v3)
import plots
k=plots.Kaylans(v2,v3,imatch,\
                ref_name='v2',obs_name='v3',savefig=True)
d=plots.Dustins(v2,v3,imatch,d2d,\
             ref_name='v2',obs_name='v3',savefig=True)
import plots
d=plots.Dustins(v2,v3,imatch,d2d,plot_all=False)
mat.match_within(self,ref,obs,dist=1./3600)
# Take v2 and shift everything by 1/3 pix in dec
fits_funcs= cat.CatalogueFuncs()
v4=fits_funcs.stack('v2_cats.txt')
third_pix= (1./3)*0.262/3600
v4.set('dec',v4.get('dec')+third_pix)
fits_funcs.set_extra_data(v4)
mat=cat.Matcher()
imatch,imiss,d2d= mat.match_within(v2,v4) #,dist=1./3600)
a=plt.hist(d2d * 3600., 100,range=(0.01,0.2))
import plots
k=plots.Kaylans(v2,v3,imatch,plot_all=False)
for mytype in ['PSF','SIMP','DEV','COMP']:
    k.stacked_confusion_matrix(v2[ imatch['ref'] ],v3[ imatch['obs'] ],\
                               ref_name='v2',obs_name='v3',savefig=True,\
                               band='z',mytype=mytype)
import plots
k=plots.Kaylans(v2,v3,imatch,plot_all=False)
# k.barchart(v2,v3,\
#            ref_name='v2',obs_name='v3',savefig=True,prefix='alldata')
# k.barchart(v2[ imatch['ref']],v3[imatch['obs']],\
#            ref_name='v2',obs_name='v3',savefig=True,prefix='matchedonly')
k.delta_mag_vs_mag(v2[ imatch['ref']],v3[imatch['obs']],\
            ref_name='v2',obs_name='v3',savefig=True,ylim=[-0.2,0.2])
import plots
d=plots.Dustins(v2,v3,imatch,d2d,plot_all=False)
d.match_distance(d2d,range=(0,0.2),prefix='',savefig=True)
# imatch_2,imiss_2,d2d_2= mat.match_within(v3,v2)
# d.match_/distance(d2d_2,range=(0,0.2),savefig=True,prefix='2_')
import catalogues
mat=catalogues.Matcher()
d={}
for key in ['v2_auto','v3_auto','v2v3_cross']:
    d[key]={}
    if key == 'v2v3_cross':
        tup= mat.nearest_neighbors_within(v2,v3,within=1./3600,min_nn=1,max_nn=3)
    elif key == 'v2_auto': 
        tup= mat.nearest_neighbors_within(v2,v2,within=1./3600,min_nn=2,max_nn=4)
    elif key == 'v3_auto': 
        tup= mat.nearest_neighbors_within(v3,v3,within=1./3600,min_nn=2,max_nn=4)
    d[key]['ref'],d[key]['obs'],d[key]['dist']= tup
d[key]['dist']
key='v2v3_cross'
dists={}
for cnt,nn in enumerate(np.sort(d[key]['ref'].keys())):
    if cnt == 0:
        dists[nn]= d[key]['dist'][nn]
    else:
        dists[nn]= np.concatenate((d[key]['dist'][nn], dists[str(int(nn)-1)]))
    print('nn=%s, len(dists[nn])=%d' % (nn,len(dists[nn])))
#     if str(int(nn)-1) in d[key]['ref'].keys():
#     distsplt.hist(d[key]['dist'][nn]*3600,20,range=(0.01,0.1),normed=True)
print dists['1']
np.histogram(dists['1']*3600,bins=bins,normed=True)
key='v2v3_cross'
bins=np.linspace(0.01,0.1,num=20)
print bins
for nn in np.sort(d[key]['ref'].keys())[::-1]:
    print nn
    hist,bj= np.histogram(dists[nn]*3600,bins=bins,normed=True)
    binc= (bins[1:]+bins[:-1])/2
    plt.step(binc,hist, where='mid')
    print binc,hist
plt.xlim((bins[0],bins[-1]))
plt.ylim()
#     plt.hist(dists[nn]*3600,20,range=(0.01,0.1),normed=True)
fig,ax=plt.subplots(1,3) #,figsize=(4,8),sharex=True)
plt.subplots_adjust(hspace=0)
# for cnt,key in zip(range(3),['v2_auto','v3_auto','v2v3_cross']):
cnt=0
key='v2_auto'
for nn in np.sort(d[key]['ref'].keys()):
    ax[cnt].hist(d[key]['dist'][nn]*3600,100,range=(0,0.1),label='nn='+nn,\
                normed=True)
ax[cnt].legend(loc='upper right')
#     hist[band],bins= np.histogram(chi[band][imag],\
#                             range=(low,hi),bins=50,normed=True)
#     db= (bins[1:]-bins[:-1])/2
#     binc[band]= bins[:-1]+db
#     ax[cnt].step(binc[band],hist[band], where='mid',c='b',lw=2) #label="%.1f < mag < %.1f" % (b_low,b_hi))
d.match_distance(d2d,range=(0,0.15),savefig=False)
k.
Explanation: MZLS v2, v3 tractor cats
End of explanation
fits_funcs= cat.CatalogueFuncs()
c40=fits_funcs.stack('cosmos_40_tractor_list.txt')
c41=fits_funcs.stack('cosmos_41_tractor_list.txt')
mat=cat.Matcher()
cmatch,imiss,c_d2d= mat.match_within(c40,c41) #,dist=1./3600)
fits_funcs.set_extra_data(c40)
fits_funcs.set_extra_data(c41)
import plots
e=plots.EnriqueCosmos(c40,c41,cmatch,ref_name='40',obs_name='41',savefig=False)
import plots
Explanation: Cosmos comparison
End of explanation
<END_TASK>