/
IronyComprehensionAllInOne.py
executable file
·1066 lines (875 loc) · 44.7 KB
/
IronyComprehensionAllInOne.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy2 Experiment Builder (v1.64.00), May 22, 2011, at 23:42
If you publish work using this script please cite the relevant PsychoPy publications
Peirce, JW (2007) PsychoPy - Psychophysics software in Python. Journal of Neuroscience Methods, 162(1-2), 8-13.
Peirce, JW (2009) Generating stimuli for neuroscience using PsychoPy. Frontiers in Neuroinformatics, 2:10. doi: 10.3389/neuro.11.010.2008
"""
from numpy import *
from numpy.random import *
import os
from psychopy import core, data, event, visual, gui
import psychopy.log
import pyglet
import re
import datetime
import sys
import shutil
import random
# Problem reading french words otherwise (default encoding is ASCII)
reload(sys) # scaring... but necessary :( setdefaultencoding is deleted from sys, by default
sys.setdefaultencoding('utf-8')
# User-defined variables = ['Instructions', 'key_resp', 'text', 'trial']
known_name_collisions = None #(collisions are bad)
# Keyboard layout
keyb = 'IT'
# Debug flag (for testing only)
_debug_on = False
#################################
# General definitions and consts
#################################
class DictKeys():
pass
dk = DictKeys()
dk.expInfoName = 'name'
dk.expInfoAge = 'age'
dk.expInfoSex = 'gender'
dk.expInfoHand = 'hand'
dk.expInfoCondition = 'condition'
dk.expInfoSide = 'side'
dk.expInfoDate = 'date'
dk.expInfoPattern = 'pattern'
dk.expInfoExpName = 'expName'
#------------------
dk.condMalePos = 'MP'
dk.condMaleNeg = 'MN'
dk.condFemalePos = 'FP'
dk.condFemaleNeg = 'FN'
#------------------
dk.sexMale = 'M'
dk.sexFemale = 'F'
#------------------
dk.handRight = 'R'
dk.handLeft = 'L'
#------------------
dk.sideDefault = 'D'
dk.sideInverted = 'I'
#------------------
dk.patternSemanticPicture = 'SIPI'
dk.patternPictureSemantic = 'PISI'
#------------------
dk.condList = [ dk.condMalePos, dk.condMaleNeg, dk.condFemalePos, dk.condFemaleNeg ]
dk.sexList = [ dk.sexMale, dk.sexFemale ]
dk.handList = [ dk.handRight, dk.handLeft ]
dk.sideList = [ dk.sideDefault, dk.sideInverted ]
dk.patternList = [ dk.patternSemanticPicture, dk.patternPictureSemantic ]
class InfoEntry(object):
def __init__(self, label, value, range, position):
self.label = label
self.value = value
self.range = range
self.position = position
def __str__(self):
return str(self.value)
class SubjInfoEntry(InfoEntry):
def __init__(self, label, value, range, position):
super(SubjInfoEntry,self).__init__(label, value, range, position)
class ExpInfoEntry(InfoEntry):
def __init__(self, label, value, range, position):
super(ExpInfoEntry,self).__init__(label, value, range, position)
expInfo= {
dk.expInfoName: SubjInfoEntry('Name','', [],0),
dk.expInfoAge: SubjInfoEntry('Age','',[],1),
dk.expInfoSex: SubjInfoEntry('Gender ' + '/'.join(dk.sexList),'',dk.sexList, 2),
dk.expInfoHand: SubjInfoEntry('Hand ' + '/'.join(dk.handList),'',dk.handList,3),
dk.expInfoCondition: ExpInfoEntry('Condition ' + '/'.join(dk.condList),'',dk.condList, 4),
dk.expInfoSide: ExpInfoEntry('Side ' + '/'.join(dk.sideList),'',dk.sideList,5),
dk.expInfoPattern: ExpInfoEntry('Pattern ' + '/'.join(dk.patternList),'',dk.patternList,6)
}
############
# Utilities
############
def MsgDialog(title, msg):
msg_dlg=gui.Dlg(title)
msg_dlg.addText('')
msg_dlg.addText(msg)
msg_dlg.addText('')
msg_dlg.show()
# need a global files_to_move interrupt_dir
files_to_move = []
interrupt_dir = ''
def quitExpe(msg = None):
global interrupt_dir
global files_to_move
if msg != None:
MsgDialog('Error: experiment aborted!', msg)
if len(files_to_move):
if not os.path.isdir(interrupt_dir):
os.makedirs(interrupt_dir) #if this fails (e.g. permissions) we will get error
for f in files_to_move:
if os.path.isfile(f):
newname = interrupt_dir + os.path.sep + os.path.basename(f)
shutil.move(f, newname)
core.quit()
def screen_size():
s = pyglet.window.get_platform().get_default_display().get_default_screen()
return s.width, s.height
def mouse_in_rect(mouse_pos, rect_pos, rect_size, units = 'norm'):
if units == 'pix':
mouse_pos[0] = mouse_pos[0]*window_size[0]/2
mouse_pos[1] = mouse_pos[1]*window_size[1]/2
if mouse_pos[0] >= rect_pos[0] - rect_size[0]/2 and \
mouse_pos[0] <= rect_pos[0] + rect_size[0]/2 and \
mouse_pos[1] >= rect_pos[1] - rect_size[1]/2 and \
mouse_pos[1] <= rect_pos[1] + rect_size[1]/2:
return True
return False
def mouse_pressed(mouse, mouse_btns=[0,1,2], rects = [], units = 'norm'):
if mouse_btns == None:
mouse_btns = [0,1,2]
buttons, ts = mouse.getPressed(getTime=True)
pos = mouse.getPos()
pressed = [ b for b in mouse_btns if buttons[b] ]
timestp = [ ts[b] for b in pressed ]
inrects = [ i for i in xrange(0, len(rects)) if mouse_in_rect(pos, rects[i][0], rects[i][1], units) ]
rt = -1
if len(pressed):
rt = min(timestp)
return rt, pressed, inrects, timestp
def mouse_release(mouse, clock = None):
delay = None
if clock != None:
delay=clock.getTime()
while sum(mouse.getPressed()):
continue
if clock != None:
delay=clock.getTime()-delay
return delay
def key_pressed(clock, key_list = None):
keys = event.getKeys()
rt = clock.getTime()
if 'escape' in keys:
quitExpe()
if key_list != None:
ret_keys = [ k for k in key_list if k in keys ]
# this is for testing purposes (automatize input, randomly)
if _debug_on and len(key_list):
ret_keys = [ key_list[random.randrange(0, len(key_list))] ]
else:
ret_keys = keys
return rt, ret_keys
def show(win, to_show = [], mouse_btns = [0,1,2], exit_keys = None, buttons = [], delay = 100000, clickReset = True):
if mouse_btns == None:
mouse_btns = [0,1,2]
use_mouse = False
use_keybd = True # this is always true to capture the ESC key
if len(mouse_btns):
use_mouse = True
mouse = event.Mouse(win=win)
rects = [ button.rect() for r in buttons ]
if len(rects):
mouse.setVisible(True)
rt,b,k,re = -1,None,None,None
event.clearEvents()
if use_keybd:
event.getKeys()
if use_mouse:
mouse_release(mouse)
clock_show=core.Clock()
if use_mouse and clickReset:
mouse.clickReset()
t=0; clock_show.reset()
while True:
rt = t= clock_show.getTime()
if t >= delay:
break
if (0.0 <= t):
for t in to_show:
t.draw()
for b in buttons:
b.draw()
if use_mouse:
rt, bt, re, ts = mouse_pressed(mouse, mouse_btns, rects = rects)
if len(bt) and (len(rects) == 0 or len(re)):
break
if use_keybd:
rt, k = key_pressed(clock_show, exit_keys)
if len(k):
break
win.flip()
if use_mouse and len(rects):
mouse.setVisible(False)
return rt,b,k,re
##################
# Start Form mask
##################
class Stats(object):
def __stat_init(self, keys):
dict = {}
for x in keys: dict[x] = 0
return dict
def __init__(self, dir):
self.dir = dir
self.sex = self.__stat_init(dk.sexList)
self.condition = self.__stat_init(dk.condList)
self.side = self.__stat_init(dk.sideList)
self.hand = self.__stat_init(dk.handList)
self.pattern = self.__stat_init(dk.patternList)
def make(self):
re_stat = re.compile("(?P<SEX>.)_(?P<CND>..)_(?P<PATTERN>....)_(?P<SIDE>.)_(?P<HAND>.).*xlsx$")
flist = os.listdir(self.dir)
for f in flist:
m = re_stat.match(os.path.basename(f))
if m == None: continue
sex = m.group('SEX')
cnd = m.group('CND')
side = m.group('SIDE')
hand = m.group('HAND')
pattern = m.group('PATTERN')
if not (sex in dk.sexList and cnd in dk.condList and side in dk.sideList and hand in dk.handList and pattern in dk.patternList):
continue
self.sex[sex] = self.sex[sex] + 1
self.condition[cnd] = self.condition[cnd] + 1
self.side[side] = self.side[side] + 1
self.hand[hand] = self.hand[hand] + 1
self.pattern[pattern] = self.pattern[pattern] + 1
def expInfoByType(expInfo, otype, sorted = True):
map = {}
for x in expInfo.values():
if type(x) == otype:
map[x.position] = x
if sorted:
sorted_keys = map.keys()
sorted_keys.sort()
return [ map[k] for k in sorted_keys ]
return map.values()
def makeDlg(expInfo, stats):
dlg = gui.Dlg(title="Irony Comprehension")
dlg.addText('_____________ STATISTICS INFO _____________')
dlg.addText('Total subjects tested = %d' %(stats.sex[dk.sexMale] + stats.sex[dk.sexFemale]))
cnds = [ '%s=%d' %(k, v) for k,v in stats.condition.items() ]
dlg.addText('Condition '+', '.join(cnds))
sides = [ '%s=%d' %(k, v) for k,v in stats.side.items() ]
dlg.addText('Side '+', '.join(sides))
sexs = [ '%s=%d' %(k, v) for k,v in stats.sex.items() ]
dlg.addText('Sex '+', '.join(sexs))
hands = [ '%s=%d' %(k, v) for k,v in stats.hand.items() ]
dlg.addText('Hand '+', '.join(hands))
patterns = [ '%s=%d' %(k, v) for k,v in stats.pattern.items() ]
dlg.addText('Patterns '+', '.join(patterns))
dlg.addText('______________ SUBJECT INFO ______________')
for x in expInfoByType(expInfo, SubjInfoEntry):
dlg.addField(x.label, x.value)
dlg.addText('____________ EXPERIMENT INFO ____________')
for x in expInfoByType(expInfo, ExpInfoEntry):
dlg.addField(x.label, x.value)
return dlg
def expInfoFromDlg(dlg, expInfo):
for k,x in expInfo.items():
if type(x) in [SubjInfoEntry, ExpInfoEntry]:
x.value = dlg.data[x.position].upper().strip(' ')
def expInfoOk(expInfo):
for k, x in expInfo.items():
if not type(x) in [SubjInfoEntry, ExpInfoEntry]:
continue
if len(x.range) and x.value not in x.range:
return False
age = int(expInfo[dk.expInfoAge].value)
if age <= 1 or age >= 100:
return False
return True
def OpeningForm(expInfo, statistics):
quit_loop = False
while not quit_loop:
dlg = makeDlg(expInfo, statistics)
dlg.show()
if dlg.OK==False:
quitExpe()
try:
expInfoFromDlg(dlg, expInfo)
if expInfoOk(expInfo):
quit_loop = True
break
MsgDialog('Attention', 'Some of the parameters are missing or not within the correct range...')
except Exception, e:
print str(e)
MsgDialog('Attention', 'Some of the parameters are missing or not within the correct range...')
class RandomPermutation(object):
def __init__(self, n, auto_reset = True):
self.reset(n)
self._auto_reset = auto_reset
def reset(self, n):
self._info = [0]*n
self._size = n
self._free = n
def select(self):
if self._free == 0:
if self._auto_reset:
self.reset(self._size)
else:
return -1
p = random.randrange(0, self._free) + 1
for i in xrange(0, self._size):
if self._info[i] == 0:
p -= 1
if p == 0:
self._info[i] = 1
self._free -= 1
break
return i
def CheckStoryListBalancement(list, check_marked):
male = [ x for x in list if x['Gender'] == 'M' ]
female = [ x for x in list if x['Gender'] == 'F']
if len(male) == 0 or len(male) != len(female) or (len(male) % 2):
quitExpe('Story list not balanced by gender')
male_positive_ctx = [ x for x in male if x['Context'] == 'P' ]
male_negative_ctx = [ x for x in male if x['Context'] == 'N' ]
if len(male_positive_ctx) == 0 or len(male_positive_ctx) != len(male_negative_ctx) or (len(male_positive_ctx) % 2):
quitExpe('Male story list not balanced by context')
female_positive_ctx = [ x for x in female if x['Context'] == 'P' ]
female_negative_ctx = [ x for x in female if x['Context'] == 'N' ]
if len(female_positive_ctx) == 0 or len(female_positive_ctx) != len(female_negative_ctx) or (len(female_positive_ctx) % 2):
quitExpe('Female story list not balanced by context')
# if len(male_positive_ctx) % 2 or len(female_positive_ctx) % 2:
# quitExpe('Story list cannot be balanced by target')
if check_marked:
male_positive_ctx_marked = [ x for x in male_positive_ctx if x['Mark'] == 1 ]
female_positive_ctx_marked = [ x for x in female_positive_ctx if x['Mark'] == 1 ]
if len(male_positive_ctx_marked) < len(male_positive_ctx)/2 or \
len(female_positive_ctx_marked) < len(female_positive_ctx)/2:
quitExpe('Marked story list is not big enough')
def test_stories_split_by_context(stories_G, stories_test_list): # prm: list of stories indexes by genders
stories_G_dict = {}
stories_G_CP = [ i for i in stories_G if stories_test_list[i]['Context'] == 'P' ]
stories_G_CP_Marked = [ i for i in stories_G_CP if stories_test_list[i]['Mark'] == 1 ]
stories_G_CP_Normal = [ i for i in stories_G_CP if stories_test_list[i]['Mark'] == 0 ]
rp_gcpm = RandomPermutation(len(stories_G_CP_Marked))
rp_w = RandomPermutation(2)
stories_G_selection = [[], []]
context_ids_selection = [[], []]
which = rp_w.select()
for i in xrange(0, len(stories_G_CP_Marked)):
if i == len(stories_G_CP_Marked)/2:
which = rp_w.select()
k = stories_G_CP_Marked[rp_gcpm.select()]
stories_G_selection[which].append(k)
context_ids_selection[which].append(stories_test_list[k]['Context_Id'])
rp_gcpn = RandomPermutation(len(stories_G_CP_Normal))
for i in xrange(0, len(stories_G_selection)):
len_selection = len(stories_G_selection[i])
for j in xrange(0, len(stories_G_CP)/2 - len_selection):
k = stories_G_CP_Normal[rp_gcpn.select()]
stories_G_selection[i].append(k)
context_ids_selection[i].append(stories_test_list[k]['Context_Id'])
stories_G_CN = [ i for i in stories_G if stories_test_list[i]['Context'] == 'N']
rp_gcn = RandomPermutation(len(stories_G_CN))
j = 0
for i in xrange(0, len(stories_G_CN)):
k = stories_G_CN[rp_gcn.select()]
c = stories_test_list[k]['Context_Id']
if c in context_ids_selection[j]:
j = (j + 1)%2
if len(stories_G_selection[j]) < len(stories_G)/2:
stories_G_selection[j].append(k)
j = (j + 1)%2
return stories_G_selection
def practice_stories_split_by_context(stories_G, stories_practice_list): # prm: list of stories indexes by genders
stories_G_CP = [ i for i in stories_G if stories_practice_list[i]['Context'] == 'P' ]
stories_G_CN = [ i for i in stories_G if stories_practice_list[i]['Context'] == 'N' ]
stories_G_selection = [[],[]]
context_ids_selection = [[],[]]
rp = RandomPermutation(len(stories_G_CP))
for j in [0, 1]:
for i in xrange(0, len(stories_G_CP)/2):
k = stories_G_CP[rp.select()]
stories_G_selection[j].append(k)
context_ids_selection[j].append(stories_practice_list[k]['Context_Id'])
j = 0
rp = RandomPermutation(len(stories_G_CN))
for i in xrange(0, len(stories_G_CN)):
k = stories_G_CN[rp.select()]
c = stories_practice_list[k]['Context_Id']
if c in context_ids_selection[j]:
j = (j + 1)%2
if len(stories_G_selection[j]) < len(stories_G)/2:
stories_G_selection[j].append(k)
j = (j + 1)%2
return stories_G_selection
#############
# Expe setup
#############
expName ='IronyComprehension'#from the Builder filename that created this script
expInfo[dk.expInfoDate] = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
expInfo[dk.expInfoExpName] = expName
data_dir = 'data'
interrupt_dir = data_dir + os.path.sep + 'interrupted'
stimuli_dir = 'stimuli'+ os.path.sep
instruction_dir = 'instructions'+ os.path.sep
pictures_dir = stimuli_dir + 'pictures' + os.path.sep
my_font = 'Comic Sans MS'
if not os.path.isdir(data_dir):
os.makedirs(data_dir)
# Opening mask
statistics = Stats(data_dir)
statistics.make()
OpeningForm(expInfo, statistics)
# set the experiment strings info dictionary
expInfoStr = {}
for k, v in expInfo.items(): expInfoStr[k] = str(v)
# Get the stimuli list
cond_dict = {}
cond_dict['gender'] = expInfo[dk.expInfoCondition].value in [ dk.condMalePos, dk.condMaleNeg ] and 'M' or 'F'
cond_dict['type'] = expInfo[dk.expInfoCondition].value in [ dk.condMalePos, dk.condFemalePos ] and 'P' or 'N'
adjectives_table=stimuli_dir + 'Adjectives.csv'
adjectives_list =data.importConditions(adjectives_table)
adjectives_cond_list = [x for x in adjectives_list if x['Gender'] == cond_dict['gender'] and (x['Type'] == cond_dict['type'] or x['Type'] == 'X')]
faces_table=stimuli_dir + 'Faces.csv'
faces_list =data.importConditions(faces_table)
faces_cond_list = [x for x in faces_list if x['Gender'] == cond_dict['gender'] and (x['Type'] == cond_dict['type'] or x['Type'] == 'X')]
stories_table=stimuli_dir + 'Stories.csv'
stories_list =data.importConditions(stories_table)
stories_practice_list = [ x for x in stories_list if x['SentenceBlock'] == 'P']
stories_test_list = [ x for x in stories_list if x['SentenceBlock'] == 'T']
fillers_table= stimuli_dir + 'Fillers.csv'
fillers_list = data.importConditions(fillers_table)
fillers_practice_list = [ x for x in fillers_list if x['FillerBlock'] == 'P']
fillers_test_list = [ x for x in fillers_list if x['FillerBlock'] == 'T']
# Split the stories in 2 balanced sets
CheckStoryListBalancement(stories_practice_list, False)
CheckStoryListBalancement(stories_test_list, True)
stories_M = [ i for i in xrange(0, len(stories_test_list)) if stories_test_list[i]['Gender'] == 'M' ]
stories_F = [ i for i in xrange(0, len(stories_test_list)) if stories_test_list[i]['Gender'] == 'F' ]
stories_M_first, stories_M_second = test_stories_split_by_context(stories_M, stories_test_list)
stories_F_first, stories_F_second = test_stories_split_by_context(stories_F, stories_test_list)
stories_first_test_list = [ stories_test_list[i] for i in stories_M_first + stories_F_first ]
stories_second_test_list = [ stories_test_list[i] for i in stories_M_second + stories_F_second ]
stories_M = [ i for i in xrange(0, len(stories_practice_list)) if stories_practice_list[i]['Gender'] == 'M' ]
stories_F = [ i for i in xrange(0, len(stories_practice_list)) if stories_practice_list[i]['Gender'] == 'F' ]
stories_M_first, stories_M_second = practice_stories_split_by_context(stories_M, stories_practice_list)
stories_F_first, stories_F_second = practice_stories_split_by_context(stories_F, stories_practice_list)
stories_first_practice_list = [ stories_practice_list[i] for i in stories_M_first + stories_F_first ]
stories_second_practice_list = [ stories_practice_list[i] for i in stories_M_second + stories_F_second ]
# Split the fillers
rp = RandomPermutation(len(fillers_test_list)/2)
permutation = [ rp.select() for i in xrange(0, len(fillers_test_list)/2) ]
fillers_first_test_list = [ fillers_test_list[i] for i in permutation ]
fillers_second_test_list = [ fillers_test_list[j] for j in xrange(0, len(fillers_test_list)) if j not in permutation ]
# Keyboard input
choice_keys_dict = {}
if keyb == 'US':
choice_keys_dict['screen'] = keys_screen = { 'left': 'a', 'right': '\\' }
choice_keys_dict['input'] = { 'a': 'a', 'backslash': '\\' }
elif keyb == 'IT':
choice_keys_dict['screen'] = keys_screen = { 'left': 'q', 'right': '+' }
choice_keys_dict['input'] = { 'q': 'q', 'plus': '+' }
else:
msgDialog('Keyboard layout not supported')
quitExpe()
# File names
file_name = data_dir + os.path.sep + '%s_%s_%s_%s_%s_%s_%s' %(expInfo[dk.expInfoSex], expInfo[dk.expInfoCondition],expInfo[dk.expInfoPattern],expInfo[dk.expInfoSide], expInfo[dk.expInfoHand], expInfo[dk.expInfoExpName], expInfo[dk.expInfoDate])
logfile_name = file_name+'.log'
excel_file = file_name+'.xlsx'
psychopy.log.console.setLevel(psychopy.log.warning)
log_file = psychopy.log.LogFile(logfile_name, level=psychopy.log.EXP, encoding='utf8')
#files_to_move.append(logfile_name)
files_to_move.append(excel_file)
# setup the Window
window_size=screen_size()
win = visual.Window(size=window_size, fullscr=True, screen=0, allowGUI=False, allowStencil=False, color='Black', colorSpace='rgb')
# Set up instructions
text_elem = {
'begin':
{ 'fname': 'instruction_begin.txt', 'font': my_font, 'pos': (0, 0), 'height': 0.05, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.2 },
'begin_sp':
{ 'fname': 'instruction_sp_begin.txt', 'font': my_font, 'pos': (0, 0), 'height': 0.05, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.2 },
'begin_pp':
{ 'fname': 'instruction_pp_begin.txt', 'font': my_font, 'pos': (0, 0), 'height': 0.05, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.2 },
'begin_ic':
{ 'fname': 'instruction_ic_begin.txt', 'font': my_font, 'pos': (0, 0), 'height': 0.05, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.2 },
'end':
{ 'fname': 'instruction_end.txt', 'font': my_font, 'pos': (0, 0), 'height': 0.08, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1 },
'generic':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.8), 'height': 0.05, 'color': 'yellow', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 2 },
'attention_getter':
{ 'fname': None, 'text':'+', 'font': 'Arial', 'pos': (0, 0), 'height': 0.25, 'color': 'red', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 2 },
'adjective':
{ 'fname': None, 'font': 'Arial', 'pos': (0, 0.4), 'height': 0.2, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 2 },
'choice_adjective':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.3), 'height': 0.07, 'color': 'yellow', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.5 },
'choice_face':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.7), 'height': 0.07, 'color': 'yellow', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.5 },
'prime':
{ 'fname': None, 'font': my_font, 'pos': (0, 0.2), 'height': 0.08, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.5 },
'target':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.1), 'height': 0.08, 'color': 'red', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1 },
'choice_prime':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.6), 'height': 0.07, 'color': 'yellow', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 2 },
'question':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.3), 'height': 0.07, 'color': 'yellow', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.2 },
'filler':
{ 'fname': None, 'font': my_font, 'pos': (0, 0.2), 'height': 0.08, 'color': 'white', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 1.5 },
'choice_filler':
{ 'fname': None, 'font': my_font, 'pos': (0, -0.5), 'height': 0.07, 'color': 'yellow', 'HAlign':'center', 'VAlign':'center', 'wrapWidth': 2 },
}
for e in text_elem:
t = text_elem[e]
if t['fname'] == None:
if not ('text' in t):
t['text'] = None
else:
instruction_file = open(instruction_dir + t['fname'], 'r')
t['text'] = instruction_file.read()
instruction_file.close()
t['obj'] = visual.TextStim(win=win, ori=0, text=t['text'],
alignHoriz=t['HAlign'], alignVert=t['VAlign'],
font=t['font'], pos=t['pos'], height=t['height'],
color=t['color'], colorSpace=u'rgb', wrapWidth=t['wrapWidth'])
patch_elem = {
'face':
{ 'dir': pictures_dir, 'tex': None, 'pos': (0,100), 'size': (506, 650), 'units':'pix', 'mask': None }
}
for e in patch_elem:
p = patch_elem[e]
p['obj'] = visual.PatchStim(win=win, tex=None, mask=u'None',
ori=0, pos=[0, 100], size=[506, 650], units='pix', sf=None, phase=0.0,
color=[1,1,1], colorSpace=u'rgb',
texRes=128, interpolate=False)
###################
# SEMANTIC PRIMING
###################
def semantic_task(adjectives, text_elem, choice_keys_dict, sheet_name, extra_info, side_info_dict, data_file, append):
adjectives_len = len(adjectives)
adjectives_permutation = RandomPermutation(adjectives_len)
trial_clock = core.Clock()
trials_list = [ { 'Trial': '%d' %(i+1) } for i in xrange(0, 2*adjectives_len) ]
trials = data.TrialHandler(trialList = trials_list, nReps=1, method=u'sequential', extraInfo=extra_info)
trial_count = 0
for thisTrial in trials:
which_adjective = adjectives_permutation.select()
adjective_info = adjectives[which_adjective]
text_elem['adjective']['obj'].setText(adjective_info['Adjective'])
#run the trial
keys_to_press = choice_keys_dict['input'].keys()
timestamps = []
register_time = False
continue_trial=True
event.clearEvents()
t=0; trial_clock.reset()
while continue_trial and (t<1000000.0000):
t = trial_clock.getTime()
rt_k, keys = key_pressed(trial_clock, keys_to_press)
if t >= 0:
if len(keys) == 0:
text_elem['adjective']['obj'].draw()
text_elem['choice_adjective']['obj'].draw()
register_time = True
elif len(timestamps) > 0:
rt_adjective = rt_k - timestamps[0]
key_adjective = choice_keys_dict['input'][keys[0]]
continue_trial = False
if trial_count < 2*adjectives_len - 1:
text_elem['choice_adjective']['obj'].draw()
win.flip()
if register_time:
timestamps.append(trial_clock.getTime())
register_time = False
for k,v in adjective_info.items():
trials.addData(k, v)
trials.addData('Key', key_adjective)
trials.addData('Response', side_info_dict['CHOICE'][key_adjective])
trials.addData('RT', rt_adjective)
trial_count += 1
core.wait(0.5)
trials.saveAsExcel(data_file, sheetName = sheet_name,
stimOut=[],
dataOut=['all_raw'],
appendFile = append)
###################
# PICTURE PRIMING
###################
def picture_task(pictures, text_elem, patch_elem, choice_keys_dict, sheet_name, extra_info, side_info_dict, data_file, append):
pictures_len = len(pictures)
pictures_permutation = RandomPermutation(pictures_len)
trial_clock = core.Clock()
trials_list = [ { 'Trial': '%d' %(i+1) } for i in xrange(0, 2*pictures_len) ]
trials = data.TrialHandler(trialList = trials_list, nReps=1, method=u'sequential', extraInfo=extra_info)
trial_count = 0
for thisTrial in trials:
which_picture = pictures_permutation.select()
picture_info = pictures[which_picture]
patch_elem['face']['obj'].setTex(patch_elem['face']['dir'] + picture_info['Face'])
#run the trial
keys_to_press = choice_keys_dict['input'].keys()
timestamps = []
register_time = False
continue_trial=True
event.clearEvents()
t=0; trial_clock.reset()
while continue_trial and (t<1000000.0000):
t = trial_clock.getTime()
rt_k, keys = key_pressed(trial_clock, keys_to_press)
if t >= 0:
if len(keys) == 0:
patch_elem['face']['obj'].draw()
text_elem['choice_face']['obj'].draw()
register_time = True
elif len(timestamps) > 0:
rt_picture = rt_k - timestamps[0]
key_picture = choice_keys_dict['input'][keys[0]]
continue_trial = False
if trial_count < 2*pictures_len - 1:
text_elem['choice_face']['obj'].draw()
win.flip()
if register_time:
timestamps.append(trial_clock.getTime())
register_time = False
for k,v in picture_info.items():
trials.addData(k, v)
trials.addData('Key', key_picture)
trials.addData('Response', side_info_dict['CHOICE'][key_picture])
trials.addData('RT', rt_picture)
trial_count += 1
core.wait(0.5)
trials.saveAsExcel(data_file, sheetName = sheet_name,
stimOut=[],
dataOut=['all_raw'],
appendFile = append)
#############
# IRONY TASK
#############
def irony_task(practice, stories, fillers, text_elem, choice_keys_dict, sheet_name, extra_info, side_info_dict, msg_at_last, data_file, append):
def marked_story_balance(stories, stories_G_C, targets):
stories_G_C_Marked_Neg = [ i for i in stories_G_C if stories[i]['Mark'] == 1 and targets[i] == 0 ]
stories_G_C_Marked_Pos = [ i for i in stories_G_C if stories[i]['Mark'] == 1 and targets[i] == 1 ]
stories_G_C_Normal_Neg = [ i for i in stories_G_C if stories[i]['Mark'] == 0 and targets[i] == 0 ]
n = len(stories_G_C)/2 - len(stories_G_C_Marked_Neg)
if n <= 0:
return
rp_mp = RandomPermutation(len(stories_G_C_Marked_Pos))
rp_nn = RandomPermutation(len(stories_G_C_Normal_Neg))
while n > 0:
targets[stories_G_C_Marked_Pos[rp_mp.select()]] = 0
targets[stories_G_C_Normal_Neg[rp_nn.select()]] = 1
n -= 1
def randomize_targets(stories, practice):
targets = [-1] * len(stories)
for gnd in ['M','F']:
for ctx in ['P','N']:
stories_G_C = [ i for i in xrange(0, len(stories)) if stories[i]['Gender'] == gnd and stories[i]['Context'] == ctx ]
rp = RandomPermutation(len(stories_G_C))
for i in xrange(0, len(stories_G_C)):
targets[stories_G_C[rp.select()]] = i % 2 # 0 == negative target
if not practice and ctx == 'P':
marked_story_balance(stories, stories_G_C, targets)
return targets
stories_len = len(stories)
fillers_len = len(fillers)
rp = RandomPermutation(stories_len)
stories_permutation = [ rp.select() for x in stories ]
targets_permutation = randomize_targets(stories, practice)
rp = RandomPermutation(fillers_len)
fillers_permutation = [ rp.select() for x in fillers ]
trial_clock = core.Clock()
trials_list = [ { 'Trial': '%d' %(i+1) } for i in xrange(0, stories_len) ]
trials = data.TrialHandler(trialList = trials_list, nReps=1, method=u'sequential', extraInfo=extra_info)
trial_count = 0
for thisTrial in trials:
which_story = stories_permutation[trial_count]
story_info = stories[which_story]
target_word = targets_permutation[stories_permutation[trial_count]] == 0 and 'TW_Negative' or 'TW_Positive'
which_filler = fillers_permutation[trial_count]
filler_info = fillers[which_filler]
text_elem['prime']['obj'].setText(story_info['Sentence'])
text_elem['target']['obj'].setText(story_info[target_word])
text_elem['filler']['obj'].setText(filler_info['Filler'])
text_elem['generic']['obj'].setText(u'Appuyer sur la barre « espace » pour lire le dernier mot...')
text_elem['generic']['obj'].setPos((0,-0.4))
text_elem['generic']['obj'].setHeight(0.06)
show(win, [ text_elem['attention_getter']['obj'] ], exit_keys = [], mouse_btns = [], delay = 0.5)
timestamps = []
register_time = False
keys_to_press = ['space']
event.clearEvents()
continue_trial = True
t = 0; step = 0; trial_clock.reset()
while continue_trial and (t < 1000000.0000):
t = trial_clock.getTime()
rt_k, keys = key_pressed(trial_clock, keys_to_press)
if t >= 0:
if step == 0:
if len(keys) == 0:
text_elem['prime']['obj'].draw()
text_elem['generic']['obj'].draw()
else:
keys_to_press = choice_keys_dict['input'].keys()
register_time = True
step = 1
if step == 1:
if len(timestamps) == 0 or t < timestamps[0] + 1.0:
text_elem['prime']['obj'].draw()
text_elem['target']['obj'].draw()
else:
keys = []
event.getKeys()
register_time = True
step = 2
if step == 2:
if len(keys) == 0:
#text_elem['prime']['obj'].draw()
text_elem['question']['obj'].draw()
text_elem['choice_prime']['obj'].draw()
elif len(timestamps) == 2:
rt_prime = rt_k - timestamps[1]
key_prime = choice_keys_dict['input'][keys[0]]
keys = []
event.getKeys()
register_time = True
step = 3
if step == 3:
if len(keys) == 0:
text_elem['filler']['obj'].draw()
text_elem['choice_filler']['obj'].draw()
elif len(timestamps) == 3:
rt_filler = rt_k - timestamps[2]
key_filler = choice_keys_dict['input'][keys[0]]
continue_trial = False
win.flip()
# time registration right after flipping
if register_time:
timestamps.append(trial_clock.getTime())
register_time = False
for k,v in story_info.items():
trials.addData(k, v)
trials.addData('TW_Type', target_word)
trials.addData('TW_Value', story_info[target_word])
trials.addData('RT_Prime', rt_prime)
trials.addData('Key_Prime', key_prime)
trials.addData('Response_Prime', side_info_dict['CHOICE_PRIME'][key_prime])
for k,v in filler_info.items():
trials.addData(k, v)
trials.addData('RT_Filler', rt_filler)
trials.addData('Key_Filler', key_filler)
trials.addData('Response_Filler', side_info_dict['CHOICE_FILLER'][key_filler])
trial_count += 1
'''
if trial_count < stories_len or msg_at_last:
text_elem['generic']['obj'].setPos((0,0))
text_elem['generic']['obj'].setHeight(0.07)
text_elem['generic']['obj'].setText(u'Appuyer sur la barre « espace » pour poursuivre...')
show(win, [ text_elem['generic']['obj'] ], exit_keys = ['space'], mouse_btns = [], delay = 100000)
'''
trials.saveAsExcel(data_file, sheetName = sheet_name,
stimOut=[],
dataOut=['all_raw'],
appendFile = append)
######################
# Experiment segments
######################
# This is just a way to arrange the pieces of the expe: no parameters passed to the functions, only locals and globals
def general_instruction():
text_elem['generic']['obj'].setText(u'Appuyer sur la barre « espace » pour commencer...')
text_elem['generic']['obj'].setPos((0, -0.35))
show(win, [ text_elem['begin']['obj'], text_elem['generic']['obj']], exit_keys = ['space'], mouse_btns = [], delay = 100000)
def semantic_priming():
side_sp_dict = {}
side_sp_dict['FM'] = cond_dict['gender'] == 'M' and u'Masculine' or u'Féminin'
side_sp_dict['SIDE_FM'] = expInfo[dk.expInfoSide].value == dk.sideDefault and keys_screen['left'] or keys_screen['right']
side_sp_dict['SIDE_NEUTRAL'] = expInfo[dk.expInfoSide].value == dk.sideDefault and keys_screen['right'] or keys_screen['left']
side_sp_dict['CHOICE'] = side_sp_choice_dict = {}
side_sp_choice_dict[keys_screen['left']] = expInfo[dk.expInfoSide].value == dk.sideDefault and side_sp_dict['FM'] or 'Neutre'
side_sp_choice_dict[keys_screen['right']] = expInfo[dk.expInfoSide].value == dk.sideDefault and 'Neutre' or side_sp_dict['FM']
text_elem['generic']['obj'].setText(u'Appuyer sur la barre « espace » pour commencer...')
text_elem['generic']['obj'].setPos((0, -0.5))
text_elem['generic']['obj'].setHeight(0.05)
text_elem['begin_sp']['obj'].setText(text_elem['begin_sp']['text'] %side_sp_dict)
text_elem['choice_adjective']['obj'].setText('[ %s ] <- %s %s -> [ %s ]' %(keys_screen['left'], side_sp_choice_dict[keys_screen['left']], side_sp_choice_dict[keys_screen['right']], keys_screen['right']))
show(win, [ text_elem['begin_sp']['obj'], text_elem['generic']['obj']], exit_keys = ['space'], mouse_btns = [], delay = 100000)
semantic_task(adjectives_cond_list, text_elem, choice_keys_dict, 'Semantic Priming', expInfoStr, side_sp_dict, data_file = excel_file, append = True)
text_elem['generic']['obj'].setText(u"Vous avez bien terminé cette partie de l'expérience !\nAppuyer sur la barre « espace » pour passer à la suivante...")
text_elem['generic']['obj'].setPos((0, 0))
text_elem['generic']['obj'].setHeight(0.07)
show(win, [ text_elem['generic']['obj']], exit_keys = ['space'], mouse_btns = [], delay = 100000)
def picture_priming():
side_pp_dict = {}
side_pp_dict['PN'] = cond_dict['type'] == 'P' and u'positif' or u'negatif'
side_pp_dict['SIDE_PN'] = expInfo[dk.expInfoSide].value == dk.sideDefault and keys_screen['left'] or keys_screen['right']
side_pp_dict['SIDE_NEUTRAL'] = expInfo[dk.expInfoSide].value == dk.sideDefault and keys_screen['right'] or keys_screen['left']
side_pp_dict['CHOICE'] = side_pp_choice_dict = {}
side_pp_choice_dict[keys_screen['left']] = expInfo[dk.expInfoSide].value == dk.sideDefault and side_pp_dict['PN'] or 'neutre'
side_pp_choice_dict[keys_screen['right']] = expInfo[dk.expInfoSide].value == dk.sideDefault and 'neutre' or side_pp_dict['PN']
text_elem['generic']['obj'].setText(u'Appuyer sur la barre « espace » pour commencer...')
text_elem['generic']['obj'].setPos((0, -0.5))
text_elem['generic']['obj'].setHeight(0.05)
text_elem['begin_pp']['obj'].setText(text_elem['begin_pp']['text'] %side_pp_dict)
text_elem['choice_face']['obj'].setText('[ %s ] <- %s %s -> [ %s ]' %(keys_screen['left'], side_pp_choice_dict[keys_screen['left']], side_pp_choice_dict[keys_screen['right']], keys_screen['right']))
show(win, [ text_elem['begin_pp']['obj'], text_elem['generic']['obj']], exit_keys = ['space'], mouse_btns = [], delay = 100000)
picture_task(faces_cond_list, text_elem, patch_elem, choice_keys_dict, 'Picture Priming', expInfoStr, side_pp_dict, data_file = excel_file, append = True)
text_elem['generic']['obj'].setText(u"Vous avez bien terminé cette partie de l'expérience !\nAppuyer sur la barre « espace » pour passer à la suivante...")
text_elem['generic']['obj'].setPos((0, 0))
text_elem['generic']['obj'].setHeight(0.07)
show(win, [ text_elem['generic']['obj']], exit_keys = ['space'], mouse_btns = [], delay = 100000)
side_ic_dict = {}
def irony_comprehension_first():
global side_ic_dict
side_ic_dict['SIDE_POSITIVE'] = expInfo[dk.expInfoSide].value == dk.sideDefault and keys_screen['left'] or keys_screen['right']
side_ic_dict['SIDE_NEGATIVE'] = expInfo[dk.expInfoSide].value == dk.sideDefault and keys_screen['right'] or keys_screen['left']
side_ic_dict['CHOICE_PRIME'] = side_ic_choice_prime_dict = {}
side_ic_choice_prime_dict[keys_screen['left']] = expInfo[dk.expInfoSide].value == dk.sideDefault and u'Gentil' or u'Méchant'
side_ic_choice_prime_dict[keys_screen['right']] = expInfo[dk.expInfoSide].value == dk.sideDefault and u'Méchant' or u'Gentil'
side_ic_dict['CHOICE_FILLER'] = side_ic_choice_filler_dict = {}
side_ic_choice_filler_dict[keys_screen['left']] = expInfo[dk.expInfoSide].value == dk.sideDefault and u'Oui' or u'Non'
side_ic_choice_filler_dict[keys_screen['right']] = expInfo[dk.expInfoSide].value == dk.sideDefault and u'Non' or u'Oui'
text_elem['choice_prime']['obj'].setText(u'[ %s ] <- %s %s -> [ %s ]' %(keys_screen['left'], side_ic_choice_prime_dict[keys_screen['left']], side_ic_choice_prime_dict[keys_screen['right']], keys_screen['right']))
text_elem['choice_filler']['obj'].setText(u'[ %s ] <- %s %s -> [ %s ]' %(keys_screen['left'], side_ic_choice_filler_dict[keys_screen['left']], side_ic_choice_filler_dict[keys_screen['right']], keys_screen['right']))