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)
'#0072B2', '#009E73', '#D55E00', '#E69F00', '#56B4E9', '#CC79A7', '#F0E442', '#999999' ] # real data from Chen et al 2013, available at following URL # https://portal.nersc.gov/project/crcns/download/cai-1/GCaMP6s_9cells_Chen2013/processed_data.tar.gz filename = "/Users/joe/Downloads/data_20120627_cell2_002.mat" ################ #### Traces #### ################ # AR(1) g = .95 sn = .3 Y, trueC, trueSpikes = gen_data() N, T = Y.shape result_oasis = oasisAR1(Y[0], g=g, lam=2.4) result_foopsi = foopsi(Y[0], g=[g], lam=2.4) fig = plt.figure(figsize=(20, 5.5)) fig.add_axes([.038, .57, .96, .42]) plt.plot(result_oasis[0], c=col[0], label='OASIS') plt.plot(result_foopsi[0], '--', c=col[6], label='CVXPY') plt.plot(trueC[0], c=col[2], label='Truth', zorder=-5) plt.plot(Y[0], c=col[7], alpha=.7, zorder=-10, lw=1, label='Data') plt.legend(frameon=False, ncol=4, loc=(.275, .82)) plt.gca().set_xticklabels([]) simpleaxis(plt.gca()) plt.ylim(Y[0].min(), Y[0].max()) plt.yticks([0, int(Y[0].max())], [0, int(Y[0].max())])
"""Script illustrating the autoregressive calcium fluorescence model for OASIS, an active set method for sparse nonnegative deconvolution @author: Johannes Friedrich """ from matplotlib import pyplot as plt from oasis.functions import gen_data from oasis.plotting import init_fig, simpleaxis init_fig() # colors for colorblind from http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/ col = ['#0072B2', '#009E73', '#D55E00'] Y, trueC, trueSpikes = gen_data([1.58, -.6], .5, T=455, framerate=30, firerate=2., seed=0) plt.figure(figsize=(15, 2.5)) for i, t in enumerate(trueSpikes[0, 20:-1]): if t: plt.plot([i, i], [0, 1], c=col[2]) plt.plot([trueSpikes[0, -1], trueSpikes[0, -1]], [0, 1], c=col[2], label=r'$s$') plt.plot(trueC[0, 20:] / 3., c=col[0], label=r'$c$', zorder=-11) plt.scatter(range(435), Y[0, 20:] / 3., c=col[1], clip_on=False, label=r'$y$') plt.legend(loc=(.38, .75), ncol=3) plt.yticks([0, 1, 2], [0, 1, 2]) plt.xticks(*[[0, 150, 300]] * 2) plt.xlim(0, 435)
import pytest import numpy as np import pandas as pd import xarray as xr import numpy.testing as npt import xarray.testing as xrt from sklearn.base import clone try: from neuroglia import calcium from oasis.functions import gen_data # Test functions perform as expected true_b = 2 y, true_c, true_s = map(np.squeeze, gen_data(N=3, b=true_b, seed=0)) y = y.T TIME = np.arange(0, len(y)/30, 1/30.) LBL = ['a', 'b', 'c'] sin_scale = 5 # data = y DFF = pd.DataFrame(y, TIME, LBL) DFF_WITH_DRIFT = DFF.apply(lambda y: y + sin_scale*np.sin(.05*TIME),axis=0) calcium_import_failed = False except ImportError: calcium_import_failed = True @pytest.mark.skipif(
This is an example of how to infer spike events """ ####################################################### # First, we'll generate some fake data import numpy as np import pandas as pd from oasis.functions import gen_data neuron_ids = ['a', 'b', 'c'] sampling_rate = 30.0 traces, _, spikes = map(np.squeeze, gen_data(N=3, b=2, seed=0)) time = np.arange(0, traces.shape[1] / sampling_rate, 1 / sampling_rate) traces = pd.DataFrame(traces.T, index=time, columns=neuron_ids) spikes = pd.DataFrame(spikes.T, index=time, columns=neuron_ids) ######################################################## # let's plot the data import matplotlib.pyplot as plt traces.plot() plt.show() ########################################################## # Now, we'll deconvolve the data
active_set.pop(c + 1) len_active_set -= 1 # update solution back to lag tau v, w, f, l = active_set[c] solution[max(f, f + l - tau - 1):f + l] = max(v, 0) / \ w * g**np.arange(max(0, l - tau - 1), l) return solution # correlation with ground truth spike train for OASIS with L1 tauls = [0, 1, 2, 5, 10, np.inf] plt.figure(figsize=(7, 5)) for j, sn in enumerate([.1, .2, .3]): Y, trueC, trueSpikes = gen_data(sn=sn) N = len(Y) C = np.asarray([ [ deconvolveAR1( y, .95, tau=tau, lam=(.2 + .25 * np.exp(-tau / 2.) )) # lam=(.12 / (1 - (.8 if sn == .3 else .7)**(tau + 1)) for tau in tauls ] for y in Y ]) S = np.zeros_like(C) S[:, :, 1:] = C[:, :, 1:] - .95 * C[:, :, :-1] S[S < 0] = 0 corr = np.array([[np.corrcoef(ss, s[-1])[0, 1] for ss in s] for s in S])