Package PyDSTool :: Package Toolbox :: Module prep_boxplot
[hide private]
[frames] | no frames]

Source Code for Module PyDSTool.Toolbox.prep_boxplot

  1  from PyDSTool import remain, loadObjects, array, save_fig, arange, args 
  2  from matplotlib.font_manager import FontProperties 
  3  from PyDSTool.matplotlib_import import * 
  4  from scipy import mean 
  5   
  6  ##symbol_map = {'isomap': { 
  7  ##                    'K': {10: 'k<', 50: 'k>', 100: 'k^', 500: 'kv'}, 
  8  ##                    'eps': {20: 'w<', 50: 'w>', 100: 'w^'} 
  9  ##                        }, 
 10  ##              'pca': { 
 11  ##                  'knee': {1: 'ws', 2: 'ks'}, 
 12  ##                  'var': {80: 'wo', 90: 'ko'} 
 13  ##                     } 
 14  ##              } 
 15   
 16  symbol_map = {'isomap': { 
 17                      'K': {10: ('ws', 'E'), 50: ('ws', 'F'), 
 18                            100: ('ws', 'G'), 500: ('ws', 'H')}, 
 19                      'eps': {20: ('ws', 'I'), 50: ('ws', 'J'), 
 20                              100: ('ws', 'K')} 
 21                          }, 
 22                'pca': { 
 23                    'knee': {1: ('wo', 'A'), 2: ('wo', 'B')}, 
 24                    'var': {80: ('wo', 'C'), 90: ('wo', 'D')} 
 25                       } 
 26                } 
 27   
 28   
 29  # For use with PD-E analysis 
