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. |
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
45 46 47 48 49 50 51 52 53 | |
render(task)
¶
Show progress rate.
Source code in sleap/util.py
48 49 50 51 52 53 | |
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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
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
270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
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
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
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
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
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
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
get_package_file(filename)
¶
Returns full path to specified file within sleap package.
Source code in sleap/util.py
286 287 288 289 290 | |
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
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
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
56 57 58 59 60 61 62 63 64 65 66 67 68 | |
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
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
parse_uri_path(uri)
¶
Parse a URI starting with 'file:///' to a posix path.
Source code in sleap/util.py
421 422 423 | |
plot_img(img, dpi=72, scale=1.0)
¶
Plot an image in a tight figure.
Source code in sleap/util.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
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
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | |
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
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
resize_image(img, scale)
¶
Resizes single image with shape (height, width, channels).
Source code in sleap/util.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
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. |
Source code in sleap/util.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |