def MAKE_model_dictionary(cat, filters, clobbermodel=False):
    """
    Create model dictionary for all redshifts in z-array and filters
    input 
        cat - catalog settings
        filters - filter settings
        clobbermodel - remove any existing dictionary (default - False)
    ouput
        modelsdict
    """
    
    t0= time.time()

    if clobbermodel and os.path.lexists(cat['dict_path']):
        print "removing model dictionary "+cat['dict_path']
        os.system('rm -rf '+ cat['dict_path'])
        
    if not os.path.lexists(cat['dict_path']):

        MODELFILES.construct(cat['path'])

        mydict = MODELSDICT(cat['dict_path'], cat['path'], filters)
        mydict.build()
        
        print '_____________________________________________________'
        print 'For this dictionary creation %.2g min elapsed'% ((time.time() - t0)/60.)

    Modelsdict = cPickle.load(file(cat['dict_path'], 'rb'))
    
    return Modelsdict
Exemple #2
0
def MAKE_model_dictionary(cat, filters, clobbermodel=False):
    """
    Create model dictionary for all redshifts in z-array and filters
    input 
        cat - catalog settings
        filters - filter settings
        clobbermodel - remove any existing dictionary (default - False)
    ouput
        modelsdict
    """

    t0 = time.time()

    if clobbermodel and os.path.lexists(cat['dict_path']):
        print "removing model dictionary " + cat['dict_path']
        os.system('rm -rf ' + cat['dict_path'])

    if not os.path.lexists(cat['dict_path']):

        MODELFILES.construct(cat['path'])

        mydict = MODELSDICT(cat['dict_path'], cat['path'], filters)
        mydict.build()

        print '_____________________________________________________'
        print 'For this dictionary creation %.2g min elapsed' % (
            (time.time() - t0) / 60.)

    Modelsdict = cPickle.load(file(cat['dict_path'], 'rb'))

    return Modelsdict
def RUN_AGNfitter_onesource_independent(cat, line, data_obj, filtersz, clobbermodel=False):
    """
    Main function for fitting a single source in line 'line' and create it's modelsdict independently.
    """
    
    mc = MCMC_settings()
    out = OUTPUT_settings()

    data = DATA(data_obj,line)

    print ''
    print 'Fitting sources from catalog: ', data.catalog 
    print '- Sourceline: ', line
    print '- Sourcename: ', data.name


    ## 0. CONSTRUCT DICTIONARY for this redshift
    t0= time.time()

    # needs a list/array of z
    filtersz['dict_zarray'] = [data.z]

    # save the dictionary for this source in the OUTPUT folder for this source
    # create this source output folder if it doesn't exist
    if not os.path.lexists(cat['output_folder'] +'/'+str(data.name)):
        os.system('mkdir -p ' + cat['output_folder'] +'/'+str(data.name))
    dictz = cat['output_folder'] +'/'+str(data.name) +'/MODELSDICT_' + str(data.name) 
    # remove this source modelsdict if it already exists and we want to remove it
    if clobbermodel and os.path.lexists(dictz):
        os.system('rm -rf '+dictz)
        print "removing source model dictionary "+dictz
      
    if not os.path.lexists(dictz):
        zdict = MODELSDICT(dictz, cat['path'], filtersz)
        Modelsdictz = zdict.build(save=False)
        print '_____________________________________________________'
        print 'For this dictionary creation %.2g min elapsed'% ((time.time() - t0)/60.)
    else:
        Modelsdictz = cPickle.load(file(dictz, 'rb')) 

    data.DICTS(filtersz, Modelsdictz)


    P = parspace.Pdict (data)   # Dictionary with all parameter space especifications.
                                # From PARAMETERSPACE_AGNfitter.py

    t1= time.time()

    MCMC_AGNfitter.main(data, P, mc)        
    print 'fitting took %.2g min'% ((time.time() - t1)/60.)
    
    t2= time.time()
    PLOTandWRITE_AGNfitter.main(data,  P,  out)
    print 'plotting took %.2g min '% ((time.time() - t2)/60.)


    print '_____________________________________________________'
    print 'Processing this source took %.2g min '% ((time.time() - t0)/60.)
    return
