source: sasview/park_integration/test/batch_fit.py @ 6550b64

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 6550b64 was d1c8036, checked in by Gervaise Alina <gervyh@…>, 13 years ago

update unittest

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