1 | |
---|
2 | #import unittest |
---|
3 | import math |
---|
4 | import numpy |
---|
5 | #import sys |
---|
6 | #import string |
---|
7 | import copy |
---|
8 | from sans.pr.invertor import Invertor |
---|
9 | |
---|
10 | class Num_terms(): |
---|
11 | """ |
---|
12 | """ |
---|
13 | def __init__(self, invertor): |
---|
14 | """ |
---|
15 | """ |
---|
16 | self.invertor = invertor |
---|
17 | self.nterm_min = 10 |
---|
18 | self.nterm_max = len(self.invertor.x) |
---|
19 | if self.nterm_max > 50: |
---|
20 | self.nterm_max = 50 |
---|
21 | self.isquit_func = None |
---|
22 | |
---|
23 | self.osc_list = [] |
---|
24 | self.err_list = [] |
---|
25 | self.alpha_list = [] |
---|
26 | self.mess_list = [] |
---|
27 | |
---|
28 | self.dataset = [] |
---|
29 | |
---|
30 | def is_odd(self, n): |
---|
31 | """ |
---|
32 | """ |
---|
33 | return bool(n % 2) |
---|
34 | |
---|
35 | def sort_osc(self): |
---|
36 | """ |
---|
37 | """ |
---|
38 | #import copy |
---|
39 | osc = copy.deepcopy(self.dataset) |
---|
40 | lis = [] |
---|
41 | for i in range(len(osc)): |
---|
42 | osc.sort() |
---|
43 | re = osc.pop(0) |
---|
44 | lis.append(re) |
---|
45 | return lis |
---|
46 | |
---|
47 | def median_osc(self): |
---|
48 | """ |
---|
49 | """ |
---|
50 | osc = self.sort_osc() |
---|
51 | dv = len(osc) |
---|
52 | med = float(dv) / 2.0 |
---|
53 | odd = self.is_odd(dv) |
---|
54 | medi = 0 |
---|
55 | for i in range(dv): |
---|
56 | if odd == True: |
---|
57 | medi = osc[int(med)] |
---|
58 | else: |
---|
59 | medi = osc[int(med) - 1] |
---|
60 | return medi |
---|
61 | |
---|
62 | def get0_out(self): |
---|
63 | """ |
---|
64 | """ |
---|
65 | inver = self.invertor |
---|
66 | self.osc_list = [] |
---|
67 | self.err_list = [] |
---|
68 | self.alpha_list = [] |
---|
69 | for k in range(self.nterm_min, self.nterm_max, 1): |
---|
70 | if self.isquit_func != None: |
---|
71 | self.isquit_func() |
---|
72 | best_alpha, message, elapsed = inver.estimate_alpha(k) |
---|
73 | inver.alpha = best_alpha |
---|
74 | inver.out, inver.cov = inver.lstsq(k) |
---|
75 | osc = inver.oscillations(inver.out) |
---|
76 | err = inver.get_pos_err(inver.out, inver.cov) |
---|
77 | if osc > 10.0: |
---|
78 | break |
---|
79 | self.osc_list.append(osc) |
---|
80 | self.err_list.append(err) |
---|
81 | self.alpha_list.append(inver.alpha) |
---|
82 | self.mess_list.append(message) |
---|
83 | |
---|
84 | #print "osc", self.osc_list |
---|
85 | #print "err", self.err_list |
---|
86 | #print "alpha", self.alpha_list |
---|
87 | #new_ls = [] |
---|
88 | new_osc1 = [] |
---|
89 | new_osc2 = [] |
---|
90 | new_osc3 = [] |
---|
91 | flag9 = False |
---|
92 | flag8 = False |
---|
93 | flag7 = False |
---|
94 | for i in range(len(self.err_list)): |
---|
95 | if self.err_list[i] <= 1.0 and self.err_list[i] >= 0.9: |
---|
96 | new_osc1.append(self.osc_list[i]) |
---|
97 | flag9 = True |
---|
98 | if self.err_list[i] < 0.9 and self.err_list[i] >= 0.8: |
---|
99 | new_osc2.append(self.osc_list[i]) |
---|
100 | flag8 = True |
---|
101 | if self.err_list[i] < 0.8 and self.err_list[i] >= 0.7: |
---|
102 | new_osc3.append(self.osc_list[i]) |
---|
103 | falg7 = True |
---|
104 | |
---|
105 | if flag9 == True: |
---|
106 | self.dataset = new_osc1 |
---|
107 | elif flag8 == True: |
---|
108 | self.dataset = new_osc2 |
---|
109 | else: |
---|
110 | self.dataset = new_osc3 |
---|
111 | |
---|
112 | #print "dataset", self.dataset |
---|
113 | return self.dataset |
---|
114 | |
---|
115 | def ls_osc(self): |
---|
116 | """ |
---|
117 | """ |
---|
118 | # Generate data |
---|
119 | ls_osc = self.get0_out() |
---|
120 | med = self.median_osc() |
---|
121 | |
---|
122 | #TODO: check 1 |
---|
123 | ls_osc = self.dataset |
---|
124 | ls = [] |
---|
125 | for i in range(len(ls_osc)): |
---|
126 | if int(med) == int(ls_osc[i]): |
---|
127 | ls.append(ls_osc[i]) |
---|
128 | return ls |
---|
129 | |
---|
130 | def compare_err(self): |
---|
131 | """ |
---|
132 | """ |
---|
133 | ls = self.ls_osc() |
---|
134 | #print "ls", ls |
---|
135 | nt_ls = [] |
---|
136 | for i in range(len(ls)): |
---|
137 | r = ls[i] |
---|
138 | n = self.osc_list.index(r) + 10 |
---|
139 | #er = self.err_list[n] |
---|
140 | #nt = self.osc_list.index(r) + 10 |
---|
141 | nt_ls.append(n) |
---|
142 | #print "nt list", nt_ls |
---|
143 | return nt_ls |
---|
144 | |
---|
145 | def num_terms(self, isquit_func=None): |
---|
146 | """ |
---|
147 | """ |
---|
148 | try: |
---|
149 | self.isquit_func = isquit_func |
---|
150 | #self.nterm_max = len(self.invertor.x) |
---|
151 | #self.nterm_max = 32 |
---|
152 | nts = self.compare_err() |
---|
153 | #print "nts", nts |
---|
154 | div = len(nts) |
---|
155 | tem = float(div)/2.0 |
---|
156 | odd = self.is_odd(div) |
---|
157 | if odd == True: |
---|
158 | nt = nts[int(tem)] |
---|
159 | else: |
---|
160 | nt = nts[int(tem) - 1] |
---|
161 | return nt, self.alpha_list[nt - 10], self.mess_list[nt-10] |
---|
162 | except: |
---|
163 | return self.nterm_min, self.alpha_list[10], self.mess_list[10] |
---|
164 | |
---|
165 | #For testing |
---|
166 | def load(path): |
---|
167 | #import numpy, math, sys |
---|
168 | # Read the data from the data file |
---|
169 | data_x = numpy.zeros(0) |
---|
170 | data_y = numpy.zeros(0) |
---|
171 | data_err = numpy.zeros(0) |
---|
172 | scale = None |
---|
173 | min_err = 0.0 |
---|
174 | if not path == None: |
---|
175 | input_f = open(path,'r') |
---|
176 | buff = input_f.read() |
---|
177 | lines = buff.split('\n') |
---|
178 | for line in lines: |
---|
179 | try: |
---|
180 | toks = line.split() |
---|
181 | test_x = float(toks[0]) |
---|
182 | test_y = float(toks[1]) |
---|
183 | if len(toks) > 2: |
---|
184 | err = float(toks[2]) |
---|
185 | else: |
---|
186 | if scale == None: |
---|
187 | scale = 0.05 * math.sqrt(test_y) |
---|
188 | #scale = 0.05/math.sqrt(y) |
---|
189 | min_err = 0.01*y |
---|
190 | err = scale * math.sqrt(test_y) + min_err |
---|
191 | #err = 0 |
---|
192 | |
---|
193 | data_x = numpy.append(data_x, test_x) |
---|
194 | data_y = numpy.append(data_y, test_y) |
---|
195 | data_err = numpy.append(data_err, err) |
---|
196 | except: |
---|
197 | pass |
---|
198 | |
---|
199 | return data_x, data_y, data_err |
---|
200 | |
---|
201 | |
---|
202 | if __name__ == "__main__": |
---|
203 | i = Invertor() |
---|
204 | x, y, erro = load("test/Cyl_A_D102.txt") |
---|
205 | i.d_max = 102.0 |
---|
206 | i.nfunc = 10 |
---|
207 | #i.q_max = 0.4 |
---|
208 | #i.q_min = 0.07 |
---|
209 | i.x = x |
---|
210 | i.y = y |
---|
211 | i.err = erro |
---|
212 | #i.out, i.cov = i.lstsq(10) |
---|
213 | # Testing estimator |
---|
214 | est = Num_terms(i) |
---|
215 | print est.num_terms() |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | |
---|