source: sasmodels/doc/genmodel.py @ fac23b3

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since fac23b3 was 745b7bb, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

spherical sld: doc cleanup

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[a5b8477]1from __future__ import print_function
2
[70bbb74]3import sys, os, math, re
4import numpy as np
5import matplotlib.pyplot as plt
[91c5fdc]6sys.path.insert(0, os.path.abspath('..'))
7from sasmodels import generate, core
[745b7bb]8from sasmodels.direct_model import DirectModel, call_profile
[70bbb74]9from sasmodels.data import empty_data1D, empty_data2D
10
[a5b8477]11try:
12    from typing import Dict, Any
13except ImportError:
14    pass
15else:
16    from matplotlib.axes import Axes
17    from sasmodels.kernel import KernelModel
18    from sasmodels.modelinfo import ModelInfo
[c094758]19
[745b7bb]20
[c094758]21def plot_1d(model, opts, ax):
[a5b8477]22    # type: (KernelModel, Dict[str, Any], Axes) -> None
23    """
24    Create a 1-D image.
25    """
[c094758]26    q_min, q_max, nq = opts['q_min'], opts['q_max'], opts['nq']
27    q_min = math.log10(q_min)
28    q_max = math.log10(q_max)
29    q = np.logspace(q_min, q_max, nq)
30    data = empty_data1D(q)
31    calculator = DirectModel(data, model)
32    Iq1D = calculator()
33
[a5b8477]34    ax.plot(q, Iq1D, color='blue', lw=2, label=model.info.name)
[c094758]35    ax.set_xlabel(r'$Q \/(\AA^{-1})$')
36    ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$')
37    ax.set_xscale(opts['xscale'])
38    ax.set_yscale(opts['yscale'])
39    #ax.legend(loc='best')
40
41def plot_2d(model, opts, ax):
[a5b8477]42    # type: (KernelModel, Dict[str, Any], Axes) -> None
43    """
44    Create a 2-D image.
45    """
[c094758]46    qx_max, nq2d = opts['qx_max'], opts['nq2d']
[a5b8477]47    q = np.linspace(-qx_max, qx_max, nq2d) # type: np.ndarray
[c094758]48    data2d = empty_data2D(q, resolution=0.0)
49    calculator = DirectModel(data2d, model)
50    Iq2D = calculator() #background=0)
51    Iq2D = Iq2D.reshape(nq2d, nq2d)
52    if opts['zscale'] == 'log':
53        Iq2D = np.log(np.clip(Iq2D, opts['vmin'], np.inf))
[044a9c1]54    ax.imshow(Iq2D, interpolation='nearest', aspect=1, origin='lower',
[a5b8477]55              extent=[-qx_max, qx_max, -qx_max, qx_max], cmap=opts['colormap'])
[c094758]56    ax.set_xlabel(r'$Q_x \/(\AA^{-1})$')
57    ax.set_ylabel(r'$Q_y \/(\AA^{-1})$')
58
[745b7bb]59def plot_profile_inset(model_info, ax):
60    p = ax.get_position()
61    width, height = 0.4*(p.x1-p.x0), 0.4*(p.y1-p.y0)
62    left, bottom = p.x1-width, p.y1-height
63    inset = plt.gcf().add_axes([left, bottom, width, height])
64    x, y, labels = call_profile(model_info)
65    inset.plot(x, y, '-')
66    inset.locator_params(nbins=4)
67    #inset.set_xlabel(labels[0])
68    #inset.set_ylabel(labels[1])
69    inset.text(0.99, 0.99, "profile",
70               horizontalalignment="right",
71               verticalalignment="top",
72               transform=inset.transAxes)
73
[a5b8477]74def figfile(model_info):
75    # type: (ModelInfo) -> str
76    return model_info.id + '_autogenfig.png'
77
78def make_figure(model_info, opts):
79    # type: (ModelInfo, Dict[str, Any]) -> None
80    """
81    Generate the figure file to include in the docs.
82    """
83    model = core.build_model(model_info)
84
85    fig_height = 3.0 # in
86    fig_left = 0.6 # in
87    fig_right = 0.5 # in
88    fig_top = 0.6*0.25 # in
89    fig_bottom = 0.6*0.75
90    if model_info.parameters.has_2d:
91        plot_height = fig_height - (fig_top+fig_bottom)
92        plot_width = plot_height
93        fig_width = 2*(plot_width + fig_left + fig_right)
94        aspect = (fig_width, fig_height)
95        ratio = aspect[0]/aspect[1]
96        ax_left = fig_left/fig_width
97        ax_bottom = fig_bottom/fig_height
98        ax_height = plot_height/fig_height
99        ax_width = ax_height/ratio # square axes
100        fig = plt.figure(figsize=aspect)
101        ax2d = fig.add_axes([0.5+ax_left, ax_bottom, ax_width, ax_height])
102        plot_2d(model, opts, ax2d)
103        ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height])
104        plot_1d(model, opts, ax1d)
105        #ax.set_aspect('square')
106    else:
107        plot_height = fig_height - (fig_top+fig_bottom)
108        plot_width = (1+np.sqrt(5))/2*fig_height
109        fig_width = plot_width + fig_left + fig_right
110        ax_left = fig_left/fig_width
111        ax_bottom = fig_bottom/fig_height
112        ax_width = plot_width/fig_width
113        ax_height = plot_height/fig_height
114        aspect = (fig_width, fig_height)
115        fig = plt.figure(figsize=aspect)
116        ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height])
117        plot_1d(model, opts, ax1d)
118
[745b7bb]119    if model_info.profile:
120        plot_profile_inset(model_info, ax1d)
121
[a5b8477]122    # Save image in model/img
123    path = os.path.join('model', 'img', figfile(model_info))
124    plt.savefig(path, bbox_inches='tight')
125    #print("figure saved in",path)
126
127def gen_docs(model_info):
128    # type: (ModelInfo) -> None
129    """
130    Generate the doc string with the figure inserted before the references.
131    """
132
133    # Load the doc string from the module definition file and store it in rst
134    docstr = generate.make_doc(model_info)
135
136    # Auto caption for figure
137    captionstr = '\n'
138    captionstr += '.. figure:: img/' + figfile(model_info) + '\n'
139    captionstr += '\n'
140    if model_info.parameters.has_2d:
141        captionstr += '    1D and 2D plots corresponding to the default parameters of the model.\n'
142    else:
143        captionstr += '    1D plot corresponding to the default parameters of the model.\n'
144    captionstr += '\n'
145
146    # Add figure reference and caption to documentation (at end, before References)
147    pattern = '\*\*REFERENCE'
148    match = re.search(pattern, docstr.upper())
149
150    if match:
151        docstr1 = docstr[:match.start()]
152        docstr2 = docstr[match.start():]
153        docstr = docstr1 + captionstr + docstr2
154    else:
155        print('------------------------------------------------------------------')
156        print('References NOT FOUND for model: ', model_info.id)
157        print('------------------------------------------------------------------')
158        docstr += captionstr
159
160    open(sys.argv[2],'w').write(docstr)
161
162def process_model(path):
163    # type: (str) -> None
164    """
165    Generate doc file and image file for the given model definition file.
166    """
167
168    # Load the model file
169    model_name = os.path.basename(path)[:-3]
170    model_info = core.load_model_info(model_name)
171
172    # Plotting ranges and options
173    opts = {
174        'xscale'    : 'log',
175        'yscale'    : 'log' if not model_info.structure_factor else 'linear',
176        'zscale'    : 'log' if not model_info.structure_factor else 'linear',
177        'q_min'     : 0.001,
178        'q_max'     : 1.0,
179        'nq'        : 1000,
180        'nq2d'      : 1000,
181        'vmin'      : 1e-3,  # floor for the 2D data results
182        'qx_max'    : 0.5,
183        #'colormap'  : 'gist_ncar',
184        'colormap'  : 'nipy_spectral',
185        #'colormap'  : 'jet',
186    }
187
188    # Generate the RST file and the figure.  Order doesn't matter.
189    gen_docs(model_info)
190    make_figure(model_info, opts)
191
192if __name__ == "__main__":
193    process_model(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.