uv.util package

Submodules

uv.util.attachment module

Code for manipulating and joining prefixes and suffixes to metric keys.

TODO convert attach into a single dispatch method, and make a new suffix method.

uv.util.attachment.attach(m: Dict[str, Any], a: Union[str, List[str]], prefix: bool = True) → Dict[str, Any][source]

Attaches the supplied prefix or suffix to every key in the dictionary.

uv.util.attachment.attach_iter(xs: Iterable[str], a: Union[str, List[str]], prefix: bool = True) → Iterable[str][source]

Attaches the supplied prefix or suffix to every item in the iterable.

uv.util.attachment.attach_s(s: str, a: Union[str, List[str]], prefix: bool = True)str[source]

Attach the supplied prefix or suffix to the string s.

uv.util.attachment.by_prefix(ms: Dict[Union[str, List[str]], Dict[str, Any]]) → Dict[str, Any][source]

Collapse the prefixes into the key. If you do this, you can avoid using prefixed reporters.

uv.util.attachment.by_suffix(ms: Dict[Union[str, List[str]], Dict[str, Any]]) → Dict[str, Any][source]

Collapse the suffixes into the key. If you do this, you can avoid using suffixed reporters.

uv.util.attachment.flatten(ms: Dict[str, Union[Any, Dict[str, Any]]]) → Dict[str, Any][source]

Collapse the prefixes into the key. Leaves non-Dict values untouched.

Note: this is not a recursive flatten. Values must either be Metrics or mappings from MetricKey to Metric

uv.util.env module

Utilities for interacting with the system environment.

uv.util.env.extract_params(prefix: Optional[str] = None, env: Optional[Dict[str, str]] = None) → Dict[str, str][source]

Some libraries that interact with UV pass experiment parameters via environment variables. This function returns a dict containing all environment keys (and their values) that begin with the supplied prefix and, optionally, an underscore.

An environment like this:

ENVVAR_MY_KEY=face ENVVAR_ANOTHER_KEY=sandwich ENVVARTHIRD_KEY=ham

would return:

{

“my_key”: “face”, “another_key”: “sandwich”, “third_key”: “ham”

}

Module contents

Utilities grab bag.

class uv.util.TqdmFile(file)[source]

Bases: object

Dummy file-like that will write to tqdm’s ‘write’ method, which will in turn write back into the supplied file. Use this when you need to redirect some stream in such a way that won’t cause the tqdm progress bar to reset.

file = None
flush()[source]
isatty()[source]
write(x)[source]
uv.util.is_number(s)[source]

Returns true if the supplied item can be converted into a float; false otherwise.

uv.util.json_str(item: Any)str[source]

Converts the supplied python object to a Python dictionary.

The difference from json.dumps is that this method makes a best effort to serialize anything it finds within a nested structure. np.float32 instances, for example.

uv.util.to_serializable(val)[source]
uv.util.to_serializable(val: numpy.floating)
uv.util.to_serializable(val: numpy.integer)
uv.util.to_serializable(val: numpy.ndarray)

Used by default.

uv.util.ts_np_array(val: numpy.ndarray)[source]

Convert a numpy array to a serializable list.

uv.util.ts_np_floating(val: numpy.floating)[source]

Used if val is an instance of numpy.floating.

uv.util.ts_np_int(val: numpy.integer)[source]

Used if val is an instance of numpy.integer.

uv.util.uuid()[source]

Generates a sane UUID for use in test files and metric directories.

uv.util.wrap(item: Any) → List[Any][source]

Ensures that the input is either a list, or wrapped in a list. Returns [] for None.