Skip to content

Commit

Permalink
ran auto-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
LynnSchmittwilken committed Mar 14, 2023
1 parent 6f82203 commit d8578c0
Show file tree
Hide file tree
Showing 30 changed files with 357 additions and 318 deletions.
60 changes: 35 additions & 25 deletions stimupy/components/__init__.py
Expand Up @@ -494,12 +494,14 @@ def draw_sine_wave(
if origin is None:
raise ValueError("draw_sine_wave() missing argument 'origin' which is not 'None'")
if round_phase_width is None:
raise ValueError("draw_sine_wave() missing argument 'round_phase_width' which is not 'None'")
raise ValueError(
"draw_sine_wave() missing argument 'round_phase_width' which is not 'None'"
)
if period is None:
period = "ignore"

base_types = ["horizontal", "vertical", "rotated", "radial", "angular", "cityblock"]
if not base_type in base_types:
if base_type not in base_types:
raise ValueError(f"base_type needs to be one of {base_types}")

lst = [visual_size, ppd, shape, frequency, n_phases, phase_width]
Expand Down Expand Up @@ -533,12 +535,12 @@ def draw_sine_wave(
ppd_1D = ppd.horizontal
else:
ppd_1D = None
if rotation%90 != 0 and round_phase_width:

if rotation % 90 != 0 and round_phase_width:
round_phase_width = False
warnings.warn("Rounding phase width is turned off for oblique gratings")
if rotation%90 != 0 and period != "ignore":

if rotation % 90 != 0 and period != "ignore":
period = "ignore"
warnings.warn("Period ignored for oblique gratings")

Expand All @@ -562,7 +564,7 @@ def draw_sine_wave(

# Determine size/shape of whole image
if None in shape:
shape = [length*alpha[1], length*alpha[0]]
shape = [length * alpha[1], length * alpha[0]]
if np.round(alpha[1], 5) == 0:
shape[0] = shape[1]
if np.round(alpha[0], 5) == 0:
Expand All @@ -579,39 +581,47 @@ def draw_sine_wave(
ppd = resolution.validate_ppd(ppd)

# Set up coordinates
base = image_base(shape=shape, visual_size=visual_size, ppd=ppd, rotation=rotation, origin=origin)
base = image_base(
shape=shape, visual_size=visual_size, ppd=ppd, rotation=rotation, origin=origin
)
distances = base[base_type]
distances = np.round(distances, 6)

# Shift distances minimally to ensure proper behavior
if origin == "corner":
distances = adapt_intensity_range(distances, 1e-03, distances.max()-1e-03)
distances = adapt_intensity_range(distances, 1e-03, distances.max() - 1e-03)
else:
distances = adapt_intensity_range(distances, distances.min()-1e-05, distances.max()-1e-05)
distances = adapt_intensity_range(
distances, distances.min() - 1e-05, distances.max() - 1e-05
)

# Draw image
img = np.sin(frequency * 2 * np.pi * distances + np.deg2rad(phase_shift))
img = adapt_intensity_range(img, intensities[0], intensities[1])

# Create mask
if origin == "corner" or base_type=="radial" or base_type=="cityblock":
vals = np.arange(distances.min()+phase_width/2, distances.max()+phase_width*2, phase_width)

if origin == "corner" or base_type == "radial" or base_type == "cityblock":
vals = np.arange(
distances.min() + phase_width / 2, distances.max() + phase_width * 2, phase_width
)

if origin == "mean":
vals -= distances.min()
else:
dmin = distances.min()
dmax = distances.max()+phase_width*2
vals1 = np.arange(0+phase_width/2, dmax, phase_width)
vals2 = -np.arange(-phase_width/2, -dmin+phase_width, phase_width)
dmax = distances.max() + phase_width * 2
vals1 = np.arange(0 + phase_width / 2, dmax, phase_width)
vals2 = -np.arange(-phase_width / 2, -dmin + phase_width, phase_width)
vals = np.unique(np.append(vals2[::-1], vals1))

phase_shift_ = (phase_shift%360)/180 * phase_width
mask = round_to_vals(distances-distances.min(), np.round(vals-phase_shift_, 6)-distances.min())

phase_shift_ = (phase_shift % 360) / 180 * phase_width
mask = round_to_vals(
distances - distances.min(), np.round(vals - phase_shift_, 6) - distances.min()
)

for i, val in enumerate(np.unique(mask)):
mask = np.where(mask==val, i+1, mask)
mask = np.where(mask == val, i + 1, mask)

stim = {
"img": img,
"mask": mask.astype(int),
Expand All @@ -621,7 +631,7 @@ def draw_sine_wave(
"frequency": frequency,
"n_phases": n_phases,
"phase_width": phase_width,
}
}
return stim


Expand Down Expand Up @@ -722,7 +732,7 @@ def create_overview():

def overview(mask=False, save=None, extent_key="shape"):
"""
Plot overview with examples from all stimulus-components
Plot overview with examples from all stimulus-components
Parameters
----------
Expand Down
28 changes: 14 additions & 14 deletions stimupy/components/circulars.py
Expand Up @@ -15,7 +15,7 @@
"bessel",
"sine_wave",
"square_wave",
]
]


def resolve_circular_params(
Expand Down Expand Up @@ -214,7 +214,7 @@ def disc_and_rings(
# one axis is None; make square
visual_size = [x for x in visual_size if x is not None]
visual_size = resolution.validate_visual_size(visual_size)

if np.diff(radii).min() < 0:
raise ValueError("radii need to monotonically increase")

Expand Down Expand Up @@ -352,7 +352,7 @@ def ring(
if radii[1] is None:
shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd)
radii[1] = np.max(visual_size) / 2

if radii[1] < radii[0]:
raise ValueError("first radius needs to be smaller than second radius")

Expand All @@ -365,7 +365,7 @@ def ring(
intensity_background=intensity_background,
origin=origin,
)
stim["ring_mask"] = np.where(stim["ring_mask"]==2, 1, 0)
stim["ring_mask"] = np.where(stim["ring_mask"] == 2, 1, 0)
return stim


Expand Down Expand Up @@ -507,19 +507,19 @@ def sine_wave(
origin=origin,
round_phase_width=False,
base_type="radial",
)
)

if clip:
csize = min(sw["visual_size"]) / 2.
csize = min(sw["visual_size"]) / 2.0
circle = disc(
visual_size=sw["visual_size"],
ppd=sw["ppd"],
radius=csize,
origin=origin,
)
)
sw["img"] = np.where(circle["ring_mask"], sw["img"], intensity_background)
sw["mask"] = np.where(circle["ring_mask"], sw["mask"], 0)

# Create stimulus dict
stim = {
"img": sw["img"],
Expand All @@ -532,7 +532,7 @@ def sine_wave(
"frame_width": sw["phase_width"],
"n_frames": sw["n_phases"],
"intensity_rings": intensity_rings,
}
}
return stim


Expand Down Expand Up @@ -596,19 +596,19 @@ def square_wave(
intensity_rings=intensity_rings,
origin=origin,
clip=False,
)
)

# Round sine-wave to create square wave
stim["img"] = round_to_vals(stim["img"], intensity_rings)

if clip:
csize = min(stim["visual_size"]) / 2.
csize = min(stim["visual_size"]) / 2.0
circle = disc(
visual_size=stim["visual_size"],
ppd=stim["ppd"],
radius=csize,
origin=origin,
)
)
stim["img"] = np.where(circle["ring_mask"], stim["img"], intensity_background)
stim["ring_mask"] = np.where(circle["ring_mask"], stim["ring_mask"], 0).astype(int)
return stim
Expand Down
23 changes: 13 additions & 10 deletions stimupy/components/edges.py
Expand Up @@ -2,7 +2,6 @@
import numpy as np

from stimupy.components import gaussians, image_base
from stimupy.utils import resolution

__all__ = [
"step_edge",
Expand Down Expand Up @@ -186,18 +185,18 @@ def cornsweet_edge(
"""
if ramp_width is None:
raise ValueError("cornsweet_edge() missing argument 'ramp_width' which is not 'None'")

base = image_base(
visual_size=visual_size,
ppd=ppd,
shape=shape,
rotation=rotation,
origin="mean",
)
)

if ramp_width > max(base["visual_size"]) / 2:
raise ValueError("ramp_width is too large")

dist = np.round(base["rotated"] / ramp_width, 6)
d1 = copy.deepcopy(dist)
d2 = copy.deepcopy(dist) * (-1)
Expand All @@ -207,11 +206,15 @@ def cornsweet_edge(
d2[d2 > 1] = 1

# Create ramp profiles individually for left and right side
profile1 = (1.0 - d1) ** exponent * (intensity_edges[0] - intensity_plateau) + intensity_plateau
profile2 = (1.0 - d2) ** exponent * (intensity_edges[1] - intensity_plateau) + intensity_plateau
img = np.where(d1==-1, 0, profile1) + np.where(d2==-1, 0, profile2)
mask = np.where(d1==-1, 0, 2) + np.where(d2==-1, 0, 1)
mask[mask==3] = 1
profile1 = (1.0 - d1) ** exponent * (
intensity_edges[0] - intensity_plateau
) + intensity_plateau
profile2 = (1.0 - d2) ** exponent * (
intensity_edges[1] - intensity_plateau
) + intensity_plateau
img = np.where(d1 == -1, 0, profile1) + np.where(d2 == -1, 0, profile2)
mask = np.where(d1 == -1, 0, 2) + np.where(d2 == -1, 0, 1)
mask[mask == 3] = 1

stim = {
"img": img,
Expand Down
20 changes: 10 additions & 10 deletions stimupy/components/frames.py
Expand Up @@ -97,7 +97,7 @@ def frames(
"""
if radii is None:
raise ValueError("frames() missing argument 'radii' which is not 'None'")

if np.diff(radii).min() < 0:
raise ValueError("radii need to monotonically increase")

Expand Down Expand Up @@ -191,7 +191,7 @@ def sine_wave(
origin=origin,
round_phase_width=False,
base_type="cityblock",
)
)

if clip:
if origin == "corner":
Expand All @@ -201,17 +201,17 @@ def sine_wave(
ppd=sw["ppd"],
rectangle_size=rsize,
rectangle_position=(0, 0),
)
)
else:
rsize = min(sw["visual_size"])
rect = rectangle(
visual_size=sw["visual_size"],
ppd=sw["ppd"],
rectangle_size=rsize,
)
)
sw["img"] = np.where(rect["shape_mask"], sw["img"], intensity_background)
sw["mask"] = np.where(rect["shape_mask"], sw["mask"], 0)

# Create stimulus dict
stim = {
"img": sw["img"],
Expand All @@ -225,7 +225,7 @@ def sine_wave(
"n_frames": sw["n_phases"],
"period": period,
"intensity_frames": intensity_frames,
}
}
return stim


Expand Down Expand Up @@ -295,11 +295,11 @@ def square_wave(
intensity_frames=intensity_frames,
origin=origin,
clip=False,
)
)

# Round sine-wave to create square wave
stim["img"] = round_to_vals(stim["img"], intensity_frames)

if clip:
if origin == "corner":
rsize = min(stim["visual_size"]) / 2
Expand All @@ -308,14 +308,14 @@ def square_wave(
ppd=stim["ppd"],
rectangle_size=rsize,
rectangle_position=(0, 0),
)
)
else:
rsize = min(stim["visual_size"])
rect = rectangle(
visual_size=stim["visual_size"],
ppd=stim["ppd"],
rectangle_size=rsize,
)
)
stim["img"] = np.where(rect["shape_mask"], stim["img"], intensity_background)
stim["frame_mask"] = np.where(rect["shape_mask"], stim["frame_mask"], 0)
return stim
Expand Down

0 comments on commit d8578c0

Please sign in to comment.