def test_dump(): example_file = os.path.join(CURDIR, 'test_e2e.cfg') with io.open(example_file, 'r', encoding='utf-8') as f: c = libconf.load(f, includedir=CURDIR) with open('test_e2e_copy.cfg', 'w+') as f: libconf.dump(c, f) with io.open('test_e2e_copy.cfg', 'r', encoding='utf-8') as f: c_dumped = libconf.load(f, includedir=CURDIR) assert c == c_dumped
def test_dump_roundtrip(): example_file = os.path.join(CURDIR, 'test_e2e.cfg') with io.open(example_file, 'r', encoding='utf-8') as f: c = libconf.load(f, includedir=CURDIR) with io.StringIO() as f: libconf.dump(c, f) f.seek(0) c_dumped = libconf.load(f, includedir=CURDIR) assert c == c_dumped
def OkHandle(self, e): ''' 确定按钮句柄 :return: ''' with io.open(self.currentPath, 'r', encoding='utf-8') as f: config = libconf.load(f) ConfigKey = "NONE" Block = self.BlockCtrl.GetValue() if (Block == "DB"): BlockNum = self.BlockNumCtrl.GetValue() if (BlockNum != None and BlockNum != "0"): ConfigKey = "DB_%s" % (BlockNum) elif (Block): ConfigKey = Block if (ConfigKey == "NONE"): self.Close() return if (config.has_key(ConfigKey)): plc_config = config.pop(ConfigKey) self._get_from_the_frame() tempConfig = {ConfigKey: self.dataConfigDict} config.update(tempConfig) self._Dict_toFile(config) self.Close()
def main(argv): if len(argv) == 0: print "usage: python ./pre_validation.py config_file_path!" exit(-1) cfg_file_name = argv[0] print "config file is: " + cfg_file_name cfg_file = io.open(cfg_file_name, encoding='utf-8') cfg = libconf.load(cfg_file) if cfg.has_key("service"): if cfg["service"].has_key("xxx_addr"): addr = cfg["service"]["xxx_addr"] if ping(addr) != 0: print "ping failed, addr: " << addr exit(-1) if cfg["service"].has_key("xxx_url"): url = cfg["service"]["xxx_url"] res_code = curl(url) if res_code != '200': print "curl url failed: " + res_code + ", url: " + url exit(-1) time.sleep(0.1)
def setupConsumerConfig(self): global sampleConfig, streamName if self.options['--config_file']: self.config = libconf.load(self.options['--config_file']) else: self.config = libconf.loads(sampleConfig) self.config['general']['log_path'] = self.runDir if self.options['--verbose']: self.config['general']['log_level'] = 'all' streamPrefix = self.options['<stream_prefix>'] if self.options['--instance_name']: utils.ndnrtcClientInstanceName = self.options[ '--instance_name'] self.basePrefix = streamPrefix if streamPrefix.endswith( utils.ndnrtcClientInstanceName) else os.path.join( streamPrefix, utils.ndnrtcClientInstanceName) self.config['consume']['streams'][0][ 'base_prefix'] = self.basePrefix self.sinkPipe = os.path.join(self.runDir, 'sink') self.config['consume']['streams'][0]['sink'][ 'name'] = self.sinkPipe if self.options['--stream_name']: streamName = self.options['--stream_name'] self.config['consume']['streams'][0]['name'] = self.options[ '--stream_name'] if self.options['--thread_name']: self.config['consume']['streams'][0][ 'thread_to_fetch'] = self.options['--thread_name'] self.configFile = os.path.join(self.runDir, 'consumer.cfg') with io.open(self.configFile, mode="w") as f: libconf.dump(self.config, f) logger.debug("saved config to %s" % self.configFile)
def __init__(self, cfg_path, icode_path): # Load config using libconf library with io.open(cfg_path) as f: config = libconf.load(f) # Parse icode mapping self.icode_mapping = {} self._parse_icodes(icode_path) # Parse architecture self.arch = config['arch'] # Parse ports self.ports = config['ports'] # Parse functional units self.functional_units = [ FunctionalUnit(i) for i in config['functional_units'] ] # Parse and create extension groups self.instr_type = {} self.instr_groups = [] for cfg in config['instruction_groups']: instr_group = InstructionGroup(cfg) self.instr_groups.append(instr_group) for ext in instr_group.exts: assert (ext not in self.instr_type) self.instr_type[ext] = instr_group
def read_config(filepath): ''' Reads input data from a configuration file ''' print('\nReading out the configuration file...') simulation_parameters = {} fitting_parameters = {} optimizer = None error_analysis_parameters = [] error_analyzer = None with io.open(filepath) as file: config = libconf.load(file) data_saver = read_output_settings(config, filepath) mode = read_calculation_mode(config) experiments = read_experimental_parameters(config, mode) spins = read_spin_parameters(config) if mode['simulation']: simulation_parameters = read_simulation_parameters(config) elif mode['fitting'] or mode['error_analysis']: fitting_parameters = read_fitting_parameters(config) optimizer = read_fitting_settings(config, experiments) error_analysis_parameters = read_error_analysis_parameters( config, fitting_parameters) error_analyzer = read_error_analysis_settings(config, mode) simulator = read_calculation_settings(config) plotter = Plotter(data_saver) return mode, experiments, spins, simulation_parameters, fitting_parameters, optimizer, error_analysis_parameters, error_analyzer, simulator, data_saver, plotter
def __init__(self, args): self.args = args try: with open(args.conf, 'r') as conf: self.conf = libconf.load(conf) except OSError as err: sys.stderr.write( f"[ERROR] storage config file {args.conf} open error : {err}\n" ) exit(1) if (args.display_conf): self.display_conf_and_exit() self.zmq_context = zmq.asyncio.Context() for channel_cfg in self.conf.channels: if channel_cfg.active is False: break try: channel = StorageChannel(self.zmq_context, channel_cfg) except Exception as err: sys.stderr.write( f"[ERROR] channel config error :\n{libconf.dumps(channel_cfg)}\nError message : {err}\n" ) break self.channels.append(channel) self.start_listening()
def load_configuration(filename): """Loads a libconfig configuration file.""" try: with open(filename) as f: return libconf.load(f) except FileNotFoundError: return None
def OkHandle(self, e): ''' 确定按钮句柄 :return: ''' with io.open(self.currentPath, 'r', encoding='utf-8') as f: config = libconf.load(f) ConfigKey = "NONE" Block = self.BlockCtrl.GetValue() if(Block == "DB"): BlockNum = self.BlockNumCtrl.GetValue() if(BlockNum != None and BlockNum != "0"): ConfigKey = "DB_%s"%(BlockNum) elif(Block): ConfigKey = Block if(ConfigKey == "NONE"): self.Close() return if (config.has_key(ConfigKey)): plc_config = config.pop(ConfigKey) self._get_from_the_frame() tempConfig = {ConfigKey : self.dataConfigDict} config.update(tempConfig) self._Dict_toFile(config) self.Close()
def read_config(filepath): sys.stdout.write('\n') sys.stdout.write('Reading out the configuration file... ') mode = {} exp_data = {} spinA = {} spinB = {} sim_settings = {} fit_settings = {} err_settings = {} calc_settings = {} output_settings = {} with io.open(filepath) as file: config = libconf.load(file) mode = read_calculation_mode(config) exp_data = read_experimental_parameters(config) spinA, spinB = read_spin_parameters(config) if mode['simulation']: sim_settings = read_simulation_settings(config, exp_data) if mode['fitting'] or mode['error_analysis']: fit_settings = read_fitting_settings(config, exp_data) err_settings = read_error_analysis_settings(config, mode) calc_settings = read_calculation_settings(config, exp_data) output_settings = read_output_settings(config) sys.stdout.write('[DONE]\n\n') return [ mode, exp_data, spinA, spinB, sim_settings, fit_settings, err_settings, calc_settings, output_settings ]
def LoadConfig(self, file_path = ''): """加载系统配置信息: 成功返回True,否则False。""" self.File_Path = file_path try: f = io.open(file_path, 'r', encoding='utf-8') except: logging.error(u'解析配置文件异常 : %s' % traceback.format_exc()) return False else: with f: self.CfgInfo = libconf.load(f) self.DevId = self.CfgInfo['sysinfo']['devid'] self.DevLegal = self.CfgInfo['sysinfo']['devlegal'] self.WeighType = self.CfgInfo['sysinfo']['weightype'] self.SensorNum = self.CfgInfo['sysinfo']['senum'] self.WebIp = self.CfgInfo['webserinfo']['ipaddr'] self.WebPort = self.CfgInfo['webserinfo']['port'] self.DBHost = self.CfgInfo['dbinfo']['host'] self.DBPort = self.CfgInfo['dbinfo']['port'] self.DBUser = self.CfgInfo['dbinfo']['user'] self.DBPwd = self.CfgInfo['dbinfo']['passwd'] self.DBName = self.CfgInfo['dbinfo']['name'] return True
def setupProducerConfig(self): global sampleConfig, streamName if self.options['--config_file']: self.config = libconf.load(self.options['--config_file']) else: self.config = libconf.loads(sampleConfig) self.config['general']['log_path'] = self.runDir if self.options['--verbose']: self.config['general']['log_level'] = 'all' self.config['produce']['streams'][0]['source'][ 'name'] = self.sourcePipe # customize other things, if options are available if self.options['--video_size']: # TODO logger.debug("will setup video size here") if self.options['--bitrate']: # TODO logger.debug("will setup bitrate here") if self.options['--stream_name']: streamName = self.options['--stream_name'] self.config['produce']['streams'][0]['name'] = self.options[ '--stream_name'] if self.options['--thread_name']: self.config['produce']['streams'][0]['threads'][0][ 'name'] = self.options['--thread_name'] # save config to a temp file self.configFile = os.path.join(self.runDir, 'producer.cfg') with io.open(self.configFile, mode="w") as f: libconf.dump(self.config, f) logger.debug("saved config to %s" % self.configFile)
def LoadMethod(FilePath: str) -> dict: FilePath = os.path.expanduser(FilePath) assert os.path.exists( FilePath), "Cannot find the configfile {}".format(FilePath) f = io.open(FilePath, 'r') Config = libconf.load(f) f.close() return Config
def test_dumps_roundtrip(): example_file = os.path.join(CURDIR, 'test_e2e.cfg') with io.open(example_file, 'r', encoding='utf-8') as f: c = libconf.load(f, includedir=CURDIR) c_dumped = libconf.loads(libconf.dumps(c)) assert c == c_dumped
def test_substitutions(): example_file = os.path.join(CURDIR, 'example_subs.cfg') with io.open(example_file, 'r', encoding='utf-8') as f: c = libconf.load(f, includedir=CURDIR) assert c['capabilities']['title'] == c['window']['title'] assert c['capabilities']['version'] == c['version'] print libconf.dumps(c)
def _init_from_confile(self): """ 从配置文件中初始化配置 :return: """ with io.open(self.currentPath, 'r', encoding='utf-8') as f: config = libconf.load(f) if(config.has_key("PLCconfig")): self.plcConfigDict = config["PLCconfig"]
def _init_from_confile(self): """ 从配置文件中初始化配置 :return: """ with io.open(self.currentPath, 'r', encoding='utf-8') as f: config = libconf.load(f) if (config.has_key("PLCconfig")): self.plcConfigDict = config["PLCconfig"]
def create_particle_source(nP=1e3, filename='../input/profiles.nc', geom_file='gitrD3DGeometry2DWringsPy.cfg'): with io.open(geom_file) as f: config = libconf.load(f) r1_geom = np.array(config['geom']['x1']) z1_geom = np.array(config['geom']['z1']) r2_geom = np.array(config['geom']['x2']) z2_geom = np.array(config['geom']['z2']) surf_geom = np.array(config['geom']['surface']) surface_indices = np.where(surf_geom > 0) surface_indices = surface_indices[0] print('r surface', r1_geom[surface_indices]) print('z surface', z1_geom[surface_indices]) nP = int(nP) x = 1.48406 + np.random.rand(1, int(nP)) * (1.50302 - 1.48406) y = np.zeros(int(nP)) z = 1.24722 - 0.5438 * (x - 1.48406) x = x - 3e-3 z = z - 3e-3 / 0.5438 vx = -3.0e3 * np.ones(nP) * (0.5438 / 1.1383) vy = -3.0e3 * np.ones(nP) vz = -3.0e3 * np.ones(nP) * (1 / 1.1383) #x = [] #z = [] #for i in range(len(surf_ne)-1): # nP_this_surf = int(nP*surf_ne[i]/ne_total) # if(i == 0): # nP_this_surf = nP_this_surf + 12 # this_r = surf_midpoint_r[i] + (np.random.rand(1,nP_this_surf)-0.5)*(r1_geom[surface_indices[i+1]] - r1_geom[surface_indices[i]]) # print('nP and rs', nP_this_surf, this_r) # x.extend(list(this_r[0])) # z.extend(surf_midpoint_z[i]*np.ones(nP_this_surf)) #print('x',len(x)) #print('z',len(z)) #x = np.array(x) plt.scatter(x, z) plt.show() rootgrp = netCDF4.Dataset("particleSource.nc", "w", format="NETCDF4") nr = rootgrp.createDimension("nP", nP) x_nc = rootgrp.createVariable("x", "f8", ("nP")) y_nc = rootgrp.createVariable("y", "f8", ("nP")) z_nc = rootgrp.createVariable("z", "f8", ("nP")) vx_nc = rootgrp.createVariable("vx", "f8", ("nP")) vy_nc = rootgrp.createVariable("vy", "f8", ("nP")) vz_nc = rootgrp.createVariable("vz", "f8", ("nP")) x_nc[:] = x y_nc[:] = 0.0 * np.zeros(int(nP)) z_nc[:] = z vx_nc[:] = vx vy_nc[:] = vy vz_nc[:] = 3.0e3 * np.ones(int(nP)) rootgrp.close()
def read_config_file(self, file_path): try: with io.open(file_path) as f: input_config = libconf.load(f) return input_config except Exception as e: input_config = dict() print 'Failed to read file ', e #raise return input_config
def read_config_file(self,file_path): try: with io.open(file_path) as f: input_config = libconf.load(f) self.cornet_setup = input_config return input_config except Exception as e: input_config = dict() print 'Failed to read file:',file_path print e sys.exit()
def make_from_file(cls, fname): ''' Construct from a libconfig file. ''' if not os.path.exists(fname): raise ValueError('{}: {}: no cfg file {} found'.format( util.PACKAGE_NAME, cls.__name__, fname)) with open(fname, 'r') as fh: cfg = libconf.load(fh) return cls(cfg)
def LoadDB(DBname, mode, serverpath): filenames = [serverpath + 'db/{}/{}.conf'.format(mode, DBname)] if os.path.isfile(serverpath + 'db/{}2.conf'.format(DBname)): filenames.append(serverpath + 'db/{}2.conf'.format(DBname)) for filename in filenames: with io.open(filename) as f: config = libconf.load(f) db = config[DBname] return db print('LoadDB: invalid database name {}'.format(DBname)) exit(1)
def OkHandle(self, e): ''' 确定按钮句柄 :return: ''' with io.open(self.currentPath, 'r', encoding='utf-8') as f: config = libconf.load(f) if (config.has_key("SysConfig")): plc_config = config.pop("SysConfig") self._get_from_the_frame() tempConfig = {"SysConfig": self.UserConfigDict} config.update(tempConfig) self._Dict_toFile(config) self.Close()
def OkHandle(self, e): ''' 确定按钮句柄 :return: ''' with io.open(self.currentPath, 'r', encoding='utf-8') as f: config = libconf.load(f) if (config.has_key("SysConfig")): plc_config = config.pop("SysConfig") self._get_from_the_frame() tempConfig = {"SysConfig" : self.UserConfigDict} config.update(tempConfig) self._Dict_toFile(config) self.Close()
def get_or_make_dir(test): test_config_abspath = os.path.join(root_dir, test) # Just test that path points to a valid config file. with open(test_config_abspath, 'r') as f: config = libconf.load(f) test_name = os.path.splitext(test)[0] test_name_str = test_name.replace(os.sep, '_') dirname = os.path.join(root_dir, 'tests', 'results', 'changes', test_name_str) subprocess.check_call(['mkdir', '-p', dirname]) return dirname, test_config_abspath
def rewrite_workload_bounds(src, dst, workload_bounds, t, cooling, max_iter, beta): w, h, c, n, k, s, r, wpad, hpad, wstride, hstride = workload_bounds q = int((w - s + 2 * wpad) / wstride) + 1 p = int((h - r + 2 * hpad) / hstride) + 1 print('Workload Dimensions:') print(' W =', w) print(' H =', h) print(' C =', c) print(' K =', k) print(' S =', s) print(' R =', r) print(' P =', p) print(' Q =', q) print(' N =', n) print(' W-pad =', wpad) print(' H-pad =', hpad) print(' W-stride =', wstride) print(' H-stride =', hstride) print() with open(src, "r") as f: if "cfg" in src: config = libconf.load(f) elif "yaml" in src: config = yaml.load(f, Loader=yaml.SafeLoader) config['problem']['R'] = r config['problem']['S'] = s config['problem']['P'] = p config['problem']['Q'] = q config['problem']['C'] = c config['problem']['K'] = k config['problem']['N'] = 32 config['problem']['Wstride'] = wstride config['problem']['Hstride'] = hstride config['problem']['Wdilation'] = 1 config['problem']['Hdilation'] = 1 config['mapper']['init-temp'] = t config['mapper']['max-iter'] = max_iter config['mapper']['cooling-iter'] = cooling config['mapper']['beta'] = beta with open(dst, "w") as f: if "cfg" in src: f.write(libconf.dumps(config)) elif "yaml" in src: f.write(yaml.dump(config))
def getAnswer(filename,x,y,r,z,charge): with io.open(filename) as f: config = libconf.load(f) relTol=1e-4 if(np.isclose(x,float(config.answer.x),rtol=relTol) and np.isclose(y,float(config.answer.y),rtol=relTol) and np.isclose(z,float(config.answer.z),rtol=relTol) and np.isclose(r,float(config.answer.r),rtol=relTol) and np.isclose(charge,float(config.answer.charge),rtol=relTol)): print colored('Test passed','green') passed=1 else: print colored('Test failed','red') print('x xAns',x,config.answer.x,np.isclose(x,float(config.answer.x),rtol=1e-04)) print('y yAns',y,config.answer.y,y==float(config.answer.y)) print('z zAns',z,config.answer.z,z==float(config.answer.z)) print('r rAns',r,config.answer.r,r==float(config.answer.r)) print('charge chargeAns',charge,config.answer.charge,charge==float(config.answer.charge)) passed=0 return passed
def test_example_config(): example_file = os.path.join(CURDIR, 'test_e2e.cfg') with io.open(example_file, 'r', encoding='utf-8') as f: c = libconf.load(f, includedir=CURDIR) assert c.appconfig.version == 37 assert c.appconfig['version-long'] == 370000000000000 assert c.appconfig['version-autolong'] == 370000000000000 assert c.appconfig.name == "libconf" assert c.appconfig.delimiter == False assert c.appconfig.works == True assert c.appconfig.allows == 0xA assert c.appconfig['eol-comments'] == 0xA assert c.appconfig.list == (3, "chicken", (), dict(group=True)) assert c.appconfig.sub_group.sub_sub_group.yes == "yes" assert c.appconfig.sub_group.sub_sub_group['include-works'] == True assert c.appconfig.sub_group.arr == [1, 2] assert c.appconfig.sub_group.str == "Strings are joined despite comments";
def test_example_config(): example_file = os.path.join(CURDIR, 'test_e2e.cfg') with io.open(example_file, 'r', encoding='utf-8') as f: c = libconf.load(f, includedir=CURDIR) assert c.appconfig.version == 37 assert c.appconfig['version-long'] == 370000000000000 assert c.appconfig['version-autolong'] == 370000000000000 assert c.appconfig.name == "libconf" assert c.appconfig.delimiter == False assert c.appconfig.works == True assert c.appconfig.allows == 0xA assert c.appconfig['eol-comments'] == 0xA assert c.appconfig.list == (3, "chicken", (), dict(group=True)) assert c.appconfig.sub_group.sub_sub_group.yes == "yes" assert c.appconfig.sub_group.sub_sub_group['include-works'] == True assert c.appconfig.sub_group.arr == [1, 2] assert c.appconfig.sub_group.str == "Strings are joined despite comments"
def get_listener_for_config_file(path): """ Create an AirplayListener instance according to a specific config file. The UDP backend is the prefered method to use. If this backend isn't available the pipe backend is used. If neither of those is configured this method falls back to use the MQTT backend. :param path: path to the shairport-sync.conf file :return: AirplayListener instance """ config_file = io.open(path) config = libconf.load(config_file) config_file.close() # Make sure that the metadata section is configured correctly metadata = config.metadata mqtt = config.mqtt if metadata["enabled"] != "yes": raise MetadataNotEnabledError("Please set the `enabled` flag inside the `metadata` section to `yes`.") # Make sure that at least one of the metadata provider is configured has_pipe = metadata.get("pipe_name", "") != "" has_udp = metadata.get("socket_address", "") != "" \ and metadata.get("socket_port", 0) > 0 has_mqtt = mqtt.get("enabled", "no") == "yes" \ and mqtt.get("hostname", "") != "" \ and mqtt.get("port", "") != "" \ and mqtt.get("publish_raw", "no") == "yes" # For some reason the mqtt port is a string and not and integer mqtt["port"] = int(mqtt["port"]) # Are any of the backends available if not has_pipe and not has_udp and not has_mqtt: raise NoBackendAvailableError("Please configure at least one metadata backend.") # Create a listener instance according to the available backend if has_udp: return shairportmetadatareader.AirplayUDPListener(**metadata) if has_pipe: return shairportmetadatareader.AirplayPipeListener(**metadata) if has_mqtt: return shairportmetadatareader.AirplayMQTTListener(**mqtt) return None
def import_cfg(file_name): """ Imports curves and surfaces from files in libconfig format. :param file_name: name of the input file :type file_name: str :return: a list of NURBS curve(s) or surface(s) :rtype: list :raises ImportError: cannot find 'libconf' module :raises IOError: an error occurred writing the file """ # Check if it is possible to import 'libconf' try: import libconf except ImportError as e: print( "Please install 'libconf' module to import from libconfig format: pip install libconf" ) raise e type_map = { 'curve': _prepare_cfg_import_curve, 'surface': _prepare_cfg_import_surface } # Try to read the input file try: with open(file_name, 'r') as fp: # Get all shapes imported_data = libconf.load(fp) # Process imported data ret_list = [] for data in imported_data.shapes: temp = type_map[data.type](data) ret_list.append(temp) # Return processed data return ret_list except IOError as e: print("An error occurred: {}".format(e.args[-1])) raise e except Exception: raise
def plot3dGeom(filename="gitrGeometry.cfg"): with io.open(filename) as f: config = libconf.load(f) x1 = np.array(config.geom.x1) x2 = np.array(config.geom.x2) x3 = np.array(config.geom.x3) y1 = np.array(config.geom.y1) y2 = np.array(config.geom.y2) y3 = np.array(config.geom.y3) z1 = np.array(config.geom.z1) z2 = np.array(config.geom.z2) z3 = np.array(config.geom.z3) xs = [] ys = [] zs = [] for i in range(0, x1.size - 1): xs.append(x1[i]) xs.append(x2[i]) xs.append(x3[i]) ys.append(y1[i]) ys.append(y2[i]) ys.append(y3[i]) zs.append(z1[i]) zs.append(z2[i]) zs.append(z3[i]) fig = plt.figure() #fig.patch.set_facecolor('black') #ax = fig.gca(projection='3d') #ax.plot_trisurf(xs,ys) print('xs ys zs', xs, ys, zs) ax = Axes3D(fig) verts = [zip(xs, ys, zs)] ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.set_xlim3d(0.5, 2.5) ax.set_ylim3d(-0.2, 0.2) ax.set_zlim3d(-0.5, 1.5) ax.add_collection3d(Poly3DCollection(verts)) plt.savefig('geomPlot.png')
def main(configfile, outfile, oldfile): """ Read the config and the old anonmap and create a new one """ existing_anonmap = read_anonmap(oldfile) with io.open(configfile) as cfgfile: config = libconf.load(cfgfile) new_anonmap, stats, changed = generate_anonmap(config, existing_anonmap) write_anonmap(outfile, new_anonmap) if changed: # print ("old: ", existing_anonmap) # print ("") # print ("new ", new_anonmap) print("Known: %d; New: %d; Public: %d; Lost: %d" % (stats['known'], stats['new'], stats['public'], stats['lost'])) trigger_update(config)
def LoadDBConsts(DBname, mode, serverpath): filenames = [serverpath + 'db/{}/{}.conf'.format(mode, DBname)] if os.path.isfile(serverpath + 'db/{}2.conf'.format(DBname)): filenames.append(serverpath + 'db/{}2.conf'.format(DBname)) consts = dict() for filename in filenames: with io.open(filename) as f: config = libconf.load(f) db = config[DBname] if DBname == 'item_db': for i, v in enumerate(db): consts[db[i].Id] = db[i].AegisName elif DBname == 'mob_db': for i, v in enumerate(db): consts[db[i].Id] = db[i].SpriteName elif DBname == 'skill_db': for i, v in enumerate(db): consts[db[i].Id] = db[i].Name else: print('LoadDBConsts: invalid database name {}'.format(DBname)) exit(1) return consts
def test_load_of_BytesIO_throws(): with pytest.raises(TypeError) as excinfo: libconf.load(io.BytesIO(b'a: "37";')) assert 'libconf.load' in str(excinfo.value)
def test_circular_include_raises(): circular_file = os.path.join(CURDIR, 'circular1.cfg') with io.open(circular_file, 'r', encoding='utf-8') as f: with pytest.raises(libconf.ConfigParseError): libconf.load(f, includedir=CURDIR)
def main(): parser = OptionParser() parser.add_option('--input-sacc','-i',dest="fname_in",default=None, help='Name of input SACC file') parser.add_option('--output-sacc','-o',dest="fname_out",default=None, help='Name of output SACC file') parser.add_option('--show-plot',dest="show_plot",default=False,action="store_true", help="Show plot comparing data and theory") parser.add_option('--param-file',dest="param_file",default=None, help="Path to CoLoRe param file") parser.add_option('--power-spectrum-type',dest="pk",default='linear',type=str, help="Power spectrum type: [`linear`,`halofit`]") parser.add_option('--transfer-function',dest="tf",default='boltzmann',type=str, help="Type of transfer function: [`eisenstein_hu`,`bbks`,`boltzmann`,`halofit`]") parser.add_option("--include-rsd",dest="rsd",default=False,action="store_true", help="Include RSD") parser.add_option("--dz-lognormal",dest="dz_ln",default=0.05,type=float, help="Redshift interval to use in computation of lognormal prediction") parser.add_option("--do-lognormal",dest="do_ln",default=False,action="store_true", help="Do we want to do the lognormal transformation?") (o, args) = parser.parse_args() #Read cosmological parameters and set cosmology object print("Reading CoLoRe params") with io.open(o.param_file) as f : colore_dict=lcf.load(f) ob=colore_dict['cosmo_par']['omega_B'] om=colore_dict['cosmo_par']['omega_M'] oc=om-ob hhub=colore_dict['cosmo_par']['h'] ns=colore_dict['cosmo_par']['ns'] sigma8=colore_dict['cosmo_par']['sigma_8'] cosmo = ccl.Cosmology(ccl.Parameters(Omega_c=oc,Omega_b=ob,h=hhub,sigma8=sigma8,n_s=ns,), matter_power_spectrum=o.pk,transfer_function=o.tf) #Calculate effective smoothing scale zmax=colore_dict['global']['z_max'] ngrid=colore_dict['field_par']['n_grid'] rsm0=colore_dict['field_par']['r_smooth'] Lbox=2*ccl.comoving_radial_distance(cosmo,1./(1+zmax))*(1+2./ngrid)*hhub a_grid=Lbox/ngrid rsm_tot=np.sqrt(rsm0**2+a_grid**2/12.) #Estimate lognormal prediction print("Estimating lognormal prediction") cmd="./lnpred " cmd+="%lf "%om cmd+="%lf "%ob cmd+="%lf "%hhub cmd+="%lf "%ns cmd+="%lf "%sigma8 cmd+=colore_dict['srcs1']['bias_filename']+" " cmd+="%lf "%rsm_tot cmd+="%lf "%zmax cmd+="%lf "%o.dz_ln cmd+=o.tf+" " cmd+=o.fname_out+".lnpred " if o.do_ln>0 : cmd+="1 " else : cmd+="0 " # cmd+="> "+o.fname_out+".lnpred_log" os.system(cmd) #Read lognormal prediction # Note that the lognormal prediction can be negative due to numerical instabilities. # The lines below make sure that the power spectrum is always positive, and extrapolate # it beyond the range where it becomes unreliable numz=int(zmax/o.dz_ln) numk=len(np.loadtxt(o.fname_out+".lnpred_pk_z0.000.txt",unpack=True)[0]) pk2d=np.zeros([numz,numk]) zarr=o.dz_ln*(numz-1-np.arange(numz)) aarr=1./(1+zarr) for i in np.arange(numz) : z=o.dz_ln*(numz-1-i) karr,pk,dum1,dum2=np.loadtxt(o.fname_out+".lnpred_pk_z%.3lf.txt"%z,unpack=True) idpos=np.where(pk>0)[0]; if len(idpos)>0 : idmax=idpos[-1] pk=np.maximum(pk,pk[idmax]) w=1./karr**6 pk[idmax:]=pk[idmax]*w[idmax:]/w[idmax] pk2d[i,:]=pk pk2d=pk2d.flatten() karr*=hhub pk2d/=hhub**3 #Cleanup os.system('rm '+o.fname_out+'.lnpred*') #Update power spectrum in cosmo to lognormal prediction (both linear and non-linear) ccl.update_matter_power(cosmo,karr,aarr,pk2d,is_linear=True) ccl.update_matter_power(cosmo,karr,aarr,pk2d,is_linear=False) print('Reading SACC file') #SACC File with the N(z) to analyze binning_sacc = sacc.SACC.loadFromHDF(o.fname_in) print("Setting up CCL tracers") tracers = binning_sacc.tracers cltracers=[ccl.ClTracer(cosmo,'nc',has_rsd=o.rsd,has_magnification=False,n=(t.z,t.Nz), bias=(t.z,np.ones_like(t.z))) for t in tracers] print('Computing power spectra') theories = getTheories(cosmo,binning_sacc,cltracers) mean=getTheoryVec(binning_sacc,theories) csacc=sacc.SACC(tracers,binning_sacc.binning,mean) csacc.printInfo() csacc.saveToHDF(o.fname_out,save_precision=False) if o.show_plot: plt.figure() for t in tracers : plt.plot(t.z,t.Nz) plt.show() print('Done')