def test_simple_serialization(self): # args_list = [ # {'a': 666, 'b': 'hello!', 'c': True}, # {'a': None, 'b': None, 'c': None}, # ] items_list = [ {"kwargs": {"a": 666, "b": "hello!", "c": True}, "valid": True}, {"kwargs": {"a": "error!", "b": "hello!", "c": True}, "valid": False}, {"kwargs": {"a": 66.6, "b": "hello!", "c": True}, "valid": False}, {"kwargs": {"a": None, "b": "hello!", "c": True}, "valid": True}, ] for item in items_list: kwargs = item["kwargs"] valid = item["valid"] print(valid) o_0 = SimpleUserObject__(**kwargs) rep_0 = o_0.serialize() o_1 = Spook.create(rep_0) assert isinstance(o_1, SimpleUserObject__) rep_1 = o_1.serialize() assert o_1 == o_0 print(rep_0, rep_1)
def test_nested_serialization(self, tmpdir): item = { "kwargs": { "name": "n0", "o0": {"a": 1, "b": "hello", "c": True}, "o1": {"a": 2, "b": "world!", "c": False}, }, "valid": True, } cfg_folder = Path(tmpdir.mkdir("cfg_golder")) for ext in ["json", "yml"]: cfg_file = cfg_folder / f"cfg.{ext}" kwargs = item["kwargs"] valid = item["valid"] o0 = CustomUserObject__(**kwargs["o0"]) o1 = CustomUserObject__(**kwargs["o1"]) name = kwargs["name"] nested_0 = NestedUserObject_(name=name, o0=o0, o1=o1) if valid: nested_0.serialize_to_file(str(cfg_file), validate=True) nested_1 = Spook.create_from_file(str(cfg_file)) assert nested_0 == nested_1 else: with pytest.raises(SchemaError): nested_0.serialize(validate=True)
def test_custom_serialization(self): items_list = [ {"kwargs": {"a": 666, "b": "hello!", "c": True}, "valid": True}, {"kwargs": {"a": "error!", "b": "hello!", "c": True}, "valid": False}, {"kwargs": {"a": 66.6, "b": "hello!", "c": True}, "valid": False}, {"kwargs": {"a": None, "b": "hello!", "c": True}, "valid": True}, ] for item in items_list: kwargs = item["kwargs"] valid = item["valid"] o_0 = CustomUserObject__(**kwargs) if valid: rep_0 = o_0.serialize(validate=True) else: with pytest.raises(SchemaError): rep_0 = o_0.serialize(validate=True) continue o_1 = Spook.create(rep_0, validate=True) assert isinstance(o_1, CustomUserObject__) rep_1 = o_1.serialize(validate=True) print(rep_1) assert o_1 == o_0
def test_nested_serialization(self): items_list = [ { "kwargs": { "name": "n0", "o0": {"a": 1, "b": "hello", "c": True}, "o1": {"a": 2, "b": "world!", "c": False}, }, "valid": True, }, { "kwargs": { "name": "n0", "o0": {"a": "error!", "b": "world!", "c": False}, "o1": {"a": 2, "b": "world!", "c": False}, }, "valid": False, }, ] for clear_factory in [True, False]: for item in items_list: kwargs = item["kwargs"] valid = item["valid"] o0 = CustomUserObject__(**kwargs["o0"]) o1 = CustomUserObject__(**kwargs["o1"]) name = kwargs["name"] nested_0 = NestedUserObject_(name=name, o0=o0, o1=o1) if valid: rep_0 = nested_0.serialize(validate=True) rich.print("Representaiton", rep_0) if clear_factory: MetaSpook.clear_factory() nested_1 = Spook.create(rep_0) assert nested_0 == nested_1 else: with pytest.raises(SchemaError): nested_0.serialize(validate=True)
def from_dict(cls, d: dict): return MyBundle(name=d["name"], images=[Spook.create(x) for x in d["images"]])
# Create Object bundle = MyBundle( name="custom_bundle", images=[ MyImage(name="image", image_size=[800, 600]), MyImage(name="mask", image_size=[256, 256]), MyImage(name="crop", image_size=[32, 32]), ], ) # Serialization bundle_serialized = bundle.serialize() rich.print("Serialized object:", bundle_serialized) # IO Write cfg = XConfig.from_dict(bundle_serialized) cfg_filename = Path(tempfile.mkdtemp()) / "cfg.yml" cfg.save_to(cfg_filename) rich.print("Stored config in:", cfg_filename) # IO Read cfg = XConfig(filename=cfg_filename) hydrated_bundle: MyBundle = Spook.create(cfg) rich.print("Hydrated object:", hydrated_bundle.serialize()) for image in hydrated_bundle.images: image.my_method()
def from_dict(cls, d: dict): return cls(name=d["name"], o0=Spook.create(d["o0"]), o1=Spook.create(d["o1"]))
def calibrate( input_folder: Path, output_folder: Path, board_cfg: Path, device_cfg: Path, rectification: Path, scale_factor: int, save: bool, ): acquire_corners = False if input_folder is None: acquire_corners = True if board_cfg is None: board_cfg = oakeye.data_folder / "board" / "chessboard.yml" if device_cfg is None: device_cfg = oakeye.data_folder / "device" / "device.yml" board: Board = Spook.create(XConfig(board_cfg)) if acquire_corners: device_cfg = XConfig(device_cfg) device = OakDeviceFactory().create(device_cfg) keys = ["left", "center", "right"] acquirer = DeviceAcquirer(device) if rectification is not None: calib = XConfig(filename=rectification) acquirer = RectifiedAcquirer(acquirer, calib) acquirer = CornerAcquirer(acquirer, keys, board, scale_factor=scale_factor) dataset = acquirer() device.close() if save: ext_map = { "left": "png", "center": "png", "right": "png", "depth": "png", "board": "yml", "device": "yml", } root_files = ["board", "device"] for d in dataset: d["board"] = board.serialize() d["device"] = device_cfg.to_dict() UnderfolderWriter(output_folder, root_files_keys=root_files, extensions_map=ext_map)(SamplesSequence(dataset)) else: dataset = UnderfolderReader(input_folder) if len(dataset) < 1: raise ClickException(f"Input dataset must contain at least one sample") left = [] center = [] right = [] for sample in dataset: left.append(sample["left"]) center.append(sample["center"]) right.append(sample["right"]) calib = board.trinocular_calibration(left, center, right) output_folder.mkdir(parents=True, exist_ok=True) XConfig(plain_dict=calib).save_to(output_folder / "calibration.yml")