pulse2percept.stimuli.images

ImageStimulus, LogoBVL, LogoUCSB, SnellenChart

Classes

ImageStimulus(source[, format, resize, …]) A stimulus made from an image, where each pixel gets assigned to an electrode, and grayscale values in the range [0, 255] get converted to activation values in the range [0, 1].
LogoBVL([resize, electrodes, metadata, as_gray]) Bionic Vision Lab (BVL) logo
LogoUCSB([resize, electrodes, metadata]) UCSB logo
SnellenChart([resize, show_annotations, …]) Snellen chart
class pulse2percept.stimuli.images.ImageStimulus(source, format=None, resize=None, as_gray=False, electrodes=None, metadata=None, compress=False)[source]

A stimulus made from an image, where each pixel gets assigned to an electrode, and grayscale values in the range [0, 255] get converted to activation values in the range [0, 1].

New in version 0.7.

Parameters:
  • source (str) – Path to image file. Supported image types include JPG, PNG, and TIF; and are inferred from the file ending. If the file does not have a proper file ending, specify the file type via format. Use VideoStimulus for GIFs.
  • format (str, optional) – An image format string supported by imageio, such as ‘JPG’, ‘PNG’, or ‘TIFF’. Use if the file type cannot be inferred from source. For a full list of supported formats, see https://imageio.readthedocs.io/en/stable/formats.html.
  • resize ((height, width) or None, optional) – Shape of the resized image. If one of the dimensions is set to -1, its value will be inferred by keeping a constant aspect ratio.
  • as_gray (bool, optional) – Flag whether to convert the image to grayscale. A four-channel image is interpreted as RGBA (e.g., a PNG), and the alpha channel will be blended with the color black.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the (resized) image.

  • metadata (dict, optional) – Additional stimulus metadata can be stored in a dictionary.
  • compress (bool, optional) – If True, will remove pixels with 0 grayscale value.
append(other)[source]

Append another stimulus

This method appends another stimulus (with matching electrodes) in time. The combined stimulus duration will be the sum of the two individual stimuli.

New in version 0.7.

Parameters:other (Stimulus) – Another stimulus with matching electrodes.
Returns:comb (Stimulus) – A combined stimulus with the same number of electrodes and new stimulus duration equal to the sum of the two individual stimuli.
apply(func, *args, **kwargs)[source]

Apply a function to the image

Parameters:
  • func (function) – The function to apply to the image. Must accept a 2D or 3D image and return an image with the same dimensions
  • args (*) – Additional positional arguments passed to the function
  • **kwargs – Additional keyword arguments passed to the function
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the new image

center(loc=None)[source]

Center the image foreground

This function shifts the center of mass (CoM) to the image center.

Parameters:loc ((col, row), optional) – The pixel location at which to center the CoM. By default, shifts the CoM to the image center.
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the centered image
compress()[source]

Compress the source data

Returns:compressed (Stimulus)
crop(idx_rect=None, left=0, right=0, top=0, bottom=0, electrodes=None)[source]

Crop the image

This method maps a rectangle (defined by two corners) from the image to a rectangle of the given size. Alternatively, this method can be used to crop a number of columns either from the left or the right of the image, or a number of rows either from the top or the bottom.

New in version 0.8.

Parameters:
  • idx_rect (4-tuple (y0, x0, y1, x1)) – Image indices of the top-left corner [y0, x0] and bottom-right corner [y1, x1] (exclusive) of the rectangle to crop.
  • left (int) – Number of columns to crop from the left
  • right (int) – Number of columns to crop from the right
  • top (int) – Number of rows to crop from the top
  • bottom (int) – Number of rows to crop from the bottom
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the cropped image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the cropped image

data

Stimulus data container A 2-D NumPy array that contains the stimulus data, where the rows denote electrodes and the columns denote points in time.

dt

Sampling time step (ms)

Defines the duration of the signal edge transitions.

