示例#1
0
文件: MonteCarlo.py 项目: jkluter/MLG
def StartMC(EventList, namelist=None, keywords={}, **kwargs):
    '''------------------------------------------------------------
		Description: 
		This Function distribute the calculation for the MonteCarlo
		simulation on multiple CPU_Cores and stors the results in an PKL file	
		uses the joblib package for parallel computing 
	---------------------------------------------------------------
		Input:
			EventList: 	String of the input table 	or 
						List of Lens source paris for each event or
						MC_results for vary the mass also
			namelist: List of names for each event (for ploting routine, if None use of the index)
	---------------------------------------------------------------
		Output:
			MC_result:  Dictnary of the results. Contains:
			 	'Header': List of all control imputs, and Code Version
			 	'Results': List of the fit parameters
			 	'Input_parameter': List of the input parameters
			 	'Results_ext_obs':  List of the fit parameters with external_obs if ext_obs > 0 
			 	'Chi2':	List of the reduced CHI2
			 	'Eventlist': (see Input)
			 	'Names': (see Input)
	------------------------------------------------------------'''

    #--------------------------------------------------------------
    #Update controle keywords
    keywords.update(kwargs)
    num_core = keywords.get('num_core', 6)  # Number of cores
    instring = keywords.get('instring', '')  # string for the save files
    message = keywords.get(
        'message', False)  # write the process of the computation to an file
    DR3 = keywords.get('DR3', False)  # Use of the Data of DR3 only
    extended = keywords.get(
        'extended', False)  # Use of the 10 years data of the extended mission
    vary_eta = keywords.get('vary_eta',
                            False)  # Use an new scaninglaw for 2020.5-2024.5
    ext_obs = keywords.get('ext_obs', False)  # include external observations
    n_error_picks = keywords.get(
        'n_error_picks',
        500)  # Number of pickt Observation from the error ellips
    n_par_picks = keywords.get(
        'n_par_picks',
        1)  # Number of pickt input Parameters from the error ellips
    namelist = keywords.get(
        'namelist',
        namelist)  # Check if namelist is given in the keyword dictionary
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    #Create random seeds for the calculation
    seed = []
    for i in range(num_core):
        seed.append((int(time.time() * 10**9))**4 % 4294967295)
    keywords['seed'] = seed
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    #Load Table if EventList is an string
    if isinstance(EventList, str) == True:
        if EventList == '':
            EventList, _ = RealData.loadRealData(default_table)
        else:
            EventList, _ = RealData.loadRealData(EventList)
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    # start calculations with varing the mass if a dict is given
    if isinstance(EventList, dict) == True:
        print('vary mass')

        #--------------------------------------------------------------
        # extract lists from Dictionary
        MC_Results = EventList
        res_all_events = MC_Results['Results']
        par_all_events = MC_Results['Input_parameter']
        EventList = MC_Results['Eventlist']
        namelist = MC_Results['Names']
        header = MC_Results['Header']
        #--------------------------------------------------------------

        #--------------------------------------------------------------
        # only consider the events with an error < 100%
        EventList = [EventList[i] for i in range(len(res_all_events)) \
          if (percentile(res_all_events[i][:,0])[0]> 0)]
        if namelist is None:
            namelist_good = None
        else:
            namelist_good = [namelist[i] for i in range(len(res_all_events)) \
             if (percentile(res_all_events[i][:,0])[0]> 0)]
        #--------------------------------------------------------------

        #--------------------------------------------------------------
        # update control keywords
        keywords[
            'Good'] = True  # indication calculation of the good events only
        goodstr = 'Good_'  # string for indication calculation of the good events only
        keywords['vary_par'] = 5  # vary all parameters
        n_error_picks = keywords.get(
            'n_error_picks',
            500)  #check if value is given in keywords else set to defaut
        keywords['n_par_picks'] = n_par_picks
        n_par_picks = keywords.get(
            'n_par_picks',
            100)  #check if value is given in keywords else set to defaut
        keywords['n_error_picks'] = n_error_picks
        #--------------------------------------------------------------

    #--------------------------------------------------------------
    # start first calculation
    elif isinstance(EventList, list) == True:
        #--------------------------------------------------------------
        # update control keywords
        keywords['n_par_picks'] = n_par_picks  # Update keyword
        keywords['n_error_picks'] = n_error_picks  # Update keyword
        keywords['vary_par'] = keywords.get(
            'vary_par', 1)  # vary only non given parameters
        keywords[
            'Good'] = False  # indication calculation of the good events only
        goodstr = ''
        #--------------------------------------------------------------

        #--------------------------------------------------------------
        #set default namelist to integer (not comparable within different inputs)
        if namelist == None:
            namelist == [str(i) for i in range(len(EventList))]
        #--------------------------------------------------------------
    #--------------------------------------------------------------
    # exclude different filetypes
    else:
        print('Input Error!')
        return
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    # create header
    if instring is not '':
        instring = instring + '_'

    if DR3:
        #use only events before 2019.5 for DR3
        EventList = [
            EventList[kkk] for kkk in range(len(EventList))
            if EventList[kkk][0].getTca() < 2019.5
        ]
        header = len(
            EventList
        ), 3, n_error_picks, n_par_picks, keywords, Version + '.' + Subversion
    elif extended or vary_eta or ext_obs:
        #use the data of the extended mission
        header = len(
            EventList
        ), 10, n_error_picks, n_par_picks, keywords, Version + '.' + Subversion
    else:
        header = len(
            EventList
        ), 5, n_error_picks, n_par_picks, keywords, Version + '.' + Subversion
    print(time.ctime())
    print(header)
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    # Distribute on different cores
    num_events = len(EventList)
    events_per_core = num_events / num_core
    #calculation of multiple events (proxima centauri tend to take as much computation time as all other events)
    if len(EventList[0]) > 10 and num_core > 2:
        events_per_core = (num_events - 1) / (num_core - 1)
        for core in range(num_core):
            partstring = path + 'Simulation/evpart/eventlist_' + goodstr + instring + 'part_%i.pkl' % core
            if core == 0:
                f = open(partstring, 'wb')
                pickle.dump([
                    EventList[0],
                ], f)
                f.close()
            elif core == num_core - 1:
                f = open(partstring, 'wb')
                pickle.dump(
                    EventList[1 + round(events_per_core * (core - 1)):], f)
                f.close()
            else:
                f = open(partstring, 'wb')
                pickle.dump(
                    EventList[1 + round(events_per_core * (core - 1)):1 +
                              round(events_per_core * (core))], f)
                f.close()
    #distribute events equaly
    else:
        for core in range(num_core):
            partstring = path + 'Simulation/evpart/eventlist_' + goodstr + instring + 'part_%i.pkl' % core
            if core == num_core - 1:
                f = open(partstring, 'wb')
                pickle.dump(EventList[round(events_per_core * core):], f)
                f.close()
            else:
                f = open(partstring, 'wb')
                pickle.dump(
                    EventList[round(events_per_core *
                                    core):round(events_per_core * (core + 1))],
                    f)
                f.close()
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    #start calculations parallel
    if num_core != 1:
        res_par = Parallel(n_jobs=num_core)(
            delayed(calcParallel)(i, instring, keywords)
            for i in range(num_core))
    else:
        res_par = [
            calcParallel(0, instring, keywords),
        ]
    #--------------------------------------------------------------

    #--------------------------------------------------------------
    #merge the results for the parallel computations
    res_all_events = []
    par_all_events = []
    res_no_ML_events = []
    chi2_events = []
    for res_par_core in res_par:
        for par_event in res_par_core[1]:
            par_all_events.append(par_event)
        for res_event in res_par_core[0]:
            res_all_events.append(res_event)
        for res_no_event in res_par_core[2]:
            res_no_ML_events.append(res_no_event)
        for res_no_event in res_par_core[3]:
            chi2_events.append(res_no_event)
    if ext_obs:
        MC_Results = {'Header': header,'Results':res_all_events,'Input_parameter':par_all_events,\
          'Results_ext_obs':res_no_ML_events, 'Chi2':chi2_events,'Eventlist':EventList,'Names':namelist}
    else:
        MC_Results = {'Header': header,'Results':res_all_events,'Input_parameter':par_all_events,\
          'Results_no_ML':res_no_ML_events, 'Chi2':chi2_events,'Eventlist':EventList,'Names':namelist}

    #--------------------------------------------------------------

    #--------------------------------------------------------------
    # save results as pkl file
    string = path + 'Data/MC'+goodstr[:-1] +'_'+ instring + datetime.datetime.today().strftime('%d-%m-%Y')\
       + '_%.f_%.f_%.f_%.f.pkl' % (header[:4])
    f = open(string, 'wb')
    pickle.dump(MC_Results, f)
    print(string)
    if message:
        os.system('cp ' + string + ' ' + message_folder)
    return MC_Results
