1 | import warnings |
---|
2 | |
---|
3 | import numpy as np |
---|
4 | |
---|
5 | from .core import load_model_definition, load_model, make_kernel |
---|
6 | from .core import call_kernel, call_ER, call_VR |
---|
7 | |
---|
8 | class DirectModel: |
---|
9 | def __init__(self, name, q_vectors=None, dtype='single'): |
---|
10 | self.model_definition = load_model_definition(name) |
---|
11 | self.model = load_model(self.model_definition, dtype=dtype) |
---|
12 | if q_vectors is not None: |
---|
13 | q_vectors = [np.ascontiguousarray(q,dtype=dtype) for q in q_vectors] |
---|
14 | self.kernel = make_kernel(self.model, q_vectors) |
---|
15 | def __call__(self, **pars): |
---|
16 | return call_kernel(self.kernel, pars) |
---|
17 | def ER(self, **pars): |
---|
18 | return call_ER(self.model.info, pars) |
---|
19 | def VR(self, **pars): |
---|
20 | return call_VR(self.model.info, pars) |
---|
21 | |
---|
22 | def demo(): |
---|
23 | import sys |
---|
24 | if len(sys.argv) < 3: |
---|
25 | print "usage: python -m sasmodels.direct_model modelname (q|qx,qy) par=val ..." |
---|
26 | sys.exit(1) |
---|
27 | model_name = sys.argv[1] |
---|
28 | call = sys.argv[2].upper() |
---|
29 | if call in ("ER","VR"): |
---|
30 | q_vectors = None |
---|
31 | else: |
---|
32 | values = [float(v) for v in sys.argv[2].split(',')] |
---|
33 | if len(values) == 1: |
---|
34 | q = values[0] |
---|
35 | q_vectors = [[q]] |
---|
36 | elif len(values) == 2: |
---|
37 | qx,qy = values |
---|
38 | q_vectors = [[qx],[qy]] |
---|
39 | else: |
---|
40 | print "use q or qx,qy or ER or VR" |
---|
41 | sys.exit(1) |
---|
42 | model = DirectModel(model_name, q_vectors) |
---|
43 | pars = dict((k,float(v)) |
---|
44 | for pair in sys.argv[3:] |
---|
45 | for k,v in [pair.split('=')]) |
---|
46 | if call == "ER": |
---|
47 | print model.ER(**pars) |
---|
48 | elif call == "VR": |
---|
49 | print model.VR(**pars) |
---|
50 | else: |
---|
51 | Iq = model(**pars) |
---|
52 | print Iq[0] |
---|
53 | |
---|
54 | if __name__ == "__main__": |
---|
55 | demo() |
---|