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