source: sasview/park_integration/test/batch_fit.py @ fd08130c

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

write test for fit with map function

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