# -*- coding: utf-8 -*-
""" Update Readme.md and cumulative_gans.jpg """
from __future__ import print_function
from __future__ import division

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns   # implicitly use seaborn theme in matplotlib


def load_data():
    """ Load GANs data from the gans.csv file """
    import csv

    with open('gans.tsv') as fid:
        reader = csv.DictReader(fid, delimiter='\t')
        gans = [row for row in reader]
    return gans


def update_readme(gans):
    """ Update the Readme.md text file from a Jinja2 template """
    import jinja2 as j2

    gans.sort(key=lambda v: v['Abbr.'].upper())
    j2_env = j2.Environment(loader=j2.FileSystemLoader('.'),
                            trim_blocks=True, lstrip_blocks=True)

    with open('README.md', 'w') as fid:
        print(j2_env.get_template('README.j2.md').render(gans=gans), file=fid)


def update_figure(gans):
    """ Update the figure cumulative_gans.jpg """
    data = np.array([int(gan['Year']) + int(gan['Month']) / 12
                     for gan in gans])
    y_range = int(np.ceil(data.size / 10)) * 10

    plt.hist(data, 100, cumulative="True")
    plt.xticks(range(2014, 2018))
    plt.yticks(np.arange(0, y_range, 10))
    plt.title("Cumulative number of named GAN papers by month")
    plt.xlabel("Year")
    plt.ylabel("Total number of papers")
    plt.savefig('cumulative_gans.jpg')


if __name__ == '__main__':
    GANS = load_data()
    update_readme(GANS)
    update_figure(GANS)

@@ -192,77 +192,8 @@
 plt%0A
-import seaborn as sns   # implicitly use seaborn theme in matplotlib%0A
 %0A%0Ade
@@ -1054,18 +1054,65 @@
 0)) * 10
+ + 1
 %0A%0A
+    with plt.style.context(%22seaborn%22):%0A    
     plt.
@@ -1146,16 +1146,20 @@
 %22True%22)%0A
+    
     plt.
@@ -1184,24 +1184,28 @@
  2018))%0A    
+   
+ 
 plt.yticks(n
@@ -1230,16 +1230,20 @@
 e, 10))%0A
+    
     plt.
@@ -1298,16 +1298,20 @@
 month%22)%0A
+    
     plt.
@@ -1325,16 +1325,20 @@
 %22Year%22)%0A
+    
     plt.
@@ -1370,16 +1370,20 @@
 apers%22)%0A
+    
     plt.

use seaborn style explicitly in matplotlib and remove unused import