Exemplo n.º 1
0
def get_parameters(ini):
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(ini)

    pheno = config['capture']['phenomenon']
    duration = config['capture']['duration']
    fps = int(config['capture']['fps'])
    f_tmp = config['capture']['f_tmp']
    v_name = config['capture']['video']
    import pictures as pic
    try:
        url = config['capture']['from_url']
        method = pic.from_url
        inp = url
    except KeyError:
        dev = config['capture']['webcam']
        method = pic.webcam
        inp = dev
    except:
        ## TODO add usage
        print('no method')

    pheno = clean(pheno)
    duration = clean(duration)
    return pheno, duration, fps, method, inp, f_tmp, v_name
Exemplo n.º 2
0
 def __init__(self, args, extra_args):
     config = ConfigParser()
     config._interpolation = configparser.ExtendedInterpolation()
     config.read(args.config_file)
     if extra_args:
         extra_args = dict([(k[2:], v) for k, v in zip(extra_args[0::2], extra_args[1::2])])
     for section in config.sections():
         for k, v in config.items(section):
             if k in extra_args:
                 v = type(v)(extra_args[k])
                 config.set(section, k, v)
     config.set('Run', 'gpu', args.gpu)
     config.set('Run', 'gpu_count', args.gpu_count)
     if args.train:
         config.set('Run', 'run_num', args.run_num)
         config.set('Run', 'train_batch_size', args.batch_size)
         config.set('Network', 'helper', args.helper)
         config.set('Network', 'generator', args.gen)
         config.set('Network', 'discriminator', args.dis)
         config.set('Data', 'split_radio', args.split)
         self._config = config
         if not os.path.isdir(self.save_dir):
             os.makedirs(self.save_dir)
         config.write(open(self.config_file, 'w'))
     print('Loaded config file sucessfully.')
     self._config = config
     for section in config.sections():
         for k, v in config.items(section):
             print(k, v)
Exemplo n.º 3
0
def default_config() -> ConfigParser:
    # print(CONFIG_FILE)
    config = ConfigParser()
    config._interpolation = configparser.ExtendedInterpolation()
    config['DEFAULT']['resources_path'] = str(RESOURCE_DIR.absolute())
    config.read(str(CONFIG_FILE))
    return config
Exemplo n.º 4
0
def read_params(fname='config.ini'):
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)
    N = int(config['parameters']['N'])
    t0 = int(config['parameters']['t0'])  # Record every t0 seconds
    f = expanduser(config['parameters']['f_data'])
    return N, t0, f
Exemplo n.º 5
0
def load(fname='config.ini'):
    """
   Load the config options and return it as a class
   """
    LG.info(f'Loading config file: {fname}')
    if not os.path.isfile(fname): return None
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)

    # Date and time options
    try:
        LG.debug('Trying to read start/end dates')
        start_date = config['run']['start_date']
        start_date = dt.datetime.strptime(start_date, '%d/%m/%Y-%H:%M')
        end_date = config['run']['end_date']
        end_date = dt.datetime.strptime(end_date, '%d/%m/%Y-%H:%M')
        LG.info(f'Run for: {start_date} - {end_date}')
    except KeyError:
        LG.debug('Start/end dates not specified, going for now+days_run')
        days_run = int(config['run']['days_run'])
        start_date = dt.datetime.now()
        start_date = start_date.replace(hour=8,
                                        minute=0,
                                        second=0,
                                        microsecond=0)
        end_date = start_date + dt.timedelta(days=days_run)
        LG.info(f'Run for: {start_date} - {end_date}')

    try:
        hours_mask = config['run']['daily_hours'].split(',')  #XXX TODO
        hours_mask = [
            dt.time(*tuple(map(int, t.split(':')))) for t in hours_mask
        ]
    except KeyError:
        LG.warning('Hours mask (daily_hours) not provided! Using 0-24')
        hours_mask = [dt.time(0), dt.time(23)]

    # Domain
    domains_run = tuple(map(int, config['run']['domains'].split(',')))
    domain_folder = expanduser(config['run']['domain_folder'])
    GFS_data_folder = expanduser(config['run']['GFS_data_folder'])
    output_folder = expanduser(config['run']['output_folder'])
    plots_folder = expanduser(config['run']['plots_folder'])
    leftlon = float(config['run']['leftlon'])
    rightlon = float(config['run']['rightlon'])
    toplat = float(config['run']['toplat'])
    bottomlat = float(config['run']['bottomlat'])

    # System
    Ncores = int(config['run']['Ncores'])
    wait4batch = float(config['run']['wait4batch'])

    R = RunParams(start_date, end_date, hours_mask, domains_run, domain_folder,
                  GFS_data_folder, output_folder, plots_folder, leftlon,
                  rightlon, toplat, bottomlat, Ncores, wait4batch)

    return R