def RUN_AGNfitter_onesource_independent( line, data_obj, filtersz, models, clobbermodel=False):
    """
    Main function for fitting a single source in line and create it's modelsdict independently.
    """
    
    mc = MCMC_settings()
    out = OUTPUT_settings()
    data = DATA(data_obj,line)

    print ''
    print '________________________'    
    print 'Fitting sources from catalog: ', data.catalog 
    print '- Sourceline: ', line
    print '- Sourcename: ', data.name


    ## 0. CONSTRUCT DICTIONARY for this redshift
    t0= time.time()

    ## needs a list/array of z
    filtersz['dict_zarray'] = [data.z]

    ## save the dictionary for this source in the OUTPUT folder for this source
    ## create this source output folder if it doesn't exist
    if not os.path.lexists(cat['output_folder']+str(data.name)):
        os.system('mkdir -p ' + cat['output_folder'] +str(data.name))
    dictz = cat['output_folder'] +str(data.name) +'/MODELSDICT_' + str(data.name) 
    ## remove this source modelsdict if it already exists and we want to remove it
    if clobbermodel and os.path.lexists(dictz):
        os.system('rm -rf '+dictz)
        print "removing source model dictionary "+dictz
      
    if not os.path.lexists(dictz):
        zdict = MODELSDICT(dictz, cat['path'], filtersz, models)
        zdict.build()
        f = open(zdict.filename, 'wb')
        cPickle.dump(zdict, f, protocol=2)
        f.close()
        print '_____________________________________________________'
        print 'For this dictionary creation %.2g min elapsed'% ((time.time() - t0)/60.)
    else:
        zdict = cPickle.load(file(dictz, 'rb'))
    Modelsdictz = zdict.MD

    data.DICTS(filtersz, Modelsdictz)

    P = parspace.Pdict (data)   # Dictionary with all parameter space especifications.
                                # From PARAMETERSPACE_AGNfitter.py

    t1= time.time()

    MCMC_AGNfitter.main(data, P, mc)        
    PLOTandWRITE_AGNfitter.main(data,  P,  out, models)


    print '_____________________________________________________'
    print 'For this fit %.2g min elapsed'% ((time.time() - t1)/60.)
    return
def MAKE_model_dictionary(cat, filters, models, clobbermodel=False):
    """
    Create model dictionary for all redshifts in z-array and filters
    input 
        cat - catalog settings
        filters - filter settings
        clobbermodel - remove any existing dictionary (default - False)
    ouput
        modelsdict
    """
    
    t0= time.time()
    modelsdict_name = cat['path']+models['path']+ filters['filterset']+models['modelset']

    if clobbermodel and os.path.lexists(modelsdict_name) : ## If overwriting of model dictionary mode is on
        print "> Overwriting model dictionary (-o) "+modelsdict_name
        print '_______________________'
        os.system('rm -rf '+ modelsdict_name)
  
    if not os.path.lexists(modelsdict_name): ## If model dictionary does not exist yet

        print '> Constructing new MODELS DICTIONARY:'
        print 'SAVED AS : ', modelsdict_name
        print 'FILTERSET USED : ', filters['filterset']
        print '________________________'
        print 'This process might take some time but you have to do it only once.'
        print 'If you interrupt it, please trash the empty file created.'
        print ''

        mydict = MODELSDICT( modelsdict_name, cat['path'], filters, models)
        mydict.build()
        f = open(mydict.filename, 'wb')
        cPickle.dump(mydict, f, protocol=2)
        f.close()

        print '________________________'
        print 'The models dictionary ' + modelsdict_name +' has been created.'\
              '(%.2g min elapsed)'% ((time.time() - t0)/60.)

    else: ## If model dictionary exists and you want to reuseit

        mydict = cPickle.load(file(modelsdict_name, 'rb'))

        print '________________________'
        print 'MODELS DICTIONARY currently in use:'
        print 'SAVED AS : ', mydict.filename
        print 'FILTERSET USED : ', mydict.filterset_name
        
        test_settingschanges= [mydict.filters_list[i]==filters[i]  for i in filters.keys() if  type(filters[i]) is BooleanType]

        if False in test_settingschanges : ##  compare nr of data bands with n of model filters
            print '________________________'
            print '*** WARNING ***'  
            print 'You have changed entries in your FILTER_settings without updating the filterset name.'
            print 'The MODEL DICTIONARY will NOT be updated correctly.'
            print ''
            py3 = sys.version_info[0] > 2 #creates boolean value for test that Python major version > 2
            if py3:
              response = input(">> You want to continue with the old model dictionary nevertheless? (yes/no)")
            else:
              response = raw_input(">> You want to continue with the old model dictionary nevertheless? (yes/no)")
            while True:
                if response== 'no':
                    sys.exit('\nTo update the MODEL DICTIONARY , you have these options: \n\
                        * change the filterset name (filters["filterset"]), or \n\
                        * run the code in model-overwriting mode (-o)')
                elif response== 'yes':
                    print '________________________'
                    print 'MODELS DICTIONARY currently in use:'
                    print 'SAVED AS : ', mydict.filename
                    print 'FILTERSET USED : ', mydict.filterset_name
                    break
                else:
                    print "Please answer (yes/no)"
                    break

    Modelsdict = mydict.MD ## MD is the model dictionary saved as a class of the method MODELSDICT

    bands_in_cat =len(cat['freq/wl_list'])
    bands_in_dict = len(Modelsdict[Modelsdict.keys()[0]][1][Modelsdict[Modelsdict.keys()[0]][1].keys()[0]][0])

    if bands_in_cat!= bands_in_dict :

        sys.exit('________________________\n*** ERROR ***\n'+ \
                'Number of bands in catalog ('+str(bands_in_cat)+'), does not match the numbers of filters in models (' + str(bands_in_dict)+')\n'+\
                'This will produce a mismatched fitting. Make sure the filterset contains only/all the photometric bands corresponding your catalog.\n'+ \
                'They do NOT need to be sorted in the same order.')
        
    return Modelsdict