Skip to content

Commit

Permalink
added sbc_with_dots and dotted_sbc
Browse files Browse the repository at this point in the history
  • Loading branch information
LynnSchmittwilken committed Jul 1, 2022
1 parent f62d8ab commit 61ce0ff
Showing 1 changed file with 132 additions and 4 deletions.
136 changes: 132 additions & 4 deletions stimuli/illusions/sbc.py
@@ -1,4 +1,6 @@
from stimuli.components import rectangle
import numpy as np
from stimuli.utils import pixels_to_degrees, pad_img
from stimuli.components import rectangle, disc


def simultaneous_contrast(
Expand Down Expand Up @@ -36,10 +38,136 @@ def simultaneous_contrast(
return {"img": img, "mask": mask}


def sbc_with_dots(
ppd=10,
n_dots=(8, 9),
dot_radius=3.,
distance=1.,
target_shape=(4, 3),
vback=0.,
vdots=1.,
vtarget=0.5,
):
"""
Simultaneous contrast stimulus with dots
Parameters
----------
ppd : int
pixels per degree (visual angle)
n_dots : (int, int)
stimulus size defined as the number of dots in y and x-directions
dot_radius : float
radius of dots
distance : float
distance between dots in degree visual angle
target_shape : (int, int)
target shape defined as the number of dots that fit into the target
vback : float
value for background
vdots : float
value for dots
vtarget : float
value for target
Returns
-------
A stimulus dictionary with the stimulus ['img'] and target mask ['mask']
"""

padding = (distance/2., distance/2., distance/2., distance/2.)
patch = disc(ppd, dot_radius, vback=0., vdisc=vdots)
patch = pad_img(patch, padding, ppd, 0.)

img_height = pixels_to_degrees(n_dots[0] * patch.shape[0], ppd)
img_width = pixels_to_degrees(n_dots[1] * patch.shape[1], ppd)
rec_height = pixels_to_degrees(target_shape[0] * patch.shape[0], ppd)
rec_width = pixels_to_degrees(target_shape[1] * patch.shape[1], ppd)

# Create the sbc in the background:
tposy = (img_height-rec_height) / 2.
tposx = (img_width-rec_width) / 2.
img = rectangle(ppd, im_size=(img_height, img_width), rect_size=(rec_height, rec_width),
rect_pos=(tposy, tposx), vback=vback, vrect=vtarget)

patch = np.tile(patch, (n_dots[0], n_dots[1]))
indices_dots = np.where((patch != 0))
img[indices_dots] = vdots
mask = None
return {"img": img, "mask": mask}


def dotted_sbc(
ppd=10,
n_dots=(8, 9),
dot_radius=3.,
distance=1.,
target_shape=(4, 3),
vback=0.,
vdots=1.,
vtarget=0.5,
):
"""
Simultaneous contrast stimulus with dots
Parameters
----------
ppd : int
pixels per degree (visual angle)
n_dots : (int, int)
stimulus size defined as the number of dots in y and x-directions
dot_radius : float
radius of dots
distance : float
distance between dots in degree visual angle
target_shape : (int, int)
target shape defined as the number of dots that fit into the target
vback : float
value for background
vdots : float
value for dots
vtarget : float
value for target
Returns
-------
A stimulus dictionary with the stimulus ['img'] and target mask ['mask']
"""

padding = (distance/2., distance/2., distance/2., distance/2.)
patch = disc(ppd, dot_radius, vback=0., vdisc=vdots)
patch = pad_img(patch, padding, ppd, 0.)

img_height = pixels_to_degrees(n_dots[0] * patch.shape[0], ppd)
img_width = pixels_to_degrees(n_dots[1] * patch.shape[1], ppd)
rec_height = pixels_to_degrees(target_shape[0] * patch.shape[0], ppd)
rec_width = pixels_to_degrees(target_shape[1] * patch.shape[1], ppd)

# Create the sbc and img:
tposy = (img_height-rec_height) / 2.
tposx = (img_width-rec_width) / 2.
sbc = rectangle(ppd, im_size=(img_height, img_width), rect_size=(rec_height, rec_width),
rect_pos=(tposy, tposx), vback=vback, vrect=vtarget)
img = np.ones(sbc.shape) * vback

patch = np.tile(patch, (n_dots[0], n_dots[1]))
indices_dots_back = np.where((patch != 0) & (sbc == vback))
indices_dots_target = np.where((patch != 0) & (sbc == vtarget))
img[indices_dots_back] = vdots
img[indices_dots_target] = vtarget
mask = None
return {"img": img, "mask": mask}


if __name__ == "__main__":
import matplotlib.pyplot as plt
from stimuli.utils import plot_stim
from stimuli.utils import plot_stimuli

stims = {
"SBC": simultaneous_contrast(),
"SBC with dots": sbc_with_dots(),
"Dotted SBC": dotted_sbc(),
}

stim = simultaneous_contrast()
plot_stim(stim, stim_name="Simultaneous brightness contrast")
plot_stimuli(stims)
plt.show()

0 comments on commit 61ce0ff

Please sign in to comment.