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