[70bbb74] | 1 | import sys, os, math, re |
---|
| 2 | import numpy as np |
---|
| 3 | import matplotlib.pyplot as plt |
---|
[044a9c1] | 4 | import pylab |
---|
[91c5fdc] | 5 | sys.path.insert(0, os.path.abspath('..')) |
---|
| 6 | from sasmodels import generate, core |
---|
[70bbb74] | 7 | from sasmodels.direct_model import DirectModel |
---|
| 8 | from sasmodels.data import empty_data1D, empty_data2D |
---|
| 9 | |
---|
[19dcb933] | 10 | |
---|
[91c5fdc] | 11 | # Convert ../sasmodels/models/name.py to name |
---|
| 12 | model_name = os.path.basename(sys.argv[1])[:-3] |
---|
[c094758] | 13 | model_info = core.load_model_info(model_name) |
---|
| 14 | model = core.build_model(model_info) |
---|
[19dcb933] | 15 | |
---|
| 16 | # Load the doc string from the module definition file and store it in rst |
---|
[c094758] | 17 | docstr = generate.make_doc(model_info) |
---|
[70bbb74] | 18 | |
---|
| 19 | |
---|
| 20 | # Calculate 1D curve for default parameters |
---|
[c094758] | 21 | pars = dict((p.name, p.default) for p in model_info['parameters']) |
---|
[70bbb74] | 22 | |
---|
| 23 | # Plotting ranges and options |
---|
| 24 | opts = { |
---|
[c094758] | 25 | 'xscale' : 'log', |
---|
| 26 | 'yscale' : 'log' if not model_info['structure_factor'] else 'linear', |
---|
| 27 | 'zscale' : 'log' if not model_info['structure_factor'] else 'linear', |
---|
| 28 | 'q_min' : 0.001, |
---|
| 29 | 'q_max' : 1.0, |
---|
| 30 | 'nq' : 1000, |
---|
[824465b] | 31 | 'nq2d' : 1000, |
---|
[c094758] | 32 | 'vmin' : 1e-3, # floor for the 2D data results |
---|
| 33 | 'qx_max' : 0.5, |
---|
[824465b] | 34 | #'colormap' : 'gist_ncar', |
---|
| 35 | 'colormap' : 'jet', |
---|
[70bbb74] | 36 | } |
---|
| 37 | |
---|
[c094758] | 38 | |
---|
| 39 | def plot_1d(model, opts, ax): |
---|
| 40 | q_min, q_max, nq = opts['q_min'], opts['q_max'], opts['nq'] |
---|
| 41 | q_min = math.log10(q_min) |
---|
| 42 | q_max = math.log10(q_max) |
---|
| 43 | q = np.logspace(q_min, q_max, nq) |
---|
| 44 | data = empty_data1D(q) |
---|
| 45 | calculator = DirectModel(data, model) |
---|
| 46 | Iq1D = calculator() |
---|
| 47 | |
---|
| 48 | ax.plot(q, Iq1D, color='blue', lw=2, label=model_info['name']) |
---|
| 49 | ax.set_xlabel(r'$Q \/(\AA^{-1})$') |
---|
| 50 | ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$') |
---|
| 51 | ax.set_xscale(opts['xscale']) |
---|
| 52 | ax.set_yscale(opts['yscale']) |
---|
| 53 | #ax.legend(loc='best') |
---|
| 54 | |
---|
| 55 | def plot_2d(model, opts, ax): |
---|
| 56 | qx_max, nq2d = opts['qx_max'], opts['nq2d'] |
---|
| 57 | q = np.linspace(-qx_max, qx_max, nq2d) |
---|
| 58 | data2d = empty_data2D(q, resolution=0.0) |
---|
| 59 | calculator = DirectModel(data2d, model) |
---|
| 60 | Iq2D = calculator() #background=0) |
---|
| 61 | Iq2D = Iq2D.reshape(nq2d, nq2d) |
---|
| 62 | if opts['zscale'] == 'log': |
---|
| 63 | Iq2D = np.log(np.clip(Iq2D, opts['vmin'], np.inf)) |
---|
[044a9c1] | 64 | ax.imshow(Iq2D, interpolation='nearest', aspect=1, origin='lower', |
---|
[824465b] | 65 | extent=[-qx_max, qx_max, -qx_max, qx_max], cmap=opts['colormap']) |
---|
[c094758] | 66 | ax.set_xlabel(r'$Q_x \/(\AA^{-1})$') |
---|
| 67 | ax.set_ylabel(r'$Q_y \/(\AA^{-1})$') |
---|
| 68 | |
---|
[735507b] | 69 | # Generate image |
---|
[c094758] | 70 | fig_height = 3.0 # in |
---|
| 71 | fig_left = 0.6 # in |
---|
| 72 | fig_right = 0.5 # in |
---|
| 73 | fig_top = 0.6*0.25 # in |
---|
| 74 | fig_bottom = 0.6*0.75 |
---|
| 75 | if model_info['has_2d']: |
---|
| 76 | plot_height = fig_height - (fig_top+fig_bottom) |
---|
| 77 | plot_width = plot_height |
---|
| 78 | fig_width = 2*(plot_width + fig_left + fig_right) |
---|
| 79 | aspect = (fig_width, fig_height) |
---|
| 80 | ratio = aspect[0]/aspect[1] |
---|
| 81 | ax_left = fig_left/fig_width |
---|
| 82 | ax_bottom = fig_bottom/fig_height |
---|
| 83 | ax_height = plot_height/fig_height |
---|
| 84 | ax_width = ax_height/ratio # square axes |
---|
| 85 | fig = plt.figure(figsize=aspect) |
---|
| 86 | ax2d = fig.add_axes([0.5+ax_left, ax_bottom, ax_width, ax_height]) |
---|
| 87 | plot_2d(model, opts, ax2d) |
---|
| 88 | ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height]) |
---|
[044a9c1] | 89 | plot_1d(model, opts, ax1d) |
---|
[c094758] | 90 | #ax.set_aspect('square') |
---|
| 91 | else: |
---|
| 92 | plot_height = fig_height - (fig_top+fig_bottom) |
---|
| 93 | plot_width = (1+np.sqrt(5))/2*fig_height |
---|
| 94 | fig_width = plot_width + fig_left + fig_right |
---|
| 95 | ax_left = fig_left/fig_width |
---|
| 96 | ax_bottom = fig_bottom/fig_height |
---|
| 97 | ax_width = plot_width/fig_width |
---|
| 98 | ax_height = plot_height/fig_height |
---|
| 99 | aspect = (fig_width, fig_height) |
---|
| 100 | fig = plt.figure(figsize=aspect) |
---|
| 101 | ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height]) |
---|
[735507b] | 102 | plot_1d(model, opts, ax1d) |
---|
[70bbb74] | 103 | |
---|
| 104 | # Save image in model/img |
---|
| 105 | figname = model_name + '_autogenfig.png' |
---|
| 106 | filename = os.path.join('model', 'img', figname) |
---|
[c094758] | 107 | plt.savefig(filename, bbox_inches='tight') |
---|
| 108 | #print "figure saved in",filename |
---|
[70bbb74] | 109 | |
---|
| 110 | # Auto caption for figure |
---|
| 111 | captionstr = '\n' |
---|
[c094758] | 112 | captionstr += '.. figure:: img/' + model_info['id'] + '_autogenfig.png\n' |
---|
[70bbb74] | 113 | captionstr += '\n' |
---|
[044a9c1] | 114 | if model_info['has_2d']: |
---|
| 115 | captionstr += ' 1D and 2D plots corresponding to the default parameters of the model.\n' |
---|
| 116 | else: |
---|
| 117 | captionstr += ' 1D plot corresponding to the default parameters of the model.\n' |
---|
[70bbb74] | 118 | captionstr += '\n' |
---|
| 119 | |
---|
| 120 | # Add figure reference and caption to documentation (at end, before References) |
---|
| 121 | pattern = '\*\*REFERENCE' |
---|
| 122 | m = re.search(pattern, docstr.upper()) |
---|
| 123 | |
---|
| 124 | if m: |
---|
| 125 | docstr1 = docstr[:m.start()] |
---|
| 126 | docstr2 = docstr[m.start():] |
---|
| 127 | docstr = docstr1 + captionstr + docstr2 |
---|
| 128 | else: |
---|
[aa2edb2] | 129 | print '------------------------------------------------------------------' |
---|
[c094758] | 130 | print 'References NOT FOUND for model: ', model_info['id'] |
---|
[aa2edb2] | 131 | print '------------------------------------------------------------------' |
---|
[70bbb74] | 132 | docstr = docstr + captionstr |
---|
| 133 | |
---|
[19dcb933] | 134 | open(sys.argv[2],'w').write(docstr) |
---|