Пример #1
0
    N = 40  # estimated number of neurons
    TargetArea = N * prod(2 * array(sig)) / prod(data[0, :, :].shape)
    TargetRange = [TargetArea * 0.8, TargetArea * 1.2]
    NonNegative = True
    lam = 1
elif data_source == 3:   # Use experimental 3D data
    mat = loadmat('Datasets/data_exp3D')
    data = transpose(mat['data'], [3, 0, 1, 2])
    sig = (2, 2, 2)  # neurons size
    TargetRange = [0.005, 0.015]
    NonNegative = True
    lam = 0.001


# Run source detection algorithms
x = gaussian_group_lasso(data, sig, lam,
                         NonNegative=NonNegative, TargetAreaRatio=TargetRange, verbose=True, adaptBias=True)
# x = gaussian_group_lasso(data[:len(data) / 5 * 5].reshape((-1, 5) + data.shape[1:]).max(1), sig, lam/5.,
#                          NonNegative=NonNegative, TargetAreaRatio=TargetRange, verbose=True, adaptBias=True)
pic_x = percentile(x, 95, axis=0)
pic_data = percentile(data, 95, axis=0)
# centers extracted from fista output using RegionalMax
cent = GetCenters(pic_x)

MSE_array, shapes, activity, boxes = LocalNMF(
    data, (array(cent)[:-1]).T, sig,
    NonNegative=NonNegative, verbose=True, adaptBias=True)

L = len(cent[0])  # number of detected neurons
denoised_data = activity[:L].T.dot(shapes[:L].reshape(L, -1)).reshape(data.shape)
pic_denoised = percentile(denoised_data, 95, axis=0)
residual = data - activity.T.dot(shapes.reshape(len(shapes), -1)).reshape(data.shape)
def GetCentersData(data,NumCent,data_name=[],rep=0): 
    """
    Get intialization centers using group lasso
    
    Input
    ----------
    data : array, shape (T, X,Y,(,Z))
        data
    data_name: string
        dataset name so we can save load previous center data
    NumCent: integer
        number centers to extract
    rep: integer
        repetition number
        
    Output
    ----------
    activity: array, shape (L,T)
        extracted temporal components
    
    """
    from numpy import  array,percentile    
    from BlockGroupLasso import gaussian_group_lasso, GetCenters
    from pylab import load
    import os
    import pickle
        
    
    DataFolder=GetDataFolder()    

    center_file_name=DataFolder + '/centers_'+ str(data_name) + '_rep_' + str(rep)
    if NumCent>0:
        if data_name==[] or os.path.isfile(center_file_name)==False:
            if data.ndim==3:
                sig0=(2,2)
            else:
                sig0=(2,2,2)
                
            TargetRange = [0.1, 0.2]    
            lam = 500
            ds= 50 #downscale time for group lasso        
            NonNegative=True
    
            downscaled_data=data[:int(old_div(len(data), ds)) * ds].reshape((-1, ds) + data.shape[1:]).max(1) #for speed ups
            x = gaussian_group_lasso(downscaled_data, sig0, lam,NonNegative=NonNegative, TargetAreaRatio=TargetRange, verbose=True, adaptBias=False)
            pic_x = percentile(x, 95, axis=0)

                ######
            # centers extracted from fista output using RegionalMax
            cent = GetCenters(pic_x)
            print(np.shape(cent)[0])
                
            # Plot Results
#            import matplotlib.pyplot as plt
#            pic_data = np.percentile(data, 95, axis=0)
#            plt.figure(figsize=(12, 4. * data.shape[1] / data.shape[2]))
#            ax = plt.subplot(131)
#            ax.scatter(cent[1], cent[0],  marker='o', c='white')
#            plt.hold(True)
#            ax.set_title('Data + centers')
#            ax.imshow(pic_data.max(2))
#            ax2 = plt.subplot(132)
#            ax2.scatter(cent[1], cent[0], marker='o', c='white')
#            ax2.imshow(pic_x.max(2))
#            ax2.set_title('Inferred x')
#            ax3 = plt.subplot(133)
#            ax3.scatter(cent[1], cent[0],   marker='o', c='white')
#            ax3.imshow(pic_x.max(2))
#            ax3.set_title('Denoised data')
#            plt.show()
#        
            # save results
            if data_name!=[]:
                from io import open
                f = open(center_file_name, 'wb')
            
                pickle.dump(cent, f, protocol=pickle.HIGHEST_PROTOCOL)
                f.close()
        else:
            if data_name!=[]:
                cent=load(center_file_name)
                
        new_cent=(array(cent)[:-1]).T                
        new_cent=new_cent[:NumCent] #just give strongest centers
    else:
        new_cent=np.reshape([],(0,data.ndim-1))
        
    return new_cent
