def test_param_ref(self): config_dict = {"value": "outputs", "ref": "ops.A"} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is True config_dict = {"value": "outputs.output1", "ref": "ops.A"} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is True config_dict = { "value": "artifact.metric_events", "ref": "runs.0de53b5bf8b04a219d12a39c6b92bcce", } config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is True config_dict = {"value": "input.param1", "ref": "dag"} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is True
def test_param_config_with_value(self): config_dict = {"value": "string_value"} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is True assert config.is_ref is False config_dict = {"value": 234} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is True assert config.is_ref is False config_dict = {"value": 23.4} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is True assert config.is_ref is False config_dict = {"value": {"key": "value"}} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is True assert config.is_ref is False config_dict = {"value": ["value1", "value2"]} config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is True assert config.is_ref is False
def test_wrong_param_with_ref_and_search(self): with self.assertRaises(ValidationError): V1Param.from_dict({ "value": "something", "ref": "test", "search": { "query": "test" } })
def test_run_simple_file_passes(self): run_config = V1CompiledOperation.read([ os.path.abspath("tests/fixtures/typing/run_cmd_simple_file.yml"), { "kind": "compiled_operation" }, ]) assert run_config.inputs[0].value == "MeanSquaredError" assert run_config.inputs[1].value is None validated_params = run_config.validate_params() assert run_config.inputs[0].value == "MeanSquaredError" assert run_config.inputs[1].value is None assert { "loss": V1Param(value="MeanSquaredError"), "num_masks": V1Param(value=None), } == {p.name: p.param for p in validated_params} with self.assertRaises(ValidationError): CompiledOperationSpecification.apply_operation_contexts(run_config) validated_params = run_config.validate_params( params={"num_masks": { "value": 100 }}) assert { "loss": V1Param(value="MeanSquaredError"), "num_masks": V1Param(value=100), } == {p.name: p.param for p in validated_params} assert run_config.run.container.args == [ "video_prediction_train", "--num_masks={{num_masks}}", "--loss={{loss}}", ] with self.assertRaises(ValidationError): # Applying context before applying params CompiledOperationSpecification.apply_operation_contexts(run_config) run_config.apply_params(params={"num_masks": {"value": 100}}) run_config = CompiledOperationSpecification.apply_operation_contexts( run_config) run_config = CompiledOperationSpecification.apply_runtime_contexts( run_config) assert run_config.version == 1.1 assert run_config.tags == ["foo", "bar"] container = run_config.run.container assert isinstance(container, k8s_schemas.V1Container) assert container.image == "my_image" assert container.command == ["/bin/sh", "-c"] assert container.args == [ "video_prediction_train", "--num_masks=100", "--loss=MeanSquaredError", ]
def test_wrong_param_value_type(self): with self.assertRaises(ValidationError): V1Param.from_dict({"ref": "test", "value": 12}) with self.assertRaises(ValidationError): V1Param.from_dict({ "search": { "query": "test" }, "value": { "foo": "bar" } })
def test_validate_to_init(self): param = V1Param(value="test") spec = param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) assert spec.validate_to_init() is False # Add connection spec.param.connection = "connection" assert spec.validate_to_init() is False # Add to_init spec.param.to_init = True assert spec.validate_to_init() is True # Dockerfile but no to_init param = V1Param(value="test") spec = param.get_spec( name="foo", iotype=types.DOCKERFILE, is_flag=False, is_list=False, is_context=False, arg_format=None, ) assert spec.validate_to_init() is False # add to_init spec.param.to_init = True assert spec.validate_to_init() is True # Add connection spec.param.connection = "test" assert spec.validate_to_init() is True
def test_param_search(self): config_dict = { "value": "outputs", "search": { "query": "metrics.a: < 21", "sort": "-inputs.name1" }, } config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is False assert config.is_search is True config_dict = { "value": "outputs.output1", "search": { "query": "metrics.a: < 21", "sort": "-inputs.name1", "limit": 2 }, } config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is False assert config.is_search is True config_dict = { "value": "artifact.metric_events", "search": { "query": "status: succeeded|failed" }, } config = V1Param.from_dict(config_dict) assert_equal_dict(config.to_dict(), config_dict) assert config.is_literal is False assert config.is_ref is False assert config.is_search is True
def from_hook(cls, hook: V1Hook, inputs: Dict, outputs: Dict, condition: Dict): run_patch = None if hook.connection: run_patch = {"connections": [hook.connection]} params = hook.params # Extend params with if not hook.disable_defaults: params = params or {} if inputs: params["inputs"] = V1Param(value=inputs, context_only=True) if outputs: params["outputs"] = V1Param(value=outputs, context_only=True) if condition: params["condition"] = V1Param(value=condition, context_only=True) return cls( run_patch=run_patch, hub_ref=hook.hub_ref, presets=hook.presets, params=params, )
def from_hook(cls, hook: V1Hook, contexts: Dict): run_patch = None if hook.connection: run_patch = {"connections": [hook.connection]} params = hook.params # Extend params with if not hook.disable_defaults: contexts = contexts or {} params = params or {} for k, v in contexts.items(): params[k] = V1Param(value=v, context_only=True) return cls( run_patch=run_patch, hub_ref=hook.hub_ref, presets=hook.presets, params=params, )
def test_run_with_refs(self): # Get compiled_operation data run_config = V1CompiledOperation.read([ os.path.abspath("tests/fixtures/typing/run_with_refs.yml"), { "kind": "compiled_operation" }, ]) params = { "num_masks": { "value": 2 }, "model_path": { "ref": "runs.64332180bfce46eba80a65caf73c5396", "value": "outputs.doo", }, } validated_params = run_config.validate_params(params=params) param_specs_by_name = {p.name: p.param for p in validated_params} assert param_specs_by_name == { "num_masks": V1Param(value=2), "model_path": V1Param(ref="runs.64332180bfce46eba80a65caf73c5396", value="outputs.doo"), } ref_param = param_specs_by_name["model_path"] assert ref_param.to_dict() == params["model_path"] with self.assertRaises(ValidationError): run_config.apply_params(params=params) # Passing correct context run_config.apply_params( params=params, context={ "runs.64332180bfce46eba80a65caf73c5396.outputs.doo": V1IO( name="model_path", value="model_path", is_optional=True, iotype="path", ) }, ) # New params params = { "num_masks": { "value": 2 }, "model_path": { "ref": "ops.A", "value": "outputs.doo" }, } validated_params = run_config.validate_params(params=params) param_specs_by_name = {p.name: p.param for p in validated_params} assert param_specs_by_name == { "num_masks": V1Param(value=2), "model_path": V1Param(ref="ops.A", value="outputs.doo"), } ref_param = param_specs_by_name["model_path"] assert ref_param.to_dict() == params["model_path"] with self.assertRaises(ValidationError): run_config.apply_params(params=params) run_config.apply_params( params=params, context={ "ops.A.outputs.doo": V1IO( name="model_path", value="model_path", is_optional=True, iotype="path", ) }, )
def test_to_init(self): param = V1Param(value="test", connection="connection") spec = param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) assert spec.to_init() is None param = V1Param(value="test", to_init=True) spec = param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) assert spec.to_init() is None param = V1Param(value="test", connection="connection", to_init=True) spec = param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) init = spec.to_init() assert init.container is None assert init.connection == "connection" assert init.dockerfile is None assert init.git is None assert init.artifacts is None # Dockerfile param = V1Param(value={"image": "test"}, connection="connection") spec = param.get_spec( name="foo", iotype=types.DOCKERFILE, is_flag=True, is_list=False, is_context=False, arg_format=None, ) assert spec.to_init() is None param = V1Param(value={"image": "test"}, to_init=True) spec = param.get_spec( name="foo", iotype=types.DOCKERFILE, is_flag=True, is_list=False, is_context=False, arg_format=None, ) init = spec.to_init() assert init.container is None assert init.connection is None assert init.dockerfile.image == param.value["image"] assert init.git is None assert init.artifacts is None # Git param = V1Param(value={"url": "test"}, connection="connection") spec = param.get_spec( name="foo", iotype=types.GIT, is_flag=False, is_list=False, is_context=False, arg_format=None, ) assert spec.to_init() is None param = V1Param(value={"url": "test"}, connection="connection", to_init=True) spec = param.get_spec( name="foo", iotype=types.GIT, is_flag=False, is_list=False, is_context=False, arg_format=None, ) init = spec.to_init() assert init.container is None assert init.connection == "connection" assert init.dockerfile is None assert init.git.to_dict() == param.value assert init.artifacts is None # Artifacts param = V1Param(value={"files": ["test"]}, connection="connection") spec = param.get_spec( name="foo", iotype=types.ARTIFACTS, is_flag=False, is_list=False, is_context=False, arg_format=None, ) assert spec.to_init() is None param = V1Param( value={"files": ["test"]}, connection="connection", to_init=True ) spec = param.get_spec( name="foo", iotype=types.ARTIFACTS, is_flag=False, is_list=False, is_context=False, arg_format=None, ) init = spec.to_init() assert init.container is None assert init.connection == "connection" assert init.dockerfile is None assert init.git is None assert init.artifacts.to_dict() == param.value
def test_get_param(self): # None string values should exit fast param = V1Param(value=1) assert param.get_spec( name="foo", iotype=types.INT, is_flag=False, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.INT, param=param, is_flag=False, is_list=False, is_context=False, arg_format=None, ) # Str values none regex param = V1Param(value="1") assert param.get_spec( name="foo", iotype=types.INT, is_flag=False, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.INT, param=param, is_flag=False, is_list=False, is_context=False, arg_format=None, ) param = V1Param(value="SDfd") assert param.get_spec( name="foo", iotype=types.STR, is_flag=False, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=False, arg_format=None, ) # Validation dag param = V1Param(value="inputs.foo", ref="dag") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) param = V1Param(value="{{ inputs }}", ref="dag") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) with self.assertRaises(ValidationError): param = V1Param(value="{{ outputs }}", ref="dag") param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) with self.assertRaises(ValidationError): param = V1Param(value="inputs.foo", ref="dag.1") param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) # Validation ops param = V1Param(value="{{ outputs.foo }}", ref="ops.foo-bar") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) param = V1Param(value="inputs.foo", ref="ops.foo-bar") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) param = V1Param(value="inputs", ref="ops.foo-bar") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) # Regex validation ops: invalid params with self.assertRaises(ValidationError): param = V1Param(value="status.foo", ref="ops.foo-bar") param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) # Validation runs uid = uuid.uuid4().hex param = V1Param(value="outputs.foo", ref="runs.{}".format(uid)) assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) uid = uuid.uuid4().hex param = V1Param(value="inputs.foo", ref="runs.{}".format(uid)) assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False, is_context=False, arg_format=None, ) # Custom arg_format param = V1Param(value="SDfd") assert param.get_spec( name="foo", iotype=types.STR, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ) assert ( ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ).as_arg() == "--sdf=SDfd" ) # Custom arg_format with empty value param = V1Param(value=None) assert param.get_spec( name="foo", iotype=types.STR, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ) assert ( ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ).as_arg() == "" ) # Custom arg_format with 0 value param = V1Param(value=0) assert param.get_spec( name="foo", iotype=types.INT, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ) == ParamSpec( name="foo", iotype=types.INT, param=param, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ) assert ( ParamSpec( name="foo", iotype=types.INT, param=param, is_flag=False, is_list=False, is_context=False, arg_format="--sdf={{ foo }}", ).as_arg() == "--sdf=0" ) # Custom arg_format with bool value param = V1Param(value=True) assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=False, is_list=False, is_context=False, arg_format="{{'true-var' if foo else 'false-var'}}", ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=False, is_list=False, is_context=False, arg_format="{{'true-var' if foo else 'false-var'}}", ) assert ( ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=False, is_list=False, is_context=False, arg_format="{{'true-var' if foo else 'false-var'}}", ).as_arg() == "true-var" ) param = V1Param(value=False) assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=False, is_list=False, is_context=False, arg_format="{{'true-var' if foo else 'false-var'}}", ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=False, is_list=False, is_context=False, arg_format="{{'true-var' if foo else 'false-var'}}", ) assert ( ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=False, is_list=False, is_context=False, arg_format="{{'true-var' if foo else 'false-var'}}", ).as_arg() == "false-var" ) # isFlag param = V1Param(value=True) assert param.get_spec( name="foo", iotype=types.STR, is_flag=True, is_list=False, is_context=False, arg_format="{{foo}}", ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=True, is_list=False, is_context=False, arg_format="{{foo}}", ) assert ( str( ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=True, is_list=False, is_context=False, arg_format="{{foo}}", ) ) == "--foo" ) # isFlag empty param = V1Param(value=None) assert param.get_spec( name="foo", iotype=types.STR, is_flag=True, is_list=False, is_context=False, arg_format="{{foo}}", ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=True, is_list=False, is_context=False, arg_format="{{foo}}", ) assert ( str( ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=True, is_list=False, is_context=False, arg_format="{{foo}}", ) ) == "" ) # isContext param = V1Param(value={"key": "value"}) assert param.get_spec( name="foo", iotype=types.STR, is_flag=False, is_list=False, is_context=True, arg_format="{{foo}}", ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=True, arg_format="{{foo}}", ) assert ( ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=True, arg_format=None, ).param.value == {"key": "value"} ) assert ( str( ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False, is_context=True, arg_format=None, ) ) == ujson.dumps({"key": "value"}) ) # Regex validation runs: invalid params with self.assertRaises(ValidationError): param = V1Param(value="outputs.foo", ref="run.foo-bar") param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, is_context=False, arg_format=None, )
def test_get_param(self): # None string values should exit fast param = V1Param(value=1) assert param.get_spec( name="foo", iotype=types.INT, is_flag=False, is_list=False ) == ParamSpec( name="foo", iotype=types.INT, param=param, is_flag=False, is_list=False ) # Str values none regex param = V1Param(value="1") assert param.get_spec( name="foo", iotype=types.INT, is_flag=False, is_list=False ) == ParamSpec( name="foo", iotype=types.INT, param=param, is_flag=False, is_list=False ) param = V1Param(value="SDfd") assert param.get_spec( name="foo", iotype=types.STR, is_flag=False, is_list=False ) == ParamSpec( name="foo", iotype=types.STR, param=param, is_flag=False, is_list=False ) # Validation dag param = V1Param(value="inputs.foo", ref="dag") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) param = V1Param(value="{{ inputs }}", ref="dag") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) with self.assertRaises(ValidationError): param = V1Param(value="{{ outputs }}", ref="dag") param.get_spec(name="foo", iotype=types.BOOL, is_flag=True, is_list=False) with self.assertRaises(ValidationError): param = V1Param(value="inputs.foo", ref="dag.1") param.get_spec(name="foo", iotype=types.BOOL, is_flag=True, is_list=False) # Validation ops param = V1Param(value="{{ outputs.foo }}", ref="ops.foo-bar") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) param = V1Param(value="inputs.foo", ref="ops.foo-bar") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) param = V1Param(value="inputs", ref="ops.foo-bar") assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False, ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) # Regex validation ops: invalid params with self.assertRaises(ValidationError): param = V1Param(value="status.foo", ref="ops.foo-bar") param.get_spec(name="foo", iotype=types.BOOL, is_flag=True, is_list=False) # Validation runs uid = uuid.uuid4().hex param = V1Param(value="outputs.foo", ref="runs.{}".format(uid)) assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) uid = uuid.uuid4().hex param = V1Param(value="inputs.foo", ref="runs.{}".format(uid)) assert param.get_spec( name="foo", iotype=types.BOOL, is_flag=True, is_list=False ) == ParamSpec( name="foo", iotype=types.BOOL, param=param, is_flag=True, is_list=False ) # Regex validation runs: invalid params with self.assertRaises(ValidationError): param = V1Param(value="outputs.foo", ref="run.foo-bar") param.get_spec(name="foo", iotype=types.BOOL, is_flag=True, is_list=False)
def test_wrong_param_without_value(self): with self.assertRaises(ValidationError): V1Param.from_dict({"ref": "test"}) with self.assertRaises(ValidationError): V1Param.from_dict({"search": {"query": "test"}})
def test_wrong_param_config(self): # No name with self.assertRaises(ValidationError): V1Param.from_dict({})