Skip to content

Commit

Permalink
Use new checkerboard in illusions
Browse files Browse the repository at this point in the history
  • Loading branch information
JorisVincent committed Oct 7, 2022
1 parent f1d6e91 commit 0022c3a
Showing 1 changed file with 130 additions and 65 deletions.
195 changes: 130 additions & 65 deletions stimuli/illusions/checkerboards.py
@@ -1,65 +1,113 @@
import numpy as np
from stimuli.utils import degrees_to_pixels
from stimuli.components import checkerboard


def contrast(
ppd=30,
board_shape=(8, 8),
check_size=1.0,
target_indices=((3, 2), (5, 5)),
from stimuli.components.checkerboard import checkerboard as board
from stimuli.utils import degrees_to_pixels, resolution


def mask_from_idx(checkerboard_stim, check_idc):
check_size = checkerboard_stim["check_visual_size"]
ppd = checkerboard_stim["ppd"]

check_size_px = resolution.shape_from_visual_size_ppd(check_size, ppd)

mask = np.zeros(checkerboard_stim["shape"])
for coords in check_idc:
ypos = int(coords[0] * check_size_px.height)
xpos = int(coords[1] * check_size_px.width)
mask[
ypos : ypos + int(check_size_px.height),
xpos : xpos + int(check_size_px.width),
] = 1

return mask


def extend_target_idx(target_idx, offsets=[(-1, 0), (0, -1), (0, 0), (0, 1), (1, 0)]):
extended_idc = []
for offset in offsets:
new_idx = (target_idx[0] + offset[0], target_idx[1] + offset[1])
extended_idc.append(new_idx)
return extended_idc


def add_targets(checkerboard_stim, targets, extend_targets=False, intensity_target=0.5):
mask = np.zeros(checkerboard_stim["shape"])
for i, target in enumerate(targets):
if extend_targets:
target_idc = extend_target_idx(target)
else:
target_idc = [target]
for target_idx in target_idc:
mask += mask_from_idx(checkerboard_stim, (target_idx,)) * (i + 1)
img = np.where(mask, intensity_target, checkerboard_stim["img"])

checkerboard_stim["img"] = img
checkerboard_stim["mask"] = mask
return checkerboard_stim


def checkerboard(
shape=None,
ppd=None,
visual_size=None,
board_shape=None,
check_visual_size=None,
targets=None,
extend_targets=False,
vcheck1=0.0,
vcheck2=1.0,
vtarget=0.5,
intensity_low=0.0,
intensity_high=1.0,
intensity_target=0.5,
):
"""
Checkerboard Contrast
Parameters
----------
ppd : int
pixels per degree (visual angle)
board shape : (int, int)
number of checks per board in y, x direction
check_size : float
size of a check in degrees visual angle
targets_indices : tuple of (int, int)
tuple with check-indices (row, col)
extend_targets : bool
cross targets instead of single-check targets
vcheck1 : float
first check value
vcheck2 : float
other check value
vtarget: float
target value
Returns
-------
A stimulus dictionary with the stimulus ['img'] and target mask ['mask']
"""

check_size_px = degrees_to_pixels(check_size, ppd)
nchecks_height, nchecks_width = board_shape

img = checkerboard(ppd, board_shape, check_size, vcheck1, vcheck2)
mask = np.zeros(img.shape)

for i, coords in enumerate(target_indices):
ypos = int(coords[0]*check_size_px)
xpos = int(coords[1]*check_size_px)
img[ypos:ypos+check_size_px, xpos:xpos+check_size_px] = vtarget
mask[ypos:ypos+check_size_px, xpos:xpos+check_size_px] = i + 1

if extend_targets:
for i, coords in enumerate(target_indices):
for idx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
ypos = int(coords[0]*check_size_px + idx[0]*check_size_px)
xpos = int(coords[1]*check_size_px + idx[1]*check_size_px)
img[ypos:ypos+check_size_px, xpos:xpos+check_size_px] = vtarget
mask[ypos:ypos+check_size_px, xpos:xpos+check_size_px] = i + 1
return {"img": img, "mask": mask}
stim = board(
ppd=ppd,
shape=shape,
visual_size=visual_size,
board_shape=board_shape,
check_visual_size=check_visual_size,
intensity_low=intensity_low,
intensity_high=intensity_high,
)

if targets is not None:
stim = add_targets(
stim,
targets=targets,
extend_targets=extend_targets,
intensity_target=intensity_target,
)

return stim


# def contrast(
# ppd=30,
# board_shape=(8, 8),
# check_size=1.0,
# target_indices=((3, 2), (5, 5)),
# extend_targets=False,
# vcheck1=0.0,
# vcheck2=1.0,
# vtarget=0.5,
# ):
# check_size_px = degrees_to_pixels(check_size, ppd)
# nchecks_height, nchecks_width = board_shape

# img = checkerboard(ppd, board_shape, check_size, vcheck1, vcheck2)
# mask = np.zeros(img.shape)

# for i, coords in enumerate(target_indices):
# ypos = int(coords[0] * check_size_px)
# xpos = int(coords[1] * check_size_px)
# img[ypos : ypos + check_size_px, xpos : xpos + check_size_px] = vtarget
# mask[ypos : ypos + check_size_px, xpos : xpos + check_size_px] = i + 1

# if extend_targets:
# for i, coords in enumerate(target_indices):
# for idx in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
# ypos = int(coords[0] * check_size_px + idx[0] * check_size_px)
# xpos = int(coords[1] * check_size_px + idx[1] * check_size_px)
# img[ypos : ypos + check_size_px, xpos : xpos + check_size_px] = vtarget
# mask[ypos : ypos + check_size_px, xpos : xpos + check_size_px] = i + 1
# return {"img": img, "mask": mask}


def contrast_contrast(
Expand Down Expand Up @@ -102,13 +150,23 @@ def contrast_contrast(
check_size_px = degrees_to_pixels(check_size, ppd)
nchecks_height, nchecks_width = board_shape

img = checkerboard(ppd, board_shape, check_size, vcheck1, vcheck2)
stim = board(
ppd=ppd,
board_shape=board_shape,
check_visual_size=check_size,
intensity_low=vcheck1,
intensity_high=vcheck2,
)
img = stim["img"]
mask = np.zeros(img.shape)

idx = np.zeros(img.shape, dtype=bool)
tposy = (img.shape[0] - target_shape[0]*check_size_px) // 2
tposx = (img.shape[1] - target_shape[1]*check_size_px) // 2
idx[tposy:tposy+target_shape[0]*check_size_px, tposx:tposx+target_shape[1]*check_size_px] = True
tposy = (img.shape[0] - target_shape[0] * check_size_px) // 2
tposx = (img.shape[1] - target_shape[1] * check_size_px) // 2
idx[
tposy : tposy + target_shape[0] * check_size_px,
tposx : tposx + target_shape[1] * check_size_px,
] = True
img[idx] = alpha * img[idx] + (1 - alpha) * tau
mask[idx] = 1
return {"img": img, "mask": mask}
Expand All @@ -118,9 +176,16 @@ def contrast_contrast(
import matplotlib.pyplot as plt
from stimuli.utils import plot_stimuli

DEFAULT_PPD = 32

stims = {
"Checkerboard contrast": contrast(),
"Checkerboard contrast contrast": contrast_contrast(),
"Checkerboard (brightness)": checkerboard(
ppd=32,
board_shape=(8, 8),
check_visual_size=(2, 2),
targets=[(3, 2), (5, 5)],
),
"Checkerboard contrast-contrast": contrast_contrast(),
}
ax = plot_stimuli(stims, mask=False)
plt.show()

0 comments on commit 0022c3a

Please sign in to comment.