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