A miscellaneous set of utility functions. Try not to put things in here unless they really have no other place.

sleap.util.attr_to_dtype(cls: Any)[source]

Converts classes with basic types to numpy composite dtypes.


cls – class to convert


numpy dtype.

sleap.util.dict_cut(d: Dict, a: int, b: int)Dict[source]

Helper function for creating subdictionary by numeric indexing of items.

Assumes that dict.items() will have a fixed order.

  • d – The dictionary to “split”

  • a – Start index of range of items to include in result.

  • b – End index of range of items to include in result.


A dictionary that contains a subset of the items in the original dict.

sleap.util.find_files_by_suffix(root_dir: str, suffix: str, prefix: str = '', depth: int = 0)List[posix.DirEntry][source]

Returns list of files matching suffix, optionally searching in subdirs.

  • root_dir – Path to directory where we start searching

  • suffix – File suffix to match (e.g., ‘.json’)

  • prefix – Optional file prefix to match

  • depth – How many subdirectories deep to keep searching


List of os.DirEntry objects.

sleap.util.frame_list(frame_str: str)Optional[List[int]][source]

Converts ‘n-m’ string to list of ints.


frame_str – string representing range


List of ints, or None if string does not represent valid range.

sleap.util.get_config_file(shortname: str, ignore_file_not_found: bool = False, get_defaults: bool = False)str[source]

Returns the full path to the specified config file.

The config file will be at ~/.sleap/<version>/<shortname>

If that file doesn’t yet exist, we’ll look for a <shortname> file inside the package config directory (sleap/config) and copy the file into the user’s config directory (creating the directory if needed).

  • shortname – The short filename, e.g., shortcuts.yaml

  • ignore_file_not_found – If True, then return path for config file regardless of whether it exists.

  • get_defaults – If True, then just return the path to default config file.


FileNotFoundError – If the specified config file cannot be found.


The full path to the specified config file.

sleap.util.get_package_file(filename: str)str[source]

Returns full path to specified file within sleap package.

sleap.util.json_dumps(d: Dict, filename: Optional[str] = None)[source]

A simple wrapper around the JSON encoder we are using.

  • d – The dict to write.

  • filename – The filename to write to.



sleap.util.json_loads(json_str: str)Dict[source]

A simple wrapper around the JSON decoder we are using.


json_str – JSON string to decode.


Result of decoding JSON string.

sleap.util.make_scoped_dictionary(flat_dict: Dict[str, Any], exclude_nones: bool = True)Dict[str, Dict[str, Any]][source]

Converts dictionary with scoped keys to dictionary of dictionaries.

  • flat_dict – The dictionary to convert. Keys should be strings with format.

  • exclude_nodes – Whether to exclude items where value is None.


Dictionary in which keys are scope and values are dictionary with

foo (etc) as keys and original value of as value.

sleap.util.save_dict_to_hdf5(h5file: h5py._hl.files.File, path: str, dic: dict)[source]

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.

  • h5file – The HDF5 filename object to save the data to. Assume it is open.

  • path – The path to group save the dict under.

  • dic – The dict to save.


ValueError – If type for item in dict cannot be saved.



sleap.util.uniquify(seq: Iterable[Hashable])List[source]

Returns unique elements from list, preserving order.

Note: This will not work on Python 3.5 or lower since dicts don’t preserve order.


seq – The list to remove duplicates from.


The unique elements from the input list extracted in original order.


Gets number of CPUs usable by the current process.

Takes into consideration cpusets restrictions.


The number of usable cpus

sleap.util.weak_filename_match(filename_a: str, filename_b: str)bool[source]

Check if paths probably point to same file.

Compares the filename and names of two directories up.

  • filename_a – first path to check

  • filename_b – path to check against first path


True if the paths probably match.