def test_validator(root): parser = exman.ExParser(root=root) parser.add_argument("--param") def val1(p): return p.param != "x" def val2(p): if p.param != "y": return else: raise ValueError def val3(p): if p.param != "z": return else: return "should be z" parser.register_validator(val1, "should be not x, got {param}") parser.register_validator(val2, "should be not y, got {param}") parser.register_validator(val3, "should be not x, got {param}") with pytest.raises(argparse.ArgumentError) as e: parser.parse_args("--param " + "x") assert e.match("got x") with pytest.raises(argparse.ArgumentError) as e: parser.parse_args("--param " + "y") assert e.match("got y") with pytest.raises(argparse.ArgumentError) as e: parser.parse_args("--param " + "z") assert e.match("got z")
def test_dest_taken_in_account_while_reuse(root): parser = exman.ExParser(root=root) parser.add_argument("--arg1", dest="arg2", default="1") args1 = parser.parse_args(["--arg1", "2"]) params = args1.root / exman.parser.yaml_file("params") args2 = parser.parse_args("--config {}".format(params).split()) assert args2.arg2 == "2"
def test_reuse(root, type, nargs, py_value, str_value): parser = exman.ExParser(root=root) parser.add_argument("--param", nargs=nargs, type=type) args1 = parser.parse_args("--param " + str_value) assert args1.param == py_value params = args1.root / exman.parser.yaml_file("params") args2 = parser.parse_args("--config {}".format(params).split()) assert args2.param == py_value assert args1.root != args2.root
def test_volatile(root): parser = exman.ExParser(root=root) parser.add_argument("--arg1", dest="arg1", default="1") parser.add_argument("--arg2", dest="arg2", default="1", volatile=True) args1 = parser.parse_args(["--arg1", "2", "--arg2", "2"]) params = args1.root / exman.parser.yaml_file("params") args2 = parser.parse_args("--config {}".format(params).split()) assert args2.arg1 == "2" assert args2.arg2 == "1"
def test_safe_experiment(root): parser = exman.ExParser(root=root) args = parser.parse_args([]) with pytest.raises(ValueError), args.safe_experiment: raise ValueError("funny exception") assert not (parser.index / exman.parser.yaml_file(args.root.name)).exists() assert not args.root.exists() assert (parser.fails / args.root.name).exists() assert (parser.fails / args.root.name / "traceback.txt").exists() assert ("funny exception" in (parser.fails / args.root.name / "traceback.txt").read_text())
def test_dest(root): parser = exman.ExParser(root=root) parser.add_argument("--arg1", type=exman.optional(int), dest="arg2", default=None) args1 = parser.parse_args(["--arg1", "2"]) params = args1.root / exman.parser.yaml_file("params") args2 = parser.parse_args("--config {}".format(params).split()) assert args2.arg2 == 2
def test_safe_experiment_tmp_keyboard_interrupt(root, capsys): parser = exman.ExParser(root=root) args = parser.parse_args(["--tmp"]) with args.safe_experiment: raise KeyboardInterrupt("funny exception") assert "KeyboardInterrupt" in str(capsys.readouterr()[0]) assert not (parser.index / exman.parser.yaml_file(args.root.name)).exists() assert args.root.exists() assert not (parser.fails / args.root.name).exists() assert not (parser.fails / args.root.name / "traceback.txt").exists() assert "funny exception" in (args.root / "traceback.txt").read_text()
def test_automarked(root: pathlib.Path): parser = exman.ExParser(root=root, automark=["arg1"]) parser.add_argument("--arg1", default=1, type=int) parser.add_argument("--arg2", default=True, type=bool) parser.parse_args("--arg1=10 --arg2=F".split()) parser.parse_args("--arg1=9 --arg2=t".split()) info = exman.Index(parser.root).info() arg1_9 = exman.Index(parser.root).info("arg1/9") arg1_10 = exman.Index(parser.root).info("arg1/10") assert len(info) == 2 assert len(arg1_9) == 1 assert arg1_9.id[0] == 2 assert len(arg1_10) == 1 assert arg1_10.id[0] == 1
def test_nans(root: pathlib.Path): parser = exman.ExParser(root=root, automark=["test", "arg1", "arg2"]) parser.add_argument("--arg1", default=1, type=int) parser.add_argument("--arg2", default=True, type=bool) parser.parse_args("--arg1=10 --arg2=F".split()) parser.parse_args("--arg1=9 --arg2=t".split()) parser.add_argument("--arg3", default=2, type=int) parser.add_argument("--arg4", default="a", type=str) parser.parse_args("--arg1=9 --arg2=t".split()) parser.parse_args("--arg1=9 --arg4=1".split()) info = exman.Index(parser.root).info() # TODO: what is the proper way to process nans??? # The below appears to be float64 assert str(info.dtypes["arg3"]) == "float64" assert str(info.dtypes["arg4"]) == "object" assert info.arg4.iloc[-1] == "1"
def parser(root): exparser = exman.ExParser(root=root) exparser.add_argument("--arg1", default=1, type=int) exparser.add_argument("--arg2", default=True, type=bool) return exparser
def test_redirect(root): parser = exman.ExParser(root=root) args = parser.parse_args(["--tmp"]) with args.safe_experiment: print("hello") assert "hello" in (args.root / "log.txt").read_text()
def test_setters(root): parser = exman.ExParser(root=root) parser.register_setter(lambda p: p.__dict__.update(arg1=1)) args = parser.parse_args(["--tmp"]) assert args.arg1 == 1
#!/usr/bin/env python import os import sys import itertools import csv import time import tabulate # import inception.score first import inception.score import spectral import torch import torchvision.utils import scipy.misc import exman parser = exman.ExParser(root=exman.simpleroot(__file__), default_config_files=["local.cfg"]) # set defaults parser.add_argument("--name", type=str, default="unnamed") parser.add_argument("--wasserstein", type=bool, default=False) parser.add_argument("--mode", type=str, default="") parser.add_argument("--sn_strict", type=bool, default=True) parser.register_validator(lambda p: spectral.norm.validate_mode(p.mode), "Wrong spectral norm parameter") parser.add_argument("--latent", type=int, default=128) parser.add_argument("--dataset", default="cifar10") parser.add_argument("--batch_size", type=int, default=256) parser.add_argument("--data_root", default=os.environ.get("DATA_ROOT", "")) parser.add_argument("--iters", type=int, default=50000) parser.add_argument("--save_every", type=int, default=1000) parser.add_argument("--eval_every", type=int, default=1000)
#!/usr/bin/env python3 import os import torch import torch.nn as nn import torch.optim as optim import sys import pathlib import exman from femida_detect.detect import select, data_loader parser = exman.ExParser('Training for detection model', root=os.path.join( os.path.dirname(os.path.abspath(__file__)), '..', 'exman' )) parser.add_argument('--paug', type=float, default=.5, help='Augmentation probability') parser.add_argument('--imgsize', type=int, default=28, help='Image (Size x Size)') parser.add_argument('--data_dir', type=pathlib.Path, help='Data directory', default='./data/') parser.add_argument('--epochs', '-e', type=int, default=30, help='Number of epochs') parser.add_argument('--lr', type=float, default=.001, help='Initial learning rate') parser.add_argument('--batch_size', type=int, default=64, help='Batch size') parser.add_argument('--log_every', type=int, default=10, help='Log frequency (batches)') parser.add_argument('--augment', type=bool, default=True, help='Augmentation for training') parser.add_argument('--beta1', type=float, default=0.5) parser.add_argument('--beta2', type=float, default=0.999) parser.add_argument('--gpu', type=bool, default=True) parser.add_argument('--name', type=str, help='Name for experiment') parser.add_argument('--threads', type=int, default=16, help='Data Loading Threads') parser.add_argument('-v', type=str, help='Model Version', required=True) parser.add_argument('-a', type=str, default=1, help='Aug Version')