teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースコードを全文記載しました

2021/04/19 07:58

投稿

aca_ffk
aca_ffk

スコア3

title CHANGED
File without changes
body CHANGED
@@ -20,6 +20,143 @@
20
20
  ### 該当のソースコード
21
21
 
22
22
  ```python
23
+ # -*- coding: utf-8 -*-
24
+
25
+ from psychopy import visual, core, event, gui, data, misc
26
+ import numpy, os , random,time,csv
27
+
28
+ #参加者IDの取得
29
+ try:
30
+ expInfo = misc.fromFile('lastParams.pickle')
31
+ except:
32
+ expInfo = {'Participant':'001'}
33
+
34
+ expInfo['dateStr']=data.getDateStr()
35
+ dlg = gui.DlgFromDict(expInfo, title='Experiment', fixed=['dataStr'])
36
+ if dlg.OK:
37
+ misc.toFile('lastParams.pickle',expInfo)
38
+ else:
39
+ core.quit()
40
+
41
+ #結果の保存場所
42
+ results = []
43
+
44
+
45
+
46
+
47
+ colorDic = {
48
+ u'赤': {'rgb': ( 1, -1,-1), 'type': '1'},
49
+ u'黄': {'rgb': ( 1, 1,-1), 'type': '2'},
50
+ u'青': {'rgb': (-1, -1, 1), 'type': '3'}
51
+ }
52
+
53
+ charConditionList = [
54
+ {'kanjiChar': u'赤', 'color': u'赤'},
55
+ {'kanjiChar': u'黄', 'color': u'赤'},
56
+ {'kanjiChar': u'青', 'color': u'赤'},
57
+ {'kanjiChar': u'赤', 'color': u'黄'},
58
+ {'kanjiChar': u'黄', 'color': u'黄'},
59
+ {'kanjiChar': u'青', 'color': u'黄'},
60
+ {'kanjiChar': u'赤', 'color': u'青'},
61
+ {'kanjiChar': u'黄', 'color': u'青'},
62
+ {'kanjiChar': u'青', 'color': u'青'}
63
+ ]
64
+
65
+ text = '今から色のついたもじがでてきます。\n文字の意味ではなく文字の色に基づいて\nボタンをおしてください。\n文字が赤色ならキーボードの1を、\n黄色なら2を、青色なら3をおしてください。\nこの指示が読めたらスペースキーを押してください\n'
66
+
67
+ myWin = visual.Window(fullscr=True, monitor="Default", allowGUI=False, units="norm", color=(1,1,1))
68
+ instText = visual.TextStim(myWin,text = text,pos=(0,0),color = (-1,-1,-1),height=0.1)
69
+
70
+ instText.draw()
71
+ myWin.flip()
72
+
73
+ keyList = event.waitKeys(keyList=['space'])
74
+
75
+ #反応時間計測のための設定
76
+ stopwatch = core.Clock()
77
+
78
+ M=1
79
+ N = len(charConditionList)
80
+
81
+ for m in range(M):
82
+ r = list(range(N))
83
+ numpy.random.shuffle(r)
84
+ for i, currentState in enumerate(r):
85
+ myWin = visual.Window(fullscr=True, monitor="Default", allowGUI=False, units="norm", color=(1,1,1))
86
+ charCondition = charConditionList[currentState]
87
+ colorData = colorDic[charCondition['color']]
88
+ kanjiCharData = colorDic[charCondition['kanjiChar']]
89
+ char = charCondition['kanjiChar']
90
+ myText = visual.TextStim(myWin,text = char,pos=(0,0),color = colorData['rgb'],height=0.2)
91
+ myText.draw()
92
+ myWin.flip()
93
+
94
+ #参加者の反応測定開始
95
+ #前回の刺激提示の影響を消去する
96
+ event.clearEvents()
97
+
98
+ #ストップウォッチをリセット
99
+ stopwatch.reset()
100
+ #参加者の反応をリセット
101
+ Responded = False
102
+
103
+ #ストップウォッチをリセットしてからstopwatch.getTime()で測定した時間が一秒を超えるまで以下の処理を実行
104
+ while stopwatch.getTime() < 1:
105
+ #もしこれまでに反応がないようなら event.waitKeysで反応を抜き出す
106
+ #Respondedには反応と反応時間が入る
107
+ if not Responded:
108
+ Responded = event.getKeys(keyList=['1','2','3'],timeStamped=stopwatch)
109
+
110
+ #もし一秒たっても反応がないならno responseと反応時間なしで処理する
111
+ if not Responded:
112
+ Responded = [('no respose', 0)]
113
+
114
+ #参加者の測定終了
115
+
116
+
117
+ #正解と不正解のフィードバック
118
+ if Responded[0][0] == 'no respose':
119
+ fbText = visual.TextStim(myWin, text = u'無反応',pos=(0,-0.3),color=(-1,-1,-1),height=0.2)
120
+ rtText = visual.TextStim(myWin, text = str(Responded[0][1])+u'秒',pos=(0,-0.5),color=(-1,-1,-1),height=0.2)
121
+ #保存用の結果
122
+ correctIncorrect = None
123
+
124
+ elif Responded[0][0] == colorData['type']:
125
+ fbText = visual.TextStim(myWin, text = u'正解',pos=(0,-0.3),color=(-1,-1,-1),height=0.2)
126
+ rtText = visual.TextStim(myWin, text = str(Responded[0][1])+u'秒',pos=(0,-0.5),color=(-1,-1,-1),height=0.2)
127
+ #保存用の結果
128
+ correctIncorrect = True
129
+ else:
130
+ fbText = visual.TextStim(myWin, text = u'不正解',pos=(0,-0.3),color=(-1,-1,-1),height=0.2)
131
+ rtText = visual.TextStim(myWin, text = str(Responded[0][1])+u'秒',pos=(0,-0.5),color=(-1,-1,-1),height=0.2)
132
+ #保存用の結果
133
+ correctIncorrect = False
134
+
135
+ #上記で設定したフィードバックと反応時間の書き込み
136
+ fbText.draw()
137
+ rtText.draw()
138
+
139
+ kanjiCharType = kanjiCharData['type']
140
+ colorType = colorData['type']
141
+
142
+ results.append([
143
+ N*m + i,
144
+ kanjiCharType,
145
+ colorType,
146
+ colorType==kanjiCharType,
147
+ Responded[0][0],
148
+ correctIncorrect,
149
+ Responded[0][1]
150
+ ])
151
+
152
+
153
+ myText = visual.TextStim(myWin,text = u'+',pos=(0,0),color = (-1,-1,-1),height=0.2)
154
+ myText.draw()
155
+ myWin.flip()
156
+ core.wait(0.5)
157
+
158
+ ~~~~~~~~~~~~~~~~~該当箇所~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
159
+
23
160
  curD = os.getcwd()
24
161
  datafile = open(os.path.join(curD, 'log', 'Sub{0}_{1}.csv'.format(expInfo['Participant'],
25
162
  expInfo['dateStr'])),'wb')
@@ -28,6 +165,7 @@
28
165
  datafile.write(b'{0},{1},{2},{3},{4},{5},{6}\n'.format(*r))
29
166
  datafile.close()
30
167
 
168
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
169
 
32
170
  ```
33
171