New in version 0.7.

duration

Stimulus duration (ms)

electrodes

Electrode names A list of electrode names, corresponding to the rows in the data container.

encode(amp_range=(0, 50), pulse=None)[source]

Encode image using amplitude modulation

Encodes the image as a series of pulses, where the gray levels of the image are interpreted as the amplitude of a pulse with values in amp_range.

By default, a single biphasic pulse is used for each pixel, with 0.46ms phase duration, and 500ms total stimulus duration.

Parameters:
  • amp_range ((min_amp, max_amp)) – Range of amplitude values to use for the encoding. The image’s gray levels will be scaled such that the smallest value is mapped onto min_amp and the largest onto max_amp.
  • pulse (Stimulus, optional) – A valid pulse or pulse train to be used for the encoding. If None given, a BiphasicPulse (0.46 ms phase duration, 500 ms total duration) will be used.
Returns:

stim (Stimulus) – Encoded stimulus

filter(filt, **kwargs)[source]

Filter the image

Parameters:
  • filt (str) –

    Image filter. Additional parameters can be passed as keyword arguments. The following filters are supported:

    • ’sobel’: Edge filter the image using the Sobel filter.
    • ’scharr’: Edge filter the image using the Scarr filter.
    • ’canny’: Edge filter the image using the Canny algorithm. You can also specify sigma, low_threshold, high_threshold, mask, and use_quantiles.
    • ’median’: Return local median of the image.
  • **kwargs – Additional parameters passed to the filter
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the filtered image

invert()[source]

Invert the gray levels of the image

Returns:stim (ImageStimulus) – A copy of the stimulus object with all grayscale values inverted in the range [0, 1].
is_charge_balanced

Flag indicating whether the stimulus is charge-balanced

A stimulus with a time component is considered charge-balanced if its net current is smaller than 10 pico Amps. For the whole stimulus to be charge-balanced, every electrode must be charge-balanced as well.

is_compressed

Flag indicating whether the stimulus has been compressed

plot(ax=None, **kwargs)[source]

Plot the stimulus

Parameters:ax (matplotlib.axes.Axes or list thereof; optional, default: None) – A Matplotlib Axes object or a list thereof (one per electrode to plot). If None, a new Axes object will be created.
Returns:ax (matplotlib.axes.Axes) – Returns the axes with the plot on it
remove(electrodes)[source]

Remove electrode(s)

Removes the stimulus of a certain electrode or list of electrodes.

New in version 0.8.

Parameters:electrodes (int, string, or list of int/str) – The item(s) to remove from the stimulus. Can either be an electrode index, electrode name, or a list thereof.
resize(shape, electrodes=None)[source]

Resize the image

Parameters:
  • shape ((rows, cols)) – Shape of the resized image
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the resized image

rgb2gray(electrodes=None)[source]

Convert the image to grayscale

Parameters:electrodes (int, string or list thereof; optional) –

Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

Note

The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:stim (ImageStimulus) – A copy of the stimulus object with all RGB values converted to grayscale in the range [0, 1].

Notes

  • A four-channel image is interpreted as RGBA (e.g., a PNG), and the alpha channel will be blended with the color black.
rotate(angle, mode='constant')[source]

Rotate the image

Parameters:angle (float) – Angle by which to rotate the image (degrees). Positive: counter-clockwise, negative: clockwise
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the rotated image
save(fname)[source]

Save the stimulus as an image

Parameters:fname (str) – The name of the image file to be created. Image type will be inferred from the file extension.
scale(scaling_factor)[source]

Scale the image foreground

This function scales the image foreground (excluding black pixels) by a factor.

Parameters:scaling_factor (float) – Factory by which to scale the image
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the scaled image
shape

Data container shape

shift(shift_cols, shift_rows)[source]

Shift the image foreground

This function shifts the center of mass (CoM) of the image by the specified number of rows and columns.

