-
Notifications
You must be signed in to change notification settings - Fork 4
/
rt3.py
93 lines (78 loc) · 2.84 KB
/
rt3.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-#
# @(#)rt3.py
#
#
# Copyright (C) 2014, GC3, University of Zurich. All rights reserved.
#
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
__docformat__ = 'reStructuredText'
__author__ = 'Antonio Messina <[email protected]>'
def parse_data(fname):
"""Read the database file `fname` and returns a dictionary with the
values grouped by the `condition` parameter:
{ `condition`: [(`correct`, `rt`),
(`correct`, `rt`), ...] }
`condition` is one of ['easy', 'medium', 'hard']
`correct` is one of [0, 1]
`rt` is a floating number
"""
stream = open(fname)
data = {}
for line in stream:
cond, corr, rt = line.split()
newval = (int(corr), float(rt))
if cond not in data:
data[cond] = [newval]
else:
data[cond].append(newval)
stream.close()
return data
def avg_rt(data, condition='easy', correct=1):
"""
`data` is a dictionary {`condition`: [(`correct`, `rt`), ]}
Returns the average response time for the given parameters
`condition` and `correct`.
"""
subset = data[condition]
values = []
for corr, rt in subset:
if correct == corr:
values.append(rt)
return sum(values)/len(values)
def analyze_data(data, condition='easy', max_rt=1e100):
"""Returns the number of correct answer in the dataset `data`, where
condition is `condition` and the maximum response time is lesser
than `max_rt`
"""
subset = data[condition]
correct = 0
for corr, rt in subset:
if rt > max_rt or corr == 0:
continue
correct += 1
return correct
if __name__ == "__main__":
fname = 'rt.tsv'
maxrt = 1e100
cond='easy'
data = parse_data(fname)
print("Nr. of correct answer for condition %s: %d" % (cond, analyze_data(data, condition=cond, max_rt=maxrt)))
cond='hard'
print("Nr. of correct answer for condition %s: %d" % (cond, analyze_data(data, condition=cond, max_rt=maxrt)))
maxrt = 3.5
cond='hard'
print("Nr. of correct answer for condition %s with RT lesser than %.1f: %d" % (cond, maxrt, analyze_data(data, condition=cond, max_rt=maxrt)))