<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from numpy import * <EOL> import os , sys <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . cm as cm <EOL> import matplotlib . gridspec as gridspec <EOL> import random as rn <EOL> import pyfits as pf <EOL> path_src = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> if not path_src in sys . path : sys . path . insert ( <NUM_LIT:1> , path_src ) <EOL> from ml_codes import * <EOL> from utils import * <EOL> class Qplot ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inputs_file ) : <EOL> utils_mlz . print_welcome ( ) <EOL> Pars_in = utils_mlz . read_dt_pars ( inputs_file , verbose = True ) <EOL> self . Pars = Pars_in <EOL> self . inputs_file = inputs_file <EOL> def plot_tree ( self , ntree = <NUM_LIT:0> , save_files = '<STR_LIT>' , fileroot = '<STR_LIT>' , path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> path_tree = self . Pars . path_output_trees <EOL> fileT = self . Pars . treefilename + '<STR_LIT>' % ntree <EOL> utils_mlz . printpz ( '<STR_LIT>' , fileT ) <EOL> T = load ( path_tree + fileT ) <EOL> T = T . item ( ) <EOL> if T . dict_dim == '<STR_LIT:all>' : <EOL> dd = { } <EOL> for i in xrange ( len ( self . Pars . att ) ) : <EOL> dd [ self . Pars . att [ i ] ] = { '<STR_LIT>' : i } <EOL> T . dict_dim = dd <EOL> T . plot_tree ( save_png = save_files , fileout = fileroot , path = path ) <EOL> def plot_map ( self , nmap = <NUM_LIT:0> , colbar = '<STR_LIT:yes>' , min_m = - <NUM_LIT:100> , max_m = - <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> path_map = self . Pars . path_output_maps <EOL> fileM = self . Pars . somfilename + '<STR_LIT>' % nmap <EOL> utils_mlz . printpz ( '<STR_LIT>' , fileM ) <EOL> M = load ( path_map + fileM ) <EOL> M = M . item ( ) <EOL> M . plot_map ( colbar = colbar , min_m = min_m , max_m = max_m ) <EOL> def plot_importance ( self , result_id = <NUM_LIT:0> , Nzb = <NUM_LIT:10> , list_att = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> filenum = str ( result_id ) <EOL> froot = self . Pars . path_results + self . Pars . finalfilename + '<STR_LIT>' <EOL> zs , za , zb , oa , ob , ea , eb = loadtxt ( froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' , unpack = True ) <EOL> fdz = lambda x , y : ( abs ( x - y ) ) / ( <NUM_LIT:1.> + y ) <EOL> O_all = utils_mlz . bias ( zs , zb , '<STR_LIT>' , self . Pars . minz , self . Pars . maxz , <NUM_LIT:1> , <EOL> mode = <NUM_LIT:0> , d_z = fdz , verb = False ) <EOL> ki = arange ( len ( self . Pars . att ) ) <EOL> Im0 = zeros ( len ( ki ) ) <EOL> Im1 = zeros ( len ( ki ) ) <EOL> keys = [ ] <EOL> utils_mlz . printpz ( "<STR_LIT>" ) <EOL> utils_mlz . printpz ( "<STR_LIT>" ) <EOL> for k in xrange ( len ( self . Pars . att ) ) : <EOL> kk = self . Pars . att [ k ] <EOL> keys . append ( kk ) <EOL> filev = froot + '<STR_LIT:_>' + kk + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> zs , za , zb , oa , ob , ea , eb = loadtxt ( filev , unpack = True ) <EOL> O_temp = utils_mlz . bias ( zs , zb , '<STR_LIT>' , self . Pars . minz , self . Pars . maxz , <NUM_LIT:1> , <EOL> mode = <NUM_LIT:0> , d_z = fdz , verb = False ) <EOL> Im0 [ k ] = O_temp . mean / O_all . mean <EOL> Im1 [ k ] = O_temp . sigma / O_all . sigma <EOL> del O_temp <EOL> imp = '<STR_LIT>' % Im1 [ k ] <EOL> utils_mlz . printpz ( kk , '<STR_LIT:U+0020>' , imp ) <EOL> fimportance = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> FF = open ( fimportance , '<STR_LIT:w>' ) <EOL> linew = '<STR_LIT:U+002C>' . join ( keys ) <EOL> linew = '<STR_LIT:#>' + linew + '<STR_LIT:\n>' <EOL> FF . write ( linew ) <EOL> for i in xrange ( len ( ki ) ) : <EOL> linew = '<STR_LIT>' % Im0 [ i ] <EOL> FF . write ( linew ) <EOL> FF . close ( ) <EOL> utils_mlz . printpz ( ) <EOL> utils_mlz . printpz ( "<STR_LIT>" , fimportance ) <EOL> Im = ( Im0 + Im1 ) / <NUM_LIT> <EOL> sk = argsort ( Im0 ) <EOL> sk = sk [ : : - <NUM_LIT:1> ] <EOL> fig , ax = plt . subplots ( ) <EOL> ax . plot ( ki , Im0 [ sk ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> ax . plot ( ki , Im1 [ sk ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> ax . plot ( ki , Im [ sk ] , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> ax . set_xlabel ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> ax . set_ylabel ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> plt . legend ( loc = <NUM_LIT:0> ) <EOL> ax . set_xticks ( ki ) <EOL> ax . set_xticklabels ( array ( keys ) [ sk ] , rotation = <NUM_LIT> , ha = '<STR_LIT:right>' , fontsize = <NUM_LIT:12> ) <EOL> ax . set_xlim ( - <NUM_LIT:1> , len ( ki ) ) <EOL> zs , za , zb , oa , ob , ea , eb = loadtxt ( froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' , unpack = True ) <EOL> O_all = utils_mlz . bias ( zs , zb , '<STR_LIT>' , self . Pars . minz , self . Pars . maxz , Nzb , <EOL> mode = <NUM_LIT:1> , d_z = fdz , verb = False ) <EOL> ki = arange ( len ( self . Pars . att ) ) <EOL> Im0 = zeros ( ( Nzb , len ( ki ) ) ) <EOL> Im1 = zeros ( ( Nzb , len ( ki ) ) ) <EOL> keys = [ ] <EOL> for k in xrange ( len ( self . Pars . att ) ) : <EOL> kk = self . Pars . att [ k ] <EOL> keys . append ( kk ) <EOL> filev = froot + '<STR_LIT:_>' + kk + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> zs , za , zb , oa , ob , ea , eb = loadtxt ( filev , unpack = True ) <EOL> O_temp = utils_mlz . bias ( zs , zb , '<STR_LIT>' , self . Pars . minz , self . Pars . maxz , Nzb , <EOL> mode = <NUM_LIT:1> , d_z = fdz , verb = False ) <EOL> Im0 [ : , k ] = O_temp . mean / O_all . mean <EOL> Im1 [ : , k ] = O_temp . sigma / O_all . sigma <EOL> del O_temp <EOL> fig2 , ax2 = plt . subplots ( ) <EOL> if list_att == '<STR_LIT>' : <EOL> ax2 . plot ( O_all . bins , ( Im0 [ : , sk [ <NUM_LIT:0> ] ] + Im1 [ : , sk [ <NUM_LIT:0> ] ] ) / <NUM_LIT> , '<STR_LIT>' , label = array ( keys ) [ sk [ <NUM_LIT:0> ] ] ) <EOL> ax2 . plot ( O_all . bins , ( Im0 [ : , sk [ <NUM_LIT:1> ] ] + Im1 [ : , sk [ <NUM_LIT:1> ] ] ) / <NUM_LIT> , '<STR_LIT>' , label = array ( keys ) [ sk [ <NUM_LIT:1> ] ] ) <EOL> ax2 . plot ( O_all . bins , ( Im0 [ : , sk [ - <NUM_LIT:1> ] ] + Im1 [ : , sk [ - <NUM_LIT:1> ] ] ) / <NUM_LIT> , '<STR_LIT>' , label = array ( keys ) [ sk [ - <NUM_LIT:1> ] ] ) <EOL> else : <EOL> for katt in list_att : <EOL> wk = where ( array ( keys ) == katt ) [ <NUM_LIT:0> ] <EOL> ax2 . plot ( O_all . bins , ( Im0 [ : , wk [ <NUM_LIT:0> ] ] + Im1 [ : , wk [ <NUM_LIT:0> ] ] ) / <NUM_LIT> , '<STR_LIT>' , label = array ( keys ) [ wk [ <NUM_LIT:0> ] ] ) <EOL> ax2 . set_xlabel ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> ax2 . set_ylabel ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> plt . legend ( loc = <NUM_LIT:0> ) <EOL> plt . show ( ) <EOL> def plot_results ( self , result_1 = <NUM_LIT:0> , zconf_1 = <NUM_LIT:0.> , result_2 = <NUM_LIT:0> , zconf_2 = <NUM_LIT:0.> ) : <EOL> """<STR_LIT>""" <EOL> arg1 = str ( result_1 ) <EOL> arg2 = str ( zconf_1 ) <EOL> arg3 = str ( result_2 ) <EOL> arg4 = str ( zconf_2 ) <EOL> if result2 == <NUM_LIT:0> : <EOL> os . system ( "<STR_LIT>" + self . inputs_file + "<STR_LIT:U+0020>" + arg1 + "<STR_LIT:U+0020>" + arg2 ) <EOL> else : <EOL> os . system ( <EOL> "<STR_LIT>" + self . inputs_file + "<STR_LIT:U+0020>" + arg1 + "<STR_LIT:U+0020>" + arg2 + "<STR_LIT:U+0020>" + arg3 + "<STR_LIT:U+0020>" + arg4 ) <EOL> def plot_pdf_use ( self , result_id = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> ir = result_id <EOL> path_results = self . Pars . path_results <EOL> filebase = self . Pars . finalfilename <EOL> H1 = load ( path_results + filebase + '<STR_LIT:.>' + str ( ir ) + '<STR_LIT>' ) <EOL> minz = self . Pars . minz <EOL> maxz = self . Pars . maxz <EOL> lev = <NUM_LIT:10> <EOL> maxH = H1 . max ( ) <EOL> minH = maxH / <NUM_LIT> <EOL> LL = linspace ( minH , maxH , lev ) <EOL> plt . contourf ( H1 , levels = LL , extent = [ minz , maxz , minz , maxz ] , origin = '<STR_LIT>' , cmap = cm . jet ) <EOL> plt . colorbar ( ) <EOL> plt . plot ( [ minz , maxz ] , [ minz , maxz ] , '<STR_LIT>' , lw = <NUM_LIT> ) <EOL> plt . xlabel ( r'<STR_LIT>' , fontsize = <NUM_LIT:15> ) <EOL> plt . ylabel ( r'<STR_LIT>' , fontsize = <NUM_LIT:15> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . figure ( ) <EOL> zz , nn = loadtxt ( path_results + filebase + '<STR_LIT:.>' + str ( ir ) + '<STR_LIT>' , unpack = True ) <EOL> zs , za , zb , oa , ob , ea , eb = loadtxt ( path_results + filebase + '<STR_LIT:.>' + str ( ir ) + '<STR_LIT>' , unpack = True ) <EOL> Nbins = len ( nn ) <EOL> Nz = linspace ( minz , maxz , Nbins + <NUM_LIT:1> ) <EOL> Nzmid = <NUM_LIT:0.5> * ( Nz [ <NUM_LIT:1> : ] + Nz [ : - <NUM_LIT:1> ] ) <EOL> ns , zsp = histogram ( zs , bins = Nz , normed = True ) <EOL> na , zap = histogram ( za , bins = Nz , normed = True ) <EOL> nb , zbp = histogram ( zb , bins = Nz , normed = True ) <EOL> gs = gridspec . GridSpec ( <NUM_LIT:2> , <NUM_LIT:1> , height_ratios = [ <NUM_LIT:4> , <NUM_LIT:2> ] ) <EOL> ax1 = plt . subplot ( gs [ <NUM_LIT:0> ] ) <EOL> ax2 = plt . subplot ( gs [ <NUM_LIT:1> ] ) <EOL> fzspec = ns / sum ( ns ) <EOL> fzphot = nn / sum ( nn ) <EOL> fzmode = na / sum ( na ) <EOL> fzmean = nb / sum ( nb ) <EOL> ax1 . plot ( zz , fzspec , color = '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> ax1 . fill_between ( zz , fzspec , color = '<STR_LIT>' , label = '<STR_LIT>' , alpha = <NUM_LIT> ) <EOL> ax1 . plot ( zz , fzmean , lw = <NUM_LIT:2> , label = '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> ax1 . plot ( zz , fzmode , lw = <NUM_LIT:2> , label = '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> ax1 . plot ( zz , fzphot , lw = <NUM_LIT:2> , label = '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> ax1 . set_xlabel ( '<STR_LIT>' ) <EOL> ax1 . set_ylabel ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> ax1 . set_xlim ( minz , maxz ) <EOL> ax1 . legend ( ) <EOL> ax2 . plot ( zz , abs ( fzspec - fzmean ) , color = '<STR_LIT>' , lw = <NUM_LIT> ) <EOL> ax2 . plot ( zz , abs ( fzspec - fzmode ) , color = '<STR_LIT>' , lw = <NUM_LIT> ) <EOL> ax2 . plot ( zz , abs ( fzspec - fzphot ) , color = '<STR_LIT>' , lw = <NUM_LIT> ) <EOL> ax2 . set_xlabel ( '<STR_LIT>' ) <EOL> ax2 . set_ylabel ( '<STR_LIT>' , fontsize = <NUM_LIT:16> ) <EOL> ax2 . set_xlim ( minz , maxz ) <EOL> plt . show ( ) <EOL> def plot_sparse ( self , result_id = <NUM_LIT:0> , kgal = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> filenum = str ( result_id ) <EOL> froot = self . Pars . path_results + self . Pars . finalfilename <EOL> if self . Pars . multiplefiles == '<STR_LIT:yes>' : <EOL> if self . Pars . writefits == '<STR_LIT>' : forig = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> if self . Pars . writefits == '<STR_LIT:yes>' : forig = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> ffits = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> else : <EOL> if self . Pars . writefits == '<STR_LIT>' : forig = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> if self . Pars . writefits == '<STR_LIT:yes>' : forig = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> ffits = froot + '<STR_LIT:.>' + filenum + '<STR_LIT>' <EOL> if self . Pars . writefits == '<STR_LIT>' : PO = load ( forig ) <EOL> if self . Pars . writefits == '<STR_LIT:yes>' : <EOL> Temp = pf . open ( forig ) <EOL> PO = Temp [ <NUM_LIT:1> ] . data . field ( '<STR_LIT>' ) <EOL> Temp . close ( ) <EOL> F = pf . open ( ffits ) <EOL> P = F [ <NUM_LIT:2> ] . data . field ( '<STR_LIT>' ) <EOL> F . close ( ) <EOL> if kgal < <NUM_LIT:0> : <EOL> k = rn . sample ( xrange ( len ( PO ) - <NUM_LIT:1> ) , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> else : <EOL> k = kgal <EOL> head = pdf_storage . read_header ( ffits ) <EOL> z = head [ '<STR_LIT:z>' ] <EOL> rep_pdf = pdf_storage . reconstruct_pdf_int ( P [ k ] , head ) <EOL> plt . figure ( ) <EOL> plt . plot ( z , PO [ k ] / sum ( PO [ k ] ) , label = '<STR_LIT>' ) <EOL> plt . plot ( z , rep_pdf , label = '<STR_LIT>' ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> plt . legend ( loc = <NUM_LIT:0> ) <EOL> title = '<STR_LIT>' % ( k , len ( P ) ) <EOL> plt . title ( title ) <EOL> plt . figure ( ) <EOL> AD = pdf_storage . create_voigt_dict ( z , head [ '<STR_LIT>' ] , head [ '<STR_LIT>' ] , head [ '<STR_LIT>' ] , head [ '<STR_LIT>' ] , head [ '<STR_LIT>' ] ) <EOL> sp_ind = array ( map ( pdf_storage . get_N , P [ k ] ) ) <EOL> spi = sp_ind [ : , <NUM_LIT:0> ] <EOL> Dind2 = sp_ind [ : , <NUM_LIT:1> ] <EOL> AA = linspace ( <NUM_LIT:0> , <NUM_LIT:1> , head [ '<STR_LIT>' ] ) <EOL> Da = AA [ <NUM_LIT:1> ] - AA [ <NUM_LIT:0> ] <EOL> vals = spi * Da <EOL> delta = zeros ( shape ( AD ) [ <NUM_LIT:1> ] ) <EOL> delta [ Dind2 ] = vals <EOL> pdfr = dot ( AD , delta ) <EOL> plt . plot ( z , pdfr / sum ( pdfr ) , '<STR_LIT>' , lw = <NUM_LIT:2> , label = '<STR_LIT>' ) <EOL> for i in xrange ( len ( Dind2 ) ) : <EOL> plt . plot ( z , AD [ : , Dind2 [ i ] ] * vals [ i ] / sum ( pdfr ) , '<STR_LIT>' ) <EOL> plt . plot ( z , AD [ : , Dind2 [ <NUM_LIT:0> ] ] * vals [ <NUM_LIT:0> ] / sum ( pdfr ) , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> plt . legend ( loc = <NUM_LIT:0> ) <EOL> title = '<STR_LIT>' % ( k , len ( P ) ) <EOL> plt . title ( title ) <EOL> plt . show ( ) </s>
