- Timestamp:
- Mar 18, 2016 4:16:21 AM (9 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 30c2ac3
- Parents:
- 02e70ff (diff), c094758 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/genmodel.py
raa2edb2 rc094758 10 10 # Convert ../sasmodels/models/name.py to name 11 11 model_name = os.path.basename(sys.argv[1])[:-3] 12 model_info = core.load_model_info(model_name) 13 model = core.build_model(model_info) 12 14 13 15 # Load the doc string from the module definition file and store it in rst 14 docstr = generate.make_doc(core.load_model_info(model_name)) 15 16 # Generate automatically plot of the model and add it to rst documentation 16 docstr = generate.make_doc(model_info) 17 17 18 info = core.load_model_info(model_name)19 18 20 19 # Calculate 1D curve for default parameters 21 pars = dict((p [0], p[2]) for p ininfo['parameters'])20 pars = dict((p.name, p.default) for p in model_info['parameters']) 22 21 23 22 # Plotting ranges and options 24 23 opts = { 25 'xscale' : 'log', 26 'yscale' : 'log' if not info['structure_factor'] else 'linear', 27 'qmin' : 0.001, 28 'qmax' : 1.0, 29 'nq' : 1000, 30 'nq2d' : 100, 24 'xscale' : 'log', 25 'yscale' : 'log' if not model_info['structure_factor'] else 'linear', 26 'zscale' : 'log' if not model_info['structure_factor'] else 'linear', 27 'q_min' : 0.001, 28 'q_max' : 1.0, 29 'nq' : 1000, 30 'nq2d' : 400, 31 'vmin' : 1e-3, # floor for the 2D data results 32 'qx_max' : 0.5, 31 33 } 32 34 33 qmin, qmax, nq = opts['qmin'], opts['qmax'], opts['nq']34 qmin = math.log10(qmin)35 qmax = math.log10(qmax)36 q = np.logspace(qmin, qmax, nq)37 data = empty_data1D(q)38 model = core.load_model(model_name)39 calculator = DirectModel(data, model)40 Iq1D = calculator()41 35 42 # TO DO: Generation of 2D plots 43 # Problem in sasmodels.direct_model._calc_theory 44 # There self._kernel.q_input.nq gets a value of 0 in the 2D case 45 # and returns a 0 numpy array (it does not call the C code) 36 def plot_1d(model, opts, ax): 37 q_min, q_max, nq = opts['q_min'], opts['q_max'], opts['nq'] 38 q_min = math.log10(q_min) 39 q_max = math.log10(q_max) 40 q = np.logspace(q_min, q_max, nq) 41 data = empty_data1D(q) 42 calculator = DirectModel(data, model) 43 Iq1D = calculator() 46 44 47 # If 2D model, compute 2D image 48 #if info['has_2d'] != []: 49 # qmax, nq2d = opts['qmax'], opts['nq2d'] 50 # data2d = empty_data2D(np.linspace(-qmax, qmax, nq2d), resolution=0.0) 51 # #model = core.load_model(model_name) 52 # calculator = DirectModel(data2d, model) 53 # Iq2D = calculator() 45 ax.plot(q, Iq1D, color='blue', lw=2, label=model_info['name']) 46 ax.set_xlabel(r'$Q \/(\AA^{-1})$') 47 ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$') 48 ax.set_xscale(opts['xscale']) 49 ax.set_yscale(opts['yscale']) 50 #ax.legend(loc='best') 51 52 def plot_2d(model, opts, ax): 53 qx_max, nq2d = opts['qx_max'], opts['nq2d'] 54 q = np.linspace(-qx_max, qx_max, nq2d) 55 data2d = empty_data2D(q, resolution=0.0) 56 calculator = DirectModel(data2d, model) 57 Iq2D = calculator() #background=0) 58 Iq2D = Iq2D.reshape(nq2d, nq2d) 59 if opts['zscale'] == 'log': 60 Iq2D = np.log(np.clip(Iq2D, opts['vmin'], np.inf)) 61 h = ax.imshow(Iq2D, interpolation='nearest', aspect=1, origin='upper', 62 extent=[-qx_max, qx_max, -qx_max, qx_max], cmap=ice_cm()) 63 # , vmin=vmin, vmax=vmax) 64 ax.set_xlabel(r'$Q_x \/(\AA^{-1})$') 65 ax.set_ylabel(r'$Q_y \/(\AA^{-1})$') 66 67 def ice_cm(): 68 from matplotlib._cm import _Blues_data 69 from matplotlib import colors 70 from matplotlib import rcParams 71 def from_white(segments): 72 scale = 1.0/segments[0][1] 73 return [(k, v*scale, w*scale) for k, v, w in segments] 74 ice_data = dict((k,from_white(v)) for k,v in _Blues_data.items()) 75 ice = colors.LinearSegmentedColormap("ice", ice_data, rcParams['image.lut']) 76 return ice 77 54 78 55 79 # Generate image (comment IF for 1D/2D for the moment) and generate only 1D 56 #if info['has_2d'] == []: 57 # fig = plt.figure() 58 # ax = fig.add_subplot(1,1,1) 59 # ax.plot(q, Iq1D, color='blue', lw=2, label=model_name) 60 # ax.set_xlabel(r'$Q \/(\AA^{-1})$') 61 # ax.set_xscale(opts['xscale']) 62 # ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$') 63 # ax.set_yscale(opts['yscale']) 64 # ax.legend() 65 #else: 66 # # need figure with 1D + 2D 67 # pass 68 fig = plt.figure() 69 ax = fig.add_subplot(1,1,1) 70 ax.plot(q, Iq1D, color='blue', lw=2, label=info['name']) 71 ax.set_xlabel(r'$Q \/(\AA^{-1})$') 72 ax.set_xscale(opts['xscale']) 73 ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$') 74 ax.set_yscale(opts['yscale']) 75 ax.legend() 76 80 fig_height = 3.0 # in 81 fig_left = 0.6 # in 82 fig_right = 0.5 # in 83 fig_top = 0.6*0.25 # in 84 fig_bottom = 0.6*0.75 85 if model_info['has_2d']: 86 plot_height = fig_height - (fig_top+fig_bottom) 87 plot_width = plot_height 88 fig_width = 2*(plot_width + fig_left + fig_right) 89 aspect = (fig_width, fig_height) 90 ratio = aspect[0]/aspect[1] 91 ax_left = fig_left/fig_width 92 ax_bottom = fig_bottom/fig_height 93 ax_height = plot_height/fig_height 94 ax_width = ax_height/ratio # square axes 95 fig = plt.figure(figsize=aspect) 96 ax2d = fig.add_axes([0.5+ax_left, ax_bottom, ax_width, ax_height]) 97 plot_2d(model, opts, ax2d) 98 ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height]) 99 #ax.set_aspect('square') 100 else: 101 plot_height = fig_height - (fig_top+fig_bottom) 102 plot_width = (1+np.sqrt(5))/2*fig_height 103 fig_width = plot_width + fig_left + fig_right 104 ax_left = fig_left/fig_width 105 ax_bottom = fig_bottom/fig_height 106 ax_width = plot_width/fig_width 107 ax_height = plot_height/fig_height 108 aspect = (fig_width, fig_height) 109 fig = plt.figure(figsize=aspect) 110 ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height]) 111 plot_1d(model, opts, ax1d) 77 112 78 113 # Save image in model/img 79 114 figname = model_name + '_autogenfig.png' 80 115 filename = os.path.join('model', 'img', figname) 81 plt.savefig(filename) 116 plt.savefig(filename, bbox_inches='tight') 117 #print "figure saved in",filename 82 118 83 119 # Auto caption for figure 84 120 captionstr = '\n' 85 captionstr += '.. figure:: img/' + model_ name+ '_autogenfig.png\n'121 captionstr += '.. figure:: img/' + model_info['id'] + '_autogenfig.png\n' 86 122 captionstr += '\n' 87 #if info['has_2d'] == []:88 # captionstr += ' 1D plot corresponding to the default parameters of the model.\n'89 #else:90 # captionstr += ' 1D and 2D plots corresponding to the default parameters of the model.\n'91 123 captionstr += ' 1D plot corresponding to the default parameters of the model.\n' 92 124 captionstr += '\n' … … 102 134 else: 103 135 print '------------------------------------------------------------------' 104 print 'References NOT FOUND for model: ', model_ name136 print 'References NOT FOUND for model: ', model_info['id'] 105 137 print '------------------------------------------------------------------' 106 138 docstr = docstr + captionstr
Note: See TracChangeset
for help on using the changeset viewer.