Skip to content

Commit

Permalink
removed contrast from input variables
Browse files Browse the repository at this point in the history
  • Loading branch information
LynnSchmittwilken committed Feb 14, 2023
1 parent 294b4f4 commit 16da7ad
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 57 deletions.
25 changes: 14 additions & 11 deletions stimuli/noises/binaries.py
Expand Up @@ -4,6 +4,7 @@

import numpy as np
from stimuli.utils import resolution
from stimuli.utils.contrast_conversions import adapt_intensity_range


__all__ = [
Expand All @@ -14,7 +15,7 @@ def binary(
visual_size=None,
ppd=None,
shape=None,
rms_contrast=None,
intensity_range=(0, 1),
):
"""
Function to create white noise.
Expand All @@ -27,12 +28,14 @@ def binary(
pixels per degree [vertical, horizontal]
shape : Sequence[Number, Number], Number, or None (default)
shape [height, width] of grating, in pixels
rms_contrast : float
rms contrast of noise.
intensity_range : Sequence[Number, Number]
minimum and maximum intensity value; default: (0, 1)
Returns
-------
A stimulus dictionary with the noise array ['img']
dict[str, Any]
dict with the stimulus (key: "img"),
and additional keys containing stimulus parameters
"""
# Resolve resolution
shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd)
Expand All @@ -42,17 +45,18 @@ def binary(

binary_noise = np.random.randint(0, 2, size=shape) - 0.5

# Adjust noise rms contrast:
binary_noise = rms_contrast * binary_noise / binary_noise.std()
# Adjust intensity range:
binary_noise = adapt_intensity_range({"img": binary_noise}, intensity_range[0], intensity_range[1])["img"]

params = {
stim = {
"img": binary_noise,
"noise_mask": None,
"visual_size": visual_size,
"ppd": ppd,
"shape": shape,
"rms_contrast": rms_contrast,
"intensity_range": [binary_noise.min(), binary_noise.max()],
}
return {"img": binary_noise, "mask": None, **params}
return stim


if __name__ == "__main__":
Expand All @@ -61,10 +65,9 @@ def binary(
params = {
"visual_size": 10,
"ppd": 10,
"rms_contrast": 0.2,
}

stims = {
"Binary noise": binary(**params),
}
plot_stimuli(stims, mask=True, save=None, vmin=-0.5, vmax=0.5)
plot_stimuli(stims, mask=True, save=None)
25 changes: 14 additions & 11 deletions stimuli/noises/narrowbands.py
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from stimuli.utils import bandpass_filter, resolution
from stimuli.noises.utils import pseudo_white_spectrum
from stimuli.utils.contrast_conversions import adapt_intensity_range


__all__ = [
Expand All @@ -17,7 +18,7 @@ def narrowband(
shape=None,
center_frequency=None,
bandwidth=None,
rms_contrast=None,
intensity_range=(0, 1),
pseudo_noise=False,
):
"""
Expand All @@ -35,14 +36,16 @@ def narrowband(
noise center frequency in cpd
bandwidth : float
bandwidth of the noise in octaves
rms_contrast : float
rms contrast of noise.
intensity_range : Sequence[Number, Number]
minimum and maximum intensity value; default: (0, 1)
pseudo_noise : bool
if True, generate pseudo-random noise with ideal power spectrum.
Returns
-------
A stimulus dictionary with the noise array ['img']
dict[str, Any]
dict with the stimulus (key: "img"),
and additional keys containing stimulus parameters
"""
# Resolve resolution
shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd)
Expand Down Expand Up @@ -78,33 +81,33 @@ def narrowband(
narrow_noise = np.fft.ifft2(np.fft.ifftshift(narrow_noise_fft))
narrow_noise = np.real(narrow_noise)

# Adjust noise rms contrast:
narrow_noise = rms_contrast * narrow_noise / narrow_noise.std()
# Adjust intensity range:
narrow_noise = adapt_intensity_range({"img": narrow_noise}, intensity_range[0], intensity_range[1])["img"]

params = {
stim = {
"img": narrow_noise,
"noise_mask": None,
"visual_size": visual_size,
"ppd": ppd,
"shape": shape,
"rms_contrast": rms_contrast,
"center_frequency": center_frequency,
"sigma": sigma,
"pseudo_noise": pseudo_noise,
"intensity_range": [narrow_noise.min(), narrow_noise.max()],
}
return {"img": narrow_noise, "mask": None, **params}
return stim


if __name__ == "__main__":
from stimuli.utils import plot_stimuli
params = {
"visual_size": 10,
"ppd": 20,
"rms_contrast": 0.2,
"pseudo_noise": True,
}

stims = {
"Narrowband noise - 3cpd": narrowband(**params, bandwidth=1, center_frequency=3.0),
"Narrowband noise - 9cpd": narrowband(**params, bandwidth=1, center_frequency=9.0),
}
plot_stimuli(stims, mask=True, save=None, vmin=-0.5, vmax=0.5)
plot_stimuli(stims, mask=True, save=None)
43 changes: 22 additions & 21 deletions stimuli/noises/naturals.py
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from stimuli.utils import resolution
from stimuli.noises.utils import pseudo_white_spectrum
from stimuli.utils.contrast_conversions import adapt_intensity_range


__all__ = [
Expand All @@ -17,8 +18,8 @@ def one_over_f(
visual_size=None,
ppd=None,
shape=None,
rms_contrast=None,
exponent=None,
intensity_range=(0, 1),
pseudo_noise=False,
):
"""
Expand All @@ -32,16 +33,18 @@ def one_over_f(
pixels per degree [vertical, horizontal]
shape : Sequence[Number, Number], Number, or None (default)
shape [height, width] of grating, in pixels
rms_contrast : float
rms contrast of noise.
exponent
exponent used to create 1 / (f**exponent) noise.
exponent used to create 1 / (f**exponent) noise
intensity_range : Sequence[Number, Number]
minimum and maximum intensity value; default: (0, 1)
pseudo_noise : bool
if True, generate pseudo-random noise with ideal power spectrum.
Returns
-------
A stimulus dictionary with the noise array ['img']
dict[str, Any]
dict with the stimulus (key: "img"),
and additional keys containing stimulus parameters
"""
# Resolve resolution
shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd)
Expand Down Expand Up @@ -74,16 +77,15 @@ def one_over_f(
noise = np.fft.ifft2(np.fft.ifftshift(noise_fft))
noise = np.real(noise)

# Adjust noise rms contrast:
noise = rms_contrast * noise / noise.std()
# Adjust intensity range:
noise = adapt_intensity_range({"img": noise}, intensity_range[0], intensity_range[1])["img"]

stim = {
"img": noise,
"mask": None,
"noise_mask": None,
"visual_size": visual_size,
"ppd": ppd,
"shape": shape,
"rms_contrast": rms_contrast,
"exponent": exponent,
"pseudo_noise": pseudo_noise,
"intensity_range": [noise.min(), noise.max()],
Expand All @@ -95,7 +97,7 @@ def pink(
visual_size=None,
ppd=None,
shape=None,
rms_contrast=None,
intensity_range=(0, 1),
pseudo_noise=False,
):
"""
Expand All @@ -109,10 +111,10 @@ def pink(
pixels per degree [vertical, horizontal]
shape : Sequence[Number, Number], Number, or None (default)
shape [height, width] of grating, in pixels
rms_contrast : float
rms contrast of noise.
intensity_range : Sequence[Number, Number]
minimum and maximum intensity value; default: (0, 1)
pseudo_noise : bool
if True, generate pseudo-random noise with ideal power spectrum.
if True, generate pseudo-random noise with ideal power spectrum
Returns
-------
Expand All @@ -122,8 +124,8 @@ def pink(
stim = one_over_f(
visual_size=visual_size,
ppd=ppd,
rms_contrast=rms_contrast,
exponent=1.0,
intensity_range=intensity_range,
pseudo_noise=pseudo_noise)
return stim

Expand All @@ -132,7 +134,7 @@ def brown(
visual_size=None,
ppd=None,
shape=None,
rms_contrast=None,
intensity_range=(0, 1),
pseudo_noise=False,
):
"""
Expand All @@ -146,10 +148,10 @@ def brown(
pixels per degree [vertical, horizontal]
shape : Sequence[Number, Number], Number, or None (default)
shape [height, width] of grating, in pixels
rms_contrast : float
rms contrast of noise.
intensity_range : Sequence[Number, Number]
minimum and maximum intensity value; default: (0, 1)
pseudo_noise : bool
if True, generate pseudo-random noise with ideal power spectrum.
if True, generate pseudo-random noise with ideal power spectrum
Returns
-------
Expand All @@ -159,8 +161,8 @@ def brown(
stim = one_over_f(
visual_size=visual_size,
ppd=ppd,
rms_contrast=rms_contrast,
exponent=2.0,
intensity_range=intensity_range,
pseudo_noise=pseudo_noise)
return stim

Expand All @@ -170,7 +172,6 @@ def brown(
params = {
"visual_size": 10,
"ppd": 20,
"rms_contrast": 0.2,
"pseudo_noise": True,
}

Expand All @@ -179,4 +180,4 @@ def brown(
"Pink noise": pink(**params),
"Brown noise": brown(**params),
}
plot_stimuli(stims, mask=True, save=None, vmin=-0.5, vmax=0.5)
plot_stimuli(stims, mask=True, save=None)
5 changes: 2 additions & 3 deletions stimuli/noises/overview.py
Expand Up @@ -8,14 +8,13 @@
params = {
"visual_size": 10,
"ppd": 10,
"rms_contrast": 0.5,
"pseudo_noise": True,
}

# fmt: off
stims = {
# Binary
"binary_noise": binary(visual_size=10, ppd=10, rms_contrast=0.5),
"binary_noise": binary(visual_size=10, ppd=10),
# White
"white_noise": white(**params),
# One over frequency
Expand All @@ -29,4 +28,4 @@


if __name__ == "__main__":
plot_stimuli(stims, mask=True, save=None, vmin=-2, vmax=2)
plot_stimuli(stims, mask=True, save=None)
23 changes: 12 additions & 11 deletions stimuli/noises/whites.py
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from stimuli.utils import resolution
from stimuli.noises.utils import pseudo_white_spectrum
from stimuli.utils.contrast_conversions import adapt_intensity_range


__all__ = [
Expand All @@ -15,7 +16,7 @@ def white(
visual_size=None,
ppd=None,
shape=None,
rms_contrast=None,
intensity_range=(0, 1),
pseudo_noise=False,
):
"""
Expand All @@ -29,10 +30,10 @@ def white(
pixels per degree [vertical, horizontal]
shape : Sequence[Number, Number], Number, or None (default)
shape [height, width] of grating, in pixels
rms_contrast : float
rms contrast of noise.
intensity_range : Sequence[Number, Number]
minimum and maximum intensity value; default: (0, 1)
pseudo_noise : bool
if True, generate pseudo-random noise with ideal power spectrum.
if True, generate pseudo-random noise with ideal power spectrum
Returns
-------
Expand All @@ -55,18 +56,19 @@ def white(
# Create white noise and fft
white_noise = np.random.rand(*shape) * 2.0 - 1.0

# Adjust noise rms contrast:
white_noise = rms_contrast * white_noise / white_noise.std()
# Adjust intensity range:
white_noise = adapt_intensity_range({"img": white_noise}, intensity_range[0], intensity_range[1])["img"]

params = {
stim = {
"img": white_noise,
"noise_mask": None,
"visual_size": visual_size,
"ppd": ppd,
"shape": shape,
"rms_contrast": rms_contrast,
"pseudo_noise": pseudo_noise,
"intensity_range": [white_noise.min(), white_noise.max()],
}
return {"img": white_noise, "mask": None, **params}
return stim


if __name__ == "__main__":
Expand All @@ -75,11 +77,10 @@ def white(
params = {
"visual_size": 10,
"ppd": 20,
"rms_contrast": 0.2,
"pseudo_noise": True,
}

stims = {
"White noise": white(**params),
}
plot_stimuli(stims, mask=True, save=None, vmin=-0.5, vmax=0.5)
plot_stimuli(stims, mask=True, save=None)

0 comments on commit 16da7ad

Please sign in to comment.