Exemplo n.º 6
0
def load(fname='config.ini'):
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)

    server = config['backup']['server']
    user = config['backup']['user']
    port = int(config['backup']['port'])
    folder = expanduser(config['backup']['target_folder'])
    return Server(server, user, port, folder)
Exemplo n.º 7
0
def load(fname):
    if not os.path.isfile(fname): return None
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)
    folder = expanduser(config['sys']['folder'])
    folder_html = expanduser(config['sys']['folder_html'])
    areas = config['web']['areas']
    sections = config['web']['sections']
    return Configuration(folder, folder_html, areas, sections)
Exemplo n.º 8
0
Arquivo: util.py Projeto: B4dWo1f/RUN
def get_outfolder(fname):
    """
   Load the config options and return it as a class
   """
    # LG.info(f'Loading config file: {fname}')
    # if not os.path.isfile(fname): return None
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)
    wrfout_folder = expanduser(config['run']['output_folder'])
    plots_folder = expanduser(config['run']['plots_folder'])
    return wrfout_folder, plots_folder
Exemplo n.º 9
0
def parse_arguments(args):
    config = ConfigParser()
    config._interpolation = configparser.ExtendedInterpolation()
    config.optionxform = str
    config.read(args.config)

    if args.option is not None:
        config.set('params', 'option', args.option)
    option = config.get('params', 'option')

    if args.gpu:
        config.set('params', 'gpu', args.gpu)
    if args.ckpt_noise_param_idx:
        config.set('params', 'ckpt_noise_param_idx',
                   ' '.join(args.ckpt_noise_param_idx))
    if args.scene:
        config.set('params', 'scene', ' '.join(args.scene))
    if args.naive:
        config.set('Upsample8xDenoise', 'naive', args.naive)

    # read all values from config file
    opt = {}
    opt['gpu'] = config.get('params', 'gpu')

    opt['ckpt_noise_param_idx'] = \
        config.get('params', 'ckpt_noise_param_idx').split()
    opt['ckpt_noise_param_idx'] = \
        [int(idx) for idx in opt['ckpt_noise_param_idx']]
    if not opt['ckpt_noise_param_idx']:
        opt['ckpt_noise_param_idx'] = np.arange(1, 11)

    opt['option'] = config.get('params', 'option')
    opt['scene'] = config.get('params', 'scene').split()
    if not opt['scene']:
        opt['scene'] = scenenames
    opt['scenesizes'] = dict(config.items('SceneSizes'))
    opt['checkpoint'] = []

    if option != 'Upsample8xDenoise':
        for n in opt['ckpt_noise_param_idx']:
            opt['checkpoint'].append(config.get(option,
                                     'ckpt_noise_param_{}'.format(n)))
    else:
        opt['naive'] = int(config.get('Upsample8xDenoise', 'naive'))
        if opt['naive']:
            opt['checkpoint_msgnet'] = config.get(option, 'ckpt_msgnet')
            opt['checkpoint'].append(config.get(option, 'ckpt_noise_param_10'))
        else:
            opt['checkpoint'].append(
                config.get(option, 'ckpt_finetune_noise_param_10'))
    return opt
Exemplo n.º 10
0
Arquivo: load.py Projeto: B4dWo1f/FORE
def setup(fname='config.ini'):
    """
    Parse an ini file returning the parameter classes
    TODO: Log this function
          try/except for format errors
   """
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)

    fmt = config['config']['fmt']
    folder_data = expanduser(config['sys']['folder_data'])
    if folder_data[0] != '/': folder_data = f'{here}/{folder_data}'
    stations = expanduser(config['sys']['stations'])
    stations = path_setter(stations)
    # if stations[0] != '/': stations = f'{here}/{stations}'
    url_root = config['aemet']['url_root']
    url_base = config['aemet']['url_base']
    return params(folder_data, stations, fmt, url_root, url_base)
