[e5df560] | 1 | |
---|
| 2 | import math |
---|
| 3 | import numpy |
---|
| 4 | import copy |
---|
| 5 | import time |
---|
| 6 | import unittest |
---|
[d48858da] | 7 | from sans.dataloader.loader import Loader |
---|
[e5df560] | 8 | from sans.fit.Fitting import Fit |
---|
| 9 | from sans.models.CylinderModel import CylinderModel |
---|
| 10 | import sans.models.dispersion_models |
---|
| 11 | from sans.models.qsmearing import smear_selection |
---|
| 12 | |
---|
| 13 | NPTS = 1 |
---|
| 14 | |
---|
[d48858da] | 15 | |
---|
| 16 | |
---|
| 17 | |
---|
[e5df560] | 18 | def 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 | |
---|
| 25 | def mapapply(arguments): |
---|
| 26 | return apply(arguments[0], arguments[1:]) |
---|
| 27 | |
---|
| 28 | |
---|
[d48858da] | 29 | |
---|
[e5df560] | 30 | class BatchScipyFit: |
---|
| 31 | """ |
---|
[d48858da] | 32 | test fit module |
---|
[e5df560] | 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 = [] |
---|
[d48858da] | 91 | engine ="scipy" |
---|
[e5df560] | 92 | |
---|
| 93 | path = "testdata_line3.txt" |
---|
[d48858da] | 94 | self._reset_helper(path=path, engine=engine, npts=NPTS) |
---|
[e5df560] | 95 | path = "testdata_line.txt" |
---|
[d48858da] | 96 | self._reset_helper(path=path, engine=engine, npts=NPTS) |
---|
[e5df560] | 97 | path = "SILIC010_noheader.DAT" |
---|
[d48858da] | 98 | self._reset_helper(path=path, engine=engine, npts=NPTS) |
---|
[e5df560] | 99 | path = "cyl_400_20.txt" |
---|
[d48858da] | 100 | self._reset_helper(path=path, engine=engine, npts=NPTS) |
---|
[e5df560] | 101 | path = "sphere_80.txt" |
---|
[d48858da] | 102 | self._reset_helper(path=path, engine=engine, npts=NPTS) |
---|
[e5df560] | 103 | path = "PolySpheres.txt" |
---|
[d48858da] | 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) |
---|
[e5df560] | 109 | |
---|
| 110 | def test_map_fit(self): |
---|
| 111 | """ |
---|
| 112 | """ |
---|
| 113 | results = map(classMapper,self.list_of_fitter, self.list_of_function) |
---|
[d48858da] | 114 | print len(results) |
---|
| 115 | for result in results: |
---|
| 116 | print result.fitness, result.stderr, result.pvec |
---|
[e5df560] | 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() |
---|
[d48858da] | 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 |
---|
[e5df560] | 135 | |
---|
| 136 | class 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 | |
---|
[d48858da] | 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() |
---|
[e5df560] | 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 | |
---|
[d48858da] | 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) |
---|
[e5df560] | 164 | |
---|
| 165 | |
---|
| 166 | if __name__ == '__main__': |
---|
| 167 | unittest.main() |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | |
---|