Parameters:
  • shift_cols (float) – Number of columns by which to shift the CoM. Positive: to the right, negative: to the left
  • shift_rows (float) – Number of rows by which to shift the CoM. Positive: downward, negative: upward
Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the shifted image

threshold(thresh, **kwargs)[source]

Threshold the image

Parameters:thresh (str or float) –

If a float in [0,1] is provided, pixels whose grayscale value is above said threshold will be white, others black.

A number of additional methods are supported:

Returns:stim (ImageStimulus) – A copy of the stimulus object with two gray levels 0.0 and 1.0
time

Time steps A list of time steps, corresponding to the columns in the data container.

trim(tol=0, electrodes=None)[source]

Remove any black border around the image

New in version 0.7.

Parameters:
  • tol (float) – Any pixels with gray levels > tol will be trimmed.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the trimmed image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object with trimmed borders.

class pulse2percept.stimuli.images.LogoBVL(resize=None, electrodes=None, metadata=None, as_gray=False)[source]

Bionic Vision Lab (BVL) logo

Load the 576x720x4 Bionic Vision Lab (BVL) logo.

New in version 0.7.

Parameters:
  • resize ((height, width) or None, optional) – A tuple specifying the desired height and the width of the image stimulus.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the (resized) image.

  • metadata (dict, optional) – Additional stimulus metadata can be stored in a dictionary.
append(other)[source]

Append another stimulus

This method appends another stimulus (with matching electrodes) in time. The combined stimulus duration will be the sum of the two individual stimuli.

New in version 0.7.

Parameters:other (Stimulus) – Another stimulus with matching electrodes.
Returns:comb (Stimulus) – A combined stimulus with the same number of electrodes and new stimulus duration equal to the sum of the two individual stimuli.
apply(func, *args, **kwargs)[source]

Apply a function to the image

Parameters:
  • func (function) – The function to apply to the image. Must accept a 2D or 3D image and return an image with the same dimensions
  • args (*) – Additional positional arguments passed to the function
  • **kwargs – Additional keyword arguments passed to the function
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the new image

center(loc=None)[source]

Center the image foreground

This function shifts the center of mass (CoM) to the image center.

Parameters:loc ((col, row), optional) – The pixel location at which to center the CoM. By default, shifts the CoM to the image center.
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the centered image
compress()[source]

Compress the source data

Returns:compressed (Stimulus)
crop(idx_rect=None, left=0, right=0, top=0, bottom=0, electrodes=None)[source]

Crop the image

This method maps a rectangle (defined by two corners) from the image to a rectangle of the given size. Alternatively, this method can be used to crop a number of columns either from the left or the right of the image, or a number of rows either from the top or the bottom.

New in version 0.8.

Parameters:
  • idx_rect (4-tuple (y0, x0, y1, x1)) – Image indices of the top-left corner [y0, x0] and bottom-right corner [y1, x1] (exclusive) of the rectangle to crop.
  • left (int) – Number of columns to crop from the left
  • right (int) – Number of columns to crop from the right
  • top (int) – Number of rows to crop from the top
  • bottom (int) – Number of rows to crop from the bottom
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the cropped image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the cropped image

data

Stimulus data container A 2-D NumPy array that contains the stimulus data, where the rows denote electrodes and the columns denote points in time.

dt

Sampling time step (ms)

Defines the duration of the signal edge transitions.

New in version 0.7.

duration

Stimulus duration (ms)

electrodes

Electrode names A list of electrode names, corresponding to the rows in the data container.

encode(amp_range=(0, 50), pulse=None)[source]

Encode image using amplitude modulation

Encodes the image as a series of pulses, where the gray levels of the image are interpreted as the amplitude of a pulse with values in amp_range.

By default, a single biphasic pulse is used for each pixel, with 0.46ms phase duration, and 500ms total stimulus duration.

