示例#1
0
def glass_renv(**kwargs):
    """
    Call to set up a standard glass 'read' environment

    Args:
        None

    Kwargs:
        threads <int> - run with given number of threads
        no_window <bool> - suppress any graphics

    Return:
        glass <module> - the glass environment as a module
    """
    try:
        _, arglist = getopt.getopt(sys.argv[1:], 't:h:f', ['nw'])
    except getopt.GetoptError:
        arglist = []
    Environment.global_opts['ncpus_detected'] = _detect_cpus()
    Environment.global_opts['ncpus'] = 1
    Environment.global_opts['omp_opts'] = _detect_omp()
    Environment.global_opts['withgfx'] = True
    Environment.global_opts['argv'] = arglist
    Commands.set_env(Environment())
    if 'threads' in kwargs and kwargs['threads'] >= 1:
        Environment.global_opts['ncpus'] = kwargs['threads']
    if 'no_window' in kwargs:
        Environment.global_opts['withgfx'] = kwargs['no_window']
    import glass.glcmds
    import glass.scales
    if Environment.global_opts['withgfx']:
        import glass.plots
    glass_basis('glass.basis.pixels', solver=None)
    glass.basis.pixels.priors.exclude_all_priors()
    return glass
示例#2
0
def run(*args, **kwargs):
    """
    Call to run a GLASS config file within a standard glass 'write' environment

    Args:
        args <str/tuple(str)> - input files for GLASS, e.g. '../src/glass/Examples/B1115.gls'

    Kwargs:
        threads <int> - run with given number of threads
        no_window <bool> - suppress any graphics

    Return:
        None
    """
    Environment.global_opts['ncpus_detected'] = _detect_cpus()
    Environment.global_opts['ncpus'] = 1
    Environment.global_opts['omp_opts'] = _detect_omp()
    Environment.global_opts['withgfx'] = True
    Commands.set_env(Environment())
    if 'thread' in kwargs and kwargs['threads'] > 1:
        Environment.global_opts['ncpus'] = kwargs['threads']
    if 'no_window' in kwargs:
        Environment.global_opts['withgfx'] = kwargs['no_window']

    import glass.glcmds
    import glass.scales
    if Environment.global_opts['withgfx']:
        import glass.plots

    with open(args[0], 'r') as f:
        Commands.get_env().input_file = f.read()

    Environment.global_opts['argv'] = args

    try:
        execfile(args[0])
    except GLInputError as e:
        raise e(
            "An error occurred! Figure out whats wrong and give it another go..."
        )
示例#3
0
文件: glcmds.py 项目: RafiKueng/glass
def new_env(env):
    Commands.set_env(Environment())
示例#4
0
文件: glass.py 项目: jpcoles/glass
def help():
    print >> sys.stderr, "Usage: glass.py <input>"
    sys.exit(2)


if __name__ == "__main__":

    if len(sys.argv) < 2: help()

    Environment.global_opts['ncpus_detected'] = _detect_cpus()
    Environment.global_opts['ncpus'] = 1
    Environment.global_opts['omp_opts'] = _detect_omp()
    Environment.global_opts['withgfx'] = True

    Commands.set_env(Environment())

    optlist, arglist = getopt.getopt(sys.argv[1:], 't:h', ['nw'])

    for opt in optlist:
        if opt[0] == '-h':
            help()
        elif opt[0] == '-t':
            ncpus = int(opt[1])
            assert ncpus > 0
            #Commands.get_env().ncpus = ncpus
            Environment.global_opts['ncpus'] = ncpus
        elif opt[0] == '--nw':
            #Commands.get_env().withgfx = False
            Environment.global_opts['withgfx'] = False
示例#5
0
    f = __import__(name, globals(), locals())
    for name, [f, g, help_text] in Commands.glass_command_list.iteritems():
        if name in __builtins__.__dict__:
            message = 'WARNING: Glass command {:s} ({:s}) overrides previous function {:s}'
            print(message.format(name, f, __builtins__.__dict__[name]))
        __builtins__.__dict__[name] = g


