Changeset 747
- Timestamp:
- 08/11/14 17:58:45 (22 months ago)
- Location:
- mystic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/munge.py
r742 r747 30 30 elif isinstance(source, Null): 31 31 return [],[] #XXX: or source.x, source.y (e.g. Null(),Null())? or Error? 32 else: #XXX: what about taking a logfile instance?32 else: 33 33 raise IOError("a history filename or instance is required") 34 34 try: # read standard logfile (or monitor) … … 43 43 except: #KeyError 44 44 _step, params, cost = logfile_reader(source) 45 #FIXME: doesn't work for multi-id logfile; select id? 45 46 params, cost = raw_to_support(params, cost) 46 47 except: -
mystic/scripts/mystic_model_plotter.py
r746 r747 10 10 11 11 generate surface contour plots for model, specified by full import path 12 generate model trajectory from logfile , if provided12 generate model trajectory from logfile (or solver restart file), if provided 13 13 14 14 The option "bounds" takes an indicator string, where the bounds should … … 49 49 50 50 from mystic.munge import read_history 51 from mystic.munge import logfile_reader, raw_to_support 52 53 #XXX: better if reads single id only? (e.g. same interface as read_history) 54 def get_history(source, ids=None): 55 """get params and cost from the given source 56 57 source is the name of the trajectory logfile (or solver instance) 58 if provided, ids are the list of 'run ids' to select 59 """ 60 try: # if it's a logfile, it might be multi-id 61 step, param, cost = logfile_reader(source) 62 except: # it's not a logfile, so read and return 63 param, cost = read_history(source) 64 return [param],[cost] 65 66 # split (i,id) into iteration and id 67 multinode = len(step[0]) - 1 #XXX: what if step = []? 68 if multinode: id = [i[1] for i in step] 69 else: id = [0 for i in step] 70 71 params = [[] for i in range(max(id) + 1)] 72 costs = [[] for i in range(len(params))] 73 # populate params for each id with the corresponding (param,cost) 74 for i in range(len(id)): 75 if ids is None or id[i] in ids: # take only the selected 'id' 76 params[id[i]].append(param[i]) 77 costs[id[i]].append(cost[i]) 78 params = [r for r in params if len(r)] # only keep selected 'ids' 79 costs = [r for r in costs if len(r)] # only keep selected 'ids' 80 81 # convert to support format 82 for i in range(len(params)): 83 params[i], costs[i] = raw_to_support(params[i], costs[i]) 84 return params, costs 85 51 86 52 87 def get_instance(location, *args, **kwds): … … 138 173 139 174 140 def draw_projection( file, select=0, scale=True, shift=False, style=None, figure=None):175 def draw_projection(x, cost, scale=True, shift=False, style=None, figure=None): 141 176 """draw a solution trajectory (for overlay on a 1D plot) 142 177 143 file is monitor or logfile of solution trajectories 144 select is the parameter index (e.g. 0 -> param[0]) selected for plotting 178 x is the sequence of values for one parameter (i.e. a parameter trajectory) 179 cost is the sequence of costs (i.e. the solution trajectory) 145 180 if scale is provided, scale the intensity as 'z = log(4*z*scale+1)+2' 146 181 if shift is provided, shift the intensity as 'z = z+shift' (useful for -z's) … … 148 183 if figure is provided, plot to an existing figure 149 184 """ 150 # params are the parameter trajectories151 # cost is the solution trajectory152 params, cost = read_history(file)153 d = {'x':0, 'y':1, 'z':2} #XXX: remove the easter egg?154 if select in d: select = d[select]155 x = params[int(select)] # requires one parameter156 157 185 if not figure: figure = plt.figure() 158 186 ax = figure.gca() … … 175 203 176 204 177 def draw_trajectory( file, select=None, surface=False, scale=True, shift=False, style=None, figure=None):205 def draw_trajectory(x, y, cost=None, scale=True, shift=False, style=None, figure=None): 178 206 """draw a solution trajectory (for overlay on a contour plot) 179 207 180 file is monitor or logfile of solution trajectories 181 select is a len-2 list of parameter indicies (e.g. 0,1 -> param[0],param[1])182 if surface is True, plot the trajectories as a 3D projection 208 x is a sequence of values for one parameter (i.e. a parameter trajectory) 209 y is a sequence of values for one parameter (i.e. a parameter trajectory) 210 cost is the solution trajectory (i.e. costs); if provided, plot a 3D contour 183 211 if scale is provided, scale the intensity as 'z = log(4*z*scale+1)+2' 184 212 if shift is provided, shift the intensity as 'z = z+shift' (useful for -z's) … … 186 214 if figure is provided, plot to an existing figure 187 215 """ 188 # params are the parameter trajectories189 # cost is the solution trajectory190 params, cost = read_history(file)191 if select is None: select = (0,1)192 d = {'x':0, 'y':1, 'z':2} #XXX: remove the easter egg?193 for ind,val in enumerate(select):194 if val in d: select[ind] = d[val]195 params = [params[int(i)] for i in select[:2]]196 #XXX: take 'params,cost=None' instead of 'file,surface=False'?197 x,y = params # requires two parameters198 199 216 if not figure: figure = plt.figure() 200 217 201 if surface: kwds = {'projection':'3d'} # 3D 202 elif surface is None: # 1D 203 raise NotImplementedError('need to add an option string parser') 204 else: kwds = {} # 2D 218 if cost: kwds = {'projection':'3d'} # 3D 219 else: kwds = {} # 2D 205 220 ax = figure.gca(**kwds) 206 221 207 222 if style in [None, False]: 208 223 style = 'w-o' #if not scale else 'k-o' 209 if surface: # is 3D, cost is needed224 if cost: # is 3D, cost is needed 210 225 import numpy 211 226 if shift: … … 311 326 312 327 if __name__ == '__main__': 313 #FIXME: for a script, need to:314 # - enable 'skip' plotting points (points or line or both)?315 328 #FIXME: should be able to: 316 329 # - apply a constraint as a region of NaN -- apply when 'xx,yy=x[ij],y[ij]' 317 330 # - apply a penalty by shifting the surface (plot w/alpha?) -- as above 318 # - read logfile with multiple trajectories (i.e. parallel batch)319 331 # - build an appropriately-sized default grid (from logfile info) 332 # - move all mulit-id param/cost reading into read_history 320 333 #FIXME: current issues: 321 334 # - 1D slice and projection work for 2D function, but aren't "pretty" 322 # - 1D slice and projection for 1D function needs further testing...335 # - 1D slice and projection for 1D function, is it meaningful and correct? 323 336 # - should be able to plot from solver.genealogy (multi-monitor?) [1D,2D,3D?] 324 337 # - should be able to scale 'z-axis' instead of scaling 'z' itself 325 338 # (see https://github.com/matplotlib/matplotlib/issues/209) 339 # - if trajectory outside contour grid, will increase bounds 340 # (see support_hypercube.py for how to fix bounds) 326 341 327 342 #XXX: note that 'argparse' is new as of python2.7 … … 334 349 metavar="STR",default=",,", 335 350 help="string to assign label to axis") 336 #parser.add_option("-n","--nid",action="store",dest="id",\337 #metavar="INT",default=None,338 #help="id # of the nth simultaneous points to plot")339 # parser.add_option("-i","--iters",action="store",dest="iters",\340 # metavar="STR",default=":",341 # help="indicator string to select iterationsto plot")351 parser.add_option("-n","--nid",action="store",dest="id",\ 352 metavar="INT",default=None, 353 help="id # of the nth simultaneous points to plot") 354 parser.add_option("-i","--iter",action="store",dest="stop",\ 355 metavar="INT",default=None, 356 help="the largest iteration to plot") 342 357 parser.add_option("-r","--reduce",action="store",dest="reducer",\ 343 358 metavar="STR",default="None", … … 361 376 # get the import path for the model 362 377 model = parsed_args[0] # e.g. 'mystic.models.rosen' 378 if "None" == model: model = None #XXX: 'required'... allow this? 363 379 364 380 try: # get the name of the parameter log file … … 418 434 label = ['','',''] 419 435 420 #try: # select which 'id' to plot results for421 # id= (int(parsed_opts.id),) #XXX: allow selecting more than one id ?422 #except:423 # id = None # i.e. 'all' **or** use id=0, which should be 'best' energy ? 424 425 # try: # select which iterations to plot426 # iters = parsed_opts.iters.split(',') # format is ":2, 2:4, 5, 6:" 427 #except:428 # iters = [':'] 436 try: # select which 'id' to plot results for 437 ids = (int(parsed_opts.id),) #XXX: allow selecting more than one id ? 438 except: 439 ids = None # i.e. 'all' 440 441 try: # select which iteration to stop plotting at 442 stop = int(parsed_opts.stop) 443 except: 444 stop = None 429 445 430 446 ################################################# … … 480 496 model = masked(mask)(model) 481 497 482 # project trajectory on a 1D slice of the model surface #XXX: useful?483 # fig0 = draw_slice(model, x=x, y=sol[-1], scale=scale, shift=shift) 484 # draw_projection(source, select=0, style=style, scale=scale, shift=shift, figure=fig0) 485 486 # plot the trajectory on the model surface (2D or 3D)487 if model: # plot the surface498 ## plot the surface in 1D 499 #if solver: v=sol[-1] 500 #elif source: v=cost[-1] 501 #else: v=None 502 #fig0 = draw_slice(model, x=x, y=v, scale=scale, shift=shift) 503 # plot the surface in 2D or 3D 488 504 fig = draw_contour(model, x, y, surface=surface, fill=fill, scale=scale, shift=shift) 489 505 else: 506 #fig0 = None 490 507 fig = None 491 if source: # plot the trajectory 492 fig = draw_trajectory(source, select=select, surface=surface, style=style, scale=scale, shift=shift, figure=fig) 508 509 if source: 510 # params are the parameter trajectories 511 # cost is the solution trajectory 512 params, cost = get_history(source, ids) 513 if len(cost) > 1: style = style[1:] # 'auto-color' #XXX: or grayscale? 514 515 for p,c in zip(params, cost): 516 ## project trajectory on a 1D slice of model surface #XXX: useful? 517 #s = select[0] if len(select) else 0 518 #px = p[int(s)] # draw_projection requires one parameter 519 ## ignore everything after 'stop' 520 #_c = c[:stop] 521 #_x = px[:stop] 522 #fig0 = draw_projection(_x,_c, style=style, scale=scale, shift=shift, figure=fig0) 523 524 # plot the trajectory on the model surface (2D or 3D) 525 # get two selected params #XXX: what if len(select)<2? or len(p)<2? 526 p = [p[int(i)] for i in select[:2]] 527 px,py = p # draw_trajectory requires two parameters 528 # ignore everything after 'stop' 529 _x = px[:stop] 530 _y = py[:stop] 531 _c = c[:stop] if surface else None 532 fig = draw_trajectory(_x,_y,_c, style=style, scale=scale, shift=shift, figure=fig) 493 533 494 534 # add labels to the axes
Note: See TracChangeset
for help on using the changeset viewer.