Vector Field Analysis
Tools for analyzing model responses via principal component analysis and vector field visualization.
compute_lsta_library
compute_lsta_library(
model: Module,
movies: ndarray,
session_id: str,
cell_id: int,
batch_size: int = 64,
integration_window: tuple[int, int] = (5, 15),
device: str = "cuda",
) -> tuple[ndarray, ndarray]
Computes the Local Spike-Triggered Average (LSTA) library and response library for a given model, set of movies, and cell_id.
For each batch of input movies, this function: - Runs the model to obtain outputs for all cells and time points. - Selects the output for a specific cell over a specified integration window (time range). - Sums the selected outputs and computes the gradient of this sum with respect to the input movies. - The resulting gradients (LSTA maps) are averaged over the integration window for each movie. - Collects both the LSTA maps and the raw model outputs for all movies.
Parameters
model (torch.nn.Module): The neural network model to evaluate.
movies (np.ndarray or torch.Tensor): Array of input movie stimuli with shape (num_samples, channels, frames,
height, width).
session_id (str): Identifier for the session/data key used by the model.
cell_id (int): Index of the cell for which to compute LSTA.
batch_size (int, optional): Number of samples per batch. Default is 64.
integration_window (tuple, optional): Tuple (start, end) specifying the time window (frame indices) over which
to sum outputs. Default is (5, 10).
device (str, optional): Device to run computations on ('cuda' or 'cpu'). Default is 'cuda'.
Returns
lsta_library (np.ndarray): Array of LSTA maps averaged over the integration window, shape
(num_samples, channels, height, width).
response_library (np.ndarray): Array of model outputs for all batches, shape (num_samples, frames, num_cells).
Raises
IndexError: If cell_id is out of bounds for the model output.
Notes
- The LSTA map for each movie is computed as the gradient of the summed output for the specified cell and time
window,
with respect to the input movie frames.
- The returned lsta_library is averaged over the integration window
(i.e., mean gradient across selected frames).
- The response_library contains the raw model outputs for all movies, all frames, and all cells.
- Default integration_window is not always optimal;
adjust based on model architecture and expected response timing.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.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 | |
get_pc_from_pca
get_pc_from_pca(
model,
channel: int,
lsta_library: ndarray,
plot: bool = False,
) -> tuple[ndarray, ndarray, ndarray]
Computes the first two principal components (PC1 and PC2) from a PCA analysis on a selected channel of the input data. Parameters
model : object
Model object containing data information, specifically the input shape in model.data_info["input_shape"].
channel : int
Index of the channel to select from lsta_library for PCA analysis.
lsta_library : np.ndarray
Input data array of shape (samples, channels, height, width).
plot : bool, optional
If True, plots the first two principal components as images using matplotlib.
Returns
PC1 : np.ndarray The first principal component as a flattened array. PC2 : np.ndarray The second principal component as a flattened array. explained_variance : np.ndarray Array containing the explained variance ratio for the first two principal components. Notes
- The function reshapes the selected channel data to (samples, height * width) before applying PCA.
- If
plotis True, displays the principal components as images with color mapping.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.py
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 | |
get_images_coordinate
get_images_coordinate(
images: ndarray,
PC1: ndarray,
PC2: ndarray,
plot: bool = False,
) -> ndarray
Projects a set of images onto two principal component vectors and optionally plots their coordinates. Parameters
images (np.ndarray): Array of images with shape (n_samples, height, width).
PC1 (np.ndarray): First principal component vector with shape (height * width,).
PC2 (np.ndarray): Second principal component vector with shape (height * width,).
plot (bool, optional): If True, plots the projected coordinates. Default is False.
Returns
np.ndarray: Array of shape (n_samples, 2) containing the coordinates of each image projected onto PC1 and PC2.
Note
The function reshapes each image to a 1D vector before projection.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.py
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 | |
plot_untreated_vectorfield
plot_untreated_vectorfield(
lsta_library: ndarray,
channel: int,
PC1: ndarray,
PC2: ndarray,
images_coordinate: ndarray,
) -> Figure
Plots a vector field visualization using principal components from an LSTA library. This function extracts the specified channel from the LSTA library, projects each LSTA onto two principal components (PC1 and PC2), and visualizes the resulting vector field at given image coordinates using matplotlib's quiver plot. Additionally, it displays the PC1 and PC2 components as inset images. This function is primarily for visualization in notebooks. Returns figure for saving or further customization. Parameters
lsta_library : np.ndarray A 4D numpy array containing the LSTA library data with shape (n_samples, n_channels, x_size, y_size). channel : int The index of the channel to extract from the LSTA library for analysis. PC1 : np.ndarray The first principal component vector used for projection. PC2 : np.ndarray The second principal component vector used for projection. images_coordinate : np.ndarray A 2D numpy array of shape (n_samples, 2) containing the (x, y) coordinates for each LSTA sample. Returns
plt.Figure The matplotlib Figure object containing the vector field plot with PC1 and PC2 inset images. Call plt.show() to display, or fig.savefig() to save. Notes
- The function uses matplotlib's quiver plot to visualize the vector field.
- PC1 and PC2 are displayed as inset images for reference.
- The axes are turned off for a cleaner visualization.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.py
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 | |
plot_clean_vectorfield
plot_clean_vectorfield(
lsta_library: ndarray,
channel: int,
PC1: ndarray,
PC2: ndarray,
images: list[Any] | ndarray,
images_coordinate: ndarray,
explained_variance: ndarray,
x_bins: int = 31,
y_bins: int = 31,
responses: ndarray | None = None,
) -> Figure
Plots a cleaned vector field representation of binned image and LSTA data projected onto principal components. This function bins images and their corresponding LSTA (Local Spike-Triggered Average) responses based on spatial coordinates, projects the binned data onto two principal components (PC1 and PC2), and visualizes the resulting vector field using quiver plots. Insets showing the PC1 and PC2 components are also added to the figure. This function is primarily for visualization in notebooks. Returns figure for saving or further customization. Insets showing the PC1 and PC2 components are also added to the figure for reference. Parameters
lsta_library : np.ndarray Array of LSTA responses with shape (n_samples, n_channels, x_size, y_size). channel : int Index of the channel to select from lsta_library. PC1 : np.ndarray First principal component vector for projection (flattened). PC2 : np.ndarray Second principal component vector for projection (flattened). images : np.ndarray Array of images corresponding to LSTA responses, shape (n_samples, x_size, y_size). images_coordinate : np.ndarray Array of spatial coordinates for each image, shape (n_samples, 2). explained_variance : np.ndarray Array containing explained variance for each principal component. x_bins : int, optional Number of bins along the x-axis for spatial binning (default is 31). y_bins : int, optional Number of bins along the y-axis for spatial binning (default is 31). responses : np.ndarray, optional Array of response values for each image, shape (n_samples,). If provided, will overlay response magnitudes as colored markers at each location. Returns
fig : matplotlib.figure.Figure The matplotlib figure object containing the vector field plot and PC insets. Call plt.show() to display, or fig.savefig() to save. Raises
ValueError If no images are found in the coordinate bins (e.g., due to bin size or coordinate range). Notes
- This function is primarily intended for visualization in Jupyter notebooks.
- The vector field arrows represent the projection of binned images and LSTA responses onto the first two principal components.
- Insets display the spatial structure of PC1 and PC2 for interpretability.
- If responses are provided, they will be averaged within bins and displayed as colored markers.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.py
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 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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | |
Helper Functions
load_and_preprocess_images
load_and_preprocess_images(
image_dir: str,
target_h: int,
target_w: int,
n_channels: int,
) -> ndarray
Loads PNG images from a directory, downsamples, center-crops, and repeats channels as needed. Parameters
image_dir (str): Directory containing PNG images.
target_h (int): Target height for cropping.
target_w (int): Target width for cropping.
n_channels (int): Number of channels to repeat.
Returns
np.ndarray: Array of shape (num_images, n_channels, target_h, target_w).
Raises
ValueError: If no PNG images are found in the directory.
Notes
- Images are downsampled to fit within target dimensions while maintaining aspect ratio.
- Center-cropping is applied after downsampling.
- Single-channel images are repeated across channels if n_channels > 1.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 81 82 | |
prepare_movies_dataset
prepare_movies_dataset(
model: BaseCoreReadout,
session_id: str,
n_image_frames: int = 16,
normalize_movies: bool = True,
image_library: ndarray | None = None,
image_dir: str | None = None,
device: str = "cuda",
) -> tuple[ndarray, int]
Prepares a dataset of movie stimuli for input into a neural model. This function delegates image loading, preprocessing, normalization, and temporal padding to helper functions. Parameters
model: Neural model object with `data_info` attribute.
session_id (str): Identifier for the session.
n_image_frames (int, optional): Number of frames per movie for each image.
normalize_movies (bool, optional): Whether to normalize the movies.
image_library (np.ndarray, optional): Preprocessed image library.
image_dir (str, optional): Directory containing image files (.png).
device (str, optional): Device for torch tensors.
Returns
movies (np.ndarray): Array of shape (num_images, n_channels, n_frames, target_h, target_w).
n_empty_frames (int): Number of initial empty frames for temporal padding.
Raises
ValueError: If both `image_library` and `image_dir` are provided.
Source code in openretina/insilico/vector_field_analysis/vector_field_analysis.py
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 | |