示例#2
0
文件: MonteCarlo.py 项目: jkluter/MLG
def calcParallel(part, instring, keywords={}, **kwargs):
    '''------------------------------------------------------------
		Description: 
			creats sets of observations for a part of the Eventlist
			and fits the data
	---------------------------------------------------------------
		Input: 
			part: 	which part of the EventList
			instring: file string of the EventList
			keywords/kwargs: setup values for the simulation (see below)
	---------------------------------------------------------------
		Output:
			res_part: results of the individual fits (contains a separate list for each events)
			par_part: Inputparameters of the indiciual fits (contains a separate list for each events)
			res_no_ML: result without microlensing 
			chi2_red_part: Chi2 values of the individual fits (contains a separate list for each events)
	------------------------------------------------------------'''

    #---------------------------------------------------------------
    #extract keywords
    keywords.update(kwargs)
    seed = keywords.get('seed', None)  # seed's for the randomised process
    Good = keywords.get('Good',
                        False)  # Second loop with variation of the mass

    extended = keywords.get('extended',
                            False)  # Use of the data for extended Mission
    ext_obs = keywords.get('ext_obs',
                           False)  # ext_obs include external observations
    DR3 = keywords.get('DR3', False)  # Use of the data for DR3 only
    exact = keywords.get(
        'exact', False)  # Use the exact astrometric shift or the approximation

    n_error_picks = keywords.get(
        'n_error_picks',
        500)  # number of picks from the error elips of the measurements
    n_par_picks = keywords.get(
        'n_par_picks', 1)  # number of different picks of input parameters
    vary_par = keywords.get(
        'vary_par', 1)  # which parameters should be varied for n_par_picks

    prefit_motion = keywords.get(
        'prefit_motion',
        False)  # fit the propermotion with out microlensing as preput
    vary_eta = keywords.get('vary_eta', False)  #Use data while vary eta
    onlysource = keywords.get('onlysource',
                              False)  # use the data of the source only

    timer = keywords.get('timer',
                         False)  # print computing-time for different steps
    message = keywords.get(
        'message',
        False)  # save messeage file for keeping track of the process
    silent = keywords.get('silent',
                          True)  # boolen if information shoul not be printed
    #---------------------------------------------------------------

    #---------------------------------------------------------------
    # computationtime tracker
    cputt = [0., 0., 0., 0., 0., 0., 0]
    cputt[0] -= time.time()
    # update seed for the randomised process
    if seed is not None:
        np.random.seed(seed[part])
    #---------------------------------------------------------------

    #---------------------------------------------------------------
    # initilise arrays for storing the results
    # fit results
    res_part = []
    # fit results without microlensing
    res_no_ML = []
    # input parameters
    par_part = []
    # Chi2_reduced value
    chi2_red_part = []
    chi2_red_mot_part = []
    #---------------------------------------------------------------

    #---------------------------------------------------------------
    #	load part of the Eventlist
    if Good:
        partstring = path + 'Simulation/evpart/eventlist_Good_' + instring + 'part_%i.pkl' % part
        f = open(partstring, 'rb')
        EventList = pickle.load(f)
        f.close
        os.remove(path + 'Simulation/evpart/eventlist_Good_' + instring +
                  'part_%i.pkl' % part)

    else:
        partstring = path + 'Simulation/evpart/eventlist_' + instring + 'part_%i.pkl' % part
        f = open(partstring, 'rb')
        EventList = pickle.load(f)
        f.close
        os.remove(path + 'Simulation/evpart/eventlist_' + instring +
                  'part_%i.pkl' % part)
    cputt[0] += time.time()
    #---------------------------------------------------------------

    for i1 in range(len(EventList)):
        #---------------------------------------------------------------
        # updat process tracker
        if message:
            os.system('touch %s.%s-%s-0.message' %
                      (message_folder + instring[:-1], part, i1))
        #---------------------------------------------------------------

        cputt[1] -= time.time()

        #---------------------------------------------------------------
        # initilise list for each event
        # fit results
        res_single = []
        res_external_obs = []  #for comparison (test)
        # input parameters
        par_single = []
        # Observations (for fitting  without microlensing)
        Obs_save = []
        # Chi2_reduced value
        chi2_red_single = []
        #---------------------------------------------------------------

        #get Stars
        Stars = EventList[i1]

        #---------------------------------------------------------------
        # create observations from real scanning law
        RealObs = RealData.getRealObs(Stars[0], Stars[1], **keywords)
        Data = RawData.Data(Stars, *RealObs, **keywords)
        Obs, Starnumber, Epoch, Obs_err, sc_scandir = Data.Observations(
            **keywords)
        #---------------------------------------------------------------

        cputt[1] += time.time()

        for i2 in range(n_par_picks):
            #loop over each set of differen input parameters
            par = Data.par[i2]
            for i3 in range(n_error_picks):
                #loop over each set of differen picks from the error ellips of the Observation

                cputt[6] = -time.time()
                cputt[2] -= time.time()

                # include outliers 5% chance
                if ext_obs:
                    outliers = np.random.uniform(0, 1, len(Starnumber[i2]))
                    outliers[-4 *
                             ext_obs:] = 1  #keep the external observations
                    n = np.where(outliers > 0.05)
                else:
                    n = np.where(
                        np.random.uniform(0, 1, len(Starnumber[i2])) > 0.05)

                #---------------------------------------------------------------
                # get the right set of observations
                Obs_i = Obs[i2][i3][n]
                if len(Obs_i) < 11 + 4 * ext_obs:
                    # not enought observation
                    re = -999 * np.ones(len(Stars) * 5 + 1)
                    res_single.append(re)
                    chi2_red_single.append(-999.)
                    par_single.append(par)
                    cputt[2] += time.time()
                    res_external_obs.append(re)

                else:
                    # get the right set of observations
                    # (identical within each set of input parameters)
                    Starnumber_i = Starnumber[i2][n]
                    Epoch_i = Epoch[i2][n]
                    Obs_err_i = Obs_err[i2][n]
                    sc_scandir_i = sc_scandir[i2][n]
                    #---------------------------------------------------------------

                    #---------------------------------------------------------------
                    #compare with external observations (test)
                    if ext_obs:
                        res_ext= fitting_micro.Fit_Micro(Obs_i,Starnumber_i,Epoch_i,Obs_err_i, sc_scandir_i,\
                           bounds = True, **keywords)
                        res_external_obs.append(res_ext.x)

                        # remove external observations
                        Obs_i = Obs_i[:-4 * ext_obs]
                        Obs_err_i = Obs_err_i[:-4 * ext_obs]
                        Starnumber_i = Starnumber_i[:-4 * ext_obs]
                        Epoch_i = Epoch_i[:-4 * ext_obs]
                        sc_scandir_i = sc_scandir_i[:-4 * ext_obs]

                    #---------------------------------------------------------------

                    #---------------------------------------------------------------
                    # store observations
                    Obs_save.append([
                        Obs_i, Starnumber_i, Epoch_i, Obs_err_i, sc_scandir_i
                    ])
                    cputt[2] += time.time()
                    #---------------------------------------------------------------

                    #---------------------------------------------------------------
                    # Fit model to the observational data
                    cputt[3] -= time.time()
                    res= fitting_micro.Fit_Micro(Obs_i,Starnumber_i,Epoch_i,Obs_err_i, sc_scandir_i,\
                      bounds = True, **keywords)
                    cputt[3] -= time.time()
                    #---------------------------------------------------------------

                    #---------------------------------------------------------------
                    #store results
                    cputt[4] -= time.time()
                    if len(res.x) != len(Stars) * 5 + 1:
                        #Check if all stars have observations
                        re = -999 * np.ones(len(Stars) * 5 + 1)
                        re[:len(res.x)] = res.x
                        re[len(res.x):] = None
                        if len(res.x) == 6: re[0] = -999
                        res_single.append(re)
                    else:
                        res_single.append(res.x)
                    chi2_red_single.append(res.cost * 2 / (len(res.fun) - 11))
                    par_single.append(par)
                    cputt[4] += time.time()
                    #---------------------------------------------------------------

                #---------------------------------------------------------------
                #print computing time (for optimisation)
                if timer:
                    if not (i3 % 100):
                        cputt[5] += time.time()
                        print('Part %i-%i Step %i %.2f' %
                              (part, i1, i3, cputt[6]))
                        if time.time() - ti > 10: return 0
                #---------------------------------------------------------------

            #---------------------------------------------------------------
            # updat process tracker
            if message:
                os.system('mv %s.%s-%s-%s.message %s.%s-%s-%s.message'%\
                  (message_folder + instring[:-1], part, i1, i2,\
                  message_folder + instring[:-1], part, i1, i2+1))
            #---------------------------------------------------------------

        if not silent: print(i3, time.time() - ti)
        #---------------------------------------------------------------
        # sort results
        cputt[5] -= time.time()
        res_single = np.stack(res_single)
        res_part.append(res_single)
        par_single = np.stack(par_single)
        par_part.append(par_single)
        chi2_red_single = np.stack(chi2_red_single)
        chi2_red_part.append(chi2_red_single)
        #---------------------------------------------------------------
        #compare without external observations (test)
        if ext_obs:
            res_external_obs = np.stack(res_external_obs)
            res_no_ML.append(res_external_obs)

        #---------------------------------------------------------------

        #---------------------------------------------------------------
        # fit mean result also without microlensing
        if len(res_single[:, 0]) > 1:
            if ext_obs:
                chi2_red_micro_best = 0
                chi2_red_motion = 0
            else:
                p50 = percentile(res_single[:, 0])[1]
                if p50 == -999:
                    chi2_red_mot_part.append(-999)
                    chi2_red_micro_best = -999
                    res_no_ML.append(-999 * np.ones(5 * len(Stars)))
                    chi2_red_motion = -999
                else:
                    best = np.where(res_single[:, 0] == p50)[0][0]
                    res_motion = fitting_motion.Fit_Motion(
                        *Obs_save[best], **keywords)
                    res_no_ML.append(res_motion.x)
                    chi2_red_micro_best = chi2_red_single[best]
                    chi2_red_motion = res_motion.cost * 2 / (
                        len(Obs_save[best][1]) - 10)
                    chi2_red_mot_part.append(chi2_red_motion)

            cputt[5] += time.time()
        #---------------------------------------------------------------

        #---------------------------------------------------------------
        # print computing time (for optimisation)
        if timer:
            print('MC %.0f-%.0f: %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f' %
                  (part, i1, *tuple(cputt)))
        #---------------------------------------------------------------

        else:
            #---------------------------------------------------------------
            # print statistics
            if len(res_single[:, 0]) > 1:
                print('%.0f-%.0f: %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %s'\
                 % (part,i1, Stars[0].mass,*percentile(res_single[:,0]), chi2_red_micro_best,chi2_red_motion, time.ctime().split(' ')[3]))
            #---------------------------------------------------------------

    if not silent: print('Part {} done!'.format(part))

    #---------------------------------------------------------------
    # updat process tracker
    if message:
        os.system('rm %s.%s*.message' % (message_folder + instring[:-1], part))
        os.system('touch %s.%s.Done.message' %
                  (message_folder + instring[:-1], part))
    #---------------------------------------------------------------

    return res_part, par_part, res_no_ML, chi2_red_part
