Skip to content

Commit

Permalink
updated wedding_cake; added limits and renamed variable
Browse files Browse the repository at this point in the history
  • Loading branch information
LynnSchmittwilken committed Mar 17, 2023
1 parent 364913d commit feb695d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 11 deletions.
123 changes: 123 additions & 0 deletions docs/reference/demos/illusions/wedding_cakes.md
@@ -0,0 +1,123 @@
---
jupytext:
formats: md:myst
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.14.5
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---

```{important}
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/computational-psychology/stimupy/dev_docs?urlpath=tree/docs/reference/demos/illusions/wedding_cakes.md)
to get interactivity
```

# Illusions - Wedding cakes
{py:mod}`stimupy.illusions.wedding_cakes`

```{code-cell} ipython3
:tags: [remove-cell]
import IPython
import ipywidgets as iw
from stimupy.utils import plot_stim
```

## Wedding cake
{py:func}`stimupy.illusions.wedding_cakes.wedding_cake`

```{code-cell} ipython3
from stimupy.illusions.wedding_cakes import wedding_cake
# Define widgets
w_height = iw.IntSlider(value=10, min=1, max=20, description="height [deg]")
w_width = iw.IntSlider(value=10, min=1, max=20, description="width [deg]")
w_ppd = iw.IntSlider(value=20, min=1, max=40, description="ppd")
w_Lheight = iw.FloatSlider(value=2, min=0, max=5, description="L-height [deg]")
w_Lwidth = iw.FloatSlider(value=2, min=0, max=5, description="L-width [deg]")
w_Lthick = iw.FloatSlider(value=0.5, min=0, max=2, description="L-thickness [deg]")
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int1")
w_int2 = iw.FloatSlider(value=0., min=0, max=1, description="int2")
w_tidx11 = iw.IntSlider(value=0, min=0, max=5, description="target1 idx1")
w_tidx12 = iw.IntSlider(value=0, min=-5, max=5, description="target1 idx2")
w_tidx21 = iw.IntSlider(value=0, min=0, max=5, description="target1 idx1")
w_tidx22 = iw.IntSlider(value=0, min=-5, max=5, description="target2 idx2")
w_theight = iw.FloatSlider(value=0.5, min=0, max=2, description="target height [deg]")
w_tint = iw.FloatSlider(value=0.5, min=0, max=1, description="target int")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
b_geometry = iw.HBox([w_Lheight, w_Lwidth, w_Lthick])
b_intensities = iw.HBox([w_int1, w_int2])
b_target = iw.HBox([w_theight, w_tint])
b_target_idx = iw.HBox([w_tidx11, w_tidx12, w_tidx21, w_tidx22])
b_add = iw.HBox([w_mask])
ui = iw.VBox([b_im_size, b_geometry, b_intensities, b_target, b_target_idx, b_add])
# Function for showing stim
def show_wedding_cake(
height=None,
width=None,
ppd=None,
add_mask=False,
L_height=None,
L_width=None,
L_thick=None,
int1=None,
int2=None,
tidx11=None,
tidx12=None,
tidx21=None,
tidx22=None,
theight=None,
int_target=None,
):
stim = wedding_cake(
visual_size=(height, width),
ppd=ppd,
L_size=(L_height, L_width, L_thick),
target_height=theight,
intensity_target=int_target,
target_indices1=((tidx11, tidx12),),
target_indices2=((tidx21, tidx22),),
intensity_bars=(int1, int2),
)
plot_stim(stim, mask=add_mask)
# Set interactivity
out = iw.interactive_output(
show_wedding_cake,
{
"height": w_height,
"width": w_width,
"ppd": w_ppd,
"add_mask": w_mask,
"L_height": w_Lheight,
"L_width": w_Lwidth,
"L_thick": w_Lthick,
"int1": w_int1,
"int2": w_int2,
"tidx11": w_tidx11,
"tidx12": w_tidx12,
"tidx21": w_tidx21,
"tidx22": w_tidx22,
"theight": w_theight,
"int_target": w_tint,
},
)
# Show
display(ui, out)
```
24 changes: 14 additions & 10 deletions stimupy/illusions/wedding_cakes.py
Expand Up @@ -7,8 +7,6 @@
"wedding_cake",
]

