-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tasks] Add positional arguments to run_experiment()
- Loading branch information
Showing
9 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pathlib | ||
import json | ||
from typing import List, Union | ||
|
||
from conductor.errors import ExperimentArgumentsNonPrimitiveValue | ||
from conductor.task_identifier import TaskIdentifier | ||
|
||
ArgumentValue = Union[str, bool, int, float] | ||
|
||
|
||
class ExperimentArguments: | ||
""" | ||
Represents positional arguments that should be passed to a | ||
`run_experiment()` task. | ||
""" | ||
|
||
def __init__(self, args: List[ArgumentValue]): | ||
self._args = args | ||
|
||
@classmethod | ||
def from_raw( | ||
cls, identifier: TaskIdentifier, raw_args: list | ||
) -> "ExperimentArguments": | ||
for arg in raw_args: | ||
if ( | ||
not isinstance(arg, str) | ||
and not isinstance(arg, bool) | ||
and not isinstance(arg, int) | ||
and not isinstance(arg, float) | ||
): | ||
raise ExperimentArgumentsNonPrimitiveValue(identifier=identifier) | ||
return cls(raw_args) | ||
|
||
def empty(self) -> bool: | ||
return len(self._args) == 0 | ||
|
||
def serialize_cmdline(self) -> str: | ||
""" | ||
Serializes the options into a form that can be passed to an | ||
executable as if it were a command line program. | ||
""" | ||
args = [] | ||
for arg in self._args: | ||
if isinstance(arg, bool): | ||
args.append("true" if arg else "false") | ||
else: | ||
args.append(str(arg)) | ||
return " ".join(args) | ||
|
||
def serialize_json(self, file_path: pathlib.Path) -> None: | ||
""" | ||
Serializes the options into JSON and writes the result to a file at | ||
`file_path`. | ||
""" | ||
with open(file_path, "w") as file: | ||
json.dump(self._args, file, indent=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters