Changeset 266

Show
Ignore:
Timestamp:
10/10/2006 08:53:01 PM (3 years ago)
Author:
brian
Message:

Begin of code changes for prettier graphs. Not done.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • PhedexGraphs/trunk/grapher.py

    r175 r266  
    77from matplotlib.font_manager import FontProperties 
    88from matplotlib.ticker import FixedLocator, FixedFormatter 
     9from matplotlib.font_manager import * 
    910from sql_queries import * 
    1011from plot_helpers import * 
     12from math import ceil, floor 
     13 
     14prefs = {  
     15  'text_size' : 7,    #in pixels 
     16  'text_padding' : 2, #in pixels 
     17  'legend_padding' : .02, # In percent of screen space / 100 
     18  'figure_padding' : .08,  
     19  'width' : 800,  #in pixels 
     20  'height' : 500, 
     21  'width_inches' : 8,    # Somewhat arbitrary, as dpi is adjusted to  
     22                       # fit the pixel sizes requested above. 
     23  'columns' : 5,    # The number of columns to use in the legend  
     24  'max_rows' : 9 # Maximum number of rows in the legend 
     25} 
    1126 
    1227def make_labels( ax, text_title, xlab, ylab ): 
     
    1530  text_obj.set_weight('bold') 
    1631 
    17 def make_proper( ax, begin, end ): 
     32def pretty_graph( title, xlabel, ylabel, labels, colorpicker_cb, formatter_cb ): 
     33  """ Creates a graph w/ legend, fonts, and ticks to conform with Phedex's 
     34      existing graphs. 
     35      Returns an axes that can be used for plotting 
     36  """ 
     37  # Various size calculations 
     38  num_labels = len( labels ) 
     39  colors = colorpicker_cb( labels ) 
     40  dpi = prefs['width'] /prefs['width_inches'] 
     41  height_inches = prefs['height'] / float(dpi) 
     42  legend_width = 1 - 2 * prefs['legend_padding'] 
     43  rows = min( ceil(num_labels / float(prefs['columns'])), 9) 
     44  legend_height = (2*prefs['text_padding'] + prefs['text_size']) * prefs['max_rows']/float(prefs['height']) 
     45  leg_pix_height = legend_height * height_inches          * dpi  
     46  leg_pix_width =  legend_width  * prefs['width_inches']  * dpi 
     47  column_width = 1.0 / float( prefs['columns'] ) 
     48  column_height = (2 * prefs['text_padding'] + prefs['text_size']) / leg_pix_height 
     49  box_width = prefs['text_size'] 
     50  bottom = 2 * prefs['legend_padding'] + legend_height 
     51 
     52  # The font properties we will use: 
     53  p = FontProperties() 
     54  p.set_family('serif') 
     55  p.set_stretch('narrow') 
     56  p.set_size( prefs['text_size'] ) 
     57 
     58  # Create our figure and canvas to work with 
     59  fig = Figure() 
     60  canvas = FigureCanvas( fig ) 
     61   
     62  # Set the figure properties we derived above. 
     63  fig.set_size_inches( prefs['width_inches'], prefs['height_inches'] ) 
     64  fig.set_dpi( dpi ) 
     65 
     66  # rect = (left, bottom, width, height) 
     67  legend_rect = prefs['legend_padding'], prefs['legend_padding'], legend_width, legend_height 
     68  ax_rect = (figure_padding, figure_padding + bottom, 1 - 2*figure_padding, \ 
     69             1 - bottom - 3*figure_padding) 
     70 
     71  # Create our two axes, and set properties 
     72  ax = fig.add_axes( ax_rect ) 
     73  setp( ax.get_xticklabels(), fontproperties=p ) 
     74  setp( ax.get_yticklabels(), fontproperties=p ) 
     75  legend_ax = fig.add_axes( legend_rect ) 
     76  ax.grid( True ) 
     77  legend_ax.set_axis_off() 
     78 
     79  # Set text on main axes. 
     80  ax.set_title( title ) 
     81  ax.set_xlabel( xlabel ) 
     82  ax.set_ylabel( ylabel ) 
     83   
     84  # Create the colors: 
     85  colors = colorpicker_cb( labels ) 
     86   
     87  offset = 0 
     88  early_stop = False 
     89  for my_text, my_color in zip(labels,colors): 
     90    # Size calculations 
     91    left = (box_width+3*prefs['text_padding'])/leg_pix_width + \ 
     92            column_width*(offset % prefs['columns']) 
     93    top = 1 - column_height*floor( offset / prefs['columns'] ) 
     94    next_bottom = 1 - column_height*(1+floor((offset+1)/prefs['columns'])) 
     95     
     96    # Stop early if we ran out of room. 
     97    if next_bottom < 0 and (num_labels - offset > 1): 
     98      early_stop = True 
     99      break 
     100 
     101    # Create text 
     102    t = text( left, top, my_text, horizontalalignment='left', 
     103              verticalalignment='top') 
     104    t.set_fontproperties( p ) 
     105 
     106    # Create legend rectangle: 
     107    patch = Rectangle( (column_width*(offset % prefs['columns']) + \ 
     108                        prefs['text_padding']/leg_pix_width,  
     109                        top - box_width/leg_pix_height),  
     110                        box_width/leg_pix_width, box_width/leg_pix_height ) 
     111    patch.set_ec('black') 
     112    patch.set_fc( my_color ) 
     113    legend_ax.add_patch( patch ) 
     114    offset += 1 
     115 
     116  # Set some additional text if we stopped early 
     117  if early_stop == True: 
     118    my_text = '... plus %i more' % (num_labels - offset) 
     119    text( left, top, my_text, horizontalalignment='left', verticalalignment='top', 
     120          size = prefs['text_size'] ) 
     121 
     122  formatter_cb( ax ) 
     123 
     124  return ax, fig, canvas , colors 
     125 
     126 
     127def datetime_formatter( ax, begin, end ): 
    18128  begin_num = datetime.datetime.utcfromtimestamp( begin ) 
    19129  end_num = datetime.datetime.utcfromtimestamp( end ) 
     
    27137  labels = ax.get_xticklabels() 
    28138  setp(labels, rotation=45,horizontalalignment='right', size='smaller') 
     139 
     140def random_colors( labels ): 
     141  colors = [(random.random(), random.random(), random.random()) for label in labels] 
     142  return colors 
    29143 
    30144def make_figure( size=(8,4), subplots=111 ): 
     
    62176  my_times = points.keys(); my_times.sort() 
    63177  polygons = [] 
    64   #for idx in range(len(my_times[:-1])): 
    65   #  cur_time, next_time = my_times[idx], my_times[idx+1] 
    66   #  left = cur_time 
    67   #  right = next_time 
    68   #  if not bottom.has_key( cur_time  ): bottom[cur_time ] = 0 
    69   #  if not bottom.has_key( next_time ): bottom[next_time] = 0 
    70   #  left_bottom = bottom[cur_time] 
    71   #  bottom[cur_time] += points[cur_time]  
    72   #  left_top = bottom[cur_time] 
    73   #  right_bottom = bottom[next_time] 
    74   #  right_top = bottom[next_time] + points[next_time] 
    75   #  seq = [ (left, left_bottom), (left, left_top), (right, right_top), (right, right_bottom), (left, left_bottom) ] 
    76   #  poly = Polygon( seq, facecolor=color, fill=True, linewidth=0.0 ) 
    77   #  ax.add_patch( poly ) 
    78   #  polygons.append( poly ) 
    79178  seq = [] 
    80179  next_bottom = {} 
     
    101200                  # in the legend produced separately. 
    102201 
    103 def make_quantity( from_node, to_node, begin, end, orcl, file, kind="link" ):  
     202def make_quantity( from_node, to_node, begin, end, orcl, file, file2, kind="link" ):  
    104203  rc('grid', color=0.75, linestyle='-')  
    105204  results, amt_results = quantity_query( from_node, to_node, begin, end, orcl, kind ) 
    106   num_links = len(results.keys()) 
    107   # Get new axis and canvas to work with: 
    108   if num_links > legend_cutoff: # Make a separate  
    109                                 # legend when there are lots of links 
    110     adjust_factor = max((num_links - 48)/4*.3, 0) 
    111     ax, canvas, fig = make_figure(subplots=221, size=(8,8 + 5*adjust_factor) ) 
    112     ax2 = fig.add_subplot(223) 
    113     fig.subplots_adjust( wspace=0.0 ) 
    114     #bottom_space = 0.15 + adjust_factor 
    115     bottom_space = 0.15 
    116   else: 
    117     ax, canvas, fig = make_figure(subplots=211, size=(8,8)) 
    118     ax2 = fig.add_subplot( 212 ) 
    119     bottom_space = 0.15 
    120   #print bottom_space 
    121   fig.subplots_adjust(bottom = bottom_space, hspace=0.49) 
    122   bottom, bottom2 = None, None 
     205 
     206  # Options for the graphs 
     207  title = "PhEDEx Data Transfers By %s" % kind 
     208  xlabel = "Time" 
     209  ylabel = "Data Transferred (MB/s)" 
     210  ylabel2 = "Data Transferred (TB)" 
     211  color_picker = random_colors 
     212  def my_formatter( ax ): 
     213    datetime_formatter( ax, begin, end )  
     214 
     215  # Figure out the labels: 
     216  labels = [] 
     217  for link in results.keys(): 
     218    if kind == 'link':  
     219      labels.append(link[1] + ' to ' + link[0])  
     220    else: 
     221      labels.append( link ) 
     222     
     223  # Plot 1 
     224  ax, canvas, fig, colors = pretty_graph( title, xlabel, ylabel, labels, color_picker, my_formatter )  
     225 
     226  # Plot 2 
     227  ax2, canvas2, fig2, colors2 = pretty_graph( title, xlabel, ylabel2, labels, color_picker, my_formatter ) 
     228 
    123229  if results == {}: print "No graph to plot!"; return None 
    124230  legend_text = []; legend_boxes = [] 
     231 
     232  bottom, bottom2 = None, None 
    125233  for link in results.keys(): 
    126234    color = (random.random(), random.random(), random.random()) 
     
    128236    bottom, bars = make_bar( ax, results[link], begin, end, bottom, color ) 
    129237    bottom2, bars = make_bar( ax2,amt_results[link], begin, end, bottom2, color ) 
    130     if bars != None: 
    131       if kind == "link": 
    132         legend_text.append(link[1] + ' to ' + link[0])  
    133       else: 
    134         legend_text.append( link ) 
    135       legend_boxes.append( bars[0] ) 
    136238  legend_boxes.reverse(); legend_text.reverse() 
    137   make_labels(ax, "PhEDEx SC4 Data Transfers Rate By " + kind, "Time", "Data Transferred (MB/s)") 
    138   make_labels(ax2,"PhEDEx SC4 Data Transfers By " + kind,"Time","Data Transferred (TB)") 
    139   if num_links > legend_cutoff: 
    140     ax_leg = fig.add_subplot(222) 
    141     ax_leg.axis('off') 
    142     leg = ax_leg.legend( legend_boxes, legend_text, prop = FontProperties(size=8), ) 
    143   else: 
    144     leg = ax.legend( legend_boxes, legend_text, "upper left", prop = FontProperties(size=8), ); leg.set_zorder( 3 ) 
    145     leg =ax2.legend( legend_boxes, legend_text, "upper left", prop = FontProperties(size=8), ) 
    146   leg.set_zorder( 3 ) 
    147   for axes in [ax, ax2]: 
    148     make_proper( axes, begin, end ) 
    149   #canvas.print_figure( file, dpi=100 ) 
    150   print_figure( canvas, file, dpi=100 ) 
     239  print_figure( canvas, file, dpi=prefs['width']/prefs['width_inches'] ) 
     240  print_figure( canvas2,file2,dpi=prefs['width']/prefs['width_inches'] ) 
    151241 
    152242 
  • PhedexGraphs/trunk/make_quantity.py

    r177 r266  
    3838    print e 
    3939    sys.exit(1) 
     40  my_file = option.output[:-len(option.output.split('.')[-1])+1] 
     41  my_ext =  option.output.split('.')[-1] 
    4042  try: 
    41     file = open(option.output, 'w') 
     43    file = open(my_file + '.' + my_ext, 'w') 
     44    file2= open(my_file + '_2.' + my_ext, 'w')   
    4245  except: 
    4346    print "Cannot open the output file (", option.output, ") for writing!" 
    4447    sys.exit(1) 
    45   grapher.make_quantity( option.from_node, option.to_node, option.start, option.end, orcl, file, option.link )  
     48  grapher.make_quantity( option.from_node, option.to_node, option.start, option.end, orcl, file, file2, option.link )  
    4649