Skip to content

Commit

Permalink
Merge branch 'master' into stimuli_masks
Browse files Browse the repository at this point in the history
  • Loading branch information
matko031 committed Jul 15, 2021
2 parents b09354a + 298a289 commit 5c835c1
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 75 deletions.
1 change: 1 addition & 0 deletions stimuli/illusions/__init__.py
Expand Up @@ -4,6 +4,7 @@
from .checkerboard_sbc import checkerboard_contrast
from .cube import cube_illusion
from .dungeon import dungeon_illusion
from .disc_and_ring import disc_and_ring
from .grating_induction import grating_illusion
from .grating import grating_illusion
from .hermann import hermann_grid
Expand Down
59 changes: 54 additions & 5 deletions stimuli/illusions/checkerboard_sbc.py
@@ -1,15 +1,16 @@
import numpy as np
import stimuli.illusions
from stimuli.utils import degrees_to_pixels, pad_img, plot_stim
from stimuli.Stimulus import Stimulus

def checkerboard_contrast(ppd=10, n_checks=8, check_size=1.0, target1_coords=(3, 2), target2_coords=(5, 5), extend_targets=False,
def checkerboard_contrast(ppd=10, board_shape=(8,8), check_size=1.0, target1_coords=(3, 2), target2_coords=(5, 5), extend_targets=False,
padding=(1.0,1.0,1.0,1.0), check1=0., check2=1., target=.5):
"""
Checkerboard Contrast
Parameters
----------
n_checks: number of checks per board in each direction
board shape: number of checks per board in y, x direction
check_size: size of a check in px
target1_coords: check-coordinates of target check 1
target2_coords: check-coordinates of target check 2
Expand All @@ -25,11 +26,12 @@ def checkerboard_contrast(ppd=10, n_checks=8, check_size=1.0, target1_coords=(3,
"""

check_size_px = degrees_to_pixels(check_size, ppd)
height_checks, width_checks = board_shape

arr = np.ndarray((n_checks, n_checks))
mask = np.zeros((n_checks, n_checks))
arr = np.ndarray((height_checks, width_checks))
mask = np.zeros((height_checks, width_checks))

for i, j in np.ndindex((n_checks, n_checks)):
for i, j in np.ndindex((height_checks, width_checks)):
arr[i, j] = check1 if i % 2 == j % 2 else check2

arr[target1_coords] = target
Expand Down Expand Up @@ -65,8 +67,55 @@ def domijan2015():
def domijan2015_extended():
return checkerboard_contrast(ppd=10, board_shape=(8,8), check_size=1.0, target1_coords=(3, 2), target2_coords=(5, 5), extend_targets=True, padding=(.9,1.1,.9,1.1), check1=1., check2=9., target=5.)

def RHS2007_Checkerboard016():
total_height, total_width, ppd = (32,) * 3
height_checks, width_checks = 40, 102
check_height, check_width = 32/102, 32/102
board_shape = (height_checks, width_checks)
height, width = check_height*height_checks, check_width*width_checks
padding_horizontal = (total_width - width) / 2
padding_vertical = (total_height - height) / 2
padding = (padding_vertical, padding_vertical, padding_horizontal, padding_horizontal)
img = stimuli.illusions.checkerboard_contrast(ppd=ppd, board_shape=board_shape, check_size=check_height, target1_coords=(20, 17), target2_coords=(20, 86),
extend_targets=False, padding=padding, check1=0, check2=1, target=.5)
return img



def RHS2007_Checkerboard0938():
total_height, total_width, ppd = (32,) * 3
height_checks, width_checks = 7, 25
check_height, check_width = 0.938, 0.938
board_shape = (height_checks, width_checks)
height, width = check_height*height_checks, check_width*width_checks
padding_horizontal = (total_width - width) / 2
padding_vertical = (total_height - height) / 2
padding = (padding_vertical, padding_vertical, padding_horizontal, padding_horizontal)
target_height = height_checks //2
img = stimuli.illusions.checkerboard_contrast(ppd=ppd, board_shape=board_shape, check_size=check_height, target1_coords=(target_height, 6), target2_coords=(target_height, 17),
extend_targets=False, padding=padding, check1=0, check2=1, target=.5)
return img



def RHS2007_Checkerboard209():
total_height, total_width, ppd = (32,) * 3
height_checks, width_checks = 3, 10
check_height, check_width = 2.09, 2.09
board_shape = (height_checks, width_checks)
height, width = check_height*height_checks, check_width*width_checks
padding_horizontal = (total_width - width) / 2
padding_vertical = (total_height - height) / 2
padding = (padding_vertical, padding_vertical, padding_horizontal, padding_horizontal)
target_height = height_checks //2
img = stimuli.illusions.checkerboard_contrast(ppd=ppd, board_shape=board_shape, check_size=check_height, target1_coords=(target_height, 2), target2_coords=(target_height, 7),
extend_targets=False, padding=padding, check1=0, check2=1, target=.5)
return img



if __name__ == '__main__':
import matplotlib.pyplot as plt
stim = checkerboard_contrast()
plt.figure()
plot_stim(stim, mask=True)
29 changes: 17 additions & 12 deletions stimuli/illusions/disc_and_ring.py
Expand Up @@ -3,9 +3,11 @@
from stimuli.Stimulus import Stimulus


def disc_and_ring(shape=(10,10), radii=(9,5), values=(200, 100), bg=0, ppd=30, ssf=5):
#TODO: the parameters aren't analogous to the other stimuli
#TODO: figure out defeault parameters that create something that makes sense
def disc_and_ring(
shape=(10, 10), radii=(4, 2), values=(0.5, 1), bg=0, ppd=30, ssf=5
):
# TODO: the parameters aren't analogous to the other stimuli
# TODO: figure out defeault parameters that create something that makes sense
"""
Create a disc and ring stimulus with an arbitrary number of rings.
Expand Down Expand Up @@ -34,30 +36,33 @@ def disc_and_ring(shape=(10,10), radii=(9,5), values=(200, 100), bg=0, ppd=30, s
assert len(radii) == len(values)

# create stimulus at 5 times size to allow for supersampling antialiasing
stim = np.ones(degrees_to_pixels(np.array(shape), ppd).astype(int) * ssf) * bg
img = (
np.ones(degrees_to_pixels(np.array(shape), ppd).astype(int) * ssf) * bg
)
# compute distance from center of array for every point, cap at 1.0
x = np.linspace(-stim.shape[1] / 2., stim.shape[1] / 2., stim.shape[1])
y = np.linspace(-stim.shape[0] / 2., stim.shape[0] / 2., stim.shape[0])
x = np.linspace(-img.shape[1] / 2.0, img.shape[1] / 2.0, img.shape[1])
y = np.linspace(-img.shape[0] / 2.0, img.shape[0] / 2.0, img.shape[0])
dist = np.sqrt(x[np.newaxis, :] ** 2 + y[:, np.newaxis] ** 2)

radii = degrees_to_pixels(np.array(radii), ppd) * ssf
for radius, value in zip(radii, values):
stim[dist < radius] = value
img[dist < radius] = value

# downsample the stimulus by local averaging along rows and columns
sampler = resize_array(np.eye(stim.shape[0] // ssf), (1, ssf))
sampler = resize_array(np.eye(img.shape[0] // ssf), (1, ssf))

mask = None

stim = Stimulus()
stim.img = np.dot(sampler, np.dot(stim, sampler.T)) / ssf ** 2
stim.img = np.dot(sampler, np.dot(img, sampler.T)) / ssf ** 2
stim.target_mask = mask

return stim


if __name__ == '__main__':
if __name__ == "__main__":
import matplotlib.pyplot as plt

img, mask = disc_and_ring()
plt.imshow(img, cmap='gray')
plt.show()
plt.imshow(img, cmap="gray")
plt.show()
6 changes: 3 additions & 3 deletions stimuli/illusions/whites.py
Expand Up @@ -227,7 +227,7 @@ def RHS2007_WE_thick():
padding_horizontal = (total_width - width) / 2
padding_vertical = (total_height - height) / 2
padding = (padding_vertical, padding_vertical, padding_horizontal, padding_horizontal)
target_height = stimuli.utils.degrees_to_pixels(4, ppd)
target_height = 4
return stimuli.illusions.whites.white(shape=(height, width), ppd=ppd, frequency=frequency, start='low', target_indices=(2, 5), padding=padding, target_height=target_height)

def RHS2007_WE_thin_wide():
Expand All @@ -238,7 +238,7 @@ def RHS2007_WE_thin_wide():
padding_horizontal = (total_width - width) / 2
padding_vertical = (total_height - height) / 2
padding = (padding_vertical, padding_vertical, padding_horizontal, padding_horizontal)
target_height = stimuli.utils.degrees_to_pixels(4, ppd)
target_height = 4
return stimuli.illusions.whites.white(shape=(height, width), ppd=ppd, frequency=frequency, start='low', target_indices=(3, 12), padding=padding, target_height=target_height)

def RHS2007_WE_dual():
Expand All @@ -252,7 +252,7 @@ def RHS2007_WE_dual():
padding_horizontal2, padding_vertical2 = (total_width / 2 - height) / 2, (total_height - width) / 2
padding2 = (padding_vertical2, padding_vertical2, padding_horizontal2, padding_horizontal2)

target_height = stimuli.utils.degrees_to_pixels(2, ppd)
target_height = 2
stim1 = stimuli.illusions.whites.white(shape=(height, width), ppd=ppd, frequency=frequency, start='low', target_indices=(2, 5), padding=padding1, target_height=target_height)
stim2 = stimuli.illusions.whites.white(shape=(height, width), ppd=ppd, frequency=frequency, start='low', target_indices=(2, 5), padding=padding2, target_height=target_height, orientation='vertical')

Expand Down
10 changes: 4 additions & 6 deletions stimuli/papers/RHS2007.py
Expand Up @@ -81,16 +81,14 @@ def todorovic_out():
pass

def checkerboard_016():
#TODO: not available atm
pass
return stimuli.illusions.checkerboard_sbc.RHS2007_Checkerboard016()

def checkerboard_094():
#TODO: not available atm
pass
return stimuli.illusions.checkerboard_sbc.RHS2007_Checkerboard0938()

def checkerboard21():
#TODO: not available atm
pass
return stimuli.illusions.checkerboard_sbc.RHS2007_Checkerboard209()


def corrugated_mondrian():
#TODO: not available atm
Expand Down
Empty file added tests/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions tests/demo_lightness.py
@@ -0,0 +1,48 @@
import matplotlib.pyplot as plt
from stimuli import lightness

# %% Cornsweet / Todorovic
a = lightness.cornsweet((10, 10), 10, 0.5)
b = lightness.todorovic(a, 2, 2)

plt.figure()
plt.imshow(a, vmin=0, vmax=1, cmap="gray")
plt.show()
plt.figure()
plt.imshow(b, vmin=0, vmax=1, cmap="gray")
plt.show()

# %% Square Wave
c = lightness.square_wave((10, 10), 10, 0.5, 2)

plt.figure()
plt.imshow(c, vmin=0, vmax=1, cmap="gray")
plt.show()

# %% White's Illusion BMCC
d = lightness.whites_illusion_bmcc((10, 10), 10, 0.5, 2)
e1, e2 = lightness.contours_white_bmmc((10, 10), 10, 0.5, 2, contour_width=3)

plt.figure()
plt.imshow(d, vmin=0, vmax=1, cmap="gray")
plt.show()
plt.figure()
plt.imshow(e1, vmin=0, vmax=1, cmap="gray")
plt.show()
plt.figure()
plt.imshow(e2, vmin=0, vmax=1, cmap="gray")
plt.show()

# %% White's Illusion Gil
f = lightness.whites_illusion_gil((10, 10), 10, 0.5, 2)

plt.figure()
plt.imshow(f, vmin=0, vmax=1, cmap="gray")
plt.show()

# %% Disc and Ring
g = lightness.disc_and_ring((10, 10), [4, 2], [0.5, 1.0])

plt.figure()
plt.imshow(g, vmin=0, vmax=1, cmap="gray")
plt.show()

0 comments on commit 5c835c1

Please sign in to comment.