Exemplo n.º 11
0
def parse_arguments(args):
    config = ConfigParser()
    config._interpolation = configparser.ExtendedInterpolation()
    config.optionxform = str
    config.read(args.config)

    if args.option is not None:
        config.set('params', 'option', args.option)
    option = config.get('params', 'option')

    if args.gpu:
        config.set('params', 'gpu', args.gpu)
    if args.ckpt_noise_param_idx:
        config.set('params', 'ckpt_noise_param_idx',
                   ' '.join(args.ckpt_noise_param_idx))

    # read all values from config file
    opt = {}
    opt['gpu'] = config.get('params', 'gpu')
    
    if args.option is not None:
        config.set('params', 'option', args.option)
    option = config.get('params', 'option')
    opt['model_name'] = config.get(option, 'model_name')

    opt['ckpt_noise_param_idx'] = int(config.get('params', 'ckpt_noise_param_idx'))
    if not opt['ckpt_noise_param_idx']:
        opt['ckpt_noise_param_idx'] = np.arange(1, 11)

    opt['option'] = config.get('params', 'option')

    opt['checkpoint'] = []
    opt['checkpoint'].append(config.get(option, \
        'ckpt_noise_param_{}'.format(opt['ckpt_noise_param_idx'])))

    opt['test_files'] = config.get(option, 'test_files')
    opt['out_datapath'] = config.get(option, 'out_datapath')
    opt['spad_datapath'] = config.get(option, 'spad_datapath')
    opt['mono_datapath'] = config.get(option, 'mono_datapath')
    opt['matrices_out'] = config.get(option, 'matrices_out')

    return opt
Exemplo n.º 12
0
    def __init__(self, config_file=None):
        super(Configurations, self).__init__()

        if config_file is None:
            config_file = './Configs/config.cfg'

        config = ConfigParser()

        config._interpolation = configparser.ExtendedInterpolation()
        config.read(filenames = config_file)

        self.config = config
        self.config_path = config_file
        #self.add_section = 'Additional'
        print('Loaded Config File Successfully ...')

        # print configurations
        for section in self.config.sections():
            for k, v in self.config.items(section):
                print(k, ":", v)
Exemplo n.º 13
0
    def __init__(self):
        self.mylogger.info(msg="Starttime is {}".format(time.time()))
        config = ConfigParser()
        config._interpolation = ExtendedInterpolation()
        '''Resource based loading option.'''
        #resource = resource_stream(Requirement.parse('imgprocessor==0.01'),'config.cfg').read()
        #resource = resource.decode('UTF-8','replace')
        #print(resource)
        #config.read_string(resource)
        '''get executionpath'''
        dirname = os.path.dirname(os.path.realpath(__file__))
        config.read(os.path.join(dirname, "config.cfg"))

        loglevel = config.get(section="config", option="loglevel")
        self.mylogger.setLevel(level=loglevel)

        self.mylogger.info(msg="Dirname is {}".format(dirname))

        #print(config.keys())
        #print(config.sections())

        self.mylogger.info(
            msg="Dirname is in {}".format(os.path.join(dirname, "config.cfg")))
        '''Initialize all the configurations like screensizes, bandconfig etc'''
        screenconfig = config.get(section="config", option="screensize")
        bandconfig = config.get(section="config", option="band")
        scaleconfig = config.get(section="config", option="scale")
        self.defaultscreen = config.get(section="config",
                                        option="defaultscreen")
        self.defaultband = config.get(section="config",
                                      option="defaultbandwidth")
        self.imagepath = config.get(section="config", option="path")
        #self.mylogger.setLevel(config.get(section="config",option="loglevel"))
        self.screens = dict(
            screenmix.split(",") for screenmix in screenconfig.split(";"))
        self.bandwidth = dict(
            band.split(",") for band in bandconfig.split(";"))
        self.scales = dict(
            scale.split(",") for scale in scaleconfig.split(";"))
        self.mylogger.info(msg="Done initialization")
