sleap.util#
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.
- Parameters:
cls – class to convert
- Returns:
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.- Parameters:
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.
- Returns:
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[DirEntry] [source]#
Returns list of files matching suffix, optionally searching in subdirs.
- Parameters:
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
- Returns:
List of os.DirEntry objects.
- sleap.util.frame_list(frame_str: str) List[int] | None [source]#
Converts ‘n-m’ string to list of ints.
- Parameters:
frame_str – string representing range
- Returns:
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).
- Parameters:
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.
- Raises:
FileNotFoundError – If the specified config file cannot be found.
- Returns:
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: str | None = None)[source]#
A simple wrapper around the JSON encoder we are using.
- Parameters:
d – The dict to write.
filename – The filename to write to.
- Returns:
None
- sleap.util.json_loads(json_str: str) Dict [source]#
A simple wrapper around the JSON decoder we are using.
- Parameters:
json_str – JSON string to decode.
- Returns:
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.
- Parameters:
flat_dict – The dictionary to convert. Keys should be strings with
scope.foo
format.exclude_nodes – Whether to exclude items where value is None.
- Returns:
- Dictionary in which keys are
scope
and values are dictionary with foo
(etc) as keys and original value ofscope.foo
as value.
- Dictionary in which keys are
- sleap.util.parse_uri_path(uri: str) str [source]#
Parse a URI starting with ‘file:///’ to a posix path.
- sleap.util.save_dict_to_hdf5(h5file: 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.- Parameters:
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.
- Raises:
ValueError – If type for item in dict cannot be saved.
- Returns:
None
- 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.
- Parameters:
seq – The list to remove duplicates from.
- Returns:
The unique elements from the input list extracted in original order.
- sleap.util.usable_cpu_count() int [source]#
Gets number of CPUs usable by the current process.
Takes into consideration cpusets restrictions.
- Returns:
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.
- Parameters:
filename_a – first path to check
filename_b – path to check against first path
- Returns:
True if the paths probably match.