def GetCentersData(data, data_name, NumCent):

    from numpy import array, percentile
    from BlockGroupLasso import gaussian_group_lasso, GetCenters
    from pylab import load
    import matplotlib.pyplot as plt
    import os
    import cPickle

    DataFolder = GetDataFolder()

    center_file_name = DataFolder + '/centers_' + data_name
    if NumCent > 0:
        if os.path.isfile(center_file_name) == False:
            if data.ndim == 3:
                sig0 = (2, 2)
            else:
                sig0 = (2, 2, 2)

            TargetRange = [0.1, 0.2]
            lam = 500
            ds = 50  #downscale time for group lasso
            NonNegative = True

            downscaled_data = data[:int(len(data) / ds) *
                                   ds].reshape((-1, ds) +
                                               data.shape[1:]).max(1)
            x = gaussian_group_lasso(downscaled_data,
                                     sig0,
                                     lam,
                                     NonNegative=NonNegative,
                                     TargetAreaRatio=TargetRange,
                                     verbose=True,
                                     adaptBias=False)
            pic_x = percentile(x, 95, axis=0)

            ######
            # centers extracted from fista output using RegionalMax
            cent = GetCenters(pic_x)
            print np.shape(cent)[0]

            # Plot Results
            #            pic_data = np.percentile(data, 95, axis=0)
            #            plt.figure(figsize=(12, 4. * data.shape[1] / data.shape[2]))
            #            ax = plt.subplot(131)
            #            ax.scatter(cent[1], cent[0],  marker='o', c='white')
            #            plt.hold(True)
            #            ax.set_title('Data + centers')
            #            ax.imshow(pic_data.max(2))
            #            ax2 = plt.subplot(132)
            #            ax2.scatter(cent[1], cent[0], marker='o', c='white')
            #            ax2.imshow(pic_x.max(2))
            #            ax2.set_title('Inferred x')
            #            ax3 = plt.subplot(133)
            #            ax3.scatter(cent[1], cent[0],   marker='o', c='white')
            #            ax3.imshow(pic_x.max(2))
            #            ax3.set_title('Denoised data')
            #            plt.show()
            #
            f = file(center_file_name, 'wb')

            cPickle.dump(cent, f, protocol=cPickle.HIGHEST_PROTOCOL)
            f.close()
        else:
            cent = load(center_file_name)

        new_cent = (array(cent)[:-1]).T
        new_cent = new_cent[:NumCent]  #just give strongest centers
    else:
        new_cent = np.reshape([], (0, data.ndim - 1))

    return new_cent
Пример #4
0
    N = 40  # estimated number of neurons
    TargetArea = N * prod(2 * array(sig)) / prod(data[0, :, :].shape)
    TargetRange = [TargetArea * 0.8, TargetArea * 1.2]
    NonNegative = True
    lam = 1
elif data_source == 3:   # Use experimental 3D data
    mat = loadmat('Datasets/data_exp3D')
    data = transpose(mat['data'], [3, 0, 1, 2])
    sig = (2, 2, 2)  # neurons size
    TargetRange = [0.005, 0.015]
    NonNegative = True
    lam = 0.001


# Run source detection algorithms
x = gaussian_group_lasso(data, sig, lam, NonNegative=NonNegative,
                         TargetAreaRatio=TargetRange, verbose=True, adaptBias=True)
pic_x = percentile(x, 95, axis=0)
pic_data = percentile(data, 95, axis=0)
# centers extracted from fista output using RegionalMax
cent = GetCenters(pic_x)
# ROI around each center, using watersheding on non-zero regions
ROI = GetROI(pic_x,  (array(cent)[:-1]).T)
# temporal traces of activity for each neuron, averaged over each ROI
activity = GetActivity(x, ROI)

MSE_array, shapes, activity, boxes, background = LocalNMF(
    data, (array(cent)[:-1]).T, activity, sig,
    NonNegative=NonNegative, verbose=True, adaptBias=True)

L = len(shapes)  # number of detected neurons
denoised_data = 0 * data
def GetCentersData(data,data_name,NumCent):
    
    from numpy import  array,percentile    
    from BlockGroupLasso import gaussian_group_lasso, GetCenters
    from pylab import load
    import matplotlib.pyplot as plt
    import os
    import cPickle
        
    DataFolder=GetDataFolder()
    
    center_file_name=DataFolder + '/centers_'+data_name 
    if NumCent>0:
        if os.path.isfile(center_file_name)==False:
            if data.ndim==3:
                sig0=(2,2)
            else:
                sig0=(2,2,2)
                
            TargetRange = [0.1, 0.2]    
            lam = 500
            ds= 50 #downscale time for group lasso        
            NonNegative=True
    
            downscaled_data=data[:int(len(data) / ds) * ds].reshape((-1, ds) + data.shape[1:]).max(1)
            x = gaussian_group_lasso(downscaled_data, sig0, lam,NonNegative=NonNegative, TargetAreaRatio=TargetRange, verbose=True, adaptBias=False)
            pic_x = percentile(x, 95, axis=0)

                ######
            # centers extracted from fista output using RegionalMax
            cent = GetCenters(pic_x)
            print np.shape(cent)[0]
                
            # Plot Results
#            pic_data = np.percentile(data, 95, axis=0)
#            plt.figure(figsize=(12, 4. * data.shape[1] / data.shape[2]))
#            ax = plt.subplot(131)
#            ax.scatter(cent[1], cent[0],  marker='o', c='white')
#            plt.hold(True)
#            ax.set_title('Data + centers')
#            ax.imshow(pic_data.max(2))
#            ax2 = plt.subplot(132)
#            ax2.scatter(cent[1], cent[0], marker='o', c='white')
#            ax2.imshow(pic_x.max(2))
#            ax2.set_title('Inferred x')
#            ax3 = plt.subplot(133)
#            ax3.scatter(cent[1], cent[0],   marker='o', c='white')
#            ax3.imshow(pic_x.max(2))
#            ax3.set_title('Denoised data')
#            plt.show()
#        
            f = file(center_file_name, 'wb')
        
            cPickle.dump(cent, f, protocol=cPickle.HIGHEST_PROTOCOL)
            f.close()
        else:
            cent=load(center_file_name)
                
        new_cent=(array(cent)[:-1]).T                
        new_cent=new_cent[:NumCent] #just give strongest centers
    else:
        new_cent=np.reshape([],(0,data.ndim-1))
        
    return new_cent