Exemplo n.º 14
0
def setup(fname='config.ini'):
    """ Parse the ini file and return a Params class """
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)

    P = Params()
    P.default()

    token_file = expanduser(config['bot']['token'])
    if token_file[0] != '/': token_file = here + '/' + token_file
    P.token_file = token_file

    users_db = expanduser(config['system']['users_db'])
    if users_db[0] != '/': users_db = here + '/' + users_db
    P.users_db = users_db

    whitelist = expanduser(config['system']['whitelist'])
    if whitelist[0] != '/': whitelist = here + '/' + whitelist
    P.whitelist = whitelist

    log_file = config['system']['log']
    if log_file.lower() == 'auto':
        log_file = here + '/' + HOSTNAME + '.log'
    else:
        log_file = expanduser(log_file)
        if log_file[0] != '/': log_file = here + '/' + log_file
    P.log_file = log_file

    pid_file = config['system']['pid']
    if pid_file.lower() == 'auto':
        pid_file = here + '/' + HOSTNAME + '.pid'
    else:
        pid_file = expanduser(pid_file)
        if pid_file[0] != '/': pid_file = here + '/' + pid_file
    P.pid_file = pid_file
    with open(P.pid_file, 'w') as f:
        f.write(f'{os.getpid()}\n')
    return P
Exemplo n.º 15
0
 def __init__(self, config_file, extra_args, isTrain=True):
     config = ConfigParser()
     config._interpolation = configparser.ExtendedInterpolation()
     config.read(config_file)
     if extra_args:
         extra_args = dict([
             (k[2:], v) for k, v in zip(extra_args[0::2], extra_args[1::2])
         ])
     for section in config.sections():
         for k, v in config.items(section):
             if k in extra_args:
                 v = type(v)(extra_args[k])
                 config.set(section, k, v)
     self._config = config
     if not os.path.isdir(self.save_dir):
         os.makedirs(self.save_dir)
     if isTrain:
         config.write(open(self.config_file, 'w'))
     print('Loaded config file sucessfully.')
     for section in config.sections():
         for k, v in config.items(section):
             print(k, v)
Exemplo n.º 16
0
def get_properties(fname, section):
    """
   Load the config options and return it as a class
   """
    LG.info(f'Loading config file: {fname} for section {section}')
    # if not os.path.isfile(fname): return None
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)
    #XXX We shouldn't use eval
    factor = float(eval(config[section]['factor']))
    vmin = float(eval(config[section]['vmin']))
    vmax = float(eval(config[section]['vmax']))
    delta = float(eval(config[section]['delta']))
    try:
        levels = config[section]['levels'].replace(']', '').replace('[', '')
        levels = list(map(float, levels.split(',')))
    except KeyError:
        levels = []
    levels = [float(l) for l in levels]
    cmap = config[section]['cmap']
    units = config[section]['units']
    return factor, vmin, vmax, delta, levels, cmap, units
Exemplo n.º 17
0
    def __init__(self, config_file=None):
        super(MyConfiguration, self).__init__()

        # ./ 当前目录
        if config_file is None:
            config_file = './configs/config.cfg'

        config = ConfigParser()
        # 插补值方法
        config._interpolation = configparser.ExtendedInterpolation()
        config.read(filenames=config_file)

        self.config = config
        self.config_path = config_file
        self.add_section = 'Additional'
        print("Loaded config file successfully ...")
        #print(len(self.config.sections()))
        for section in self.config.sections():
            for k, v in self.config.items(section):
                print(k, ":", v)

        # TODO make save dir

        config.write(open(config_file, 'w'))
