Skip to content

Commit

Permalink
Ignore default value for inputs if it's local file path
Browse files Browse the repository at this point in the history
When generating/updating config to valohai.yaml, we will ignore the
value of inputs if it's pointing to a local file path
  • Loading branch information
dangquangdon committed Feb 6, 2024
1 parent 29fff94 commit 8d2063e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ venv.bak/
# JetBrains IDEs
.idea/

# Vscode
.vscode

# Rope project settings
.ropeproject

Expand Down
5 changes: 2 additions & 3 deletions tests/test_yaml/test1.expected.valohai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
type: float
inputs:
- name: input1
default: asdf
default: datum://asdf
optional: false
- name: input2
default:
- yolol
- yalala
- datum://yolol
optional: false
- name: my-optional-input
optional: true
6 changes: 5 additions & 1 deletion tests/test_yaml/test1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"param4": 0.0001,
}

inputs = {"input1": "asdf", "input2": ["yolol", "yalala"], "my-optional-input": ""}
inputs = {
"input1": "datum://asdf",
"input2": ["datum://yolol", "yalala"], # local path
"my-optional-input": "",
}


def prepare(a, b):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_yaml/test2.expected.valohai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
type: float
inputs:
- name: input1
default: asdf
default: datum://asdf
optional: false
- name: input2
default:
- yolol
- yalala
- datum://yolol
- datum://yalala
optional: false
2 changes: 1 addition & 1 deletion tests/test_yaml/test2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"param4": 0.0001,
}

inputs = {"input1": "asdf", "input2": ["yolol", "yalala"]}
inputs = {"input1": "datum://asdf", "input2": ["datum://yolol", "datum://yalala"]}


def prepare(a, b):
Expand Down
7 changes: 3 additions & 4 deletions tests/test_yaml/test3.expected.valohai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
type: float
inputs:
- name: input1
default: asdf
optional: false
optional: true
- name: input2
default:
- yolol
- yalala
- datum://yolol
- datum://yalala
optional: false
5 changes: 4 additions & 1 deletion tests/test_yaml/test3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"param4": 0.0001,
}

inputs = {"input1": "asdf", "input2": ["yolol", "yalala"]}
inputs = {
"input1": "asdf", # local path
"input2": ["datum://yolol", "datum://yalala"],
}


def prepare(a, b):
Expand Down
9 changes: 2 additions & 7 deletions tests/test_yaml/test4.expected.valohai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
type: float
inputs:
- name: input1
default: asdf/*
keep-directories: suffix
optional: false
optional: true
- name: input2
default:
- yolol
- yalala
optional: false
optional: true
- step:
name: herpderp
image: busybox
Expand Down
5 changes: 4 additions & 1 deletion tests/test_yaml/test4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"param4": 0.0001,
}

inputs = {"input1": "asdf/*", "input2": ["yolol", "yalala"]}
inputs = {
"input1": "asdf/*",
"input2": ["yolol", "yalala"],
}


def prepare(a, b):
Expand Down
6 changes: 6 additions & 0 deletions valohai/internals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ def get_sha256_hash(filepath: str) -> str:

with open(filepath, "rb") as f:
return get_fp_sha256(f) # type: ignore


def is_local_file_path(path_str: str) -> bool:
if "://" in path_str:
return False
return True
24 changes: 17 additions & 7 deletions valohai/internals/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from valohai.internals.parsing import parse
from valohai.types import InputDict, ParameterDict
from valohai.internals.utils import is_local_file_path


def generate_step(
Expand All @@ -27,7 +28,6 @@ def generate_step(
# We need to generate a POSIX-compliant command, even if we are running this method in Windows
# The path separator must be forced to POSIX
relative_source_path = relative_source_path.replace(os.sep, "/")

config_step = Step(
name=step,
image=image,
Expand All @@ -41,7 +41,6 @@ def generate_step(

for key, value in inputs.items():
config_step.inputs[key] = parse_input(key, value)

return config_step


Expand Down Expand Up @@ -70,19 +69,30 @@ def parse_input(key: str, value: Any) -> Input:
value["name"] = key
return Input.parse(value)

# Check and ignore the local path file
if isinstance(value, List):
checked_value = [val for val in value if not is_local_file_path(val)]
else:
checked_value = "" if is_local_file_path(value) else value

has_wildcards = any(
"*" in uri for uri in ([value] if isinstance(value, str) else value)
"*" in uri
for uri in (
[checked_value] if isinstance(checked_value, str) else checked_value
)
)
empty_default = (
not value
or isinstance(value, List)
and (len(value) == 0 or len(value) == 1 and not value[0])
not checked_value
or isinstance(checked_value, List)
and (
len(checked_value) == 0 or len(checked_value) == 1 and not checked_value[0]
)
)

keep_directories = KeepDirectories.SUFFIX if has_wildcards else KeepDirectories.NONE
return Input(
name=key,
default=None if empty_default else value,
default=None if empty_default else checked_value,
optional=empty_default,
keep_directories=keep_directories,
)
Expand Down

0 comments on commit 8d2063e

Please sign in to comment.