示例#3
0
def PlotHistAll(Analysis_result, string = 'mass_accuracy_histogram', log = False, logbin = True, \
 ylim = [0,60], xlim = [0.0,1.92], extended = True, future = False, use_error = 0, vary = None, ty = 'Mass'):
    '''------------------------------------------------------------
		Description: 
	---------------------------------------------------------------
		Input:
	---------------------------------------------------------------
		Output:
	------------------------------------------------------------'''
    '''
	plot an histogram of input masses with color coded accuracy
	'''
    dark = 'black' in plt.rcParams['savefig.facecolor']

    if dark: string = 'dark_' + string
    try:
        a = Analysis_result[1][0][1][1]

        TCA = None
    except IndexError:
        stats = np.array([i[0][2:] for i in Analysis_result[0]])
        if ty == 'Mass' or ty == 'm':
            stats_plot = stats[:, 0]
        elif ty == 'GL':
            stats_plot = np.array([i[0].getMag() for i in Analysis_result[2]])
        elif ty == 'GS':
            stats_plot = np.array([i[1].getMag() for i in Analysis_result[2]])

        elif ty == 'dG':
            stats_plot = np.array(
                [i[1].getMag() - i[0].getMag() for i in Analysis_result[2]])
        elif ty == 'c':
            stats_plot = np.array([
                dist(i[0], i[1], unit='arcsec', T=i[1].getTca())
                for i in Analysis_result[2]
            ])

        stats_in = stats
        stats_v = []
        stats_in_v = []
        stats_plot_v = []
        vary_bool = vary is not None
        TCA_v = []
        if vary_bool:
            Events_v = vary[2]
            if ty == 'Mass' or ty == 'm':
                stats_plot_v = np.array([i[0].getMass() for i in vary[2]])
            elif ty == 'GL':
                stats_plot_v = np.array([i[0].getMag() for i in vary[2]])
            elif ty == 'GS':
                stats_plot_v = np.array([i[1].getMag() for i in vary[2]])
            elif ty == 'dG':
                stats_plot_v = np.array(
                    [i[1].getMag() - i[0].getMag() for i in vary[2]])
            elif ty == 'c':
                stats_plot_v = np.array([
                    dist(i[0], i[1], unit='arcsec', T=i[1].getTca())
                    for i in vary[2]
                ])
            for i in range(len(vary[0])):
                stats_in_v.append(Events_v[i][0].getMass())
                mm_v = np.array([
                    vary[0][i][x][0][6] for x in range(len(vary[0][i]))
                    if vary[0][i][x][0][4] != -999
                ])
                mp_v = np.array([
                    vary[0][i][x][0][7] for x in range(len(vary[0][i]))
                    if vary[0][i][x][0][4] != -999
                ])
                mmm_v = percentile(mm_v)
                ppp_v = percentile(mp_v)
                delta_M_v = max(ppp_v[1], -mmm_v[1]) / Events_v[i][0].getMass()
                stats_v.append(delta_M_v)
                TCA_v.append(Events_v[i][0].getTca())

            stats_v = np.array(stats_v)
            stats_in_v = np.array(stats_in_v)
            stats_plot_v = np.array(stats_plot_v)
            TCA_v = np.array(TCA_v)

            gmag = np.array([i[0].getMag() for i in Events_v])
            Good_ = np.array([name_good(i[0].getId()) for i in Events_v])
            gg = np.where((gmag > 5) & (Good_))

            stats_v = stats_v[gg]
            stats_plot_v = stats_plot_v[gg]

            TCA_v = TCA_v[gg]

            if not extended:
                ngm_v = np.where(TCA_v < 2019.5)
            if future:
                ngm_v = np.where(TCA_v > 2019.5)
                stats_v = stats_v[ngm_v]
                stats_plot_v = stats_plot_v[ngm_v]

        Eventnames = Analysis_result[1]
        Events = Analysis_result[2]
        ID = np.array([i[0].getId() for i in Events])
        gmag = np.array([i[0].getMag() for i in Events])
        Good_ = np.array([name_good(i[0].getId()) for i in Events])
        gg = np.where((gmag > 5) & (Good_))
        stats = stats[gg]
        stats_plot = stats_plot[gg]
        stats_in = stats_in[gg]
        ID = ID[gg]
        TCA = np.array([ev[0].getTca() for ev in Events])
        TCA = TCA[gg]
        if not extended:
            ngm = np.where(TCA < 2019.5)[0]
        if future:
            ngm = np.where(TCA > 2019.5)[0]
            stats = stats[ngm]
            stats_plot = stats_plot[ngm]

    else:
        stats = np.array([i[0][2:] for i in Analysis_result])
        stats_plot = np.array([i[0][2:] for i in Analysis_result])

    #--------------------------------------------
    #limits for the colorcoding
    lim1 = 0.15
    lim2 = 0.000
    lim3 = 0.3
    lim4 = 0.5
    lim = [lim1, lim2, lim3, lim4, 1]

    if vary_bool:
        p10 = np.where(stats_v < lim1)[0]
        p20 = np.where(stats_v < lim2)[0]
        p30 = np.where(stats_v < lim3)[0]
        p50 = np.where(stats_v < lim4)[0]
        p100 = np.where(stats_v < 1)[0]
    elif use_error == -1:
        p10 = np.where(-stats[:, 4] / stats[:, 0] < lim1)[0]
        p20 = np.where(-stats[:, 4] / stats[:, 0] < lim2)[0]
        p30 = np.where(-stats[:, 4] / stats[:, 0] < lim3)[0]
        p50 = np.where(-stats[:, 4] / stats[:, 0] < lim4)[0]
        p100 = np.where(-stats[:, 4] / stats[:, 0] < 1)[0]
    elif use_error == 1:
        p10 = np.where(stats[:, 5] / stats[:, 0] < lim1)[0]
        p20 = np.where(stats[:, 5] / stats[:, 0] < lim2)[0]
        p30 = np.where(stats[:, 5] / stats[:, 0] < lim3)[0]
        p50 = np.where(stats[:, 5] / stats[:, 0] < lim4)[0]
        p100 = np.where(stats[:, 5] / stats[:, 0] < 1)[0]
    elif use_error == -999:
        p10 = np.where(
            np.minimum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim1)[0]
        p20 = np.where(
            np.minimum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim2)[0]
        p30 = np.where(
            np.minimum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim3)[0]
        p50 = np.where(
            np.minimum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim4)[0]
        p100 = np.where(
            np.minimum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < 1)[0]
    else:
        p10 = np.where(
            np.maximum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim1)[0]
        p20 = np.where(
            np.maximum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim2)[0]
        p30 = np.where(
            np.maximum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim3)[0]
        p50 = np.where(
            np.maximum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < lim4)[0]
        p100 = np.where(
            np.maximum(stats[:, 5], -stats[:, 4]) / stats[:, 0] < 1)[0]
    p = [p10, p20, p30, p50, p100]

    #--------------------------------------------

    if not extended and TCA is not None:
        print(len(p10), len(p20), len(p30), len(p50), len(p100), len(stats),
              len(ngm))
    else:
        print(len(p10), len(p20), len(p30), len(p50), len(p100), len(stats),
              len(np.unique(ID)))

    #--------------------------------------------
    #setup figure
    fig = plt.figure(figsize=(12, 8))
    ax = fig.add_subplot(111)
    plt.subplots_adjust(top=0.9, bottom=0.16)
    #--------------------------------------------

    #--------------------------------------------
    #defines a scale for the bins
    if ty == 'Mass' or ty == 'm':
        if log:
            plt.xscale('log')
            bins = 1.3**np.arange(0, 20) * 0.05
        elif logbin:
            d = 1.25**np.arange(0, 11) * 0.05
            bins = [np.sum(d[:i]) + 0.05 for i in range(len(d))]
        else:
            bins = 0.05 + 0.05 * np.arange(0, 30)
    elif ty == 'c':
        bins = [0, 1 / 3, 2 / 3, 1, 4 / 3, 5 / 3, 2, 3, 4, 5, 6, 7]
        #bins = 0.5*np.arange(14)
        #bins = 0.35 * 20 ** (np.arange(-2,5)/4)
        print(bins)
    elif 'G' in ty:
        bins = 8 + np.arange(0, 11)
    else:
        bins = 10
    #--------------------------------------------

    #--------------------------------------------
    #setup colores and hatches
    col_list = [[0.6, 0.6, 0.6, 0.6], [1, 0, 0, 1], [1, 1, 0, 1], [0, 0, 1, 1],
                [0, 1, 0, 1]]
    color = iter(color_own(col_list))

    hatch = iter(['', '//', 'x', '\\', '|'])
    if dark:
        black = color_own([0.7, 0.7, 0.7, 1])
    else:
        black = color_own([0., 0., 0., 1])
    #--------------------------------------------

    #--------------------------------------------
    # plot input distribution
    if ty == 'Mass' or ty == 'm':
        if not extended and TCA is not None:
            plt.hist(stats_in[ngm, 0],
                     bins=bins,
                     ls='-',
                     histtype='step',
                     linewidth=2,
                     color=black,
                     label='5 years sample',
                     zorder=-2)
        if future and TCA is not None:
            q = plt.hist(stats_in[ngm, 0],
                         bins=bins,
                         histtype='step',
                         color=black,
                         linewidth=5,
                         label='future events',
                         zorder=55)
        else:
            q = plt.hist(stats_in[:, 0],
                         bins=bins,
                         histtype='step',
                         color=black,
                         linewidth=5,
                         label='10 years sample',
                         zorder=55)
    else:
        q = plt.hist(stats_plot,
                     bins=bins,
                     histtype='step',
                     color=black,
                     linewidth=5,
                     label='10 years sample',
                     zorder=55)

    #--------------------------------------------

    #--------------------------------------------
    # plot distribution for each limit
    for j in range(4, -1, -1):
        c = next(color)
        h = next(hatch)
        if len(p[j] > 0):
            if vary_bool:
                print(len(p[j]))
                plt.hist(stats_plot_v[p[j]], bins = bins, color = c, hatch = h, rwidth = 0.9, \
                 label = '$\sigma < %i\%s$'%(100*lim[j],'%'))
            else:
                plt.hist(stats_plot[p[j]], bins = bins, color = c, hatch = h, rwidth = 0.9, \
                 label = '$\sigma < %i\%s$'%(100*lim[j],'%'))
    #--------------------------------------------

    #--------------------------------------------
    #scale y axis with labels
    if max(q[0]) < ylim[1]:
        ylim = plt.ylim()
    n = np.where(q[0] > ylim[1])[0]

    if ylim[1] > 20:
        y_minor_ticks = np.arange(ylim[0], ylim[1] + 5, 1)
        y_major_ticks = np.arange(ylim[0] + 10, ylim[1] + 10, 10)
    else:
        y_minor_ticks = np.arange(ylim[0], ylim[1] + 5, 1)
        y_major_ticks = np.arange(ylim[0] + 2, ylim[1] + 5, 2)
    #--------------------------------------------

    #--------------------------------------------
    #setup axis and labels
    ax.tick_params(axis='both', which='major', labelsize=20)
    ax.tick_params(axis='both', which='minor', labelsize=0)
    ax.set_yticks(y_major_ticks)
    ax.set_yticks(y_minor_ticks, minor=True)
    if 'G' in ty:
        plt.xticks([(round(b, 2)) for b in bins],
                   ["%i" % (round(b, 2)) for b in bins],
                   fontsize=20)
    elif not isinstance(bins, int):
        plt.xticks([(round(b, 2)) for b in bins],
                   ["%0.2f" % (round(b, 2)) for b in bins],
                   fontsize=20)
    for tick in ax.get_xticklabels():
        tick.set_rotation(90)
    plt.yticks(fontsize=25)

    ax.set_axisbelow(True)
    ax.yaxis.grid(color='gray', linestyle='dashed', linewidth=3, zorder=-20)
    for j in n:
        plt.text((q[1][j]+q[1][j+1])/2, ylim[1]-2,str(int(q[0][j])), verticalalignment = 'top',\
         horizontalalignment = 'center', fontsize = 16)
    if ty == 'Mass' or ty == 'm': plt.xlim(xlim)
    plt.ylim(ylim)
    if 'c' in ty:
        plt.xlabel('$\phi_{min}\,$["]', fontsize=30)
        plt.legend(fontsize=25, loc=1)
        plt.title('Impact parameter', fontsize=40)
    elif 'G' in ty:
        plt.xlabel('$G\,[mag]$', fontsize=30)
        plt.legend(fontsize=25, loc=2)
        plt.title('G magnitude (source)', fontsize=40)
    elif ty == 'Mass' or ty == 'm':
        plt.xlabel('$M\,[M_{\odot}]$', fontsize=30)
        plt.legend(fontsize=25, loc=1)
        if future: plt.title('Future events', fontsize=40)
        elif extended: plt.title('Extended mission', fontsize=40)
        else: plt.title('Nominal mission', fontsize=40)
    else: plt.legend(fontsize=25, loc=0)
    plt.ylabel('#', fontsize=30)

    #--------------------------------------------

    #--------------------------------------------
    #save figure
    if ty != 'Mass' and ty != 'm':
        string = string + '_' + ty
    fig.savefig(imagepath + string + '.png', format='png')
    if paperpath is not None:
        fig.savefig(paperpath + string + '.png', format='png')
    print('Create Image: ' + imagepath + string + '.png')
    plt.close(fig)
