def test_cli_mock(capsys): sys.argv = ["layout_generator", "-V"] with pytest.raises(SystemExit): main() captured = capsys.readouterr() assert captured.out == f"{__package_name__} version: {__version__}\n" sys.argv = ["layout_generator", "generate", "--test"] with pytest.raises(SystemExit): main()
def test_cli_default(): # default config test sys.argv = ["layout_generator", "generate"] parser, options = main(debug=True) assert options.length == 0.1 assert options.prefix == "Example" # assert options.bcs == [ [[0.01, 0], [0.02, 0]], [[0.08, 0], [0.09, 0]] ] # cli test bcs = parser.parse_args([ "generate", "--bcs", "[[0.25,0],[0.75,0]]", "--bcs", "[[0.1,0],[0.2,0]]", ]).bcs assert bcs == [[[0.25, 0], [0.75, 0]], [[0.1, 0], [0.2, 0]]] # unknown method with pytest.raises(SystemExit), pytest.raises(argparse.ArgumentError): parser.parse_args("--method unknown") isinstance(options.power, list) power = parser.parse_args("generate --power 2 --power 3").power assert power == [2, 3] with pytest.raises(SystemExit): parser.parse_args("-V")
def test_multi_powers_seq(): sys.argv = "layout_generator generate_c".split() parser, options = main(debug=True) # powers is 2D list assert isinstance(options.powers, list) assert isinstance(options.powers[0], list) n = len(options.powers) options.powers[0] = [1000, 2000] task: continuous.sequence_layout_sampling.TaskSeq = continuous.get_task( geometry_board="s", size_board=options.length, grid_board=options.nx, geometry=["r"] * n, size=options.units, angle=options.angles, intensity=options.powers, rad=False, method="sequence", ) # 每个组件的功率都是 list assert (isinstance(p, list) for p in task.components.intensity) task.warmup() layout, _ = task.sample_until_success() assert layout.shape == (options.nx, ) * 2 assert any(p in layout for p in options.powers[0])
def test_powers(): sys.argv = "layout_generator generate_c_power".split() parser, options = main(debug=True) # powers is 2D list assert isinstance(options.powers, list) assert isinstance(options.powers[0], list) positions = np.array([k for k in options.positions]) if options.positions_type == "coord": pass elif options.positions_type == "grid": positions = positions / (options.nx + 1) * options.length else: raise LookupError(f"Type {options.positions_type} is not supported!") task = continuous.get_task_powers_sampling( geometry_board="s", size_board=options.length, grid_board=options.nx, geometry=["r"] * len(options.units), size=options.units, angle=options.angles, intensity=options.powers, rad=False, position=positions, ) # 每个组件的功率都是 list assert (isinstance(p, list) for p in task.components.intensity) task.warmup() layout, _ = task.sample_until_success() assert layout.shape == (options.nx, ) * 2
def test_interpolation(): sys.argv = "layout_generator generate_c_power".split() parser, options = main(debug=True) positions = np.array([k for k in options.positions]) if options.positions_type == "coord": pass elif options.positions_type == "grid": positions = positions / (options.nx + 1) * options.length else: raise LookupError(f"Type {options.positions_type} is not supported!") task = continuous.get_task_powers_sampling( geometry_board="s", size_board=options.length, grid_board=options.nx, geometry=["r"] * len(options.units), size=options.units, angle=options.angles, intensity=options.powers, rad=False, position=positions, ) task.warmup() F, _ = task.sample_until_success() intensity = task.intensity_sample if options.bcs is None: options.bcs = [] U, xs, ys, zs = run_solver_c( options.ndim, options.length, options.units, options.bcs, options.u_D, intensity, options.nx, F, coordinates=True, ) ind_x = np.array([10, 20, 40, 60, 80, 100]) ind_y = np.array([100, 80, 60, 40, 20, 10]) points_x = xs[ind_x, ind_y] points_y = ys[ind_x, ind_y] points_realvalue = U[ind_x, ind_y].reshape(-1, 1) # points = np.array(options.observation_points) points = np.hstack((points_x.reshape(-1, 1), points_y.reshape(-1, 1))) temp_points = observe_temperature_of_points(points, xs, ys, U).reshape(-1, 1) assert len(temp_points) == len(points) assert np.sum(np.abs(points_realvalue - temp_points)) < 1e-10
def get_parser_options(): config_path = Path(utils__file__).parent / "default3D.yml" assert config_path.exists(), "default3D.yml does not exist" sys.argv = [ "layout_generator", "generate", "--config", str(config_path), "--sample_n", "2", "--worker", "2", ] parser, options = main(debug=True) return parser, options
def test_2d_bc(tmp_path): try: from pytest_cov.embed import cleanup_on_sigterm except ImportError: pass else: cleanup_on_sigterm() sample_n = 10 worker = 4 path = tmp_path / "test" sys.argv = [ "layout_generator", "generate", "--data_dir", str(path), "--bcs", "[[0.01, 0], [0.02, 0]]", "--bcs", "[[0.08, 0], [0.09, 0]]", "--sample_n", str(sample_n), "--worker", str(worker), ] options = main(options_flag=True) data_dir = path # assert data_dir == 's' # data_path_list = os.listdir(options.data_dir) data_path_list = list(data_dir.glob(f"*.{options.file_format}")) assert data_dir.exists() assert len(data_path_list) == options.sample_n datum_path = data_path_list[0] r = io.load_mat(datum_path) assert set(["u", "F", "list", "xs", "ys"]).issubset(set(r.keys())) u = r["u"] assert u.shape == (options.nx,) * options.ndim assert u.min() >= options.u_D
def test_2d_generator(tmp_path, capsys): try: from pytest_cov.embed import cleanup_on_sigterm except ImportError: pass else: cleanup_on_sigterm() sample_n = 10 worker = 4 path = tmp_path / "test" sys.argv = [ "layout_generator", "generate", "--data_dir", str(path), "--bcs", "[]", "--sample_n", str(sample_n), "--worker", str(worker), ] # parser = get_parser() # options, _ = parser.parse_known_args() options = main(options_flag=True) data_dir = path # assert data_dir == 's' # data_path_list = os.listdir(options.data_dir) data_path_list = list(data_dir.glob(f"*.{options.file_format}")) assert data_dir.exists() assert len(data_path_list) == options.sample_n datum_path = data_path_list[0] r = io.load_mat(datum_path) assert set(["u", "F", "list", "xs", "ys"]).issubset(set(r.keys())) u = r["u"] assert u.shape == (options.nx,) * options.ndim assert u.min() >= options.u_D # plot dir sys.argv = [ "layout_generator", "plot", "-p", str(path), "--dir", "--worker", "2", ] main() assert len(list(path.glob("*.png"))) == options.sample_n sys.argv = [ "layout_generator", "plot", "-p", str(path), "--worker", "2", "-o", str(path / "sub"), ] main() outpath = path / "sub" assert len(list(outpath.glob("*.png"))) == options.sample_n # plot single file file_path = next(path.glob("*.mat")) sys.argv = [ "layout_generator", "plot", "-p", str(file_path), "-o", str(path / "o.png"), "--worker", "2", ] options = main(options_flag=True) png_path = path / "o.png" assert png_path.exists()
# -*- encoding: utf-8 -*- """ Desc : Generate layout dataset from command line interface. Example : >> python -m layout_generator --config config.yml """ # File : __main__.py # Time : 2020/03/29 15:15:10 # Author : Zweien # Contact : [email protected] from layout_generator.cli import main if __name__ == "__main__": main()
def config(): sys.argv = ["layout_generator", "generate_c"] parser, options = main(debug=True) return parser, options