30 -def prep_boxplots(data, xlabel_str, figname='', fignum=1, do_legend=1, 31 means=1, xlegoff=0, ylegstep=1, ylegoff=0, spacing=None):
32 spacing_actual = { 33 'width': 0.1, 34 'wgapfac': 0.75, 35 'markersize': 12, 36 'off_fac': 0.7, 37 'x_step': 0.9, # 9 * width 38 'x_off': 0, 39 'box_to_marker': 1.1, 40 'notch_size': 0.2} 41 if spacing is not None: 42 spacing_actual.update(spacing) 43 width = spacing_actual['width'] 44 wgapfac = spacing_actual['wgapfac'] 45 markersize = spacing_actual['markersize'] 46 off_fac = spacing_actual['off_fac'] 47 x_step = spacing_actual['x_step'] 48 x_off = spacing_actual['x_off'] 49 box_to_marker = spacing_actual['box_to_marker'] 50 notch_size = spacing_actual['notch_size'] 51 52 n = len(data) 53 x_min = -width*3.8 #3.75 54 x_max = (n-1)*x_step+width*4.5 #3.75 55 if n > 1: 56 halfticks = arange(1,n)*x_step-x_step/2 57 figure(fignum) 58 # work out ordering of data from 'pos' key 59 order = {} 60 # `pos` position runs from 1 to n, `ns` runs from 0 to n-1 61 ns = [] 62 for k, v in data.iteritems(): 63 order[v['pos']] = k 64 ns.append(v['pos']-1) 65 ns.sort() 66 assert ns == range(n) 67 maxD = 0 68 max_dimval_markers = 0 69 labels = [] 70 for pos in range(n): 71 name = order[pos+1] 72 pde_name = 'PD_E-'+name 73 if 'known_dim' in data[name]: 74 if n == 1: 75 kdx1 = x_min 76 kdx2 = x_max 77 else: 78 if pos == 0: 79 kdx1 = x_min 80 kdx2 = halfticks[0] 81 elif pos == n-1: 82 kdx1 = halfticks[n-2] 83 kdx2 = x_max 84 else: 85 kdx1 = halfticks[pos-1] 86 kdx2 = halfticks[pos] 87 plot([[kdx1], [kdx2]], 88 [data[name]['known_dim'],data[name]['known_dim']], 89 'k', linewidth=1, zorder=0) 90 slope_data = loadObjects(pde_name)[2] 91 ds_mins = array(slope_data[:,0])#,shape=(len(slope_data),1)) 92 ds_mins.shape=(len(slope_data),1) 93 ds_maxs = array(slope_data[:,1])#,shape=(len(slope_data),1)) 94 ds_maxs.shape=(len(slope_data),1) 95 max_ds = max([max(ds_mins[:,0]),max(ds_maxs[:,0])]) 96 if max_ds > maxD: 97 maxD = max_ds 98 # limits args are ineffective here 99 boxplot(ds_mins,positions=[pos*x_step-width*wgapfac+x_off],whis=100, 100 means=means,monochrome=True,notch=2,notchsize=notch_size, 101 limits=(),widths=width,fill=1) 102 boxplot(ds_maxs,positions=[pos*x_step+width*wgapfac+x_off],whis=100, 103 means=means,monochrome=True,notch=2,notchsize=notch_size, 104 limits=(),widths=width,fill=1) 105 if pos == 0: 106 fa = figure(fignum).axes[0] 107 fa.hold(True) 108 if means: 109 ds_all_mean = (mean(ds_mins[:,0])+mean(ds_maxs[:,0]))/2 110 plot([pos*x_step+x_off], [ds_all_mean], 'k^', 111 markersize=markersize-2) 112 pca_x = pos*x_step-width*(wgapfac+box_to_marker)+x_off 113 isomap_x = pos*x_step+width*(wgapfac+box_to_marker)+x_off 114 pca_ds = {} 115 isomap_ds = {} 116 try: 117 pca_data = data[name]['pca'] 118 except KeyError: 119 pca_data = [] 120 pca_ds, max_dimval_pca, pca_used = plot_markers(pca_data, 121 pca_x, 'PCA', 122 symbol_map['pca'], -1, 123 width, off_fac, markersize) 124 if max_dimval_pca > maxD: 125 maxD = max_dimval_pca 126 if max_dimval_pca > max_dimval_markers: 127 max_dimval_markers = max_dimval_pca 128 try: 129 isomap_data = data[name]['isomap'] 130 except KeyError: 131 isomap_data = [] 132 isomap_ds, max_dimval_iso, isomap_used = plot_markers(isomap_data, 133 isomap_x, 'Isomap', 134 symbol_map['isomap'], 1, 135 width, off_fac, markersize) 136 if max_dimval_iso > maxD: 137 maxD = max_dimval_iso 138 if max_dimval_iso > max_dimval_markers: 139 max_dimval_markers = max_dimval_iso 140 labels.append(data[name]['label']) 141 ## legend 142 if do_legend: 143 font = FontProperties() 144 font.set_family('sans-serif') 145 font.set_size(11) 146 x_legend = x_min + 3*width/4 + xlegoff 147 y_legend = maxD+ylegoff 148 # pca legend 149 for k, s in pca_used: 150 plot_markers([(k,s,y_legend)], x_legend, 'Legend', symbol_map['pca'], 151 1, width, off_fac, markersize) 152 if k == 'var': 153 legstr = "%s=%d%%"%(k,s) 154 else: 155 legstr = "%s=%d"%(k,s) 156 text(x_legend+3*width/4, y_legend-width*2., legstr, 157 fontproperties=font) 158 y_legend -= ylegstep 159 # isomap legend 160 isomap_leg_data = [] 161 for k, s in isomap_used: 162 if y_legend-width*2. <= max_dimval_markers + 2: 163 y_legend = maxD+ylegoff 164 x_legend += x_step #-width*.75 165 plot_markers([(k,s,y_legend)], x_legend, 'Legend', symbol_map['isomap'], 166 1, width, off_fac, markersize) 167 ## if k == 'eps': 168 ## kstr = '\\epsilon' 169 ## else: 170 ## kstr = k 171 text(x_legend+3*width/4, y_legend-width*2., "%s=%d"%(k,s), 172 fontproperties=font) 173 y_legend -= ylegstep 174 ## tidy up axes, etc. 175 fa.set_xticks(arange(n)*x_step) 176 if n>1: 177 for h in range(n-1): 178 plot([halfticks[h], halfticks[h]], [0,maxD+1+ylegoff], 'k:') 179 fa.set_xticklabels(labels) 180 fa.set_position([0.07, 0.11, 0.9, 0.85]) 181 fa.set_xlim(x_min,x_max) 182 fa.set_ylim(0,maxD+1+ylegoff) 183 if xlabel_str != '': 184 xlabel(r'$\rm{'+xlabel_str+r'}$',args(fontsize=20,fontname='Times')) 185 ylabel(r'$\rm{Dimension}$',args(fontsize=20,fontname='Times')) 186 draw() 187 if figname != '': 188 save_fig(fignum, figname)
189 190
191 -def plot_markers(data, x_base, name, map, xoff_sgn, width, off_fac, 192 markersize):
193 maxD = 0 194 ds = {} 195 used = [] 196 font = FontProperties() 197 font.set_family('sans-serif') 198 font.set_size(10) 199 for (kind, subkind, dimval) in data: 200 try: 201 symb, lab = map[kind][subkind] 202 except KeyError: 203 raise KeyError("Invalid key for %s symbols"%name) 204 used.append((kind, subkind)) 205 try: 206 ds[dimval] += 1 207 x_off = xoff_sgn*width*off_fac*(ds[dimval]-1) 208 except KeyError: 209 ds[dimval] = 1 210 x_off = 0 211 plot([x_base+x_off], [dimval], symb, markersize=markersize) 212 # hack tweaks 213 ## if lab=='C': 214 ## x_off -= width/15 215 if lab=='A': 216 x_off += width/30 217 text(x_base+x_off-width*.15, dimval-width*2., lab, 218 fontproperties=font) 219 if dimval > maxD: 220 maxD = dimval 221 return ds, maxD, used
222