Changeset 562 for branches


Ignore:
Timestamp:
09/18/12 14:07:03 (4 years ago)
Author:
mmckerns
Message:

better bounds clipping for datapoints; allow coneless plotting of data

File:
1 edited

Legend:

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

    r546 r562  
    33 
    44#### building the cone primitive #### 
    5 def cone_builder(slope, lb, ub): 
     5def cone_builder(slope, bounds): 
    66  """ factory to create a cone primitive 
    77 
    88  slope -- slope multiplier for cone on the X,Y,Z axes (for mesh construction) 
    9   lb -- tuple of lower bounds for the plot view 
    10   ub -- tuple of upper bounds for the plot view 
     9  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    1110""" 
    1211  from mystic.math import almostEqual 
     
    3534    return X,Z,Y 
    3635 
     36  lb,ub = zip(*bounds) 
     37  # if False, the cone surface may violate bounds 
     38  strict = True # always respect the bounds 
     39 
    3740  def cone(position, top=True): 
    3841    """ construct a cone primitive, with vertex at given position 
     
    4548    d_hi = ub - position 
    4649    d_lo = lb - position 
    47     # check if out of bounds 
    48     if any(d_hi < 0): return None  #FIXME: poor design 
    49     if any(d_lo > 0): return None  #FIXME: poor design 
     50    if strict and (any(d_hi < 0) or any(d_lo > 0)): 
     51      return None  # don't plot cone if vertex is out of bounds 
    5052 
    5153    if top: # distance of vertex from upper edge 
     
    6567 
    6668#### plotting the cones #### 
    67 def plot_cones(ax, data, slope, lb, ub): 
     69def plot_cones(ax, data, slope, bounds): 
    6870  """plot double cones for a given dataset 
    6971 
     
    7173  data -- list of datapoints, where datapoints are 3-tuples (i.e. x,y,z) 
    7274  slope -- slope multiplier for cone on the X,Y,Z axes (for mesh construction) 
    73   lb -- tuple of lower bounds for the plot view 
    74   ub -- tuple of upper bounds for the plot view 
     75  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    7576""" 
    76   cone = cone_builder(slope, lb, ub) 
     77  cone = cone_builder(slope, bounds) 
    7778  # plot the upper and lower cone 
    7879  for datapt in data: 
     
    8788  return ax 
    8889 
    89 def plot_data(ax, data): 
     90def plot_data(ax, data, bounds): 
    9091  """plot datapoints for a given dataset 
    9192 
    9293  ax -- matplotlib 'Axes3D' plot object 
    9394  data -- list of datapoints, where datapoints are 3-tuples (i.e. x,y,z) 
     95  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    9496""" 
     97  strict = True # always respect the bounds 
     98  lb,ub = zip(*bounds) 
    9599  # plot the datapoints themselves 
     100  from numpy import asarray 
    96101  for datapt in data: 
    97     ax.plot([datapt[0]], [datapt[2]], [datapt[1]], \ 
    98             color='r', marker='o', markersize=10) 
     102    position = asarray(datapt) 
     103    d_hi = ub - position 
     104    d_lo = lb - position 
     105    if strict and (any(d_hi < 0) or any(d_lo > 0)): 
     106      pass  # don't plot if datapt is out of bounds 
     107    else: 
     108      ax.plot([datapt[0]], [datapt[2]], [datapt[1]], \ 
     109              color='r', marker='o', markersize=10) 
    99110  return ax 
    100111 
    101 def clip_axes(ax, lb, ub): 
     112def clip_axes(ax, bounds): 
    102113  """ clip plots to be within given lower and upper bounds 
    103114 
    104115  ax -- matplotlib 'Axes3D' plot object 
    105   lb -- tuple of lower bounds for the plot view 
    106   ub -- tuple of upper bounds for the plot view 
     116  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    107117""" 
     118  lb,ub = zip(*bounds) 
    108119  # plot only within [lb,ub] 
    109120  ax.set_xlim3d(lb[0], ub[0]) 
     
    112123  return ax 
    113124 
    114 def reduce_dimension(axis, coords, values, slope, lb, ub): 
     125def reduce_dimension(axis, coords, values, slope, bounds): 
    115126  """ replace one coordiate axis in a 3-D data set with 'data values' 
    116127  (i.e. replace an 'x' axis with the 'y' values of the data) 
     
    120131  values -- list of datapoint values, where values are floats 
    121132  slope -- slope multiplier for cone on the X,Y,Z axes 
    122   lb -- tuple of lower bounds for the plot view 
    123   ub -- tuple of upper bounds for the plot view 
     133  bounds -- list of tuples of bounds for the plot; (lower,upper) for each axis 
    124134 
    125135NOTE: for the axis indicated by 'axis', replace 
     
    128138   - selected slope with 1.0 
    129139  """ 
     140  if axis not in range(len(bounds[:-1])):  # don't replace an axis 
     141    return coords, slope[:-1], bounds[:-1]  
    130142  xs = axis 
    131   lb = lb[:xs] + lb[xs+1:] 
    132   ub = ub[:xs] + ub[xs+1:] 
     143  bounds = bounds[:xs] + bounds[xs+1:] 
    133144  slope = slope[:xs] + slope[xs+1:] 
    134145  coords = [coords[i][:xs] + coords[i][xs+1:] + [values[i]] \ 
    135146                             for i in range(len(coords))] 
    136   return coords, slope, lb, ub 
     147  return coords, slope, bounds 
    137148 
    138149 
     
    149160  h_lower = [0.062]; a_lower = [0.0];  v_lower = [2300.0] 
    150161  h_upper = [0.125]; a_upper = [30.0]; v_upper = [3200.0] 
    151   axis_to_replace = xs = 0  #XXX: select axis NOT to plot here 
     162  axis_to_replace = xs = 0    #XXX: select axis NOT to plot here 
    152163 
     164  # temporarily 'inflate' bounds and slopes to include values 
    153165  lb = h_lower + a_lower + v_lower + Y_lower 
    154166  ub = h_upper + a_upper + v_upper + Y_upper 
    155  
     167  bounds = zip(lb,ub) 
    156168  slope = data.lipschitz + [1.0] 
    157169  coords = data.coords 
    158170  values = data.values 
     171  # replace selected coords axis; 'reduce' the size of bounds and slopes 
     172  coords, slope, bounds = reduce_dimension(xs, coords, values, slope, bounds) 
    159173 
    160   coords, slope, lb, ub = reduce_dimension(xs, coords, values, slope, lb, ub) 
    161  
    162   print "lower: %s" % lb 
    163   print "upper: %s" % ub 
     174  print "bounds: %s" % bounds 
    164175  print "slope: %s" % slope 
    165176 #print "coords: %s" % coords 
     
    170181  fig = plt.figure()  
    171182  ax = Axes3D(fig)#,aspect='equal')  
    172   plot_cones(ax, coords, slope, lb, ub) 
    173   plot_data(ax, coords) 
    174  #clip_axes(ax, lb, ub) 
     183  if xs in range(len(bounds)):  # we are replacing an axis 
     184    plot_cones(ax, coords, slope, bounds) 
     185  plot_data(ax, coords, bounds) 
     186  clip_axes(ax, bounds) 
    175187  plt.show()  
    176188 
Note: See TracChangeset for help on using the changeset viewer.