Changeset 117

Show
Ignore:
Timestamp:
07/02/07 16:28:35 (1 year ago)
Author:
apdavison
Message:

Added VAbenchmarks example

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/CNS2007/poster_cns2007.py

    r116 r117  
    1111from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 
    1212from reportlab.lib.enums import * 
     13from reportlab.pdfbase.pdfmetrics import stringWidth 
    1314import PIL 
    1415import pysvn 
    1516import imp, os 
     17import SilverCity 
     18import sys 
     19import StringIO 
     20 
    1621 
    1722class Author(object): 
     
    3439            setattr(style, attr_name, attr*x)      
    3540 
    36 def getStyleSheet(scale_factor): 
     41def getStyleSheet(base_fontSize, title_fontSize=None): 
    3742    styles = getSampleStyleSheet() 
     43    scale_factor = float(base_fontSize)/styles['BodyText'].fontSize 
    3844    # Scale font sizes and related spacing 
    3945    for style in styles.byName.values(): 
    4046        scale_style(style, scale_factor) 
    41     styles['Title'].fontSize += 8 
     47    if title_fontSize is not None: 
     48        scale_factor = float(title_fontSize)/styles['Title'].fontSize 
     49        scale_style(styles['Title'], scale_factor) 
    4250     
    4351    author_style = ParagraphStyle('Authors', parent=styles['Heading3']) 
     
    4957    styles.add(affiliation_style) 
    5058     
    51     users_guide_style = ParagraphStyle('UsersGuide', parent=styles['BodyText']) 
    52     scale_style(users_guide_style, 1.5/scale_factor) 
    53     styles.add(users_guide_style) 
    54      
    55     for style in "Heading1","Heading2","Heading3","Code": 
    56         newstyle = ParagraphStyle('UsersGuide%s' % style, parent=styles[style]) 
    57         scale_style(newstyle, 1.5/scale_factor) 
    58         styles.add(newstyle) 
    59      
    6059    styles['Title'].alignment = TA_CENTER 
    6160    styles['BodyText'].alignment = TA_JUSTIFY 
    62     styles['UsersGuide'].alignment = TA_JUSTIFY 
    6361     
    6462    print "Font sizes:" 
    65     for style in 'BodyText','Title', 'Heading1', 'Heading2', 'Heading3', 'UsersGuide'
     63    for style in 'BodyText','Title', 'Heading1', 'Heading2', 'Heading3'
    6664        print style, styles[style].fontSize 
    67     ##styles['UsersGuideCode'].listAttrs() 
    6865 
    6966    return styles 
     
    8582    poster.drawInlineImage(logo, x, y, height=logo_height, width=logo_width) 
    8683 
    87 def make_title(title,authors,institutions,logo_left,logo_right): 
     84def make_title(styles,title,authors,institutions,logo_left,logo_right): 
    8885    """Returns the y position of the bottom of the title (including bottom margin).""" 
    89     global poster, margins, pageheight, pagewidth, styles, pagetop 
     86    global poster, margins, pageheight, pagewidth, pagetop 
    9087    title_frame = Frame(margins['left'], pageheight/2.0, pagewidth, 20*cm, 
    9188                        showBoundary=True) 
     
    120117    return title_bottom 
    121118 
    122 def make_abstract(text,x,y,width,height,_debug): 
     119def make_abstract(styles,text,x,y,width,height,_debug): 
    123120    abstract_frame = Frame(x, y - height, width, height, 
    124121                           showBoundary=_debug) 
     
    133130    #    print abstract_frame._height 
    134131 
    135 def make_users_guide(x,y,width,height,_debug=False): 
    136     global poster, styles 
     132def make_users_guide(styles,x,y,width,height,_debug=False): 
     133    global poster 
    137134    svn_client = pysvn.Client() 
    138135    svn_client.checkout(svnpath, "pyNN") 
     
    141138    import restxsl.transform 
    142139    import re 
     140     
     141    #scale_factor = 3.6 
     142    #users_guide_style = ParagraphStyle('UsersGuide', parent=styles['BodyText']) 
     143    #scale_style(users_guide_style, 1.5/scale_factor) 
     144    #styles.add(users_guide_style) 
     145     
     146    #for style in "Heading1","Heading2","Heading3","Code": 
     147    #    newstyle = ParagraphStyle('UsersGuide%s' % style, parent=styles[style]) 
     148    #    scale_style(newstyle, 1.5/scale_factor) 
     149    #    styles.add(newstyle) 
     150    #styles['UsersGuide'].alignment = TA_JUSTIFY 
    143151     
    144152    user_guide_frame = Frame(x, y - height, width, height, showBoundary=_debug) 
     
    156164        #if _debug: print xmltext 
    157165        #if _debug: print "-----------------------------" 
    158         paragraph_list = [Paragraph(section, styles['UsersGuideHeading1'])] 
     166        paragraph_list = [Paragraph(section, styles['Heading1'])] 
    159167        for match in paragraph_template.finditer(xmltext): 
    160168            groups = match.groupdict() 
    161169            content = groups['content'] 
    162             style = "UsersGuide" 
     170            #style = "UsersGuide" 
     171            style = "BodyText" 
    163172            if groups['style'] is not None: 
    164                 style += groups['style'] 
     173                #style += groups['style'] 
     174                style = groups['style'] 
    165175            #if _debug: print "###### " + style 
    166176            #if _debug: print content 
     
    168178        user_guide_frame.addFromList(paragraph_list, poster) 
    169179 
    170 def make_apidocs(x,y,width,height,scale_factor=0.5,_debug=False): 
    171     global poster, styles 
     180def make_apidocs(styles,x,y,width,height,_debug=False): 
     181    global poster 
    172182    svn_client = pysvn.Client() 
    173183    svn_client.checkout(svnpath, "pyNN") 
     
    181191                              ('Category','Heading1'),('Class','Heading2')]: 
    182192        newstyle = ParagraphStyle(style_name, parent=styles[parent]) 
    183         scale_style(newstyle, scale_factor) 
    184193        styles.add(newstyle) 
    185194        styles[style_name].alignment = TA_LEFT 
     
    193202    styles['Class'].spaceAfter *= 0.3 
    194203    styles['APIBody'].spaceBefore = 0 
    195     styles['APIBody'].fontSize += 1 
    196     styles['APIBody'].leading += 1 
     204    styles['Title'].alignment = TA_LEFT 
    197205    print "APIBody ", styles['APIBody'].fontSize 
    198     ##print styles['FunctionDef'].listAttrs() 
    199206     
    200207    apidoc = wikidoc.apidoc('reportlab_xml') 
    201     #print apidoc 
    202208    margin = 0.03 
    203209    subcol_width = width*(1-margin)/2.0 
    204210     
    205211     
    206     paragraph_list = [Paragraph("API reference <font size=14>(version %s)</font>" % __version__, styles['UsersGuideHeading1'])] 
     212    paragraph_list = [Paragraph("API reference <font size=14>(version %s)</font>" % __version__, 
     213                                styles['Title'])] 
    207214    for match in paragraph_template.finditer(apidoc): 
    208215        groups = match.groupdict() 
     
    230237    poster.roundRect(x,y-height,width,f_height,1*cm) 
    231238 
     239def colourize(code): 
     240    generator = SilverCity.Python.PythonHTMLGenerator() 
     241    io = StringIO.StringIO() 
     242    io.getvalue() 
     243    generator.generate_html(io, code) 
     244    output = io.getvalue() 
     245    colours = {'operator': 'brown', # bold 
     246               'number': 'seagreen', 
     247               'default': 'green', 
     248               'identifier': 'black', 
     249               'string': 'olive', 
     250               'word': 'black',  # bold 
     251               'tripledouble': 'olive',  # italic 
     252               'character': 'olive', 
     253               'commentline': 'gray', 
     254               } 
     255    for cls, colour in colours.items(): 
     256      output = output.replace('<span class="p_%s">' % cls, '<font color="%s">' % colour) 
     257    output = output.replace('</span>','</font>') 
     258    return output 
     259 
     260def make_example(styles,x,y,width,height,_debug=False): 
     261    """ Make a frame showing the VAbenchmarks.py script, together with figure.""" 
     262    global poster 
     263    styles['Title'].alignment = TA_LEFT 
     264    styles['Code'].leftIndent = 0 
     265    svn_client = pysvn.Client() 
     266    svn_client.checkout(svnpath, "pyNN") 
     267    f = open(os.path.join('pyNN','test','VAbenchmarks.py'),'r') 
     268    example_script = f.read() 
     269    f.close() 
     270    example_script = colourize(example_script) 
     271     
     272    pad = 0.5*cm 
     273    code_flowable = XPreformatted(example_script, styles['Code']) 
     274    f_width = stringWidth("m"*110, styles['Code'].fontName, styles['Code'].fontSize, 'UTF-8') + 2*pad 
     275    paragraph_list = [Paragraph("Example", styles['Title']), 
     276                      code_flowable] 
     277    # Scale the font size to fill the space 
     278    def calc_height(): 
     279        f_height = 0 
     280        for p in paragraph_list: 
     281            f_height += p.wrap(f_width, pageheight)[1] + p.getSpaceAfter() + p.getSpaceBefore() 
     282        ##f_height = nlines*(styles['Code'].leading+styles['Code'].spaceAfter+styles['Code'].spaceBefore) 
     283        ##f_height += styles['Title'].leading + styles['Title'].spaceBefore + styles['Title'].spaceAfter 
     284        f_height += 2*pad 
     285        return f_height 
     286    scale_style(styles['Code'],5.0) 
     287    while calc_height() > height: 
     288        #print styles['Code'].fontSize 
     289        scale_style(styles['Code'], 0.99) 
     290    print styles['Code'].fontSize 
     291    example_frame = Frame(x, y-height, f_width, height, showBoundary=_debug, 
     292                          leftPadding=pad, rightPadding=pad, bottomPadding=pad, 
     293                          topPadding=pad) 
     294    example_frame.addFromList(paragraph_list, poster) 
     295    poster.roundRect(x,y-height,f_width,height,1*cm) 
     296 
     297def make_example_figure(styles,filename,caption,x,y,width,_debug=False): 
     298    global poster 
     299    img = Image(filename, width=width, height=1e12, kind='proportional', lazy=0) 
     300    caption_paras = [Paragraph(p.strip(), styles['BodyText']) for p in caption.split("\n") if p.strip()] 
     301    print len(caption_paras) 
     302    pad = 0.5*cm 
     303    f_height = img.drawHeight + 2*pad 
     304    for p in caption_paras: 
     305        f_height += p.wrap(width,pageheight)[1] + p.getSpaceAfter() + p.getSpaceBefore() 
     306    figure_frame = Frame(x, y-f_height, 
     307                         width + 2*pad, f_height, 
     308                         showBoundary=_debug, 
     309                         leftPadding=pad, rightPadding=pad, 
     310                         bottomPadding=pad, topPadding=pad) 
     311    figure_frame.addFromList([img] + caption_paras, poster) 
     312 
     313def huge_url(x,y,width): 
     314    global poster 
     315    poster.saveState() 
     316    poster.setFillColor(colors.beige) 
     317    poster.setStrokeColor(colors.beige) 
     318    font = "Helvetica-Bold" 
     319    url = "http://neuralensemble.org/PyNN" 
     320    font_size = 200 
     321    while stringWidth(url, font, font_size, 'UTF-8') > width: 
     322        font_size *= 0.9 
     323    poster.setFont(font, font_size) 
     324    poster.drawCentredString(x,y,"http://neuralensemble.org/PyNN") 
     325    poster.restoreState() 
     326 
    232327# ============================================================================== 
    233328 
    234329if __name__ == "__main__": 
    235330    svnpath = "https://neuralensemble.kip.uni-heidelberg.de/svn/PyNN/branches/0.3" 
    236     DEBUG = Fals
     331    DEBUG = Tru
    237332     
    238333    TITLE = "PyNN: Towards a universal neural simulator API in Python" 
     
    255350    community, and promises leaps in simulation complexity and maintainability to any neural simulator 
    256351    that adopts it. PyNN [<a href="http://neuralensemble.org/PyNN">http://neuralensemble.org/PyNN</a>] strives to provide a uniform application programming 
    257     interface (API) across neural simulators. Presently NEURON, NEST are PCSIM supported, and support for 
     352    interface (API) across neural simulators. Presently NEURON, NEST and PCSIM are supported, and support for 
    258353    other simulators, NeuroML output and neuromorphic VLSI hardware is under development. 
    259354     
     
    276371    This work is supported by the European Union through the FACETS project (contract number FP6- 
    277372    2004-IST-FETPI-15879).""" 
     373     
     374    CAPTIONS = { 
     375        'VAbenchmark_CUBA_exc.png': """<b>Results of running the CUBA benchmark script</b> (see Box: 'Example'), written in PyNN, with three different simulators (from left to right: NEURON, NEST, PCSIM). 
     376         
     377        The <b><i>top row</i></b> shows the membrane potential traces for two of the excitatory neurons in the network. 
     378        The <b><i>second row</i></b> shows the raster plots for the first 320 excitatory neurons in the network (total network size 3200 excitatory and 800 inhibitory neurons). Each dot represents one spike. Each row is a different cell. 
     379        The <b><i>third row</i></b> shows histograms of interspike intervals (ISIs) for the excitatory and inhibitory sub-populations. 
     380        The <b><i>bottom row</i></b> shows histograms of the coefficient of variation (CV) of the ISI. 
     381         
     382        For NEST and NEURON, the same sequence of random numbers was used to generate the network connectivity (using a PyNN <font face="Courier">NumpyRNG</font> object. For PCSIM, the random numbers were generated within the simulator (represented in PyNN with a <font face="Courier">NativeRNG</font> object). 
     383         
     384        Even when using exactly the same connectivity pattern, the membrane potential trajectories diverge after about the first 100 ms, although the pattern of mean activity across the network is well preserved between simulators. 
     385         
     386        With a different random connectivity (but with the same connection probability), there is no similarity in detailed activity patterns, but the statistical behaviour of the network in terms of spike train variability is the same. 
     387        """ 
     388    } 
     389         
     390         
     391     
    278392    main_font = 'Helvetica' 
    279393    margins = {'left':2*cm, 'right':2*cm, 'top':2*cm, 'bottom':2*cm} 
    280394    colsep = 2*cm 
    281395    titlesep = 2*cm 
    282     scale_factor = 3.6 
    283     styles = getStyleSheet(scale_factor) 
     396     
     397    ##base_fontSize = 36 
     398    ##styles = getStyleSheet(36, 8) 
    284399 
    285400    poster = Canvas("poster_cns2007.pdf", 
     
    295410    pagetop = poster._pagesize[1] - margins['top'] 
    296411    pageright = poster._pagesize[0] - margins['right'] 
    297     title_bottom = make_title(TITLE, AUTHORS, INSTITUTIONS, "pynn_tree2.png", "facetslogoweb.gif") 
     412     
     413    title_bottom = make_title(getStyleSheet(36, 72), TITLE, AUTHORS, INSTITUTIONS, "pynn_tree2.png", "facetslogoweb.gif") 
    298414    # logos 
    299415    institution_logos = ['facetslogoweb.gif','logo-cnrs.jpg','bccn-logo.jpg','kip_logo.gif'] 
    300416    ncol = 3 
    301417    colwidth = (pagewidth - (ncol-1)*colsep)/float(ncol) 
    302     make_abstract(ABSTRACT, margins['left'], title_bottom, colwidth, pageheight, DEBUG) 
    303     make_users_guide(margins['left']+colwidth+colsep, title_bottom, colwidth, pageheight, DEBUG) 
     418    colheight = title_bottom - margins['bottom'] 
     419    huge_url(poster._pagesize[0]/2.0, title_bottom/2.0, pagewidth) 
     420    make_abstract(getStyleSheet(36, 72),ABSTRACT, margins['left'], title_bottom, colwidth, colheight, DEBUG) 
     421    #make_users_guide(getStyleSheet(15),margins['left']+colwidth+colsep, title_bottom, colwidth, colheight, DEBUG) 
     422    make_example(getStyleSheet(10.5,36),margins['left']+colwidth+colsep, title_bottom, colwidth, colheight, DEBUG) 
     423    make_example_figure(getStyleSheet(20, 72), "VAbenchmark_CUBA_exc.png", 
     424                        CAPTIONS["VAbenchmark_CUBA_exc.png"], 
     425                        margins['left']+colwidth+colsep, title_bottom, colwidth, DEBUG) 
    304426    apiwidth = 0.25*pagewidth 
    305     make_apidocs(margins['left']+pagewidth-apiwidth, title_bottom, apiwidth, 
    306                  title_bottom-margins['bottom'], 0.8/scale_factor, DEBUG) 
     427    make_apidocs(getStyleSheet(9,36),margins['left']+pagewidth-apiwidth, title_bottom, apiwidth, 
     428                 colheight, DEBUG) 
    307429 
    308430    poster.save()