trident.utils package

Submodules

trident.utils.dictlist module

class trident.utils.dictlist.DictList(data=None)[source]

Bases: Generic[T]

A dictionary-like class that allows access to its items by key or index.

_data[source]

The underlying dictionary to store items.

Type:

Dict[str, Any]

_keys[source]

List of keys to maintain the order for index-based access.

Type:

List[str]

__getitem__(key

Union[str, int]) -> Any: Get an item by key or index.

__setitem__(key

str, value: Any) -> None: Set an item by key.

__delitem__(key

str) -> None: Delete an item by key.

get(key

str, default: Optional[Any] = None) -> Any: Get an item with a default if the key is not present.

__iter__() Iterator[str][source]

Get an iterator over the keys of the dictionary.

keys() KeysView[str][source]

Get a view object of the dictionary’s keys.

values() ValuesView[Any][source]

Get a view object of the dictionary’s values.

items() ItemsView[str, Any][source]

Get a view object of the dictionary’s items.

__len__() int[source]

Get the number of items in the dictionary.

__str__() str[source]

Get the string representation of the dictionary.

__repr__() str[source]

Get the official string representation of the object.

get(key, default=None)[source]

Get an item with a default if the key is not present.

Return type:

TypeVar(T)

items()[source]

Get a view object of the dictionary’s items.

Return type:

ItemsView[str, TypeVar(T)]

keys()[source]

Get a view object of the dictionary’s keys.

Return type:

KeysView[str]

values()[source]

Get a view object of the dictionary’s values.

Return type:

ValuesView[TypeVar(T)]

trident.utils.enums module

class trident.utils.enums.Split(value)[source]

Bases: Enum

The Split enum encapsulates various dataset splits used in Lightning. Lightning also refers to stage (str) which either is ‘fit’, ‘validate’, ‘test’, or ‘predict’. ‘fit’ spans ‘train’ and ‘val’ splits.

PREDICT = 'predict'[source]
TEST = 'test'[source]
TRAIN = 'train'[source]
VAL = 'val'[source]

trident.utils.logging module

trident.utils.logging.get_logger(name='trident.utils.logging', level=20)[source]

Initializes multi-GPU-friendly python logger.

Return type:

Logger

trident.utils.runner module

trident.utils.runner.extras(cfg)[source]

A couple of optional utilities, controlled by main config file: - disabling warnings - forcing debug friendly configuration - verifying experiment name is set when running in experiment mode

Modifies DictConfig in place.

Parameters:

config (DictConfig) – Configuration composed by Hydra.

Return type:

None

trident.utils.runner.finish(cfg, module, datamodule, trainer, callbacks, logger)[source]

Makes sure everything closed properly.

Return type:

None

trident.utils.runner.log_hyperparameters(cfg, module, trainer)[source]

This method controls which parameters from Hydra config are saved by Lightning loggers.

Return type:

None

Additionaly saves:
  • number of module parameters

trident.utils.runner.print_config(config, fields=('run', 'trainer', 'module', 'datamodule', 'callbacks', 'logger'), resolve=True)[source]

Prints content of DictConfig using Rich library and its tree structure.

Parameters:
  • config (DictConfig) – Configuration composed by Hydra.

  • fields (Sequence[str], optional) – Determines which main fields from config will

  • order. (be printed and in what)

  • resolve (bool, optional) – Whether to resolve reference fields of DictConfig.

Return type:

None

trident.utils.transform module

trident.utils.transform.concatenate_3d(tensors, pad_id=-100)[source]
Return type:

Tensor

trident.utils.transform.flatten_dict(inputs)[source]

Conflates keys of list[dict] and stacks np arrays & tensors along 0-dim.

Return type:

dict[str, Any]

trident.utils.transform.stack_or_pad_2d(tensors, pad_id=-100)[source]

Stack along first axis of latter axis is homogenous in length else pad and stack.

Return type:

Tensor

Module contents

trident.utils.deepgetitem(obj, item, default=None)[source]

Steps through an item chain to get the ultimate value.

If ultimate value or path to value does not exist, does not raise an exception and instead returns fallback.

Does not work if keys comprise dots.

Credits: https://stackoverflow.com/a/38623359

>>> d = {'snl_final': {'about': {'_icsd': {'icsd_id': 1}}}}
>>> deepgetitem(d, 'snl_final.about._icsd.icsd_id')
1
>>> deepgetitem(d, 'snl_final.about._sandbox.sbx_id')
>>>
trident.utils.get_dict_by_glob_pattern(dictionary, pattern)[source]