Skip to content

Commit

Permalink
added Stimulus class and made all functions return a stimulus object
Browse files Browse the repository at this point in the history
  • Loading branch information
matko031 committed Jul 3, 2021
1 parent c68b54c commit 72a9983
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 93 deletions.
5 changes: 5 additions & 0 deletions stimuli/Stimulus.py
@@ -0,0 +1,5 @@
class Stimulus():
def __init__(self):
target_mask = None
img = None

11 changes: 7 additions & 4 deletions stimuli/illusions/benary_cross.py
@@ -1,6 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img

from stimuli.Stimulus import Stimulus

def benarys_cross(ppd=10, cross_size=(8,8,8,8), cross_thickness=5, padding=(1,1,1,1), target_size=2, back=1., cross=0., target=.5):
"""
Expand Down Expand Up @@ -51,13 +51,16 @@ def benarys_cross(ppd=10, cross_size=(8,8,8,8), cross_thickness=5, padding=(1,1,
img = pad_img(img, padding, ppd, back)
mask = pad_img(mask, padding, ppd, 0)

return (img, mask)
stim = Stimulus()
stim.target_mask = mask
stim.img = img
return stim

def domijan2015():
return benarys_cross(ppd=10, cross_size=(3,3,3,3), cross_thickness=2.1, padding=(.9,1.0,.9,1.0),target_size=1.1, back=9., cross=1., target=5.)

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = benarys_cross()
plt.imshow(img, cmap='gray')
stim = benarys_cross()
plt.imshow(stim.img, cmap='gray')
plt.show()
18 changes: 13 additions & 5 deletions stimuli/illusions/bullseye.py
@@ -1,6 +1,7 @@
import numpy as np
from stimuli.illusions.rings import ring_pattern
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus

def bullseye_illusion(ppd=10, n_rings=8, ring_width=.5, target_pos_l=0, target_pos_r=0, padding=(1.0,1.0,1.0,1.0), back=0., rings=1., target=.5):
"""
Expand All @@ -20,12 +21,19 @@ def bullseye_illusion(ppd=10, n_rings=8, ring_width=.5, target_pos_l=0, target_p
-------
2D numpy array
"""
img1, mask1 = ring_pattern(n_rings=n_rings, target_pos_l=target_pos_l, ring_width=ring_width, padding=padding,
stim1 = ring_pattern(n_rings=n_rings, target_pos_l=target_pos_l, ring_width=ring_width, padding=padding,
back=back, rings=rings, target=target, invert_rings=False, double=False)
img2, mask2 = ring_pattern(n_rings=n_rings, target_pos_l=target_pos_r, ring_width=ring_width, padding=padding,
stim2 = ring_pattern(n_rings=n_rings, target_pos_l=target_pos_r, ring_width=ring_width, padding=padding,
back=back, rings=rings, target=target, invert_rings=True, double=False)

return (np.hstack((img1, img2)), np.hstack((mask1, mask2)))
img = np.hstack((stim1.img, stim2.img))
mask = np.hstack((stim1.target_mask, stim2.target_mask))

stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim

def domijan2015():
img = bullseye_illusion(n_rings=8, ring_width=.5, target_pos_l=0, target_pos_r=0, padding=(.9,1.0,.9,1.0), back=1., rings=9., target=5.)
Expand All @@ -39,6 +47,6 @@ def RHS2007_bullseye_thick():

if __name__ == '__main__':
import matplotlib.pyplot as plt
img, mask = bullseye_illusion()
plt.imshow(img, cmap='gray')
stim = bullseye_illusion()
plt.imshow(stim.img, cmap='gray')
plt.show()
9 changes: 8 additions & 1 deletion stimuli/illusions/checkerboard_contrast_contrast.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus


def checkerboard_contrast_contrast_effect(ppd=10, n_checks=8, check_size=1.0, target_length=4, padding=(1.0,1.0,1.0,1.0), check1=0., check2=2.,
Expand Down Expand Up @@ -53,7 +54,13 @@ def checkerboard_contrast_contrast_effect(ppd=10, n_checks=8, check_size=1.0, ta
mask2 = np.repeat(np.repeat(mask_arr2, check_size_px, axis=0), check_size_px, axis=1)
mask2 = pad_img(mask2, padding, ppd, 0)

return (np.hstack([img1, img2]), np.hstack([mask1, mask2]))
img = np.hstack([img1, img2])
mask = np.hstack([mask1, mask2])
stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim



Expand Down
7 changes: 6 additions & 1 deletion stimuli/illusions/checkerboard_sbc.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
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,
padding=(1.0,1.0,1.0,1.0), check1=0., check2=1., target=.5):
Expand Down Expand Up @@ -51,7 +52,11 @@ def checkerboard_contrast(ppd=10, n_checks=8, check_size=1.0, target1_coords=(3,
img = pad_img(img, padding, ppd, (check1+check2)/2)
mask = pad_img(mask, padding, ppd, 0)

return (img, mask)
stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim


def domijan2015():
Expand Down
8 changes: 7 additions & 1 deletion stimuli/illusions/cornsweet.py
@@ -1,4 +1,5 @@
import numpy as np
from stimuli.Stimulus import Stimulus

def cornsweet(size=(10,10), ppd=10, contrast=0.5, ramp_width=2, exponent=2.75,
mean_lum=.5):
Expand Down Expand Up @@ -55,7 +56,12 @@ def cornsweet(size=(10,10), ppd=10, contrast=0.5, ramp_width=2, exponent=2.75,
stim[:, :size[1] // 2] += profile[::-1]
stim[:, size[1] // 2:] -= profile
mask = None
return (stim, mask)

stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim


if __name__ == '__main__':
Expand Down
14 changes: 10 additions & 4 deletions stimuli/illusions/cube.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus


def cube_illusion(ppd=10, n_cells=4, target_length=1, cell_long=1.5, cell_short=1.0, corner_cell_width=1.8, corner_cell_height=1.8,
Expand Down Expand Up @@ -109,11 +110,16 @@ def cube_illusion(ppd=10, n_cells=4, target_length=1, cell_long=1.5, cell_short=
mask = pad_img(mask, padding, ppd, 0)

if double:
img2, mask2 = cube_illusion(ppd=ppd, n_cells=n_cells, target_length=target_length, cell_long=cell_long, cell_short=cell_short, corner_cell_width=corner_cell_width, corner_cell_height=corner_cell_height,
stim2 = cube_illusion(ppd=ppd, n_cells=n_cells, target_length=target_length, cell_long=cell_long, cell_short=cell_short, corner_cell_width=corner_cell_width, corner_cell_height=corner_cell_height,
cell_spacing=cell_spacing, padding=padding, occlusion_overlap=occlusion_overlap, back=grid, grid=back, target=target, double=False)
return (np.hstack([img, img2]), np.hstack([mask, mask2]))
else:
return (img, mask)
img = np.hstack([img, stim2.img])
mask = np.hstack([mask, stim2.target_mask])

stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim


def domijan2015():
Expand Down
7 changes: 6 additions & 1 deletion stimuli/illusions/disc_and_ring.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils.utils import degrees_to_pixels, resize_array
from stimuli.Stimulus import Stimulus


def disc_and_ring(shape=(10,10), radii=(9,5), values=(200, 100), bg=0, ppd=30, ssf=5):
Expand Down Expand Up @@ -48,7 +49,11 @@ def disc_and_ring(shape=(10,10), radii=(9,5), values=(200, 100), bg=0, ppd=30, s

mask = None

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

return stim


if __name__ == '__main__':
Expand Down
14 changes: 10 additions & 4 deletions stimuli/illusions/dungeon.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus


def dungeon_illusion(ppd=10, n_cells=5, target_radius=1, cell_size=1.0, padding=(1.0,1.0,1.0,1.0), back=0., grid=1., target=0.5, double=True):
Expand Down Expand Up @@ -51,10 +52,15 @@ def dungeon_illusion(ppd=10, n_cells=5, target_radius=1, cell_size=1.0, padding=
mask = pad_img(mask, padding, ppd, False)

if double:
img2, mask2 = dungeon_illusion(ppd=ppd, n_cells=n_cells, target_radius=target_radius, cell_size=cell_size, padding=padding, back=grid, grid=back, target=target, double=False)
return (np.hstack([img, img2]), np.hstack([mask, mask2]))
else:
return (img, mask)
stim2 = dungeon_illusion(ppd=ppd, n_cells=n_cells, target_radius=target_radius, cell_size=cell_size, padding=padding, back=grid, grid=back, target=target, double=False)
img = np.hstack([img, stim2.img])
mask = np.hstack([mask, stim2.target_mask])

stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim

def domijan2015():
return dungeon_illusion(ppd=10, n_cells=5, target_radius=1,cell_size=1.0, padding=(.9,1.1,.9,1.1), back=1.0, grid=9.0, target=5.0, double=True)
Expand Down
14 changes: 10 additions & 4 deletions stimuli/illusions/grating.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus


def grating_illusion(ppd=10, n_bars=5, target_length=1, bar_width=1.0, bar_height=8.0, padding=(1.0,1.0,1.0,1.0), back=0., grid=1., target=0.5, double=True):
Expand Down Expand Up @@ -50,11 +51,16 @@ def grating_illusion(ppd=10, n_bars=5, target_length=1, bar_width=1.0, bar_heigh


if double:
img2, mask2 = grating_illusion(ppd=ppd, n_bars=n_bars, target_length=target_length, bar_width=bar_width, bar_height=bar_height,
stim2 = grating_illusion(ppd=ppd, n_bars=n_bars, target_length=target_length, bar_width=bar_width, bar_height=bar_height,
padding=padding, back=grid, grid=back, target=target, double=False)
return (np.hstack([img, img2]), np.hstack([mask, mask2]))
else:
return (img, mask)
img = np.hstack([img, stim2.img])
mask = np.hstack([mask, stim2.target_mask])

stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim

def domijan2015():
return grating_illusion(ppd=10, n_bars=5, target_length=1, bar_width=1.0, bar_height=8.1, padding=(.9,1.0,.9,1.1), back=1, grid=9, target=5, double=True)
Expand Down
7 changes: 6 additions & 1 deletion stimuli/illusions/grating_induction.py
@@ -1,6 +1,7 @@
import numpy as np
from scipy.ndimage.filters import gaussian_filter
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus

import stimuli

Expand All @@ -26,7 +27,11 @@ def grating_illusion(shape=(10,10), ppd=40, frequency=0.5, target_height=0.5, bl
img = pad_img(img, padding, ppd, target)
mask = pad_img(mask, padding, ppd, 0)

return (img, mask)
stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim

def RHS2007_grating_induction():
total_height, total_width, ppd = (32,)*3
Expand Down
14 changes: 10 additions & 4 deletions stimuli/illusions/rings.py
@@ -1,5 +1,6 @@
import numpy as np
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus

def ring_pattern(ppd=10, n_rings=8, target_pos_l=4, target_pos_r=3, ring_width=.5, padding=(1.0,1.0,1.0,1.0,), back=0., rings=1., target=.5, invert_rings=False, double=True, ):
"""
Expand Down Expand Up @@ -63,11 +64,16 @@ def ring_pattern(ppd=10, n_rings=8, target_pos_l=4, target_pos_r=3, ring_width=.

# create right half of stimulus
if double:
img2, mask2 = ring_pattern(ppd=ppd, n_rings=n_rings, target_pos_l=target_pos_r, target_pos_r=0, ring_width=ring_width,
stim2 = ring_pattern(ppd=ppd, n_rings=n_rings, target_pos_l=target_pos_r, target_pos_r=0, ring_width=ring_width,
padding=padding, back=back, rings=rings, target=target, invert_rings=invert_rings, double=False)
return (np.hstack([img, img2]), np.hstack([mask, mask2]))
else:
return (img, mask)
img = np.hstack([img, stim2.img])
mask = np.hstack([mask, stim2.target_mask])

stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim

def domijan2015():
img = ring_pattern(ppd=10, n_rings=8, target_pos_l=4, target_pos_r=3, ring_width=.5, padding=(.9,1.0,.9,1.0), back=1., rings=9., target=5., invert_rings=False, double=True)
Expand Down
7 changes: 6 additions & 1 deletion stimuli/illusions/sbc.py
@@ -1,6 +1,7 @@
import numpy as np
import stimuli
from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus


def simultaneous_brightness_contrast(ppd=10, target_shape=(5,5), padding=(2,2,2,2), inner_padding=(3,3,3,3), left=1., right=0., target=.5):
Expand Down Expand Up @@ -39,7 +40,11 @@ def simultaneous_brightness_contrast(ppd=10, target_shape=(5,5), padding=(2,2,2,
img = pad_img(img, padding, ppd, target)
mask = pad_img(mask, padding, ppd, 0)

return (img, mask)
stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim


def domijan2015():
Expand Down
12 changes: 8 additions & 4 deletions stimuli/illusions/todorovic.py
Expand Up @@ -3,6 +3,7 @@
import math

from stimuli.utils import degrees_to_pixels, pad_img
from stimuli.Stimulus import Stimulus


def todorovic_illusion(target_shape=(4,4), ppd=10, covers_shape=(2.5, 2.5), spacing=(1.5,1.5,1.5,1.5), inner_padding=(3,3,3,3), padding=(2,2,2,2), back=0., grid=1., target=.5, double=True):
Expand Down Expand Up @@ -72,16 +73,19 @@ def todorovic_illusion(target_shape=(4,4), ppd=10, covers_shape=(2.5, 2.5), spac

# create right half of stimulus
if double:
img2, mask2 = todorovic_illusion(target_shape=target_shape, ppd=ppd, covers_shape=covers_shape, spacing=spacing,
stim2 = todorovic_illusion(target_shape=target_shape, ppd=ppd, covers_shape=covers_shape, spacing=spacing,
padding=(0,0,0,0), inner_padding=inner_padding, back=grid, grid=back, target=target, double=False)
img = np.hstack([img, img2])
mask = np.hstack([mask, mask2])
img = np.hstack([img, stim2.img])
mask = np.hstack([mask, stim2.target_mask])

img = pad_img(img, padding, ppd, target)
mask = pad_img(mask, padding, ppd, target)

return (img, mask)
stim = Stimulus()
stim.img = img
stim.target_mask = mask

return stim

def domijan2015():
return todorovic_illusion(target_shape=(4.1, 4.1), ppd=10, covers_shape=(3.1, 3.1), spacing=(1.5, 1.5, 1.5, 1.5), inner_padding=(2.9,3.0, 2.9,3.0 ),
Expand Down

0 comments on commit 72a9983

Please sign in to comment.