def test_primitive_set_default(self): parser = PAIArgumentParser() parser.add_root_argument("root", DCPrimitive, DCPrimitive(dl={4, 5})) dc: DCPrimitive = parser.parse_args([]).root self.assertSetEqual(dc.l, set()) self.assertSetEqual(dc.dl, {4, 5})
def parse_args(args=None): parser = PAIArgumentParser() # GENERAL/SHARED PARAMETERS parser.add_argument('--version', action='version', version='%(prog)s v' + __version__) parser.add_root_argument("args", PredictAndEvalArgs) return parser.parse_args(args=args).args
def test_primitive_list_default(self): parser = PAIArgumentParser() parser.add_root_argument("root", DCPrimitve, DCPrimitve(dl=[4, 5])) dc: DCPrimitve = parser.parse_args([]).root self.assertListEqual(dc.l, []) self.assertListEqual(dc.dl, [4, 5])
def test_ssnake(self): parser = PAIArgumentParser() parser.add_root_argument("root", Root) root: Root = parser.parse_args(["--ssc.p1", "5", "--ssc.c.p2", "7", "--root.sc.p1", "2"]).root self.assertEqual(5, root.ssc.p1) self.assertEqual(7, root.ssc.c.p2) self.assertEqual(2, root.sc.p1)
def test_invalid_selection(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1) with self.assertRaises(TypeError): # Error that Level1 is not subclass of Level2 (type of arg.l) args = parser.parse_args(args=[ "--arg.p1", "0", "--arg.l", "test.test_data_class:Level1b" ])
def test_data_class_list_no_subclass(self): parser = PAIArgumentParser() parser.add_root_argument("root", DC) with self.assertRaises(TypeError): dc: DC = parser.parse_args([ "--root.l", "test.test_dataclass_list:SubSub", ]).root
def test_three_dc(self): parser = PAIArgumentParser() parser.add_root_argument("arg", DCTestMetaLevel1) args = parser.parse_args(args=["--arg.p", "0", "--arg/sub+p", "0.5"]) self.assertIsInstance(args.arg, DCTestMetaLevel1) self.assertIsInstance(args.arg.sub, DCTestMetaLevel2) self.assertEqual(args.arg.p, 0) self.assertEqual(args.arg.sub.p, "0.5")
def test_single_dc(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1b) args = parser.parse_args(args=["--arg.p1", "0", "--arg.p2", "test"]) dc = args.arg self.assertIsInstance(dc, Level1b) self.assertEqual(dc.p1, 0) self.assertEqual(dc.p2, "test")
def test_data_class_list_with_default(self): parser = PAIArgumentParser() parser.add_root_argument("root", DCWithDefault) dc: DCWithDefault = parser.parse_args([ "--root.l.1.int_arg", "-4", ]).root self.assertListEqual([Sub(int_arg=-2), Sub(int_arg=-4)], dc.l)
def test_required_from_default(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1b, default=Level1b(p1=-13)) args = parser.parse_args(args=[]) dc = args.arg self.assertIsInstance(dc, Level1b) self.assertEqual(dc.p1, -13) self.assertEqual(dc.p2, "")
def test_dc_with_fixed_flat(self): parser = PAIArgumentParser() parser.add_root_argument("arg", DCTestWithFixedFlat) args = parser.parse_args(args=["--arg.p", "0", "asdf"]) dc = args.arg self.assertIsInstance(dc.p, Level1b) self.assertEqual(dc.p.p1, 0) self.assertEqual(dc.p.p2, "asdf")
def test_invalid_choice(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1) with self.assertRaises(InvalidChoiceError): # Error that Level1 is not subclass of Level2 (type of arg.l) args = parser.parse_args(args=[ "--arg.p1", "0", "--arg.l.lvl3", "test.test_data_class:Level3base" ])
def parse_args(args=None): parser = PAIArgumentParser() parser.add_root_argument("root", CrossFoldTrainerParams, CrossFoldTrainerParams()) params: CrossFoldTrainerParams = parser.parse_args(args).root # TODO: add the training args (omit those params, that are set by the cross fold training) # setup_train_args(parser, omit=["files", "validation", "weights", # "early_stopping_best_model_output_dir", "early_stopping_best_model_prefix", # "output_dir"]) return params
def test_enum_list(self): parser = PAIArgumentParser() parser.add_root_argument("root", DifferentTypes) dt: DifferentTypes = parser.parse_args([ "--root.int_enum_list", "0", "B", ]).root self.assertEqual([IntEnumEx.A, IntEnumEx.B], dt.int_enum_list)
def parse_args(args=None): from calamari_ocr.ocr.scenario import CalamariScenario parser = PAIArgumentParser() parser.add_argument("--version", action="version", version="%(prog)s v" + __version__) default_trainer_params = CalamariScenario.default_trainer_params() parser.add_root_argument("trainer", default_trainer_params.__class__, default=default_trainer_params) params = parser.parse_args(args).trainer return params
def test_str_dataclass_dict_no_subclass(self): parser = PAIArgumentParser() parser.add_root_argument("dc", DC) with self.assertRaises(TypeError): dc: DC = parser.parse_args([ "--dc.str_dc", "a=test.test_dict:Sub", "b=test.test_dict:SubSub", "c", ]).dc
def main(): parser = PAIArgumentParser() parser.add_argument("--version", action="version", version="%(prog)s v" + __version__) parser.add_root_argument("root", PredictArgs, flat=True) args = parser.parse_args() run(args.root)
def test_optionals(self): parser = PAIArgumentParser() parser.add_root_argument("root", Base) base: Base = parser.parse_args([]).root self.assertIsNone(base.i1) self.assertEqual(base.i2, -1) self.assertIsInstance(base.sub1, Sub) self.assertIsNone(base.sub1.f1) self.assertIsNone(base.sub2)
def test_two_dc(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1) args = parser.parse_args(args=["--arg.l.p1", "-13"]) dc = args.arg self.assertIsInstance(dc, Level1) self.assertIsInstance(dc.l, Level2) self.assertEqual(dc.l.p1, -13) self.assertIsInstance(dc.l.lvl3, Level3a)
def test_required_via_meta(self): parser = PAIArgumentParser() parser.add_root_argument("arg", DCTestWithRequiredMeta) with self.assertRaises(RequiredArgumentError): parser.parse_args(args=[]) parser = PAIArgumentParser(ignore_required=True) parser.add_root_argument("arg", DCTestWithRequiredMeta) arg = parser.parse_args(args=[]).arg self.assertEqual(1, arg.p)
def test_primitive_default(self): parser = PAIArgumentParser() parser.add_root_argument("dc", DC, DC(str_int={"daf": 1})) dc: DC = parser.parse_args([ "--dc.int_str", "2=qwer", "3=rewq", ]).dc self.assertDictEqual({"daf": 1}, dc.str_int) self.assertDictEqual({2: "qwer", 3: "rewq"}, dc.int_str)
def test_two_dc_change(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1) args = parser.parse_args(args=[ "--arg.l.p1", "-13", "--arg.l", "test.test_data_class:Level2a", "--arg.l.p1a", "0.5" ]) dc = args.arg r = Level1.from_json(dc.to_json()) self.assertEqual(r, dc)
def test_enum(self): parser = PAIArgumentParser() parser.add_root_argument("root", DifferentTypes) dt: DifferentTypes = parser.parse_args([ "--root.int_enum", "B", "--root.str_enum", "test2", ]).root self.assertEqual(IntEnumEx.B, dt.int_enum) self.assertEqual(StrEnumEx.B, dt.str_enum)
def test_primitive_list(self): parser = PAIArgumentParser() parser.add_root_argument("root", DCPrimitve) dc: DCPrimitve = parser.parse_args([ "--root.l", "0", "1", "0", ]).root self.assertListEqual(dc.l, [0, 1, 0]) self.assertListEqual(dc.dl, [1, 2])
def test_primitive_set(self): parser = PAIArgumentParser() parser.add_root_argument("root", DCPrimitive) dc: DCPrimitive = parser.parse_args([ "--root.l", "0", "1", "0", ]).root self.assertSetEqual(dc.l, {0, 1, 0}) self.assertSetEqual(dc.dl, {1, 2})
def test_setting_second_level(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1, default=Level1(p1=-1, l=Level2a(p1=-13))) dc = parser.parse_args(args=["--arg.l.p1a", "-13"]).arg self.assertIsInstance(dc, Level1) self.assertIsInstance(dc.l, Level2a) self.assertEqual(dc.p1, -1) self.assertEqual(dc.l.p1, -13) self.assertEqual(dc.l.p1a, -13)
def test_two_dc_change(self): parser = PAIArgumentParser() parser.add_root_argument("arg", Level1) args = parser.parse_args(args=[ "--arg.l.p1", "-13", "--arg.l", "test.test_data_class:Level2a", "--arg.l.p1a", "0.5" ]) dc = args.arg self.assertIsInstance(dc, Level1) self.assertIsInstance(dc.l, Level2a) self.assertEqual(dc.l.p1, -13) self.assertEqual(dc.l.p1a, 0.5)
def test_list(self): parser = PAIArgumentParser() parser.add_root_argument("root", DifferentTypes) dt: DifferentTypes = parser.parse_args([ "--root.int_list", "1", "2", "--root.str_list", "test1", "test2", ]).root self.assertEqual([1, 2], dt.int_list) self.assertEqual(["test1", "test2"], dt.str_list)
def parse_args(args=None): from calamari_ocr.ocr.scenario import CalamariScenario parser = PAIArgumentParser() parser.add_argument('--version', action='version', version='%(prog)s v' + __version__) default_trainer_params = CalamariScenario.default_trainer_params() parser.add_root_argument('trainer', default_trainer_params.__class__, default=default_trainer_params) return parser.parse_args(args).trainer
def test_optionals_args(self): parser = PAIArgumentParser() parser.add_root_argument("root", Base) base: Base = parser.parse_args([ "--root.i1", "2", "--root.i2", "None", "--root.sub2", "Sub", "--root.sub1", "None" ]).root self.assertEqual(base.i1, 2) self.assertIsNone(base.i2) self.assertIsInstance(base.sub2, Sub) self.assertIsNone(base.sub2.f1) self.assertIsNone(base.sub1)