def generate_mayavi_point_plot(self, srcid_sciclasses_list): """ Generate a Mayavi mlab 3D plot which summarizes iterative classification of a TUTOR source over the number of epochs used/added. """ # # # # # # # # # # TODO: I should label which science class by color, as Y axis labels from enthought.mayavi.scripts import mayavi2 mayavi2.standalone(globals()) from enthought.mayavi import mlab epoch_ids = [0] # x class_groups = [0] # y probs = [0] # z styles = [0] # numbers used for coloring & glyph sizes for src_id,sci_classes in srcid_sciclasses_list: #print 'src_id:', src_id i = 0 for class_name, class_dict in sci_classes.class_dict.iteritems(): epoch_ids.extend(class_dict['epoch_ids']) class_groups.extend([1]*len(class_dict['epoch_ids'])) probs.extend(class_dict['probs']) styles.extend([i + 1]*len(class_dict['epoch_ids'])) i += 1 mlab.points3d(numpy.array(epoch_ids), numpy.array(class_groups), numpy.array(probs)*100.0, numpy.array(styles), colormap="Paired", scale_mode="none", scale_factor=2.0) mlab.axes(xlabel='N of epochs', ylabel='science class', zlabel='% Prob.')
# Example of option baskets # Distributed under BSD License from enthought.mayavi.scripts import mayavi2 from enthought.tvtk.tools import mlab from plotspace import PlotSpace mayavi2.standalone(globals()) import eurooption import basketoption import scipy import numpy import threading spot = scipy.arange(10.0, 100.0, 5.0) vol = scipy.arange(0.1, 1.0, 0.1) riskfree = scipy.arange(0.0, 5.0, 1.0) u1 = scipy.arange(0.5, 15.0, 0.5) u2 = scipy.arange(0.5, 15.0, 0.5) if __name__ == "__main__": mayavi.new_scene() p = PlotSpace(mayavi.engine.current_scene, [1, 100, 1]) p.add_points([[1, 2, 1], [1, 3, 1], [2, 4, 2]]) p.add_lines([[2, 2, 1], [3, 3, 1], [3, 3, 1], [3, 4, 1], [4, 4, 2]]) mayavi.new_scene() p = PlotSpace(mayavi.engine.current_scene, [1, 100, 1])
# Example of option baskets # Distributed under BSD License from enthought.mayavi.scripts import mayavi2 mayavi2.standalone(globals()) import scipy import QuantLib as ql from enthought.tvtk.tools import mlab from enthought.mayavi.sources.vtk_data_source import VTKDataSource from enthought.mayavi.filters.warp_scalar import WarpScalar from enthought.mayavi.modules.surface import Surface spot = scipy.arange(10.0, 100.0, 5.0) vol = scipy.arange(0.1, 1.0, 0.1) riskfree = scipy.arange(0.0, 5.0, 1.0) todaysDate = ql.Date(15, ql.May, 1998) ql.Settings.instance().evaluationDate = todaysDate settlementDate = ql.Date(17, ql.May, 1998) riskFreeQuote = ql.SimpleQuote(0.05) riskFreeRate = ql.FlatForward(settlementDate, ql.QuoteHandle(riskFreeQuote), ql.Actual365Fixed()) # option parameters exercise1 = ql.AmericanExercise(settlementDate, ql.Date(17, ql.May, 1999)) exercise2 = ql.EuropeanExercise(settlementDate) payoff = ql.PlainVanillaPayoff(ql.Option.Call, 40.0) # market data
def generate_mayavi_line_plot(self, srcid_sciclasses_list, use_linfit_segments=False, enable_mayavi2_interactive_gui=False): """ Generate a Mayavi mlab 3D plot which summarizes iterative classification of a TUTOR source over the number of epochs used/added. """ # TODO: I would like to plot each sci class a different color # - I should have each science class re-use a single number/color # - I should label which science class by color, as Y axis labels # TODO: I will need to insert a 0 point onto arrays (for s[0]?) if enable_mayavi2_interactive_gui: from enthought.mayavi.scripts import mayavi2 mayavi2.standalone(globals()) from enthought.mayavi import mlab # scalar cut plane plotting module stuff: import enthought.mayavi from enthought.mayavi.modules.scalar_cut_plane import ScalarCutPlane epoch_ids = [0] # x class_groups = [0] # y probs = [0] # z styles = [0] # numbers used for coloring & glyph sizes used_final_classes = [] i_srcid = 0 for final_class in self.pars['interested_sci_classes']: if not finalclass_ordered_dict.has_key(final_class): continue # skip this science class since nothing to plot else: used_final_classes.append(final_class) srcid_sciclasses_list = finalclass_ordered_dict[final_class] for src_id,sci_classes in srcid_sciclasses_list: print 'src_id:', src_id, '\t', final_class sci_classes.generate_linearfit_endpoints_segments() i = 0 for class_name,class_dict in sci_classes.class_dict.iteritems(): class_style = self.sciclass_style_dict[class_name] if use_linfit_segments: for segment_dict in class_dict['linfit_segments']: epoch_ids.extend([segment_dict['epoch_ids'][0]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) epoch_ids.extend(segment_dict['epoch_ids']) class_groups.extend([i_srcid]*len(segment_dict['epoch_ids'])) probs.extend(segment_dict['probs']) styles.extend([class_style]*len(segment_dict['epoch_ids'])) epoch_ids.extend([segment_dict['epoch_ids'][-1]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) else: epoch_ids.extend([class_dict['epoch_ids'][0]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) epoch_ids.extend(class_dict['epoch_ids']) class_groups.extend([i_srcid]*len(class_dict['epoch_ids'])) probs.extend(class_dict['probs']) styles.extend([class_style]*len(class_dict['epoch_ids'])) epoch_ids.extend([class_dict['epoch_ids'][-1]]) class_groups.extend([i_srcid]) probs.extend([0]) styles.extend([0]) i += 1 i_srcid += 3 # Y spacing between srcids within a science-class i_srcid += 20 # Y spacing between science-class groups mlab.plot3d(numpy.array(epoch_ids), numpy.array(class_groups), numpy.array(probs)*100.0, numpy.array(styles), colormap="Paired", tube_radius=1) # extent=[0,600, # 0,i_srcid, # 15, 110]) mlab.axes(xlabel='N of epochs', ylabel='science class', zlabel='% Prob.')#, # DEBUG/UPGRADE: These seem to trigger some bug about Actor methods: # extent=[0,600, # 0,i_srcid, # -10, 110]) title_str = "num_srcids=%d probability_cut=%0.2lf factor_threshold=%0.2lf bin_size=%d poly_order=%d" % (\ self.pars['num_srcids_to_retrieve_plot'], self.pars['sciclass_probability_cut'], self.pars['polyfit_factor_threshold'], self.pars['polyfit_bin_size'], self.pars['polyfit_poly_order']) ##### TITLE: # The 'z' is a flag in Mayavi2 v3.1.0 documentation: #mlab.text(0.01, 0.97, title_str, width=1.0, name='title', z=0.0) mlab.text(0.01, 0.97, title_str, width=1.0, name='title') ##### SCIENCE CLASS LABELS: # TODO: Eventually I would like the class labels to be colored and # placed on the y axis, but this requires: # 1) later mayavi version to allow 3D text positioning # 2) ability to match text color to the line color-map. if 1: used_final_classes.reverse() y = 0.95 for class_name in used_final_classes: class_str = "%2d %s" %(len(finalclass_ordered_dict[class_name]), class_name) mlab.text(0.85, y, class_str, width=0.095*(len(class_str)/20.0)) y -= 0.018 ##### Add a x-axis plane (I can't figure out code to make it opaque) if 0: cp = ScalarCutPlane() mayavi.add_module(cp) cp.implicit_plane._hideshow() # this un-displays the plane cp.implicit_plane.normal = 0,0,1 cp.implicit_plane.origin = 150,168,15 #cp.implicit_plane.position= 0.15 # feature not available yet print '##### cp:' cp.print_traits() print '##### cp.implicit_plane:' cp.implicit_plane.print_traits() print '##### cp.implicit_plane._HideShowAction:' cp.implicit_plane._HideShowAction.print_traits() ##### Camera position: if enable_mayavi2_interactive_gui: camera_distance = 600 else: camera_distance = 1200 enthought.mayavi.tools.camera.view(azimuth=50, elevation=70, # 0:looking down to -z distance=camera_distance, focalpoint=(100,(i_srcid*0.4),50)) #enthought.mayavi.mlab.show_pipeline() # this introspecive feature is not available in current mayavi version. #####If no Mayavi2 GUI, we allow user to resize image before saving file if not enable_mayavi2_interactive_gui: print 'Please resize window & Press a Key.' import curses stdscr = curses.initscr() while 1: c = stdscr.getch() break curses.endwin() ##### Save figure: img_fpath ="/tmp/%s%s.png" %(title_str.replace('=','').replace(' ','_'), self.pars['save_plot_image_suffix']) if os.path.exists(img_fpath): os.system('rm ' + img_fpath) mlab.savefig(img_fpath)#, size=(500,500))#, dpi=200) #size flag doesn't do anything print "Saved:", img_fpath