Exemplo n.º 18
0
    def generate(self,
                 filename,
                 ssize="1080",
                 band="*",
                 width=0,
                 height=0,
                 forcesize=False):

        try:
            '''To avoid issues with zerolength params, default such items'''
            if ssize == "None" or len(str(ssize)) == 0: ssize = "1080"
            if band == "None" or len(str(band)) == 0: band = "*"
            '''if weight is given but height not given or other way around'''
            width = int(width)
            height = int(height)

            #if (width >0 and height==0) : height = width
            #if (height>0 and width==0): width = height

            self.mylogger.info(msg="Starttime is {}".format(time.time()))
            config = ConfigParser()
            config._interpolation = ExtendedInterpolation()
            '''get executionpath'''
            dirname = os.path.dirname(os.path.realpath(__file__))

            config.read(os.path.join(dirname, "config.cfg"))
            '''set logger level from config'''
            loglevel = config.get(section="config", option="loglevel")
            self.mylogger.setLevel(level=loglevel)

            self.mylogger.info(config.sections())
            self.mylogger.info(msg="Dirname is in {}".format(
                os.path.join(dirname, "config.cfg")))
            '''Initialize all the configurations like screensizes, bandconfig etc'''
            screenconfig = config.get(section="config", option="screensize")
            bandconfig = config.get(section="config", option="band")
            dynamicqualityconfig = config.get(section="config",
                                              option="dynamicqualityscore")
            bandqualityconfig = config.get(section="config",
                                           option="bandqualityscore")
            scaleconfig = config.get(section="config", option="scale")
            defaultscreen = config.get(section="config",
                                       option="defaultscreen")
            defaultband = config.get(section="config",
                                     option="defaultbandwidth")

            imagepath = config.get(section="config", option="path")
            '''Populate all the dict values from configuration'''
            self.screens = dict(
                screenmix.split(",") for screenmix in screenconfig.split(";"))
            self.bandwidth = dict(
                band.split(",") for band in bandconfig.split(";"))
            self.dynamicqualityvalues = dict(
                quality.split(",")
                for quality in dynamicqualityconfig.split(";"))
            self.bandqualityvalues = dict(
                quality.split(",") for quality in bandqualityconfig.split(";"))
            self.scales = dict(
                scale.split(",") for scale in scaleconfig.split(";"))

            if (width > 0 and height > 0): self.iswidthheightset = True
            self.mylogger.info(msg="Done initialization")

            imgfile = BytesIO()
            self.mylogger.info('Generating file {}'.format(filename))
            '''Expected name of the file to be generated'''
            '''A consistent logic is used to generate name of image'''
            savefilename = self._createimageid(
                filename.split(".")[0], ssize, band, width,
                height) + filename.split(".")[1]

            savefilename = os.path.join(imagepath, savefilename)
            '''if filealready exist return file name'''
            if os.path.isfile(savefilename):
                img = Image.open(savefilename)
                img.save(imgfile, format="JPEG")
                '''encode it using base64 and return in asciformat'''
                #base64encode=  base64.encodebytes(imgfile.getvalue())
                #return base64encode.decode("ascii")
                return imgfile.getvalue()
            else:
                '''Open the file if it isnt there and resize, send back the path'''
                '''Check if input fullpath leads to file, if not throw exception'''
                fullpath = os.path.join(imagepath, filename)
                self.mylogger.info("Image {} is generated from path {}".format(
                    filename, fullpath))

                if not os.path.isfile(fullpath):
                    raise NameError('File not found')
                '''if fullpath is file, then open the same'''
                img = self._getimage(fullpath, ssize, band, width, height,
                                     forcesize)
                '''load from bytes too'''
                '''Sharpen by 1.5, detail out and set subsampling to 0'''
                sharp = ImageEnhance.Sharpness(img)
                img = sharp.enhance(1.5)
                img = img.filter(ImageFilter.DETAIL)
                img.save(imgfile,
                         format="JPEG",
                         subsampling=0,
                         quality=int(self.qualityscore))
                self.mylogger.info(
                    "Image {} is generated with force {}".format(
                        filename, forcesize))
                '''encode it using base64 and return in asciformat'''
                #base64encode = base64.encodebytes(imgfile.getvalue())
                #return base64encode.decode('ascii')
                return imgfile.getvalue()
        except NameError as filenotfound:
            raise NameError('File {} doesnt exist. Exception - {}'.format(
                filename, str(filenotfound)))
            self.mylogger.exception(
                "Exception occurred in image generation for {}".format(
                    filename))
        finally:
            self.mylogger.info(
                "Completed image for {} in finally".format(filename))
Exemplo n.º 19
0
tmpl_frontpage = Template(f.read())
f.close()

f = open('templates/frontpage-index-item.md', 'r')
tmpl_frontpage_index_item = Template(f.read())
f.close()

f = open('templates/frontpage-index-item-nohtml.md', 'r')
tmpl_frontpage_index_item_nohtml = Template(f.read())
f.close()

# == Read templates == #

# == Reading meta == #
meta = ConfigParser()
meta._interpolation = ExtendedInterpolation()
meta.read('meta.ini')
# == Read meta == #

index = ''

# == Generating pages == #
for style_name in ['default', 'structure']:
    f = open('themes/' + style_name + '.css')
    style = f.read()
    f.close()

    for root, subdirs, files in os.walk('html'):
        for f in files:
            if not f.endswith('.html') or f == 'index.html':
                continue
