-
Notifications
You must be signed in to change notification settings - Fork 0
/
getResults.py
executable file
·141 lines (123 loc) · 4.54 KB
/
getResults.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Instituto Nacional de Pesquisas Espaciais (INPE)
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import json
import urllib2
from optparse import OptionParser
def getAppId(server, appName):
"""
Get the application id given the short name
:arg string server: Address of the server
:arg string appName: Short name of the application
:returns: Numerical id of the application
:rtype: integer
"""
JSONdata = urllib2.urlopen(server+"/api/app?short_name="+appName)
data = json.load(JSONdata)
appId = data[0]['id']
return appId
def getTasks(server, appId):
"""
Get the tasks of a particular application from the server.
:arg string server: Address of the server
:arg string appID: ID of the application to be analysed
:returns: Tasks for the applcation
:rtype: dictionary
"""
JSONdata = urllib2.urlopen(server+"/api/task?app_id="+str(appId))
data = json.load(JSONdata)
numberTasks = len(data)
taskId = []
for item in range(numberTasks):
taskId.append(data[item]['id'])
return taskId
def getResults(server, appID, tasksId):
"""
Get the results of a particular application from the server.
:arg string server: Address of the server
:arg string appID: ID of the application to be analysed
:arg list taskId: List of task ID
:returns: Results for the application
:rtype: dictionary
"""
answersApp = {}
numberTasks = len(tasksId)
idxData = 0
for item in range(numberTasks):
JSONdata = urllib2.urlopen(server+"/api/taskrun?app_id="+str(appID)+"&task_id="+str(tasksId[item]))
data = json.load(JSONdata)
lenData = len(data)
for ans in range(lenData):
answersApp[idxData] = {'taskId':data[ans]['task_id'], 'id':data[ans]['id'], 'answer':data[ans]['info']}
idxData = idxData + 1
return answersApp
def genStats(taskId, data, printStats = 0):
"""
Calculate statistics about the results
:art list taskId: List of unique tasks
:arg dict data: Dictionary with all the results.
:returns: Some basic statistics about the results
:rtype: Nothing so far
"""
numberTasks = len(taskId)
numberResults = len(data)
#This structure for search is not efficient. Must replace!!!
for task in range(numberTasks):
voteYes = 0
voteNo = 0
for result in range(numberResults):
if data[result]['taskId'] == taskId[task]:
if data[result]['answer'] == 'Yes':
voteYes = voteYes + 1
else:
voteNo = voteNo + 1
#Print info for debugging
if printStats == 1:
print "Stats for task " + str(taskId[task])
print "Yes = " + str(voteYes)
print "No = " + str(voteNo)
print ""
stats = 'Done.'
return stats
#######################
# Begin of the script #
#######################
if __name__ == "__main__":
# Arguments for the application
usage = "usage: %prog arg1 arg2"
parser = OptionParser(usage)
parser.add_option("-s", "--server", dest="server", help="Address to the server", metavar="SERVER")
parser.add_option("-n", "--app-name", dest="appName", help="Short name of the application", metavar="APPNAME")
(options, args) = parser.parse_args()
if options.server:
server = options.server
else:
server = "http://forestwatchers.net/pybossa"
#server = "http://pybossa.com"
if options.appName:
appName = options.appName
else:
appName = "filtering"
#appName = "flickrperson"
#Get the data
appId = getAppId(server, appName)
tasksId = getTasks(server, appId)
numberTasks = len(tasksId)
print "Number of tasks:", numberTasks
results = getResults(server, appId, tasksId)
numberResults = len(results)
print "Number of results:", numberResults
stats = genStats(tasksId, results, 1)