source: sasmodels/doc/genmodel.py @ 03e8a6e

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 03e8a6e was 03e8a6e, checked in by gonzalezm, 8 years ago

Axis labels in options

  • Property mode set to 100644
File size: 3.6 KB
Line 
1import sys, os, math, re
2import numpy as np
3import matplotlib.pyplot as plt
4sys.path.insert(0, os.path.abspath('..'))
5from sasmodels import generate, core
6from sasmodels.direct_model import DirectModel
7from sasmodels.data import empty_data1D, empty_data2D
8
9
10# Convert ../sasmodels/models/name.py to name
11model_name = os.path.basename(sys.argv[1])[:-3]
12
13# Load the doc string from the module definition file and store it in rst
14docstr = generate.make_doc(core.load_model_info(model_name))
15   
16# Generate automatically plot of the model and add it to rst documentation
17
18info = core.load_model_info(model_name)
19
20# Calculate 1D curve for default parameters
21pars = dict((p[0], p[2]) for p in info['parameters'])
22
23# Plotting ranges and options
24opts = {
25        'xscale'    : 'log',
26        'yscale'    : 'log' if not info['structure_factor'] else 'linear',
27        'xlabel'    : '$Q \/(\AA^{-1})$',
28        'ylabel'    : '$I(Q) \/(\mathrm{cm}^{-1})$' if not info['structure_factor'] else '$S(Q) \/(\mathrm{cm}^{-1})$',
29        'qmin'      : 0.001,
30        'qmax'      : 1.0,
31        'nq'        : 1000,
32        'nq2d'      : 100,
33}
34
35qmin, qmax, nq = opts['qmin'], opts['qmax'], opts['nq']
36qmin = math.log10(qmin)
37qmax = math.log10(qmax)
38q = np.logspace(qmin, qmax, nq)
39data = empty_data1D(q)
40model = core.load_model(model_name)
41calculator = DirectModel(data, model)
42Iq1D = calculator()
43
44# TO DO: Generation of 2D plots
45# Problem in sasmodels.direct_model._calc_theory
46# There self._kernel.q_input.nq  gets a value of 0 in the 2D case
47# and returns a 0 numpy array (it does not call the C code)
48
49# If 2D model, compute 2D image
50#if info['has_2d'] != []:
51#    qmax, nq2d = opts['qmax'], opts['nq2d']
52#    data2d = empty_data2D(np.linspace(-qmax, qmax, nq2d), resolution=0.0)
53#    #model = core.load_model(model_name)
54#    calculator = DirectModel(data2d, model)
55#    Iq2D = calculator()
56
57# Generate image (comment IF for 1D/2D for the moment) and generate only 1D
58#if info['has_2d'] == []:
59#    fig = plt.figure()
60#    ax = fig.add_subplot(1,1,1)
61#    ax.plot(q, Iq1D, color='blue', lw=2, label=model_name)
62#    ax.set_xlabel(r'$Q \/(\AA^{-1})$')
63#    ax.set_xscale(opts['xscale'])   
64#    ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$')
65#    ax.set_yscale(opts['yscale']) 
66#    ax.legend()
67#else:
68#    # need figure with 1D + 2D
69#    pass
70fig = plt.figure()
71ax = fig.add_subplot(1,1,1)
72ax.plot(q, Iq1D, color='blue', lw=2, label=info['name'])
73ax.set_xlabel(opts['xlabel'])
74ax.set_xscale(opts['xscale'])   
75ax.set_ylabel(opts['ylabel'])
76ax.set_yscale(opts['yscale']) 
77ax.legend()
78 
79
80# Save image in model/img
81figname = model_name + '_autogenfig.png'
82filename = os.path.join('model', 'img', figname)
83plt.savefig(filename)
84
85# Auto caption for figure
86captionstr = '\n'
87captionstr += '.. figure:: img/' + model_name + '_autogenfig.png\n'
88captionstr += '\n'
89#if info['has_2d'] == []:
90#    captionstr += '    1D plot corresponding to the default parameters of the model.\n'
91#else:
92#    captionstr += '    1D and 2D plots corresponding to the default parameters of the model.\n'
93captionstr += '    1D plot corresponding to the default parameters of the model.\n'
94captionstr += '\n'
95
96# Add figure reference and caption to documentation (at end, before References)
97pattern = '\*\*REFERENCE'
98m = re.search(pattern, docstr.upper())
99
100if m:
101    docstr1 = docstr[:m.start()]
102    docstr2 = docstr[m.start():]
103    docstr = docstr1 + captionstr + docstr2
104else:
105    print '------------------------------------------------------------------'
106    print 'References NOT FOUND for model: ', model_name
107    print '------------------------------------------------------------------'
108    docstr = docstr + captionstr
109
110open(sys.argv[2],'w').write(docstr)
Note: See TracBrowser for help on using the repository browser.