TEXT   13

makeBibCitePics py

Guest on 23rd August 2022 10:51:07 AM

  1. #! /usr/bin/env python
  2. ## -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Apr 13 16:52:54
  5.  
  6. @author: siome
  7. #! /usr/bin/env python
  8. """
  9.  
  10. import sys
  11. import os
  12. import bibtexparser
  13. #from   bibtexparser.bparser import BibTexParser
  14. import bibtexparser.bparser
  15. import bibtexparser.customization
  16.  
  17. import numpy as np
  18. import matplotlib.pyplot as plt
  19.  
  20. directory = '/Users/siome/Work/Version.Control/HTML-SVN/html/trunk/pub-python/bibfig'
  21.  
  22.  
  23. areas = { u'cv':(u'Computer Vision', u'Images/Vision-100.png'),
  24.           u'ai':(u'Artificial Intelligence', u'Images/AI-100.png'),
  25.           u'ml':(u'Machine Learning', u'Images/ML-80.png'),
  26.           u'forensics':(u'Forensics', u'Images/Forensics-80.png'),
  27.           u'access':(u'Accessibility', u'Images/Access-80.png'),
  28.           u'cg':(u'Computer Graphics', u'Images/Graphics-80.png'),
  29.           u'asl':(u'Sign Language', u'Images/ASL-100.png'),
  30.           u'wavelets':(u'Wavelets', u'Images/Wavelet-100.png'),
  31.           u'sport': (u'Sport', u'Images/Sport-80.png'),
  32.           u'uncert': (u'Uncertanties', u'Images/Uncert-100.png'),
  33.           u'virtcrd': (u'Virtual Crowds', u'Images/Crowd-80.png'),
  34.           u'med': (u'Biomedical', u'Images/Med-64.png'),
  35.           u'biblio': (u'Bibliometrics', u'Images/Book-80.png')
  36.           }
  37.  
  38. areas_keys = areas.keys()
  39.  
  40.  
  41. def stripnline(record):
  42.     if u"author" in record:
  43.         record[u"author"] = record[u"author"].replace(u'\n', u' ').strip()
  44.  
  45.     if "title" in record:
  46.         record[u"title"] = record[u"title"].replace(u'\n', u' ').strip()
  47.        
  48.     return record
  49.  
  50. def histcit(r):
  51.  
  52.     dyc = {}    
  53.     if u'citeshist' in r:
  54.         for ync in r[u'citeshist'].split(';'):
  55.             vync = ync.split(',')
  56.             dyc[int(vync[0])]=int(vync[1])
  57.    
  58.     r[u'citeshist'] = dyc
  59.     return r
  60.  
  61.  
  62.  
  63. def customizations(record):
  64.     """Use some functions delivered by the library
  65.  
  66.     :param record: a record
  67.     :returns: -- customized record
  68.     """
  69.     record = bibtexparser.customization.type(record)
  70.     record = bibtexparser.customization.keyword(record)
  71.     record = stripnline(record)
  72.     record = histcit(record)
  73.     return record
  74.  
  75.  
  76. def autolabel(rects, ax):
  77.     # attach some text labels
  78.     for rect in rects:
  79.         height = rect.get_height()
  80.         width  = rect.get_width()
  81.         ax.text(rect.get_x() + width/2., height+0.6*width,
  82.                 '%d' % int(height),
  83.                 ha='center', va='bottom')
  84.  
  85.  
  86. def pltHbar(bib):
  87.  
  88.     width = 0.7
  89.     ch   = bib['citeshist']
  90.     vch = np.array([ [int(v.split(',')[0]), \
  91.                       int(v.split(',')[1])] \
  92.                       for v in ch.split(';')])
  93.  
  94.     minyr = np.min(vch[:,0])
  95.     maxyr = np.max(vch[:,0])
  96.     years = vch[:,0] - minyr + 1
  97.     cites = vch[:,1]
  98.    
  99.     fig, ax = plt.subplots()
  100.     rects = ax.bar(years, cites, width, color='0.75')#, width, color='r', yerr=menStd)
  101.  
  102.     ax.set_title(bib['title'])
  103.     ax.set_ylabel('Citations')
  104.     ax.set_xlabel('Years')
  105.     plt.xlim((np.min(years)-width/2, np.max(years)+1.5*width))
  106.     plt.ylim(0, 1.15 * np.max(vch[:,1]))
  107.  
  108.     ax.set_xticks(years + width/2.0)
  109.     ax.set_xticklabels([str(v) for v in vch[:,0] ])
  110.  
  111.     autolabel(rects, ax)
  112.  
  113.     plt.savefig(os.path.join(directory, bib['ID']+'.pdf'))
  114.     plt.cla()
  115.  
  116.  
  117. #
  118. #  input is the variable with all the text of the bibtex file
  119. #  output is a list of dictionaries
  120. #
  121. def parseBibtex( bibtex_fname ):
  122.  
  123.     with open(bibtex_fname) as bibtex_file:
  124.         parser = bibtexparser.bparser.BibTexParser()
  125. #        parser.customization = customizations
  126.         bibtex_database = bibtexparser.load(bibtex_file, parser=parser)
  127.  
  128.     # get list of dictionaries
  129.     return bibtex_database.entries
  130.  
  131.  
  132.  
  133.  
  134. if __name__ == "__main__":
  135.     if len(sys.argv) != 2:
  136.         print ('\n %s: requires name of the bibtex file!\n' % sys.argv[0])
  137.         sys.exit()
  138.     fin  = sys.argv[1]
  139.    
  140. #    bibfile = codecs.open(fname,encoding='latin_1').read()
  141.     bibs = parseBibtex( fin )
  142. #    s = generateWebpage( bibs )
  143.  
  144. #    with open(sys.argv[2],'w') as fout:
  145. #        fout.write(s.encode('utf8'))
  146.  
  147.     if not os.path.exists(directory):
  148.         os.makedirs(directory)
  149.        
  150.     for b in bibs:
  151.         if not b['citeshist'] == '':
  152.             pltHbar(b)

Raw Paste


Login or Register to edit or fork this paste. It's free.