Exemplo n.º 20
0
def load_setting(section, name):
    config = ConfigParser()
    config._interpolation = ExtendedInterpolation()
    config.read("config.ini")
    name = config.get(section, name)
    return name
Exemplo n.º 21
0
    def generate(self, filename, ssize="1080", band="*"):

        try:
            self.mylogger.info(msg="Starttime is {}".format(time.time()))
            config = ConfigParser()
            config._interpolation = ExtendedInterpolation()
            '''Resource based loading option.'''
            #resource = resource_stream(Requirement.parse('imgprocessor==0.01'),'config.cfg').read()
            #resource = resource.decode('UTF-8','replace')
            #print(resource)
            #config.read_string(resource)
            '''get executionpath'''
            dirname = os.path.dirname(os.path.realpath(__file__))
            config.read(os.path.join(dirname, "config.cfg"))
            self.mylogger.info(msg="Dirname is {}".format(dirname))

            #print(config.keys())
            #print(config.sections())

            self.mylogger.info(msg="Dirname is in {}".format(
                os.path.join(dirname, "config.cfg")))
            '''Initialize all the configurations like screensizes, bandconfig etc'''
            screenconfig = config.get(section="config", option="screensize")
            bandconfig = config.get(section="config", option="band")
            qualityconfig = config.get(section="config", option="qualityscore")
            scaleconfig = config.get(section="config", option="scale")
            defaultscreen = config.get(section="config",
                                       option="defaultscreen")
            defaultband = config.get(section="config",
                                     option="defaultbandwidth")
            imagepath = config.get(section="config", option="path")
            screens = dict(
                screenmix.split(",") for screenmix in screenconfig.split(";"))
            bandwidth = dict(band.split(",") for band in bandconfig.split(";"))
            self.qualityvalues = dict(
                quality.split(",") for quality in qualityconfig.split(";"))
            self.scales = dict(
                scale.split(",") for scale in scaleconfig.split(";"))
            self.mylogger.info(msg="Done initialization")

            imgfile = BytesIO()
            self.mylogger.info('Generating file {}'.format(filename))
            '''Expected name of the file to be generated'''
            scale = self._getoptimizesizebasedonsizebandwidth(
                ssize, band, screens, bandwidth)
            #print("Scale is {}".format(scale))
            savefilename = filename.split(".")[0] + "_" + str(
                self.sumup) + "." + filename.split(".")[1]
            savefilename = os.path.join(imagepath, savefilename)
            '''if filealready exist return file name'''
            if os.path.isfile(savefilename):
                img = Image.open(savefilename)
                img.save(imgfile, format="JPEG")
                '''encode it using base64 and return in asciformat'''
                #base64encode=  base64.encodebytes(imgfile.getvalue())
                #return base64encode.decode("ascii")
                return imgfile.getvalue()
            else:
                '''Open the file if it isnt there and resize, send back the path'''
                '''Check if input fullpath leads to file, if not throw exception'''
                fullpath = os.path.join(imagepath, filename)
                if not os.path.isfile(fullpath):
                    raise NameError('File not found')
                '''if fullpath is file, then open the same'''
                img = Image.open(fullpath, 'r')
                self.mylogger.info("Size of image is {}".format(img.size))
                img.thumbnail((int(img.size[0] * float(scale)),
                               int(img.size[1] * float(scale))), Image.BICUBIC)
                '''saved locally'''
                #img.save(savefilename,format="JPEG")
                '''load from bytes too'''
                img.save(imgfile,
                         format="JPEG",
                         quality=int(self.qualityscore))
                '''encode it using base64 and return in asciformat'''
                #base64encode = base64.encodebytes(imgfile.getvalue())
                #return base64encode.decode('ascii')
                return imgfile.getvalue()

            self.mylogger.info("Saved file {} for {}".format(
                filename, savefilename))
        except NameError as filenotfound:
            raise NameError('File {} doesnt exist'.format(filename))
            self.mylogger.exception(
                "Exception occurred in image generation for {}".format(
                    filename))
        finally:
            self.mylogger.info("Completed")
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Create the RAS party database")
    parser.add_argument('-c', '--config',
                        metavar='<config-file-path>',
                        type=str,
                        help="Path to the DB configuration file",
                        required=True)

    parser.add_argument('-e', '--environment',
                        metavar='<environment-name>',
                        type=str,
                        help="Name of the config section to use",
                        required=True)

    args = parser.parse_args()

    config = ConfigParser()
    config._interpolation = ExtendedInterpolation()
    config.read(args.config)

    config = config[args.environment]

    logger = ons_logger.create(config)

    engine = create_engine(config.get('db_connection'), convert_unicode=True)

    if config.get('db_drop').lower() in ['true', 'yes']:
        drop(config, engine, logger)
    create(config, engine, logger)