Parameters:
  • amp_range ((min_amp, max_amp)) – Range of amplitude values to use for the encoding. The image’s gray levels will be scaled such that the smallest value is mapped onto min_amp and the largest onto max_amp.
  • pulse (Stimulus, optional) – A valid pulse or pulse train to be used for the encoding. If None given, a BiphasicPulse (0.46 ms phase duration, 500 ms total duration) will be used.
Returns:

stim (Stimulus) – Encoded stimulus

filter(filt, **kwargs)[source]

Filter the image

Parameters:
  • filt (str) –

    Image filter. Additional parameters can be passed as keyword arguments. The following filters are supported:

    • ’sobel’: Edge filter the image using the Sobel filter.
    • ’scharr’: Edge filter the image using the Scarr filter.
    • ’canny’: Edge filter the image using the Canny algorithm. You can also specify sigma, low_threshold, high_threshold, mask, and use_quantiles.
    • ’median’: Return local median of the image.
  • **kwargs – Additional parameters passed to the filter
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the filtered image

invert()[source]

Invert the gray levels of the image

Returns:stim (ImageStimulus) – A copy of the stimulus object with all grayscale values inverted in the range [0, 1].
is_charge_balanced

Flag indicating whether the stimulus is charge-balanced

A stimulus with a time component is considered charge-balanced if its net current is smaller than 10 pico Amps. For the whole stimulus to be charge-balanced, every electrode must be charge-balanced as well.

is_compressed

Flag indicating whether the stimulus has been compressed

plot(ax=None, **kwargs)[source]

Plot the stimulus

Parameters:ax (matplotlib.axes.Axes or list thereof; optional, default: None) – A Matplotlib Axes object or a list thereof (one per electrode to plot). If None, a new Axes object will be created.
Returns:ax (matplotlib.axes.Axes) – Returns the axes with the plot on it
remove(electrodes)[source]

Remove electrode(s)

Removes the stimulus of a certain electrode or list of electrodes.

New in version 0.8.

Parameters:electrodes (int, string, or list of int/str) – The item(s) to remove from the stimulus. Can either be an electrode index, electrode name, or a list thereof.
resize(shape, electrodes=None)[source]

Resize the image

Parameters:
  • shape ((rows, cols)) – Shape of the resized image
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the resized image

rgb2gray(electrodes=None)[source]

Convert the image to grayscale

Parameters:electrodes (int, string or list thereof; optional) –

Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

Note

The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:stim (ImageStimulus) – A copy of the stimulus object with all RGB values converted to grayscale in the range [0, 1].

Notes

  • A four-channel image is interpreted as RGBA (e.g., a PNG), and the alpha channel will be blended with the color black.
rotate(angle, mode='constant')[source]

Rotate the image

Parameters:angle (float) – Angle by which to rotate the image (degrees). Positive: counter-clockwise, negative: clockwise
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the rotated image
save(fname)[source]

Save the stimulus as an image

Parameters:fname (str) – The name of the image file to be created. Image type will be inferred from the file extension.
scale(scaling_factor)[source]

Scale the image foreground

This function scales the image foreground (excluding black pixels) by a factor.

Parameters:scaling_factor (float) – Factory by which to scale the image
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the scaled image
shape

Data container shape

shift(shift_cols, shift_rows)[source]

Shift the image foreground

This function shifts the center of mass (CoM) of the image by the specified number of rows and columns.

Parameters:
  • shift_cols (float) – Number of columns by which to shift the CoM. Positive: to the right, negative: to the left
  • shift_rows (float) – Number of rows by which to shift the CoM. Positive: downward, negative: upward
Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the shifted image

threshold(thresh, **kwargs)[source]

Threshold the image

Parameters:thresh (str or float) –

If a float in [0,1] is provided, pixels whose grayscale value is above said threshold will be white, others black.

A number of additional methods are supported:

Returns:stim (ImageStimulus) – A copy of the stimulus object with two gray levels 0.0 and 1.0
time

