Sessions API Reference¶
The vrAnalysis.sessions module provides classes for loading and managing VR session data. It's the base class for all session data objects. It provides critical functionality for managing session data. In addition, it contains the API for handling onedata files, which are the main way that session data is stored and loaded.
Main Classes and Functions¶
SessionData
dataclass
¶
Bases: ABC
Base class for session data.
This abstract base class provides the core functionality for managing VR session data, including loading and saving onedata files, managing session identifiers, and providing a flexible values namespace for storing arbitrary data.
Attributes:
| Name | Type | Description |
|---|---|---|
mouse_name |
str
|
Name of the mouse for this session. |
date |
str, datetime, or PrettyDatetime
|
Date of the session. Will be converted to string format. |
session_id |
(str, optional)
|
Optional session identifier. Default is None. |
data_path |
Path or str
|
Path to the session data directory. Set automatically during initialization. |
values |
SimpleNamespace
|
Namespace for storing arbitrary data. Useful for flexible storing of small bytes data. |
one_cache |
dict
|
Cache for buffering onedata files to avoid repeated disk reads. |
Source code in vrAnalysis/sessions/base.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 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 419 420 421 422 423 424 425 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 452 453 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 491 492 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 557 558 559 560 561 562 563 564 565 566 567 568 | |
one_path
property
¶
Path to onedata directory.
Returns:
| Type | Description |
|---|---|
Path
|
Path object pointing to the onedata subdirectory within the session data path. |
recipe_loaders
property
¶
Dictionary of loaders for loading data from recipes.
This property enables specialized loading of onedata stored with recipes. If not specified, attempting to load a recipe will raise an error.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary mapping loader type strings to loader functions. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If this property is not overridden by a subclass that uses recipes. |
Notes
Subclasses that use LoadingRecipe objects should override this property to provide a dictionary of loader functions. Each loader function should accept a source_arg and optional kwargs.
recipe_transforms
property
¶
Dictionary of transforms for applying to data when loading recipes.
This property enables specialized transforms of onedata stored with recipes. If not specified, attempting to load a recipe that requires a transform will raise an error.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary mapping transform names to transform functions. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If this property is not overridden by a subclass that uses recipes with transforms. |
Notes
Subclasses that use LoadingRecipe objects with transforms should override this property to provide a dictionary of transform functions. Each transform function should accept data and return transformed data.
session_name
property
¶
Return the session identifier as a tuple.
Returns:
| Type | Description |
|---|---|
tuple[str]
|
Tuple containing (mouse_name, date) or (mouse_name, date, session_id) if session_id is provided. |
spks
abstractmethod
property
¶
Neural spks data.
This property must be implemented by all subclasses. It should return the neural spike data for the session.
Returns:
| Type | Description |
|---|---|
ndarray
|
Neural spike data array. Shape and format depend on the subclass implementation. |
Notes
This is always required for all session data objects.
__post_init__()
¶
Post-initialization method to set all properties specific to subclasses.
This method is called automatically after dataclass initialization. It formats the date, initializes the data path, and calls additional loading steps for subclasses.
Source code in vrAnalysis/sessions/base.py
clear_cache(file_names=None)
¶
Clear cached data to free memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_names
|
list[str]
|
List of specific file names (with extension) to remove from cache. If None, the entire cache is cleared. Default is None. |
None
|
Notes
This method helps manage memory usage by removing cached onedata from memory. The files themselves are not deleted, only the in-memory cache.
Source code in vrAnalysis/sessions/base.py
clear_one_data(one_file_names=None, certainty=False)
¶
Clear onedata files from the session directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
one_file_names
|
list[str]
|
List of specific onedata file names (without extension) to clear. If None, all onedata files will be cleared. Default is None. |
None
|
certainty
|
bool
|
Safety flag that must be set to True to actually delete files. Default is False. |
False
|
Notes
This operation is destructive and cannot be undone. The certainty flag must be set to True to prevent accidental deletions.
Source code in vrAnalysis/sessions/base.py
get_one_filename(*names)
¶
Create onedata filename from an arbitrary length list of names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*names
|
str
|
Variable number of strings to join into a filename. |
()
|
Returns:
| Type | Description |
|---|---|
str
|
Filename with components joined by "." and ".npy" extension added. Example: get_one_filename("mpci", "roiActivityF") returns "mpci.roiActivityF.npy". |
Source code in vrAnalysis/sessions/base.py
get_saved_one()
¶
Get all saved onedata files.
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of Path objects for all .npy and .npz files in the onedata directory. |
Source code in vrAnalysis/sessions/base.py
get_value(key)
¶
Get value from the session stored in the values namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key name of the value to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Value stored under the specified key in the values namespace. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the key does not exist in the values namespace. |
Source code in vrAnalysis/sessions/base.py
loadone(*names, force=False, sparse=False, keep_sparse=False)
¶
Load data, either directly or by following a recipe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*names
|
str
|
Sequence of strings to join into filename (e.g., "mpci", "roiActivityF" -> "mpci.roiActivityF.npy"). |
()
|
force
|
bool
|
If True, reload data even if it is already in the cache. Default is False. |
False
|
sparse
|
bool
|
If True, load as sparse array (.npz format). Default is False. |
False
|
keep_sparse
|
bool
|
If True and data is sparse, return a sparse array. Otherwise, convert sparse arrays to dense. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Loaded data. May be transformed if loaded from a recipe. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested onedata file does not exist. |
NotImplementedError
|
If loading a recipe but recipe_loaders or recipe_transforms are not properly configured. |
Notes
Data is cached in one_cache after loading to avoid repeated disk reads. If a LoadingRecipe is encountered, it will be executed using the recipe_loaders and recipe_transforms properties.
Source code in vrAnalysis/sessions/base.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 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 | |
print_saved_one(include_path=False, include_extension=False)
¶
Get formatted list of all saved onedata files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_path
|
bool
|
If True, include the full path in the output. Default is False. |
False
|
include_extension
|
bool
|
If True, include the file extension in the output. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of formatted file names (and optionally paths) for all saved onedata files. |
Source code in vrAnalysis/sessions/base.py
saveone(data, *names, sparse=False)
¶
Save data directly or as a loading recipe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray or LoadingRecipe
|
Data to save. Can be a numpy array or a LoadingRecipe object. |
required |
*names
|
str
|
Sequence of strings to join into filename (e.g., "mpci", "roiActivityF" -> "mpci.roiActivityF.npy"). |
()
|
sparse
|
bool
|
If True, save as sparse array (.npz format). Data must be a scipy.sparse.csc_array. Default is False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If sparse=True but data is not a scipy.sparse.csc_array. |
Notes
When saving a numpy array (not sparse), the data is also cached in one_cache for efficient subsequent access. LoadingRecipe objects are saved as numpy arrays containing dictionaries with a special marker.
Source code in vrAnalysis/sessions/base.py
session_print(joinby='/')
¶
Generate string representation of session name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
joinby
|
str
|
String to join session name components. Default is "/". |
'/'
|
Returns:
| Type | Description |
|---|---|
str
|
String representation of the session name with components joined by the specified separator. |
Source code in vrAnalysis/sessions/base.py
set_value(key, value)
¶
Set value in the session stored in the values namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key name to store the value under. |
required |
value
|
Any
|
Value to store in the values namespace. |
required |
Source code in vrAnalysis/sessions/base.py
LoadingRecipe
¶
Source code in vrAnalysis/sessions/base.py
__init__(loader_type, source_arg, transforms=None, **kwargs)
¶
Represents instructions for loading data.
Use case: Save a recipe to load data from an existing location instead of resaving the data with a new name. Will save memory on the device and generally have a very small overhead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader_type
|
str
|
String identifying the loading method (e.g., 'numpy', 's2p'). |
required |
source_arg
|
str
|
String identifying the source data (e.g. 'F', 'ops'). |
required |
transforms
|
list
|
List of transformation operations to apply. Default is None. |
None
|
**kwargs
|
Additional arguments for the loader. |
{}
|
Source code in vrAnalysis/sessions/base.py
from_dict(data)
classmethod
¶
Create recipe from dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Dictionary containing recipe information with keys: loader_type, source_arg, transforms, and kwargs. |
required |
Returns:
| Type | Description |
|---|---|
LoadingRecipe
|
LoadingRecipe instance created from the dictionary. |
Source code in vrAnalysis/sessions/base.py
is_recipe(data)
classmethod
¶
Check if the loaded data is actually a recipe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Numpy array that may contain a recipe dictionary. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the data contains a recipe marker, False otherwise. |
Source code in vrAnalysis/sessions/base.py
to_dict()
¶
Convert recipe to dictionary for serialization.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary representation of the recipe with marker, loader_type, source_arg, transforms, and kwargs. |