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

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

update fit batch and smear

  • Property mode set to 100644
File size: 5.9 KB
Line 
1
2import math
3import numpy
4import copy
5import time
6import unittest
7from sans.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
15
16
17   
18def classMapper(classInstance, classFunc, *args):
19    """
20    Take an instance of a class and a function name as a string.
21    Execute class.function and return result
22    """
23    return  getattr(classInstance,classFunc)(*args)
24
25def mapapply(arguments):
26    return apply(arguments[0], arguments[1:])
27
28
29
30class BatchScipyFit:
31    """
32    test fit module
33    """
34    def __init__(self, qmin=None, qmax=None):
35        """ """
36        self.list_of_fitter = []
37        self.list_of_function = []
38        self.param_to_fit = ['scale', 'length', 'radius']
39        self.list_of_constraints = []
40        self.list_of_mapper = []
41        self.polydisp = sans.models.dispersion_models.models
42        self.qmin = qmin
43        self.qmax = qmin
44        self.reset_value()
45       
46    def set_range(self, qmin=None, qmax=None): 
47        self.qmin = qmin
48        self.qmax = qmax
49       
50    def _reset_helper(self, path=None, engine="scipy", npts=NPTS):
51        """
52        Set value to fitter engine and prepare inputs for map function
53        """
54        for i in range(npts):
55            data = Loader().load(path)
56            fitter = Fit(engine)
57            #create model
58            model = CylinderModel()
59            model.setParam('scale', 1.0)
60            model.setParam('radius', 20.0)
61            model.setParam('length', 400.0)
62            model.setParam('sldCyl', 4e-006)
63            model.setParam('sldSolv', 1e-006)
64            model.setParam('background', 0.0)
65            for param in model.dispersion.keys():
66                model.set_dispersion(param, self.polydisp['gaussian']())
67            model.setParam('cyl_phi.width', 10)
68            model.setParam('cyl_phi.npts', 3)
69            model.setParam('cyl_theta.nsigmas', 10)
70            """ for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg]"""
71            fitter.set_model(model, i, self.param_to_fit, 
72                             self.list_of_constraints)
73            #smear data
74            current_smearer = smear_selection(data, model)
75            fitter.set_data(data=data, id=i,
76                             smearer=current_smearer, qmin=self.qmin, qmax=self.qmax)
77            fitter.select_problem_for_fit(id=i, value=1)
78            self.list_of_fitter.append(copy.deepcopy(fitter))
79            self.list_of_function.append('fit')
80            self.list_of_mapper.append(classMapper)
81                   
82    def reset_value(self):
83        """
84        Initialize inputs for the map function
85        """
86        self.list_of_fitter = []
87        self.list_of_function = []
88        self.param_to_fit = ['scale', 'length', 'radius']
89        self.list_of_constraints = []
90        self.list_of_mapper = []
91        engine ="scipy"
92       
93        path = "testdata_line3.txt"
94        self._reset_helper(path=path, engine=engine, npts=NPTS)
95        path = "testdata_line.txt"
96        self._reset_helper(path=path, engine=engine, npts=NPTS)
97        path = "SILIC010_noheader.DAT"
98        self._reset_helper(path=path, engine=engine, npts=NPTS)
99        path = "cyl_400_20.txt"
100        self._reset_helper(path=path, engine=engine, npts=NPTS)
101        path = "sphere_80.txt"
102        self._reset_helper(path=path, engine=engine, npts=NPTS)
103        path = "PolySpheres.txt"
104        self._reset_helper(path=path, engine=engine, npts=NPTS)
105        path = "latex_qdev.txt"
106        self._reset_helper(path=path, engine=engine, npts=NPTS)
107        path = "latex_qdev2.txt"
108        self._reset_helper(path=path, engine=engine, npts=NPTS)
109     
110    def test_map_fit(self):
111        """
112        """ 
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 = BatchScipyFit(qmin=None, qmax=None)
142   
143    def test_fit1(self):
144        """test fit with python built in map function---- full range of each data"""
145        self.test.test_map_fit()
146       
147    #def test_fit2(self):
148    #   """test fit with python built in map function---- common range for all data"""
149    #    self.test.set_range(qmin=0.013, qmax=0.05)
150    #    self.test.reset_value()
151    #    self.test.test_map_fit()
152       
153    def test_fit3(self):
154        """test fit with data full range using 1 processor and map"""
155        self.test.set_range(qmin=None, qmax=None)
156        self.test.reset_value()
157        self.test.test_process_map_fit(n=1)
158       
159    #def test_fit4(self):
160    #    """test fit with a common fixed range for data using 1 processor and map"""
161    #    self.test.set_range(qmin=0.013, qmax=0.05)
162    #    self.test.reset_value()
163    #    self.test.test_process_map_fit(n=1)
164       
165           
166if __name__ == '__main__':
167   unittest.main()
168   
169   
170   
Note: See TracBrowser for help on using the repository browser.