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