Summary

This project aims to automatically detect abrupt changes in the spiking response of high dimesnional neural circuits. Whereas previous efforts have focused on finding 'change points' in the spiking response of single neurons, the rapid growth in the ability to simultaneously record from hundreds of neurons requires the development of new methods.


In [44]:
%load_ext autoreload
%autoreload 2
%matplotlib inline


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

In [3]:
import numpy as np
import scipy.stats as stats
import scipy.signal, scipy.io, numpy.matlib
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm 
from matplotlib import colors as mcolors

In [5]:
import re, os, sys
sys.path.append('../')

In [7]:
from src.models import train_model
from src.features import build_features
from src.visualization import visualize

Loading retinal data


In [41]:
dir=r'../Data/external/'
data_files=os.listdir(path=dir)
data_files=[ x for x in data_files if x.find('.mat') !=-1]

Loading up change detection modules


In [6]:
from changepy import pelt
from changepy.costs import normal_mean, poisson, exponential

In [55]:
# setting up parameters
gauss_options=[2]
block_options=[60]

for file in data_files[4:5]:
    for gauss in gauss_options:
        for block in block_options:
            params={'res':2, 'block_width': block }
            params['gauss_width']=gauss * params['res']
            params['methods']=[ 'diff_frobenius']#variation','frobenius', 
            res=params['res']
            block_width=params['block_width']
            gauss_width=params['gauss_width']

            data_retina=scipy.io.loadmat(dir+file)
            data_retina['file']=file
            sum_diff_corr, stim=train_model.detect_changes(data_retina, params)
            plt.title('Gauss: ' + str(gauss_width) + ' Block: ' + str(block_width))

            summary_stat=sum_diff_corr['diff_frobenius']
            cp_cost=poisson # cost metric
            model=pelt# pelt as change point algorithm
            time_pt=train_model.change_1d_series(summary_stat, model, cp_cost, params)
            visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            plt.plot(time_pt)


(22993, 44, 44)
WARNING    /Users/abhinav/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
 [py.warnings]

In [54]:
np.array([5.2, 3.1,4]).astype(int)


Out[54]:
array([5, 3, 4])

In [38]:
# setting up parameters
gauss_options=[2]
block_options=[60]

for file in data_files[2:9]:
    for gauss in gauss_options:
        for block in block_options:
            params={'res':2, 'block_width': block }
            params['gauss_width']=gauss * params['res']
            params['methods']=[ 'diff_frobenius']#'pop_sum', 'mean', 'diff_base', 'variation','frobenius', 
            res=params['res']
            block_width=params['block_width']
            gauss_width=params['gauss_width']

            #file='data_03_13_12_t6.mat'
            #file='data_03_13_12_t2'
            data_retina=scipy.io.loadmat(dir+file)
            data_retina['file']=file
            sum_diff_corr, stim=train_model.detect_changes(data_retina, params)
            #visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            plt.title('Gauss: ' + str(gauss_width) + ' Block: ' + str(block_width))

            summary_stat=sum_diff_corr['diff_frobenius']
            cp_model=poisson
            cp_pts=pelt(cp_model(summary_stat), len(summary_stat))
            time_pt=np.zeros(summary_stat.size * res * block_width)
            change_points=np.array(cp_pts) * res * block_width
            visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            time_pt[change_points]=1
            plt.plot(time_pt)


WARNING    ../src/features/build_features.py:28: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  discrete_spikes = np.zeros(n_bins)
 [py.warnings]
WARNING    ../src/features/build_features.py:30: VisibleDeprecationWarning: non integer (and non boolean) array-likes will not be accepted as indices in the future
  discrete_spikes[spike_times] = 1
 [py.warnings]
(73, 2401098)
(40018, 73, 73)
WARNING    /Users/abhinav/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
 [py.warnings]
white15_a
spattempexp15_a
white30_a
spattempexp30_a
(21, 1222081)
(20368, 21, 21)
fullfield_a
white64_a
fullfield_b
multiscale64_g
white64_b
multiscale64_h
(44, 1379585)
(22993, 44, 44)
fullfield_a
white64_a
multiscale64_g
fullfield_b
white64_b
multiscale64_h
(8, 1213699)
(20228, 8, 8)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
(11, 1230586)
(20509, 11, 11)
white64_a
multiscale64_g
fullfield_a
white64_b
multiscale64_h
fullfield_b
(9, 1213943)
(20232, 9, 9)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
(31, 1214595)
(20243, 31, 31)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b

In [10]:
# setting up parameters
gauss_options=[2]
block_options=[60]

