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