Skip to content

Commit

Permalink
Merge pull request #57 from computational-psychology/feat_rename_masks
Browse files Browse the repository at this point in the history
closes #13; renamed shape_masks and grating_masks in checkerboard
  • Loading branch information
LynnSchmittwilken committed Mar 24, 2023
2 parents 3c73c5d + f6c7636 commit 16f15d6
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 60 deletions.
12 changes: 6 additions & 6 deletions docs/reference/demos/stimuli/benarys.md
Expand Up @@ -53,7 +53,7 @@ w_trot = iw.IntSlider(value=0, min=0, max=360, description="target rotation [deg
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int cross")
w_int_back = iw.FloatSlider(value=0, min=0, max=1, description="int background")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'shape_mask'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'cross_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
Expand Down Expand Up @@ -141,7 +141,7 @@ w_tint = iw.FloatSlider(value=0.5, min=0, max=1, description="target int")
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int cross")
w_int_back = iw.FloatSlider(value=0, min=0, max=1, description="int background")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'shape_mask'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'cross_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
Expand Down Expand Up @@ -215,7 +215,7 @@ w_tint = iw.FloatSlider(value=0.5, min=0, max=1, description="target int")
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int cross")
w_int_back = iw.FloatSlider(value=0, min=0, max=1, description="int background")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'shape_mask'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'cross_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
Expand Down Expand Up @@ -293,7 +293,7 @@ w_trot = iw.IntSlider(value=0, min=0, max=360, description="target rotation [deg
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int cross")
w_int_back = iw.FloatSlider(value=0, min=0, max=1, description="int background")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'shape_mask'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'L_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
Expand Down Expand Up @@ -381,7 +381,7 @@ w_tint = iw.FloatSlider(value=0.5, min=0, max=1, description="target int")
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int cross")
w_int_back = iw.FloatSlider(value=0, min=0, max=1, description="int background")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'shape_mask'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'L_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
Expand Down Expand Up @@ -455,7 +455,7 @@ w_tint = iw.FloatSlider(value=0.5, min=0, max=1, description="target int")
w_int1 = iw.FloatSlider(value=1, min=0, max=1, description="int cross")
w_int_back = iw.FloatSlider(value=0, min=0, max=1, description="int background")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'shape_mask'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'L_mask'], description="add mask")
# Layout
b_im_size = iw.HBox([w_height, w_width, w_ppd])
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/demos/stimuli/checkerboards.md
Expand Up @@ -48,7 +48,7 @@ w_int2 = iw.FloatSlider(value=0., min=0, max=1, description="int2")
w_period = iw.Dropdown(value="ignore", options=['ignore', 'even', 'odd', 'either'], description="period")
w_round = iw.ToggleButton(value=False, disabled=False, description="round check width")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'checker_mask', 'grating_mask', 'grating_mask2'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'checker_mask', 'row_mask', 'col_mask'], description="add mask")
w_tidxx = iw.IntSlider(value=0, min=0, max=10, description="target x-idx")
w_tidxy = iw.IntSlider(value=0, min=0, max=10, description="target y-idx")
Expand Down Expand Up @@ -142,7 +142,7 @@ w_int2 = iw.FloatSlider(value=0., min=0, max=1, description="int2")
w_period = iw.Dropdown(value="ignore", options=['ignore', 'even', 'odd', 'either'], description="period")
w_round = iw.ToggleButton(value=False, disabled=False, description="round check width")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'checker_mask', 'grating_mask', 'grating_mask2'], description="add mask")
w_mask = iw.Dropdown(value=None, options=[None, 'target_mask', 'checker_mask', 'row_mask', 'col_mask'], description="add mask")
w_tidxy = iw.IntSlider(value=5, min=0, max=10, description="target height")
w_tidxx = iw.IntSlider(value=5, min=0, max=10, description="target width")
Expand Down
2 changes: 1 addition & 1 deletion stimupy/components/gaussians.py
Expand Up @@ -82,7 +82,7 @@ def gaussian(
rotation=rotation,
origin=origin,
restrict_size=False,
)["shape_mask"]
)["ellipse_mask"]

