<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from numpy import * <EOL> import utils_mlz <EOL> import os , sys <EOL> from scipy . stats import mode <EOL> import pyfits as pf <EOL> import datetime <EOL> def get_zbins ( Pars ) : <EOL> Nbins = int ( Pars . nzbins ) <EOL> zfine = linspace ( Pars . minz , Pars . maxz , Nbins + <NUM_LIT:1> ) <EOL> resz = zfine [ <NUM_LIT:1> ] - zfine [ <NUM_LIT:0> ] <EOL> resz2 = resz / <NUM_LIT:1.> <EOL> zfine2 = arange ( Pars . minz - resz2 * <NUM_LIT> - resz2 / <NUM_LIT> , Pars . maxz + resz2 * <NUM_LIT> , resz2 ) <EOL> wzin = where ( ( zfine2 >= Pars . minz ) & ( zfine2 <= Pars . maxz ) ) [ <NUM_LIT:0> ] <EOL> return zfine , zfine2 , resz , resz2 , wzin <EOL> class GetPz ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict_zp , nobj , Pars ) : <EOL> zfine , zfine2 , resz , resz2 , wzin = get_zbins ( Pars ) <EOL> self . nD = nobj <EOL> self . Pars = Pars <EOL> self . Nbins = int ( self . Pars . nzbins ) <EOL> self . zfine = zfine <EOL> self . resz = resz <EOL> self . zbins = ( self . zfine [ <NUM_LIT:1> : ] + self . zfine [ : - <NUM_LIT:1> ] ) * <NUM_LIT:0.5> <EOL> self . resolution = resz2 <EOL> self . zfine2 = zfine2 <EOL> self . wzin = wzin <EOL> self . sigma_g = self . Pars . sigmafactor * self . resolution <EOL> self . dz = self . resolution <EOL> self . x = arange ( - <NUM_LIT> * self . sigma_g - self . dz / <NUM_LIT> , <NUM_LIT> * self . sigma_g + self . dz / <NUM_LIT> , self . dz ) <EOL> self . gaus2 = exp ( - <NUM_LIT:0.5> * ( self . x / self . sigma_g ) ** <NUM_LIT:2> ) <EOL> self . zp = zeros ( self . nD ) - <NUM_LIT:1.> <EOL> self . zs = zeros ( self . nD ) <EOL> self . dict_zp = dict_zp <EOL> self . fac = zeros ( self . nD ) <EOL> self . bigpdf = zeros ( len ( self . zbins ) ) <EOL> self . bigpdf2 = zeros ( ( self . nD , len ( self . zfine2 [ self . wzin ] ) ) ) <EOL> self . zs0 = zeros ( self . nD ) <EOL> self . zs1 = zeros ( self . nD ) <EOL> self . err0 = zeros ( self . nD ) <EOL> self . err1 = zeros ( self . nD ) <EOL> self . zConf0 = zeros ( self . nD ) <EOL> self . zConf1 = zeros ( self . nD ) <EOL> def compute ( self , do_pdf = '<STR_LIT:yes>' ) : <EOL> for i in xrange ( self . nD ) : <EOL> self . bigpdf = zeros ( len ( self . zbins ) ) <EOL> if self . dict_zp . has_key ( i ) : <EOL> out = array ( self . dict_zp [ i ] [ '<STR_LIT>' ] ) <EOL> if self . dict_zp [ i ] . has_key ( '<STR_LIT>' ) : self . zs [ i ] = self . dict_zp [ i ] [ '<STR_LIT>' ] <EOL> if self . Pars . predictionclass == '<STR_LIT>' : <EOL> for zpi in xrange ( len ( out ) ) : <EOL> mybin = int ( floor ( out [ zpi ] / self . resz ) ) <EOL> if mybin > self . Nbins - <NUM_LIT:1> : continue <EOL> self . bigpdf [ mybin ] += <NUM_LIT:1.> <EOL> pdf = self . bigpdf <EOL> pdf2 = interp ( self . zfine2 , self . zbins , pdf ) <EOL> pdf2 = where ( greater ( pdf2 , max ( pdf2 ) * <NUM_LIT> ) , pdf2 , <NUM_LIT:0.> ) <EOL> pdf2 = convolve ( pdf2 , self . gaus2 , <NUM_LIT:1> ) <EOL> pdf2 = where ( greater ( pdf2 , max ( pdf2 ) * <NUM_LIT> ) , pdf2 , <NUM_LIT:0.> ) <EOL> if sum ( pdf2 ) > <NUM_LIT:0.> : pdf2 /= sum ( pdf2 ) <EOL> self . zs0 [ i ] = self . zfine2 [ argmax ( pdf2 ) ] <EOL> self . zs0 [ i ] = min ( self . zs0 [ i ] , self . Pars . maxz ) <EOL> self . zs0 [ i ] = max ( self . zs0 [ i ] , self . Pars . minz ) <EOL> self . zs1 [ i ] = sum ( self . zfine2 * pdf2 ) <EOL> self . zs1 [ i ] = min ( self . zs1 [ i ] , self . Pars . maxz ) <EOL> self . zs1 [ i ] = max ( self . zs1 [ i ] , self . Pars . minz ) <EOL> if do_pdf == '<STR_LIT:yes>' : <EOL> self . err0 [ i ] = utils_mlz . compute_error ( self . zfine2 , pdf2 , self . zs0 [ i ] ) <EOL> self . err1 [ i ] = utils_mlz . compute_error ( self . zfine2 , pdf2 , self . zs1 [ i ] ) <EOL> self . zConf0 [ i ] = utils_mlz . compute_zConf ( self . zfine2 , pdf2 , self . zs0 [ i ] , self . Pars . rmsfactor ) <EOL> self . zConf1 [ i ] = utils_mlz . compute_zConf ( self . zfine2 , pdf2 , self . zs1 [ i ] , self . Pars . rmsfactor ) <EOL> pdf2 = pdf2 [ self . wzin ] <EOL> if sum ( pdf2 ) > <NUM_LIT:0.> : pdf2 /= sum ( pdf2 ) <EOL> self . bigpdf2 [ i , : ] = pdf2 <EOL> if self . Pars . predictionclass == '<STR_LIT>' : <EOL> if len ( out ) > <NUM_LIT:0> : <EOL> self . zs0 [ i ] = mode ( out * <NUM_LIT:1.> ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . zs1 [ i ] = mean ( out * <NUM_LIT:1.> ) <EOL> if len ( out ) > <NUM_LIT:0.> : self . err0 [ i ] = mode ( out * <NUM_LIT:1.> ) [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] * <NUM_LIT:1.> / ( len ( out ) * <NUM_LIT:1.> ) <EOL> self . err1 [ i ] = std ( out * <NUM_LIT:1.> ) <EOL> bigZ = zeros ( ( self . nD , <NUM_LIT:7> ) ) <EOL> bigZ [ : , <NUM_LIT:0> ] = self . zs <EOL> bigZ [ : , <NUM_LIT:1> ] = self . zs0 <EOL> bigZ [ : , <NUM_LIT:2> ] = self . zs1 <EOL> bigZ [ : , <NUM_LIT:3> ] = self . zConf0 <EOL> bigZ [ : , <NUM_LIT:4> ] = self . zConf1 <EOL> bigZ [ : , <NUM_LIT:5> ] = self . err0 <EOL> bigZ [ : , <NUM_LIT:6> ] = self . err1 <EOL> if do_pdf == '<STR_LIT>' : <EOL> return bigZ <EOL> else : <EOL> return bigZ , self . bigpdf2 <EOL> class GetPz_short ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , Pars ) : <EOL> zfine , zfine2 , resz , resz2 , wzin = get_zbins ( Pars ) <EOL> self . Pars = Pars <EOL> self . Nbins = int ( self . Pars . nzbins ) <EOL> self . zfine = zfine <EOL> self . resz = resz <EOL> self . zbins = ( self . zfine [ <NUM_LIT:1> : ] + self . zfine [ : - <NUM_LIT:1> ] ) * <NUM_LIT:0.5> <EOL> self . resolution = resz2 <EOL> self . zfine2 = zfine2 <EOL> self . wzin = wzin <EOL> self . sigma_g = self . Pars . sigmafactor * self . resolution <EOL> self . dz = self . resolution <EOL> self . x = arange ( - <NUM_LIT> * self . sigma_g - self . dz / <NUM_LIT> , <NUM_LIT> * self . sigma_g + self . dz / <NUM_LIT> , self . dz ) <EOL> self . gaus2 = exp ( - <NUM_LIT:0.5> * ( self . x / self . sigma_g ) ** <NUM_LIT:2> ) <EOL> self . bigpdf = zeros ( len ( self . zbins ) ) <EOL> self . bigpdf2 = zeros ( len ( self . zfine2 [ self . wzin ] ) ) <EOL> def get_hist ( self , vals ) : <EOL> self . bigpdf = zeros ( len ( self . zbins ) ) <EOL> out = array ( vals ) <EOL> if self . Pars . predictionclass == '<STR_LIT>' : <EOL> for zpi in xrange ( len ( out ) ) : <EOL> mybin = int ( floor ( out [ zpi ] / self . resz ) ) <EOL> if mybin > self . Nbins - <NUM_LIT:1> : continue <EOL> self . bigpdf [ mybin ] += <NUM_LIT:1> <EOL> return self . bigpdf <EOL> def get_pdf ( self , rawpdf , zs = <NUM_LIT:0> , zbins = '<STR_LIT>' ) : <EOL> pdf = rawpdf <EOL> self . zs = zs <EOL> if zbins != '<STR_LIT>' : self . zbins = zbins <EOL> pdf2 = interp ( self . zfine2 , self . zbins , pdf ) <EOL> pdf2 = where ( greater ( pdf2 , max ( pdf2 ) * <NUM_LIT> ) , pdf2 , <NUM_LIT:0.> ) <EOL> pdf2 = convolve ( pdf2 , self . gaus2 , <NUM_LIT:1> ) <EOL> pdf2 = where ( greater ( pdf2 , max ( pdf2 ) * <NUM_LIT> ) , pdf2 , <NUM_LIT:0.> ) <EOL> if sum ( pdf2 ) > <NUM_LIT:0.> : pdf2 /= sum ( pdf2 ) <EOL> self . zs0 = self . zfine2 [ argmax ( pdf2 ) ] <EOL> self . zs0 = min ( self . zs0 , self . Pars . maxz ) <EOL> self . zs0 = max ( self . zs0 , self . Pars . minz ) <EOL> self . zs1 = sum ( self . zfine2 * pdf2 ) <EOL> self . zs1 = min ( self . zs1 , self . Pars . maxz ) <EOL> self . zs1 = max ( self . zs1 , self . Pars . minz ) <EOL> self . err0 = utils_mlz . compute_error3 ( self . zfine2 , pdf2 , self . zs0 ) <EOL> self . err1 = utils_mlz . compute_error3 ( self . zfine2 , pdf2 , self . zs1 ) <EOL> self . zConf0 = utils_mlz . compute_zConf2 ( self . zfine2 , pdf2 , self . zs0 , self . Pars . rmsfactor ) <EOL> self . zConf1 = utils_mlz . compute_zConf2 ( self . zfine2 , pdf2 , self . zs1 , self . Pars . rmsfactor ) <EOL> pdf2 = pdf2 [ self . wzin ] <EOL> if sum ( pdf2 ) > <NUM_LIT:0.> : pdf2 /= sum ( pdf2 ) <EOL> self . bigpdf2 = pdf2 <EOL> bigZ = zeros ( <NUM_LIT:7> ) <EOL> bigZ [ <NUM_LIT:0> ] = self . zs <EOL> bigZ [ <NUM_LIT:1> ] = self . zs0 <EOL> bigZ [ <NUM_LIT:2> ] = self . zs1 <EOL> bigZ [ <NUM_LIT:3> ] = self . zConf0 <EOL> bigZ [ <NUM_LIT:4> ] = self . zConf1 <EOL> bigZ [ <NUM_LIT:5> ] = self . err0 <EOL> bigZ [ <NUM_LIT:6> ] = self . err1 <EOL> return bigZ , self . bigpdf2 <EOL> def class_stat ( s1 , s2 , nv , Pars ) : <EOL> z1 = s1 / nv <EOL> midp = <NUM_LIT:0.5> * ( Pars . minz + Pars . maxz ) <EOL> z0 = where ( z1 >= midp , Pars . maxz , Pars . minz ) <EOL> s0 = sqrt ( ( s2 - nv * z1 * z1 ) / nv ) <EOL> return z0 , z1 , s0 <EOL> def save_single ( Zall , Pars , path = '<STR_LIT>' , fileout = '<STR_LIT>' , oob = '<STR_LIT>' , var = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> ztrue = Zall [ : , <NUM_LIT:0> ] <EOL> zs0 = Zall [ : , <NUM_LIT:1> ] <EOL> zs1 = Zall [ : , <NUM_LIT:2> ] <EOL> zC0 = Zall [ : , <NUM_LIT:3> ] <EOL> zC1 = Zall [ : , <NUM_LIT:4> ] <EOL> e0 = Zall [ : , <NUM_LIT:5> ] <EOL> e1 = Zall [ : , <NUM_LIT:6> ] <EOL> if path == '<STR_LIT>' : path = Pars . path_results <EOL> if not os . path . exists ( path ) : os . system ( '<STR_LIT>' + path ) <EOL> if fileout == '<STR_LIT>' : <EOL> filebase = Pars . finalfilename <EOL> for j in xrange ( <NUM_LIT:100> ) : <EOL> if os . path . exists ( path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) : <EOL> continue <EOL> else : <EOL> if oob == '<STR_LIT>' : fileout = path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> if oob == '<STR_LIT:yes>' : fileout = path + filebase + '<STR_LIT>' + var + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> fileinputs = path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> break <EOL> else : <EOL> filebase = fileout <EOL> if oob == '<STR_LIT>' : fileout = path + filebase + '<STR_LIT>' <EOL> if oob == '<STR_LIT:yes>' : fileout = path + filebase + '<STR_LIT>' + '<STR_LIT>' <EOL> fileinputs = path + filebase + '<STR_LIT>' <EOL> if oob == '<STR_LIT>' : utils_mlz . print_dtpars ( Pars , fileinputs ) <EOL> fout = open ( fileout , '<STR_LIT:w>' ) <EOL> header = '<STR_LIT>' <EOL> fout . write ( header ) <EOL> for i in xrange ( len ( zs0 ) ) : <EOL> Line = '<STR_LIT>' . format ( ztrue [ i ] , zs0 [ i ] , zs1 [ i ] , zC0 [ i ] , <EOL> zC1 [ i ] , e0 [ i ] , e1 [ i ] ) <EOL> fout . write ( Line + '<STR_LIT:\n>' ) <EOL> fout . close ( ) <EOL> def get_path_new ( Pars ) : <EOL> path = Pars . path_results <EOL> if not os . path . exists ( path ) : os . system ( '<STR_LIT>' + path ) <EOL> filebase = Pars . finalfilename <EOL> for j in xrange ( <NUM_LIT:100> ) : <EOL> if os . path . exists ( path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) : <EOL> continue <EOL> else : <EOL> break <EOL> return path , filebase , j <EOL> def save_PDF ( zfine , pdfs , Pars , path = '<STR_LIT>' , filebase = '<STR_LIT>' , num = - <NUM_LIT:1> , oob = '<STR_LIT>' , var = '<STR_LIT>' , multiple = '<STR_LIT>' , rank = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if path == '<STR_LIT>' : <EOL> path = Pars . path_results <EOL> if not os . path . exists ( path ) : os . system ( '<STR_LIT>' + path ) <EOL> if filebase == '<STR_LIT>' : <EOL> filebase = Pars . finalfilename <EOL> if num == - <NUM_LIT:1> : <EOL> for j in xrange ( <NUM_LIT:100> ) : <EOL> if os . path . exists ( path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) and os . path . exists ( <EOL> path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) : <EOL> continue <EOL> else : <EOL> fileoutPDF = path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> if oob == '<STR_LIT:yes>' : fileoutPDF = path + filebase + '<STR_LIT>' + var + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> break <EOL> else : <EOL> fileoutPDF = path + filebase + '<STR_LIT:.>' + str ( num ) + '<STR_LIT>' <EOL> if oob == '<STR_LIT:yes>' : fileoutPDF = path + filebase + '<STR_LIT>' + var + '<STR_LIT:.>' + str ( num ) + '<STR_LIT>' <EOL> if multiple == '<STR_LIT:yes>' : fileoutPDF = fileoutPDF + '<STR_LIT:_>' + str ( rank ) <EOL> pdfs = concatenate ( ( pdfs , [ zfine ] ) ) <EOL> if Pars . writefits == '<STR_LIT>' : <EOL> save ( fileoutPDF , pdfs ) <EOL> else : <EOL> head = pf . Header ( ) <EOL> head [ '<STR_LIT>' ] = len ( pdfs ) - <NUM_LIT:1> <EOL> head [ '<STR_LIT>' ] = zfine [ <NUM_LIT:1> ] - zfine [ <NUM_LIT:0> ] <EOL> head [ '<STR_LIT>' ] = len ( zfine ) <EOL> head [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> head [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> head [ '<STR_LIT>' ] = '<STR_LIT>' + datetime . datetime . now ( ) . strftime ( "<STR_LIT>" ) <EOL> fmt = '<STR_LIT>' % len ( zfine ) <EOL> col0 = pf . Column ( name = '<STR_LIT>' , format = fmt , array = pdfs ) <EOL> table0 = pf . BinTableHDU . from_columns ( pf . ColDefs ( [ col0 ] ) ) <EOL> prihdu = pf . PrimaryHDU ( header = head ) <EOL> hdulist = pf . HDUList ( [ prihdu , table0 ] ) <EOL> hdulist . writeto ( fileoutPDF + '<STR_LIT>' , clobber = True ) <EOL> def save_PDF_sparse ( zfine , pdfs , head , Pars , path = '<STR_LIT>' , filebase = '<STR_LIT>' , num = - <NUM_LIT:1> , oob = '<STR_LIT>' , var = '<STR_LIT>' , multiple = '<STR_LIT>' , rank = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if path == '<STR_LIT>' : <EOL> path = Pars . path_results <EOL> if not os . path . exists ( path ) : os . system ( '<STR_LIT>' + path ) <EOL> if filebase == '<STR_LIT>' : <EOL> filebase = Pars . finalfilename <EOL> if num == - <NUM_LIT:1> : <EOL> for j in xrange ( <NUM_LIT:100> ) : <EOL> if os . path . exists ( path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) and os . path . exists ( <EOL> path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) : <EOL> continue <EOL> else : <EOL> fileoutPDF = path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> if oob == '<STR_LIT:yes>' : fileoutPDF = path + filebase + '<STR_LIT>' + var + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> break <EOL> else : <EOL> fileoutPDF = path + filebase + '<STR_LIT:.>' + str ( num ) + '<STR_LIT>' <EOL> if oob == '<STR_LIT:yes>' : fileoutPDF = path + filebase + '<STR_LIT>' + var + '<STR_LIT:.>' + str ( num ) + '<STR_LIT>' <EOL> if multiple == '<STR_LIT:yes>' : fileoutPDF = fileoutPDF + '<STR_LIT:_>' + str ( rank ) <EOL> col1 = pf . Column ( name = '<STR_LIT>' , format = '<STR_LIT:E>' , array = zfine ) <EOL> fmt = '<STR_LIT>' % head [ '<STR_LIT>' ] <EOL> col2 = pf . Column ( name = '<STR_LIT>' , format = fmt , array = pdfs ) <EOL> table1 = pf . BinTableHDU . from_columns ( pf . ColDefs ( [ col1 ] ) ) <EOL> table2 = pf . BinTableHDU . from_columns ( pf . ColDefs ( [ col2 ] ) ) <EOL> prihdu = pf . PrimaryHDU ( header = head ) <EOL> hdulist = pf . HDUList ( [ prihdu , table1 , table2 ] ) <EOL> hdulist . writeto ( fileoutPDF + '<STR_LIT>' , clobber = True ) <EOL> def save_single_t ( Zall , Pars , path = '<STR_LIT>' , fileout = '<STR_LIT>' , oob = '<STR_LIT>' , var = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> type_phot = Zall [ : , <NUM_LIT:0> ] <EOL> type_ml = Zall [ : , <NUM_LIT:1> ] <EOL> if path == '<STR_LIT>' : path = Pars . path_results <EOL> if not os . path . exists ( path ) : os . system ( '<STR_LIT>' + path ) <EOL> if fileout == '<STR_LIT>' : filebase = Pars . finalfilename <EOL> for j in xrange ( <NUM_LIT:100> ) : <EOL> if os . path . exists ( path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' ) : <EOL> continue <EOL> else : <EOL> if oob == '<STR_LIT>' : fileout = path + filebase + '<STR_LIT:.>' + str ( j ) + '<STR_LIT>' <EOL> break <EOL> fout = open ( fileout , '<STR_LIT:w>' ) <EOL> header = '<STR_LIT>' <EOL> fout . write ( header ) <EOL> for i in xrange ( len ( type_phot ) ) : <EOL> Line = '<STR_LIT>' . format ( type_phot [ i ] , type_ml [ i ] ) <EOL> fout . write ( Line + '<STR_LIT:\n>' ) <EOL> fout . close ( ) </s>
