stimupy.utils.utils#

Functions#

round_to_vals

Round array to provided values (vals)

int_factorize

All integer factors of integer n

get_function_argument_names

Get all argument names for a given function

apply_bessel

Bessel function of the first kind of real order and complex argument.

resize_array

Return a copy of an array, resized by the given factor.

resize_dict

Return a copy of an array, resized by the given factor.

stack_dicts

Return a dict with resized key-arrays by the given factor.

rotate_dict

Return a dict with key-arrays rotated by nrots*90 degrees.

flip_dict

Return a dict with key-arrays rotated by nrots*90 degrees.

roll_dict

Return a dict with key-arrays rolled by shift in axes.

strip_dict

Create a dictionary by stripping it from all keys that are not also an argument to the provided function

make_two_sided

Create two-sided version of a stimulus function

permutate_params

create_stimspace_stimuli

round_to_vals(arr, vals)[source]#

Round array to provided values (vals)

Parameters:
  • arr (np.ndarray) – Numpy array which values will be rounded

  • vals (Sequence(float, ...)) – Values to which array will be rounded

Returns:

out_arr – Rounded output array

Return type:

np.ndarray

int_factorize(n)[source]#

All integer factors of integer n

All integer factors, i.e., all integers that n is integer-divisible by. Also not a very efficient algorithm (brute force trial division), so should only be used as a helpter function.

Parameters:

n (int) – number to factorize

Returns:

set of all integer factors of n

Return type:

set

get_function_argument_names(func)[source]#

Get all argument names for a given function

Parameters:

func (function) – Get argument names from this function

Returns:

names – Tuple containing all argument names of given function

Return type:

tuple

apply_bessel(arr, order=0)[source]#

Bessel function of the first kind of real order and complex argument.

Parameters:
  • arr (np.ndarray) – Input array

  • order (float) – Order of the bessel function. Default is 0.

Returns:

out – Output array

Return type:

np.ndarray

resize_array(arr, factor)[source]#

Return a copy of an array, resized by the given factor. Every value is repeated factor[d] times along dimension d.

Parameters:
  • arr (2D array) – the array to be resized

  • factor (tuple of 2 ints) – the resize factor in the y and x dimensions

Return type:

An array of shape (arr.shape[0] * factor[0], arr.shape[1] * factor[1])

resize_dict(dct, factor, keys=('img', '*mask'))[source]#

Return a copy of an array, resized by the given factor. Every value is repeated factor[d] times along dimension d.

Parameters:
  • dct (dict) – dict containing arrays to be resized

  • factor (tuple of 2 ints) – the resize factor in the y and x dimensions

  • keys (Sequence[String, String] or String) – keys in dict for images to be padded

Returns:

same as input dict but with larger key-arrays according to “(arr.shape[0] * factor[0], arr.shape[1] * factor[1])” and updated keys for “visual_size” and “shape”

Return type:

dict[str, Any]

stack_dicts(dct1, dct2, direction='horizontal', keys=('img', '*mask'), keep_mask_indices=False)[source]#

Return a dict with resized key-arrays by the given factor. Every value is repeated factor[d] times along dimension d.

Parameters:
  • dct1 (dict) – dict containing arrays to be stacked

  • dct2 (dict) – dict containing arrays to be stacked

  • direction (str) – stack horizontal(ly) or vertical(ly) (default: horizontal)

  • keys (Sequence[String, String] or String) – keys in dict for images to be padded

Returns:

same as input dict1 but with stacked key-arrays and updated keys for “visual_size” and “shape”

Return type:

dict[str, Any]

rotate_dict(dct, nrots=1, keys=('img', '*mask'))[source]#

Return a dict with key-arrays rotated by nrots*90 degrees.

Parameters:
  • dct (dict) – dict containing arrays to be stacked

  • nrot (int) – number of rotations by 90 degrees

  • keys (Sequence[String, String] or String) – keys in dict for images to be padded

Returns:

same as input dict but with rotated key-arrays and updated keys for “visual_size” and “shape”

Return type:

dict[str, Any]

flip_dict(dct, direction='lr', keys=('img', '*mask'))[source]#

Return a dict with key-arrays rotated by nrots*90 degrees.

Parameters:
  • dct (dict) – dict containing arrays to be stacked

  • direction (str) – “lr” for left-right, “ud” for up-down flipping

  • keys (Sequence[String, String] or String) – keys in dict for images to be padded

Returns:

same as input dict but with flipped key-arrays

Return type:

dict[str, Any]

roll_dict(dct, shift, axes, keys=('img', '*mask'))[source]#

Return a dict with key-arrays rolled by shift in axes.

Parameters:
  • dct (dict) – dict containing arrays to be stacked

  • shift (int) – number of pixels by which to shift

  • axes (Number or Sequence[Number, ...]) – axes in which to shift

  • keys (Sequence[String, String] or String) – keys in dict for images to be padded

Returns:

same as input dict but with rolled key-arrays

Return type:

dict[str, Any]

strip_dict(dct, func)[source]#

Create a dictionary by stripping it from all keys that are not also an argument to the provided function

Parameters:
  • dct (dict) – dict which will be stripped

  • func (function) – Get argument names from this function

Returns:

same as input dict but stripped from all keys which are not also an argument to the provided function

Return type:

dict[str, Any]

make_two_sided(func, two_sided_params)[source]#

Create two-sided version of a stimulus function

Where (some) parameters can be specified separately for each side. These parameters should then be specified as a 2-Sequence (2-ple, list of len=2), where entry [0] is the parameter value for left side, and [1] for right side. This means that if the kwarg takes a Sequence itself, e.g., intensities, then the two-sided specification must be, e.g., ((int_left_0, int_left_1), (int_right_0, int_right_1))

Will be left- and right-sided.

Parameters:
  • func (function) – stimulus function to double

  • two_sided_params (Sequence[str]) – names of parameters (kwargs) of func that can be specified separately for each side of the display.

Returns:

two-sided version of stimulus function

Return type:

function