pugh_torch package

Subpackages

Submodules

pugh_torch.exceptions module

exception pugh_torch.exceptions.DataUnavailableError[source]

Bases: Exception

Data is missing from disk and isn’t readily available to easily download.

exception pugh_torch.exceptions.HashMismatchError[source]

Bases: Exception

Occurs when a file does not have the expected hash.

exception pugh_torch.exceptions.ShouldNeverHappenError[source]

Bases: Exception

Portions of code have been reached that the developer never intended.

pugh_torch.helpers module

pugh_torch.helpers.add_text_under_img(img, text, font_size=None, min_font_size=10, font='DejaVuSansMono.ttf', bg='white', fg='black')[source]

Rasterize and add text under an image.

Based on:

https://stackoverflow.com/a/4902713/13376237

Parameters
  • img (numpy.ndarray or PIL.Image.Image) – (H,W,3) Image

  • text (str) – Text to display under the image

  • min_font_size (int) – Minimum font size to render. If the resulting text is wider than the passed in img, then the img will be resized. Ignored if font_size is provided.

  • font_size (int) – If provided, uses this font size and doesn’t auto-search for a font size.

  • bg (str or tuple) – Background color of annotation. Defaults to white.

  • fg (str or tuple) – Text color. Defaults to black.

Returns

Resulting annotated image. The image may be rescaled depending on min_font_size.

Return type

numpy.ndarray

pugh_torch.helpers.calling_scope(name, index=1, strict=True)[source]

Gets an object from the calling scope.

This uses a bunch of hacky stuff and may be fragile.

Parameters
  • name (str) – Object in the calling scope to get.

  • index (int) – How many frames to go up in the stack. Defaults to 1 (direct caller).

  • strict (bool) – If True, only search the specified frame’s local and global scope. Otherwise, iterate up the stack until the object is found.

Returns

Object from caller scope.

Return type

obj

pugh_torch.helpers.camel_to_snake(s)[source]

Converts a camelCase or pascalCase string to snake_case

Parameters

s (str) – Some camel/pascal case string to convert to snake case

Returns

camel_case equivalent of the provided string.

Return type

str

pugh_torch.helpers.compare_hash(expected, actual)[source]
Raises

pugh_torch.HashMismatchError – If the hashes do not match.

pugh_torch.helpers.compute_sha1(path, chunk_size=1048576)[source]

Computes the SHA1 hash of a file

Parameters

path (str-like) – Path to file to compute the hash of.

pugh_torch.helpers.download(url, path=None, overwrite=False, sha1_hash=None)[source]

Download an given URL

Parameters
  • url (str) – URL to download

  • path (str or Path-like, optional) – Destination path to store downloaded file. By default stores to the current directory with same name as in url.

  • overwrite (bool, optional) – Whether to overwrite destination file if already exists.

  • sha1_hash (str, optional) – Expected sha1 hash in hexadecimal digits. Will delete and re-download existing file when hash is specified but doesn’t match.

Returns

The file path of the downloaded file.

Return type

pathlib.Path

pugh_torch.helpers.most_recent_checkpoint(outputs_path)[source]

Get the most recent valid checkpoint path.

Searches over all the subdirectories in outputs_paths/ and returns the most recent found checkpoint path.

Relies on ModelCheckpoint(save_last=True)

Parameters

outputs_path (pathlib.Path or str) – The root Hydra outputs directory

Raises

FileNotFoundError – If the most recent checkpoint can not be found.

Returns

Most recent output path.

Return type

pathlib.Path

pugh_torch.helpers.most_recent_run(outputs_path, fmts=['%Y-%m-%d', '%H-%M-%S'])[source]

Get the most recent Hydra run folder.

Parameters
  • outputs_path (pathlib.Path or str) – The root Hydra outputs directory

  • fmts (list) – Optional list of string formats of how to interpret the folders.

Returns

Most recent output path.

Return type

pathlib.Path

pugh_torch.helpers.move_dim(input, src, dst)[source]

Pops dimension src and inserts it at dimension dst, shifting all the remaining dimensions down one.

Primarily useful when the dimensionality of input is unknown.

Example:

foo = torch.rand(2,1,5,10) bar = move_dim(foo, -1, 1) assert bar.shape == (2, 10, 1, 5)

Parameters
  • input (torch.Tensor)

  • src (int) – Source dimension index to pop.

  • dst (int) – Dimension index to insert. NOTE: this index is the dimension AFTER the src destination has been popped. This only matters if src <= dst. Also note: -1 means the second to last dimension. Example:

    foo = torch.rand(2,1,5,10) bar = move_dim(foo, 1, -1) assert bar.shape == (2, 5, 1, 10)

    To have the dst be the actual last dim, use input.ndim - 1 or the special value None

pugh_torch.helpers.plot_to_np(fig)[source]

Converts a matplotlib.pyplot figure into a numpy array.

Parameters

fig (matplotlib.figure.Figure) – Figure that you would like converted

Returns

Rasterized figure as an RGB-ordered numpy array

Return type

numpy.ndarray

pugh_torch.helpers.to_obj(s, index=0)[source]

Converts str to its respective object in caller’s scope.

This can be thought of converting the string into the object available in the caller’s scope.

Useful for specifying programmatic objects in Hydra configs.

Example:

# Assuming we are in a method where this is available assert self.foo.bar == to_obj(“self.foo.bar”)

Parameters
  • s (obj or str) – Object to convert into it’s callable equivalent. If this is already an object, it justs passes it through.

  • index (int) – Scope to search, where 0 means the caller’s scope, 1 is the caller’s caller scope, etc.

Returns

Callable equivalent represented by the input.

Return type

callable

pugh_torch.helpers.working_dir(newdir)[source]

Changes working directory and returns to previous on exit.

Usage:
with working_dir(“my_experiment_path”):

# do stuff within my_experiment_path/

# Now we are back in the original working directory

Parameters

new

Module contents

Top-level package for Pugh Torch.

pugh_torch.get_module_version()[source]