forked from claudiodonofrio/icoslimesurvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lime.py
228 lines (160 loc) · 6.08 KB
/
lime.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 23 09:59:32 2019
@author: Claudio
"""
import icoslimer
import base64
import pandas as pd
import io
import os
import matplotlib.pyplot as plt
import report
import shutil
# ------------------------------------------------------------------
# set server and authentication
# ------------------------------------------------------------------
url = 'http://localhost/limesurvey/index.php/admin/remotecontrol'
usr = 'limey'
pwd = 'EnvriPlus'
# ------------------------------------------------------------------
# create a new instance,
# ------------------------------------------------------------------
lime = icoslimer.IcosLimer()
lime.set_url(url)
lime.set_auth(usr, pwd)
lime.set_session_key()
# ------------------------------------------------------------------
# get a list of all the survyes
# ------------------------------------------------------------------
surveys = lime.list_surveys()
for s in surveys['result']:
print(s)
#print(s['sid'])
# ------------------------------------------------------------------
# get the statistics for all surveys
# ------------------------------------------------------------------
# define the output format from limesurvey
# you may choose between html, xls, pdf. Default is set to html
# set "lang" to choose a specific language, leaf blank for survey
# default language
# set graph to "1" if you like to have bar charts included. Is only
# valid for output format pdf
output_format = 'html'
lang = ''
graph = '0'
# empty container to hold all the statistics
stats_list = []
for s in surveys['result']:
stats = lime.get_statistics(s['sid'], output_format)
# check if it is a json object and decode base64
try:
stats = base64.b64decode(stats['result'])
stats_list.append([s['sid'], stats])
except:
print(stats['error'])
# you can get a timline for each survery. When was each survey
# submitted? start/endate in form: yyyymmdd
startdate = '20190101'
enddate = '20251231'
timeline_list = []
for s in stats_list:
t = lime.get_timeline(s[0], 'day', startdate, enddate)
timeline_list.append([s[0], t['result']])
# ------------------------------------------------------------------
# now release the session key
# afterwards you CAN NOT CALL new functions from the survey
# ------------------------------------------------------------------
lime.release_session_key()
# ------------------------------------------------------------------
# do some fancy stuff with the statistics
# ------------------------------------------------------------------
# denpending on output format for statistics
# you have either pdf's, xls or html files.
# let's safe all the stats to a file. Format depending on output_format
# make sure the output directory exists
if not os.path.isdir('out'):
os.mkdir('out')
for index, stats in stats_list:
if not os.path.isdir('./out/'+index):
os.mkdir('./out/'+index)
with open('./out/' + index + '/' + index + '.' + output_format, 'wb') as f:
f.write(stats)
# if you got the "xls" format you can read the data into a python pandas
# data frame to calculate your own statistics
if output_format == 'xls':
for id, s in stats_list:
dataframe = pd.read_excel(io.BytesIO(s))
# print to console
print(dataframe)
# we have extracted the timelines for each survey
# create a pandas data frame with column names
# print to console
# print each survey with a "dict" for the time entries
col = ['survey id', 'count per day']
df = pd.DataFrame(timeline_list, columns=col)
print(df)
# or as continous array
val=[]
for t in timeline_list:
sid = t[0]
data = t[1]
for k,v in data.items():
val.append([sid,k,v])
df = pd.DataFrame(val, columns=['sid', 'time', 'count'])
print(df)
# now we create timeline figures and save them
for t in timeline_list:
fig, ax = plt.subplots()
ax.grid()
ax.set(xlabel='date (day)', ylabel='n (count) ',
title='Timeline for survey submissions')
ts=[]
count=[]
sid = t[0]
data = t[1]
for k,v in data.items():
ts.append(v)
count.append(k)
ax.plot(count, ts, '*')
ax.plot(count, ts, label=sid)
ax.legend()
ax.set_ylim(bottom=0)
plt.savefig('./out/' + sid + '/' + sid + '_timeline.png', dpi=300, format='png')
# and finally we create a custom report
# but this is only possible, if output_format is HTML
if output_format == 'html':
templatename = 'envriplus.html'
for detail in surveys['result']:
sid = detail['sid']
report_name = sid + '_report.html'
statistic = None
timeline = None
detail = None
# get statistis.
for s in stats_list:
if s[0] == sid:
statistic = s[1]
# prettyfy
statistic = statistic.decode()
# find the corresponding timeline
for t in timeline_list:
if t[0] == sid:
# we assume, the timeline image is already saved.
timeline = sid + '_timeline.png'
for d in surveys['result']:
if d['sid'] == sid:
detail = d
# if stats or timeline is missing, skip
if statistic is None or timeline is None or detail is None:
pass
else:
# create the report and safe to file
r = report.report(templatename,detail, statistic, timeline)
folder = './out/' + sid +'/'
with open(folder + report_name, 'w') as f:
f.write(r)
# now we copy the logo and css file into the same directory
# such that the report is self contained
shutil.copyfile('./template/envriplus_logo.png', folder + 'envriplus_logo.png')
shutil.copyfile('./template/envriplus.css', folder + 'envriplus.css')