Time steps A list of time steps, corresponding to the columns in the data container.

trim(tol=0, electrodes=None)[source]

Remove any black border around the image

New in version 0.7.

Parameters:
  • tol (float) – Any pixels with gray levels > tol will be trimmed.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the trimmed image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object with trimmed borders.

class pulse2percept.stimuli.images.LogoUCSB(resize=None, electrodes=None, metadata=None)[source]

UCSB logo

Load a 324x727 white-on-black logo of the University of California, Santa Barbara.

New in version 0.7.

Parameters:
  • resize ((height, width) or None, optional) – A tuple specifying the desired height and the width of the image stimulus.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the (resized) image.

  • metadata (dict, optional) – Additional stimulus metadata can be stored in a dictionary.
append(other)[source]

Append another stimulus

This method appends another stimulus (with matching electrodes) in time. The combined stimulus duration will be the sum of the two individual stimuli.

New in version 0.7.

Parameters:other (Stimulus) – Another stimulus with matching electrodes.
Returns:comb (Stimulus) – A combined stimulus with the same number of electrodes and new stimulus duration equal to the sum of the two individual stimuli.
apply(func, *args, **kwargs)[source]

Apply a function to the image

Parameters:
  • func (function) – The function to apply to the image. Must accept a 2D or 3D image and return an image with the same dimensions
  • args (*) – Additional positional arguments passed to the function
  • **kwargs – Additional keyword arguments passed to the function
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the new image

center(loc=None)[source]

Center the image foreground

This function shifts the center of mass (CoM) to the image center.

Parameters:loc ((col, row), optional) – The pixel location at which to center the CoM. By default, shifts the CoM to the image center.
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the centered image
compress()[source]

Compress the source data

Returns:compressed (Stimulus)
crop(idx_rect=None, left=0, right=0, top=0, bottom=0, electrodes=None)[source]

Crop the image

This method maps a rectangle (defined by two corners) from the image to a rectangle of the given size. Alternatively, this method can be used to crop a number of columns either from the left or the right of the image, or a number of rows either from the top or the bottom.

New in version 0.8.

Parameters:
  • idx_rect (4-tuple (y0, x0, y1, x1)) – Image indices of the top-left corner [y0, x0] and bottom-right corner [y1, x1] (exclusive) of the rectangle to crop.
  • left (int) – Number of columns to crop from the left
  • right (int) – Number of columns to crop from the right
  • top (int) – Number of rows to crop from the top
  • bottom (int) – Number of rows to crop from the bottom
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the cropped image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the cropped image

data

Stimulus data container A 2-D NumPy array that contains the stimulus data, where the rows denote electrodes and the columns denote points in time.

dt

Sampling time step (ms)

Defines the duration of the signal edge transitions.

New in version 0.7.

duration

Stimulus duration (ms)

electrodes

Electrode names A list of electrode names, corresponding to the rows in the data container.

encode(amp_range=(0, 50), pulse=None)[source]

Encode image using amplitude modulation

Encodes the image as a series of pulses, where the gray levels of the image are interpreted as the amplitude of a pulse with values in amp_range.

By default, a single biphasic pulse is used for each pixel, with 0.46ms phase duration, and 500ms total stimulus duration.

Parameters:
  • amp_range ((min_amp, max_amp)) – Range of amplitude values to use for the encoding. The image’s gray levels will be scaled such that the smallest value is mapped onto min_amp and the largest onto max_amp.
  • pulse (Stimulus, optional) – A valid pulse or pulse train to be used for the encoding. If None given, a BiphasicPulse (0.46 ms phase duration, 500 ms total duration) will be used.
Returns:

stim (Stimulus) – Encoded stimulus

filter(filt, **kwargs)[source]

Filter the image

