source: sasmodels/doc/genmodel.py @ c1e44e5

Last change on this file since c1e44e5 was c1e44e5, checked in by Paul Kienzle <pkienzle@…>, 5 years ago

Add local link to source files. Refs #1263.

  • Property mode set to 100644
File size: 8.1 KB
Line 
1from __future__ import print_function
2
3import sys, os, math, re
4import numpy as np
5import matplotlib
6matplotlib.use('Agg')
7import matplotlib.pyplot as plt
8sys.path.insert(0, os.path.abspath('..'))
9import sasmodels
10from sasmodels import generate, core
11from sasmodels.direct_model import DirectModel, call_profile
12from sasmodels.data import empty_data1D, empty_data2D
13
14try:
15    from typing import Dict, Any
16except ImportError:
17    pass
18else:
19    from matplotlib.axes import Axes
20    from sasmodels.kernel import KernelModel
21    from sasmodels.modelinfo import ModelInfo
22
23
24def plot_1d(model, opts, ax):
25    # type: (KernelModel, Dict[str, Any], Axes) -> None
26    """
27    Create a 1-D image.
28    """
29    q_min, q_max, nq = opts['q_min'], opts['q_max'], opts['nq']
30    q_min = math.log10(q_min)
31    q_max = math.log10(q_max)
32    q = np.logspace(q_min, q_max, nq)
33    data = empty_data1D(q)
34    calculator = DirectModel(data, model)
35    Iq1D = calculator()
36
37    ax.plot(q, Iq1D, color='blue', lw=2, label=model.info.name)
38    ax.set_xlabel(r'$Q \/(\AA^{-1})$')
39    ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$')
40    ax.set_xscale(opts['xscale'])
41    ax.set_yscale(opts['yscale'])
42    #ax.legend(loc='best')
43
44def plot_2d(model, opts, ax):
45    # type: (KernelModel, Dict[str, Any], Axes) -> None
46    """
47    Create a 2-D image.
48    """
49    qx_max, nq2d = opts['qx_max'], opts['nq2d']
50    q = np.linspace(-qx_max, qx_max, nq2d) # type: np.ndarray
51    data2d = empty_data2D(q, resolution=0.0)
52    calculator = DirectModel(data2d, model)
53    Iq2D = calculator() #background=0)
54    Iq2D = Iq2D.reshape(nq2d, nq2d)
55    if opts['zscale'] == 'log':
56        Iq2D = np.log(np.clip(Iq2D, opts['vmin'], np.inf))
57    ax.imshow(Iq2D, interpolation='nearest', aspect=1, origin='lower',
58              extent=[-qx_max, qx_max, -qx_max, qx_max], cmap=opts['colormap'])
59    ax.set_xlabel(r'$Q_x \/(\AA^{-1})$')
60    ax.set_ylabel(r'$Q_y \/(\AA^{-1})$')
61
62def plot_profile_inset(model_info, ax):
63    p = ax.get_position()
64    width, height = 0.4*(p.x1-p.x0), 0.4*(p.y1-p.y0)
65    left, bottom = p.x1-width, p.y1-height
66    inset = plt.gcf().add_axes([left, bottom, width, height])
67    x, y, labels = call_profile(model_info)
68    inset.plot(x, y, '-')
69    inset.locator_params(nbins=4)
70    #inset.set_xlabel(labels[0])
71    #inset.set_ylabel(labels[1])
72    inset.text(0.99, 0.99, "profile",
73               horizontalalignment="right",
74               verticalalignment="top",
75               transform=inset.transAxes)
76
77def figfile(model_info):
78    # type: (ModelInfo) -> str
79    return model_info.id + '_autogenfig.png'
80
81def make_figure(model_info, opts):
82    # type: (ModelInfo, Dict[str, Any]) -> None
83    """
84    Generate the figure file to include in the docs.
85    """
86    model = core.build_model(model_info)
87
88    fig_height = 3.0 # in
89    fig_left = 0.6 # in
90    fig_right = 0.5 # in
91    fig_top = 0.6*0.25 # in
92    fig_bottom = 0.6*0.75
93    if model_info.parameters.has_2d:
94        plot_height = fig_height - (fig_top+fig_bottom)
95        plot_width = plot_height
96        fig_width = 2*(plot_width + fig_left + fig_right)
97        aspect = (fig_width, fig_height)
98        ratio = aspect[0]/aspect[1]
99        ax_left = fig_left/fig_width
100        ax_bottom = fig_bottom/fig_height
101        ax_height = plot_height/fig_height
102        ax_width = ax_height/ratio # square axes
103        fig = plt.figure(figsize=aspect)
104        ax2d = fig.add_axes([0.5+ax_left, ax_bottom, ax_width, ax_height])
105        plot_2d(model, opts, ax2d)
106        ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height])
107        plot_1d(model, opts, ax1d)
108        #ax.set_aspect('square')
109    else:
110        plot_height = fig_height - (fig_top+fig_bottom)
111        plot_width = (1+np.sqrt(5))/2*fig_height
112        fig_width = plot_width + fig_left + fig_right
113        ax_left = fig_left/fig_width
114        ax_bottom = fig_bottom/fig_height
115        ax_width = plot_width/fig_width
116        ax_height = plot_height/fig_height
117        aspect = (fig_width, fig_height)
118        fig = plt.figure(figsize=aspect)
119        ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height])
120        plot_1d(model, opts, ax1d)
121
122    if model_info.profile:
123        plot_profile_inset(model_info, ax1d)
124
125    # Save image in model/img
126    path = os.path.join('model', 'img', figfile(model_info))
127    plt.savefig(path, bbox_inches='tight')
128    #print("figure saved in",path)
129
130def copy_if_newer(src, dst):
131    from os.path import dirname, exists, getmtime
132    import shutil
133    if not exists(dst):
134        path = dirname(dst)
135        if not exists(path):
136            os.makedirs(path)
137        shutil.copy2(src, dst)
138    elif getmtime(src) > getmtime(dst):
139        shutil.copy2(src, dst)
140
141def link_sources(model_info):
142    from os.path import basename, dirname, realpath, join as joinpath
143
144    # List source files in reverse order of dependency.
145    model_file = basename(model_info.filename)
146    sources = list(reversed(model_info.source + [model_file]))
147
148    # Copy files to src dir under models directory.  Need to do this
149    # because sphinx can't link to an absolute path.
150    root = dirname(dirname(realpath(__file__)))
151    src = joinpath(root, "sasmodels", "models")
152    dst = joinpath(root, "doc", "model", "src")
153    for path in sources:
154        copy_if_newer(joinpath(src, path), joinpath(dst, path))
155
156    # Link to local copy of the files
157    downloads = [":download:`%s <src/%s>`"%(path, path) for path in sources]
158
159    # Link to github repo (either the tagged sasmodels version or master)
160    url = "https://github.com/SasView/sasmodels/blob/v%s"%sasmodels.__version__
161    #url = "https://github.com/SasView/sasmodels/blob/master"%sasmodels.__version__
162    links = ["`%s <%s/sasmodels/models/%s>`_"%(path, url, path) for path in sources]
163
164    sep = u"\n\\ \u25E6 \\ "  # bullet
165    body = "\n**Source**\n"
166    #body += "\n\\ " + sep.join(links) + "\n\n"
167    body += "\n\\ " + sep.join(downloads) + "\n\n"
168    return body
169
170def gen_docs(model_info):
171    # type: (ModelInfo) -> None
172    """
173    Generate the doc string with the figure inserted before the references.
174    """
175
176    # Load the doc string from the module definition file and store it in rst
177    docstr = generate.make_doc(model_info)
178
179    # Auto caption for figure
180    captionstr = '\n'
181    captionstr += '.. figure:: img/' + figfile(model_info) + '\n'
182    captionstr += '\n'
183    if model_info.parameters.has_2d:
184        captionstr += '    1D and 2D plots corresponding to the default parameters of the model.\n'
185    else:
186        captionstr += '    1D plot corresponding to the default parameters of the model.\n'
187    captionstr += '\n'
188
189    # Add figure reference and caption to documentation (at end, before References)
190    pattern = '\*\*REFERENCE'
191    match = re.search(pattern, docstr.upper())
192
193    sources = link_sources(model_info)
194
195    insertion = captionstr + sources
196
197    if match:
198        docstr1 = docstr[:match.start()]
199        docstr2 = docstr[match.start():]
200        docstr = docstr1 + insertion + docstr2
201    else:
202        print('------------------------------------------------------------------')
203        print('References NOT FOUND for model: ', model_info.id)
204        print('------------------------------------------------------------------')
205        docstr += insertion
206
207    open(sys.argv[2],'w').write(docstr)
208
209def process_model(path):
210    # type: (str) -> None
211    """
212    Generate doc file and image file for the given model definition file.
213    """
214
215    # Load the model file
216    model_name = os.path.basename(path)[:-3]
217    model_info = core.load_model_info(model_name)
218
219    # Plotting ranges and options
220    opts = {
221        'xscale'    : 'log',
222        'yscale'    : 'log' if not model_info.structure_factor else 'linear',
223        'zscale'    : 'log' if not model_info.structure_factor else 'linear',
224        'q_min'     : 0.001,
225        'q_max'     : 1.0,
226        'nq'        : 1000,
227        'nq2d'      : 1000,
228        'vmin'      : 1e-3,  # floor for the 2D data results
229        'qx_max'    : 0.5,
230        #'colormap'  : 'gist_ncar',
231        'colormap'  : 'nipy_spectral',
232        #'colormap'  : 'jet',
233    }
234
235    # Generate the RST file and the figure.  Order doesn't matter.
236    gen_docs(model_info)
237    make_figure(model_info, opts)
238
239if __name__ == "__main__":
240    process_model(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.