def test_load(config): """load a non-default parameter from a yaml file and verify that the config object matches """ with tempfile.TemporaryDirectory() as tempdir: params = {'printer_extents': [1, 2, 3], 'test_key': 'test_value'} yaml_path = os.path.join(tempdir, "test.yml") with open(yaml_path, 'w') as f: yaml.safe_dump(params, f) new_config = Configuration(yaml_path) assert isinstance(new_config.printer_extents, np.ndarray) assert np.all(new_config.printer_extents == np.array([1, 2, 3])) assert new_config.test_key == 'test_value' assert not hasattr(config, 'test_key')
def test_regression(file_number): print() # files tree_file = f"regression_tree_{file_number}.json" config_file = f"regression_config_{file_number}.yml" tree_file = os.path.join(os.path.dirname(__file__), 'regression_test_data', tree_file) config_file = os.path.join(os.path.dirname(__file__), 'regression_test_data', config_file) Configuration.config = Configuration(config_file) # open and prepare mesh mesh = utils.open_mesh() # open tree baseline baseline = utils.open_tree(tree_file) # run new tree tree = search.beam_search(mesh) # verify they are the same print() for baseline_node in baseline.nodes: print(f"path: {baseline_node.path}") # same path node = tree.get_node(baseline_node.path) if node.plane is None: assert baseline_node.plane is None else: # same origin print( f"baseline origin {baseline_node.plane[0]}, test origin {node.plane[0]}" ) # same normal print( f"baseline normal {baseline_node.plane[1]}, test normal {node.plane[1]}" ) assert np.all(baseline_node.plane[0] == node.plane[0]) assert np.all(baseline_node.plane[1] == node.plane[1]) config = Configuration.config for i in range(config.beam_width): os.remove( os.path.join(os.path.dirname(__file__), 'regression_test_data', f'{i}.json')) for stl in glob.glob( os.path.join(os.path.dirname(__file__), 'regression_test_data', '*.stl')): os.remove(stl) config.restore_defaults()
def test_save(config): """modify the config, save it, verify that the modified values are saved and can be loaded """ config.connector_diameter = 100 with tempfile.TemporaryDirectory() as tempdir: # change directory config.directory = tempdir # save using a file name path = config.save("test_config.yml") # load the config back new_config = Configuration(path) assert new_config.connector_diameter == 100 with tempfile.TemporaryDirectory() as tempdir: # change config directory config.directory = tempdir # save using cached name, should be 'test_config.yml' path = config.save() assert path == os.path.join(tempdir, 'test_config.yml')
import time import logging from pychop3d import utils from pychop3d import connector from pychop3d.configuration import Configuration logging.basicConfig( level=logging.INFO, format="%(asctime)s %(name)s [%(levelname)s] %(message)s", handlers=[logging.StreamHandler()]) Configuration.config = Configuration( "C:\\Users\\Greg\\Documents\\things\\table\\config.yml") tree_file = "C:\\Users\\Greg\\Documents\\things\\table\\table_platform_20200503_040048\\final_tree_with_connectors.json" tree = utils.open_tree(tree_file) connector_placer = connector.ConnectorPlacer(tree) state = utils.load_connector_configuration(tree_file) tree = connector_placer.insert_connectors(tree, state) utils.export_tree_stls(tree)
help= 'path to a config yaml file, for an example, see "bunny_config.yml"', default="examples/bunny_config.yml") parser.add_argument( '-m', '--mesh', type=str, default=None, help= 'Specify the mesh file path to chop. This will override the mesh file in the config yaml' ) args = parser.parse_args() # load specified or default config file try: config = Configuration(args.config) except: parser.print_help() traceback.print_exc() sys.exit(0) # override the mesh path in config if specified on command line if args.mesh: config.mesh = args.mesh config.name = os.path.splitext(os.path.basename(args.mesh)[0]) # name the folder based on the name of the object and the current date / time output_folder = f"{config.name}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}" # create the new directory in the 'output' subdirectory of pychop3d new_directory = os.path.abspath( os.path.join(config.directory, output_folder))
f" ITERATION: {_}, FAILED: {n_failed}" ) print( f"******************************************************************************************************************" ) # create temporary directory with tempfile.TemporaryDirectory() as tempdir: # create timestamped directory within temporary directory date_string = datetime.datetime.now().strftime('%Y%m%d_%H%M%S') timestamped_dir = os.path.join(tempdir, date_string) os.mkdir(timestamped_dir) # download STL stl_file = download_stl(timestamped_dir) name = os.path.splitext(os.path.basename(stl_file))[0] # create config config = Configuration() config.name = name config.beam_width = 2 config.plane_spacing = 30 config.connector_diameter = 5 config.connector_spacing = 10 config.directory = timestamped_dir config.mesh = stl_file config.part_separation = True config.save() Configuration.config = config # run starter = utils.open_mesh() scale_factor = np.ceil( 1.1 * config.printer_extents / starter.bounding_box_oriented.primitive.extents).max()
import time from pychop3d import utils from pychop3d import connector from pychop3d.configuration import Configuration Configuration.config = Configuration( "C:\\Users\\Greg\\code\\pychop3d\\output\\success\\config.yml") Configuration.config.sa_initial_connector_ratio = .01 Configuration.config.connector_spacing = 20 Configuration.config.sa_iterations = 50_000 tree = utils.open_tree( "C:\\Users\\Greg\\code\\pychop3d\\output\\success\\0.json") connector_placer = connector.ConnectorPlacer(tree) t0 = time.time() state = connector_placer.simulated_annealing_connector_placement() print(f"TIME: {time.time() - t0}") tree = connector_placer.insert_connectors(tree, state) tree.export_stl()