if __name__ == "__main__":

    app = 'app.py'
    Environment.global_opts['ncpus_detected'] = _detect_cpus()
    Environment.global_opts['ncpus'] = 1
    Environment.global_opts['omp_opts'] = _detect_omp()
    Environment.global_opts['withgfx'] = True
    Commands.set_env(Environment())

    import glass.glcmds
    import glass.scales
    if Environment.global_opts['withgfx']:
        import glass.plots

    glass_basis('glass.basis.pixels', solver=None)
    exclude_all_priors()

    Environment.global_opts['argv'] = [app] + sys.argv[1:]
    opts = Environment.global_opts['argv']
    states = [loadstate(f) for f in opts[1:]]

    root, zapper = Zapp.init(gls_states=states, verbose=1)
    os.system(
示例#6
0
def runGLASS(self, GLASSconfig, config):

    import matplotlib as mpl
    mpl.use('Agg') # make sure this is above the other mpl imports

    from matplotlib import pyplot as plt
    import pylab as pl

    
    
    this = self
    hist = []
    
#    def status(*args):
#        if len(args)<3:
#            while len(args)<3:
#                args.append(0)
#        hist.append(args)
#        update_status(hist)
#    
#    def status2(lst):
#    # this is hack to fix the complicated stuff in glass.
#    # I don't know why I put it there.. possibly its better to have the history here
#        print "\n", "--"*40
#        print lst
#        print "--"*40
#        status(lst[-1])
    
    def update_status(hist):
        
        currStatus = hist[-1]

        if not this.request.called_directly:
            this.update_state(state='PROGRESS', meta={
                'text': currStatus[0],
                'i'   : currStatus[1],
                'of'  : currStatus[2],
                'hist': json.dumps(hist)
            })

        #print ">> UPDATE_STAT: %-20s: %04i of %04i" % tuple(currStatus)

    if DEBUG:
        print '='*80
        print "got GLASSconfig:\n"
        for k, v in GLASSconfig.items():
            print '%-16s : %s' % (k,v)
        print '='*80
        print "got config:\n"
        for k, v in config.items():
            print '%-16s : %s' % (k,v)
        print '='*80


    figpath = '/tmp/spaghettilens/'
    name = str(config['model_id']) # we don't like unicode here for some reason...
    path = os.path.join(figpath, name)

    try: # this prevents a race condition
        os.makedirs(path)
    except OSError:
        if not os.path.isdir(path):
            raise



    # don't take this outside, because only the worker has glass installed,
    # the webserver doesn't
    # so there will be import errors

    from glass.environment import env, Environment
    from glass.command import command, Commands
    from glass.exmass import PointMass
    from glass.exceptions import GLInputError
    from glass.log import Status, setup_status_reporter


    @command('Load a glass basis set')
    def glass_basis(env, name, **kwargs):
        #print __builtins__
        env.basis_options = kwargs
        f = __import__(name, globals(), locals())
        for name,[f,g,help_text] in Commands.glass_command_list.iteritems():
            if __builtins__.has_key(name):
                print 'WARNING: Glass command %s (%s) overrides previous function %s' % (name, f, __builtins__[name])
            __builtins__[name] = g


    #update_status({'text':'', 'progress':(0,0)})   
    setup_status_reporter([update_status])
    Status('init')
    
    Environment.global_opts['ncpus_detected'] = 2
    Environment.global_opts['ncpus'] = 2
    Environment.global_opts['omp_opts'] = {} #_detect_omp()
    Environment.global_opts['withgfx'] = True
    
    Commands.set_env(Environment())
    
    if Environment.global_opts['withgfx']:
        import glass.plots 
    
    import glass.glcmds
    import glass.scales
    

    GC = GLASSconfig
    C = config
    
    logfile = os.path.join(path, "glass.log")


    glass_basis('glass.basis.pixels', solver='rwalk')
    meta(author=GC['author'], notes=GC['notes'])
    setup_log(logfile, stdout=True)
    samplex_random_seed(GC['random_seed'])
#    samplex_acceptance(rate=0.25, tol=0.15)
    samplex_acceptance(**GC['samplex_acceptance'])
    
    if GC['exclude_all_priors']:
        exclude_all_priors()

#    include_prior(
#        'lens_eq', 
#        'time_delay', 
#        'profile_steepness', 
#        'J3gradient', 
#        'magnification',
#        'hubble_constant',
#        'PLsmoothness3',
#        'shared_h',
#        'external_shear'
#    )
    include_prior(*GC['include_priors'])

#    hubble_time(13.7)
#    globject('B1115+080')

    hubble_time(GC['hubbletime'])
    globject(C['model_id']) 

    
#    zlens(0.31)  
#    pixrad(6)
#    steepness(0,None)
#    smooth(2,include_central_pixel=False)
#    local_gradient(45)
#    shear(0.01)
    zlens(GC['z_lens'])  
    pixrad(GC['pixrad'])
    steepness(*GC['steepness'])
    smooth(GC['smooth_val'],include_central_pixel=GC['smooth_ic'])
    local_gradient(GC['loc_grad'])
    shear(GC['shear'])
    hires(GC['hires'])
    
    if GC['isSym']:
        symm()
        
        
        
#    
#    A =  0.3550,  1.3220 
#    B = -0.9090, -0.7140
#    C = -1.0930, -0.2600
#    D =  0.7170, -0.6270
#    
#    source(1.722,   A,'min', 
#                    B,'min', 13.3,
#                    C,'sad', None,
#                    D,'sad', 11.7)

    #print GC['source']
    source(*GC['source'])

    for i, exm in enumerate(GC['ext_masses']):
        external_mass(PointMass(exm['x'],exm['y'],name='PM%02i'%i), (0, exm['m']))

                    
#    update_status({'text':'started modelling', 'progress':(0,0)})   
    Status('started modelling')   

    model(GC['n_models']) #, update_hook=status2)
    
#    update_status({'text':'create files', 'progress':(0,5)})   
    Status('create files', 0,5)   
    
    savestate(os.path.join(path, 'state.glass'))
#    update_status({'text':'create files', 'progress':(1,5)})   
    Status('create files', 1,5)   

    env().make_ensemble_average()
    env().arrival_plot(env().ensemble_average, only_contours=True, colors='magenta', clevels=40)
    env().overlay_input_points(env().ensemble_average)
    pl.gca().axes.get_xaxis().set_visible(False)
    pl.gca().axes.get_yaxis().set_visible(False)
    pl.savefig(os.path.join(path, 'img1.png'))
    pl.close()
#    update_status({'text':'create files', 'progress':(2,5)})   
    Status('create files', 2,5)   


    env().kappa_plot(env().ensemble_average, 0, with_contours=True, clevels=20, vmax=1, with_colorbar=False)
    pl.gca().axes.get_xaxis().set_visible(False)
    pl.gca().axes.get_yaxis().set_visible(False)
    pl.savefig(os.path.join(path, 'img2.png'))
    pl.close()
#    update_status({'text':'create files', 'progress':(3,5)})   
    Status('create files', 3,5)   
    
    env().srcdiff_plot(env().ensemble_average)
    env().overlay_input_points(env().ensemble_average)
    pl.gca().axes.get_xaxis().set_visible(False)
    pl.gca().axes.get_yaxis().set_visible(False)
    pl.savefig(os.path.join(path, 'img3.png'))
    pl.close()
#    update_status({'text':'create files', 'progress':(4,5)})   
    Status('create files', 4,5)   
    
    env().srcdiff_plot_adv(env().ensemble_average, night=True, upsample=8)
    env().overlay_input_points(env().ensemble_average)
    pl.savefig(os.path.join(path, 'img3_ipol.png'), facecolor='black', edgecolor='none')
    pl.close()
#    update_status({'text':'create files', 'progress':(5,5)})
    Status('create files', 5,5)   


    # upload
    files = [
        'state.glass',
        'img1.png',
        'img2.png',
        'img3.png',
        'img3_ipol.png',
    ]
    host = config['upload_host']
    user = config['upload_user']
    destpath = config['upload_dest']

    # TODO: if this woker runs on the web server, ssh connection failes
    # solutions:
    # have a dedicated user for uploading the files, OR
    # code in a check, and on server use simple file transfer method
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, username=user)
    scp = SCPClient(ssh.get_transport())
    
