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