Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

gen_movie_seq.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # -*- coding: utf-8 -*-
  2. from objsimpy.int_mod_n import IntModuloN
  3. from random import choice
  4. import pylab
  5. from stim_sequence import StimulusSequence
  6. import numpy as np
  7. class SequenceGenerator_Gauss:
  8. def __init__(self, Nx, Ny, NRepetitions, NoJumpDuration=10, XContinuous=1, XGap=None, YGap=None, n_high_num=2):
  9. self.Nx = Nx
  10. self.Ny = Ny
  11. self.n_high_num = n_high_num
  12. self.n_low_num = 1
  13. self.NRepetitions = NRepetitions
  14. self.NoJumpDuration = NoJumpDuration
  15. self.XContinuous = XContinuous
  16. self.X = IntModuloN(0, self.Nx)
  17. self.Y = IntModuloN(0, self.Ny)
  18. self.StimHistogram = StimHistogram(self.Nx*self.Ny)
  19. self.NoJumpHold = self.NoJumpDuration
  20. self.NStimuli = self.Nx*self.Ny
  21. self.XStimuli = range(self.Nx)
  22. self.YStimuli = range(self.Ny)
  23. self.StimulusSequence=[]
  24. self.Directions = [-1,1]
  25. self.Direction = choice(self.Directions)
  26. self.Stimuli = range(self.NStimuli)
  27. self.StimReservoirPattern = range(self.NStimuli)*self.n_high_num
  28. if not XGap is None:
  29. for i in range(self.n_high_num)[self.n_low_num:]:
  30. for y in self.YStimuli:
  31. for x in range(XGap[0],XGap[1]): self.StimReservoirPattern.remove(y*self.Nx + x)
  32. print "YGap is", YGap
  33. if not YGap is None:
  34. for i in range(self.n_high_num)[self.n_low_num:]:
  35. for stim in range(YGap[0]*self.Nx,YGap[1]*self.Nx):
  36. self.StimReservoirPattern.remove(stim)
  37. self.StimReservoir = self.StimReservoirPattern[:]
  38. self.setRandomStimulus()
  39. def setRandomStimulus(self):
  40. CurStim = choice(self.StimReservoir)
  41. self.CurX = IntModuloN(CurStim % self.Nx, self.Nx)
  42. self.CurY = IntModuloN(CurStim / self.Nx, self.Ny)
  43. if (self.XContinuous):
  44. self.X = choice(self.XStimuli)
  45. else:
  46. self.Y = choice(self.YStimuli)
  47. def generate(self):
  48. RandomJumpDurRange=range(int(0.5*self.NoJumpDuration))
  49. #while self.StimHistogram.getMinRepetitions()<self.NRepetitions :
  50. for i in range(self.NRepetitions):
  51. #print i
  52. self.NoJumpHold-=1
  53. if self.NoJumpHold<1:
  54. self.NoJumpHold=self.NoJumpDuration+choice(RandomJumpDurRange)
  55. self.Direction = choice(self.Directions)
  56. self.setRandomStimulus()
  57. else:
  58. if self.XContinuous == 1:
  59. self.CurX += self.Direction
  60. else:
  61. self.CurY += self.Direction
  62. self.appendStimXY(self.CurX, self.CurY)
  63. print("Sequence generated. Length=", len(self.StimulusSequence))
  64. def appendStimXY(self, X, Y):
  65. CurStim = Y.int()*self.Nx + X.int()
  66. self.StimHistogram.addStimulus(CurStim)
  67. self.StimulusSequence.append(CurStim)
  68. try:
  69. self.StimReservoir.remove(CurStim)
  70. except:
  71. self.StimReservoir += self.StimReservoirPattern
  72. self.StimReservoir.remove(CurStim)
  73. def __repr__(self):
  74. return str(self.StimulusSequence)
  75. def plot(self, x0=0, x1=300):
  76. XSeq = map(lambda x: x%self.Nx, self.StimulusSequence)
  77. YSeq = map(lambda x: x/self.Nx, self.StimulusSequence)
  78. x = range(x0,x1)
  79. pylab.subplot(211) #first subplot
  80. pylab.ylabel('y parameter')
  81. pylab.plot(x, YSeq[x0:x1], '.')
  82. pylab.ylabel("Y Parameter")
  83. pylab.subplot(212) #second subplot
  84. pylab.ylabel('x parameter')
  85. pylab.plot(x, XSeq[x0:x1], '+')
  86. pylab.grid(True)
  87. pylab.ylabel("X Parameter")
  88. pylab.figure(3)
  89. n, bins, patches = pylab.hist(XSeq, self.Nx, normed=1, facecolor='g', alpha=0.75)
  90. print("n=", n, " bins=", bins, " patches=", patches)
  91. pylab.xlabel('x parameter')
  92. pylab.ylabel('Probability')
  93. pylab.title('Histogram of X stimuli')
  94. pylab.axis([0, 19, 0, 0.15])
  95. pylab.grid(True)
  96. pylab.figure(4)
  97. n, bins, patches = pylab.hist(YSeq, self.Ny, normed=1, facecolor='r', alpha=0.75)
  98. print("n=", n, " bins=", bins, " patches=", patches)
  99. pylab.xlabel('y parameter')
  100. pylab.ylabel('Probability')
  101. pylab.title('Histogram of Y stimuli')
  102. pylab.axis([0, 19, 0, 0.15])
  103. pylab.grid(True)
  104. pylab.figure(5)
  105. n, bins, patches = pylab.hist(self.StimReservoirPattern, 100, normed=1, facecolor='r', alpha=0.75)
  106. print("n=", n, " bins=", bins, " patches=", patches)
  107. pylab.show()
  108. class StimHistogram:
  109. def __init__(self, NStimuli):
  110. self.NStimuli = NStimuli
  111. self.Histogram = [0]*self.NStimuli
  112. self.MinRepetitions = 0
  113. self.MinRepIndex=0
  114. def addStimulus(self, StimIndex):
  115. self.Histogram[StimIndex] += 1
  116. if StimIndex==self.MinRepIndex:
  117. self.findMinRepIndex()
  118. def findMinRepIndex(self):
  119. for Index in range(self.MinRepIndex+1, self.NStimuli):
  120. if self.Histogram[Index]<=self.MinRepetitions:
  121. assert self.Histogram[Index]==self.MinRepetitions, "kein Histogram-Wert darf kleiner sein als self.MinRepetitions"
  122. self.MinRepIndex = Index
  123. break
  124. else:
  125. self.MinRepetitions += 1
  126. for Index in range(0, self.NStimuli):
  127. if self.Histogram[Index]<=self.MinRepetitions:
  128. self.MinRepIndex = Index
  129. break
  130. else:
  131. raise "inconsistent histogram data"
  132. def getMinRepetitions(self):
  133. return self.MinRepetitions
  134. def generateMovieSequence_Gauss(Nx, Ny, NRepetitions, NoJumpDuration=10, XContinuous=1, XGap=None, YGap=None, n_high_num=2):
  135. gen = SequenceGenerator_Gauss(Nx, Ny, NRepetitions, NoJumpDuration, XContinuous, XGap=XGap, YGap=YGap, n_high_num=n_high_num)
  136. gen.generate()
  137. Seq = StimulusSequence(gen.StimulusSequence)
  138. SeqFileName = "test_gauss20x20"
  139. SeqFileName += "_nojump"+str(NoJumpDuration)
  140. if (XContinuous==1):
  141. SeqFileName += "_xcont"
  142. else:
  143. SeqFileName += "_ycont"
  144. if (XGap):
  145. print "XGap=", XGap
  146. SeqFileName += "_xgap"
  147. if (YGap):
  148. print "YGap=", YGap
  149. SeqFileName += "_ygap"
  150. SeqFileName += "_high" + str(n_high_num)
  151. SeqFileName += ".sequence"
  152. DirName="/home/frank/prog/objsim/data/movies/"
  153. SeqFileName = DirName + SeqFileName
  154. print("SeqFileName=", SeqFileName)
  155. Seq.tofile(SeqFileName)
  156. gen.plot()
  157. def generateMovieSequence_Diagonal(Nx, Ny, NRepetitions):
  158. max_diagonal_index = min(Nx, Ny)
  159. sequence = []
  160. for val in range(max_diagonal_index):
  161. sequence.append(val*Nx + val)
  162. DirName = "/home/frank/prog/objsim/data/movies/"
  163. FileName = "diagonal.sequence"
  164. path = DirName + FileName
  165. seq = StimulusSequence(sequence)
  166. seq.tofile(path)
  167. seq.show()
  168. class RectStateMachine:
  169. def __init__(self, n_x_elements, n_y_elements, n_x_groups, n_y_groups):
  170. if n_x_elements % n_x_groups != 0:
  171. raise ValueError
  172. if n_y_elements % n_y_groups != 0:
  173. raise ValueError
  174. elements = range(n_x_elements * n_y_elements)
  175. self.groups = []
  176. self.n_x_elements = n_x_elements
  177. self.n_y_elements = n_y_elements
  178. for x_group_nr in range(n_x_groups):
  179. for y_group_nr in range(n_y_groups):
  180. group_start_x = x_group_nr * n_x_elements / n_x_groups
  181. group_stop_x = (x_group_nr + 1) * n_x_elements / n_x_groups
  182. group_start_y = y_group_nr * n_y_elements / n_y_groups
  183. group_stop_y = (y_group_nr + 1) * n_y_elements / n_y_groups
  184. #print "(",group_start_x, ",",group_stop_x, ",",group_start_y, ",",group_stop_y,")"
  185. group = []
  186. for x_element in range(group_start_x, group_stop_x):
  187. #print "x=",x_element
  188. for y_element in range(group_start_y, group_stop_y):
  189. #print "y=",y_element
  190. element_nr = x_element + y_element * n_x_elements
  191. #print element_nr
  192. group.append(element_nr)
  193. elements.remove(element_nr)
  194. self.groups.append(group)
  195. if elements:
  196. raise RuntimeError("elements should be empty now")
  197. self.n_groups = len(self.groups)
  198. self.group_ids = range(self.n_groups)
  199. self.cur_group = choice(self.group_ids)
  200. self.group_ids.remove(self.cur_group)
  201. self.cur_elements = list(self.groups[self.cur_group])
  202. self.stay_in_group_period = 2 * len(self.cur_elements)
  203. self.stay_in_group = self.stay_in_group_period-1
  204. def set_stay_in_group_factor(self, f):
  205. self.stay_in_group_period = f * len(self.cur_elements)
  206. def next_group(self):
  207. if not self.group_ids:
  208. self.group_ids = range(self.n_groups)
  209. self.cur_group = choice(self.group_ids)
  210. self.group_ids.remove(self.cur_group)
  211. return self.cur_group
  212. def next_element(self):
  213. if not self.cur_elements:
  214. self.cur_elements = list(self.groups[self.cur_group])
  215. self.cur_element = choice(self.cur_elements)
  216. self.cur_elements.remove(self.cur_element)
  217. return self.cur_element
  218. def next(self):
  219. if not self.stay_in_group:
  220. self.next_group()
  221. self.stay_in_group = self.stay_in_group_period
  222. self.stay_in_group -= 1
  223. return self.next_element()
  224. def show(self):
  225. fig = pylab.figure()
  226. for group in self.groups:
  227. g = np.array(group)
  228. y = g / self.n_x_elements
  229. x = g % self.n_x_elements
  230. pylab.plot(x,y, '-o')
  231. pylab.show()
  232. def generate_movie_sequence_rectangles(n_x_elements=20, n_y_elements=20, n_x_groups=4, n_y_groups=4, n_repetitions=5):
  233. """ generates a stimulus sequence with groups of rectangles
  234. Stimulus space is a rectangle of numbers.
  235. This rectangle is devided into subrectangles.
  236. """
  237. rect_state = RectStateMachine(n_x_elements, n_y_elements, n_x_groups, n_y_groups)
  238. f = 3
  239. rect_state.set_stay_in_group_factor(f)
  240. sequence = []
  241. n = 20 * n_x_elements * n_y_elements * n_repetitions * f
  242. for i in range(n):
  243. sequence.append(rect_state.next())
  244. DirName = "/home/frank/prog/objsim/data/movies/"
  245. FileName = "rectangle.sequence"
  246. path = DirName + FileName
  247. seq = StimulusSequence(sequence)
  248. seq.tofile(path)
  249. seq.show()
  250. return sequence
  251. if __name__=='__main__':
  252. generateMovieSequence_Gauss(20,20,50000, 20, 1, n_high_num=2)
  253. #generateMovieSequence_Gauss(20,20,50000, 20, 1, YGap=(4,16), n_high_num=2)
  254. #generateMovieSequence_Diagonal(20,20,1)
  255. #sequence = generate_movie_sequence_rectangles(20,20,4,4,1)