]> git.donarmstrong.com Git - neurodebian.git/blobdiff - survey/makestats
initial redo of the ratings with too much fanciness
[neurodebian.git] / survey / makestats
index 328eed253064231584cb756f14bdb17b0368543a..44b133c70654c99749709d4e069b1f7e87968649 100755 (executable)
@@ -91,7 +91,13 @@ def load_list2dict(name):
     d = {}
     lfile = open(name)
     for line in lfile:
+        if line.strip() == "":
+            continue
         kv = line.split(':')
+        if kv[0] in d:
+            raise RuntimeError(
+                "Got a line %s with a duplicate key %s whenever value for it "
+                "is known already to be %r" % (line, kv[0], d[kv[0]]))
         d[kv[0]] = kv[1].strip().strip('"')
     return d
 
@@ -103,6 +109,8 @@ class DB(dict):
     sw_dict = load_list2dict('swlist.txt')
     position_dict = load_list2dict('position-dd-list.txt')
     employer_dict = load_list2dict('employer-dd-list.txt')
+    ratings_dict = load_list2dict('ratingslist.txt')
+    vm_dict = load_list2dict('vmlist.txt')
 
     def __init__(self, srcdir):
         # eats the whole directory
@@ -171,7 +179,8 @@ class DB(dict):
     def get_nice_name(self, id):
         srcs = [DB.os_dict, os_cat_names, DB.sw_dict, sw_categories,
                 resource_categories, time_categories,
-                DB.datamod_dict, DB.position_dict, DB.employer_dict]
+                DB.datamod_dict, DB.position_dict, DB.employer_dict,
+                DB.vm_dict, DB.ratings_dict]
         for src in srcs:
             if id in src:
                 return src[id]
@@ -196,19 +205,20 @@ def mkpic_os_per_env(db, destdir):
                 stats[osf] = float(stats[osf]) / total_count
         env_stats[env] = stats
     # make stacked barplot
-    pl.figure(figsize=(6.4, 4.8))
+    pl.figure(figsize=(7.5, 4))
     x = np.arange(len(envs))
     bottoms = np.zeros(len(envs))
     for i, os in enumerate(os_order):
-        stat = [env_stats[e][os] for e in envs]
-        pl.bar(x, stat, bottom=bottoms, color=os_colors[i],
-               label=db.get_nice_name(os), width=0.8)
+        stat = [env_stats[e][os] for e in envs[::-1]]
+        pl.barh(x, stat, left=bottoms, color=os_colors[i],
+               label=db.get_nice_name(os), height=0.8)
         bottoms += stat
-    pl.legend(loc='lower right')
-    pl.xticks(x + 0.4,  [db.get_nice_name(e) for e in env_names])
-    pl.xlim(-0.25, len(envs))
+    pl.legend(loc='lower left')
+    pl.yticks(x + 0.4,  env_names[::-1])
+    pl.ylim(-0.25, len(envs))
     pl.title("Operating system preference by environment")
-    pl.ylabel("Fraction of submissions")
+    pl.xlabel("Fraction of submissions")
+    pl.subplots_adjust(left=0.15, right=0.97)
     pl.savefig('%s/ospref_by_env.png' % destdir, format='png', dpi=80)
 
 
@@ -237,13 +247,17 @@ def mkpic_time_per_env(db, destdir):
     pl.ylim(-0.4, len(envs))
     pl.title("Research activity time by environment")
     pl.xlabel("Fraction of submissions")
+    pl.subplots_adjust(right=0.97)
     pl.savefig('%s/time_by_env.png' % destdir, format='png', dpi=80)
 
 
 def mkpic_submissions_per_key(db, destdir, key, title, sortby='name',
                             multiple=False):
     counts = db.get_counts(key)