Parameters:
  • filt (str) –

    Image filter. Additional parameters can be passed as keyword arguments. The following filters are supported:

    • ’sobel’: Edge filter the image using the Sobel filter.
    • ’scharr’: Edge filter the image using the Scarr filter.
    • ’canny’: Edge filter the image using the Canny algorithm. You can also specify sigma, low_threshold, high_threshold, mask, and use_quantiles.
    • ’median’: Return local median of the image.
  • **kwargs – Additional parameters passed to the filter
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the filtered image

invert()[source]

Invert the gray levels of the image

Returns:stim (ImageStimulus) – A copy of the stimulus object with all grayscale values inverted in the range [0, 1].
is_charge_balanced

Flag indicating whether the stimulus is charge-balanced

A stimulus with a time component is considered charge-balanced if its net current is smaller than 10 pico Amps. For the whole stimulus to be charge-balanced, every electrode must be charge-balanced as well.

is_compressed

Flag indicating whether the stimulus has been compressed

plot(ax=None, **kwargs)[source]

Plot the stimulus

Parameters:ax (matplotlib.axes.Axes or list thereof; optional, default: None) – A Matplotlib Axes object or a list thereof (one per electrode to plot). If None, a new Axes object will be created.
Returns:ax (matplotlib.axes.Axes) – Returns the axes with the plot on it
remove(electrodes)[source]

Remove electrode(s)

Removes the stimulus of a certain electrode or list of electrodes.

New in version 0.8.

Parameters:electrodes (int, string, or list of int/str) – The item(s) to remove from the stimulus. Can either be an electrode index, electrode name, or a list thereof.
resize(shape, electrodes=None)[source]

Resize the image

Parameters:
  • shape ((rows, cols)) – Shape of the resized image
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the resized image

rgb2gray(electrodes=None)[source]

Convert the image to grayscale

Parameters:electrodes (int, string or list thereof; optional) –

Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

Note

The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:stim (ImageStimulus) – A copy of the stimulus object with all RGB values converted to grayscale in the range [0, 1].

Notes

  • A four-channel image is interpreted as RGBA (e.g., a PNG), and the alpha channel will be blended with the color black.
rotate(angle, mode='constant')[source]

Rotate the image

Parameters:angle (float) – Angle by which to rotate the image (degrees). Positive: counter-clockwise, negative: clockwise
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the rotated image
save(fname)[source]

Save the stimulus as an image

Parameters:fname (str) – The name of the image file to be created. Image type will be inferred from the file extension.
scale(scaling_factor)[source]

Scale the image foreground

This function scales the image foreground (excluding black pixels) by a factor.

Parameters:scaling_factor (float) – Factory by which to scale the image
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the scaled image
shape

Data container shape

shift(shift_cols, shift_rows)[source]

Shift the image foreground

This function shifts the center of mass (CoM) of the image by the specified number of rows and columns.

Parameters:
  • shift_cols (float) – Number of columns by which to shift the CoM. Positive: to the right, negative: to the left
  • shift_rows (float) – Number of rows by which to shift the CoM. Positive: downward, negative: upward
Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the shifted image

threshold(thresh, **kwargs)[source]

Threshold the image

Parameters:thresh (str or float) –

If a float in [0,1] is provided, pixels whose grayscale value is above said threshold will be white, others black.

A number of additional methods are supported:

Returns:stim (ImageStimulus) – A copy of the stimulus object with two gray levels 0.0 and 1.0
time

Time steps A list of time steps, corresponding to the columns in the data container.

trim(tol=0, electrodes=None)[source]

Remove any black border around the image

New in version 0.7.

Parameters:
  • tol (float) – Any pixels with gray levels > tol will be trimmed.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the trimmed image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object with trimmed borders.

class pulse2percept.stimuli.images.SnellenChart(resize=None, show_annotations=True, row=None, electrodes=None, metadata=None)[source]

Snellen chart

Load the 1348x840 Snellen chart commonly used to measure visual acuity.

New in version 0.7.