示例#4
0
def PlotDifferentIdeas(dat_single,dat_multi_all,dat_multi_fps,dat_multi_sig, \
 string = 'Diff_ID', k = None, Folder = 'Multi_events/'):
    '''------------------------------------------------------------
		Description: 
	---------------------------------------------------------------
		Input:
	---------------------------------------------------------------
		Output:
	------------------------------------------------------------'''
    dark = 'black' in plt.rcParams['savefig.facecolor']
    if dark:
        string = 'dark_' + string
        black = color_own([0.7, 0.7, 0.7, 1])
        cc = color_own([[0.6, 1.2, 0, 0.9], [0, 1, 1, 0.9], [1, 1, 0, 0.9],
                        [1, 0, 1, 0.9]])
    else:
        black = color_own([0., 0., 0., 1])
        cc = color_own([[0, 1, 0, 0.9], [0, 1, 1, 0.9], [1, .5, 0, 0.9],
                        [1, 0, 0, 0.9]])
    t = [0, 0, 0, 0, 0, 0]

    #match_events betwenn differnt methods
    ev = []
    c = 0
    try:
        a = dat_single[0][0][0][0][0]
    except:
        error = 1
    else:
        error = 3
    ti = time.time()
    for i in range(len(dat_multi_all[1])):
        c += 1
        event_id = dat_multi_all[1][i]
        m = dat_multi_all[2][i][0].getMass()
        nstars = []
        nstars.append(len(dat_multi_all[2][i]) - 1)

        if error == 3:
            ev1 = [
                event_id,
                [
                    dat_multi_all[0][i][x][0]
                    for x in range(len(dat_multi_all[0][i]))
                ]
            ]
        else:
            ev1 = [event_id, dat_multi_all[0][i][0]]
        for j in range(len(dat_multi_fps[1])):
            if dat_multi_fps[1][j] == event_id:
                if error == 3:
                    ev1.append([
                        dat_multi_fps[0][j][x][0]
                        for x in range(len(dat_multi_fps[0][j]))
                    ])
                    nstars.append(len(dat_multi_fps[2][j]) - 1)
                else:
                    ev1.append(dat_multi_fps[0][j][0])
                    nstars.append(len(dat_multi_fps[2][j]) - 1)

        if len(ev1) == 2:
            ev1.append(None)
            nstars.append(0)

        for j in range(len(dat_multi_sig[1])):
            if dat_multi_sig[1][j] == event_id:
                if error == 3:
                    ev1.append([
                        dat_multi_sig[0][j][x][0]
                        for x in range(len(dat_multi_sig[0][j]))
                    ])
                    nstars.append(len(dat_multi_sig[2][j]) - 1)
                else:
                    ev1.append(dat_multi_sig[0][j][0])
                    nstars.append(len(dat_multi_sig[2][j]) - 1)

        if len(ev1) == 3:
            ev1.append(None)
            nstars.append(0)
        acc = -999
        ind = -1
        counter = 0
        for j in range(len(dat_single[1])):
            if dat_single[1][j] == event_id:
                counter += 1
                if ind == -1:
                    if error == 3:
                        acc = percentile(np.array([dat_single[0][j][x][0][6]/dat_single[0][j][x][0][2] \
                          for x in range(len(dat_single[0][j]))]))[1]
                    else:
                        acc = dat_single[0][j][0][3]
                    ind = j
                else:
                    if error == 3:
                        pc = percentile(np.array([dat_single[0][j][x][0][6]/dat_single[0][j][x][0][2] \
                          for x in range(len(dat_single[0][j]))]))[1]
                        if pc > acc:
                            acc = pc
                            ind = j
                    else:
                        if dat_single[0][j][0][3] > acc:
                            acc = dat_single[0][j][0][3]
                            ind = j
        if ind != -1:
            nstars.append(1)
            if error == 3:
                ev1.append([
                    dat_single[0][ind][x][0]
                    for x in range(len(dat_single[0][ind]))
                ])
                #if percentile(np.array([dat_multi_all[0][i][x][0][6]/dat_multi_all[0][i][x][0][3] \
                #					for x in range(len(dat_multi_all[0][i]))]))[1] > -1:
            else:
                ev1.append(dat_single[0][ind][0])
                #if dat_multi_all[0][i][0][3] > 0:
        else:
            ev1.append(None)
            nstars.append(0)
        ev1.append(nstars)

        ev1.append(m)
        ev.append(ev1)

    t[0] = time.time() - ti

    t0 = time.time()
    #select_events
    #plot line
    if len(Folder) != 0:
        if Folder[-1] != '/': Folder + '/'
    if not os.path.isdir(imagepath + Folder): os.mkdir(imagepath + Folder)
    outstrings = []
    if k is None:
        k = np.arange(len(ev))
    for kk in range(len(k)):
        ti = time.time()

        fig = plt.figure(figsize=(12, 11))
        ax = fig.add_subplot(111)
        plt.subplots_adjust(top=0.9,
                            bottom=0.1,
                            left=0.16,
                            right=0.9,
                            hspace=0.2,
                            wspace=0.2)

        i = abs(k[kk])
        ax.tick_params(axis='both', which='major', labelsize=25)

        ax.tick_params(axis='both', which='minor', labelsize=0)
        #if kk%2 == 0:
        ax.set_ylabel('$\Delta M \,/\, M_{int} \,[\%]$', fontsize=30)
        ax.set_xlabel('Number of used background stars', fontsize=30)

        ax.set_title(name(ev[i][0]), fontsize=40)
        plt.tick_params(
            axis='x',  # changes apply to the x-axis
            which='both',  # both major and minor ticks are affected
            bottom=False,  # ticks along the bottom edge are off
            top=False,  # ticks along the top edge are off
            labelbottom=True)
        xx = []
        num = []
        if error == 3:
            #for k in range(50):
            ll = 0.2

            color = iter(cc)

            t[4] = time.time() - ti
            print(name(ev[i][0]), ev[i][-1])

            for j in range(1, 5):
                ti = time.time()
                sep = 0.5 * j

                c = next(color)
                good = True
                if j < 3:
                    if ev[i][-2][j - 1] == ev[i][-2][j]: good = False

                if good:
                    if ev[i][j] is not None:
                        mm = np.array([
                            ev[i][j][x][6] / ev[i][j][x][2] * 100
                            for x in range(len(ev[i][j]))
                            if ev[i][j][x][4] != -999
                        ])
                        mp = np.array([
                            ev[i][j][x][7] / ev[i][j][x][2] * 100
                            for x in range(len(ev[i][j]))
                            if ev[i][j][x][4] != -999
                        ])
                        mm1 = mm[np.where(mm > -200)]
                        mp1 = mp[np.where(mp < 200)]
                        mm1 = mm1[np.where(mm1 < 0)]
                        mp1 = mp1[np.where(mp1 > 0)]

                        parts = ax.violinplot([mp1],
                                              positions=[sep],
                                              showextrema=False)
                        for pc in parts['bodies']:
                            pc.set_facecolor(c)
                            pc.set_edgecolor(c)
                            pc.set_alpha(0.7)
                            pc.set_zorder(-0.1 * j)
                        rm = percentile(mm)
                        rp = percentile(mp)

                        plt.plot([sep, sep, sep],
                                 rp[0:3],
                                 '_-',
                                 c=black,
                                 markersize=30,
                                 linewidth=8,
                                 markeredgewidth=8)
                        print(j, np.array(rp[0:3]) * ev[i][-1] / 100, rp[0:3])
                        if j == 4:
                            xlim = ax.get_xlim()
                            plt.plot([0, sep * 5 / 4], [rp[1], rp[1]],
                                     linestyle='--',
                                     dashes=(10, 5),
                                     linewidth=5,
                                     color=black,
                                     zorder=-20)
                        ylim = ax.get_xlim()
                        xx.append(sep)
                        num.append(str(ev[i][-2][j - 1]))
                        #plt.text(sep, ylim[0],str(ev[i][-1][j-1]), verticalalignment = 'bottom',\
                        #		horizontalalignment = 'right', fontsize = 40)
                        t[2] += time.time() - ti
                    else:
                        xx.append(sep)
                        num.append(' ')
                else:
                    xx.append(sep)
                    num.append(' ')

        else:
            alpha = 1
            ll = 5
            color=iter(([0, 0., 1., alpha ],\
              [0, 1, 0., alpha ], \
              [1, 0, 0.5, alpha ], \
              [1., 0.5,0, alpha ]))
            for j in range(1, len(ev[i])):
                c = next(color)
                if ev[i][j][0] is not None:
                    m_minus, m, mplus = ev[i][j][3:6]
                    m = ev[i][j][2]
                    plt.plot([sep, sep, sep], [m_minus / m, 1, mplus / m],
                             '.',
                             c=c,
                             linewidth=ll)
        ax.grid(axis='both', linewidth=3, zorder=-50000)
        plt.xlim([0, sep / 4 * 5])
        ti = time.time()

        plt.xticks(xx, num)
        plt.xticks(fontsize=25)
        plt.yticks(fontsize=25)
        nn = ''.join(('_'.join(name(ev[i][0]).split(' '))).split(':'))
        fig.savefig(imagepath + Folder + string + '_' + nn + '.png',
                    format='png')
        outstrings.append(imagepath + Folder + string + '_' + nn + '.png')
        if paperpath is not None:
            fig.savefig(paperpath + string + '_' + nn + '.png', format='png')
        print('Create Image: ' + imagepath + Folder + string + '_' + nn +
              '.png')

        plt.close(fig)
        t[5] += time.time() - ti

    t[1] = time.time() - t0

    return outstrings