stim = {
"img": gaussian,
Expand Down
6 changes: 3 additions & 3 deletions stimupy/components/lines.py
Expand Up @@ -290,9 +290,9 @@ def ellipse(
origin="mean",
)

stim["img"] = np.where(stim2["shape_mask"] == 1, intensity_background, stim["img"])
stim["line_mask"] = np.where(stim2["shape_mask"] == 1, 0, stim["shape_mask"])
del stim["shape_mask"]
stim["img"] = np.where(stim2["ellipse_mask"] == 1, intensity_background, stim["img"])
stim["line_mask"] = np.where(stim2["ellipse_mask"] == 1, 0, stim["ellipse_mask"])
del stim["ellipse_mask"]
return stim


Expand Down
26 changes: 14 additions & 12 deletions stimupy/components/shapes.py
Expand Up @@ -55,7 +55,7 @@ def rectangle(
-------
dict[str, Any]
dict with the stimulus (key: "img"),
mask with integer index for the shape (key: "shape_mask"),
mask with integer index for the shape (key: "rectangle_mask"),
and additional keys containing stimulus parameters
"""
if rectangle_size is None:
Expand Down Expand Up @@ -125,7 +125,7 @@ def rectangle(

return {
"img": img * (intensity_rectangle - intensity_background) + intensity_background,
"shape_mask": img.astype(int),
"rectangle_mask": img.astype(int),
"visual_size": base["visual_size"],
"ppd": base["ppd"],
"shape": base["shape"],
Expand Down Expand Up @@ -170,7 +170,7 @@ def triangle(
-------
dict[str, Any]
dict with the stimulus (key: "img"),
mask with integer index for the shape (key: "shape_mask"),
mask with integer index for the shape (key: "triangle_mask"),
and additional keys containing stimulus parameters
"""
if triangle_size is None:
Expand Down Expand Up @@ -209,11 +209,11 @@ def triangle(
rectangle_size=triangle_size,
rotation=rotation,
)
img = img * rect["shape_mask"]
img = img * rect["rectangle_mask"]

return {
"img": img * (intensity_triangle - intensity_background) + intensity_background,
"shape_mask": img.astype(int),
"triangle_mask": img.astype(int),
"visual_size": base["visual_size"],
"ppd": base["ppd"],
"shape": base["shape"],
Expand Down Expand Up @@ -262,7 +262,7 @@ def cross(
-------
dict[str, Any]
dict with the stimulus (key: "img"),
mask with integer index for the shape (key: "shape_mask"),
mask with integer index for the shape (key: "cross_mask"),
and additional keys containing stimulus parameters
"""
if cross_size is None:
Expand Down Expand Up @@ -319,7 +319,7 @@ def cross(

return {
"img": img * (intensity_cross - intensity_background) + intensity_background,
"shape_mask": img.astype(int),
"cross_mask": img.astype(int),
"shape": shape,
"visual_size": visual_size,
"ppd": ppd,
Expand Down Expand Up @@ -364,7 +364,7 @@ def parallelogram(
-------
dict[str, Any]
dict with the stimulus (key: "img"),
mask with integer index for the shape (key: "shape_mask"),
mask with integer index for the shape (key: "parallelogram_mask"),
and additional keys containing stimulus parameters
"""
if parallelogram_size is None:
Expand Down Expand Up @@ -433,7 +433,7 @@ def parallelogram(

return {
"img": img * (intensity_parallelogram - intensity_background) + intensity_background,
"shape_mask": img.astype(int),
"parallelogram_mask": img.astype(int),
"shape": base["shape"],
"visual_size": base["visual_size"],
"ppd": base["ppd"],
Expand Down Expand Up @@ -484,7 +484,7 @@ def ellipse(
-------
dict[str, Any]
dict with the stimulus (key: "img"),
mask with integer index for the shape (key: "shape_mask"),
mask with integer index for the shape (key: "ellipse_mask"),
and additional keys containing stimulus parameters
"""
if radius is None:
Expand Down Expand Up @@ -525,7 +525,7 @@ def ellipse(

return {
"img": img * (intensity_ellipse - intensity_background) + intensity_background,
"shape_mask": img.astype(int),
"ellipse_mask": img.astype(int),
"shape": base["shape"],
"visual_size": base["visual_size"],
"ppd": base["ppd"],
Expand Down Expand Up @@ -573,7 +573,7 @@ def circle(
----------
dict[str, Any]
dict with the stimulus (key: "img"),
mask with integer index for the shape (key: "shape_mask"),
mask with integer index for the shape (key: "circle_mask"),
and additional keys containing stimulus parameters
"""
if radius is None:
Expand All @@ -592,6 +592,8 @@ def circle(
origin=origin,
restrict_size=restrict_size,
)
stim["circle_mask"] = stim["ellipse_mask"]
stim.pop("ellipse_mask")
return stim


Expand Down
8 changes: 5 additions & 3 deletions stimupy/stimuli/benarys.py
Expand Up @@ -393,7 +393,7 @@ def todorovic_generalized(
stim["L_width"] = L_width
stim["intensity_background"] = intensity_background
stim["intensity_cross"] = intensity_cross
stim["shape_mask"] = mask
stim["L_mask"] = mask
return stim


Expand Down Expand Up @@ -677,6 +677,8 @@ def add_targets(
intensity_background=0,
rotation=target_rotation[i],
)
mpatch = target["rectangle_mask"][~np.all(target["rectangle_mask"] == 0, axis=1)]
tpatch = target["img"][~np.all(target["rectangle_mask"] == 0, axis=1)]

elif target_type[i] == "t":
target = triangle(
Expand All @@ -688,13 +690,13 @@ def add_targets(
rotation=target_rotation[i],
include_corners=True,
)
mpatch = target["triangle_mask"][~np.all(target["triangle_mask"] == 0, axis=1)]
tpatch = target["img"][~np.all(target["triangle_mask"] == 0, axis=1)]

else:
raise Exception("You can only use r or t as shapes")

# Remove zero-rows and -columns
mpatch = target["shape_mask"][~np.all(target["shape_mask"] == 0, axis=1)]
tpatch = target["img"][~np.all(target["shape_mask"] == 0, axis=1)]
tpatch = tpatch[:, ~np.all(mpatch == 0, axis=0)]
mpatch = mpatch[:, ~np.all(mpatch == 0, axis=0)]
theight_, twidth_ = tpatch.shape
Expand Down
8 changes: 4 additions & 4 deletions stimupy/stimuli/checkerboards.py
Expand Up @@ -38,8 +38,8 @@ def mask_from_idx(checkerboard_stim, check_idc):
for i, coords in enumerate(check_idc):
if coords[0] < 0 or coords[0] > coords[0] or coords[1] < 0 or coords[1] > board_shape[1]:
raise ValueError(f"Cannot provide mask for check {coords} outside board {board_shape}")
m1 = np.where(checkerboard_stim["grating_mask"] == coords[1] + 1, 1, 0)
m2 = np.where(checkerboard_stim["grating_mask2"] == coords[0] + 1, 1, 0)
m1 = np.where(checkerboard_stim["col_mask"] == coords[1] + 1, 1, 0)
m2 = np.where(checkerboard_stim["row_mask"] == coords[0] + 1, 1, 0)
mask = np.where(m1 + m2 == 2, i + 1, mask)

if len(np.unique(mask)) == 1:
Expand Down Expand Up @@ -281,8 +281,8 @@ def checkerboard(
stim = {
"img": img,
"checker_mask": mask.astype(int),
"grating_mask": sw1["grating_mask"],
"grating_mask2": sw2["grating_mask"],
"col_mask": sw1["grating_mask"],
"row_mask": sw2["grating_mask"],
"visual_size": sw1["visual_size"],
"ppd": sw1["ppd"],
"shape": sw1["shape"],
Expand Down
8 changes: 4 additions & 4 deletions stimupy/stimuli/gratings.py
Expand Up @@ -453,8 +453,8 @@ def grating_induction(
)

# Superimpose
stim["img"] = np.where(target_mask["shape_mask"], intensity_target, stim["img"])
stim["target_mask"] = np.where(target_mask["shape_mask"], stim["grating_mask"], 0)
stim["img"] = np.where(target_mask["rectangle_mask"], intensity_target, stim["img"])
stim["target_mask"] = np.where(target_mask["rectangle_mask"], stim["grating_mask"], 0)
return stim


Expand Down Expand Up @@ -569,8 +569,8 @@ def grating_induction_blur(
)

# Superimpose
stim["img"] = np.where(target_mask["shape_mask"], intensity_target, stim["img"])
stim["target_mask"] = np.where(target_mask["shape_mask"], stim["grating_mask"], 0)
stim["img"] = np.where(target_mask["rectangle_mask"], intensity_target, stim["img"])
stim["target_mask"] = np.where(target_mask["rectangle_mask"], stim["grating_mask"], 0)
return stim


Expand Down
2 changes: 1 addition & 1 deletion stimupy/stimuli/mondrians.py
Expand Up @@ -123,7 +123,7 @@ def mondrian(
if (ypos + yshape > shape[0]) or (xpos + xshape > shape[1]):
raise ValueError("Not all Mondrians fit into the stimulus")
mask_large = np.zeros(shape)
mask_large[ypos : ypos + yshape, xpos : xpos + xshape] = patch["shape_mask"]
mask_large[ypos : ypos + yshape, xpos : xpos + xshape] = patch["parallelogram_mask"]

img[mask_large == 1] = next(ints)
mask[mask_large == 1] = m + 1
Expand Down
2 changes: 1 addition & 1 deletion stimupy/stimuli/pinwheels.py
Expand Up @@ -108,7 +108,7 @@ def pinwheel(
shape=shape,
radius=radius,
origin=origin,
)["shape_mask"]
)["circle_mask"]

stim["img"] = np.where(circle_mask, stim["img"], intensity_background)

Expand Down
12 changes: 6 additions & 6 deletions stimupy/stimuli/sbcs.py
Expand Up @@ -68,12 +68,12 @@ def generalized(
intensity_rectangle=intensity_target,
)

stim["target_mask"] = stim["shape_mask"]
stim["target_mask"] = stim["rectangle_mask"]
stim["target_size"] = stim["rectangle_size"]
stim["target_position"] = stim["rectangle_position"]
stim["intensity_target"] = stim["intensity_rectangle"]
del (
stim["shape_mask"],
stim["rectangle_mask"],
stim["rectangle_size"],
stim["rectangle_position"],
stim["intensity_rectangle"],
Expand Down Expand Up @@ -308,11 +308,11 @@ def with_dots(
intensity_rectangle=intensity_target,
)
img = sbc["img"]
mask = sbc["shape_mask"]
mask = sbc["rectangle_mask"]

patch = np.tile(patch, (int(n_dots[0]), int(n_dots[1])))
img = np.where(patch == 1, intensity_dots, sbc["img"])
mask = np.where(patch == 1, 0, sbc["shape_mask"])
mask = np.where(patch == 1, 0, sbc["rectangle_mask"])

try:
img = pad_to_shape(img, shape, intensity_background)
Expand Down Expand Up @@ -537,8 +537,8 @@ def dotted(

patch = np.tile(patch, (int(n_dots[0]), int(n_dots[1])))
img = np.where(patch, intensity_dots, intensity_background)
img = np.where(patch + sbc["shape_mask"] == 2, intensity_target, img)
mask = np.where(patch + sbc["shape_mask"] == 2, 1, 0)
img = np.where(patch + sbc["rectangle_mask"] == 2, intensity_target, img)
mask = np.where(patch + sbc["rectangle_mask"] == 2, 1, 0)

try:
img = pad_to_shape(img, shape, intensity_background)
Expand Down
10 changes: 5 additions & 5 deletions stimupy/stimuli/todorovics.py
Expand Up @@ -110,7 +110,7 @@ def rectangle_generalized(
intensity_rectangle=intensity_target,
)
img = stim["img"]
mask = stim["shape_mask"]
mask = stim["rectangle_mask"]

# Add covers
cheight, cwidth = resolution.lengths_from_visual_angles_ppd(
Expand Down Expand Up @@ -442,7 +442,7 @@ def cross_generalized(

stim = pad_dict_to_shape(stim, shape=shape, pad_value=intensity_background)
img = stim["img"]
mask = stim["shape_mask"]
mask = stim["cross_mask"]

cheight, cwidth = resolution.lengths_from_visual_angles_ppd(
covers_size, np.unique(ppd), round=False
Expand Down Expand Up @@ -480,7 +480,7 @@ def cross_generalized(
stim["visual_size"] = visual_size
stim["ppd"] = ppd
stim["shape"] = shape
del (stim["cross_size"], stim["intensity_cross"], stim["shape_mask"])
del (stim["cross_size"], stim["intensity_cross"], stim["cross_mask"])
return stim


Expand Down Expand Up @@ -757,8 +757,8 @@ def equal(
rectangle_size=cross_size,
)

stim["img"] = np.where(window["shape_mask"], stim["img"], intensity_background)
stim["target_mask"] = np.where(window["shape_mask"], stim["target_mask"], 0).astype(int)
stim["img"] = np.where(window["rectangle_mask"], stim["img"], intensity_background)
stim["target_mask"] = np.where(window["rectangle_mask"], stim["target_mask"], 0).astype(int)
return stim


Expand Down

0 comments on commit 16f15d6

Please sign in to comment.