Changeset 564 for branches


Ignore:
Timestamp:
09/20/12 10:20:03 (4 years ago)
Author:
mmckerns
Message:

cone plotter now a more generic script, takes commandline input;
replaces selected coords with values, instead of values always along same axis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/UQ/math/legacy/plot_data.py

    r563 r564  
    11#!/usr/bin/env python 
     2__doc__ = """ 
     3support_hypercube_scenario.py [options] filename datafile 
     4 
     5generate scenario support plots from file written with 'write_support_file' 
     6and a dataset file 
     7 
     8The options "bounds", "axes", and "iters" all take indicator strings. 
     9The bounds should be given as a quoted list of tuples.  For example, using 
     10bounds = "[(.062,.125),(0,30),(2300,3200)]" will set the lower and upper bounds 
     11for x to be (.062,.125), y to be (0,30), and z to be (2300,3200). Similarly, 
     12axes also accepts a quoted list of tuples; however, for axes, the first tuple 
     13indicates which parameters are along the x direction, the second tuple for 
     14the y direction, and the third tuple for the z direction. Thus, axes = 
     15"[(2,3),(6,7),(10,11)]" would set the 2nd and 3rd parameters along x. Iters, 
     16however, accepts a list of strings instead of a list of tuples. For example, 
     17iters = "[':']" will plot all iters in a single plot. Alternatively, 
     18iters = "[':2','2:']" will split the iters into two plots, while 
     19iters = "['0']" will only plot the first iteration. 
     20 
     21The option "label" takes a list of strings. For example, label = "['x','y','']" 
     22will place 'x' on the x-axis, 'y' on the y-axis, and nothing on the z-axis. 
     23LaTeX is also accepted. For example, label = "[r'$ h $',r'$ {\alpha}$',r'$ v$']" 
     24will label the axes with standard LaTeX math formatting. Note that the leading 
     25space is required, while the trailing space aligns the text with the axis 
     26instead of the plot frame. 
     27 
     28Required Inputs: 
     29  filename            name of the python convergence logfile (e.g. paramlog.py) 
     30  datafile            name of the dataset textfile (e.g. StAlDataset.txt) 
     31""" 
    232ZERO = 1.0e-6  # zero-ish 
    333 
     
    6797 
    6898#### plotting the cones #### 
    69 def plot_cones(ax, data, slope, bounds, color='0.75'): 
     99def plot_cones(ax, data, slope, bounds, color='0.75', axis=-1): 
    70100  """plot double cones for a given dataset 
    71101 
     
    75105  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    76106  color -- string name (or rbg value) of color to use for datapoints 
    77 """ 
     107  axis -- the axis of the cone 
     108""" 
     109  # adjust slope, bounds, and data so cone axis is last  
     110  slope = swap(slope, axis)  
     111  bounds = swap(bounds, axis)  
     112  data = [swap(pt,axis) for pt in data] 
    78113  cone = cone_builder(slope, bounds) 
    79114  # plot the upper and lower cone 
     
    81116    _cone = cone(datapt, top=True) 
    82117    if _cone: 
    83       X,Z,Y = _cone 
     118      X,Z,Y = swap(_cone, xs) # 'unswap' the axes 
    84119      ax.plot_surface(X, Z,Y, rstride=1, cstride=1, color=color)  
    85120    _cone = cone(datapt, top=False) 
    86121    if _cone: 
    87       X,Z,Y = _cone 
     122      X,Z,Y = swap(_cone, xs) # 'unswap' the axes 
    88123      ax.plot_surface(X, Z,Y, rstride=1, cstride=1, color=color) 
    89124  return ax 
    90125 
    91 def plot_data(ax, data, bounds, color='red'): 
     126def plot_data(ax, data, bounds, color='red', axis=-1): 
    92127  """plot datapoints for a given dataset 
    93128 
     
    96131  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    97132  color -- string name (or rbg value) of color to use for datapoints 
     133  axis -- the axis of the cone 
    98134""" 
    99135  strict = True # always respect the bounds 
     
    125161  return ax 
    126162 
    127 def reduce_dimension(axis, coords, values, slope, bounds): 
     163def label_axes(ax, labels): 
     164  """ label plots with given string labels 
     165 
     166  ax -- matplotlib 'Axes3D' plot object 
     167  labels -- list of string labels for the plot 
     168""" 
     169  ax.set_xlabel(labels[0]) 
     170  ax.set_zlabel(labels[1]) 
     171  ax.set_ylabel(labels[2]) # cone "center" axis 
     172  return ax 
     173 
     174def get_coords(data, replace=None): 
    128175  """ replace one coordiate axis in a 3-D data set with 'data values' 
    129176  (i.e. replace an 'x' axis with the 'y' values of the data) 
    130177 
    131   axis -- selected coordinate axis (an int) to replace with 'y' values 
    132   coords -- list of datapoint coordinates, where coordinates are 3-tuples 
    133   values -- list of datapoint values, where values are floats 
    134   slope -- slope multiplier for cone on the X,Y,Z axes 
    135   bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    136  
    137 NOTE: for the axis indicated by 'axis', replace 
    138    - selected axis bounds with Y-bounds 
    139    - selected coords with values 
    140    - selected slope with 1.0 
     178  data -- dataset object, where coordinates are 3-tuples and values are floats 
     179  replace -- selected axis (an int) to plot values NOT coords 
    141180  """ 
    142   if axis not in range(len(bounds[:-1])):  # don't replace an axis 
    143     return coords, slope[:-1], bounds[:-1]  
    144   xs = axis 
    145   bounds = bounds[:xs] + bounds[xs+1:] 
    146   slope = slope[:xs] + slope[xs+1:] 
    147   coords = [coords[i][:xs] + coords[i][xs+1:] + [values[i]] \ 
    148                              for i in range(len(coords))] 
    149   return coords, slope, bounds 
    150  
    151  
    152 if __name__ == '__main__': 
    153   ### FLAGS ############ 
    154   # get selected data set (filename) 
    155   # get lower and upper bounds for plot view 
    156   # get selected (1 or 2) axes to plot  #FIXME: need to enable 2-D plots 
    157   ###################### 
    158   from legacydata import load_dataset 
    159   data = load_dataset('StAlDataset.txt') 
    160   w_lower = [0.0]; Y_lower = [0.0] 
    161   w_upper = [1.0]; Y_upper = [40.0] # was 100 
    162   h_lower = [0.062]; a_lower = [0.0];  v_lower = [2300.0] 
    163   h_upper = [0.125]; a_upper = [30.0]; v_upper = [3200.0] 
    164   axis_to_replace = xs = 0    #XXX: select axis NOT to plot here 
    165  
    166   # temporarily 'inflate' bounds and slopes to include values 
    167   lb = h_lower + a_lower + v_lower + Y_lower 
    168   ub = h_upper + a_upper + v_upper + Y_upper 
    169   bounds = zip(lb,ub) 
    170   slope = data.lipschitz + [1.0] 
     181  slope = data.lipschitz 
    171182  coords = data.coords 
    172183  values = data.values 
    173   # replace selected coords axis; 'reduce' the size of bounds and slopes 
    174   coords, slope, bounds = reduce_dimension(xs, coords, values, slope, bounds) 
    175  
     184  if replace not in range(len(slope)):  # don't replace an axis 
     185    return coords, slope 
     186  slope = slope[:replace] + [1.0] + slope[replace+1:] 
     187  coords = [coords[i][:replace] + [values[i]] + coords[i][replace+1:] \ 
     188                              for i in range(len(coords))] 
     189  return coords, slope 
     190 
     191def reduce(alist, axis=-1): 
     192  """ reduce a n-member list to a (n-1)-member list 
     193 
     194  alist -- a list 
     195  axis -- selected member (an int) to remove 
     196  """ 
     197  if axis not in range(len(alist[:-1])):  # reduce the last axis 
     198    return alist[:-1]  
     199  return alist[:axis] + alist[axis+1:] 
     200 
     201def swap(alist, axis=-1): 
     202  if axis not in range(len(alist)):  # don't swap an axis 
     203    return alist  
     204  return alist[:axis] + alist[axis+1:] + alist[axis:axis+1] 
     205 
     206 
     207if __name__ == '__main__': 
     208  #FIXME: need to enable 2-D plots 
     209 
     210  #XXX: note that 'argparse' is new as of python2.7 
     211  from optparse import OptionParser 
     212  parser = OptionParser(usage=__doc__) 
     213  parser.add_option("-b","--bounds",action="store",dest="bounds",\ 
     214                    metavar="STR",default="[(0,1),(0,1),(0,1)]", 
     215                    help="indicator string to set hypercube bounds") 
     216# parser.add_option("-x","--axes",action="store",dest="xyz",\ 
     217#                   metavar="STR",default="[(0,),(1,),(2,)]", 
     218#                   help="indicator string to assign parameter to axis") 
     219  parser.add_option("-l","--label",action="store",dest="label",\ 
     220                    metavar="STR",default="['','','']", 
     221                    help="string to assign label to axis") 
     222  parser.add_option("-r","--replace",action="store",dest="replace",\ 
     223                    metavar="INT",default=None, 
     224                    help="id # of axis to plot values NOT coords") 
     225  parsed_opts, parsed_args = parser.parse_args() 
     226 
     227# try:  # get the name of the parameter log file 
     228#   file = parsed_args[0] 
     229#   import re 
     230#   file = re.sub('\.py*.$', '', file)  #XXX: strip off .py* extension 
     231# except: 
     232#   raise IOError, "please provide log file name" 
     233# try:  # read standard logfile 
     234#   from mystic.munge import logfile_reader, raw_to_support 
     235#   _step, params, _cost = logfile_reader(file) 
     236#   params, _cost = raw_to_support(params, _cost) 
     237# except: 
     238#   exec "from %s import params" % file 
     239#   #exec "from %s import meta" % file 
     240#   # would be nice to use meta = ['wx','wx2','x','x2','wy',...] 
     241 
     242  try:  # get the name of the dataset file 
     243    file = parsed_args[0] 
     244    from legacydata import load_dataset 
     245    data = load_dataset(file) 
     246  except: 
     247    raise IOError, "please provide dataset file name" 
     248 
     249  try: # select the bounds 
     250    bounds = eval(parsed_opts.bounds)  # format is "[(60,105),(0,30),(2.1,2.8)]" 
     251  except: 
     252    bounds = [(0,1),(0,1),(0,1)] 
     253 
     254  try: # select labels for the axes 
     255    label = eval(parsed_opts.label)  # format is "['x','y','z']" 
     256  except: 
     257    label = ['','',''] 
     258 
     259  try: # select which axis to plot 'values' on 
     260    xs = int(parsed_opts.replace) 
     261  except: 
     262    xs = None # don't plot values; plot values on 'x' axis with xs = 0 
     263 
     264  # ensure all terms of bounds and xyz are tuples 
     265  for bound in bounds: 
     266    if not isinstance(bound, tuple): 
     267      raise TypeError, "bounds should be tuples of (lower_bound,upper_bound)" 
     268# for i in range(len(xyz)): 
     269#   if isinstance(xyz[i], int): 
     270#     xyz[i] = (xyz[i],) 
     271#   elif not isinstance(xyz[i], tuple): 
     272#     raise TypeError, "xyz should be tuples of (param1,param2,param3,...)" 
     273 
     274  # get coords (and values) for selected axes 
     275  coords, slope = get_coords(data, xs)  #FIXME: requires Y-value to be last 
    176276  print "bounds: %s" % bounds 
    177277  print "slope: %s" % slope 
    178278 #print "coords: %s" % coords 
     279 
     280  # use the default bounds where not specified 
     281  bounds = [list(i) for i in bounds] 
     282  for i in range(len(bounds)): 
     283    if bounds[i][0] is None: bounds[i][0] = 0 
     284    if bounds[i][1] is None: bounds[i][1] = 1 
    179285 
    180286  import matplotlib  
     
    184290  ax = Axes3D(fig)#,aspect='equal')  
    185291  if xs in range(len(bounds)):  # we are replacing an axis 
    186     plot_cones(ax, coords, slope, bounds) 
    187   plot_data(ax, coords, bounds) 
     292    plot_cones(ax, coords, slope, bounds, axis=xs) 
     293  plot_data(ax, coords, bounds, axis=xs) 
    188294 #clip_axes(ax, bounds) 
     295  label_axes(ax, label) 
    189296  plt.show()  
    190297 
Note: See TracChangeset for help on using the changeset viewer.