General Utilities#

Classes#

class Shapes(iterable=(), /)[source]#

Bases: list[list[int] | list[list[int]]]

Shapes list that allows for indexing of multiple shapes.

check(idx)[source]#

Checks if the shape at the provided index is a single shape.

Parameters:
idxint

Index of the shape to check

Returns:
bool

True if the shape at the provided index is a single shape

get(idx, list_=True)[source]#

Get shape from the shapes list.

Overloads:

get(self, idx: slice, list_: bool) Shapes
get(self, idx: tuple[int, int], list_: bool) list[int]
get(self, idx: int, list_: Literal[False]) list[int]
get(self, idx: int, list_: Literal[True]) list[int] | list[list[int]]
Parameters:
idxint | tuple[int, int] | slice

Index of the shape to get, if tuple then first index is the layer index and second is the shape index if the layer has multiple shapes, if slice, returns a Shapes object with the sliced shapes

list_bool, Optional

If true, returns a list of shapes if the index contains multiple shapes, else returns a single shape, default = True

Returns:
list[int] | list[list[int]] | Shapes

Shape at the provided index

class TypedModuleList(modules=None)[source]#

Bases: ModuleList, Generic[ModuleT]

A typed version of nn.ModuleList that allows for type checking of the modules in the list. See nn.ModuleList for more information.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

append(module)[source]#

Append a given module to the end of the list.

Args:

module (nn.Module): module to append

extend(modules)[source]#

Append modules from a Python iterable to the end of the list.

Args:

modules (iterable): iterable of modules to append

insert(index, module)[source]#

Insert a given module before a given index in the list.

Args:

index (int): index to insert. module (nn.Module): module to insert

Functions#

ascii_plot(data, *, clear=False, x_num=20, height=6, text='', label='', data2=None)[source]#

Prints a simple ASCII graph of data per index.

Parameters:
datalist[float]

Data to plot

clearbool, Optional

If true, clears the previous graph, WARNING: this is very buggy and may not work properly, default = False

x_numint, Optional

Maximum number of epochs to display in the graph, default = 20

heightint, Optional

Height of the graph in lines, default = 6

textstr, Optional

Optional text to display at the end of the graph

labelstr, Optional

Label for the x-axis

data2list[float] | NoneType, Optional

Secondary data to plot

check_params(name, supported_params, in_params)[source]#

Checks if provided parameters are supported by the function.

Parameters:
namestr

Name of the function

supported_paramslist[str] | ndarray

Parameters supported by the function

in_paramsndarray

Input parameters of shape (N), where N is the number of parameters

compare_versions(cur_ver, min_ver)[source]#

Compares two version strings.

Parameters:
cur_verstr

Current version

min_verstr

Minimum version

Returns:
bool

True if current version is greater than or equal to minimum version

deep_merge(base, new)[source]#

Performs a deep merge of two dictionaries, equivalent to recursive base | new

Parameters:
basedict

Base dictionary

newdict

Dictionary to deep merge into base

Returns:
dict

Deep merged dictionary

dict_list_append(dict1, dict2, concat=False, balance=False)[source]#

Merges two dictionaries. If a key exists in both dictionaries and not concat, the data type must match. If data type is list or ndarray, the values are extended. If data type is any other type and not concat, the value from the second dictionary overrides the first, else if concat, the values are converted to a list and appended. If balance is True, all lists and ndarrays in each dictionary must have the same length, if a key is missing from one dictionary, it is added with None values to match the length of the other lists/ndarrays in that dictionary.

Parameters:
dict1dict[str, Any]

Primary dict to merge secondary dict into

dict2dict[str, Any]

Secondary dict to merge into primary dict

concatbool, Optional

If true, concatenates non list/ndarray types instead of overriding them, default = False

balancebool, Optional

If true, balances the lengths of lists/ndarrays in each dictionary so that each list/ndarray in the returning dictionary has the same length, default = False

Returns:
dict[str, Any]

First dict with second dict merged into it

dict_list_convert(data)[source]#

Converts a dictionary of lists to a list of dictionaries.

Parameters:
datadict[str, list[Any] | ndarray]

Dictionary of lists to convert

Returns:
list[dict[str, Any]]

List of dictionaries

get_device()[source]#

Gets the device for PyTorch to use.

Returns:
tuple[dict[str, Any], torch.device]

Arguments for the PyTorch DataLoader to use when loading data into memory and PyTorch device

label_change(data, in_label, *, one_hot=False, out_label=None)[source]#

Converts an array or tensor of class values to an array or tensor of class indices.

Parameters:
dataArrayT

Classes of shape (N) where N is the number of samples

in_labelArrayT

Unique class values of size (C) where C is the number of classes

one_hotbool, Optional

If the returned tensor should be 1D array of class indices or 2D one hot tensor if out_label is None or is an int, default = False

out_labelArrayT | NoneType, Optional

Unique class values of size (C) to transform data into, if None, then values will be indexes

Returns:
ArrayT

ndarray or Tensor of class indices of shape (N) and type float, or if one_hot is True, one hot tensor of shape (N,C) and type float

list_dict_convert(data, balance=False, concat=False)[source]#

Converts a list of dictionaries to a dictionary of lists

Parameters:
datalist[dict[str, float | ndarray]]

List of dictionaries to convert

balancebool, Optional

If true, balances the lengths of lists/ndarrays in each dictionary so that each list/ndarray in the returning dictionary has the same length, default = False

concatbool, Optional

If true, concatenates non list/ndarray types instead of overriding them, default = False

Returns:
dict[str, list[float | NoneType] | list[ndarray]]

Dictionary of lists

safe_globals(safe_package, modules, use_all=False, legacy_path_replace=None)[source]#

Adds all classes in the module from the specified package to the list of safe PyTorch classes when loading saved architectures.

Parameters:
safe_packagestr

Package that classes must belong to to be added to the safe globals

modulesModuleType | list[ModuleType]

Module(s) to add all classes from to the list of safe PyTorch classes when loading saved architectures

use_allbool, Optional

If true, only adds classes declared in module.__all__, default = False

legacy_path_replacetuple[str, str] | NoneType, Optional

If not None, replaces the first string with the second string in the module path when adding classes to the safe globals

save_name(num, states_dir, name)[source]#

Standardises the architecture save file naming.

Parameters:
numint | str

File number or name

states_dirstr

Directory of network saves

namestr

Name of the network

Returns:
str

Path to the network save file

suppress_logger_warnings(logger_name)[source]#

Suppresses warnings from a specified logger.

Parameters:
logger_namestr

Name of the logger to suppress warnings from