heapyx 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
heapy/__info__.py ADDED
@@ -0,0 +1 @@
1
+ __version__ = '0.1.3'
heapy/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .__info__ import __version__
heapy/auto/__init__.py ADDED
File without changes
heapy/auto/baseline.py ADDED
@@ -0,0 +1,297 @@
1
+ import operator
2
+ import warnings
3
+ import numpy as np
4
+ import pybaselines
5
+ import scipy.sparse as sparse
6
+ from scipy.sparse.linalg import spsolve
7
+ from astropy.stats import sigma_clip, mad_std
8
+ from scipy.interpolate import interp1d, UnivariateSpline
9
+
10
+
11
+
12
+ class Baseline(object):
13
+
14
+ def __init__(self):
15
+ pass
16
+
17
+
18
+ @classmethod
19
+ def set_method(cls, method='drpls'):
20
+ if method == 'drpls':
21
+ pass
22
+ # print(
23
+ # '''
24
+ # +-----------------+-----------+
25
+ # | baseline method | drpls |
26
+ # +-----------------+-----------+
27
+ # ''')
28
+ else:
29
+ raise ValueError('invalid method')
30
+
31
+ cls_ = cls()
32
+ cls_.method = method
33
+ return cls_
34
+
35
+
36
+ def fit(self, x, y, w=None, lam=None, nk=None):
37
+ self.x = np.array(x).astype(float)
38
+ self.y = np.array(y).astype(float)
39
+
40
+ if w is None:
41
+ w = np.ones(len(y))
42
+ if lam is None:
43
+ lam = 10 ** (4 * (np.log10(len(x)) - 1))
44
+ if nk is None:
45
+ nk = 2 * len(x) - 200
46
+
47
+ if self.method == 'snip':
48
+ mo = self.snip(y, x, w=w) # for same binsize
49
+
50
+ elif self.method == 'isnip':
51
+ mo = self.isnip(y, x, w=w) # for same binsize
52
+
53
+ elif self.method == 'drpls':
54
+ mo = pybaselines.whittaker.drpls(y, lam=lam, eta=0.5, weights=w)[0]
55
+
56
+ elif self.method == 'fabc':
57
+ mo = pybaselines.classification.fabc(y, lam=lam, weights=w)[0]
58
+
59
+ elif self.method == 'goldindec':
60
+ mo = pybaselines.polynomial.goldindec(y, x, poly_order=3, peak_ratio=0.1, weights=w)[0]
61
+
62
+ elif self.method == 'mixture':
63
+ mo = pybaselines.spline.mixture_model(y, p=0.001, lam=lam, diff_order=2, num_knots=nk, weights=w)[0]
64
+
65
+ else:
66
+ raise ValueError('invalid method')
67
+
68
+ self.mo = mo
69
+
70
+
71
+ def val(self, x):
72
+ x = np.array(x)
73
+
74
+ if min(x) < min(self.x) or max(x) > max(self.x):
75
+ msg = "External interpolation may be imprecise"
76
+ warnings.warn(msg, InterpWarning, stacklevel=2)
77
+ interp = UnivariateSpline(self.x, self.mo, w=None, k=3, s=0)
78
+ mo = interp(x)
79
+ else:
80
+ interp = interp1d(self.x, self.mo, kind='quadratic')
81
+ mo = interp(x)
82
+
83
+ return mo
84
+
85
+
86
+ @staticmethod
87
+ def speyediff(N, d, format='csc'):
88
+ assert not (d < 0)
89
+ shape = (N - d, N)
90
+ diagonals = np.zeros(2 * d + 1)
91
+ diagonals[d] = 1.
92
+ for i in range(d):
93
+ diff = diagonals[:-1] - diagonals[1:]
94
+ diagonals = diff
95
+ offsets = np.arange(d + 1)
96
+ spmat = sparse.diags(diagonals, offsets, shape, format=format)
97
+ return spmat
98
+
99
+
100
+ @staticmethod
101
+ def whittaker_smooth(self, y, lmbd, d, w):
102
+ y = np.array(y)
103
+ m = len(y)
104
+ y = np.mat(y)
105
+ # E = sparse.eye(m, format='csc')
106
+ D = self.speyediff(m, d, format='csc')
107
+ W = sparse.diags(w, 0, shape=(m, m))
108
+ coefmat = W + lmbd * D.conj().T.dot(D)
109
+ z = spsolve(coefmat, W * y.T)
110
+ return coefmat, z
111
+
112
+
113
+ @staticmethod
114
+ def get_smooth(self, y, w=None, d=None, lmbd=None):
115
+ y = np.array(y)
116
+ m = len(y)
117
+ if w is None:
118
+ w = np.ones(m)
119
+ if d is None:
120
+ d = 3
121
+ if lmbd is None:
122
+ lmbd = 10 ** (4*(np.log10(len(y))-1))
123
+ _, z = self.whittaker_smooth(y, lmbd, d, w)
124
+
125
+ cs = y - z
126
+ for sigma in [3, 2, 1]:
127
+ mask = sigma_clip(cs, sigma=sigma, maxiters=5, stdfunc=mad_std).mask
128
+ w[mask] = 0
129
+ _, z = self.whittaker_smooth(y, lmbd, d, w)
130
+ cs = y - z
131
+
132
+ return z
133
+
134
+
135
+ def snip(self, y, x, w=None, d=None, lmbd=None, inte=None, pr=None, hwi=None, it=None):
136
+ #--------------------------
137
+ #lmbd: smooth hardness
138
+ #it: iterations
139
+ #inte: number of samples
140
+ #hwi: width of window
141
+ #pr: peak ratio
142
+ #for same binsize
143
+ #--------------------------
144
+ y, x = np.array(y), np.array(x)
145
+ m = len(y)
146
+ if w is None:
147
+ w = np.ones(m)
148
+ z = self.get_smooth(y, w, d, lmbd)
149
+
150
+ dt = x[1] - x[0]
151
+ if (it is None):
152
+ it = 5
153
+ if (inte is None):
154
+ #inte = round(len(z)/(10*dt**(1/30)))
155
+ inte = round(len(z) * dt / 5)
156
+ if pr is None:
157
+ pr = 0.4
158
+ if (hwi is None):
159
+ hwi = round(inte / 2 * pr)
160
+
161
+ if (it != 1):
162
+ d1 = np.log10(hwi)
163
+ d2 = 0
164
+ win = np.ceil(np.concatenate((10 ** (d1 + np.arange(0, it - 1, 1) * (d2 - d1) / (np.floor(it) - 1)), [d2])))
165
+ win = np.array(win, dtype=int)
166
+ else:
167
+ win = np.array([hwi], dtype=int)
168
+
169
+ lims = np.linspace(0, z.size - 1, inte + 1)
170
+ lefts = np.array(np.ceil(lims[:-1]), dtype=int)
171
+ rights = np.array(np.floor(lims[1:]), dtype=int)
172
+ samp_x = np.round((lefts + rights) * 0.5)
173
+
174
+ bsl_z = np.zeros(inte)
175
+ for i in range(inte):
176
+ bsl_z[i] = z[lefts[i]:rights[i] + 1].mean()
177
+
178
+ for i in range(it):
179
+ # Current window width
180
+ win0 = win[i]
181
+ # Point-wise iteration to the right
182
+ for j in range(1, inte - 1):
183
+ # Interval cut-off close to edges
184
+ v = min([j, win0, inte - j - 1])
185
+ # Baseline suppression
186
+ a = bsl_z[j - v:j + v + 1].mean()
187
+ bsl_z[j] = min([a, bsl_z[j]])
188
+ # xx[j] = np.mean([a, xx[j]])
189
+ for j in range(1, inte - 1):
190
+ k = inte - j - 1
191
+ # Interval cut-off close to edges
192
+ v = min([j, win0, inte - j - 1])
193
+ # Baseline suppression
194
+ a = bsl_z[k - v:k + v + 1].mean()
195
+ bsl_z[k] = min([a, bsl_z[k]])
196
+ # xx[k] = np.mean([a,xx[k]])
197
+
198
+ samp_x = np.concatenate(([0], samp_x, [z.size - 1]))
199
+ bsl_z = np.concatenate((bsl_z[:1], bsl_z, bsl_z[-1:]))
200
+ idx = np.arange(0, z.size, 1)
201
+
202
+ poly_fit = np.polyfit(samp_x, bsl_z, deg=3)
203
+ poly_z = np.polyval(poly_fit, idx)
204
+
205
+ return poly_z
206
+
207
+
208
+ def isnip(self, y, x, w=None, d=None, lmbd=None, inte=None, pr=None, hwi=None, it=None):
209
+ #--------------------------
210
+ #lmbd: smooth hardness
211
+ #it: iterations
212
+ #inte: number of samples
213
+ #hwi: width of window
214
+ #pr: peak ratio
215
+ #for same binsize
216
+ #--------------------------
217
+ y, x = np.array(y), np.array(x)
218
+ m = len(y)
219
+ if w is None:
220
+ w = np.ones(m)
221
+ z = self.get_smooth(y, w, d, lmbd)
222
+
223
+ dt = x[1] - x[0]
224
+ if (it is None):
225
+ it = 5
226
+ if (inte is None):
227
+ #inte = int(len(z) / 5)
228
+ inte = round(len(z) * dt / 5)
229
+ if pr is None:
230
+ pr = 0.4
231
+ if (hwi is None):
232
+ #hwi = int(10 / dt)
233
+ hwi = round(inte / 2 * pr)
234
+
235
+ if (it != 1):
236
+ d1 = np.log10(hwi)
237
+ d2 = 0
238
+ win = np.ceil(np.concatenate((10 ** (d1 + np.arange(0, it - 1, 1) * (d2 - d1) / (np.floor(it) - 1)), [d2])))
239
+ win = np.array(win, dtype=int)
240
+ else:
241
+ win = np.array([hwi], dtype=int)
242
+
243
+ lims = np.linspace(0, z.size - 1, inte + 1)
244
+ lefts = np.array(np.ceil(lims[:-1]), dtype=int)
245
+ rights = np.array(np.floor(lims[1:]), dtype=int)
246
+ samp_x = np.round((lefts + rights) * 0.5)
247
+
248
+ bsl_z = np.zeros(inte)
249
+ for i in range(inte):
250
+ bsl_z[i] = z[lefts[i]:rights[i] + 1].mean()
251
+
252
+ for i in range(it):
253
+ # Current window width
254
+ win0 = win[i]
255
+ # Point-wise iteration to the right
256
+ for j in range(1, inte - 1):
257
+ # Interval cut-off close to edges
258
+ v = min([j, win0, inte - j - 1])
259
+ # Baseline suppression
260
+ a = bsl_z[j - v:j + v + 1].mean()
261
+ bsl_z[j] = min([a, bsl_z[j]])
262
+ # xx[j] = np.mean([a, xx[j]])
263
+ for j in range(1, inte - 1):
264
+ k = inte - j - 1
265
+ # Interval cut-off close to edges
266
+ v = min([j, win0, inte - j - 1])
267
+ # Baseline suppression
268
+ a = bsl_z[k - v:k + v + 1].mean()
269
+ bsl_z[k] = min([a, bsl_z[k]])
270
+ # xx[k] = np.mean([a,xx[k]])
271
+
272
+ samp_x = np.concatenate(([0], samp_x, [z.size - 1]))
273
+ bsl_z = np.concatenate((bsl_z[:1], bsl_z, bsl_z[-1:]))
274
+ idx = np.arange(0, z.size, 1)
275
+
276
+ poly_fit = np.polyfit(samp_x, bsl_z, deg=3)
277
+ poly_z0 = np.polyval(poly_fit, idx)
278
+
279
+ poly_cs = y - poly_z0
280
+ for sigma in [3, 2, 1]:
281
+ mask = sigma_clip(poly_cs, sigma=sigma, maxiters=5, stdfunc=mad_std).mask
282
+ myfilter = list(map(operator.not_, mask))
283
+ idx_filter = idx[myfilter]
284
+ y_filter = y[myfilter]
285
+
286
+ poly_fit = np.polyfit(idx_filter, y_filter, deg=3)
287
+ poly_z = np.polyval(poly_fit, idx)
288
+ poly_cs = y - poly_z
289
+
290
+ return poly_z
291
+
292
+
293
+ class InterpWarning(UserWarning):
294
+ """
295
+ Issued by self.val for External interpolation.
296
+ """
297
+ pass