def test_liam2_demo_files(): liam2_demo_directory = os.path.join( pkg_resources.get_distribution('liam2').location, '..', 'tests', 'examples' ) excluded_files = [ 'demo_import.yml', # non working example 'demo02.yml', # TODO: pb with figures ] yaml_files = [os.path.join(liam2_demo_directory, _file) for _file in os.listdir(liam2_demo_directory) if os.path.isfile(os.path.join(liam2_demo_directory, _file)) and _file.endswith('.yml') and _file not in excluded_files] print yaml_files for yaml_file in yaml_files: print yaml_file simulation = Simulation.from_yaml( yaml_file, input_dir = os.path.join(liam2_demo_directory), # input_file = input_file, output_dir = os.path.join(os.path.dirname(__file__), 'output'), # output_file = output_file, ) simulation.run(False)
def explore(fpath): _, ext = splitext(fpath) ftype = 'data' if ext in ('.h5', '.hdf5') else 'simulation' print("Using {} file: '{}'".format(ftype, fpath)) if ftype == 'data': globals_def, entities = entities_from_h5(fpath) simulation = Simulation(globals_def, None, None, None, None, list(entities.values()), 'h5', fpath, None) period, entity_name = None, None else: simulation = Simulation.from_yaml(fpath) # use output as input simulation.data_source = H5Source(simulation.data_sink.output_path) period = simulation.start_period + simulation.periods - 1 entity_name = simulation.default_entity dataset = simulation.load() data_source = simulation.data_source data_source.as_fake_output(dataset, simulation.entities_map) data_sink = simulation.data_sink entities = simulation.entities_map if entity_name is None and len(entities) == 1: entity_name = list(entities.keys())[0] if period is None and entity_name is not None: entity = entities[entity_name] period = max(entity.output_index.keys()) eval_ctx = EvaluationContext(simulation, entities, dataset['globals'], period, entity_name) try: c = Console(eval_ctx) c.run() finally: data_source.close() if data_sink is not None: data_sink.close()
def test(regenerate_hdf_file = False, test_case = False): if regenerate_hdf_file: tax_benefit_system = TaxBenefitSystem() scenario = tax_benefit_system.new_scenario().init_single_entity( period = 2014, parent1 = dict( age = 25, salaire_imposable = 12000, ), ) simulation = scenario.new_simulation() dump( simulation, file_path = os.path.join(path_model, 'toto.h5'), overwrite = True, ) output_dir = os.path.join(os.path.dirname(__file__), 'output') console_file = os.path.join(path_model, 'test_case' if test_case else '', 'console.yml') simulation = Simulation.from_yaml( console_file, input_dir = None, input_file = 'toto.h5', output_dir = output_dir, output_file = None, ) # simulation.run(False) return simulation
def run_file(test_file): if 'import' in test_file: csv2h5(test_file) else: # We should NOT override output_dir here because it breaks tests and examples which load back what they # write (e.g. demo_load.yml) simulation = Simulation.from_yaml(test_file, log_level='processes') simulation.run()
def test(): console_file = os.path.join(path_model, 'console.yml') output_dir = os.path.join(path_til, 'output') simulation = Simulation.from_yaml( console_file, input_dir = None, input_file = None, output_dir = output_dir, output_file = None, ) simulation.run(False)
def display(fpath): print("Launching ViTables...") if fpath: _, ext = splitext(fpath) if ext in ('.h5', '.hdf5'): files = [fpath] else: simulation = Simulation.from_yaml(fpath) files = [ simulation.data_source.input_path, simulation.data_sink.output_path ] print("Trying to open:", " and ".join(str(f) for f in files)) else: files = [] viewhdf(files)
def simulate(args): print("Using simulation file: '{}'".format(args.fpath)) simulation = Simulation.from_yaml(args.fpath, input_dir=args.input_path, input_file=args.input_file, output_dir=args.output_path, output_file=args.output_file, start_period=args.startperiod, periods=args.periods, seed=args.seed, skip_shows=args.skipshows, skip_timings=args.skiptimings, log_level=args.loglevel, assertions=args.assertions, autodump=args.autodump, autodiff=args.autodiff) simulation.run(args.interactive)
if action != 'sort': if len(args) < 5 and action != 'sort': print("link_fields argument must be provided if using an .h5 " "input file") entities = [entity.split(':') for entity in args[4].split(';')] to_change = {ent_name: fields.split(',') for ent_name, fields in entities} # convert {ent_name: [target_ent1.fname1, target_ent2.fname2]} # to {ent_name: [(target_ent1, fname1), (target_ent2, fname2)]} for ent_name, fields in to_change.items(): for i, fname in enumerate(fields): fields[i] = \ fname.split('.') if '.' in fname else (ent_name, fname) else: simulation = Simulation.from_yaml(inputpath) inputpath = simulation.data_source.input_path to_change = {entity.name: fields_from_entity(entity) for entity in simulation.entities} assert action in {'shrink', 'shuffle', 'sort'} if action == 'shrink': timed(change_ids, inputpath, outputpath, to_change) elif action == 'shuffle': timed(change_ids, inputpath, '_shuffled_temp.h5', to_change, shuffle=True) timed(h5_sort, '_shuffled_temp.h5', outputpath, list(to_change.keys())) else: ent_names = args[4].split(',') if len(args) >= 5 else None timed(h5_sort, inputpath, outputpath, ent_names)