for file in data_files[9:15]:
    for gauss in gauss_options:
        for block in block_options:
            params={'res':2, 'block_width': block }
            params['gauss_width']=gauss * params['res']
            params['methods']=[ 'diff_frobenius']#'pop_sum', 'mean', 'diff_base', 'variation','frobenius', 
            res=params['res']
            block_width=params['block_width']
            gauss_width=params['gauss_width']

            #file='data_03_13_12_t6.mat'
            #file='data_03_13_12_t2'
            data_retina=scipy.io.loadmat(dir+file)
            data_retina['file']=file
            sum_diff_corr, stim=train_model.detect_changes(data_retina, params)
            #visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            plt.title('Gauss: ' + str(gauss_width) + ' Block: ' + str(block_width))

            summary_stat=sum_diff_corr['diff_frobenius']
            cp_exponential=pelt(exponential(summary_stat), len(summary_stat))
            time_pt=np.zeros(summary_stat.size * res * block_width)
            change_points=np.array(cp_exponential) * res * block_width
            visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            time_pt[change_points]=1
            plt.plot(time_pt)


../src/features/build_features.py:28: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  discrete_spikes = np.zeros(n_bins)
../src/features/build_features.py:30: VisibleDeprecationWarning: non integer (and non boolean) array-likes will not be accepted as indices in the future
  discrete_spikes[spike_times] = 1
(67, 1213097)
(20218, 67, 67)
/Users/abhinav/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(33, 1235092)
(20584, 33, 33)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(58, 1235092)
(20584, 58, 58)
fullfield_a
multiscale64_g
white64_a
fullfield_b
multiscale64_h
white64_b
spatexp64_a
spatexp64_b
(26, 1219093)
(20318, 26, 26)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(20, 1213092)
(20218, 20, 20)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(21, 1224594)
(20410, 21, 21)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b

In [37]:
# setting up parameters
gauss_options=[2]
block_options=[60]

for file in data_files[9:10]:
    for gauss in gauss_options:
        for block in block_options:
            params={'res':2, 'block_width': block }
            params['gauss_width']=gauss * params['res']
            params['methods']=[ 'diff_frobenius']#'pop_sum', 'mean', 'diff_base', 'variation','frobenius', 
            res=params['res']
            block_width=params['block_width']
            gauss_width=params['gauss_width']

            #file='data_03_13_12_t6.mat'
            #file='data_03_13_12_t2'
            data_retina=scipy.io.loadmat(dir+file)
            data_retina['file']=file
            sum_diff_corr, stim=train_model.detect_changes(data_retina, params)
            #visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            plt.title('Gauss: ' + str(gauss_width) + ' Block: ' + str(block_width))

            summary_stat=sum_diff_corr['diff_frobenius']
            cp_model=poisson
            cp_pts=pelt(cp_model(summary_stat), len(summary_stat))
            
            time_pt=np.zeros(summary_stat.size * res * block_width)
            change_points=np.array(cp_pts) * res * block_width
            time_pt[change_points]=1
            visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            
            plt.plot(time_pt)


WARNING    ../src/features/build_features.py:28: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  discrete_spikes = np.zeros(n_bins)
 [py.warnings]
WARNING    ../src/features/build_features.py:30: VisibleDeprecationWarning: non integer (and non boolean) array-likes will not be accepted as indices in the future
  discrete_spikes[spike_times] = 1
 [py.warnings]
(67, 1213097)
(20218, 67, 67)
WARNING    /Users/abhinav/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
 [py.warnings]
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b

In [39]:
# setting up parameters
gauss_options=[2]
block_options=[60]

for file in data_files[10:15]:
    for gauss in gauss_options:
        for block in block_options:
            params={'res':2, 'block_width': block }
            params['gauss_width']=gauss * params['res']
            params['methods']=[ 'diff_frobenius']#'pop_sum', 'mean', 'diff_base', 'variation','frobenius', 
            res=params['res']
            block_width=params['block_width']
            gauss_width=params['gauss_width']

            #file='data_03_13_12_t6.mat'
            #file='data_03_13_12_t2'
            data_retina=scipy.io.loadmat(dir+file)
            data_retina['file']=file
            sum_diff_corr, stim=train_model.detect_changes(data_retina, params)
            #visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            plt.title('Gauss: ' + str(gauss_width) + ' Block: ' + str(block_width))

            summary_stat=sum_diff_corr['diff_frobenius']
            cp_model=poisson
            cp_pts=pelt(cp_model(summary_stat), len(summary_stat))
            
            time_pt=np.zeros(summary_stat.size * res * block_width)
            change_points=np.array(cp_pts) * res * block_width
            time_pt[change_points]=1
            visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
            
            plt.plot(time_pt)


