source: sasmodels/doc/genmodel.py @ 82c37f5

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 82c37f5 was 89f4163, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

change docs to nipy_spectral colormap

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