示例#5
0
def statistics(MC_Results, relative = False, plot = False, string = 'Event',\
 vary_par = False, v = False, ext_obs = False):
    '''------------------------------------------------------------
		Description: Calculate the persentiles of the distribution 
	---------------------------------------------------------------
		Input:
			MC_Results: Results of the Montecarlo Simulation
			plot: 		Create Histogram of the distribution of values
			string: 	name of the plot's
			vary_par/v: MC_Results contains results for different input parameters
			ext_obs: 	Use the data including external observations  
	---------------------------------------------------------------
		Output:
			stats_all:	List of the statistic results for each event, 
						differen set of input parameters (if vary == TRUE)
						different Type of input parameters    
			Eventnames: List of Names for each event
			Event_out:	Lens, and Source data for each event
	------------------------------------------------------------'''

    #------------------------------------------------------------
    #combine keyword
    vary_par = any([vary_par, v])
    #------------------------------------------------------------

    #------------------------------------------------------------
    #extract data from dictionary
    Events = MC_Results['Eventlist']
    Names = MC_Results['Names']
    if ext_obs:
        Result = MC_Results['Results_ext_obs']
    else:
        Result = MC_Results['Results']

    # give each event an name if Names is empty
    if Names is None:
        Names = [str(ev[0].getId()) for ev in Events]
    elif Names == '':
        Names = [str(ev[0].getId()) for ev in Events]
    Names = np.array(Names)

    #remove certain events from outList
    outList = np.array(
        ATable.read(path + 'out_list', format='csv')['source_ID'], str)
    #------------------------------------------------------------
    if relative:
        #------------------------------------------------------------
        # place holder for doing somthing with relative uncertainties which is not defined
        #------------------------------------------------------------
        pass
    else:
        #------------------------------------------------------------
        # arrays for storing results, and eventlist cleaning
        stats_all = []
        Eventnames = []
        Event_out = []
        #------------------------------------------------------------

        #------------------------------------------------------------
        # order the results by determine the number of differnt input parameters
        if vary_par:
            n_par_picks = 0
            m = -1
            for mass in MC_Results['Input_parameter'][0][:, 0]:
                if mass != m:
                    m = mass
                    n_par_picks += 1

        else:
            n_par_picks = 1
        n_per_mass = int(len(Result[0][:, 0]) / n_par_picks)
        #------------------------------------------------------------

        # loop over all events
        for i in range(len(Result)):
            #------------------------------------------------------------
            # check if element in outlist, if so: skip
            if Names[i] in outList:
                continue
            elif len(Result[i].shape) == 1:
                continue
            elif (Result[i] == -999).all():
                continue
            else:
                Event_out.append(Events[i])

            #------------------------------------------------------------

            #------------------------------------------------------------
            #store results for different sets of inputparameters
            stats_mass = []
            #------------------------------------------------------------

            # loop over input parameters
            for k in range(n_par_picks):
                stats = []
                # loop over parameters
                #				if len(Result[i].shape) ==1 : print(i)
                for j in range(Result[i].shape[1]):

                    #------------------------------------------------------------
                    # determines the type of the parameter
                    if j == 0: par_type = 'mass'
                    else:
                        par_type = ['px', 'ra', 'dec', 'pmra', 'pmdec'][j % 5]
                    if j > 5:
                        par_type = par_type + '_star' + str(int((j - 1) / 5))
                    else:
                        par_type = par_type + '_lens'
                    #------------------------------------------------------------

                    #------------------------------------------------------------
                    # calculate the statistics
                    par_result = (percentile(Result[i][(n_per_mass*k): \
                      (n_per_mass * (k+1)),j]))
                    par_init = (np.mean(
                        MC_Results['Input_parameter'][i][n_per_mass *
                                                         k:n_per_mass *
                                                         (k + 1), j]))
                    accuracy = par_result[-1] / par_init
                    #------------------------------------------------------------

                    #------------------------------------------------------------
                    #create an Histogramm
                    if plot:
                        #------------------------------------------------------------
                        # get Folder
                        if len(Events) == 22:
                            Folder = 'Multi_All'
                            idd = ''
                        elif len(Events) == 19:
                            idd = ''
                            Folder = 'Multi_Fivepar'
                        elif len(Events) == 14:
                            idd = ''
                            Folder = 'Multi_Sigma'
                        elif len(Events) > 100:
                            Folder = 'Single'
                            # counter for multiple events of the same lens
                            idd = '_%i' % int(
                                np.where(np.where(
                                    Names == Names[i])[0] == i)[0])
                        else:
                            Folder = ''
                            # counter for multiple events of the same lens
                            idd = '_%i' % int(
                                np.where(np.where(
                                    Names == Names[i])[0] == i)[0])
                        #------------------------------------------------------------

                        #------------------------------------------------------------
                        # creates Plot
                        if j == 0:
                            if vary_par:
                                Namestring = Names[i] + idd + '_%3f' % par_init

                            else:
                                Namestring = Names[i] + idd

                            Plots.PlotHistSingle(Result[i][n_per_mass * k: \
                             n_per_mass * (k+1),j],par_result,par_init, Folder, \
                             string + '_' + Namestring)
                        #------------------------------------------------------------

                    #------------------------------------------------------------
                    # save statistics for each parameter in a list
                    stats.append(
                        (i, par_type, par_init, *par_result, accuracy))
                stats_mass.append(stats)
            Eventnames.append(Names[i])
            #------------------------------------------------------------

            #------------------------------------------------------------
            # stor multiple results for each set of input parameters
            if vary_par:
                stats_all.append(stats_mass)
            else:
                stats_all.append(stats)
            #------------------------------------------------------------

    return stats_all, Eventnames, Event_out
