Changeset 70bbb74 in sasmodels


Ignore:
Timestamp:
Mar 17, 2016 3:57:42 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:
c5dadbb
Parents:
fcd7bbd
Message:

Autogenerate 1D figures for documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/genmodel.py

    r91c5fdc r70bbb74  
    1 import sys, os 
     1import sys, os, math, re 
     2import numpy as np 
     3import matplotlib.pyplot as plt 
    24sys.path.insert(0, os.path.abspath('..')) 
    35from sasmodels import generate, core 
     6from sasmodels.direct_model import DirectModel 
     7from sasmodels.data import empty_data1D, empty_data2D 
     8 
    49 
    510# Convert ../sasmodels/models/name.py to name 
     
    813# Load the doc string from the module definition file and store it in rst 
    914docstr = 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        'qmin'      : 0.005, 
     28        'qmax'      : 1.0, 
     29        'nq'        : 1000, 
     30        'nq2d'      : 100, 
     31} 
     32 
     33qmin, qmax, nq = opts['qmin'], opts['qmax'], opts['nq'] 
     34qmin = math.log10(qmin) 
     35qmax = math.log10(qmax) 
     36q = np.logspace(qmin, qmax, nq) 
     37data = empty_data1D(q) 
     38model = core.load_model(model_name) 
     39calculator = DirectModel(data, model) 
     40Iq1D = 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 
     68fig = plt.figure() 
     69ax = fig.add_subplot(1,1,1) 
     70ax.plot(q, Iq1D, color='blue', lw=2, label=model_name) 
     71ax.set_xlabel(r'$Q \/(\AA^{-1})$') 
     72ax.set_xscale(opts['xscale'])    
     73ax.set_ylabel(r'$I(Q) \/(\mathrm{cm}^{-1})$') 
     74ax.set_yscale(opts['yscale'])   
     75ax.legend() 
     76  
     77 
     78# Save image in model/img 
     79figname = model_name + '_autogenfig.png' 
     80filename = os.path.join('model', 'img', figname) 
     81plt.savefig(filename) 
     82 
     83# Auto caption for figure 
     84captionstr = '\n' 
     85captionstr += '.. figure:: img/' + model_name + '_autogenfig.png\n' 
     86captionstr += '\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' 
     91captionstr += '    1D plot corresponding to the default parameters of the model.\n' 
     92captionstr += '\n' 
     93 
     94# Add figure reference and caption to documentation (at end, before References) 
     95pattern = '\*\*REFERENCE' 
     96m = re.search(pattern, docstr.upper()) 
     97 
     98if m: 
     99    docstr1 = docstr[:m.start()] 
     100    docstr2 = docstr[m.start():] 
     101    docstr = docstr1 + captionstr + docstr2 
     102else: 
     103    print 'References NOT FOUND for model: ', model_name 
     104    docstr = docstr + captionstr 
     105 
    10106open(sys.argv[2],'w').write(docstr) 
Note: See TracChangeset for help on using the changeset viewer.