Exemplo n.º 23
0
def setup(fname='SK1.ini'):
    """
    Parse an ini file returning the parameter classes
    TODO: Log this function
          try/except for format errors
   """
    config = ConfigParser(inline_comment_prefixes='#')
    config._interpolation = ExtendedInterpolation()
    config.read(fname)

    lzee = config['hamiltonian']['lzee'].replace(')', '').replace('(', '')
    lzee = np.array(list(map(float, lzee.split(','))))
    lSO = float(config['hamiltonian']['lSO'])
    lmass = float(config['hamiltonian']['lmass'])
    lelec = float(config['hamiltonian']['lelec'])
    HP = ham_param(lzee, lSO, lmass, lelec)

    bands = eval(config['calculations']['bands'].capitalize())
    spectrum = eval(config['calculations']['spectrum'].capitalize())
    try:
        DOS = eval(config['calculations']['dos'].capitalize())
    except NameError:
        DOS = config['calculations']['dos'].lower()
    DOS_local = eval(config['calculations']['local'].capitalize())
    Ndos = eval(config['calculations']['Ndos'])
    Nddos = eval(config['calculations']['Nddos'])
    Nkdos = eval(config['calculations']['Nkdos'])
    ns = int(config['calculations']['Ns'])
    nk = int(config['calculations']['nk'])
    CP = calc_param(bands=bands,
                    spectrum=spectrum,
                    DOS=DOS,
                    local=DOS_local,
                    ns=ns,
                    nk=nk,
                    ndos=Ndos,
                    nddos=Nddos,
                    nkdos=Nkdos)

    sys = config['system']['sys']
    n = int(config['system']['n'])
    l = int(config['system']['l'])
    xyz_file = config['system']['xyz_file']
    #xyz_dir = config['system']['xyz_dir']    #XXX future implementation
    pasivate = eval(config['system']['pasivate'].capitalize())
    dist = eval(config['system']['dist'])
    force0D = eval(config['system']['force0D'].capitalize())
    periodic = eval(config['system']['periodic'].capitalize())
    DOspin = eval(config['system']['DOspin'].capitalize())

    Nv = int(config['vacancy']['N'])
    d = eval(config['vacancy']['d'])
    alpha = float(config['vacancy']['alpha'])
    hollow = eval(config['vacancy']['hollow'])
    vp = vacancy_param(Nv, d, alpha, hollow=hollow)

    Na = int(config['adatom']['na'])
    sp3 = float(config['adatom']['sp3'])
    hollow = bool(config['adatom']['hollow'])
    ap = adatom_param(Na, sp3, hollow)

    ## System parameters
    SP = sys_param(xyz_file, pasivate, dist, vp, ap, DOspin, force0D, periodic)

    keys, values = [], []
    for key in config['atoms']:
        keys.append(key.capitalize())
        values.append(eval(config['atoms'][key]))
    atoms = dict(zip(keys, values))

    keys, values = [], []
    for key in config['hopping']:
        a = eval(config['hopping'][key])
        keys.append('-'.join(sorted(key.title().split('-'))))
        values.append(a)
    hoppings = dict(zip(keys, values))
    #### FIX of the hoppings
    #    C-C = {Vsss:...} ---> C-C = {1:{Vsss:...,Vsps:...}}
    for k, v in hoppings.items():
        #k should be a string the hopping type (C-C, C-H, Mo-S...)
        #v should be a dictionary with the SK hopping parameters of the form
        #   v = {1:{params...}, 2:{params}}
        try:
            for ik, iv in v.items():
                if type(ik) != int:
                    aux = {1: hoppings[k]}
                    hoppings[k] = aux
                    break
                else:
                    pass
        except AttributeError:
            pass
    HP.hoppings = hoppings
    #### FIX the spin doubling if needed but not requested
    if np.linalg.norm(HP.lzee) != 0. or HP.lSO != 0.:
        LG.warning('Need spin doubling but it wasn\'t requested')
        SP.DOspin = True

    tail = ''
    try:
        tail += '%sorb/' % len(atoms['C'])
    except KeyError:
        pass
    tail += config['system']['sys'] + '/'
    tail += 'n%s_l%s/' % (config['system']['n'], config['system']['l'])
    tail += 'nv%s_na%s/' % (Nv, Na)
    tail += 'd%s/' % (d)
    tail += 'alpha%s/' % (alpha)
    tail += 'e%s/' % (lelec)

    basedir = expanduser(config['I/O']['basedir'])
    if basedir[-1] != '/': basedir += '/'
    out = basedir + 'OUTS/' + tail
    slf = basedir + 'SELFEs/' + '/'.join(tail.split('/')[:-4]) + '/'
    ham = basedir + 'HAMILs/' + '/'.join(tail.split('/')[:-4]) + '/'
    FP = fol_param(out, slf, ham)

    return FP, HP, CP, SP, atoms  #,hoppings
