| 17 | | def make_proper( ax, begin, end ): |
|---|
| | 32 | def 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 | |
|---|
| | 127 | def datetime_formatter( ax, begin, end ): |
|---|
| 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 ) |
|---|
| 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 | |
|---|
| 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'] ) |
|---|