stimgen.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. '''
  3. - Created on 16.12.2012
  4. - author: frank
  5. - purpose: generate stimuli for simulations
  6. '''
  7. from stim_movie import StimulusSet, Filter
  8. import numpy as np
  9. import pylab as plt
  10. def generate_gauss_stim(nx=20, ny=20, ow=20, oh=20, rel_hwb_x=0.1, rel_hwb_y=0.2):
  11. filter = Filter(wshift=1, hshift=1, output_width=ow, output_height=oh)
  12. stimset = StimulusSet()
  13. stimset.filters = [filter]
  14. stim_prototype = gaussian_blob_hbw(ow=ow, oh=oh, rel_hwb_x=rel_hwb_x, rel_hwb_y=rel_hwb_y)
  15. yrolled = stim_prototype
  16. for y in range(ny):
  17. yrolled = np.roll(yrolled, 1, axis=0)
  18. xrolled = yrolled
  19. for x in range(nx):
  20. xrolled = np.roll(xrolled, 1, axis=1)
  21. stimset.add_frame([xrolled])
  22. return stimset
  23. def gaussian_blob_hbw(ow=20, oh=20, rel_hwb_x=0.1, rel_hwb_y=0.2):
  24. sigma = 1.
  25. k = 2 * np.sqrt(2 * np.log(2))
  26. lx = k * sigma / rel_hwb_x
  27. ly = k * sigma / rel_hwb_y
  28. return gaussian_blob(ow, oh, sigma=sigma,
  29. xlim=(-0.5*lx, 0.5*lx),
  30. ylim=(-0.5*ly, 0.5*ly))
  31. def gaussian_blob(nx=20, ny=20, sigma=0.1, xlim=(-1,1), ylim=(-1,1)):
  32. d = dist(nx, ny, xlim=xlim, ylim=ylim)
  33. return gaussian(d, sigma=sigma)
  34. def dist(nx, ny, xlim=(-1,1), ylim=(-1,1)):
  35. xvals = np.linspace(xlim[0], xlim[1],nx)
  36. yvals = np.linspace(ylim[0], ylim[1],ny)
  37. x,y = np.meshgrid(xvals, yvals)
  38. d = np.sqrt(x**2 + y**2)
  39. return d
  40. def gaussian(x, m=0., sigma=0.1):
  41. return np.exp(-(x)**2/(2* (sigma**2)))
  42. def sine_grid(nx, ny, frequency, angle=0., phase=0):
  43. ''' berechnet ein Sinus-Gitter mit nx mal ny Pixeln, Ortsfrequenz frequency, Winkel angle.
  44. angle ist in Radiant anzugeben.
  45. Wenn angle==0, dann geht die Sinus-Welle entlang der X-Achse
  46. Die Ortsfrequenz ist in 1./Pixel angegeben.
  47. Wenn genau eine Welle entlang der X-Achse gewünscht ist, muss angle=0 und frequency = 1./nx gesetzt werden.
  48. Mit dem Parameter phase kann die Phase der Welle verschoben werden.
  49. '''
  50. x_axis = np.linspace(0, nx-1, nx)
  51. y_axis = np.linspace(0, ny-1, ny)
  52. x, y = np.meshgrid(x_axis,y_axis)
  53. x_transformed = x * np.cos(angle) + y * np.sin(angle)
  54. return np.sin(frequency * 2 * np.pi * x_transformed + phase)
  55. def generate_gradient(ow=4, oh=6):
  56. filter = Filter(wshift=1, hshift=1, output_width=ow, output_height=oh)
  57. stimset = StimulusSet()
  58. stimset.filters = [filter]
  59. max_intensity = 1.0
  60. img1 = np.zeros((oh, ow))
  61. img2 = np.zeros((oh, ow))
  62. print img1.shape
  63. row_values = np.linspace(0, 1, oh)
  64. reversed_row = np.array(row_values[::-1])
  65. for x in range(ow):
  66. print (img1[x:]).shape
  67. img1[:,x] = row_values
  68. img2[:,x] = reversed_row
  69. print img1
  70. print img2
  71. stimset.add_frame([img1])
  72. stimset.add_frame([img2])
  73. return stimset
  74. def test_sinegrid():
  75. import pylab as plt
  76. g = sine_grid(100,100, 4./100, 0 * np.pi / 180, 0.5*np.pi)
  77. plt.imshow(g, interpolation='nearest', cmap=plt.cm.gray)
  78. plt.show()
  79. def test_gradient():
  80. import pylab as plt
  81. stim = generate_gradient()
  82. stim.save("/home/frank/prog/objsim/data/movies/gradient.idlmov")
  83. plt.imshow(stim.pics[0], interpolation='nearest', cmap=plt.cm.gray)
  84. plt.show()
  85. if __name__ == '__main__':
  86. test_gradient()