Exemplo n.º 24
0
def parse_arguments(args):
    opt = {}

    print('=> Reading config file and command line arguments')
    config = ConfigParser()
    config._interpolation = configparser.ExtendedInterpolation()
    config.read(args.config)

    # figure out which model we're working with
    if args.option is not None:
        config.set('params', 'option', args.option)
    option = config.get('params', 'option')

    # handle the case of resuming an older model
    opt['resume_new_log_folder'] = False
    if args.resume:
        config.set(option, 'resume', args.resume)
    opt['resume'] = config.get(option, 'resume')
    if opt['resume']:
        if os.path.isfile(os.path.dirname(opt['resume']) + '/config.ini'):
            print('=> Resume flag set; switching to resumed config file')
            args.config = os.path.dirname(opt['resume']) + '/config.ini'
            config.clear()
            config._interpolation = configparser.ExtendedInterpolation()
            config.read(args.config)
        else:
            opt['resume_new_log_folder'] = True

    if args.gpu:
        config.set('params', 'gpu', args.gpu)
    if args.noise_param_idx:
        config.set('params', 'noise_param_idx', ' '.join(args.noise_param_idx))
    if args.logdir:
        config.set(option, 'logdir', args.logdir)
    if args.log_name:
        config.set(option, 'log_name', args.log_name)
    if args.batch_size:
        config.set(option, 'batch_size', str(args.batch_size))
    if args.workers:
        config.set(option, 'workers', str(args.workers))
    if args.epochs:
        config.set(option, 'epochs', str(args.epochs))
    if args.lambda_tv:
        config.set(option, 'lambda_tv', str(args.lambda_tv))
    if args.print_every:
        config.set(option, 'print_every', str(args.print_every))
    if args.save_every:
        config.set(option, 'save_every', str(args.save_every))
    if args.lr:
        config.set(option, 'lr', str(args.lr))
    if args.train_files:
        config.set(option, 'train_files', args.train_files)
    if args.val_files:
        config.set(option, 'val_files', args.val_files)

    # read all values from config file
    opt['lambda_tv'] = float(config.get(option, 'lambda_tv'))
    opt['intensity_scale'] = 1

    opt['gpu'] = config.get('params', 'gpu')
    opt['noise_param_idx'] = int(config.get('params', 'noise_param_idx'))
    opt['logdir'] = config.get(option, 'logdir')
    opt['log_name'] = config.get(option, 'log_name')
    opt['batch_size'] = int(config.get(option, 'batch_size'))
    opt['workers'] = int(config.get(option, 'workers'))
    opt['epochs'] = int(config.get(option, 'epochs'))
    opt['print_every'] = int(config.get(option, 'print_every'))
    opt['save_every'] = int(config.get(option, 'save_every'))
    opt['lr'] = float(config.get(option, 'lr'))
    opt['train_files'] = config.get(option, 'train_files')
    opt['val_files'] = config.get(option, 'val_files')
    opt['optimizer_init'] = config.get(option, 'optimizer')
    opt['model_name'] = config.get(option, 'model_name')
    opt['spad_datapath'] = config.get(option, 'spad_datapath')
    opt['mono_datapath'] = config.get(option, 'mono_datapath')
    # write these values to config file
    cfgfile = open(args.config, 'w')
    config.write(cfgfile)
    cfgfile.close()

    return opt