|
| 1 | +import random |
1 | 2 | from smile.common import * |
2 | 3 | from smile.scale import scale as s |
3 | 4 | from smile.clock import clock |
4 | 5 | from . import happy_config as default_happy_config |
5 | 6 |
|
| 7 | +def make_trials(config): |
| 8 | + temp_moods = [mm for mm in config.MOODS] |
| 9 | + random.shuffle(temp_moods) |
| 10 | + trials = [] |
| 11 | + for mm in temp_moods: |
| 12 | + trial = dict( |
| 13 | + inst=f"How {mm['mood']} are you at this moment?\nPress F to move left, Press J to move right.", |
| 14 | + mood=mm['mood'].title(), |
| 15 | + notmood=mm['notmood'].title() |
| 16 | + ) |
| 17 | + trials.append(trial) |
| 18 | + return trials |
| 19 | + |
6 | 20 | @Subroutine |
7 | 21 | def HappyQuest(self, task, block_num, trial_num, config=default_happy_config): |
8 | | - with Parallel(): |
9 | | - Label(text="How happy are you at this moment?\nPress F to move left, Press J to move right.", |
10 | | - font_size=s(config.HAPPY_FONT_SIZE), |
11 | | - halign='center', |
12 | | - center_y=self.exp.screen.center_y + s(300)) |
13 | | - sld = Slider(min=-10, max=10, value=0, width=s(config.SLIDER_WIDTH)) |
14 | | - Label(text="unhappy", font_size=s(config.HAPPY_FONT_SIZE), |
15 | | - center_x=sld.left, center_y=sld.center_y - s(100)) |
16 | | - Label(text="happy", font_size=s(config.HAPPY_FONT_SIZE), |
17 | | - center_x=sld.right, center_y=sld.center_y - s(100)) |
18 | | - Label(text='Press Spacebar to lock-in your response.', |
19 | | - top=sld.bottom - s(250), font_size=s(config.HAPPY_FONT_SIZE)) |
| 22 | + gen = Func( |
| 23 | + make_trials, |
| 24 | + config |
| 25 | + ) |
| 26 | + self.trials = gen.result |
20 | 27 |
|
21 | 28 | with UntilDone(): |
22 | | - self.happy_start_time = Ref(clock.now) |
23 | | - self.last_check = self.happy_start_time |
24 | | - self.happy_dur = 0.0 |
25 | | - self.happy_speed = config.HAPPY_INC_BASE |
26 | | - self.first_press_time = None |
27 | | - with Loop(): |
28 | | - ans = KeyPress(keys=config.RESP_HAPPY) |
29 | | - with If(self.first_press_time == None): |
30 | | - self.first_press_time = ans.press_time |
31 | | - with If(ans.press_time['time'] - self.last_check < |
32 | | - config.NON_PRESS_INT): |
33 | | - self.happy_speed = (config.HAPPY_INC_BASE * (Ref(clock.now) - |
34 | | - self.happy_start_time) * config.HAPPY_MOD) + config.HAPPY_INC_START |
35 | | - with Else(): |
36 | | - self.happy_speed = config.HAPPY_INC_START |
| 29 | + |
| 30 | + with Loop(self.trials) as trial: |
| 31 | + Wait(0.3) |
| 32 | + with Parallel(): |
| 33 | + Label(text=trial.current['inst'], |
| 34 | + font_size=s(config.HAPPY_FONT_SIZE), |
| 35 | + halign='center', |
| 36 | + center_y=self.exp.screen.center_y + s(300)) |
| 37 | + sld = Slider(min=-10, max=10, value=0, width=s(config.SLIDER_WIDTH)) |
| 38 | + Label(text=trial.current['notmood'], font_size=s(config.HAPPY_FONT_SIZE), |
| 39 | + center_x=sld.left, center_y=sld.center_y - s(100)) |
| 40 | + Label(text=trial.current['mood'], font_size=s(config.HAPPY_FONT_SIZE), |
| 41 | + center_x=sld.right, center_y=sld.center_y - s(100)) |
| 42 | + Label(text='Press Spacebar to lock-in your response.', |
| 43 | + top=sld.bottom - s(250), font_size=s(config.HAPPY_FONT_SIZE)) |
| 44 | + |
| 45 | + with UntilDone(): |
37 | 46 | self.happy_start_time = Ref(clock.now) |
38 | | - self.last_check = Ref(clock.now) |
39 | | - with If(ans.pressed == config.RESP_HAPPY[0]): |
40 | | - with If(sld.value - self.happy_speed <= (-1*config.HAPPY_RANGE)): |
41 | | - UpdateWidget(sld, value=(-1*config.HAPPY_RANGE)) |
42 | | - with Else(): |
43 | | - UpdateWidget(sld, value=sld.value - self.happy_speed) |
44 | | - with Elif(ans.pressed == config.RESP_HAPPY[1]): |
45 | | - with If(sld.value + self.happy_speed >= config.HAPPY_RANGE): |
46 | | - UpdateWidget(sld, value=config.HAPPY_RANGE) |
47 | | - with Else(): |
48 | | - UpdateWidget(sld, value=sld.value + self.happy_speed) |
49 | | - with UntilDone(): |
50 | | - submit = KeyPress(keys=['SPACEBAR']) |
51 | | - Log(name="happy", |
52 | | - task=task, |
53 | | - block_num=block_num, |
54 | | - trial_num=trial_num, |
55 | | - slider_appear=sld.appear_time, |
56 | | - first_press=self.first_press_time, |
57 | | - submit_time=submit.press_time, |
58 | | - value=sld.value) |
| 47 | + self.last_check = self.happy_start_time |
| 48 | + self.happy_dur = 0.0 |
| 49 | + self.happy_speed = config.HAPPY_INC_BASE |
| 50 | + self.first_press_time = None |
| 51 | + with Loop(): |
| 52 | + ans = KeyPress(keys=config.RESP_HAPPY) |
| 53 | + with If(self.first_press_time == None): |
| 54 | + self.first_press_time = ans.press_time |
| 55 | + with If(ans.press_time['time'] - self.last_check < |
| 56 | + config.NON_PRESS_INT): |
| 57 | + self.happy_speed = (config.HAPPY_INC_BASE * (Ref(clock.now) - |
| 58 | + self.happy_start_time) * config.HAPPY_MOD) + config.HAPPY_INC_START |
| 59 | + with Else(): |
| 60 | + self.happy_speed = config.HAPPY_INC_START |
| 61 | + self.happy_start_time = Ref(clock.now) |
| 62 | + self.last_check = Ref(clock.now) |
| 63 | + with If(ans.pressed == config.RESP_HAPPY[0]): |
| 64 | + with If(sld.value - self.happy_speed <= (-1*config.HAPPY_RANGE)): |
| 65 | + UpdateWidget(sld, value=(-1*config.HAPPY_RANGE)) |
| 66 | + with Else(): |
| 67 | + UpdateWidget(sld, value=sld.value - self.happy_speed) |
| 68 | + with Elif(ans.pressed == config.RESP_HAPPY[1]): |
| 69 | + with If(sld.value + self.happy_speed >= config.HAPPY_RANGE): |
| 70 | + UpdateWidget(sld, value=config.HAPPY_RANGE) |
| 71 | + with Else(): |
| 72 | + UpdateWidget(sld, value=sld.value + self.happy_speed) |
| 73 | + with UntilDone(): |
| 74 | + submit = KeyPress(keys=['SPACEBAR']) |
| 75 | + Log(name="happy", |
| 76 | + task=task, |
| 77 | + mood=trial.current['mood'], |
| 78 | + block_num=block_num, |
| 79 | + trial_num=trial_num, |
| 80 | + slider_appear=sld.appear_time, |
| 81 | + first_press=self.first_press_time, |
| 82 | + submit_time=submit.press_time, |
| 83 | + value=sld.value) |
59 | 84 |
|
60 | 85 |
|
61 | 86 | if __name__ == "__main__": |
62 | 87 | import config |
63 | 88 |
|
64 | 89 | exp = Experiment() |
65 | 90 |
|
66 | | - HappyQuest(config) |
| 91 | + HappyQuest(task='test', block_num=0, trial_num=0) |
| 92 | + HappyQuest(task='test', block_num=0, trial_num=1) |
67 | 93 |
|
68 | 94 | exp.run() |
0 commit comments