-    pl.figure(figsize=(6.4, 4.8))
+    pl.figure(figsize=(6.4, (len(counts)-2) * 0.4 + 2))
+    tmargin = .8/len(counts)
+    if tmargin > 0.3: tmargin = 0.3
+    pl.subplots_adjust(left=0.03, right=0.97, top=1-tmargin, bottom=tmargin)
     pl.title(title)
     if not len(counts):
         pl.text(.5, .5, "[Insufficient data for this figure]",
@@ -258,37 +272,22 @@ def mkpic_submissions_per_key(db, destdir, key, title, sortby='name',
         else:
             raise ValueError("Specify either name or count for sortby")
         x = np.arange(len(stats))
-        pl.bar(x + (1./8), [s[1] for s in stats], width=0.75, color = '#008200')
-        pl.xticks(x + 0.5,  ['' for s in stats])
-        for i, s in enumerate(stats):
-            pl.text(i+.5, 0.1, db.get_nice_name(s[0]), rotation=90,
-                    horizontalalignment='center',
-                    verticalalignment='bottom',
+        pl.barh(x + (1./8), [s[1] for s in stats[::-1]], height=0.75, color = '#008200')
+        pl.yticks(x + 0.5,  ['' for s in stats])
+        text_offset = pl.gca().get_xlim()[1] / 30.
+        for i, s in enumerate(stats[::-1]):
+            pl.text(text_offset, i+.5, db.get_nice_name(s[0]),
+                    horizontalalignment='left',
+                    verticalalignment='center',
                     bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
-        pl.xlim(0, len(stats))
+        pl.ylim(0, len(stats))
         yl = "Number of submissions"
         if multiple:
             yl += "\n(multiple choices per submission possible)"
-        pl.ylabel(yl)
+        pl.xlabel(yl)
     pl.savefig('%s/submissions_per_%s.png' % (destdir, key), format='png', dpi=80)
 
 
-def mkpic_resources(db, destdir):
-    res = db.get_counts('software_resource')
-    res = res.items()
-    x = np.arange(len(res))
-    pl.figure(figsize=(6.4, 4.8))
-    pl.title('Software resources')
-    pl.bar(x + (1./8), [s[1] for s in res], width=0.75, color = '#008200')
-    pl.xticks(x + 0.5,  ['' for s in res])
-    for i, s in enumerate(res):
-        pl.text(i+.5, 0.1, db.get_nice_name(s[0]), rotation=90,
-                horizontalalignment='center',
-                verticalalignment='bottom',
-                bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))
-    pl.ylabel('Number of submissions')
-    pl.savefig('%s/software_resources.png' % destdir, format='png', dpi=80)
-
 def mkpic_software(db, destdir):
     for typ in sw_categories.keys():
         mkpic_submissions_per_key(
@@ -312,12 +311,104 @@ def mkpic_rating_by_os(db, env, items, destdir, title):
     pl.legend(loc='lower right')
     pl.savefig('%s/ratings_%s.png' % (destdir, env), format='png', dpi=80)
 
+def mkpic_rating_by_os_hor_joined(db, env, items, destdir, title,
+                                  intro_sentence="I agree with the statements"):
+    per_os_width = 10
+    max_rating = 4
+    #pl.figure(figsize=(6.4, 4.8))
+    nos = len(os_order)
+    rst = open('figures/ratings_%s.rst' % env, 'w')
+    rst.write("""
+
+%s
+%s
+
+%s
+
+.. raw:: html
+
+   <table>
+    """ % (title, '=' * len(title), intro_sentence))
+    for k, it in enumerate(items):
+        pl.figure(figsize=(3.2, 0.5))
+        it_nice = db.get_nice_name(it)#.lstrip('.').lstrip(' ')
+        it_nice = it_nice[0].upper() + it_nice[1:]
+        for i, os in enumerate(os_order):
+            ratings = np.array(db.select_match(env,
+                                               os_family[os]).get_not_none('%s' % (it,))[0])
+            # assume that we have 4 grades from 0 to 3
+            if len(ratings):
+                assert(max(ratings) < max_rating)
+            total = len(ratings)
+            bottom = 0
+            for r in range(max_rating)[::-1]: # so we go from agree to don't
+                stat = np.sum(ratings == r) * per_os_width / float(total)
+                #print r, it, os, stat, total
+                #if it == "pers_r8" and os == "linux" and r == 3:
+                #    import pydb; pydb.debugger()
+                kwargs = dict(label=None)
+                if stat:
+                    pl.barh(1.0/nos * (nos - 1 - i), stat, left=bottom, color=os_colors[i],
+                            height=.25, alpha=0.25 + r/float(max_rating),
+                            label=None,
+                            edgecolor=os_colors[i])
+                bottom += stat
+            # Complement with errorbar
+            if len(ratings):
+                meanstat = np.mean(ratings)
+                errstat = len(ratings) > 1 and np.std(ratings) or 0
+                pl.errorbar([per_os_width * (max_rating-1 - float(meanstat))/(max_rating-1)],
+                            [1.0/nos * (nos - 0.5 - i)],
+                            xerr=[errstat], fmt='o', color=os_colors[i], ecolor='k')
+        #plot_bars(ratings, offset=((i+1)*0.2)-0.1, color=os_colors[i],
+        #          title=title, ylabel="Mean rating", label=db.get_nice_name(os))
+        #import pydb; pydb.debugger()
+        #pl.xlim((0, per_os_width * len(os_order)))
+        #pl.ylim((0, 1)) # len(items)))
+        #pl.yticks((0, 3), ['Disagree', 'Agree'], rotation=90)
+        #pl.xticks(np.arange(len(items))+0.5, [i[-2:] for i in items],
+        #          horizontalalignment='center')
+        #pl.legend(loc='lower right')
+        pl.xlim((0, per_os_width))
+        pl.ylim((0, 1))
+        pl.axis('off')
+        pl.subplots_adjust(left=0.01, right=0.99, bottom=0.0, top=1,
+                       wspace=0.05, hspace=0.05)
+        fname = '%s/ratings_%s_%s.png' % (destdir, env, it)
+        pl.savefig(fname, format='png', dpi=80)
+        pl.close()
+        oddrow_s = k % 2 == 0 and ' class="oddrow"' or ''
+        rst.write("""
+        <tr%(oddrow_s)s>
+        <td class="task">%(it_nice)s</td>
+        <td class="response"> <div class="figure"><img border=0 alt="%(fname)s" src="%(fname)s" /> </div> </td> </tr>"""
+                  % (locals()))
+
+    #pl.yticks(range(len(items)),  [db.get_nice_name(it) for it in items])
+    #pl.xlim((0, per_os_width))# * len(os_order)))
+    #pl.ylim((0, len(items)))
+    #pl.axis('off')
+    #pl.subplots_adjust(left=0, right=1, bottom=0, top=1,
+    #                   wspace=0.05, hspace=0.05)
+    #pl.savefig('%s/ratings_%s.png' % (destdir, env), format='png', dpi=80)
+    #pl.close()
+    rst.write("""
+    </table>
+
+    """)
+    rst.close()
+    return
+
 
 def main(srcdir, destdir):
     db = DB(srcdir)
     if not os.path.exists(destdir):
         os.makedirs(destdir)
 
+    mkpic_submissions_per_key(
+        db, destdir, 'virt_prod', sortby='name',
+        title='Virtualization product popularity\n(multiple choices per submission possible)')
+
     mkpic_submissions_per_key(
         db, destdir, 'bg_datamod', sortby='count',
         title='Submissions per data modality\n(multiple choices per submission possible)')
@@ -331,15 +422,24 @@ def main(srcdir, destdir):
     mkpic_submissions_per_key(
         db, destdir, 'bg_employer', title='Submissions per venue', sortby='count')
 
-    for pic in [mkpic_os_per_env, mkpic_software,
-                mkpic_resources, mkpic_time_per_env]:
+    mkpic_submissions_per_key(
+        db, destdir, 'software_resource', title='Software resource popularity', sortby='count')
+
+    for pic in [mkpic_os_per_env, mkpic_software, mkpic_time_per_env]:
         pic(db, destdir)
-    mkpic_rating_by_os(db, 'pers_os', ['pers_r%i' % i for i in range(1, 9)], destdir,
-                       "Ratings: Personal environment")
-    mkpic_rating_by_os(db, 'man_os', ['man_r%i' % i for i in range(1, 5)], destdir,
-                       "Ratings: Managed environment")
-    mkpic_rating_by_os(db, 'virt_host_os', ['virt_r%i' % i for i in range(1, 4)], destdir,
-                       "Ratings: Virtual environment (by host OS)")
+    mkpic_rating_by_os_hor_joined(db, 'pers_os', ['pers_r%i' % i for i in range(1, 9)], destdir,
+                       "Personal environment", "I prefer this particular scientific software environment because ...")
+    mkpic_rating_by_os_hor_joined(db, 'man_os', ['man_r%i' % i for i in range(1, 5)], destdir,
+                       "Managed environment")
+    mkpic_rating_by_os_hor_joined(db, 'virt_host_os', ['man_r%i' % i for i in range(1, 4)], destdir,
+                       "Virtual environment (by host OS)")
+
+    ## mkpic_rating_by_os(db, 'pers_os', ['pers_r%i' % i for i in range(1, 9)], destdir,
+    ##                    "Ratings: Personal environment")
+    ## mkpic_rating_by_os(db, 'man_os', ['man_r%i' % i for i in range(1, 5)], destdir,
+    ##                    "Ratings: Managed environment")
+    ## mkpic_rating_by_os(db, 'virt_host_os', ['virt_r%i' % i for i in range(1, 4)], destdir,
+    ##                    "Ratings: Virtual environment (by host OS)")
     # submission stats: this is RST
     statsfile = open('%s/stats.txt' % destdir, 'w')
     statsfile.write('::\n\n  Number of submissions: %i\n' % len(db))