WARNING    ../src/features/build_features.py:28: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  discrete_spikes = np.zeros(n_bins)
 [py.warnings]
WARNING    ../src/features/build_features.py:30: VisibleDeprecationWarning: non integer (and non boolean) array-likes will not be accepted as indices in the future
  discrete_spikes[spike_times] = 1
 [py.warnings]
(33, 1235092)
(20584, 33, 33)
WARNING    /Users/abhinav/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
 [py.warnings]
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(58, 1235092)
(20584, 58, 58)
fullfield_a
multiscale64_g
white64_a
fullfield_b
multiscale64_h
white64_b
spatexp64_a
spatexp64_b
(26, 1219093)
(20318, 26, 26)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(20, 1213092)
(20218, 20, 20)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
(21, 1224594)
(20410, 21, 21)
white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b

In [11]:
import pandas as pd

In [14]:
discrete_sim_spikes=build_features.sim_brian_spikes(duration_epochs=5400)


INFO       No numerical integration method specified for group 'neurongroup', using method 'euler' (took 0.06s, trying other methods took 0.00s). [brian2.stateupdaters.base.method_choice]
INFO       No numerical integration method specified for group 'neurongroup_1', using method 'euler' (took 0.02s, trying other methods took 0.00s). [brian2.stateupdaters.base.method_choice]
INFO       No numerical integration method specified for group 'neurongroup_2', using method 'euler' (took 0.02s, trying other methods took 0.00s). [brian2.stateupdaters.base.method_choice]
INFO       No numerical integration method specified for group 'neurongroup_3', using method 'euler' (took 0.03s, trying other methods took 0.00s). [brian2.stateupdaters.base.method_choice]
INFO       No numerical integration method specified for group 'neurongroup_4', using method 'euler' (took 0.02s, trying other methods took 0.00s). [brian2.stateupdaters.base.method_choice]
INFO       No numerical integration method specified for group 'neurongroup_5', using method 'euler' (took 0.02s, trying other methods took 0.00s). [brian2.stateupdaters.base.method_choice]
WARNING    ../src/features/build_features.py:110: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  array_bins = np.zeros([n_neurons, n_bins], dtype=np.int8)
 [py.warnings]
WARNING    ../src/features/build_features.py:113: VisibleDeprecationWarning: non integer (and non boolean) array-likes will not be accepted as indices in the future
  array_bins[neurons_spiketimes, bins_with_spikes] = 1
 [py.warnings]

In [ ]:
gauss=10
params={'res':2, 'block_width': 37 }

params['gauss_width']=gauss * params['res']
params['methods']=['frobenius' ,'diff_frobenius']
sum_diff_corr=train_model.detect_brian_spikes_changes(discrete_sim_spikes, params)

In [35]:
summary_stat=sum_diff_corr['diff_frobenius'][1:]

In [36]:
cp_exponential=pelt(poisson(summary_stat), len(summary_stat))
time_pt=np.zeros(summary_stat.size * params['res'] * params['block_width'])
change_points=np.array(cp_exponential) * params['res'] * params['block_width']
#visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
time_pt[change_points]=1
plt.plot(time_pt)


Out[36]:
[<matplotlib.lines.Line2D at 0x114c1f5f8>]

In [25]:
plt.plot(sum_diff_corr['frobenius'])
plt.plot(sum_diff_corr['diff_frobenius'])


Out[25]:
[<matplotlib.lines.Line2D at 0x113fdb2e8>]

In [27]:
pelt?

In [47]:
sum_diff_corr


Out[47]:
{'diff_frobenius': array([ 0.98305242,  0.97628269,  1.30227766, ...,  1.60960938,
         1.83114332,  1.42218314])}

In [49]:
cp_exponential=pelt(exponential(summary_stat), len(summary_stat))

In [50]:
time_pt=np.zeros(summary_stat.size * res * block_width)

In [40]:
summary_stat.size


Out[40]:
22871

In [51]:
change_points=np.array(cp_exponential) * res * block_width

In [52]:
time_pt[change_points]=1

In [53]:
plt.plot(time_pt)


Out[53]:
[<matplotlib.lines.Line2D at 0x115b1eeb8>]

In [54]:
visualize.plot_cp_results(sum_diff_corr,stim, data_retina, params)
plt.plot(time_pt)


white64_a
fullfield_a
multiscale64_g
white64_b
fullfield_b
multiscale64_h
spatexp64_a
spatexp64_b
/Users/abhinav/anaconda/envs/py35/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The set_color_cycle attribute was deprecated in version 1.5. Use set_prop_cycle instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
Out[54]:
[<matplotlib.lines.Line2D at 0x11f53a908>]