source: sasmodels/doc/genmodel.py @ 117090a

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 117090a 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
Line 
1import sys, os, math, re
2import numpy as np
3import matplotlib.pyplot as plt
4import pylab
5sys.path.insert(0, os.path.abspath('..'))
6from sasmodels import generate, core
7from sasmodels.direct_model import DirectModel
8from sasmodels.data import empty_data1D, empty_data2D
9
10
11# Convert ../sasmodels/models/name.py to name
12model_name = os.path.basename(sys.argv[1])[:-3]
13model_info = core.load_model_info(model_name)
14model = core.build_model(model_info)
15
16# Load the doc string from the module definition file and store it in rst
17docstr = generate.make_doc(model_info)
18
19
20# Calculate 1D curve for default parameters
21pars = dict((p.name, p.default) for p in model_info['parameters'])
22
23# Plotting ranges and options
24opts = {
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,
31    'nq2d'      : 1000,
32    'vmin'      : 1e-3,  # floor for the 2D data results
33    'qx_max'    : 0.5,
34    #'colormap'  : 'gist_ncar',
35    'colormap'  : 'nipy_spectral',
36    #'colormap'  : 'jet',
37}
38
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))
65    ax.imshow(Iq2D, interpolation='nearest', aspect=1, origin='lower',
66        extent=[-qx_max, qx_max, -qx_max, qx_max], cmap=opts['colormap'])
67    ax.set_xlabel(r'$Q_x \/(\AA^{-1})$')
68    ax.set_ylabel(r'$Q_y \/(\AA^{-1})$')
69
70# Generate image
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])
90    plot_1d(model, opts, ax1d)
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])
103    plot_1d(model, opts, ax1d)
104
105# Save image in model/img
106figname = model_name + '_autogenfig.png'
107filename = os.path.join('model', 'img', figname)
108plt.savefig(filename, bbox_inches='tight')
109#print "figure saved in",filename
110
111# Auto caption for figure
112captionstr = '\n'
113captionstr += '.. figure:: img/' + model_info['id'] + '_autogenfig.png\n'
114captionstr += '\n'
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'
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:
130    print '------------------------------------------------------------------'
131    print 'References NOT FOUND for model: ', model_info['id']
132    print '------------------------------------------------------------------'
133    docstr = docstr + captionstr
134
135open(sys.argv[2],'w').write(docstr)
Note: See TracBrowser for help on using the repository browser.