Parameters:
  • resize ((height, width) or None, optional) – A tuple specifying the desired height and the width of the image stimulus.
  • show_annotations ({True, False}, optional) – If True, show the full Snellen chart including annotations of the rows and corresponding acuity measures.
  • row (None, optional) – Select a single row (between 1 and 11) from the Snellen chart. For example, row 1 corresponds to 20/200, row 2 to 20/100.
  • electrodes (int, string or list thereof; optional, default: None) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the (resized) image.

  • metadata (dict, optional, default: None) – Additional stimulus metadata can be stored in a dictionary.
append(other)[source]

Append another stimulus

This method appends another stimulus (with matching electrodes) in time. The combined stimulus duration will be the sum of the two individual stimuli.

New in version 0.7.

Parameters:other (Stimulus) – Another stimulus with matching electrodes.
Returns:comb (Stimulus) – A combined stimulus with the same number of electrodes and new stimulus duration equal to the sum of the two individual stimuli.
apply(func, *args, **kwargs)[source]

Apply a function to the image

Parameters:
  • func (function) – The function to apply to the image. Must accept a 2D or 3D image and return an image with the same dimensions
  • args (*) – Additional positional arguments passed to the function
  • **kwargs – Additional keyword arguments passed to the function
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the new image

center(loc=None)[source]

Center the image foreground

This function shifts the center of mass (CoM) to the image center.

Parameters:loc ((col, row), optional) – The pixel location at which to center the CoM. By default, shifts the CoM to the image center.
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the centered image
compress()[source]

Compress the source data

Returns:compressed (Stimulus)
crop(idx_rect=None, left=0, right=0, top=0, bottom=0, electrodes=None)[source]

Crop the image

This method maps a rectangle (defined by two corners) from the image to a rectangle of the given size. Alternatively, this method can be used to crop a number of columns either from the left or the right of the image, or a number of rows either from the top or the bottom.

New in version 0.8.

Parameters:
  • idx_rect (4-tuple (y0, x0, y1, x1)) – Image indices of the top-left corner [y0, x0] and bottom-right corner [y1, x1] (exclusive) of the rectangle to crop.
  • left (int) – Number of columns to crop from the left
  • right (int) – Number of columns to crop from the right
  • top (int) – Number of rows to crop from the top
  • bottom (int) – Number of rows to crop from the bottom
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the cropped image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the cropped image

data

Stimulus data container A 2-D NumPy array that contains the stimulus data, where the rows denote electrodes and the columns denote points in time.

dt

Sampling time step (ms)

Defines the duration of the signal edge transitions.

New in version 0.7.

duration

Stimulus duration (ms)

electrodes

Electrode names A list of electrode names, corresponding to the rows in the data container.

encode(amp_range=(0, 50), pulse=None)[source]

Encode image using amplitude modulation

Encodes the image as a series of pulses, where the gray levels of the image are interpreted as the amplitude of a pulse with values in amp_range.

By default, a single biphasic pulse is used for each pixel, with 0.46ms phase duration, and 500ms total stimulus duration.

Parameters:
  • amp_range ((min_amp, max_amp)) – Range of amplitude values to use for the encoding. The image’s gray levels will be scaled such that the smallest value is mapped onto min_amp and the largest onto max_amp.
  • pulse (Stimulus, optional) – A valid pulse or pulse train to be used for the encoding. If None given, a BiphasicPulse (0.46 ms phase duration, 500 ms total duration) will be used.
Returns:

stim (Stimulus) – Encoded stimulus

filter(filt, **kwargs)[source]

Filter the image

Parameters:
  • filt (str) –

    Image filter. Additional parameters can be passed as keyword arguments. The following filters are supported:

    • ’sobel’: Edge filter the image using the Sobel filter.
    • ’scharr’: Edge filter the image using the Scarr filter.
    • ’canny’: Edge filter the image using the Canny algorithm. You can also specify sigma, low_threshold, high_threshold, mask, and use_quantiles.
    • ’median’: Return local median of the image.
  • **kwargs – Additional parameters passed to the filter
