prfmodel.stimuli¶
Stimuli.
Submodules¶
Classes¶
Stimulus base class. |
|
Container for a connective field stimulus distance matrix and source response. |
|
Container for a population receptive field stimulus design and its associated grid. |
Functions¶
Animate a two-dimensional population receptive field stimulus. |
|
|
Plot a single frame of a two-dimensional population receptive field stimulus. |
Package Contents¶
- class prfmodel.stimuli.CFStimulus[source]¶
Container for a connective field stimulus distance matrix and source response.
- Parameters:
distance_matrix (numpy.ndarray) – A matrix with distances between source units (e.g., voxels).
source_response (numpy.ndarray) – Array with responses for each source unit with shape (num_voxels, num_frames). num_voxels is the number of source units and num_frames the number of time frames for each source response. num_voxels must match the number of rows and columns in distance_matrix.
- Raises:
DistanceMatrixShapeError – If the distance matrix has more or less than two dimensions or dimensions of different size.
DistanceMatrixSourceShapeError – If the source response has a first dimension with a different size than the number of rows or columns in the distance matrix.
- class prfmodel.stimuli.PRFStimulus[source]¶
Container for a population receptive field stimulus design and its associated grid.
- Parameters:
design (numpy.ndarray) – The stimulus design array containing the stimulus value in one or more dimensions over different time frames. The first axis is assumed to be time frames. Additional axes represent design dimensions.
grid (numpy.ndarray) – The coordinate system of the design. The last axis is the number of design dimensions excluding the time frame dimension. The shape excluding the last axis must match the shape of the design excluding the first axis.
dimension_labels (Sequence[str] or None, optional) – Names of the grid dimensions (e.g., [“y”, “x”]). If given, the number of labels must match the last grid axis.
- Raises:
GridDesignShapeError – If the design and grid dimensions do not match.
GridDimensionsError – If the number of dimensions of the grid except the last does not match the size of the last grid dimension.
DimensionLabelsError – If the number of dimensions does not match the grid’s last dimension.
Notes
The shapes of the design and grid must match according to design.shape[1:] == grid.shape[:-1]. That is, all design dimensions but the first must have the same size as the grid dimensions excluding the last grid dimension.
Examples
Create a population receptive field stimulus on a 2D grid.
>>> import numpy as np >>> num_frames, width, height = 10, 16, 16 >>> design = np.ones((num_frames, width, height)) >>> pixel_size = 0.05 >>> x = (np.arange(width) - (width - 1) / 2) * pixel_size >>> y = (np.arange(height) - (height - 1) / 2) * pixel_size >>> xv, yv = np.meshgrid(x, y) >>> grid = np.stack((xv, yv), axis=-1) # shape (height, width, 2) >>> grid = np.stack((xv, yv), axis=-1) # shape (height, width, 2) >>> # The coordinates of the bottom-left corner: >>> grid[0, 0, :] array([-0.375, -0.375]) >>> # The coordinates of the top-right corner: >>> grid[15, 15, :] array([0.375, 0.375]) >>> stimulus = PRFStimulus(design=design, grid=grid, dimension_labels=["y", "x"]) >>> print(stimulus) PRFStimulus(design=array[10, 16, 16], grid=array[16, 16, 2], dimension_labels=['y', 'x'])
- classmethod create_2d_bar_stimulus(num_frames: int = 100, width: int = 128, height: int = 128, bar_width: int = 20, direction: str = 'horizontal', pixel_size: float = 0.05) PRFStimulus[source]¶
Create a population receptive field bar stimulus that moves across a 2D screen.
The stimulus starts and ends moving just outside the screen.
- Parameters:
num_frames (int, optional) – Number of time frames in the stimulus.
width (int, optional) – Width of the stimulus grid (in pixels).
height (int, optional) – Height of the stimulus grid (in pixels).
bar_width (int, optional) – Width of the moving bar (in pixels).
direction ({"horizontal", "vertical"}, optional) – Direction in which the bar moves.
pixel_size (float, optional) – Size of a pixel in spatial units.
- Returns:
A stimulus instance with the generated design and grid.
- Return type:
- Raises:
ValueError – If direction is not “horizontal” or “vertical”.
Examples
>>> stimulus = PRFStimulus.create_2d_bar_stimulus(num_frames=200) >>> print(stimulus) PRFStimulus(design=array[200, 128, 128], grid=array[128, 128, 2], dimension_labels=['y', 'x'])
- prfmodel.stimuli.animate_2d_prf_stimulus(stimulus: PRFStimulus, title: str | None = None, origin: Literal['upper', 'lower'] = 'lower', interval: int = 50, blit: bool = True, repeat_delay: int = 1000, **kwargs) matplotlib.animation.ArtistAnimation[source]¶
Animate a two-dimensional population receptive field stimulus.
- Parameters:
stimulus (PRFStimulus) – The population receptive field stimulus to visualize.
title (str or None, optional) – Title for the video animation.
origin (str, optional) – origin argument for
matplotlib.axes.Axes.imshow().interval (int, optional) – interval argument passed to
matplotlib.animation.ArtistAnimation.blit (bool, optional) – blit argument passed to
matplotlib.animation.ArtistAnimation.repeat_delay (int, optional) – repeat_delay argument passed to
matplotlib.animation.ArtistAnimation.kwargs – Extra arguments passed to
matplotlib.pyplot.subplots()andmatplotlib.animation.ArtistAnimation.
- Return type:
- Raises:
StimulusDimensionError – If stimulus is not 2-dimensional.
Notes
The function uses matplotlib under the hood, and you can use the
matplotlib.rcParamsto customize the animation, as described on the matplotlib docs.Examples
>>> from IPython.display import HTML >>> from prfmodel.stimulus import PRFStimulus, animate_2d_prf_stimulus >>> bar_stimulus = PRFStimulus.create_2d_bar_stimulus(num_frames=100, width=128, height=64) >>> ani = animate_2d_prf_stimulus(bar_stimulus) >>> video = ani.to_html5_video() >>> HTML(video)
- prfmodel.stimuli.plot_2d_prf_stimulus(stimulus: PRFStimulus, frame_idx: int, origin: Literal['upper', 'lower'] = 'lower', title: str | None = None, **kwargs) tuple[matplotlib.figure.Figure, matplotlib.axes.Axes][source]¶
Plot a single frame of a two-dimensional population receptive field stimulus.
- Parameters:
stimulus (PRFStimulus) – The population receptive field stimulus to visualize.
frame_idx (int) – Index of the frame to plot.
origin (str, optional) – origin argument for
matplotlib.axes.Axes.imshow().title (str or None, optional) – Title for the plot.
kwargs – Extra arguments passed to
matplotlib.pyplot.subplots().
- Return type:
- Raises:
StimulusDimensionError – If the stimulus is not 2-dimensional.