#    update_status({'text':'upload files', 'progress':(0,5)})
    Status('upload files', 0, 6)

    for i, f in enumerate(files):
        scp.put(os.path.join(path, f), os.path.join(destpath, f))
        #update_status({'text':'upload files', 'progress':(i+1,5)})
        Status('upload files', i+1, 6)

    # upload log file
    scp.put(logfile, os.path.join(destpath, 'glass.log'))
    os.remove(logfile) # delete logfile
    Status('upload files', i+1, 6)
  
    # ...
    #
    # model(1000, update_hook=update_status)
    # 
    
#    for i in range(40):
#        print i
#        time.sleep(0.5)
#        if not self.request.called_directly:
#            self.update_state(state='PROGRESS', meta={'solutions': ( i, 40)})

    Status('finished')    
    
    return {}






######
# DEMO
##@shared_task
#@app.task(bind=True)
#def add(self, x, y):
#
#    print 0
#    
#    time.sleep(1)    
#    print 1
#
#    if not self.request.called_directly:
#        self.update_state(state='PROGRESS', meta={'current': 0, 'total': 2})
#
#
#    time.sleep(1)    
#    print 2
#
#    if not self.request.called_directly:
#        self.update_state(state='PROGRESS', meta={'current': 1, 'total': 2})
#
#    time.sleep(1)    
#    print 3
#
#    
#    if not self.request.called_directly:
#        self.update_state(state='PROGRESS', meta={'current': 2, 'total': 2})
#        
#    return x + y
示例#7
0
文件: glass.py 项目: jpcoles/glass

