state
sleap.gui.state
¶
Module with object for storing and accessing gui state variables.
Each project open in the GUI will have its own instance of GuiState, as will any
video player (QtVideoPlayer widget) which shows different images than in the
main app GUI (e.g., QtImageDirectoryWidget used for visualizing results
during training).
The state object makes it easier to separate code which updates state (e.g., sets current frame or current video) and code which updates the GUI in response to state-change.
The state object is effectively a dictionary which allows you to bind functions to keys so that the functions each get called when the value for that key changes (or is initially set).
Note that there's no type checking, e.g., to ensure that state["video"] is
being set to a Video object. This is a potential source of bugs since
callbacks connected to some key will often assume that value will always be of
some specific type.
Classes:
| Name | Description |
|---|---|
GuiState |
Class for passing persistent gui state variables. |
Functions:
| Name | Description |
|---|---|
compute_qc_visibility |
Map a QC display mode + selection -> per-instance visibility flags. |
instance_shows_non_visible |
Return whether THIS instance's non-visible (occluded/NaN) nodes are drawn. |
instance_visible |
Return the effective canvas visibility for an instance. |
GuiState
¶
Bases: object
Class for passing persistent gui state variables.
Arbitrary variables can be set, bools can be toggled, and callbacks can be automatically triggered on variable changes.
This allows us to separate controls (which set state variables) and views (which can update themselves when the relevant state variables change).
Methods:
| Name | Description |
|---|---|
__contains__ |
Does state contain key? |
__delitem__ |
Removes key from state. Doesn't trigger callbacks. |
__getitem__ |
Gets value for key, or None if no value. |
__setitem__ |
Sets value for key, triggering any callbacks bound to key. |
connect |
Connects one or more callbacks for state variable. |
emit |
Trigger callbacks for state variable. |
get |
Getter with support for default value. |
increment |
Increment numeric value for specified key. |
increment_in_list |
Advance to subsequent (or prior) value in list. |
set |
Functional version of setter (for use in lambdas). |
toggle |
Toggle boolean value for specified key. |
Source code in sleap/gui/state.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 345 346 347 348 349 350 351 352 353 354 355 356 | |
__contains__(key)
¶
__delitem__(key)
¶
__getitem__(key)
¶
__setitem__(key, value)
¶
Sets value for key, triggering any callbacks bound to key.
connect(key, callbacks)
¶
Connects one or more callbacks for state variable.
Callbacks are called (triggered) whenever the state is changed, i.e., when the value for some key is set either (i) initially or (ii) to a different value than the current value.
This is analogous to connecting a function to a Qt slot.
Callback should take a single arg, which will be the current (new) value of whatever state var is triggering the callback.
Source code in sleap/gui/state.py
emit(key)
¶
Trigger callbacks for state variable.
This calls each callback for the specified key, without needing to change the value of the key.
This is analogous to emitting a Qt signal.
Source code in sleap/gui/state.py
get(key, default=NO_ARG)
¶
increment(key, step=1, mod=None, default=0)
¶
Increment numeric value for specified key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
GSVarType
|
The key. |
required |
step
|
int
|
What to add to current value. |
1
|
mod
|
Optional[int]
|
Wrap value (i.e., apply modulus) if not None. |
None
|
default
|
int
|
Set value to this if there's no current value for key. |
0
|
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in sleap/gui/state.py
increment_in_list(key, value_list, reverse=False)
¶
Advance to subsequent (or prior) value in list.
When current value for key is not found in list, the value is set to the first (or last, if reverse) item in list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
GSVarType
|
The key. |
required |
value_list
|
list
|
List of values of any type which supports equality check. |
required |
reverse
|
bool
|
Whether to use next or previous item in value list. |
False
|
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in sleap/gui/state.py
set(key, value)
¶
compute_qc_visibility(mode, selected_instance, instances, global_show_non_visible)
¶
Map a QC display mode + selection -> per-instance visibility flags.
Returns {id(instance): (visible, show_non_visible)}. An empty dict is the
"manual" sentinel: the caller leaves the per-instance transient state alone.
Selection match is by id(); if selected_instance is None or not in
instances, the selection-relative modes fall back to the FIRST instance
(so the mode stays visible and the canvas is never blank), narrowing to the
real instance once one is selected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
One of the |
required |
selected_instance
|
Any
|
The currently selected instance, or |
required |
instances
|
list
|
The instances shown on the current frame. |
required |
global_show_non_visible
|
bool
|
The global "show non-visible nodes" flag, used as a master gate: when off, no instance draws occluded keypoints (even the focused one); when on, the mode decides which instances show them. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dict |
Source code in sleap/gui/state.py
instance_shows_non_visible(state, instance, global_default)
¶
Return whether THIS instance's non-visible (occluded/NaN) nodes are drawn.
Orthogonal to instance_visible: that decides whether the instance is drawn
at all; this decides whether its occluded keypoints draw. Per-instance
overrides (the "Invisible Nodes" column / a non-manual QC mode) beat the
global "show non-visible nodes" flag; absent override -> the global default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GuiState
|
The |
required |
instance
|
Any
|
The |
required |
global_default
|
bool
|
The current global "show non-visible nodes" flag, used when there is no per-instance override. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in sleap/gui/state.py
instance_visible(state, instance)
¶
Return the effective canvas visibility for an instance.
This is the single source of truth shared by the table model (which sets the state) and the instance overlay (which applies it on every replot), so the two cannot drift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GuiState
|
The |
required |
instance
|
Any
|
The |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|