示例#6
0
def create_Table(Analysis_result, Analysis_result5, Analysis_result_external = None, sort_epoch =False):
	'''------------------------------------------------------------
		Description: 
	---------------------------------------------------------------
		Input:
	---------------------------------------------------------------
		Output:
	------------------------------------------------------------'''
	tab = None
	lines = []
	epoch = []
	rel_err = []
	lens5 = np.array([i[0].getId() for i in Analysis_result5[2]])
	source5 = np.array([i[1].getId() for i in Analysis_result5[2]])
	if Analysis_result_external is not None:
		lens_ext = np.array([i[0].getId() for i in Analysis_result_external[2]])
		source_ext = np.array([i[1].getId() for i in Analysis_result_external[2]])
		external = True
	else:
		external = False
	for i in range(len(Analysis_result[0])):
		lens = Analysis_result[2][i][0]
		s_ID1 = str(lens.getId())
		s_name = name(lens.getId(), False, True)
		Good_ = name_good(lens.getId())

		if Good_ and lens.getMag() > 5:
			s_count = '%s'%i
			source = Analysis_result[2][i][1]
			s_ID2 = str(source.getId())
			s_TCA = '%.3f'%lens.getTca()
			s_Mass = '%.2g'%lens.getMass()

			if source.getPx() == 0: fivepar = '^{*}'
			else: fivepar =''
			mm_10 = np.array([Analysis_result[0][i][x][0][6] for x in range(len(Analysis_result[0][i]))\
				if Analysis_result[0][i][x][0][4] != -999])
			mp_10 = np.array([Analysis_result[0][i][x][0][7] for x in range(len(Analysis_result[0][i]))\
				if Analysis_result[0][i][x][0][4] != -999])
			mmm_10 = percentile(mm_10)
			ppp_10 = percentile(mp_10)
			delta_M_10_p = max(ppp_10[4],-mmm_10[3])
			delta_M_10_m = min(ppp_10[3],-mmm_10[4])  
			delta_M_10 = max(ppp_10[1], -mmm_10[1])
			c=0
			while 10**(-c)>= delta_M_10 and c < 10:
					c+=1
			c+=1
			s_delta_M_10_m = str(round(delta_M_10_m-0.49999999*10**(-c),c))	
			s_delta_M_10_p = str(round(delta_M_10_p+0.49999999*10**(-c),c))
			s_delta_M_10 = str(round(delta_M_10+0.49999999*10**(-c),c))
			while(len(s_delta_M_10_m) <= c):  s_delta_M_10_m=s_delta_M_10_m+'0'
			while(s_delta_M_10_m[-c-1]) != '.': s_delta_M_10_m=s_delta_M_10_m+'0'
			while(len(s_delta_M_10_p) <= c):  s_delta_M_10_p=s_delta_M_10_p+'0'
			while(s_delta_M_10_p[-c-1]) != '.': s_delta_M_10_p=s_delta_M_10_p+'0'
			while(len(s_delta_M_10) <= c):  s_delta_M_10=s_delta_M_10+'0'
			while(s_delta_M_10[-c-1]) != '.': s_delta_M_10=s_delta_M_10+'0'
			s_delta_M_percent = '%.2g'%(delta_M_10/lens.getMass()*100)
			s_delta_M_5_m = 'NONE'
			s_delta_M_5_p = 'NONE'
			s_delta_M_5 = 'NONE'	
			delta_M_5_test = 0
			delta_M_5_p = -999
			delta_M_5_m = -999
			delta_M_5 = -999
			which5 =  np.where((lens5 == lens.getId()) & (source5 == source.getId()))[0]
			if len(which5)  == 1 : 
				k = which5[0]
				mm_5 = np.array([Analysis_result5[0][k][x][0][6] for x in range(len(Analysis_result5[0][k]))\
					if Analysis_result5[0][k][x][0][4] != -999])
				mp_5 = np.array([Analysis_result5[0][k][x][0][7] for x in range(len(Analysis_result5[0][k]))\
					if Analysis_result5[0][k][x][0][4] != -999])
				mmm_5 = percentile(mm_5)
				ppp_5 = percentile(mp_5)
				delta_M_5_p = max(ppp_5[4],-mmm_5[3])
				delta_M_5_m = min(ppp_5[3],-mmm_5[4])  
				delta_M_5 = max(ppp_5[1], -mmm_5[1])
				delta_M_5_test = delta_M_5
				c=0
				while 10**(-c)>= delta_M_5 and c < 10:
						c+=1
				c+=1
				s_delta_M_5_m = str(round(delta_M_5_m-0.49999999*10**(-c),c))	
				s_delta_M_5_p = str(round(delta_M_5_p+0.49999999*10**(-c),c))
				s_delta_M_5 = str(round(delta_M_5+0.49999999*10**(-c),c))
				while(len(s_delta_M_5_m) <= c):  s_delta_M_5_m=s_delta_M_5_m+'0'
				while(s_delta_M_5_m[-c-1]) != '.': s_delta_M_5_m=s_delta_M_5_m+'0'
				while(len(s_delta_M_5_p) <= c):  s_delta_M_5_p=s_delta_M_5_p+'0'
				while(s_delta_M_5_p[-c-1]) != '.': s_delta_M_5_p=s_delta_M_5_p+'0'
				while(len(s_delta_M_5) <= c):  s_delta_M_5=s_delta_M_5+'0'
				while(s_delta_M_5[-c-1]) != '.': s_delta_M_5=s_delta_M_5+'0'
			

			if external:
				s_delta_M_ext_m = 'NONE'
				s_delta_M_ext_p = 'NONE'
				s_delta_M_ext = 'NONE'	
				delta_M_ext_test = 0
				which_ext =  np.where((lens_ext == lens.getId()) & (source_ext == source.getId()))[0]
				if len(which_ext)  == 1 : 
					k = which_ext[0]
					mm_ext = np.array([Analysis_result_external[0][k][x][0][6]\
						for x in range(len(Analysis_result_external[0][k])) \
						if Analysis_result_external[0][k][x][0][4] != -999])
					mp_ext = np.array([Analysis_result_external[0][k][x][0][7] \
						for x in range(len(Analysis_result_external[0][k])) \
						if Analysis_result_external[0][k][x][0][4] != -999])
					mmm_ext = percentile(mm_ext)
					ppp_ext = percentile(mp_ext)
					delta_M_ext_p = max(ppp_ext[4],-mmm_ext[3])
					delta_M_ext_m = min(ppp_ext[3],-mmm_ext[4])  
					delta_M_ext = max(ppp_ext[1], -mmm_ext[1])
					delta_M_ext_test = delta_M_ext
					c=0
					while 10**(-c)>= delta_M_ext and c < 10:
							c+=1
					c+=1
					s_delta_M_ext_m = str(round(delta_M_ext_m-0.49999999*10**(-c),c))	
					s_delta_M_ext_p = str(round(delta_M_ext_p+0.49999999*10**(-c),c))
					s_delta_M_ext = str(round(delta_M_ext+0.49999999*10**(-c),c))
					while(len(s_delta_M_ext_m) <= c):  s_delta_M_ext_m=s_delta_M_ext_m+'0'
					while(s_delta_M_ext_m[-c-1]) != '.': s_delta_M_ext_m=s_delta_M_ext_m+'0'
					while(len(s_delta_M_ext_p) <= c):  s_delta_M_ext_p=s_delta_M_ext_p+'0'
					while(s_delta_M_ext_p[-c-1]) != '.': s_delta_M_ext_p=s_delta_M_ext_p+'0'
					while(len(s_delta_M_ext) <= c):  s_delta_M_ext=s_delta_M_ext+'0'
					while(s_delta_M_ext[-c-1]) != '.': s_delta_M_ext=s_delta_M_ext+'0'
					s_delta_M_percent_ext = '%.2g'%(delta_M_ext/lens.getMass()*100)

				#---------------------------
				#create Astropy.table
				if tab is None:
					tab  = Table(names = ['name', 'lens_id', 'source_id', 'fivepar', 'TCA', 'Mass',\
						'deltaM_10','sigma_deltaM_10_+', 'sigma_deltaM_10_-',\
						'deltaM_5' , 'sigma_deltaM_5_+','sigma_deltaM_5_-',\
						'deltaM_ext' , 'sigma_deltaM_ext_+','sigma_deltaM_ext_-'],\
						dtype = [object, np.int64, np.int64, np.bool_,np.float64, np.float64, np.float64, np.float64,\
						np.float64, np.float64, np.float64, np.float64, np.float64, np.float64, np.float64]) 
				tab.add_row([s_name,lens.getId(),source.getId(),source.getPx() != 0,lens.getTca(),lens.getMass(),\
					delta_M_10, delta_M_10_p, delta_M_10_m,\
					delta_M_5, delta_M_5_p, delta_M_5_m,\
					delta_M_ext, delta_M_ext_p, delta_M_ext_m])			
				#---------------------------

				#---------------------------
				#element list for sorting 
				epoch.append(lens.getTca())
				rel_err.append(delta_M_10/lens.getMass())
				#---------------------------

				#---------------------------
				#create row in latex table 
				# \\(int\\) & \\(Name\\) & \\(ID1\\) & \\(ID2\\) & \\(T_CA\\) & \\(M_in\\)
				# & \\(deltaM_plus^{sigma+}_{sigma-}\\) & \\(deltaM_minus^{sigma+}_{sigma-}  \\) \\\\
				line = '%s & \\(%s\\) & \\(%s%s\\) & \\(%s\\) & \\(%s\\) & \\(\\pm%s^{+%s}_{%s}\\)  & \\(%s%s\\)'\
						%(s_name,s_ID1, s_ID2,fivepar, s_TCA, s_Mass,\
							s_delta_M_10, s_delta_M_10_p, s_delta_M_10_m, s_delta_M_percent,'\\%')
				if s_delta_M_5_m == 'NONE' or  delta_M_5_test > lens.getMass():
					line = line + ' & '
				else:
					line = line + ' & \\(\\pm%s^{+%s}_{%s}\\)'% (s_delta_M_5, s_delta_M_5_p, s_delta_M_5_m)
				if s_delta_M_ext_m == 'NONE' or  delta_M_ext_test > lens.getMass():
					line = line + ' & '
				else:
					
					line = line + ' & \\(%s%s\\)'%(s_delta_M_percent_ext,'\\%')
				lines.append(line) 

				#---------------------------
				
			else:
				#---------------------------
				#create Astropy.table
				if tab is None:
					tab  = Table(names = ['name', 'lens_id', 'source_id', 'fivepar', 'TCA', 'Mass',\
							'deltaM_10', 'sigma_deltaM_10_+', 'sigma_deltaM_10_-',\
							'deltaM_5' , 'sigma_deltaM_5_+','sigma_deltaM_5_-'],\
							dtype = [object, np.int64, np.int64, np.bool_,np.float64, np.float64, np.float64,\
							np.float64, np.float64, np.float64, np.float64, np.float64]) 
				tab.add_row([s_name,lens.getId(),source.getId(),source.getPx() != 0,lens.getTca(),lens.getMass(),\
					delta_M_10, delta_M_10_p, delta_M_10_m, delta_M_5, delta_M_5_p, delta_M_5_m])			
				#---------------------------
				#element in list for sorting 
				epoch.append(lens.getTca())
				rel_err.append(delta_M_10/lens.getMass())
				#---------------------------

				#---------------------------
				#create row in latex table 
				#'\\(int\\) & \\(Name\\) & \\(ID1\\) & \\(ID2\\) & \\(T_CA\\) & \\(M_in\\) 
				#& \\(deltaM_plus^{sigma+}_{sigma-}\\) & \\(deltaM_minus^{sigma+}_{sigma-}  \\) \\\\'
				line = '%s & \\(%s\\) & \\(%s%s\\) & \\(%s\\) & \\(%s\\) & \\(\\pm%s^{+%s}_{%s}\\)  & \\(%s%s\\)'\
						%(s_name,s_ID1, s_ID2,fivepar, s_TCA, s_Mass, s_delta_M_10, s_delta_M_10_p, s_delta_M_10_m, s_delta_M_percent,'\\%')
				if s_delta_M_5_m == 'NONE' or  delta_M_5_test > lens.getMass():
					line = line + ' & '
				else:
					line = line + ' & \\(\\pm%s^{+%s}_{%s}\\)'% (s_delta_M_5, s_delta_M_5_p, s_delta_M_5_m)
				line = line + ' \\\\'
				lines.append(line) 
	
	#---------------------------
	#sorting list		
	if sort_epoch:	
		lines = [x for _,x in sorted(zip(epoch,lines))]
		rel_err = [x for _,x in sorted(zip(epoch,rel_err))]
		epoch = [x for _,x in sorted(zip(epoch,epoch))]
	else:
		lines = [x for _,x in sorted(zip(rel_err,lines))]
		epoch = [x for _,x in sorted(zip(rel_err,epoch))]
		rel_err = [x for _,x in sorted(zip(rel_err,rel_err))]
	#---------------------------

	#---------------------------
	#save Table
	if external: tablename = 'result_single_ext'
	else: tablename = 'result_single'
	print('write table: ' + tablename+ '.vot')
	print('write tex_table: ' + tablename+ '.txt')
	tab.write(path + 'Data/' + tablename+ '.vot', format = 'votable',overwrite=True)
	#---------------------------
	
	#---------------------------
	#writing Latex Table
	f = open(paperpath+tablename+'.tex','w')
	
	#Setup a Table 
	f.write('\\renewcommand*{\\arraystretch}{1.4}'+'\n')
	f.write('\\begin{table*}[]' +'\n')
	f.write('\\input{result_single_caption1}\n') # include caption
	f.write('\\label{table:single}' +'\n')
	f.write('\\tiny')
	
	#Format and head Row
	f.write('\\begin{tabular}{l|rrrrr|rr|r|}' +'\n')
	f.write('\\# & Name-Lens & \\(DR2\\_ID\\)-Lens & \\(DR2\\_ID\\)-Source & \\(T_{CA}\\) & \\(M_{in}\\)')
	f.write(' & \\(\\sigma\\,M_{10}\\) & \\(\\sigma\\,M_{10}/M_{in}\\) & \\(\\sigma\\,M_{5}  \\)')
	f.write(' \\\\\n')
	f.write('  & & & & \\(\\mathrm{Jyear}\\) & \\(\\mathrm{M_{\\odot}}\\)')
	f.write(' & \\(\\mathrm{M_{\\odot}}\\) & & \\(\\mathrm{M_{\\odot}}\\)')
	#if external: f.write(' & \\(\\mathrm{M_{\\odot}}\\) \\\\\n')
	f.write(' \\\\\n')
	f.write('\\hline' +'\n')
	
	#
	c3 = 1 #row counter combined
	c4 = 0 #row counter individual tables


	if sort_epoch: #sorted by epoch
		#write rows
		for l in range(len(lines)):
			if (epoch[l] <= 2019.5) & (rel_err[l] <=0.5): 
				if c4%10> 4: f.write('\\rowcolor{lightGray}\n')	
				ll = lines[l]
				if external:
					ll = ll.split('&')
					ll.pop(-1)
					ll = '&'.join(ll)
				f.write('\\(%d\\) & '%c3 + ll+'\\\\\n')
				c3 +=1
				c4+=1
		#Table end		
		f.write('\\hline' +'\n')
		f.write('\\end{tabular}' + '\n' + '\\end{table*}')

		#Setup second Table
		f.write('\n'+'\n'+'\n'+'\n'+'\n'+'\\begin{table*}[]' +'\n')
		f.write('\\input{result_single_caption2}\n')
		f.write('\\label{table:single2}' +'\n')
		f.write('\\tiny')

		#Format and head Row
		f.write('\\begin{tabular}{l|rrrrr|rr|r|}' +'\n')
		f.write('\\# & Name-Lens & \\(DR2\\_ID\\)-Lens & \\(DR2\\_ID\\)-Source & \\(T_{CA}\\) & \\(M_{in}\\)')
		f.write(' & \\(\\sigma\\,M_{10}\\) & \\(\\sigma\\,M_{10}/M_{in}\\)')
		if external: f.write(' & \\(\\sigma\\,M_{obs}/M_{in}\\)\\\\\n')
		else: f.write(' & \\(\\sigma\\,M_{5}\\) \\\\\n')
		f.write('  & & & & \\(\\mathrm{Jyear}\\) & \\(\\mathrm{M_{\\odot}}\\)')
		if external: f.write(' & \\(\\mathrm{M_{\\odot}}\\) & & \\\\\n')
		else: f.write(' & \\(\\mathrm{M_{\\odot}}\\) & & \\(\\mathrm{M_{\\odot}}\\)\\\\\n')
		f.write('\\hline' +'\n')

		
		c4 = 0 #Row counter individual tables
		
		#Write rows
		for l in range(len(lines)):
			if (epoch[l] > 2019.5) & (rel_err[l] <=0.5): 
				if c4%10> 4: f.write('\\rowcolor{lightGray}\n')	
				ll=lines[l]
				if external:
					ll = ll.split('&')
					ll.pop(-2)
					ll = '&'.join(ll)
				f.write('\\(%d\\) & '%c3 + ll+'\\\\\n')
				c3 +=1
				c4+=1

		#Table end		
		f.write('\\hline' +'\n')
		f.write('\\end{tabular}' + '\n' + '\\end{table*}')	
		f.close()
	

	else:
		i = 0
		c2 = [15,30,50,100,101,200]
		for line in lines: 
			if c2[i]<= 50:
				if c4%10> 4: f.write('\\rowcolor{lightGray}\n')	
				f.write('\\(%d\\) & '%c3 + line+'\\\\\n')
				c3 +=1
				c4+=1
			if c < len(rel_err):
				if rel_err[c]*100 > c2[i]:
					while rel_err[c]*100 > c2[i]:
						i+=1  
					if c2[i] == 50: 
						f.write('\\hline' +'\n')
						f.write('\\end{tabular}' + '\n' + '\\end{table*}')
						f.write('\n'+'\n'+'\n'+'\n'+'\n'+'\\begin{table*}[]' +'\n')
						f.write('\\input{result_single_caption2}\n')
						f.write('\\label{table:single2}' +'\n')
						f.write('\\tiny')
						if external: f.write('\\begin{tabular}{l|rrrrr|rr|r|r|}' +'\n')
						else: f.write('\\begin{tabular}{l|rrrrr|rr|r|}' +'\n')
						f.write('\\# & Name-Lens & \\(DR2\\_ID\\)-Lens & \\(DR2\\_ID\\)-Source')
						f.write(' & \\(T_{CA}\\) & \\(M_{in}\\)')
						f.write(' & \\(\\sigma\\,M_{10}\\) & \\(\\sigma\\,M_{10}/M_{in}\\) & \\(\\sigma\\,M_{5}  \\)')
						if external: f.write(' & \\(\\sigma\\,M_{obs}  \\) \\\\\n')
						else: f.write(' \\\\\n')
						f.write('  & & & & \\(\\mathrm{Jyear}\\) & \\(\\mathrm{M_{\\odot}}\\) \\\\\n')
						f.write(' & \\(\\mathrm{M_{\\odot}}\\) & & \\(\\mathrm{M_{\\odot}}\\) \\\\\n')
						f.write('\\hline' +'\n')
						c4 = 0 

			if c2[i] >= 100: break
			c+=1
		f.write('\\hline' +'\n')
		f.write('\\end{tabular}' + '\n' + '\\end{table*}')	
		f.close()
