source: sasview/test/park_integration/test/batch_fit.py @ 4de1fed

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 4de1fed was 4de1fed, checked in by pkienzle, 10 years ago

enable small test unit test

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