Returns:

stim (ImageStimulus) – A copy of the stimulus object with the filtered image

invert()[source]

Invert the gray levels of the image

Returns:stim (ImageStimulus) – A copy of the stimulus object with all grayscale values inverted in the range [0, 1].
is_charge_balanced

Flag indicating whether the stimulus is charge-balanced

A stimulus with a time component is considered charge-balanced if its net current is smaller than 10 pico Amps. For the whole stimulus to be charge-balanced, every electrode must be charge-balanced as well.

is_compressed

Flag indicating whether the stimulus has been compressed

plot(ax=None, **kwargs)[source]

Plot the stimulus

Parameters:ax (matplotlib.axes.Axes or list thereof; optional, default: None) – A Matplotlib Axes object or a list thereof (one per electrode to plot). If None, a new Axes object will be created.
Returns:ax (matplotlib.axes.Axes) – Returns the axes with the plot on it
remove(electrodes)[source]

Remove electrode(s)

Removes the stimulus of a certain electrode or list of electrodes.

New in version 0.8.

Parameters:electrodes (int, string, or list of int/str) – The item(s) to remove from the stimulus. Can either be an electrode index, electrode name, or a list thereof.
resize(shape, electrodes=None)[source]

Resize the image

Parameters:
  • shape ((rows, cols)) – Shape of the resized image
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the resized image

rgb2gray(electrodes=None)[source]

Convert the image to grayscale

Parameters:electrodes (int, string or list thereof; optional) –

Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

Note

The number of electrode names provided must match the number of pixels in the grayscale image.

Returns:stim (ImageStimulus) – A copy of the stimulus object with all RGB values converted to grayscale in the range [0, 1].

Notes

  • A four-channel image is interpreted as RGBA (e.g., a PNG), and the alpha channel will be blended with the color black.
rotate(angle, mode='constant')[source]

Rotate the image

Parameters:angle (float) – Angle by which to rotate the image (degrees). Positive: counter-clockwise, negative: clockwise
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the rotated image
save(fname)[source]

Save the stimulus as an image

Parameters:fname (str) – The name of the image file to be created. Image type will be inferred from the file extension.
scale(scaling_factor)[source]

Scale the image foreground

This function scales the image foreground (excluding black pixels) by a factor.

Parameters:scaling_factor (float) – Factory by which to scale the image
Returns:stim (ImageStimulus) – A copy of the stimulus object containing the scaled image
shape

Data container shape

shift(shift_cols, shift_rows)[source]

Shift the image foreground

This function shifts the center of mass (CoM) of the image by the specified number of rows and columns.

Parameters:
  • shift_cols (float) – Number of columns by which to shift the CoM. Positive: to the right, negative: to the left
  • shift_rows (float) – Number of rows by which to shift the CoM. Positive: downward, negative: upward
Returns:

stim (ImageStimulus) – A copy of the stimulus object containing the shifted image

threshold(thresh, **kwargs)[source]

Threshold the image

Parameters:thresh (str or float) –

If a float in [0,1] is provided, pixels whose grayscale value is above said threshold will be white, others black.

A number of additional methods are supported:

Returns:stim (ImageStimulus) – A copy of the stimulus object with two gray levels 0.0 and 1.0
time

Time steps A list of time steps, corresponding to the columns in the data container.

trim(tol=0, electrodes=None)[source]

Remove any black border around the image

New in version 0.7.

Parameters:
  • tol (float) – Any pixels with gray levels > tol will be trimmed.
  • electrodes (int, string or list thereof; optional) –

    Optionally, you can provide your own electrode names. If none are given, electrode names will be numbered 0..N.

    Note

    The number of electrode names provided must match the number of pixels in the trimmed image.

Returns:

stim (ImageStimulus) – A copy of the stimulus object with trimmed borders.