generate_gauss_stim.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Created on 28.07.2012
  4. @author: frank
  5. '''
  6. import numpy as np
  7. from stim_movie import StimulusSet
  8. from objsimpy.stimview import run_stimview
  9. import os
  10. def main():
  11. file_path = "gauss.idlmov"
  12. generate_gauss_stim(file_path,
  13. output_width=20,
  14. output_height=20,
  15. fwhm=0.15, shift_x=1,
  16. shift_y=1,
  17. nx=20,
  18. ny=20,
  19. start_shifted=False,
  20. xy_ratio=1.7)
  21. run_stimview(file_path)
  22. def generate_gauss_stim(file_path="gauss.idlmov", output_width=20, output_height=20, fwhm=0.17,
  23. shift_x=1, shift_y=1, nx=20, ny=20, start_shifted=False, xy_ratio=None):
  24. """Generate a stimulus set of Gaussian blobs at different positions"""
  25. pic_prototype = make_gaussian_2d(output_width, output_height, fwhm, xy_ratio)
  26. if start_shifted:
  27. pic_prototype = np.roll(pic_prototype, 1-output_width/2, axis=1)
  28. pic_prototype = np.roll(pic_prototype, 1-output_height/2, axis=0)
  29. pics = []
  30. for y in range(ny):
  31. y_rolled = np.roll(pic_prototype, y * shift_y, axis=0)
  32. for x in range(nx):
  33. x_rolled = np.roll(y_rolled, x * shift_x, axis=1)
  34. pics.append(x_rolled)
  35. stim_set = StimulusSet(pics=pics)
  36. stim_set.save(file_path)
  37. def make_gaussian_2d(output_width, output_height, fwhm, xy_ratio=None):
  38. """ Make gaussian kernel
  39. parameters:
  40. @param output_width: image width in pixels
  41. @param output_height: image height in pixels
  42. @param fwhm: full-width-at-half_maximum, value between 0 and 1, relative to output_width
  43. @return: numpy array with centered gaussian
  44. """
  45. if xy_ratio is None:
  46. xy_ratio = float(output_width) / float(output_height)
  47. x = np.linspace(-0.5, 0.5, output_width)
  48. y = np.linspace(-0.5/xy_ratio, 0.5/xy_ratio, output_height)
  49. y = y[:,np.newaxis]
  50. sigma = fwhm / (2 * np.sqrt(2*np.log(2)))
  51. return np.exp(-(x**2 + y**2) / (2*sigma*sigma))
  52. def generate_single_stim(file_path="single.idlmov", output_width=20, output_height=20):
  53. pic = np.zeros([output_width, output_height])
  54. pic[int(output_width/2)+4,int(output_height/2)+4] = 1.
  55. stim_set = StimulusSet(pics = [pic])
  56. stim_set.save(file_path)
  57. if __name__ == '__main__':
  58. main()