def constrained_oasisAR2(y, g, sn, optimize_b=True, b_nonneg=True, optimize_g=0, decimate=5, shift=100, window=None, tol=1e-9, max_iter=1, penalty=1): """ Infer the most likely discretized spike train underlying an AR(2) fluorescence trace Solves the noise constrained sparse non-negative deconvolution problem min |s|_1 subject to |c-y|^2 = sn^2 T and s_t = c_t-g1 c_{t-1}-g2 c_{t-2} >= 0 Parameters: ---------- y : array of float One dimensional array containing the fluorescence intensities (with baseline already subtracted) with one entry per time-bin. g : (float, float) Parameters of the AR(2) process that models the fluorescence impulse response. sn : float Standard deviation of the noise distribution. optimize_b : bool, optional, default True Optimize baseline if True else it is set to 0, see y. b_nonneg: bool, optional, default True Enforce strictly non-negative baseline if True. optimize_g : int, optional, default 0 Number of large, isolated events to consider for optimizing g. No optimization if optimize_g=0. decimate : int, optional, default 5 Decimation factor for estimating hyper-parameters faster on decimated data. shift : int, optional, default 100 Number of frames by which to shift window from on run of NNLS to the next. window : int, optional, default None (200 or larger dependend on g) Window size. tol : float, optional, default 1e-9 Tolerance parameter. max_iter : int, optional, default 1 Maximal number of iterations. penalty : int, optional, default 1 Sparsity penalty. 1: min |s|_1 0: min |s|_0 Returns: ------- c : array of float The inferred denoised fluorescence signal at each time-bin. s : array of float Discretized deconvolved neural activity (spikes). b : float Fluorescence baseline value. (g1, g2) : tuple of float Parameters of the AR(2) process that models the fluorescence impulse response. lam : float Sparsity penalty parameter lambda of dual problem. References: ---------- * Friedrich J and Paninski L, NIPS 2016 * Friedrich J, Zhou P, and Paninski L, arXiv 2016 """ T = len(y) d = (g[0] + sqrt(g[0] * g[0] + 4 * g[1])) / 2 r = (g[0] - sqrt(g[0] * g[0] + 4 * g[1])) / 2 if window is None: window = int(min(T, max(200, -5 / log(d)))) if not optimize_g: g11 = (np.exp(log(d) * np.arange(1, T + 1)) * np.arange(1, T + 1)) if d == r else \ (np.exp(log(d) * np.arange(1, T + 1)) - np.exp(log(r) * np.arange(1, T + 1))) / (d - r) g12 = np.append(0, g[1] * g11[:-1]) g11g11 = np.cumsum(g11 * g11) g11g12 = np.cumsum(g11 * g12) Sg11 = np.cumsum(g11) f_lam = 1 - g[0] - g[1] elif decimate == 0: # need to run AR1 anyways for estimating AR coeffs decimate = 1 thresh = sn * sn * T # get initial estimate of b and lam on downsampled data using AR1 model if decimate > 0: from caiman.source_extraction.cnmf.oasis import oasisAR1, constrained_oasisAR1 _, s, b, aa, lam = constrained_oasisAR1( y[:len(y) // decimate * decimate].reshape(-1, decimate).mean(1), d**decimate, sn / sqrt(decimate), optimize_b=optimize_b, b_nonneg=b_nonneg, optimize_g=optimize_g) if optimize_g: from scipy.optimize import minimize d = aa**(1. / decimate) if decimate > 1: s = oasisAR1(y - b, d, lam=lam * (1 - aa) / (1 - d))[1] r = estimate_time_constant(s, 1, fudge_factor=.98)[0] g[0] = d + r g[1] = -d * r g11 = (np.exp(log(d) * np.arange(1, T + 1)) - np.exp(log(r) * np.arange(1, T + 1))) / (d - r) g12 = np.append(0, g[1] * g11[:-1]) g11g11 = np.cumsum(g11 * g11) g11g12 = np.cumsum(g11 * g12) Sg11 = np.cumsum(g11) f_lam = 1 - g[0] - g[1] elif decimate > 1: s = oasisAR1(y - b, d, lam=lam * (1 - aa) / (1 - d))[1] lam *= (1 - d**decimate) / f_lam # this window size seems necessary and sufficient possible_spikes = [x + np.arange(-2, 3) for x in np.where(s > s.max() / 10.)[0]] ff = np.array(possible_spikes, dtype=np.int).ravel() ff = np.unique(ff[(ff >= 0) * (ff < T)]) mask = np.zeros(T, dtype=bool) mask[ff] = True else: b = np.percentile(y, 15) if optimize_b else 0 lam = 2 * sn * np.linalg.norm(g11) mask = None if b_nonneg: b = max(b, 0) # run ONNLS c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) if not optimize_b: # don't optimize b, just the dual variable lambda for _ in range(max_iter - 1): res = y - c RSS = res.dot(res) if np.abs(RSS - thresh) < 1e-4 * thresh: break # calc shift dlam, here attributed to sparsity penalty tmp = np.empty(T) ls = np.append(np.where(s > 1e-6)[0], T) l = ls[0] tmp[:l] = (1 + d) / (1 + d**l) * \ np.exp(log(d) * np.arange(l)) # first pool for i, f in enumerate(ls[:-1]): # all other pools l = ls[i + 1] - f - 1 # if and elif correct last 2 time points for |s|_1 instead |c|_1 if i == len(ls) - 2: # last pool tmp[f] = (1. / f_lam if l == 0 else (Sg11[l] + g[1] / f_lam * g11[l - 1] + (g[0] + g[1]) / f_lam * g11[l] - g11g12[l] * tmp[f - 1]) / g11g11[l]) # secondlast pool if last one has length 1 elif i == len(ls) - 3 and ls[-2] == T - 1: tmp[f] = (Sg11[l] + g[1] / f_lam * g11[l] - g11g12[l] * tmp[f - 1]) / g11g11[l] else: # all other pools tmp[f] = (Sg11[l] - g11g12[l] * tmp[f - 1]) / g11g11[l] l += 1 tmp[f + 1:f + l] = g11[1:l] * tmp[f] + g12[1:l] * tmp[f - 1] aa = tmp.dot(tmp) bb = res.dot(tmp) cc = RSS - thresh try: db = (-bb + sqrt(bb * bb - aa * cc)) / aa except: db = -bb / aa # perform shift b += db c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) db = np.mean(y - c) - b b += db lam -= db / f_lam else: # optimize b db = max(np.mean(y - c), 0 if b_nonneg else -np.inf) - b b += db lam -= db / (1 - g[0] - g[1]) g_converged = False for _ in range(max_iter - 1): res = y - c - b RSS = res.dot(res) if np.abs(RSS - thresh) < 1e-4 * thresh: break # calc shift db, here attributed to baseline tmp = np.empty(T) ls = np.append(np.where(s > 1e-6)[0], T) l = ls[0] tmp[:l] = (1 + d) / (1 + d**l) * \ np.exp(log(d) * np.arange(l)) # first pool for i, f in enumerate(ls[:-1]): # all other pools l = ls[i + 1] - f tmp[f] = (Sg11[l - 1] - g11g12[l - 1] * tmp[f - 1]) / g11g11[l - 1] tmp[f + 1:f + l] = g11[1:l] * tmp[f] + g12[1:l] * tmp[f - 1] tmp -= tmp.mean() aa = tmp.dot(tmp) bb = res.dot(tmp) cc = RSS - thresh try: db = (-bb + sqrt(bb * bb - aa * cc)) / aa except: db = -bb / aa # perform shift if b_nonneg: db = max(db, -b) b += db c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) # update b and lam db = max(np.mean(y - c), 0 if b_nonneg else -np.inf) - b b += db lam -= db / f_lam # update g and b if optimize_g and (not g_converged): def getRSS(y, opt): b, ld, lr = opt if ld < lr: return 1e3 * thresh d, r = exp(ld), exp(lr) g1, g2 = d + r, -d * r tmp = b + onnls(y - b, [g1, g2], lam, mask=(s > 1e-2 * s.max()))[0] - y return tmp.dot(tmp) result = minimize(lambda x: getRSS(y, x), (b, log(d), log(r)), bounds=((0 if b_nonneg else None, None), (None, -1e-4), (None, -1e-3)), method='L-BFGS-B', options={'gtol': 1e-04, 'maxiter': 10, 'ftol': 1e-05}) if abs(result['x'][1] - log(d)) < 1e-3: g_converged = True b, ld, lr = result['x'] d, r = exp(ld), exp(lr) g = (d + r, -d * r) c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) # update b and lam db = max(np.mean(y - c), 0 if b_nonneg else -np.inf) - b b += db lam -= db if penalty == 0: # get (locally optimal) L0 solution def c4smin(y, s, s_min): ls = np.append(np.where(s > s_min)[0], T) tmp = np.zeros_like(s) l = ls[0] # first pool tmp[:l] = max(0, np.exp(log(d) * np.arange(l)).dot(y[:l]) * (1 - d * d) / (1 - d**(2 * l))) * np.exp(log(d) * np.arange(l)) for i, f in enumerate(ls[:-1]): # all other pools l = ls[i + 1] - f tmp[f] = (g11[:l].dot(y[f:f + l]) - g11g12[l - 1] * tmp[f - 1]) / g11g11[l - 1] tmp[f + 1:f + l] = g11[1:l] * tmp[f] + g12[1:l] * tmp[f - 1] return tmp spikesizes = np.sort(s[s > 1e-6]) i = len(spikesizes) // 2 l = 0 u = len(spikesizes) - 1 while u - l > 1: s_min = spikesizes[i] tmp = c4smin(y - b, s, s_min) res = y - b - tmp RSS = res.dot(res) if RSS < thresh or i == 0: l = i i = (l + u) // 2 res0 = tmp else: u = i i = (l + u) // 2 if i > 0: c = res0 s = np.append([0, 0], c[2:] - g[0] * c[1:-1] - g[1] * c[:-2]) return c, s, b, g, lam
def constrained_oasisAR2(y, g, sn, optimize_b=True, b_nonneg=True, optimize_g=0, decimate=5, shift=100, window=None, tol=1e-9, max_iter=1, penalty=1): """ Infer the most likely discretized spike train underlying an AR(2) fluorescence trace Solves the noise constrained sparse non-negative deconvolution problem min (s)_1 subject to (c-y)^2 = sn^2 T and s_t = c_t-g1 c_{t-1}-g2 c_{t-2} >= 0 Args: y : array of float One dimensional array containing the fluorescence intensities (with baseline already subtracted) with one entry per time-bin. g : (float, float) Parameters of the AR(2) process that models the fluorescence impulse response. sn : float Standard deviation of the noise distribution. optimize_b : bool, optional, default True Optimize baseline if True else it is set to 0, see y. b_nonneg: bool, optional, default True Enforce strictly non-negative baseline if True. optimize_g : int, optional, default 0 Number of large, isolated events to consider for optimizing g. No optimization if optimize_g=0. decimate : int, optional, default 5 Decimation factor for estimating hyper-parameters faster on decimated data. shift : int, optional, default 100 Number of frames by which to shift window from on run of NNLS to the next. window : int, optional, default None (200 or larger dependend on g) Window size. tol : float, optional, default 1e-9 Tolerance parameter. max_iter : int, optional, default 1 Maximal number of iterations. penalty : int, optional, default 1 Sparsity penalty. 1: min (s)_1 0: min (s)_0 Returns: c : array of float The inferred denoised fluorescence signal at each time-bin. s : array of float Discretized deconvolved neural activity (spikes). b : float Fluorescence baseline value. (g1, g2) : tuple of float Parameters of the AR(2) process that models the fluorescence impulse response. lam : float Sparsity penalty parameter lambda of dual problem. References: Friedrich J and Paninski L, NIPS 2016 Friedrich J, Zhou P, and Paninski L, arXiv 2016 """ T = len(y) d = (g[0] + sqrt(g[0] * g[0] + 4 * g[1])) / 2 r = (g[0] - sqrt(g[0] * g[0] + 4 * g[1])) / 2 if window is None: window = int(min(T, max(200, -5 / log(d)))) if not optimize_g: g11 = (np.exp(log(d) * np.arange(1, T + 1)) * np.arange(1, T + 1)) if d == r else \ (np.exp(log(d) * np.arange(1, T + 1)) - np.exp(log(r) * np.arange(1, T + 1))) / (d - r) g12 = np.append(0, g[1] * g11[:-1]) g11g11 = np.cumsum(g11 * g11) g11g12 = np.cumsum(g11 * g12) Sg11 = np.cumsum(g11) f_lam = 1 - g[0] - g[1] elif decimate == 0: # need to run AR1 anyways for estimating AR coeffs decimate = 1 thresh = sn * sn * T # get initial estimate of b and lam on downsampled data using AR1 model if decimate > 0: from caiman.source_extraction.cnmf.oasis import oasisAR1, constrained_oasisAR1 _, s, b, aa, lam = constrained_oasisAR1( y[:len(y) // decimate * decimate].reshape(-1, decimate).mean(1), d**decimate, sn / sqrt(decimate), optimize_b=optimize_b, b_nonneg=b_nonneg, optimize_g=optimize_g) if optimize_g: from scipy.optimize import minimize d = aa**(1. / decimate) if decimate > 1: s = oasisAR1(y - b, d, lam=lam * (1 - aa) / (1 - d))[1] r = estimate_time_constant(s, 1, fudge_factor=.98)[0] g[0] = d + r g[1] = -d * r g11 = (np.exp(log(d) * np.arange(1, T + 1)) - np.exp(log(r) * np.arange(1, T + 1))) / (d - r) g12 = np.append(0, g[1] * g11[:-1]) g11g11 = np.cumsum(g11 * g11) g11g12 = np.cumsum(g11 * g12) Sg11 = np.cumsum(g11) f_lam = 1 - g[0] - g[1] elif decimate > 1: s = oasisAR1(y - b, d, lam=lam * (1 - aa) / (1 - d))[1] lam *= (1 - d**decimate) / f_lam # this window size seems necessary and sufficient possible_spikes = [ x + np.arange(-2, 3) for x in np.where(s > s.max() / 10.)[0] ] ff = np.array(possible_spikes, dtype=np.int).ravel() ff = np.unique(ff[(ff >= 0) * (ff < T)]) mask = np.zeros(T, dtype=bool) mask[ff] = True else: b = np.percentile(y, 15) if optimize_b else 0 lam = 2 * sn * np.linalg.norm(g11) mask = None if b_nonneg: b = max(b, 0) # run ONNLS c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) if not optimize_b: # don't optimize b, just the dual variable lambda for _ in range(max_iter - 1): res = y - c RSS = res.dot(res) if np.abs(RSS - thresh) < 1e-4 * thresh: break # calc shift dlam, here attributed to sparsity penalty tmp = np.empty(T) ls = np.append(np.where(s > 1e-6)[0], T) l = ls[0] tmp[:l] = (1 + d) / (1 + d**l) * \ np.exp(log(d) * np.arange(l)) # first pool for i, f in enumerate(ls[:-1]): # all other pools l = ls[i + 1] - f - 1 # if and elif correct last 2 time points for |s|_1 instead |c|_1 if i == len(ls) - 2: # last pool tmp[f] = (1. / f_lam if l == 0 else (Sg11[l] + g[1] / f_lam * g11[l - 1] + (g[0] + g[1]) / f_lam * g11[l] - g11g12[l] * tmp[f - 1]) / g11g11[l]) # secondlast pool if last one has length 1 elif i == len(ls) - 3 and ls[-2] == T - 1: tmp[f] = (Sg11[l] + g[1] / f_lam * g11[l] - g11g12[l] * tmp[f - 1]) / g11g11[l] else: # all other pools tmp[f] = (Sg11[l] - g11g12[l] * tmp[f - 1]) / g11g11[l] l += 1 tmp[f + 1:f + l] = g11[1:l] * tmp[f] + g12[1:l] * tmp[f - 1] aa = tmp.dot(tmp) bb = res.dot(tmp) cc = RSS - thresh try: db = (-bb + sqrt(bb * bb - aa * cc)) / aa except: db = -bb / aa # perform shift b += db c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) db = np.mean(y - c) - b b += db lam -= db / f_lam else: # optimize b db = max(np.mean(y - c), 0 if b_nonneg else -np.inf) - b b += db lam -= db / (1 - g[0] - g[1]) g_converged = False for _ in range(max_iter - 1): res = y - c - b RSS = res.dot(res) if np.abs(RSS - thresh) < 1e-4 * thresh: break # calc shift db, here attributed to baseline tmp = np.empty(T) ls = np.append(np.where(s > 1e-6)[0], T) l = ls[0] tmp[:l] = (1 + d) / (1 + d**l) * \ np.exp(log(d) * np.arange(l)) # first pool for i, f in enumerate(ls[:-1]): # all other pools l = ls[i + 1] - f tmp[f] = (Sg11[l - 1] - g11g12[l - 1] * tmp[f - 1]) / g11g11[l - 1] tmp[f + 1:f + l] = g11[1:l] * tmp[f] + g12[1:l] * tmp[f - 1] tmp -= tmp.mean() aa = tmp.dot(tmp) bb = res.dot(tmp) cc = RSS - thresh try: db = (-bb + sqrt(bb * bb - aa * cc)) / aa except: db = -bb / aa # perform shift if b_nonneg: db = max(db, -b) b += db c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) # update b and lam db = max(np.mean(y - c), 0 if b_nonneg else -np.inf) - b b += db lam -= db / f_lam # update g and b if optimize_g and (not g_converged): def getRSS(y, opt): b, ld, lr = opt if ld < lr: return 1e3 * thresh d, r = exp(ld), exp(lr) g1, g2 = d + r, -d * r tmp = b + onnls( y - b, [g1, g2], lam, mask=(s > 1e-2 * s.max()))[0] - y return tmp.dot(tmp) result = minimize(lambda x: getRSS(y, x), (b, log(d), log(r)), bounds=((0 if b_nonneg else None, None), (None, -1e-4), (None, -1e-3)), method='L-BFGS-B', options={ 'gtol': 1e-04, 'maxiter': 10, 'ftol': 1e-05 }) if abs(result['x'][1] - log(d)) < 1e-3: g_converged = True b, ld, lr = result['x'] d, r = exp(ld), exp(lr) g = (d + r, -d * r) c, s = onnls(y - b, g, lam=lam, mask=mask, shift=shift, window=window, tol=tol) # update b and lam db = max(np.mean(y - c), 0 if b_nonneg else -np.inf) - b b += db lam -= db if penalty == 0: # get (locally optimal) L0 solution def c4smin(y, s, s_min): ls = np.append(np.where(s > s_min)[0], T) tmp = np.zeros_like(s) l = ls[0] # first pool tmp[:l] = max( 0, np.exp(log(d) * np.arange(l)).dot(y[:l]) * (1 - d * d) / (1 - d**(2 * l))) * np.exp(log(d) * np.arange(l)) for i, f in enumerate(ls[:-1]): # all other pools l = ls[i + 1] - f tmp[f] = (g11[:l].dot(y[f:f + l]) - g11g12[l - 1] * tmp[f - 1]) / g11g11[l - 1] tmp[f + 1:f + l] = g11[1:l] * tmp[f] + g12[1:l] * tmp[f - 1] return tmp spikesizes = np.sort(s[s > 1e-6]) i = len(spikesizes) // 2 l = 0 u = len(spikesizes) - 1 while u - l > 1: s_min = spikesizes[i] tmp = c4smin(y - b, s, s_min) res = y - b - tmp RSS = res.dot(res) if RSS < thresh or i == 0: l = i i = (l + u) // 2 res0 = tmp else: u = i i = (l + u) // 2 if i > 0: c = res0 s = np.append([0, 0], c[2:] - g[0] * c[1:-1] - g[1] * c[:-2]) return c, s, b, g, lam