source: sasview/test/sasfit/test/batch_fit.py @ efa5e44

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since efa5e44 was efa5e44, checked in by pkienzle, 9 years ago

remove remaining refs to park/scipy

  • Property mode set to 100644
File size: 6.2 KB
Line 
1import copy
2import time
3import unittest
4from sas.dataloader.loader import Loader
5from sas.fit.Fitting import Fit
6from sas.models.CylinderModel import CylinderModel
7import sas.models.dispersion_models 
8from sas.models.qsmearing import smear_selection
9
10NPTS = 1
11
12
13def classMapper(classInstance, classFunc, *args):
14    """
15    Take an instance of a class and a function name as a string.
16    Execute class.function and return result
17    """
18    return  getattr(classInstance,classFunc)(*args)
19
20def mapapply(arguments):
21    return apply(arguments[0], arguments[1:])
22
23
24class BatchFit:
25    """
26    test fit module
27    """
28    def __init__(self, qmin=None, qmax=None):
29        """ """
30        self.list_of_fitter = []
31        self.list_of_function = []
32        self.param_to_fit = ['scale', 'length', 'radius']
33        self.list_of_constraints = []
34        self.list_of_mapper = []
35        self.polydisp = sas.models.dispersion_models.models
36        self.qmin = qmin
37        self.qmax = qmin
38        self.reset_value()
39       
40    def set_range(self, qmin=None, qmax=None): 
41        self.qmin = qmin
42        self.qmax = qmax
43       
44    def _reset_helper(self, path=None, engine="bumps", npts=NPTS):
45        """
46        Set value to fitter engine and prepare inputs for map function
47        """
48        for i in range(npts):
49            data = Loader().load(path)
50            fitter = Fit(engine)
51            #create model
52            model = CylinderModel()
53            model.setParam('scale', 1.0)
54            model.setParam('radius', 20.0)
55            model.setParam('length', 400.0)
56            model.setParam('sldCyl', 4e-006)
57            model.setParam('sldSolv', 1e-006)
58            model.setParam('background', 0.0)
59            for param in model.dispersion.keys():
60                model.set_dispersion(param, self.polydisp['gaussian']())
61            model.setParam('cyl_phi.width', 10)
62            model.setParam('cyl_phi.npts', 3)
63            model.setParam('cyl_theta.nsigmas', 10)
64            # for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg]
65            fitter.set_model(model, i, self.param_to_fit, 
66                             self.list_of_constraints)
67            #smear data
68            current_smearer = smear_selection(data, model)
69            import cPickle
70            p = cPickle.dumps(current_smearer)
71            sm = cPickle.loads(p)
72            fitter.set_data(data=data, id=i,
73                             smearer=current_smearer, qmin=self.qmin, qmax=self.qmax)
74            fitter.select_problem_for_fit(id=i, value=1)
75            self.list_of_fitter.append(copy.deepcopy(fitter))
76            self.list_of_function.append('fit')
77            self.list_of_mapper.append(classMapper)
78                   
79    def reset_value(self, engine='bumps'):
80        """
81        Initialize inputs for the map function
82        """
83        self.list_of_fitter = []
84        self.list_of_function = []
85        self.param_to_fit = ['scale', 'length', 'radius']
86        self.list_of_constraints = []
87        self.list_of_mapper = []
88
89        path = "testdata_line3.txt"
90        self._reset_helper(path=path, engine=engine, npts=NPTS)
91        path = "testdata_line.txt"
92        self._reset_helper(path=path, engine=engine, npts=NPTS)
93        path = "SILIC010_noheader.DAT"
94        self._reset_helper(path=path, engine=engine, npts=NPTS)
95        path = "cyl_400_20.txt"
96        self._reset_helper(path=path, engine=engine, npts=NPTS)
97        path = "sphere_80.txt"
98        self._reset_helper(path=path, engine=engine, npts=NPTS)
99        path = "PolySpheres.txt"
100        self._reset_helper(path=path, engine=engine, npts=NPTS)
101        path = "latex_qdev.txt"
102        self._reset_helper(path=path, engine=engine, npts=NPTS)
103        path = "latex_qdev2.txt"
104        self._reset_helper(path=path, engine=engine, npts=NPTS)
105       
106     
107    def test_map_fit(self, n=0):
108        """
109        """
110        if n > 0:
111            self._test_process_map_fit(n=n)
112        else:
113            results =  map(classMapper,self.list_of_fitter, self.list_of_function)
114            print len(results)
115            for result in results:
116                print result.fitness, result.stderr, result.pvec
117       
118    def test_process_map_fit(self, n=1):
119        """
120        run fit usong map , n is the number of processes used
121        """ 
122        t0 = time.time()
123        print "start fit with %s process(es) at %s" % (str(n), time.strftime(" %H:%M:%S", time.localtime(t0)))
124        from multiprocessing import Pool
125        temp = zip(self.list_of_mapper, self.list_of_fitter, self.list_of_function)
126        results =  Pool(n).map(func=mapapply, 
127                               iterable=temp)
128        t1 = time.time()
129        print "got fit results ", time.strftime(" %H:%M:%S", time.localtime(t1)), t1 - t0
130        print len(results)
131        for result in results:
132            print result.fitness, result.stderr, result.pvec
133        t2 = time.time()
134        print "print fit1 results ", time.strftime(" %H:%M:%S", time.localtime(t2)), t2 - t1   
135               
136class testBatch(unittest.TestCase):
137    """
138    fitting
139    """ 
140    def setUp(self):
141        self.test = BatchFit(qmin=None, qmax=None)
142       
143   
144    def test_fit1(self):
145        """test fit with python built in map function---- full range of each data"""
146        self.test.test_map_fit()
147       
148    def test_fit2(self):
149        """test fit with python built in map function---- common range for all data"""
150        self.test.set_range(qmin=0.013, qmax=0.05)
151        self.test.reset_value()
152        self.test.test_map_fit()
153        raise Exception("fail")
154       
155    def test_fit3(self):
156        """test fit with data full range using 1 processor and map"""
157        self.test.set_range(qmin=None, qmax=None)
158        self.test.reset_value()
159        self.test.test_map_fit(n=1)
160       
161    def test_fit4(self):
162        """test fit with a common fixed range for data using 1 processor and map"""
163        self.test.set_range(qmin=-1, qmax=10)
164        self.test.reset_value()
165        self.test.test_map_fit(n=3)
166       
167           
168if __name__ == '__main__':
169   unittest.main()
170   
171   
172   
Note: See TracBrowser for help on using the repository browser.