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