# TODO: constrain allowed stimulus sizes


def wedding_cake(
visual_size=None,
Expand All @@ -18,7 +16,7 @@ def wedding_cake(
target_height=None,
target_indices1=None,
target_indices2=None,
intensity_grating=(1.0, 0.0),
intensity_bars=(1.0, 0.0),
intensity_target=0.5,
):
"""
Expand All @@ -42,8 +40,8 @@ def wedding_cake(
target_indices2 : nested tuples
target indices with intensity2-value; as many tuples as there are targets
each with (y, x) indices
intensity_grating : (float, float)
intensity values of the grating
intensity_bars : (float, float)
intensity values of the bars
intensity_target : float
intensity value of targets
Expand All @@ -67,10 +65,16 @@ def wedding_cake(
shape, visual_size, ppd_ = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd)
if len(np.unique(ppd)) > 1:
raise ValueError("ppd should be equal in x and y direction")
if visual_size[1] < visual_size[0] * 0.75 or visual_size[1] > visual_size[0] * 1.25:
raise ValueError(
"wedding_cake not available for images with very different heights+widths"
)

nY, nX = shape
Ly, Lx, Lw = resolution.lengths_from_visual_angles_ppd(L_size, np.unique(ppd))
Lyh, Lxh = int(Ly / 2) + 1, int(Lx / 2) + 1
if Lw > Ly / 2:
raise ValueError("L-thickness should not exceed L_height / 2")

# Create L-shaped patch
L_patch = np.zeros([Ly, Lx])
Expand Down Expand Up @@ -100,7 +104,7 @@ def wedding_cake(
theight = resolution.lengths_from_visual_angles_ppd(target_height, np.unique(ppd))
tpatch1 = np.zeros(L_patch.shape)
tpatch1[int(Ly / 2 - theight / 2) : int(Ly / 2 + theight / 2), Lx - Lw : :] = (
-intensity_grating[1] + intensity_target
-intensity_bars[1] + intensity_target
)

array_t1 = np.zeros(array3.shape)
Expand All @@ -123,7 +127,7 @@ def wedding_cake(
theight = resolution.lengths_from_visual_angles_ppd(target_height, np.unique(ppd))
tpatch2 = np.zeros(L_patch.shape)
tpatch2[int(Ly / 2 - theight / 2) : int(Ly / 2 + theight / 2), Lx - Lw : :] = (
-intensity_grating[0] + intensity_target
-intensity_bars[0] + intensity_target
)

array_t2 = np.zeros(array3.shape)
Expand All @@ -145,8 +149,8 @@ def wedding_cake(
imgt = convolve(array3, L_patch, "same")
imgt = np.round(imgt)
img = np.copy(imgt)
img[imgt > 1] = intensity_grating[1]
img[imgt < 1] = intensity_grating[0]
img[imgt > 1] = intensity_bars[1]
img[imgt < 1] = intensity_bars[0]
img = img[Lyh : Lyh + int(nY / 2), Lxh : Lxh + int(nX / 2)]

# Create target mask
Expand All @@ -165,7 +169,7 @@ def wedding_cake(
"ppd": ppd,
"L_size": L_size,
"target_height": target_height,
"intensity_grating": intensity_grating,
"intensity_bars": intensity_bars,
"intensity_target": intensity_target,
"target_indices1": target_indices1,
"target_indices2": target_indices2,
Expand Down
2 changes: 1 addition & 1 deletion stimupy/papers/RHS2007.py
Expand Up @@ -439,7 +439,7 @@ def WE_zigzag(ppd=PPD, pad=True):
target_height=2.0,
target_indices1=((i1, -1), (i1, 0), (i1, 1), (i1, 2)),
target_indices2=((i2, 0), (i2, 1), (i2, 2), (i2, 3)),
intensity_grating=(1.0, 0.0),
intensity_bars=(1.0, 0.0),
intensity_target=0.5,
)

Expand Down

0 comments on commit feb695d

Please sign in to comment.