def test_generate_config(sim_engine): sim_engine = sim_engine() settings = sim_engine.settings # prepare expected_config with open(u.CONFIG_FILE_PATH, 'r') as f: expected_config = json.load(f) # adjust 'regular' field: # put first values in combination settings to 'regular' field as well as # empty the combination field if 'combination' in expected_config['settings']: comb_keys = expected_config['settings']['combination'].keys() while len(comb_keys) > 0: key = comb_keys.pop(0) value = expected_config['settings']['combination'][key][0] assert key not in expected_config['settings']['regular'] expected_config['settings']['regular'][key] = value del expected_config['settings']['combination'][key] expected_config # make 'exec_numMotes' a combination setting so that a directory for a log # file is properly made. SimSettings needs one combination at least. See # SimSettings.getOutputFile() expected_config['settings']['combination'] = { 'exec_numMotes': [ expected_config['settings']['regular']['exec_numMotes'] ] } del expected_config['settings']['regular']['exec_numMotes'] # adjust 'post' field expected_config['post'] = [] # adjust 'log' related fields expected_config['log_directory_name'] = 'startTime' expected_config['logging'] = 'all' # make sure the 'execution' field is fine expected_config['execution']['numCPUs'] == 1 expected_config['execution']['numRuns'] == 1 # set a random value expected_config['settings']['regular']['exec_randomSeed'] = ( sim_engine.random_seed ) # ready to test config = SimConfig.generate_config( settings_dict = settings.__dict__, random_seed = sim_engine.random_seed ) assert config == expected_config
def main(): # command line arguments parser = argparse.ArgumentParser(description='config.json extractor', epilog=""" This script generates config.json contents out of a log file. The log file should have "config" and "simulator.random_seed" lines for a target simulation run. """) parser.add_argument('log_file_path', help='the path to a log file (.dat)', type=str, default=None) parser.add_argument('-r', '--run_id', dest='target_run_id', help='target run_id to extract config.json', type=int) args = parser.parse_args() # identify config_line and random_seed config_line = None random_seed = None with open(args.log_file_path, 'r') as f: for line in f: log = json.loads(line) if log['_run_id'] != args.target_run_id: continue else: if log['_type'] == 'config': config_line = log elif log['_type'] == 'simulator.random_seed': random_seed = log['value'] if ((config_line is not None) and (random_seed is not None)): break if ((config_line is None) or (random_seed is None)): raise ValueError( 'cannot find "config" and "random_seed" lines for the target ' 'run_id:{0}.'.format(args.target_run_id)) # remove unnecessary '_type' element from config_line, which was added to # the dictionary of SimSettings object; see SimLog.__init__(). del config_line['_type'] # remove '_run_id' and add 'run_id' config_line['run_id'] = config_line['_run_id'] del config_line['_run_id'] # then, dump a generated config object nicely print json.dumps(SimConfig.generate_config(config_line, random_seed), indent=4)