]> git.donarmstrong.com Git - neurodebian.git/blob - survey/makestats
Towards survey stats.
[neurodebian.git] / survey / makestats
1 #!/usr/bin/python
2
3 from glob import glob
4 import json
5 import sys
6 import pylab as pl
7 import numpy as np
8
9 class DB(dict):
10     def __init__(self, srcdir):
11         # eats the whole directory
12         datafilenames = glob('%s/*.json' % srcdir)
13         for dfn in datafilenames:
14             rawdata = json.load(open(dfn))
15             self[rawdata['timestamp']] = rawdata
16
17     def get_unique(self, key):
18         # return a set of all (unique) values for a field id
19         uniq = set()
20         for d in self.values():
21             if key in d:
22                 el = d[key]
23                 if isinstance(el, list):
24                     uniq = uniq.union(el)
25                 else:
26                     uniq = uniq.union((el,))
27         return uniq
28
29     def get_not_none(self, key):
30         # return a list of all values of a specific field id
31         # the second return value is count of submission that did not have data
32         # for this field id
33         val = []
34         missing = 0
35         for d in self.values():
36             if key in d:
37                 el = d[key]
38                 if isinstance(el, list):
39                     val.extend(el)
40                 else:
41                     if el == 'none':
42                         missing += 1
43                     else:
44                         val.append(el)
45             else:
46                 missing += 1
47         return val, missing
48
49     def get_counts(self, key):
50         # return a dict with field values as keys and respective submission 
51         # count as value
52         vals = self.get_not_none(key)[0]
53         uniq = np.unique(vals)
54         counts = dict(zip(uniq, [vals.count(u) for u in uniq]))
55         return counts
56
57     def select_match(self, key, values):
58         # return a db with all submissions were a field id has one of the
59         # supplied values
60         match = {}
61         for k, v in self.items():
62             if not key in v:
63                 continue
64             el = v[key]
65             if isinstance(el, list):
66                 if len(set(values).intersection(el)):
67                     match[k] = v
68             elif el in values:
69                 match[k] = v
70         return match
71
72
73
74 def load_list2dict(name):
75     d = {}
76     lfile = open(name)
77     for line in lfile:
78         kv = line.split(':')
79         d[kv[0]] = kv[1].strip().strip('"')
80     return d
81
82 def mkpic_submissions_per_datamod(db, destdir):
83     # simple demo
84     dmd = load_list2dict('datamodlist.txt')
85     spd = db.get_counts('bg_datamod')
86     spd = sorted(spd.items(), cmp=lambda x, y: cmp(x[1], y[1]))[::-1]
87     x = np.arange(len(spd))
88     pl.figure(figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
89     pl.title('Data modality')
90     pl.bar(x, [s[1] for s in spd])
91     pl.xticks(x + 0.5,  [dmd[k[0]] for k in spd], rotation=-15)
92     pl.ylabel('Survey submissions per data modality\n(multiple choices per submission possible)')
93     pl.savefig('%s/submissions_per_datamod.png' % destdir, format='png')
94
95 def main(srcdir, destdir):
96     db = DB(srcdir)
97     mkpic_submissions_per_datamod(db, destdir)
98
99 if __name__ == '__main__':
100     main(sys.argv[1], sys.argv[2])