Skip to content

The functions module

as_hsv(image)

Convert an RGB image into HSV format

Parameters:

Name Type Description Default
image ndarray

The input RGB image.

required

Returns:

Type Description
ndarray

The HSV image.

as_rgb(image, greyscale=False)

Convert an image into an RGB version.

Parameters:

Name Type Description Default
image ndarray

The image to convert.

required
greyscale bool

Switch to convert the image to greyscale. Defaults to False.

False

Returns:

Type Description
ndarray

The RGB image.

camel2snake(string)

Convert a CamelCase string into a snake_case version.

Parameters:

Name Type Description Default
string str

The input CamelCase string.

required

Returns:

Type Description
str

The output snake_case string.

create_asset_dir(namespace, subdir)

Create a managed asset directory, usually for a data source.

Parameters:

Name Type Description Default
namespace str

A namespace (category) for the source type.

required
subdir str

A subdirectory corresponding to the source.

required

Returns:

Type Description
Path

A Path to the asset directory.

ensure_dir(path)

Resolve and expand a directory path and create the directory if it doesn't exist.

Parameters:

Name Type Description Default
path Path | str | None

A directory path.

required

Returns:

Type Description
Path

The (potentially newly created) expanded path.

filter_files(path, pattern)

Filter files in a directory based on a pattern.

Parameters:

Name Type Description Default
path Path | str

The path (a directory) to traverse.

required
pattern str

The regex pattern to apply.

required

Raises:

Type Description
TypeError

Raised if a file is passed to the function.

Returns:

Type Description

The filtered file paths.

hide_home(dir)

A very simple function that replaces the home directory with a tilde.

Useful for printing the home directory in notebooks without revealing private information.

Parameters:

Name Type Description Default
dir Path

The directory to process.

required

Returns:

Type Description
str

The directory with a tilde (~) instead of the user's home directory.

is_notebook()

Determine if the caller is running in a Jupyter notebook.

Courtesy of https://stackoverflow.com/a/39662359/4639195.

Returns:

Name Type Description
bool bool

True if running in a notebook.

make_colourmap(labels, cmap='jet')

Create a dictionary of colours (used for visualising instances).

Parameters:

Name Type Description Default
labels dict | list | tuple

A dictionary of labels.

required
cmap str

Colourmap. Defaults to "jet".

'jet'

Returns:

Name Type Description
dict dict

Dictionary of class/colour associations.

make_path(path, root=None, suffix=None)

Construct a path (a file or a directory) with optional modifications.

Parameters:

Name Type Description Default
path str | Path

The original path.

required
root Path | None

An optional root path. Defaults to None.

None
suffix str | None

An optional (replacement) suffix. Defaults to None.

None

Returns:

Type Description

The resolved path.

open_image(path, as_grey=False)

Open an image as a NumPy array.

Parameters:

Name Type Description Default
path Path

The path to the image file.

required
as_grey bool

Open the image as a greyscale.

False

Returns:

Type Description
ndarray

A NumPy array containing the image.

show_dir_tree(dir)

Create and return a tree-like representation of a directory.

TODO: Limit the depth, etc. Perhaps use **kwargs to pass options to seedir.

Returns:

Type Description
str | None

The directory structure with the subdirectories and

str | None

files that they contain.