Removed copyrighted file
[TrainBwister.git] / trainbwister.py
blob7904b2077795ed0ff8227da727b661e2a9d53c22
1 #!/usr/bin/env python
3 ### BEGIN CONFIGURATION SECTION ###
4 # which RESOLUTION?
5 RESOLUTION = (1024,768)
7 # which SIZE of the visible area?
8 SIZE = (600,600)
10 # how many TRIALS per round (MINimum)
11 MINTRIALS = 20
13 # LENGTH of STimulus presentation in seconds
14 ST_LENGTH = 0.5
16 # LENGTH of Break between Trials in seconds
17 TB_LENGTH = 2.5
19 # which N-back initially?
20 N = 1
22 # UPPER ThresHold, above which N is increased
23 UPPERTH = 90
25 # LOWER ThresHold, below which N is decreased
26 LOWERTH = 75
28 # SIZE of FONT
29 FONTSIZE = 20
31 KEYLEFT = "a"
32 KEYRIGHT = "o"
33 SPACE = " "
34 IFDIR = "data/images"
35 SFDIR = "data/sounds"
36 IFS = ['1.png','2.png','3.png','4.png','5.png','6.png','7.png','8.png']
37 SFS = ['1.ogg','2.ogg','3.ogg','4.ogg','5.ogg','6.ogg','7.ogg','8.ogg']
38 BASE = IFDIR+"/"+'base.png'
40 ### END CONFIGURATION SECTION ###
42 TOPLEFT =((RESOLUTION[0]-SIZE[0])/2,(RESOLUTION[1]-SIZE[1])/2)
44 for i in range(0,len(IFS)):
45 IFS[i]=IFDIR+"/"+IFS[i]
46 SFS[i]=SFDIR+"/"+SFS[i]
50 import time, sys, re
51 from random import randint
52 from pygame import display, image, key, Surface, mixer, event, mouse, font
53 from pygame import FULLSCREEN, KEYDOWN
54 from pygame.transform import scale
56 def selftests():
57 print "Running some selftests"
58 die = None
59 # do some preloading to minimize lag blow
60 for f in IFS+SFS+[BASE]:
61 try:
62 open(f,"rb")
63 except IOError, e:
64 die = e
65 print >> sys.stderr, "FATAL:",die
66 if die:
67 raise die
69 if not len(IFS) == len(SFS):
70 print >> sys.stderr, "FATAL: amount of stimuli for different modalities do not match!"
71 sys.exit(1)
72 print "All data present, great!"
73 print
75 class Trial:
76 def __init__(self,imagefile,soundfile,trgtimg,trgtsnd):
77 self.image = scale(image.load(imagefile), SIZE).convert()
78 self.fill = scale(image.load(BASE),SIZE).convert()
79 self.sound = mixer.Sound(soundfile)
80 self.trgtimg = trgtimg
81 self.trgtsnd = trgtsnd
82 self.result = [not(self.trgtimg),not(self.trgtsnd)]
84 def runtrial(self):
85 surface = display.get_surface()
86 surface.fill((255,255,255))
87 surface.blit(self.image,TOPLEFT)
88 display.flip()
89 self.sound.play()
90 time.sleep(ST_LENGTH)
91 surface.blit(self.fill,TOPLEFT)
92 display.flip()
93 time.sleep(TB_LENGTH)
94 keypresses = []
95 for e in event.get(KEYDOWN):
96 keypresses += [e.dict['unicode']]
97 if SPACE in keypresses:
98 return None
99 if unicode(KEYLEFT) in keypresses:
100 if self.trgtimg:
101 #print "user hit key \""+ KEYLEFT +"\" correctly"
102 self.result[0] = True
103 else:
104 #print "user hit key \""+ KEYLEFT +"\" incorrectly"
105 self.result[0] = False
106 if unicode(KEYRIGHT) in keypresses:
107 if self.trgtsnd:
108 #print "user hit key \""+ KEYRIGHT +"\" correctly"
109 self.result[1] = True
110 else:
111 #print "user hit key \""+ KEYRIGHT +"\" incorrectly"
112 self.result[1] = False
113 return True
115 def myrandom(l):
116 result = []
117 for i in range(0,N):
118 result.append(l[randint(0,len(l)-1)])
119 for i in range(0,MINTRIALS):
120 if randint(0,1):
121 result.append(result[-N])
122 else:
123 # be strict about probabilities
124 myl = l[:]
125 myl.pop(result[-N])
126 result.append(myl[randint(0,len(myl)-1)])
127 return result
129 def gentrials():
130 trials = []
131 iis = myrandom(range(0,len(IFS)-1))
132 sis = myrandom(range(0,len(SFS)-1))
133 for i,j,k in zip(iis,sis,range(0,len(iis))):
134 if k < N:
135 trials.append(Trial(IFS[i],SFS[j],False,False))
136 else:
137 nb = k - N
138 trials.append(Trial(IFS[i],SFS[j],iis[k]==iis[nb],sis[k]==sis[nb]))
139 return trials
142 def ask():
143 spam = raw_input(" [Yes/No]? ")
144 if re.match("y(es)?", spam, re.I):
145 return True
146 elif re.match("n(o)?", spam, re.I):
147 return False
149 def main():
150 print "#"*31
151 print "### Welcome to TrainBwister ###"
152 print "####### Version 0.1beta #######"
153 print """Have a look at the sourcecode!
154 Change stuff to suit your needs!
155 The program will hopefully be
156 self explaining. Hafe fun!"""
157 print "#"*31
158 selftests()
159 global N
160 while 1:
161 print "(Hint: while training, you can hit SPACE to abort)"
162 print "Hit '"+KEYLEFT+"' if the",str(N)+". previous image is identical to the one shown"
163 print "Hit '"+KEYRIGHT+"' if the",str(N)+". previous sound is identical to the one heard"
164 while 1:
165 print "Ready to train with N=%i?" %(N),
166 if ask():
167 break
168 else:
169 print "Do you wish to train with N set to a different value? Choosing 'No' exits the program.",
170 if ask():
171 n = int(raw_input("Ok, enter the desired value here: "))
172 while n < 1:
173 print "N must be 1 or higher!"
174 n = int(raw_input("Enter a value higher than 1: "))
175 N = n
176 else:
177 print "bye"
178 sys.exit(1)
180 display.init()
181 display.set_mode(RESOLUTION, FULLSCREEN)
182 font.init()
183 mixer.init(44100)
184 event.set_grab(True)
185 mouse.set_visible(False)
186 trials = gentrials()
187 for trial in trials:
188 if not trial.runtrial():
189 break
190 display.quit()
191 vis = 0.0
192 acu = 0.0
193 for trial in trials:
194 if trial.result[0]:
195 vis+=1
196 if trial.result[1]:
197 acu+=1
198 vp = (vis/(MINTRIALS+N))*100
199 ap = (acu/(MINTRIALS+N))*100
200 message = "percentage in visual modality:%i\npercentage in acoustic modality:%i\n" %(int(vp),int(ap))
201 print message
202 if vp >= UPPERTH and ap >= UPPERTH:
203 N+=1
204 elif (vp < LOWERTH or ap < LOWERTH) and N > 1:
205 N-=1
207 if __name__ == "__main__":
208 main()