def help():
    print >>sys.stderr, "Usage: glass.py <input>"
    sys.exit(2)

if __name__ == "__main__":

    if len(sys.argv) < 2: help()

    Environment.global_opts['ncpus_detected'] = _detect_cpus()
    Environment.global_opts['ncpus'] = 1
    Environment.global_opts['omp_opts'] = _detect_omp()
    Environment.global_opts['withgfx'] = True

    Commands.set_env(Environment())

    optlist, arglist = getopt.getopt(sys.argv[1:], 't:h', ['nw'])

    for opt in optlist:
        if   opt[0] == '-h':
            help()
        elif opt[0] == '-t':
            ncpus = int(opt[1])
            assert ncpus > 0
            #Commands.get_env().ncpus = ncpus
            Environment.global_opts['ncpus'] = ncpus
        elif opt[0] == '--nw':
            #Commands.get_env().withgfx = False
            Environment.global_opts['withgfx'] = False
示例#8
0
    for name,[f,g,help_text] in Commands.glass_command_list.iteritems():
        if __builtins__.has_key(name):
            print 'WARNING: Glass command %s (%s) overrides previous function %s' % (name, f, __builtins__.__dict__[name])
        __builtins__[name] = g




#if len(sys.argv) < 2: help()

Environment.global_opts['ncpus_detected'] = 2
Environment.global_opts['ncpus'] = 2
Environment.global_opts['omp_opts'] = {} #_detect_omp()
Environment.global_opts['withgfx'] = True

Commands.set_env(Environment())

if Environment.global_opts['withgfx']:
    import glass.plots 

import glass.glcmds
import glass.scales
#import pytipsy 

#with open(arglist[0], 'r') as f:
    #Commands.get_env().input_file = f.read()

#Environment.global_opts['argv'] = arglist
##Commands.get_env().argv = arglist

示例#9
0
def new_env(env):
    Commands.set_env(Environment())
示例#10
0
def env():
    #return _env
    return Commands.get_env()