Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore default value for inputs if it's local file path #124

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 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,12 @@
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
9 changes: 8 additions & 1 deletion tests/test_yaml/test1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
"param4": 0.0001,
}

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


def prepare(a, b):
Expand Down
7 changes: 4 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,11 @@
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
3 changes: 2 additions & 1 deletion tests/test_yaml/test7.expected.valohai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
type: string
inputs:
- name: my-image
default: https://dist.valohai.com/valohai-utils-tests/Example.jpg
default:
- https://dist.valohai.com/valohai-utils-tests/Example.jpg
optional: false
- name: my-optional-input
optional: true
3 changes: 2 additions & 1 deletion tests/test_yaml/test8.expected.valohai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
default: s3://special-bucket/images/**.jpg
optional: false
- name: weights
default: s3://special-bucket/weights/yolo.pb
default:
- s3://special-bucket/weights/yolo.pb
optional: false
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
dangquangdon marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 8 additions & 12 deletions valohai/internals/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from valohai_yaml.objs import Config, Parameter, Step
from valohai_yaml.objs.input import Input, KeepDirectories
from valohai_yaml.utils import listify

from valohai.consts import DEFAULT_DOCKER_IMAGE
from valohai.internals.notebooks import (
Expand All @@ -12,6 +13,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 +29,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 +42,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,20 +70,16 @@ def parse_input(key: str, value: Any) -> Input:
value["name"] = key
return Input.parse(value)

has_wildcards = any(
"*" in uri for uri in ([value] if isinstance(value, str) else value)
)
empty_default = (
not value
or isinstance(value, List)
and (len(value) == 0 or len(value) == 1 and not value[0])
)
# Check and ignore the local path file
checked_value = [val for val in listify(value) if not is_local_file_path(val)]

has_wildcards = any("*" in uri for uri in checked_value)

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

Expand Down
Loading