tools module

Full Documentation for hippynn.tools module. Click here for a summary page.

Misc. helpful functions which are not part of the library organization per se.

class TeedFileOutput(*streams)[source]

Bases: object

flush(*arg, **kwargs)[source]
write(*arg, **kwargs)[source]
active_directory(dirname, create=None)[source]

Context manager for temporarily switching the current working directory.

If create is None, always succeed. If create is True, only succeed if the directory does not exist, and create one. If create is False, only succeed if the directory does exist, and switch to it.

In other words, use create=True if you want to force that it’s a new directory. Use create=False if you want to switch to an existing directory. Use create=None create a directory if you are okay with either alternative.

Parameters:
  • dirname – directory to enter

  • create – (None,True,False)

Returns:

None

Raises:

If directory status not compatible with create constraints.

arrdict_len(array_dictionary)[source]

Return the length of one of the arrays in a dictionary. Under the assumption that they are all the same. :param array_dictionary: :return:

device_fallback()[source]
is_equal_state_dict(d1, d2, raise_where=False)[source]

Checks if two pytorch state dictionaries are equal. Calls itself recursively if the value for a parameter is a dictionary.

Parameters:
  • d1

  • d2

  • raise_where – if not equal, use an assertion to fail.

Returns:

isiterable(obj)[source]
log_terminal(file, *args, **kwargs)[source]
Param:

file: filename or string

Param:

args: piped to open(file,*args,**kwargs) if file is a string

Param:

kwargs: piped to open(file,*args,**kwargs) if file is a string

Context manager where stdout and stderr are redirected to the specified file in addition to the usual stdout and stderr. The manager yields the file. Writes to the opened file object with “with log_terminal(…) as <file>” will not automatically be piped into the terminal.

np_of_torchdefaultdtype()[source]
pad_np_array_to_length_with_zeros(array, length, axis=0)[source]
param_print(module)[source]
print_lr(optimizer, print_=<built-in function print>)[source]
progress_bar(iterable, *args, **kwargs)[source]

Wrap an iterable in a progress bar according to hippynn’s current progress bar settings.

for args and kwargs, see tqdm documentation.

Parameters:
  • iterable

  • args

  • kwargs

Returns:

recursive_param_count(state_dict, n=0)[source]
unsqueeze_multiple(tensor, dims: tuple)[source]

Adds unsqueezing dimensions dimensions :param tensor: :param dims: :return: