-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTrialSeq.py
More file actions
130 lines (107 loc) · 4.09 KB
/
CustomTrialSeq.py
File metadata and controls
130 lines (107 loc) · 4.09 KB
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
from psychopy_template import (
TrialGroup,
BlockBreak,
Fixation,
Intro,
Outro,
TrialBreak,
RatingScreen,
)
# from itertools import chain
import copy
from random import shuffle
class TrialSequence:
def __init__(self, exp, win, endsurvey=False):
self.trial_sequence = []
self.initTrials(exp, win)
self.addIntro(exp, win)
if endsurvey: self.addEndSurvey(exp, win)
self.addOutro(exp, win)
def initTrials(self, exp, win):
# Define blocks
block = self.defineBlock(exp, win)
blockbreak = BlockBreak(exp, win)
# self.trial_sequence.append(self.intro)
if exp.blocked == True: # Block design
# Repeats the same block, adds breaks in between
for ind in range(exp.total_blocks):
thisBlock = copy.copy(block)
shuffle(thisBlock)
self.trial_sequence.extend(thisBlock)
if ind < exp.total_blocks - 1:
self.trial_sequence.append(blockbreak)
if exp.blocked == False: # Random design
# Inserts block breaks at defined intervals
ntrl = len(self.trial_sequence)
for ind in range(ntrl, 0, -1):
if (ind % (ntrl // exp.total_blocks) == 0
and ind >= ntrl // exp.total_blocks
and ind < ntrl):
self.trial_sequence.insert(ind, blockbreak)
def defineBlock(self, exp, win):
block = []
before_trials = self.addBeforeTrials(exp, win)
after_trials = self.addAfterTrials(exp, win)
for cond in exp.trials:
trial_group = [*before_trials, cond["cond_trl"], *after_trials]
# trial_group = list(chain(*trial_group))
cond_group = [
TrialGroup(trial_group=copy.copy(trial_group))
for _ in range(cond["nreps"])
]
block.extend(cond_group)
return block
def addBeforeTrials(self, exp, win):
self.fixation = Fixation(exp, win, duration=1)
self.before_trials = [self.fixation]
return self.before_trials
def addAfterTrials(self, exp, win):
self.trial_break = TrialBreak(exp, win)
self.after_trials = [self.trial_break]
return self.after_trials
def addEndSurvey(self, exp, win):
self.endsurvey = [
RatingScreen(
exp,
win,
question="Were the instructions you received before the experiment clear? How well do you think you understood the task in the experiment?",
choices=map(str, range(1, 6)),
),
RatingScreen(
exp,
win,
question="How confident were you about the responses you have given during the experiment?",
choices=map(str, range(1, 6)),
),
]
self.trial_sequence.extend(self.endsurvey)
def addIntro(self, exp, win):
self.intro = Intro(exp, win)
self.trial_sequence.insert(0, self.intro)
def addOutro(self, exp, win):
self.outro = Outro(exp, win)
self.trial_sequence.append(self.outro)
def append(self, trialseq):
if not isinstance(trialseq, TrialSequence):
raise TypeError("Can only append TrialSequences to TrialSequences.")
# Remove Outro and EndSurvey from trial_sequence
trial_sequence1 = [
trl
for trl in self.trial_sequence
if trl.__class__.__name__ not in ["Outro", "EndSurvey"]
]
# Remove Intro from trialseq
intro = [
trl
for trl in trialseq.trial_sequence
if trl.__class__.__name__ == "Intro"
][0]
trial_sequence2 = [
trl
for trl in trialseq.trial_sequence
if trl.__class__.__name__ != "Intro"
]
# Add new intro
intro.text = "The first part is over.\nThe second part is about to start."
trial_sequence2.insert(0, intro)
self.trial_sequence = trial_sequence1 + trial_sequence2