Base Dataloader
MovieDataSet
MovieDataSet(
movies: Float[
ndarray | Tensor, "n_channels n_frames h w"
],
responses: Float[ndarray | Tensor, "n_frames n_neurons"]
| dict[str, Any],
roi_ids: Float[ndarray, " n_neurons"] | None,
roi_coords: Float[ndarray, "n_neurons 2"] | None,
group_assignment: Float[ndarray, " n_neurons"] | None,
split: str
| Literal["train", "validation", "val", "test"],
chunk_size: int,
)
Bases: Dataset
A dataset class for handling movie data and corresponding neural responses.
| PARAMETER | DESCRIPTION |
|---|---|
movies
|
The movie data.
TYPE:
|
responses
|
TYPE:
|
roi_ids
|
A list of ROI IDs.
TYPE:
|
roi_coords
|
A list of ROI coordinates.
TYPE:
|
group_assignment
|
A list of group assignments (cell types).
TYPE:
|
split
|
TYPE:
|
chunk_size
|
The size of the chunks to split the data into.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
samples |
A tuple containing movie data and neural responses.
TYPE:
|
test_responses_by_trial |
TYPE:
|
roi_ids |
A list of region of interest (ROI) IDs.
TYPE:
|
chunk_size |
The size of the chunks to split the data into.
TYPE:
|
mean_response |
The mean response per neuron.
TYPE:
|
group_assignment |
A list of group assignments.
TYPE:
|
roi_coords |
A list of ROI coordinates.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__getitem__ |
Returns a DataPoint object for the given index or slice. |
movies |
Returns the movie data. |
responses |
Returns the neural responses. |
__len__ |
Returns the number of chunks of clips and responses used for training. |
__str__ |
Returns a string representation of the dataset. |
__repr__ |
Returns a string representation of the dataset. |
Source code in openretina/data_io/base_dataloader.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
MovieSampler
MovieSampler(
start_indices: list[int],
split: str
| Literal["train", "validation", "val", "test"],
chunk_size: int,
movie_length: int,
scene_length: int,
allow_over_boundaries: bool = False,
)
Bases: Sampler
A custom sampler for selecting movie frames for training, validation, or testing.
| PARAMETER | DESCRIPTION |
|---|---|
start_indices
|
List of starting indices for the movie sections to select.
TYPE:
|
split
|
The type of data split.
TYPE:
|
chunk_size
|
The size of each contiguous chunk of frames to select.
TYPE:
|
movie_length
|
The total length of the movie.
TYPE:
|
scene_length
|
The length of each scene, if the movie is divided in any scenes. Defaults to None.
TYPE:
|
allow_over_boundaries
|
Whether to allow selected chunks to go over scene boundaries. Defaults to False.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
indices |
The starting indices for the movie sections to sample.
TYPE:
|
split |
The type of data split.
TYPE:
|
chunk_size |
The size of each chunk of frames.
TYPE:
|
movie_length |
The total length of the movie.
TYPE:
|
scene_length |
The length of each scene, if the movie is made up of scenes.
TYPE:
|
allow_over_boundaries |
Whether to allow chunks to go over scene boundaries.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__iter__ |
Returns an iterator over the sampled indices. |
__len__ |
Returns the number of starting indices (which will corresponds to the number of sampled clips). |
Source code in openretina/data_io/base_dataloader.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
NeuronDataSplit
NeuronDataSplit(
responses: ResponsesTrainTestSplit,
val_clip_idx: List[int],
num_clips: int,
clip_length: int,
key: Optional[dict] = None,
**kwargs,
)
Preprocesses ResponsesTrainTestSplit objects before feeding them to dataloaders.
Responsibilities
- Remove validation clips from the training responses while storing them separately.
- Expose torch tensors for train/val/test splits via
response_dict. - Surface averaged and per-trial test responses per each test stimulus name so that downstream
MovieDataSetinstances can providedataset.test_responses_by_trial.
Initialize the NeuronData object. Boilerplate class to compute and store neuron data train/test/validation splits before feeding into a dataloader
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key information for the neuron data, includes date, exp_num, experimenter, field_id, stim_id.
TYPE:
|
responses
|
The train and test responses of neurons.
TYPE:
|
val_clip_idx
|
The indices of validation clips.
TYPE:
|
num_clips
|
The number of clips.
TYPE:
|
clip_length
|
The length of each clip.
TYPE:
|
key
|
Additional key information.
TYPE:
|
Source code in openretina/data_io/base_dataloader.py
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 | |
response_dict
property
response_dict: dict
Create and return a dictionary of neural responses for train, validation, and test datasets.
Structure
{ "train": Tensor[T_train, neurons], "validation": Tensor[T_val, neurons], "test": { stimulus_name: { "avg": Tensor[T_test, neurons], "by_trial": Optional[Tensor[trials, T_test, neurons]] }, ... } }
response_dict_test
property
response_dict_test: dict[str, dict[str, Tensor | None]]
Torch representation of the averaged and per-trial test responses keyed by stimulus name.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, dict[str, Tensor | None]]
|
{ stimulus_name: {"avg": Tensor[t_test, neurons], "by_trial": Optional[Tensor[trials, t_test, neurons]]} |
dict[str, dict[str, Tensor | None]]
|
} |
split_data_train_val
split_data_train_val() -> tuple[ndarray, ndarray]
Compute validation responses and updated train responses stripped from validation clips. Can deal with unsorted validation clip indices, and parallels the way movie validation clips are handled.
| RETURNS | DESCRIPTION |
|---|---|
tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: The updated train and validation responses. |
Source code in openretina/data_io/base_dataloader.py
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 | |
generate_movie_splits
generate_movie_splits(
movie_train,
movie_test: dict[str, ndarray],
val_clip_idc: list[int],
num_clips: int,
clip_length: int,
) -> tuple[Tensor, Tensor, dict[str, Tensor]]
Split training movies into train/validation subsets and convert test movies to tensors.
Source code in openretina/data_io/base_dataloader.py
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 | |
gen_shifts_with_boundaries
gen_shifts_with_boundaries(
clip_bounds: list[int] | ndarray,
start_indices: list[int] | ndarray,
clip_chunk_size: int = 50,
) -> list[int]
Generate shifted indices based on clip bounds and start indices. Assumes that the original start indices are already within the clip bounds. If they are not, it changes the overflowing indexes to respect the closest bound.
| PARAMETER | DESCRIPTION |
|---|---|
clip_bounds
|
A list of clip bounds.
TYPE:
|
start_indices
|
A list of start indices.
TYPE:
|
clip_chunk_size
|
The size of each clip chunk. Defaults to 50.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
A list of shifted indices.
TYPE:
|
Source code in openretina/data_io/base_dataloader.py
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 | |
handle_missing_start_indices
handle_missing_start_indices(
movie_length: int,
chunk_size: int | None,
scene_length: int | None,
split: str,
) -> list[int]
Handle missing start indices for different splits of the dataset.
Parameters: movies (np.ndarray or torch.Tensor): The movies data, as an array. chunk_size (int or None): The size of each chunk for training split. Required if split is "train". scene_length (int or None): The length of each scene. Required if split is "validation" or "val". split (str): The type of split, one of "train", "validation", "val", or "test".
Returns: dict or list: The generated or provided start indices for each movie.
Raises: AssertionError: If chunk_size is not provided for training split when start_indices is None. AssertionError: If scene_length is not provided for validation split when start_indices is None. NotImplementedError: If start_indices is None and split is not one of "train", "validation", "val", or "test".
Source code in openretina/data_io/base_dataloader.py
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 | |
get_movie_dataloader
get_movie_dataloader(
movie: Float[
ndarray | Tensor, "n_channels n_frames h w"
],
responses: Float[ndarray | Tensor, "n_frames n_neurons"]
| dict[str, Any],
*,
split: str
| Literal["train", "validation", "val", "test"],
scene_length: int,
chunk_size: int,
batch_size: int,
start_indices: list[int] | None = None,
roi_ids: Float[ndarray, " n_neurons"] | None = None,
roi_coords: Float[ndarray, "n_neurons 2"] | None = None,
group_assignment: Float[ndarray, " n_neurons"]
| None = None,
drop_last: bool = True,
allow_over_boundaries: bool = True,
**kwargs,
) -> DataLoader
Create a DataLoader for processing movie data and associated responses. This function prepares the dataset and sampler for training or evaluation based on the specified parameters.
| PARAMETER | DESCRIPTION |
|---|---|
movie
|
The movie data represented as a multi-dimensional array or tensor.
TYPE:
|
responses
|
The responses corresponding to the frames of the movie.
TYPE:
|
split
|
The dataset split to use (train, validation, or test).
TYPE:
|
scene_length
|
The length of the scene to be processed.
TYPE:
|
chunk_size
|
The size of each chunk to be extracted from the movie.
TYPE:
|
batch_size
|
The number of samples per batch.
TYPE:
|
start_indices
|
The starting indices for each chunk. If None, will be computed.
TYPE:
|
roi_ids
|
The region of interest IDs. If None, will not be used.
TYPE:
|
roi_coords
|
The coordinates of the regions of interest. If None, will not be used.
TYPE:
|
group_assignment
|
The group assignments (cell types) for the neurons. If None, will not be used.
TYPE:
|
drop_last
|
Whether to drop the last incomplete batch. Defaults to True.
TYPE:
|
allow_over_boundaries
|
Whether to allow chunks that exceed the scene boundaries. Defaults to True.
TYPE:
|
**kwargs
|
Additional keyword arguments for the DataLoader.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
DataLoader
|
A DataLoader instance configured with the specified dataset and sampler.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in openretina/data_io/base_dataloader.py
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 | |
multiple_movies_dataloaders
multiple_movies_dataloaders(
neuron_data_dictionary: dict[
str, ResponsesTrainTestSplit
],
movies_dictionary: dict[str, MoviesTrainTestSplit],
train_chunk_size: int = 50,
batch_size: int = 32,
seed: int = 42,
clip_length: int = 100,
num_val_clips: int = 10,
val_clip_indices: list[int] | None = None,
allow_over_boundaries: bool = True,
) -> dict[str, dict[str, DataLoader]]
Create multiple dataloaders for training, validation, and testing from given neuron and movie data. This function ensures that the neuron data and movie data are aligned and generates dataloaders for each session. It does not make assumptions about the movies in different sessions to be the same, the same length, composed of the same clips or in the same order.
| PARAMETER | DESCRIPTION |
|---|---|
neuron_data_dictionary
|
A dictionary containing neuron response data split for training and testing.
TYPE:
|
movies_dictionary
|
A dictionary containing movie data split for training and testing.
TYPE:
|
train_chunk_size
|
The size of the chunks for training data. Defaults to 50.
TYPE:
|
batch_size
|
The number of samples per batch. Defaults to 32.
TYPE:
|
seed
|
The random seed for reproducibility. Defaults to 42.
TYPE:
|
clip_length
|
The length of each clip. Defaults to 100.
TYPE:
|
num_val_clips
|
The number of validation clips to draw. Defaults to 10.
TYPE:
|
val_clip_indices
|
The indices of validation clips to use. If provided, num_val_clips is ignored. Defaults to None.
TYPE:
|
allow_over_boundaries
|
Whether to allow selected chunks to go over scene boundaries.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing dataloaders for training, validation, and testing for each session.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If the keys of neuron_data_dictionary and movies_dictionary do not match exactly. |
Source code in openretina/data_io/base_dataloader.py
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 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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |