def AR1(constrained=False): g = .95 sn = .3 y, c, s = [a[0] for a in gen_data([g], sn, N=1)] result = constrained_oasisAR1(y, g, sn) if constrained else oasisAR1(y, g, lam=2.4) result_foopsi = constrained_foopsi(y, [g], sn) if constrained else foopsi(y, [g], lam=2.4) npt.assert_allclose(np.corrcoef(result[0], result_foopsi[0])[0, 1], 1) npt.assert_allclose(np.corrcoef(result[1], result_foopsi[1])[0, 1], 1) npt.assert_allclose(np.corrcoef(result[0], c)[0, 1], 1, .03) npt.assert_allclose(np.corrcoef(result[1], s)[0, 1], 1, .2)
def AR2(constrained=False): g = [1.7, -.712] sn = .3 y, c, s = [a[0] for a in gen_data(g, sn, N=1, seed=3)] result = constrained_onnlsAR2(y, g, sn) if constrained else onnls(y, g, lam=25) result_foopsi = constrained_foopsi(y, g, sn) if constrained else foopsi(y, g, lam=25) npt.assert_allclose(np.corrcoef(result[0], result_foopsi[0])[0, 1], 1, 1e-3) npt.assert_allclose(np.corrcoef(result[1], result_foopsi[1])[0, 1], 1, 1e-2) npt.assert_allclose(np.corrcoef(result[0], c)[0, 1], 1, .03) npt.assert_allclose(np.corrcoef(result[1], s)[0, 1], 1, .2) result2 = constrained_oasisAR2(y, g[0], g[1], sn) if constrained \ else oasisAR2(y, g[0], g[1], lam=25) npt.assert_allclose(np.corrcoef(result2[0], c)[0, 1], 1, .03) npt.assert_allclose(np.corrcoef(result2[1], s)[0, 1], 1, .2)
print("The solver " + solver + " is actually not installed, hence skipping it.") break constrained_ts = {} for solver in solvers[:-1]: # GUROBI failed constrained_ts[solver] = np.nan * np.zeros(N) print( 'running %7s with p=1 and optimizing lambda such that noise constraint is tight' % solver) for i, y in enumerate(Y): if solver == 'OASIS': constrained_ts[solver][i] = Timer(lambda: constrained_oasisAR1( y, g=g, sn=sn)).timeit(number=runs) / runs else: try: constrained_ts[solver][i] = Timer(lambda: constrained_foopsi( y, g=[g], sn=sn, solver=solver)).timeit(number=runs) / runs except SolverError: print("The solver " + solver + " is actually not installed, hence skipping it.") break constrained_ts['GUROBI'] = np.zeros(N) * np.nan # GUROBI failed # plot fig = plt.figure(figsize=(7, 5)) fig.add_axes([.14, .17, .79, .82]) plt.errorbar(range(len(solvers)), [np.mean(ts[s]) for s in solvers], [np.std(ts[s]) / np.sqrt(N) for s in solvers], ls='', marker='o', ms=10, c=col[0],
plt.ylim(-.2, 1.1) plt.xlim(0, 452) plt.ylabel(r'$s_{\min}$') plt.xlabel('Time [s]', labelpad=-10) plt.show() # AR(1) g = .95 sn = .3 Y, trueC, trueSpikes = gen_data() y = Y[0] N, T = Y.shape c, s = constrained_foopsi(y, [g], sn)[:2] c_t, s_t = oasisAR1(y, g, s_min=.55) res = [np.where(oasisAR1(y, g, s_min=s0)[1] > 1e-2)[0] for s0 in np.arange(0, 1.1, .1)] plotTrace(True) # AR(2) g = [1.7, -.712] sn = 1. Y, trueC, trueSpikes = gen_data(g, sn, seed=3) rng = slice(150, 600) trueC = trueC[:, rng] trueSpikes = trueSpikes[:, rng] y = Y[0, rng]