source: sasmodels/sasmodels/generate.py @ 68e7f9d

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 68e7f9d was 68e7f9d, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

remove pyc file after loading custom model

  • Property mode set to 100644
File size: 31.9 KB
Line 
1"""
2SAS model constructor.
3
4Small angle scattering models are defined by a set of kernel functions:
5
6    *Iq(q, p1, p2, ...)* returns the scattering at q for a form with
7    particular dimensions averaged over all orientations.
8
9    *Iqxy(qx, qy, p1, p2, ...)* returns the scattering at qx, qy for a form
10    with particular dimensions for a single orientation.
11
12    *Imagnetic(qx, qy, result[], p1, p2, ...)* returns the scattering for the
13    polarized neutron spin states (up-up, up-down, down-up, down-down) for
14    a form with particular dimensions for a single orientation.
15
16    *form_volume(p1, p2, ...)* returns the volume of the form with particular
17    dimension.
18
19    *ER(p1, p2, ...)* returns the effective radius of the form with
20    particular dimensions.
21
22    *VR(p1, p2, ...)* returns the volume ratio for core-shell style forms.
23
24    #define INVALID(v) (expr)  returns False if v.parameter is invalid
25    for some parameter or other (e.g., v.bell_radius < v.radius).  If
26    necessary, the expression can call a function.
27
28These functions are defined in a kernel module .py script and an associated
29set of .c files.  The model constructor will use them to create models with
30polydispersity across volume and orientation parameters, and provide
31scale and background parameters for each model.
32
33*Iq*, *Iqxy*, *Imagnetic* and *form_volume* should be stylized C-99
34functions written for OpenCL.  All functions need prototype declarations
35even if the are defined before they are used.  OpenCL does not support
36*#include* preprocessor directives, so instead the list of includes needs
37to be given as part of the metadata in the kernel module definition.
38The included files should be listed using a path relative to the kernel
39module, or if using "lib/file.c" if it is one of the standard includes
40provided with the sasmodels source.  The includes need to be listed in
41order so that functions are defined before they are used.
42
43Floating point values should be declared as *double*.  For single precision
44calculations, *double* will be replaced by *float*.  The single precision
45conversion will also tag floating point constants with "f" to make them
46single precision constants.  When using integral values in floating point
47expressions, they should be expressed as floating point values by including
48a decimal point.  This includes 0., 1. and 2.
49
50OpenCL has a *sincos* function which can improve performance when both
51the *sin* and *cos* values are needed for a particular argument.  Since
52this function does not exist in C99, all use of *sincos* should be
53replaced by the macro *SINCOS(value, sn, cn)* where *sn* and *cn* are
54previously declared *double* variables.  When compiled for systems without
55OpenCL, *SINCOS* will be replaced by *sin* and *cos* calls.   If *value* is
56an expression, it will appear twice in this case; whether or not it will be
57evaluated twice depends on the quality of the compiler.
58
59If the input parameters are invalid, the scattering calculator should
60return a negative number. Particularly with polydispersity, there are
61some sets of shape parameters which lead to nonsensical forms, such
62as a capped cylinder where the cap radius is smaller than the
63cylinder radius.  The polydispersity calculation will ignore these points,
64effectively chopping the parameter weight distributions at the boundary
65of the infeasible region.  The resulting scattering will be set to
66background.  This will work correctly even when polydispersity is off.
67
68*ER* and *VR* are python functions which operate on parameter vectors.
69The constructor code will generate the necessary vectors for computing
70them with the desired polydispersity.
71The kernel module must set variables defining the kernel meta data:
72
73    *id* is an implicit variable formed from the filename.  It will be
74    a valid python identifier, and will be used as the reference into
75    the html documentation, with '_' replaced by '-'.
76
77    *name* is the model name as displayed to the user.  If it is missing,
78    it will be constructed from the id.
79
80    *title* is a short description of the model, suitable for a tool tip,
81    or a one line model summary in a table of models.
82
83    *description* is an extended description of the model to be displayed
84    while the model parameters are being edited.
85
86    *parameters* is the list of parameters.  Parameters in the kernel
87    functions must appear in the same order as they appear in the
88    parameters list.  Two additional parameters, *scale* and *background*
89    are added to the beginning of the parameter list.  They will show up
90    in the documentation as model parameters, but they are never sent to
91    the kernel functions.  Note that *effect_radius* and *volfraction*
92    must occur first in structure factor calculations.
93
94    *category* is the default category for the model.  The category is
95    two level structure, with the form "group:section", indicating where
96    in the manual the model will be located.  Models are alphabetical
97    within their section.
98
99    *source* is the list of C-99 source files that must be joined to
100    create the OpenCL kernel functions.  The files defining the functions
101    need to be listed before the files which use the functions.
102
103    *ER* is a python function defining the effective radius.  If it is
104    not present, the effective radius is 0.
105
106    *VR* is a python function defining the volume ratio.  If it is not
107    present, the volume ratio is 1.
108
109    *form_volume*, *Iq*, *Iqxy*, *Imagnetic* are strings containing the
110    C source code for the body of the volume, Iq, and Iqxy functions
111    respectively.  These can also be defined in the last source file.
112
113    *Iq* and *Iqxy* also be instead be python functions defining the
114    kernel.  If they are marked as *Iq.vectorized = True* then the
115    kernel is passed the entire *q* vector at once, otherwise it is
116    passed values one *q* at a time.  The performance improvement of
117    this step is significant.
118
119    *demo* is a dictionary of parameter=value defining a set of
120    parameters to use by default when *compare* is called.  Any
121    parameter not set in *demo* gets the initial value from the
122    parameter list.  *demo* is mostly needed to set the default
123    polydispersity values for tests.
124
125    *oldname* is the name of the model in sasview before sasmodels
126    was split into its own package, and *oldpars* is a dictionary
127    of *parameter: old_parameter* pairs defining the new names for
128    the parameters.  This is used by *compare* to check the values
129    of the new model against the values of the old model before
130    you are ready to add the new model to sasmodels.
131
132
133An *model_info* dictionary is constructed from the kernel meta data and
134returned to the caller.
135
136The model evaluator, function call sequence consists of q inputs and the return vector,
137followed by the loop value/weight vector, followed by the values for
138the non-polydisperse parameters, followed by the lengths of the
139polydispersity loops.  To construct the call for 1D models, the
140categories *fixed-1d* and *pd-1d* list the names of the parameters
141of the non-polydisperse and the polydisperse parameters respectively.
142Similarly, *fixed-2d* and *pd-2d* provide parameter names for 2D models.
143The *pd-rel* category is a set of those parameters which give
144polydispersitiy as a portion of the value (so a 10% length dispersity
145would use a polydispersity value of 0.1) rather than absolute
146dispersity such as an angle plus or minus 15 degrees.
147
148The *volume* category lists the volume parameters in order for calls
149to volume within the kernel (used for volume normalization) and for
150calls to ER and VR for effective radius and volume ratio respectively.
151
152The *orientation* and *magnetic* categories list the orientation and
153magnetic parameters.  These are used by the sasview interface.  The
154blank category is for parameters such as scale which don't have any
155other marking.
156
157The doc string at the start of the kernel module will be used to
158construct the model documentation web pages.  Embedded figures should
159appear in the subdirectory "img" beside the model definition, and tagged
160with the kernel module name to avoid collision with other models.  Some
161file systems are case-sensitive, so only use lower case characters for
162file names and extensions.
163
164
165The function :func:`make` loads the metadata from the module and returns
166the kernel source.  The function :func:`make_doc` extracts the doc string
167and adds the parameter table to the top.  The function :func:`model_sources`
168returns a list of files required by the model.
169
170Code follows the C99 standard with the following extensions and conditions::
171
172    M_PI_180 = pi/180
173    M_4PI_3 = 4pi/3
174    square(x) = x*x
175    cube(x) = x*x*x
176    sinc(x) = sin(x)/x, with sin(0)/0 -> 1
177    all double precision constants must include the decimal point
178    all double declarations may be converted to half, float, or long double
179    FLOAT_SIZE is the number of bytes in the converted variables
180"""
181from __future__ import print_function
182
183#TODO: determine which functions are useful outside of generate
184#__all__ = ["model_info", "make_doc", "make_source", "convert_type"]
185
186from os.path import abspath, dirname, join as joinpath, exists, basename, \
187    splitext, getmtime
188import re
189import string
190import warnings
191
192import numpy as np
193
194from .modelinfo import ModelInfo, Parameter, make_parameter_table, set_demo
195from .custom import load_custom_kernel_module
196
197# TODO: identify model files which have changed since loading and reload them.
198
199TEMPLATE_ROOT = dirname(__file__)
200
201F16 = np.dtype('float16')
202F32 = np.dtype('float32')
203F64 = np.dtype('float64')
204try:  # CRUFT: older numpy does not support float128
205    F128 = np.dtype('float128')
206except TypeError:
207    F128 = None
208
209# Conversion from units defined in the parameter table for each model
210# to units displayed in the sphinx documentation.
211RST_UNITS = {
212    "Ang": "|Ang|",
213    "1/Ang": "|Ang^-1|",
214    "1/Ang^2": "|Ang^-2|",
215    "1e-6/Ang^2": "|1e-6Ang^-2|",
216    "degrees": "degree",
217    "1/cm": "|cm^-1|",
218    "Ang/cm": "|Ang*cm^-1|",
219    "g/cm3": "|g/cm^3|",
220    "mg/m2": "|mg/m^2|",
221    "": "None",
222    }
223
224# Headers for the parameters tables in th sphinx documentation
225PARTABLE_HEADERS = [
226    "Parameter",
227    "Description",
228    "Units",
229    "Default value",
230    ]
231
232# Minimum width for a default value (this is shorter than the column header
233# width, so will be ignored).
234PARTABLE_VALUE_WIDTH = 10
235
236# Documentation header for the module, giving the model name, its short
237# description and its parameter table.  The remainder of the doc comes
238# from the module docstring.
239DOC_HEADER = """.. _%(id)s:
240
241%(name)s
242=======================================================
243
244%(title)s
245
246%(parameters)s
247
248%(returns)s
249
250%(docs)s
251"""
252
253def format_units(units):
254    """
255    Convert units into ReStructured Text format.
256    """
257    return "string" if isinstance(units, list) else RST_UNITS.get(units, units)
258
259def make_partable(pars):
260    """
261    Generate the parameter table to include in the sphinx documentation.
262    """
263    column_widths = [
264        max(len(p.name) for p in pars),
265        max(len(p.description) for p in pars),
266        max(len(format_units(p.units)) for p in pars),
267        PARTABLE_VALUE_WIDTH,
268        ]
269    column_widths = [max(w, len(h))
270                     for w, h in zip(column_widths, PARTABLE_HEADERS)]
271
272    sep = " ".join("="*w for w in column_widths)
273    lines = [
274        sep,
275        " ".join("%-*s" % (w, h)
276                 for w, h in zip(column_widths, PARTABLE_HEADERS)),
277        sep,
278        ]
279    for p in pars:
280        lines.append(" ".join([
281            "%-*s" % (column_widths[0], p.name),
282            "%-*s" % (column_widths[1], p.description),
283            "%-*s" % (column_widths[2], format_units(p.units)),
284            "%*g" % (column_widths[3], p.default),
285            ]))
286    lines.append(sep)
287    return "\n".join(lines)
288
289def _search(search_path, filename):
290    """
291    Find *filename* in *search_path*.
292
293    Raises ValueError if file does not exist.
294    """
295    for path in search_path:
296        target = joinpath(path, filename)
297        if exists(target):
298            return target
299    raise ValueError("%r not found in %s" % (filename, search_path))
300
301
302def model_sources(model_info):
303    """
304    Return a list of the sources file paths for the module.
305    """
306    search_path = [dirname(model_info['filename']),
307                   abspath(joinpath(dirname(__file__), 'models'))]
308    return [_search(search_path, f) for f in model_info['source']]
309
310def timestamp(model_info):
311    """
312    Return a timestamp for the model corresponding to the most recently
313    changed file or dependency.
314    """
315    source_files = (model_sources(model_info)
316                    + model_templates()
317                    + [model_info['filename']])
318    newest = max(getmtime(f) for f in source_files)
319    return newest
320
321def convert_type(source, dtype):
322    """
323    Convert code from double precision to the desired type.
324
325    Floating point constants are tagged with 'f' for single precision or 'L'
326    for long double precision.
327    """
328    if dtype == F16:
329        fbytes = 2
330        source = _convert_type(source, "float", "f")
331    elif dtype == F32:
332        fbytes = 4
333        source = _convert_type(source, "float", "f")
334    elif dtype == F64:
335        fbytes = 8
336        # no need to convert if it is already double
337    elif dtype == F128:
338        fbytes = 16
339        source = _convert_type(source, "long double", "L")
340    else:
341        raise ValueError("Unexpected dtype in source conversion: %s"%dtype)
342    return ("#define FLOAT_SIZE %d\n"%fbytes)+source
343
344
345def _convert_type(source, type_name, constant_flag):
346    """
347    Replace 'double' with *type_name* in *source*, tagging floating point
348    constants with *constant_flag*.
349    """
350    # Convert double keyword to float/long double/half.
351    # Accept an 'n' # parameter for vector # values, where n is 2, 4, 8 or 16.
352    # Assume complex numbers are represented as cdouble which is typedef'd
353    # to double2.
354    source = re.sub(r'(^|[^a-zA-Z0-9_]c?)double(([248]|16)?($|[^a-zA-Z0-9_]))',
355                    r'\1%s\2'%type_name, source)
356    # Convert floating point constants to single by adding 'f' to the end,
357    # or long double with an 'L' suffix.  OS/X complains if you don't do this.
358    source = re.sub(r'[^a-zA-Z_](\d*[.]\d+|\d+[.]\d*)([eE][+-]?\d+)?',
359                    r'\g<0>%s'%constant_flag, source)
360    return source
361
362
363def kernel_name(model_info, is_2d):
364    """
365    Name of the exported kernel symbol.
366    """
367    return model_info['name'] + "_" + ("Iqxy" if is_2d else "Iq")
368
369
370def indent(s, depth):
371    """
372    Indent a string of text with *depth* additional spaces on each line.
373    """
374    spaces = " "*depth
375    sep = "\n" + spaces
376    return spaces + sep.join(s.split("\n"))
377
378
379_template_cache = {}
380def load_template(filename):
381    path = joinpath(TEMPLATE_ROOT, filename)
382    mtime = getmtime(path)
383    if filename not in _template_cache or mtime > _template_cache[filename][0]:
384        with open(path) as fid:
385            _template_cache[filename] = (mtime, fid.read(), path)
386    return _template_cache[filename][1]
387
388def model_templates():
389    # TODO: fails DRY; templates are listed in two places.
390    # should instead have model_info contain a list of paths
391    return [joinpath(TEMPLATE_ROOT, filename)
392            for filename in ('kernel_header.c', 'kernel_iq.c')]
393
394
395_FN_TEMPLATE = """\
396double %(name)s(%(pars)s);
397double %(name)s(%(pars)s) {
398    %(body)s
399}
400
401
402"""
403
404def _gen_fn(name, pars, body):
405    """
406    Generate a function given pars and body.
407
408    Returns the following string::
409
410         double fn(double a, double b, ...);
411         double fn(double a, double b, ...) {
412             ....
413         }
414    """
415    par_decl = ', '.join(p.as_function_argument() for p in pars) if pars else 'void'
416    return _FN_TEMPLATE % {'name': name, 'body': body, 'pars': par_decl}
417
418def _call_pars(prefix, pars):
419    """
420    Return a list of *prefix.parameter* from parameter items.
421    """
422    return [p.as_call_reference(prefix) for p in pars]
423
424_IQXY_PATTERN = re.compile("^((inline|static) )? *(double )? *Iqxy *([(]|$)",
425                           flags=re.MULTILINE)
426def _have_Iqxy(sources):
427    """
428    Return true if any file defines Iqxy.
429
430    Note this is not a C parser, and so can be easily confused by
431    non-standard syntax.  Also, it will incorrectly identify the following
432    as having Iqxy::
433
434        /*
435        double Iqxy(qx, qy, ...) { ... fill this in later ... }
436        */
437
438    If you want to comment out an Iqxy function, use // on the front of the
439    line instead.
440    """
441    for code in sources:
442        if _IQXY_PATTERN.search(code):
443            return True
444    else:
445        return False
446
447def make_source(model_info):
448    """
449    Generate the OpenCL/ctypes kernel from the module info.
450
451    Uses source files found in the given search path.
452    """
453    if callable(model_info['Iq']):
454        return None
455
456    # TODO: need something other than volume to indicate dispersion parameters
457    # No volume normalization despite having a volume parameter.
458    # Thickness is labelled a volume in order to trigger polydispersity.
459    # May want a separate dispersion flag, or perhaps a separate category for
460    # disperse, but not volume.  Volume parameters also use relative values
461    # for the distribution rather than the absolute values used by angular
462    # dispersion.  Need to be careful that necessary parameters are available
463    # for computing volume even if we allow non-disperse volume parameters.
464
465    partable = model_info['parameters']
466
467    # Identify parameters for Iq, Iqxy, Iq_magnetic and form_volume.
468    # Note that scale and volume are not possible types.
469
470    # Load templates and user code
471    kernel_header = load_template('kernel_header.c')
472    kernel_code = load_template('kernel_iq.c')
473    user_code = [open(f).read() for f in model_sources(model_info)]
474
475    # Build initial sources
476    source = [kernel_header] + user_code
477
478    # Make parameters for q, qx, qy so that we can use them in declarations
479    q, qx, qy = [Parameter(name=v) for v in ('q', 'qx', 'qy')]
480    # Generate form_volume function, etc. from body only
481    if model_info['form_volume'] is not None:
482        pars = partable.form_volume_parameters
483        source.append(_gen_fn('form_volume', pars, model_info['form_volume']))
484    if model_info['Iq'] is not None:
485        pars = [q] + partable.iq_parameters
486        source.append(_gen_fn('Iq', pars, model_info['Iq']))
487    if model_info['Iqxy'] is not None:
488        pars = [qx, qy] + partable.iqxy_parameters
489        source.append(_gen_fn('Iqxy', pars, model_info['Iqxy']))
490
491    # Define the parameter table
492    source.append("#define PARAMETER_TABLE \\")
493    source.append("\\\n".join(p.as_definition()
494                              for p in partable.kernel_parameters))
495
496    # Define the function calls
497    if partable.form_volume_parameters:
498        refs = _call_pars("v.", partable.form_volume_parameters)
499        call_volume = "#define CALL_VOLUME(v) form_volume(%s)" % (",".join(refs))
500    else:
501        # Model doesn't have volume.  We could make the kernel run a little
502        # faster by not using/transferring the volume normalizations, but
503        # the ifdef's reduce readability more than is worthwhile.
504        call_volume = "#define CALL_VOLUME(v) 1.0"
505    source.append(call_volume)
506
507    refs = ["q[i]"] + _call_pars("v.", partable.iq_parameters)
508    call_iq = "#define CALL_IQ(q,i,v) Iq(%s)" % (",".join(refs))
509    if _have_Iqxy(user_code):
510        # Call 2D model
511        refs = ["q[2*i]", "q[2*i+1]"] + _call_pars("v.", partable.iqxy_parameters)
512        call_iqxy = "#define CALL_IQ(q,i,v) Iqxy(%s)" % (",".join(refs))
513    else:
514        # Call 1D model with sqrt(qx^2 + qy^2)
515        warnings.warn("Creating Iqxy = Iq(sqrt(qx^2 + qy^2))")
516        # still defined:: refs = ["q[i]"] + _call_pars("v", iq_parameters)
517        pars_sqrt = ["sqrt(q[2*i]*q[2*i]+q[2*i+1]*q[2*i+1])"] + refs[1:]
518        call_iqxy = "#define CALL_IQ(q,i,v) Iq(%s)" % (",".join(pars_sqrt))
519
520    # Fill in definitions for numbers of parameters
521    source.append("#define MAX_PD %s"%partable.max_pd)
522    source.append("#define NPARS %d"%partable.npars)
523
524    # TODO: allow mixed python/opencl kernels?
525
526    # define the Iq kernel
527    source.append("#define KERNEL_NAME %s_Iq"%model_info['name'])
528    source.append(call_iq)
529    source.append(kernel_code)
530    source.append("#undef CALL_IQ")
531    source.append("#undef KERNEL_NAME")
532
533    # define the Iqxy kernel from the same source with different #defines
534    source.append("#define KERNEL_NAME %s_Iqxy"%model_info['name'])
535    source.append(call_iqxy)
536    source.append(kernel_code)
537    source.append("#undef CALL_IQ")
538    source.append("#undef KERNEL_NAME")
539
540    return '\n'.join(source)
541
542class CoordinationDetails(object):
543    def __init__(self, model_info):
544        parameters = model_info['parameters']
545        max_pd = parameters.max_pd
546        npars = parameters.npars
547        par_offset = 4*max_pd
548        self.details = np.zeros(par_offset + 3*npars + 4, 'i4')
549
550        # generate views on different parts of the array
551        self._pd_par     = self.details[0*max_pd:1*max_pd]
552        self._pd_length  = self.details[1*max_pd:2*max_pd]
553        self._pd_offset  = self.details[2*max_pd:3*max_pd]
554        self._pd_stride  = self.details[3*max_pd:4*max_pd]
555        self._par_offset = self.details[par_offset+0*npars:par_offset+1*npars]
556        self._par_coord  = self.details[par_offset+1*npars:par_offset+2*npars]
557        self._pd_coord   = self.details[par_offset+2*npars:par_offset+3*npars]
558
559        # theta_par is fixed
560        self.details[-1] = parameters.theta_offset
561
562    @property
563    def ctypes(self): return self.details.ctypes
564    @property
565    def pd_par(self): return self._pd_par
566    @property
567    def pd_length(self): return self._pd_length
568    @property
569    def pd_offset(self): return self._pd_offset
570    @property
571    def pd_stride(self): return self._pd_stride
572    @property
573    def pd_coord(self): return self._pd_coord
574    @property
575    def par_coord(self): return self._par_coord
576    @property
577    def par_offset(self): return self._par_offset
578    @property
579    def num_coord(self): return self.details[-2]
580    @num_coord.setter
581    def num_coord(self, v): self.details[-2] = v
582    @property
583    def total_pd(self): return self.details[-3]
584    @total_pd.setter
585    def total_pd(self, v): self.details[-3] = v
586    @property
587    def num_active(self): return self.details[-4]
588    @num_active.setter
589    def num_active(self, v): self.details[-4] = v
590
591    def show(self):
592        print("total_pd", self.total_pd)
593        print("num_active", self.num_active)
594        print("pd_par", self.pd_par)
595        print("pd_length", self.pd_length)
596        print("pd_offset", self.pd_offset)
597        print("pd_stride", self.pd_stride)
598        print("par_offsets", self.par_offset)
599        print("num_coord", self.num_coord)
600        print("par_coord", self.par_coord)
601        print("pd_coord", self.pd_coord)
602        print("theta par", self.details[-1])
603
604def mono_details(model_info):
605    details = CoordinationDetails(model_info)
606    # The zero defaults for monodisperse systems are mostly fine
607    details.par_offset[:] = np.arange(2, len(details.par_offset)+2)
608    return details
609
610def poly_details(model_info, weights):
611    #print("weights",weights)
612    weights = weights[2:] # Skip scale and background
613
614    # Decreasing list of polydispersity lengths
615    # Note: the reversing view, x[::-1], does not require a copy
616    pd_length = np.array([len(w) for w in weights])
617    num_active = np.sum(pd_length>1)
618    if num_active > model_info['parameters'].max_pd:
619        raise ValueError("Too many polydisperse parameters")
620
621    pd_offset = np.cumsum(np.hstack((0, pd_length)))
622    idx = np.argsort(pd_length)[::-1][:num_active]
623    par_length = np.array([max(len(w),1) for w in weights])
624    pd_stride = np.cumprod(np.hstack((1, par_length[idx])))
625    par_offsets = np.cumsum(np.hstack((2, par_length)))
626
627    details = CoordinationDetails(model_info)
628    details.pd_par[:num_active] = idx
629    details.pd_length[:num_active] = pd_length[idx]
630    details.pd_offset[:num_active] = pd_offset[idx]
631    details.pd_stride[:num_active] = pd_stride[:-1]
632    details.par_offset[:] = par_offsets[:-1]
633    details.total_pd = pd_stride[-1]
634    details.num_active = num_active
635    # Without constraints coordinated parameters are just the pd parameters
636    details.par_coord[:num_active] = idx
637    details.pd_coord[:num_active] = 2**np.arange(num_active)
638    details.num_coord = num_active
639    #details.show()
640    return details
641
642def constrained_poly_details(model_info, weights, constraints):
643    # Need to find the independently varying pars and sort them
644    # Need to build a coordination list for the dependent variables
645    # Need to generate a constraints function which takes values
646    # and weights, returning par blocks
647    raise NotImplementedError("Can't handle constraints yet")
648
649
650def create_default_functions(model_info):
651    """
652    Autogenerate missing functions, such as Iqxy from Iq.
653
654    This only works for Iqxy when Iq is written in python. :func:`make_source`
655    performs a similar role for Iq written in C.
656    """
657    if callable(model_info['Iq']) and model_info['Iqxy'] is None:
658        partable = model_info['parameters']
659        if partable.has_2d:
660            raise ValueError("Iqxy model is missing")
661        Iq = model_info['Iq']
662        def Iqxy(qx, qy, **kw):
663            return Iq(np.sqrt(qx**2 + qy**2), **kw)
664        model_info['Iqxy'] = Iqxy
665
666
667def load_kernel_module(model_name):
668    if model_name.endswith('.py'):
669        kernel_module = load_custom_kernel_module(model_name)
670    else:
671        from sasmodels import models
672        __import__('sasmodels.models.'+model_name)
673        kernel_module = getattr(models, model_name, None)
674    return kernel_module
675
676
677def make_model_info(kernel_module):
678    """
679    Interpret the model definition file, categorizing the parameters.
680
681    The module can be loaded with a normal python import statement if you
682    know which module you need, or with __import__('sasmodels.model.'+name)
683    if the name is in a string.
684
685    The *model_info* structure contains the following fields:
686
687    * *id* is the id of the kernel
688    * *name* is the display name of the kernel
689    * *filename* is the full path to the module defining the file (if any)
690    * *title* is a short description of the kernel
691    * *description* is a long description of the kernel (this doesn't seem
692      very useful since the Help button on the model page brings you directly
693      to the documentation page)
694    * *docs* is the docstring from the module.  Use :func:`make_doc` to
695    * *category* specifies the model location in the docs
696    * *parameters* is the model parameter table
697    * *single* is True if the model allows single precision
698    * *structure_factor* is True if the model is useable in a product
699    * *variant_info* contains the information required to select between
700      model variants (e.g., the list of cases) or is None if there are no
701      model variants
702    * *par_type* categorizes the model parameters. See
703      :func:`categorize_parameters` for details.
704    * *demo* contains the *{parameter: value}* map used in compare (and maybe
705      for the demo plot, if plots aren't set up to use the default values).
706      If *demo* is not given in the file, then the default values will be used.
707    * *tests* is a set of tests that must pass
708    * *source* is the list of library files to include in the C model build
709    * *Iq*, *Iqxy*, *form_volume*, *ER*, *VR* and *sesans* are python functions
710      implementing the kernel for the module, or None if they are not
711      defined in python
712    * *oldname* is the model name in pre-4.0 Sasview
713    * *oldpars* is the *{new: old}* parameter translation table
714      from pre-4.0 Sasview
715    * *composition* is None if the model is independent, otherwise it is a
716      tuple with composition type ('product' or 'mixture') and a list of
717      *model_info* blocks for the composition objects.  This allows us to
718      build complete product and mixture models from just the info.
719    """
720    # TODO: maybe turn model_info into a class ModelDefinition
721    #print("make parameter table", kernel_module.parameters)
722    parameters = make_parameter_table(kernel_module.parameters)
723    filename = abspath(kernel_module.__file__)
724    kernel_id = splitext(basename(filename))[0]
725    name = getattr(kernel_module, 'name', None)
726    if name is None:
727        name = " ".join(w.capitalize() for w in kernel_id.split('_'))
728    model_info = dict(
729        id=kernel_id,  # string used to load the kernel
730        filename=abspath(kernel_module.__file__),
731        name=name,
732        title=getattr(kernel_module, 'title', name+" model"),
733        description=getattr(kernel_module, 'description', 'no description'),
734        parameters=parameters,
735        composition=None,
736        docs=kernel_module.__doc__,
737        category=getattr(kernel_module, 'category', None),
738        single=getattr(kernel_module, 'single', True),
739        structure_factor=getattr(kernel_module, 'structure_factor', False),
740        profile_axes=getattr(kernel_module, 'profile_axes', ['x','y']),
741        variant_info=getattr(kernel_module, 'invariant_info', None),
742        demo=getattr(kernel_module, 'demo', None),
743        source=getattr(kernel_module, 'source', []),
744        oldname=getattr(kernel_module, 'oldname', None),
745        oldpars=getattr(kernel_module, 'oldpars', {}),
746        tests=getattr(kernel_module, 'tests', []),
747        )
748    set_demo(model_info, getattr(kernel_module, 'demo', None))
749    # Check for optional functions
750    functions = "ER VR form_volume Iq Iqxy profile sesans".split()
751    model_info.update((k, getattr(kernel_module, k, None)) for k in functions)
752    create_default_functions(model_info)
753    # Precalculate the monodisperse parameters
754    # TODO: make this a lazy evaluator
755    # make_model_info is called for every model on sasview startup
756    model_info['mono_details'] = mono_details(model_info)
757    return model_info
758
759section_marker = re.compile(r'\A(?P<first>[%s])(?P=first)*\Z'
760                            %re.escape(string.punctuation))
761def _convert_section_titles_to_boldface(lines):
762    """
763    Do the actual work of identifying and converting section headings.
764    """
765    prior = None
766    for line in lines:
767        if prior is None:
768            prior = line
769        elif section_marker.match(line):
770            if len(line) >= len(prior):
771                yield "".join(("**", prior, "**"))
772                prior = None
773            else:
774                yield prior
775                prior = line
776        else:
777            yield prior
778            prior = line
779    if prior is not None:
780        yield prior
781
782def convert_section_titles_to_boldface(s):
783    """
784    Use explicit bold-face rather than section headings so that the table of
785    contents is not polluted with section names from the model documentation.
786
787    Sections are identified as the title line followed by a line of punctuation
788    at least as long as the title line.
789    """
790    return "\n".join(_convert_section_titles_to_boldface(s.split('\n')))
791
792def make_doc(model_info):
793    """
794    Return the documentation for the model.
795    """
796    Iq_units = "The returned value is scaled to units of |cm^-1| |sr^-1|, absolute scale."
797    Sq_units = "The returned value is a dimensionless structure factor, $S(q)$."
798    docs = convert_section_titles_to_boldface(model_info['docs'])
799    subst = dict(id=model_info['id'].replace('_', '-'),
800                 name=model_info['name'],
801                 title=model_info['title'],
802                 parameters=make_partable(model_info['parameters']),
803                 returns=Sq_units if model_info['structure_factor'] else Iq_units,
804                 docs=docs)
805    return DOC_HEADER % subst
806
807
808def demo_time():
809    """
810    Show how long it takes to process a model.
811    """
812    from .models import cylinder
813    import datetime
814    tic = datetime.datetime.now()
815    make_source(make_model_info(cylinder))
816    toc = (datetime.datetime.now() - tic).total_seconds()
817    print("time: %g"%toc)
818
819def main():
820    """
821    Program which prints the source produced by the model.
822    """
823    import sys
824    if len(sys.argv) <= 1:
825        print("usage: python -m sasmodels.generate modelname")
826    else:
827        name = sys.argv[1]
828        kernel_module = load_kernel_module(name)
829        model_info = make_model_info(kernel_module)
830        source = make_source(model_info)
831        print(source)
832
833if __name__ == "__main__":
834    main()
Note: See TracBrowser for help on using the repository browser.