示例#7
0
def create_Table_multi(Analysis_multi_result):
	'''------------------------------------------------------------
		Description: 
	---------------------------------------------------------------
		Input:
	---------------------------------------------------------------
		Output:
	------------------------------------------------------------'''
	Dat_single, Dat_all, Dat_fps, Dat_sig = Analysis_multi_result
	
	q1 = []
	q2 = []



	for i in range(len(Dat_all[1])):
		lens = Dat_all[2][i][0]
		s_Mass = '%.2g'%lens.getMass()
		lens_id = Dat_all[1][i]
		s_lens_id = str(lens_id)
		s_name = name(lens.getId(), False, True)
		all_bool = True
		fps_bool = False
		sig_bool = False
		for j in range(len(Dat_fps[1])):
			if lens_id == Dat_fps[1][j]: 
				fps_bool = True
				break

		for k in range(len(Dat_sig[1])):
			if lens_id == Dat_sig[1][k]: 
				sig_bool = True
				break

		Vs_delta_M_m = ['','','']
		Vs_delta_M_p = ['','','']
		Vs_delta_M = ['','','']
		rel_err = ['','','']
		for uu in range(3):
			if [all_bool, fps_bool, sig_bool][uu]:
				nn = [i,j,k][uu]
				Analysis_result = [Dat_all, Dat_fps, Dat_sig][uu]

				mm = np.array([Analysis_result[0][nn][x][0][6] for x in range(len(Analysis_result[0][nn])) if Analysis_result[0][nn][x][0][4] != -999])
				mp = np.array([Analysis_result[0][nn][x][0][7] for x in range(len(Analysis_result[0][nn])) if Analysis_result[0][nn][x][0][4] != -999])
				mmm = percentile(mm)
				ppp = percentile(mp)
				delta_M_p = max(ppp[4],-mmm[3])
				delta_M_m = min(ppp[3],-mmm[4])  
				delta_M = max(ppp[1], -mmm[1])
				c=0
				while 10**(-c)>= delta_M and c < 10:
						c+=1
				c+=1
				s_delta_M_m = str(round(delta_M_m-0.49999999*10**(-c),c))	
				s_delta_M_p = str(round(delta_M_p+0.49999999*10**(-c),c))
				s_delta_M = str(round(delta_M+0.49999999*10**(-c),c))
				while(len(s_delta_M_m) <= c):  s_delta_M_m=s_delta_M_m+'0'
				while(s_delta_M_m[-c-1]) != '.': s_delta_M_m=s_delta_M_m+'0'
				while(len(s_delta_M_p) <= c):  s_delta_M_p=s_delta_M_p+'0'
				while(s_delta_M_p[-c-1]) != '.': s_delta_M_p=s_delta_M_p+'0'
				while(len(s_delta_M) <= c):  s_delta_M=s_delta_M+'0'
				while(s_delta_M[-c-1]) != '.': s_delta_M=s_delta_M+'0'
				Vs_delta_M_m[uu] = s_delta_M_m
				Vs_delta_M_p[uu] = s_delta_M_p
				Vs_delta_M[uu] = s_delta_M
			if uu ==0:
				rel_err1 = (delta_M/lens.getMass())
			rel_err[uu] = '%.2g'%(delta_M/lens.getMass()*100)

	# sortiere Source ID into 4 
		ID_all = [x.getId() for x in Dat_all[2][i][1:]]
		ID_fps = [x.getId() for x in Dat_fps[2][j][1:]]
		ID_sig = [x.getId() for x in Dat_sig[2][k][1:]]
		tps = []
		fps = []
		sig = []
		for ID in  ID_all:
			if ID in ID_sig: sig.append(ID)
			elif ID in ID_fps: fps.append(ID)
			else: tps.append(ID)

		q1.append([rel_err1,rel_err,s_Mass, s_lens_id,s_name, tps,fps ,sig, Vs_delta_M_m,Vs_delta_M_p,Vs_delta_M])
	q1.sort(key=lambda x: x[0])
		
	f = open(paperpath+'result_multi.tex','w')
	f.write('\\renewcommand*{\\arraystretch}{1.4}' + '\n')
	f.write('\\begin{sidewaystable*}[]' + '\n')
	f.write('\\input{result_multi_caption1}\n')
	f.write('\\label{table:multi}'+ '\n')
	f.write('\\tiny\\begin{tabular}{l|r|rrrr|rr|rr|rr|}' + '\n')
	f.write('\\# & Name-Lens & ' + '\n')
	f.write('\\(DR2\\_ID\\)-Source & \\(DR2\\_ID\\)-Source' )
	f.write(' & \\(DR2\\_ID\\)-Source' + '\n')
	f.write(' & \\(M_{\mathrm{in}}\\) & \\(\\sigma\\,M_{\mathrm{all}}\\)')
	f.write(' & \\(\\sigma\\,M_{\mathrm{all}}/M_{\mathrm{in}}\\) & \\(\\sigma\\,M_{\mathrm{5-par}}  \\)')
	f.write(' & \\(\\sigma\\,M_{\mathrm{5-par.}}/M_{in}\\)  & \\(\\sigma\\,M_{\mathrm{sig.}}  \\)')
	f.write(' & \\(\\sigma\\,M_{\mathrm{sig.}}/M_{\mathrm{in}}\\)  \\\\' + '\n')
	f.write(' & \\(DR2\\_ID\\)-Lens & (2-parameter) & (5-parameter)& (sigma)')
	f.write(' &  \\(\\Msun{}\\) & \\(\\Msun{}\\) & \\(\\%\\)& \\(\\Msun{}\\) & \\(\\%\\)& \\(\\Msun{}\\) & \\(\\%\\)')
	f.write(' \\\\\n')
	f.write('\\hline' +'\n')

	for n  in range(len(q1)):

		rel_err1,rel_err, s_Mass, s_lens_id,s_name,  tps,fps,sig,  Vs_delta_M_m,Vs_delta_M_p,Vs_delta_M =q1[n]
		if rel_err1 < 0.5:
			ii = 0
			if s_name == '' :ll = ['\\(%s\\)'% s_lens_id,]
			else: ll = [s_name, '\\(%s\\)'% s_lens_id]
			for line_index in range(max([len(tps), len(fps), len(sig),len(ll)])):
				if line_index >= len(tps): s_tps = ''
				else: s_tps = str(tps[line_index])
				if line_index >= len(fps): s_fps = ''
				else: s_fps = str(fps[line_index])
				if line_index >= len(sig): s_sig = ''
				else: s_sig = str(sig[line_index])
				if line_index >= len(ll): s_ln = ''
				else: s_ln= str(ll[line_index])

				if line_index == 0: 
					line = '%s &  %s  & \\(%s\\) & \\(%s\\) & \\(%s\\)  & \\(%s\\)  &'\
						%(str(n+1), s_ln, s_tps,s_fps, s_sig, s_Mass)
					if len(tps) == 0: line = line + ' & &' 
					else: line = line+ '\\(\\pm%s^{+%s}_{%s}\\) & \\(%s%s\\) &' \
						% (Vs_delta_M[0], Vs_delta_M_p[0], Vs_delta_M_m[0], rel_err[0], '\\%')
					if len(fps) == 0: line = line + ' & &'
					else:	
						line = line+ '\\(\\pm%s^{+%s}_{%s}\\) &\\(%s%s\\) &' \
						% (Vs_delta_M[1], Vs_delta_M_p[1], Vs_delta_M_m[1], rel_err[1],'\\%')
					if len(sig) == 0: line = line + ' \\\\'
					else:	
						line = line+ '\\(\\pm%s^{+%s}_{%s}\\) & \\(%s%s\\)  \\\\'\
							% (Vs_delta_M[2], Vs_delta_M_p[2], Vs_delta_M_m[2], rel_err[2],'\\%')
					 
				else: 
					line = ' &%s& \\(%s\\) & \\(%s\\) & \\(%s\\)  & & & & & & & \\\\'\
						%  (s_ln, s_tps,s_fps, s_sig)

				if n%2 : f.write('\\rowcolor{lightGray}\n')	
				f.write(line)
				f.write('\n')
			f.write('\\hline'+ '\n')


	f.write('\\end{tabular}' +'\n' +'\\end{sidewaystable*}')
	f.close()