util
sleap.util
¶
A miscellaneous set of utility functions.
Note: to avoid circular imports, this file is used for utility functions that do not depend on any other modules in the package.
Try not to put things in here unless they really have no other place.
Classes:
| Name | Description |
|---|---|
RateColumn |
Renders the progress rate. |
Functions:
| Name | Description |
|---|---|
attr_to_dtype |
Converts classes with basic types to numpy composite dtypes. |
dict_cut |
Helper function for creating subdictionary by numeric indexing of items. |
find_files_by_suffix |
Returns list of files matching suffix, optionally searching in subdirs. |
frame_list |
Converts 'n-m' string to list of ints. |
get_config_file |
Returns the full path to the specified config file. |
get_package_file |
Returns full path to specified file within sleap package. |
imgfig |
Create a tight figure for image plotting. |
json_dumps |
A simple wrapper around the JSON encoder we are using. |
json_loads |
A simple wrapper around the JSON decoder we are using. |
make_scoped_dictionary |
Converts dictionary with scoped keys to dictionary of dictionaries. |
parse_uri_path |
Parse a URI starting with 'file:///' to a posix path. |
plot_img |
Plot an image in a tight figure. |
plot_instance |
Plot a single instance with edge coloring. |
plot_instances |
Plot a list of instances with identity coloring. |
resize_image |
Resizes single image with shape (height, width, channels). |
save_dict_to_hdf5 |
Saves dictionary to an HDF5 file. |
show_sleap_nn_installation_message |
Show a Qt popup message about SLEAP-NN installation requirements. |
uniquify |
Returns unique elements from list, preserving order. |
usable_cpu_count |
Gets number of CPUs usable by the current process. |
weak_filename_match |
Check if paths probably point to same file. |
RateColumn
¶
Bases: ProgressColumn
Renders the progress rate.
Methods:
| Name | Description |
|---|---|
render |
Show progress rate. |
Source code in sleap/util.py
render(task)
¶
Show progress rate.
attr_to_dtype(cls)
¶
Converts classes with basic types to numpy composite dtypes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Any
|
class to convert |
required |
Returns:
| Type | Description |
|---|---|
|
numpy dtype. |
Source code in sleap/util.py
dict_cut(d, a, b)
¶
Helper function for creating subdictionary by numeric indexing of items.
Assumes that dict.items() will have a fixed order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Dict
|
The dictionary to "split" |
required |
a
|
int
|
Start index of range of items to include in result. |
required |
b
|
int
|
End index of range of items to include in result. |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
A dictionary that contains a subset of the items in the original dict. |
Source code in sleap/util.py
find_files_by_suffix(root_dir, suffix, prefix='', depth=0)
¶
Returns list of files matching suffix, optionally searching in subdirs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_dir
|
str
|
Path to directory where we start searching |
required |
suffix
|
str
|
File suffix to match (e.g., '.json') |
required |
prefix
|
str
|
Optional file prefix to match |
''
|
depth
|
int
|
How many subdirectories deep to keep searching |
0
|
Returns:
| Type | Description |
|---|---|
List[DirEntry]
|
List of os.DirEntry objects. |
Source code in sleap/util.py
frame_list(frame_str)
¶
Converts 'n-m' string to list of ints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame_str
|
str
|
string representing range |
required |
Returns:
| Type | Description |
|---|---|
Optional[List[int]]
|
List of ints, or None if string does not represent valid range. |
Source code in sleap/util.py
get_config_file(shortname, ignore_file_not_found=False, get_defaults=False)
¶
Returns the full path to the specified config file.
The config file will be at ~/.sleap/
If that file doesn't yet exist, we'll look for a
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shortname
|
str
|
The short filename, e.g., shortcuts.yaml |
required |
ignore_file_not_found
|
bool
|
If True, then return path for config file regardless of whether it exists. |
False
|
get_defaults
|
bool
|
If True, then just return the path to default config file. |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified config file cannot be found. |
Returns:
| Type | Description |
|---|---|
str
|
The full path to the specified config file. |
Source code in sleap/util.py
get_package_file(filename)
¶
imgfig(size=6, dpi=72, scale=1.0)
¶
Create a tight figure for image plotting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
float | tuple
|
Scalar or 2-tuple specifying the (width, height) of the figure in inches. If scalar, will assume equal width and height. |
6
|
dpi
|
int
|
Dots per inch, controlling the resolution of the image. |
72
|
scale
|
float
|
Factor to scale the size of the figure by. This is a convenience for increasing the size of the plot at the same DPI. |
1.0
|
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib.figure.Figure to use for plotting. |
Source code in sleap/util.py
json_dumps(d, filename=None)
¶
A simple wrapper around the JSON encoder we are using.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Dict
|
The dict to write. |
required |
filename
|
str
|
The filename to write to. |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in sleap/util.py
json_loads(json_str)
¶
A simple wrapper around the JSON decoder we are using.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_str
|
str
|
JSON string to decode. |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
Result of decoding JSON string. |
Source code in sleap/util.py
make_scoped_dictionary(flat_dict, exclude_nones=True)
¶
Converts dictionary with scoped keys to dictionary of dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flat_dict
|
Dict[str, Any]
|
The dictionary to convert. Keys should be strings with
|
required |
exclude_nodes
|
Whether to exclude items where value is None. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, Any]]
|
Dictionary in which keys are |
Source code in sleap/util.py
parse_uri_path(uri)
¶
plot_img(img, dpi=72, scale=1.0)
¶
Plot an image in a tight figure.
Source code in sleap/util.py
plot_instance(instance, skeleton=None, cmap=None, color_by_node=False, lw=2, ms=10, bbox=None, scale=1.0, **kwargs)
¶
Plot a single instance with edge coloring.
Source code in sleap/util.py
plot_instances(instances, skeleton=None, cmap=None, color_by_track=False, tracks=None, **kwargs)
¶
Plot a list of instances with identity coloring.
Source code in sleap/util.py
resize_image(img, scale)
¶
Resizes single image with shape (height, width, channels).
Source code in sleap/util.py
save_dict_to_hdf5(h5file, path, dic)
¶
Saves dictionary to an HDF5 file.
Calls itself recursively if items in dictionary are not
np.ndarray, np.int64, np.float64, str, or bytes.
Objects must be iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h5file
|
File
|
The HDF5 filename object to save the data to. Assume it is open. |
required |
path
|
str
|
The path to group save the dict under. |
required |
dic
|
dict
|
The dict to save. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If type for item in dict cannot be saved. |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in sleap/util.py
show_sleap_nn_installation_message()
¶
Show a Qt popup message about SLEAP-NN installation requirements.
This function displays a popup window informing users that sleap-nn is not installed and provides instructions for enabling training functionality.
Source code in sleap/util.py
uniquify(seq)
¶
Returns unique elements from list, preserving order.
Note: This will not work on Python 3.5 or lower since dicts don't preserve order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq
|
Iterable[Hashable]
|
The list to remove duplicates from. |
required |
Returns:
| Type | Description |
|---|---|
List
|
The unique elements from the input list extracted in original order. |
Source code in sleap/util.py
usable_cpu_count()
¶
Gets number of CPUs usable by the current process.
Takes into consideration cpusets restrictions.
Returns:
| Type | Description |
|---|---|
int
|
The number of usable cpus |
Source code in sleap/util.py
weak_filename_match(filename_a, filename_b)
¶
Check if paths probably point to same file.
Compares the filename and names of two directories up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename_a
|
str
|
first path to check |
required |
filename_b
|
str
|
path to check against first path |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the paths probably match. |