Changeset 735507b in sasmodels for doc/genmodel.py


Ignore:
Timestamp:
Mar 18, 2016 4:12:48 AM (8 years ago)
Author:
gonzalezm
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
2622b3f
Parents:
03e8a6e (diff), c094758 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of https://github.com/SasView/sasmodels

Conflicts:

doc/genmodel.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/genmodel.py

    r03e8a6e r735507b  
    1010# Convert ../sasmodels/models/name.py to name 
    1111model_name = os.path.basename(sys.argv[1])[:-3] 
     12model_info = core.load_model_info(model_name) 
     13model = core.build_model(model_info) 
    1214 
    1315# 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 
     16docstr = generate.make_doc(model_info) 
    1717 
    18 info = core.load_model_info(model_name) 
    1918 
    2019# Calculate 1D curve for default parameters 
    21 pars = dict((p[0], p[2]) for p in info['parameters']) 
     20pars = dict((p.name, p.default) for p in model_info['parameters']) 
    2221 
    2322# Plotting ranges and options 
    2423opts = { 
    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, 
     24    'xscale'    : 'log', 
     25    'yscale'    : 'log' if not model_info['structure_factor'] else 'linear', 
     26    'zscale'    : 'log' if not model_info['structure_factor'] else 'linear', 
     27    'q_min'     : 0.001, 
     28    'q_max'     : 1.0, 
     29    'nq'        : 1000, 
     30    'nq2d'      : 400, 
     31    'vmin'      : 1e-3,  # floor for the 2D data results 
     32    'qx_max'    : 0.5, 
    3333} 
    3434 
    35 qmin, qmax, nq = opts['qmin'], opts['qmax'], opts['nq'] 
    36 qmin = math.log10(qmin) 
    37 qmax = math.log10(qmax) 
    38 q = np.logspace(qmin, qmax, nq) 
    39 data = empty_data1D(q) 
    40 model = core.load_model(model_name) 
    41 calculator = DirectModel(data, model) 
    42 Iq1D = calculator() 
    4335 
    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) 
     36def plot_1d(model, opts, ax): 
     37    q_min, q_max, nq = opts['q_min'], opts['q_max'], opts['nq'] 
     38    q_min = math.log10(q_min) 
     39    q_max = math.log10(q_max) 
     40    q = np.logspace(q_min, q_max, nq) 
     41    data = empty_data1D(q) 
     42    calculator = DirectModel(data, model) 
     43    Iq1D = calculator() 
    4844 
    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() 
     45    ax.plot(q, Iq1D, color='blue', lw=2, label=model_info['name']) 
     46    ax.set_xlabel(r'$Q \/(\AA^{-1})$') 
     47    ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$') 
     48    ax.set_xscale(opts['xscale']) 
     49    ax.set_yscale(opts['yscale']) 
     50    #ax.legend(loc='best') 
    5651 
    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 
    70 fig = plt.figure() 
    71 ax = fig.add_subplot(1,1,1) 
    72 ax.plot(q, Iq1D, color='blue', lw=2, label=info['name']) 
    73 ax.set_xlabel(opts['xlabel']) 
    74 ax.set_xscale(opts['xscale'])    
    75 ax.set_ylabel(opts['ylabel']) 
    76 ax.set_yscale(opts['yscale'])   
    77 ax.legend() 
    78   
     52def plot_2d(model, opts, ax): 
     53    qx_max, nq2d = opts['qx_max'], opts['nq2d'] 
     54    q = np.linspace(-qx_max, qx_max, nq2d) 
     55    data2d = empty_data2D(q, resolution=0.0) 
     56    calculator = DirectModel(data2d, model) 
     57    Iq2D = calculator() #background=0) 
     58    Iq2D = Iq2D.reshape(nq2d, nq2d) 
     59    if opts['zscale'] == 'log': 
     60        Iq2D = np.log(np.clip(Iq2D, opts['vmin'], np.inf)) 
     61    h = ax.imshow(Iq2D, interpolation='nearest', aspect=1, origin='upper', 
     62           extent=[-qx_max, qx_max, -qx_max, qx_max], cmap=ice_cm()) 
     63    # , vmin=vmin, vmax=vmax) 
     64    ax.set_xlabel(r'$Q_x \/(\AA^{-1})$') 
     65    ax.set_ylabel(r'$Q_y \/(\AA^{-1})$') 
     66 
     67def ice_cm(): 
     68    from matplotlib._cm import _Blues_data 
     69    from matplotlib import colors 
     70    from matplotlib import rcParams 
     71    def from_white(segments): 
     72        scale = 1.0/segments[0][1] 
     73        return [(k, v*scale, w*scale) for k, v, w in segments] 
     74    ice_data = dict((k,from_white(v)) for k,v in _Blues_data.items()) 
     75    ice = colors.LinearSegmentedColormap("ice", ice_data, rcParams['image.lut']) 
     76    return ice 
     77 
     78 
     79# Generate image  
     80fig_height = 3.0 # in 
     81fig_left = 0.6 # in 
     82fig_right = 0.5 # in 
     83fig_top = 0.6*0.25 # in 
     84fig_bottom = 0.6*0.75 
     85if model_info['has_2d']: 
     86    plot_height = fig_height - (fig_top+fig_bottom) 
     87    plot_width = plot_height 
     88    fig_width = 2*(plot_width + fig_left + fig_right) 
     89    aspect = (fig_width, fig_height) 
     90    ratio = aspect[0]/aspect[1] 
     91    ax_left = fig_left/fig_width 
     92    ax_bottom = fig_bottom/fig_height 
     93    ax_height = plot_height/fig_height 
     94    ax_width = ax_height/ratio # square axes 
     95    fig = plt.figure(figsize=aspect) 
     96    ax2d = fig.add_axes([0.5+ax_left, ax_bottom, ax_width, ax_height]) 
     97    plot_2d(model, opts, ax2d) 
     98    ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height]) 
     99    #ax.set_aspect('square') 
     100else: 
     101    plot_height = fig_height - (fig_top+fig_bottom) 
     102    plot_width = (1+np.sqrt(5))/2*fig_height 
     103    fig_width = plot_width + fig_left + fig_right 
     104    ax_left = fig_left/fig_width 
     105    ax_bottom = fig_bottom/fig_height 
     106    ax_width = plot_width/fig_width 
     107    ax_height = plot_height/fig_height 
     108    aspect = (fig_width, fig_height) 
     109    fig = plt.figure(figsize=aspect) 
     110    ax1d = fig.add_axes([ax_left, ax_bottom, ax_width, ax_height]) 
     111    plot_1d(model, opts, ax1d) 
    79112 
    80113# Save image in model/img 
    81114figname = model_name + '_autogenfig.png' 
    82115filename = os.path.join('model', 'img', figname) 
    83 plt.savefig(filename) 
     116plt.savefig(filename, bbox_inches='tight') 
     117#print "figure saved in",filename 
    84118 
    85119# Auto caption for figure 
    86120captionstr = '\n' 
    87 captionstr += '.. figure:: img/' + model_name + '_autogenfig.png\n' 
     121captionstr += '.. figure:: img/' + model_info['id'] + '_autogenfig.png\n' 
    88122captionstr += '\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' 
    93123captionstr += '    1D plot corresponding to the default parameters of the model.\n' 
    94124captionstr += '\n' 
     
    104134else: 
    105135    print '------------------------------------------------------------------' 
    106     print 'References NOT FOUND for model: ', model_name 
     136    print 'References NOT FOUND for model: ', model_info['id'] 
    107137    print '------------------------------------------------------------------' 
    108138    docstr = docstr + captionstr 
Note: See TracChangeset for help on using the changeset viewer.