コード例 #1
0
def get_deriv(blk,blk_smooth,varlist,smoothing=range(10)):
    """

    :param blk:
    :param blk_smooth:
    :param varlist:
    :param smoothing: A list of indices of which smoothing parameter to use. Default is all 10
    :return: Xdot, X
    """
    use_flags = neoUtils.concatenate_epochs(blk)
    Cbool = neoUtils.get_Cbool(blk)
    X =[]
    for varname in varlist:
        var = neoUtils.get_var(blk_smooth, varname+'_smoothed', keep_neo=False)[0]

        if varname in ['M', 'F']:
            var[np.invert(Cbool), :, :] = 0
        if varname in ['TH', 'PHIE']:
            for ii in smoothing:
                var[:, :, ii] = neoUtils.center_var(var[:,:,ii], use_flags)
            var[np.invert(Cbool), :, :] = 0
        var = var[:, :, smoothing]
        # var = neoUtils.replace_NaNs(var, 'pchip')
        # var = neoUtils.replace_NaNs(var, 'interp')

        X.append(var)
    X = np.concatenate(X, axis=1)
    zero_pad = np.zeros([1,X.shape[1],X.shape[2]])
    Xdot = np.diff(np.concatenate([zero_pad,X],axis=0),axis=0)
    Xdot = np.reshape(Xdot,[Xdot.shape[0],Xdot.shape[1]*Xdot.shape[2]])
    X = np.reshape(X,[X.shape[0],X.shape[1]*X.shape[2]])
    return(Xdot,X)
コード例 #2
0
ファイル: input_pc_and_cov.py プロジェクト: nbush257/VG3D
def manifold_fit(blk,sub_samp=4,n_components=2,method='ltsa'):
    '''
    Fit the input data to a LLE manifold
    :param blk:
    :return:
    '''
    X = get_X(blk)
    cbool=neoUtils.get_Cbool(blk)
    X[np.invert(cbool),:]=np.nan
    idx = np.all(np.isfinite(X),axis=1)
    scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
    X[idx,:] = scaler.fit_transform(X[idx,:])

    LLE = sklearn.manifold.LocallyLinearEmbedding(n_neighbors=10,method=method,n_jobs=-1,n_components=n_components,eigen_solver='dense')
    X_sub = X[idx,:]
    # samp = np.random.choice(X_sub.shape[0],n_pts,replace=False)
    samp = np.arange(0,X_sub.shape[0],sub_samp)
    X_sub = X_sub[samp,:]

    LLE.fit(X_sub)
    Y = np.empty([X.shape[0],n_components],dtype='f8')
    Y[:] = np.nan
    Y[idx,:] = LLE.transform(X[idx,:])

    return(LLE,Y)
コード例 #3
0
def get_Xc_yc(fname,p_smooth,unit_num,binsize):
    varlist = ['M', 'F', 'TH', 'PHIE']
    blk = neoUtils.get_blk(fname)
    blk_smooth = GLM.get_blk_smooth(fname,p_smooth)

    cbool = neoUtils.get_Cbool(blk)
    X = GLM.create_design_matrix(blk,varlist)
    Xdot = GLM.get_deriv(blk,blk_smooth,varlist,[0,5,9]) #maybe only want one derivative?

    X = np.concatenate([X,Xdot],axis=1)
    X = neoUtils.replace_NaNs(X,'pchip')
    X = neoUtils.replace_NaNs(X,'interp')

    Xbin = GLM.bin_design_matrix(X,binsize=binsize)
    scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
    Xbin = scaler.fit_transform(Xbin)
    cbool_bin= GLM.bin_design_matrix(cbool[:,np.newaxis],binsize=binsize).ravel()

    y = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
    ybin = elephant.conversion.BinnedSpikeTrain(y,binsize=binsize*pq.ms).to_array().T.astype('f8')
    Xbin = Xbin[:ybin.shape[0],:]
    cbool_bin = cbool_bin[:ybin.shape[0]]
    yhat = np.zeros(ybin.shape[0])

    Xc = Xbin[cbool_bin,:]
    yc = ybin[cbool_bin,:]
    return(Xc,yc,cbool_bin,yhat)
コード例 #4
0
def create_design_matrix(blk,varlist,window=1,binsize=1,deriv_tgl=False,bases=None):
    '''
    Takes a list of variables and turns it into a matrix.
    Sets the non-contact mechanics to zero, but keeps all the kinematics as NaN
    You can append the derivative or apply the pillow bases, or both.
    Scales, but does not center the output
    '''
    X = []
    if type(window)==pq.quantity.Quantity:
        window = int(window)

    if type(binsize)==pq.quantity.Quantity:
        binsize = int(binsize)
    Cbool = neoUtils.get_Cbool(blk,-1)
    use_flags = neoUtils.concatenate_epochs(blk)

    # ================================ #
    # GET THE CONCATENATED DESIGN MATRIX OF REQUESTED VARS
    # ================================ #

    for varname in varlist:
        if varname in ['MB','FB']:
            var = neoUtils.get_var(blk,varname[0],keep_neo=False)[0]
            var = neoUtils.get_MB_MD(var)[0]
            var[np.invert(Cbool)]=0
        elif varname in ['MD','FD']:
            var = neoUtils.get_var(blk,varname[0],keep_neo=False)[0]
            var = neoUtils.get_MB_MD(var)[1]
            var[np.invert(Cbool)]=0
        elif varname in ['ROT','ROTD']:
            TH = neoUtils.get_var(blk,'TH',keep_neo=False)[0]
            PH = neoUtils.get_var(blk,'PHIE',keep_neo=False)[0]
            TH = neoUtils.center_var(TH,use_flags=use_flags)
            PH = neoUtils.center_var(PH,use_flags=use_flags)
            TH[np.invert(Cbool)] = 0
            PH[np.invert(Cbool)] = 0
            if varname=='ROT':
                var = np.sqrt(TH**2+PH**2)
            else:
                var = np.arctan2(PH,TH)
        else:
            var = neoUtils.get_var(blk,varname, keep_neo=False)[0]

        if varname in ['M','F']:
            var[np.invert(Cbool),:]=0
        if varname in ['TH','PHIE']:
            var = neoUtils.center_var(var,use_flags)
            var[np.invert(Cbool),:]=0

        var = neoUtils.replace_NaNs(var,'pchip')
        var = neoUtils.replace_NaNs(var,'interp')

        X.append(var)
    X = np.concatenate(X, axis=1)

    return X
コード例 #5
0
ファイル: input_pc_and_cov.py プロジェクト: nbush257/VG3D
def get_pc(blk):
    '''
    apply PCA to the input data
    :param blk: 
    :return: principal components structure
    '''    
    cbool = neoUtils.get_Cbool(blk)
    X = get_X(blk)
    pc = neoUtils.applyPCA(X, cbool)[1]
    
    return(pc)
コード例 #6
0
def get_X_y(fname, unit_num=0):
    varlist = ['M', 'FX', 'FY', 'TH']
    blk = neoUtils.get_blk(fname)
    cbool = neoUtils.get_Cbool(blk)
    X = GLM.create_design_matrix(blk, varlist)
    Xdot, Xsmooth = GLM.get_deriv(blk, blk, varlist, [0, 5, 9])

    X = np.concatenate([X, Xdot], axis=1)
    X = neoUtils.replace_NaNs(X, 'pchip')
    X = neoUtils.replace_NaNs(X, 'interp')
    scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
    X = scaler.fit_transform(X)
    y = neoUtils.get_rate_b(blk, unit_num)[1][:, np.newaxis]
    yhat = np.zeros_like(y)
    return (X, y, cbool)
コード例 #7
0
ファイル: input_pc_and_cov.py プロジェクト: nbush257/VG3D
def get_X(blk):
    use_flags = neoUtils.concatenate_epochs(blk)
    cbool = neoUtils.get_Cbool(blk)
    M = neoUtils.get_var(blk, 'M').magnitude
    F = neoUtils.get_var(blk, 'F').magnitude
    TH = neoUtils.get_var(blk, 'TH').magnitude
    PH = neoUtils.get_var(blk, 'PHIE').magnitude

    # center angles
    deltaTH = neoUtils.center_var(TH, use_flags)
    deltaPH = neoUtils.center_var(PH, use_flags)
    deltaTH[np.invert(cbool)] = np.nan
    deltaPH[np.invert(cbool)] = np.nan
    X = np.concatenate([M, F, deltaTH, deltaPH], axis=1)
    return(X)
コード例 #8
0
def calc_MSE(fname, p_smooth, unit_num):
    blk = neoUtils.get_blk(fname)
    blk_smooth = GLM.get_blk_smooth(fname, p_smooth)
    varlist = ['M', 'F', 'TH', 'PHIE']
    root = neoUtils.get_root(blk, unit_num)
    print('Working on {}'.format(root))
    Xdot = GLM.get_deriv(blk, blk_smooth, varlist)[0]
    Xdot = np.reshape(Xdot, [-1, 8, 10])

    sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(0)]
    cbool = neoUtils.get_Cbool(blk)
    mse = []
    for ii in range(Xdot.shape[1]):
        var_in = Xdot[:, ii, :].copy()
        mse.append(tuning_curve_MSE(var_in, sp, cbool, bins=50))
    return (mse)
コード例 #9
0
def smoothed_best():
    df = pd.read_csv(min_entropy, index_col='id')
    smooth_vals = np.arange(5, 100, 10).tolist()
    best_smooth = df.mode(axis=1)[0]
    best_idx = [smooth_vals.index(x) for x in best_smooth]
    best_idx = pd.DataFrame({'idx': best_idx}, index=best_smooth.index)

    for f in glob.glob(os.path.join(p_load, '*NEO.h5')):
        try:
            blk = neoUtils.get_blk(f)
            blk_smooth = GLM.get_blk_smooth(f, p_smooth)
            num_units = len(blk.channel_indexes[-1].units)
            for unit_num in range(num_units):
                varlist = ['M', 'F', 'TH', 'PHIE']
                root = neoUtils.get_root(blk, unit_num)
                print('Working on {}'.format(root))
                if root not in best_idx.index:
                    print('{} not found in best smoothing derivative data'.
                          format(root))
                    continue
                outname = os.path.join(
                    p_save,
                    'best_smoothing_deriv\\{}_best_smooth_pillowX.mat'.format(
                        root))
                X = GLM.create_design_matrix(blk, varlist)
                smoothing_to_use = best_idx.loc[root][0]

                Xdot = GLM.get_deriv(blk,
                                     blk_smooth,
                                     varlist,
                                     smoothing=[smoothing_to_use])[0]
                X = np.concatenate([X, Xdot], axis=1)
                y = neoUtils.get_rate_b(blk, unit_num)[1]
                cbool = neoUtils.get_Cbool(blk)
                arclengths = get_arclength_bool(blk, unit_num)

                sio.savemat(
                    outname, {
                        'X': X,
                        'y': y,
                        'cbool': cbool,
                        'smooth': best_smooth.loc[root],
                        'arclengths': arclengths
                    })
        except Exception as ex:
            print('Problem with {}:{}'.format(os.path.basename(f), ex))
コード例 #10
0
def get_X_y(fname, p_smooth, unit_num, pca_tgl=False, n_pcs=3):
    varlist = ['M', 'F', 'TH', 'PHIE']
    blk = neoUtils.get_blk(fname)
    blk_smooth = get_blk_smooth(fname, p_smooth)

    cbool = neoUtils.get_Cbool(blk)
    X = GLM.create_design_matrix(blk, varlist)
    Xdot, Xsmooth = GLM.get_deriv(blk, blk_smooth, varlist, [0, 5, 9])
    # if using the PCA decomposition of the inputs:
    if pca_tgl:

        X = neoUtils.replace_NaNs(X, 'pchip')
        X = neoUtils.replace_NaNs(X, 'interp')

        Xsmooth = neoUtils.replace_NaNs(Xsmooth, 'pchip')
        Xsmooth = neoUtils.replace_NaNs(Xsmooth, 'interp')

        scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
        X = scaler.fit_transform(X)

        scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
        Xsmooth = scaler.fit_transform(Xsmooth)

        pca = sklearn.decomposition.PCA()
        X_pc = pca.fit_transform(X)[:, :n_pcs]
        pca = sklearn.decomposition.PCA()
        Xs_pc = pca.fit_transform(Xsmooth)[:, :n_pcs]
        zero_pad = np.zeros([1, n_pcs])
        Xd_pc = np.diff(np.concatenate([zero_pad, Xs_pc], axis=0), axis=0)
        X = np.concatenate([X_pc, Xd_pc], axis=1)

        scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
        X = scaler.fit_transform(X)
    else:
        X = np.concatenate([X, Xdot], axis=1)
        X = neoUtils.replace_NaNs(X, 'pchip')
        X = neoUtils.replace_NaNs(X, 'interp')
        scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
        X = scaler.fit_transform(X)

    y = neoUtils.get_rate_b(blk, unit_num)[1][:, np.newaxis]
    # Xc = X[cbool,:]
    # yc = y[cbool]
    yhat = np.zeros_like(y)
    return (X, y, cbool)
コード例 #11
0
ファイル: IF_modelling.py プロジェクト: nbush257/VG3D
def get_X_y(fname,p_smooth,unit_num=0):
    varlist = ['M', 'F', 'TH', 'PHIE']
    blk = neoUtils.get_blk(fname)
    blk_smooth = GLM.get_blk_smooth(fname,p_smooth)

    cbool = neoUtils.get_Cbool(blk)
    X = GLM.create_design_matrix(blk,varlist)
    Xdot,Xsmooth = GLM.get_deriv(blk,blk_smooth,varlist,[9])

    X = np.concatenate([X,Xdot],axis=1)
    X = neoUtils.replace_NaNs(X,'pchip')
    X = neoUtils.replace_NaNs(X,'interp')
    scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
    X = scaler.fit_transform(X)

    y = neoUtils.get_rate_b(blk,unit_num)[1][:,np.newaxis]
    y[np.invert(cbool)]=0
    return(X,y,cbool)
コード例 #12
0
def mymz_space(blk,unit_num,bin_stretch=False,save_tgl=False,p_save=None,im_ext='png',dpi_res=300):

    root = neoUtils.get_root(blk,unit_num)
    use_flags = neoUtils.get_Cbool(blk)
    M = neoUtils.get_var(blk).magnitude
    sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
    idx = np.all(np.isfinite(M),axis=1)
    if bin_stretch:
        MY = np.empty(M.shape[0])
        MZ = np.empty(M.shape[0])
        MY[idx], logit_y = nl(M[idx, 1],90)
        MZ[idx], logit_z = nl(M[idx, 2],90)
    else:
        MY = M[:,1]*1e-6
        MZ = M[:,2]*1e-6


    response, var1_edges,var2_edges = varTuning.joint_response_hist(MY,MZ,sp,use_flags,bins = 100,min_obs=15)
    if bin_stretch:
        var1_edges = logit_y(var1_edges)
        var2_edges = logit_z(var2_edges)
    else:
        pass
    ax = varTuning.plot_joint_response(response,var1_edges,var2_edges,contour=False)
    ax.axvline(color='k',linewidth=1)
    ax.axhline(color='k',linewidth=1)
    ax.patch.set_color([0.6,0.6,0.6])

    mask = response.mask.__invert__()
    if not mask.all():
        ax.set_ylim(var2_edges[np.where(mask)[0].min()], var2_edges[np.where(mask)[0].max()])
        ax.set_xlim(var1_edges[np.where(mask)[1].min()], var1_edges[np.where(mask)[1].max()])

    ax.set_xlabel('M$_y$ ($\mu$N-m)')
    ax.set_ylabel('M$_z$ ($\mu$N-m)')
    plt.draw()
    plt.tight_layout()
    if save_tgl:
        if p_save is None:
            raise ValueError("figure save location is required")
        else:
            plt.savefig(os.path.join(p_save,'{}_mymz.{}'.format(root,im_ext)),dpi=dpi_res)
            plt.close('all')
コード例 #13
0
def smoothed_mechanics():
    """
    use this function to grab the data from the smoothed mechanics and the
    derivative of the same
    """

    f_arclength = '/projects/p30144/_VG3D/deflections/direction_arclength_FR_group_data.csv'
    f_list = glob.glob(os.path.join(p_load, '*NEO.h5'))
    f_list.sort()

    for f in f_list:
        try:
            blk = neoUtils.get_blk(f)
            blk_smooth = GLM.get_blk_smooth(f, p_smooth)
            num_units = len(blk.channel_indexes[-1].units)
            for unit_num in range(num_units):
                varlist = ['M', 'F', 'TH', 'PHIE']
                root = neoUtils.get_root(blk, unit_num)
                print('Working on {}'.format(root))
                outname = os.path.join(p_save,
                                       '{}_smooth_mechanicsX.mat'.format(root))

                Xdot, X = GLM.get_deriv(blk,
                                        blk_smooth,
                                        varlist,
                                        smoothing=[5])
                X = np.concatenate([X, Xdot], axis=1)
                y = neoUtils.get_rate_b(blk, unit_num)[1]
                cbool = neoUtils.get_Cbool(blk)
                arclengths = get_arclength_bool(blk,
                                                unit_num,
                                                fname=f_arclength)

                sio.savemat(
                    outname, {
                        'X': X,
                        'y': y,
                        'cbool': cbool,
                        'smooth': 55,
                        'arclengths': arclengths
                    })
        except Exception as ex:
            print('Problem with {}:{}'.format(os.path.basename(f), ex))
コード例 #14
0
def MB_curve(blk,unit_num,save_tgl=False,im_ext='svg',dpi_res=300):
    root = neoUtils.get_root(blk, unit_num)
    M = neoUtils.get_var(blk)
    use_flags = neoUtils.get_Cbool(blk)
    MB = mechanics.get_MB_MD(M)[0].magnitude.ravel()
    MB[np.invert(use_flags)]=0
    sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
    r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms)

    MB_bayes,edges = varTuning.stim_response_hist(MB*1e6,r,use_flags,nbins=100,min_obs=5)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(edges[:-1],MB_bayes,'o',color='k')
    ax.set_ylabel('Spike Rate (sp/s)')
    ax.set_xlabel('Bending Moment ($\mu$N-m)')
    plt.tight_layout()
    if save_tgl:
        plt.savefig('./figs/{}_MB_tuning.{}'.format(root,im_ext),dpi=dpi_res)
        plt.close('all')
コード例 #15
0
def get_arclength_bool(blk, unit_num, fname=None):
    # fname is the name of the csv file with arclength groupings
    if fname is None:
        if 'BOX_PATH' in os.environ:
            fname = os.path.join(
                os.environ['BOX_PATH'],
                r'__VG3D\_deflection_trials\_NEO\results\direction_arclength_FR_group_data.csv'
            )
        else:
            fname = os.path.join(
                '/projects/p30144/_VG3D/deflections/direction_arclength_FR_group_data.csv'
            )
    df = pd.read_csv(fname)
    id = neoUtils.get_root(blk, unit_num)
    sub_df = df[df.id == id]
    arclength_list = sub_df.Arclength.tolist()
    use_flags = neoUtils.concatenate_epochs(blk)
    if len(sub_df) != len(use_flags):
        raise ValueError(
            'The number of contacts in the block {} do not match the number of contacts in the csv {}'
            .format(len(use_flags), len(sub_df)))
    cbool = neoUtils.get_Cbool(blk)
    distal_cbool = np.zeros_like(cbool)
    medial_cbool = np.zeros_like(cbool)
    proximal_cbool = np.zeros_like(cbool)
    # loop through each contact and set the appropriate arclength boolean
    for ii in range(len(use_flags)):
        start = use_flags[ii].magnitude.astype('int')
        dur = use_flags.durations[ii].magnitude.astype('int')
        if arclength_list[ii] == 'Proximal':
            proximal_cbool[start:start + dur] = 1
        elif arclength_list[ii] == 'Distal':
            distal_cbool[start:start + dur] = 1
        elif arclength_list[ii] == 'Medial':
            medial_cbool[start:start + dur] = 1
    arclengths = {
        'Distal': distal_cbool,
        'Medial': medial_cbool,
        'Proximal': proximal_cbool
    }

    return (arclengths)
コード例 #16
0
def FX_plots(blk,unit_num,save_tgl=False,im_ext='svg',dpi_res=300):
    root = neoUtils.get_root(blk, unit_num)
    F = neoUtils.get_var(blk,'F')
    Fx = F.magnitude[:,0]
    use_flags = neoUtils.get_Cbool(blk)
    sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
    r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms)

    Fx[np.invert(use_flags)] = 0

    Fx_bayes, edges = varTuning.stim_response_hist(Fx * 1e6, r, use_flags, nbins=50, min_obs=5)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(edges[:-1], Fx_bayes*1000, 'o', color='k')
    ax.set_ylabel('Spike Rate (sp/s)')
    ax.set_xlabel('Axial Force ($\mu$N-m)')
    plt.tight_layout()
    if save_tgl:
        plt.savefig('./figs/{}_Fx_tuning.{}'.format(root,im_ext), dpi=dpi_res)
        plt.close('all')
コード例 #17
0
ファイル: get_whisker_PCA.py プロジェクト: nbush257/VG3D
def get_components(fname,p_smooth=None,smooth_idx=9):
    ''' Get the PCA comonents given a filename'''
    varlist = ['M', 'F', 'TH', 'PHIE']
    blk = neoUtils.get_blk(fname)
    cbool = neoUtils.get_Cbool(blk)
    root = neoUtils.get_root(blk,0)[:-2]
    X = GLM.create_design_matrix(blk,varlist)
    if p_smooth is not None:
        blk_smooth = GLM.get_blk_smooth(fname,p_smooth)
        Xdot = GLM.get_deriv(blk,blk_smooth,varlist,smoothing=[smooth_idx])[0]
        X = np.concatenate([X,Xdot],axis=1)
    X[np.invert(cbool),:]=0
    X = neoUtils.replace_NaNs(X,'pchip')
    X = neoUtils.replace_NaNs(X,'interp')

    scaler = sklearn.preprocessing.StandardScaler(with_mean=False)
    X[cbool,:] = scaler.fit_transform(X[cbool,:])

    pca = sklearn.decomposition.PCA()
    pca.fit_transform(X[cbool,:])

    return(pca,root)
コード例 #18
0
def smoothed(smooth_idx=9):
    smooth_vals = np.arange(5, 100, 10)
    sub_p_save = os.path.join(
        p_save, '{}ms_smoothing_deriv'.format(smooth_vals[smooth_idx]))
    if not os.path.isdir(sub_p_save):
        os.mkdir(sub_p_save)
    for f in glob.glob(os.path.join(p_load, '*NEO.h5')):
        try:
            blk = neoUtils.get_blk(f)
            blk_smooth = GLM.get_blk_smooth(f, p_smooth)
            num_units = len(blk.channel_indexes[-1].units)
            for unit_num in range(num_units):
                varlist = ['M', 'F', 'TH', 'PHIE']
                root = neoUtils.get_root(blk, unit_num)
                print('Working on {}'.format(root))
                outname = os.path.join(
                    sub_p_save,
                    '{}ms_{}_pillowX.mat'.format(smooth_vals[smooth_idx],
                                                 root))

                X = GLM.create_design_matrix(blk, varlist)
                Xdot = GLM.get_deriv(blk, blk_smooth, varlist, [smooth_idx])[0]
                X = np.concatenate([X, Xdot], axis=1)
                sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
                y = neoUtils.get_rate_b(blk, unit_num)[1]
                cbool = neoUtils.get_Cbool(blk)
                arclengths = get_arclength_bool(blk, unit_num)

                sio.savemat(outname, {
                    'X': X,
                    'y': y,
                    'cbool': cbool,
                    'arclengths': arclengths
                })
        except Exception as ex:
            print('Problem with {}:{}'.format(os.path.basename(f), ex))
コード例 #19
0
def calc_corr(fname, p_smooth, unit_num):
    blk = neoUtils.get_blk(fname)
    blk_smooth = GLM.get_blk_smooth(fname, p_smooth)
    varlist = ['M', 'F', 'TH', 'PHIE']
    component_list = [
        '{}_dot'.format(x)
        for x in ['Mx', 'My', 'Mz', 'Fx', 'Fy', 'Fz', 'TH', 'PHI']
    ]
    root = neoUtils.get_root(blk, unit_num)
    Xdot = GLM.get_deriv(blk, blk_smooth, varlist)[0]
    Xdot = np.reshape(Xdot, [-1, 8, 10])
    windows = np.arange(5, 100, 10)

    sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(0)]
    cbool = neoUtils.get_Cbool(blk)
    corr = []
    R = []
    # loop over variables
    for ii in range(Xdot.shape[1]):
        var_in = Xdot[:, ii, :].copy()
        # loop over smoothing
        r = []
        for jj in range(var_in.shape[1]):
            kernel = elephant.kernels.GaussianKernel(pq.ms * windows[jj])
            FR = elephant.statistics.instantaneous_rate(sp,
                                                        pq.ms,
                                                        kernel=kernel)
            idx = np.isfinite(var_in[:, jj])
            r.append(
                scipy.corrcoef(var_in[:, jj].ravel()[idx],
                               FR.magnitude.ravel()[idx])[0, 1])
        R.append(r)
    R = np.array(R)
    df = pd.DataFrame(data=R, columns=['{}ms'.format(x) for x in windows])
    df.index = component_list
    return (df)
コード例 #20
0
def calc_world_geom_hist(p_load,p_save,n_bins=100):
    """
     Since calculation takes so long on getting the histograms (mostly loading of data)
    we want to calculate them once and save the data.

    This calculates the Geometry.

    :param p_load: Location where all the neo h5 files live
    :param p_save: Location to save the output data files
    :param n_bins: Number of bins in with which to split the data
    :return None: Saves a 'world_geom_hists.npz' file.
    """
    # init
    ID = []
    all_S_bayes = []
    all_TH_bayes = []
    all_PHIE_bayes = []
    all_ZETA_bayes = []

    all_S_edges = []
    all_TH_edges = []
    all_PHIE_edges = []
    all_ZETA_edges = []

    # loop files
    for f in glob.glob(os.path.join(p_load,'rat*.h5')):
        # load in
        print(os.path.basename(f))
        blk = neoUtils.get_blk(f)

        # get contact
        Cbool = neoUtils.get_Cbool(blk)
        use_flags = neoUtils.concatenate_epochs(blk)

        # get vars
        S = neoUtils.get_var(blk, 'S').magnitude

        TH = neoUtils.get_var(blk, 'TH').magnitude
        neoUtils.center_var(TH, use_flags)

        PHIE = neoUtils.get_var(blk, 'PHIE').magnitude
        neoUtils.center_var(PHIE, use_flags)

        ZETA = neoUtils.get_var(blk, 'ZETA').magnitude
        neoUtils.center_var(ZETA, use_flags)

        # loop units
        for unit in blk.channel_indexes[-1].units:
            # get unit info
            unit_num = int(unit.name[-1])
            r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms)
            sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
            root = neoUtils.get_root(blk,unit_num)
            ID.append(root)

            # Create hists
            S_bayes, S_edges = varTuning.stim_response_hist(S.ravel(), r, Cbool, nbins=n_bins, min_obs=5)
            TH_bayes, TH_edges = varTuning.stim_response_hist(TH.ravel(), r, Cbool, nbins=n_bins, min_obs=5)
            PHIE_bayes, PHIE_edges = varTuning.stim_response_hist(PHIE.ravel(), r, Cbool, nbins=n_bins,min_obs=5)
            ZETA_bayes, ZETA_edges = varTuning.stim_response_hist(ZETA.ravel(), r, Cbool, nbins=n_bins,min_obs=5)

            # append outputs
            plt.close('all')
            all_S_bayes.append(S_bayes)
            all_TH_bayes.append(TH_bayes)
            all_PHIE_bayes.append(PHIE_bayes)
            all_ZETA_bayes.append(ZETA_bayes)

            all_S_edges.append(S_edges)
            all_TH_edges.append(TH_edges)
            all_PHIE_edges.append(PHIE_edges)
            all_ZETA_edges.append(ZETA_edges)


    np.savez(os.path.join(p_save, 'world_geom_hists.npz'),
             all_S_bayes=all_S_bayes,
             all_TH_bayes=all_TH_bayes,
             all_PHIE_bayes=all_PHIE_bayes,
             all_ZETA_bayes=all_ZETA_bayes,
             all_S_edges=all_S_edges,
             all_TH_edges=all_TH_edges,
             all_PHIE_edges=all_PHIE_edges,
             all_ZETA_edges=all_ZETA_edges,
             ID=ID
             )
コード例 #21
0
def calc_all_mech_hists(p_load,p_save,n_bins=100):
    """
    Since calculation takes so long on getting the histograms (mostly loading of data)
    we want to calculate them once and save the data.

    This calculates the mechanics.

    :param p_load: Location where all the neo h5 files live
    :param p_save: Location to save the output data files
    :param n_bins: Number of bins in with which to split the data
    :return None: Saves a 'mech_histograms.npz' file.
    """

    # TODO: This is currently pretty gross, it is really too hardcoded (I wrote it in a car). Do better.
    # TODO: Combine with geometry

    # Case in point:
    all_F_edges = []
    all_M_edges = []
    all_F_bayes = []
    all_M_bayes = []
    all_MB_edges = []
    all_MD_edges = []
    all_MD_bayes = []
    all_MB_bayes = []
    ID = []

    # Loop all neo files
    for f in glob.glob(os.path.join(p_load,'rat*.h5')):
        print(os.path.basename(f))
        blk = neoUtils.get_blk(f)
        Cbool = neoUtils.get_Cbool(blk)
        # Loop all units
        for unit in blk.channel_indexes[-1].units:
            unit_num = int(unit.name[-1])

            # grab needed variables
            r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms)
            sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
            root = neoUtils.get_root(blk,unit_num)
            M = neoUtils.get_var(blk).magnitude
            F = neoUtils.get_var(blk,'F').magnitude
            MB, MD = neoUtils.get_MB_MD(M)

            # init histograms
            M_bayes = np.empty([n_bins,3])
            F_bayes = np.empty([n_bins, 3])

            M_edges = np.empty([n_bins+1, 3])
            F_edges = np.empty([n_bins+1, 3])

            #calculate tuning curves (seperately on each dimension)
            for ii in range(3):
                F_bayes[:, ii], F_edges[:, ii] = varTuning.stim_response_hist(F[:, ii] * 1e6, r, Cbool, nbins=n_bins, min_obs=5)
                M_bayes[:, ii], M_edges[:, ii] = varTuning.stim_response_hist(M[:, ii] * 1e6, r, Cbool, nbins=n_bins, min_obs=5)
            MB_bayes, MB_edges = varTuning.stim_response_hist(MB.squeeze() * 1e6, r, Cbool, nbins=n_bins, min_obs=5)
            MD_bayes, MD_edges,_,_ = varTuning.angular_response_hist(MD.squeeze(), r, Cbool, nbins=n_bins)
            plt.close('all')

            # append to output lists
            all_F_edges.append(F_edges)
            all_M_edges.append(M_edges)
            all_MB_edges.append(MB_edges)
            all_MD_edges.append(MD_edges)

            all_F_bayes.append(F_bayes)
            all_M_bayes.append(M_bayes)
            all_MB_bayes.append(MB_bayes)
            all_MD_bayes.append(MD_bayes)
            ID.append(root)
    # save
    np.savez(os.path.join(p_save,'mech_histograms.npz'),
             all_F_bayes=all_F_bayes,
             all_F_edges=all_F_edges,
             all_M_bayes=all_M_bayes,
             all_M_edges=all_M_edges,
             all_MB_bayes=all_MB_bayes,
             all_MB_edges=all_MB_edges,
             all_MD_bayes=all_MD_bayes,
             all_MD_edges=all_MD_edges,
             ID=ID
             )
コード例 #22
0
ファイル: observe_spiking.py プロジェクト: nbush257/VG3D
import sys
import neoUtils
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
sns.set()
sns.set_style('ticks')

blk = neoUtils.get_blk(sys.argv[1])
M = neoUtils.get_var(blk).magnitude
sp = neoUtils.concatenate_sp(blk)
cc = neoUtils.concatenate_epochs(blk, -1)
Cbool = neoUtils.get_Cbool(blk)
c_idx = np.where(Cbool)[0]
# M[np.invert(Cbool),:] = 0

ymax = np.nanmax(M) / 4
ymin = np.nanmin(M) / 4


def shadeVector(cc, color='k'):
    ax = plt.gca()
    ylim = ax.get_ylim()
    for start, dur in zip(cc.times.magnitude, cc.durations.magnitude):
        ax.fill([start, start, start + dur, start + dur],
                [ylim[0], ylim[1], ylim[1], ylim[0]],
                color,
                alpha=0.1)


for ii in xrange(len(sp)):
コード例 #23
0
def phase_plots(blk,unit_num,save_tgl=False,bin_stretch=False,p_save=None,im_ext='png',dpi_res=300):
    ''' Plot Phase planes for My and Mz'''
    root = neoUtils.get_root(blk, unit_num)
    M = neoUtils.get_var(blk).magnitude
    sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)]
    r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms)


    use_flags = neoUtils.get_Cbool(blk)
    Mdot = mechanics.get_deriv(M)


    if bin_stretch:
        raise Exception('Not finished with use_flags')
        # MY, logit_y = nl(M[idx, 1], 90)
        # MZ, logit_z = nl(M[idx, 2], 90)
        # MY_dot, logit_ydot = nl(Mdot[idx, 1], 95)
        # MZ_dot, logit_zdot = nl(Mdot[idx, 2], 95)

    else:
        MY = M[:, 1] * 1e-6
        MZ = M[:, 2] * 1e-6
        MY_dot = Mdot[:, 1] * 1e-6
        MZ_dot = Mdot[:, 2] * 1e-6

    My_response,My_edges,Mydot_edges = varTuning.joint_response_hist(MY, MY_dot, r, use_flags, [100,30],min_obs=15)
    Mz_response,Mz_edges,Mzdot_edges = varTuning.joint_response_hist(MZ, MZ_dot, r, use_flags, [100,30],min_obs=15)


    if bin_stretch:
        My_edges = logit_y(My_edges)
        Mz_edges = logit_z(Mz_edges)
        Mydot_edges = logit_ydot(Mydot_edges)
        Mzdot_edges = logit_zdot(Mzdot_edges)
    else:
        pass

    axy = varTuning.plot_joint_response(My_response,My_edges,Mydot_edges,contour=False)
    axz = varTuning.plot_joint_response(Mz_response,Mz_edges,Mzdot_edges,contour=False)

    # Set bounds
    y_mask = My_response.mask.__invert__()
    if not y_mask.all():
        axy.set_ylim(Mydot_edges[np.where(y_mask)[0].min()], Mydot_edges[np.where(y_mask)[0].max()])
        axy.set_xlim(My_edges[np.where(y_mask)[1].min()], My_edges[np.where(y_mask)[1].max()])

    z_mask = Mz_response.mask.__invert__()
    if not z_mask.all():
        axz.set_ylim(Mzdot_edges[np.where(z_mask)[0].min()], Mzdot_edges[np.where(z_mask)[0].max()])
        axz.set_xlim(Mz_edges[np.where(z_mask)[1].min()], Mz_edges[np.where(z_mask)[1].max()])

    # other annotations
    axy.set_title('M$_y$ Phase Plane')
    axz.set_title('M$_z$ Phase Plane')

    axy.set_xlabel('M$_y$ ($\mu$N-m)')
    axy.set_ylabel('M$_\dot{y}$ ($\mu$N-m/ms)')

    axz.set_xlabel('M$_z$ ($\mu$N-m)')
    axz.set_ylabel('M$_\dot{z}$ ($\mu$N-m/ms)')

    axy.grid('off')
    axy.set_facecolor([0.6, 0.6, 0.6])
    axy.axvline(color='k',linewidth=1)
    axy.axhline(color='k',linewidth=1)

    axz.grid('off')
    axz.set_facecolor([0.6, 0.6, 0.6])
    axz.axvline(color='k', linewidth=1)
    axz.axhline(color='k', linewidth=1)


    plt.sca(axy)
    plt.tight_layout()
    if save_tgl:
        if p_save is None:
            raise ValueError("figure save location is required")
        else:
            plt.savefig(os.path.join(p_save,'{}_My_phaseplane.{}'.format(root,im_ext)),dpi=dpi_res)

    plt.sca(axz)
    plt.tight_layout()
    if save_tgl:
        if p_save is None:
            raise ValueError("figure save location is required")
        else:
            plt.savefig(os.path.join(p_save,'{}_Mz_phaseplane.{}'.format(root,im_ext)),dpi=dpi_res)
        plt.close('all')
コード例 #24
0
def plot_smooth_hists(blk,blk_smooth,unit_num=0,p_save=None,nbins=75):
    DPI_RES=600
    id = neoUtils.get_root(blk, unit_num)
    fig_name = os.path.join(p_save, '{}_derivative_smoothing_compare.png'.format(id))
    if os.path.isfile(fig_name):
        print('{} found, skipping...'.format(fig_name))
        return(None)

    smoothing_windows = range(5,101,10)
    use_flags = neoUtils.concatenate_epochs(blk)
    cbool = neoUtils.get_Cbool(blk)
    r,b =neoUtils.get_rate_b(blk,unit_num,2*pq.ms)

    # catch empty smoothed data
    if len(blk_smooth.segments)==0 or len(blk_smooth.segments[0].analogsignals)==0:
        print('Smoothed data not found in {}'.format(id))
        return(-1)

    # get vars
    M = neoUtils.get_var(blk_smooth,'M_smoothed').magnitude
    M[np.invert(cbool),:]=np.nan
    Mdot = neoUtils.get_deriv(M)

    F = neoUtils.get_var(blk_smooth,'F_smoothed').magnitude
    F[np.invert(cbool),:]=np.nan
    Fdot = neoUtils.get_deriv(F)

    PHI = neoUtils.get_var(blk_smooth,'PHIE_smoothed').magnitude
    PHI = neoUtils.center_var(PHI.squeeze(),use_flags)
    PHI[np.invert(cbool),:]=np.nan
    PHIdot = neoUtils.get_deriv(PHI)

    TH = neoUtils.get_var(blk_smooth,'TH_smoothed').magnitude
    TH = neoUtils.center_var(TH.squeeze(),use_flags)
    TH[np.invert(cbool),:]=np.nan
    THdot = neoUtils.get_deriv(TH)

    # ROT = np.sqrt(np.add(np.power(PHI,2),np.power(TH,2)))
    # ROTdot = neoUtils.get_deriv(ROT)


    # calculate histograms
    R_Mdot, bins_Mdot, edgesx_Mdot, edgesy_Mdot = mult_join_plots(Mdot[:, 1, :], Mdot[:, 2, :], r, cbool, bins=nbins)
    newbins =[np.linspace(bins_Mdot[0][edgesx_Mdot][0],bins_Mdot[0][edgesx_Mdot][1],nbins),
              np.linspace(bins_Mdot[1][edgesy_Mdot][0], bins_Mdot[1][edgesy_Mdot][1], nbins)]
    R_Mdot, bins_Mdot, edgesx_Mdot, edgesy_Mdot = mult_join_plots(Mdot[:, 1, :], Mdot[:, 2, :], r, cbool, bins=newbins)

    R_Fdot, bins_Fdot, edgesx_Fdot, edgesy_Fdot = mult_join_plots(Fdot[:, 1, :], Fdot[:, 2, :], r, cbool,bins=nbins)
    newbins = [np.linspace(bins_Fdot[0][edgesx_Fdot][0], bins_Fdot[0][edgesx_Fdot][1], nbins),
               np.linspace(bins_Fdot[1][edgesy_Fdot][0], bins_Fdot[1][edgesy_Fdot][1], nbins)]
    R_Fdot, bins_Fdot, edgesx_Fdot, edgesy_Fdot = mult_join_plots(Fdot[:, 1, :], Fdot[:, 2, :], r, cbool, bins=newbins)

    R_ROTdot, bins_ROTdot, edgesx_ROTdot, edgesy_ROTdot = mult_join_plots(THdot, PHIdot, r, cbool,bins=nbins)
    newbins = [np.linspace(bins_ROTdot[0][edgesx_ROTdot][0], bins_ROTdot[0][edgesx_ROTdot][1], nbins),
               np.linspace(bins_ROTdot[1][edgesy_ROTdot][0], bins_ROTdot[1][edgesy_ROTdot][1], nbins)]
    R_ROTdot, bins_ROTdot, edgesx_ROTdot, edgesy_ROTdot = mult_join_plots(THdot, PHIdot, r, cbool, bins=newbins)

    FR = []
    FR.append(np.nanmax([x.max() for x in R_Mdot.values()]))
    FR.append(np.nanmax([x.max() for x in R_Fdot.values()]))
    FR.append(np.nanmax([x.max() for x in R_ROTdot.values()]))
    colormax = np.nanmax(FR)

    # Plots
    f = plt.figure()
    figManager = plt.get_current_fig_manager()
    figManager.window.showMaximized()
    # hardcoded for 5 smoothing steps
    for loc,ii in enumerate(range(0,10,2)):
        ax = f.add_subplot(3,5,loc+1)
        ax.pcolormesh(bins_Mdot[0],bins_Mdot[1],R_Mdot[ii], cmap='OrRd', edgecolors='None',vmin=0,vmax=colormax)
        ax.set_xlim(bins_Mdot[0][edgesx_Mdot])
        ax.set_ylim(bins_Mdot[1][edgesy_Mdot])
        ax.set_title('Smoothing window = {}ms'.format(smoothing_windows[ii]))
        ax.axvline(color='k',linewidth=1)
        ax.axhline(color='k',linewidth=1)
        if ii==0:
            ax.set_ylabel('$\\dot{M_y}$ vs  $\\dot{M_z}$',rotation=0,labelpad=20)
    for loc,ii in enumerate(range(0,10,2)):
        ax = f.add_subplot(3, 5, loc + 1+5)
        ax.pcolormesh(bins_Fdot[0], bins_Fdot[1], R_Fdot[ii], cmap='OrRd', edgecolors='None', vmin=0, vmax=colormax)
        ax.set_xlim(bins_Fdot[0][edgesx_Fdot])
        ax.set_ylim(bins_Fdot[1][edgesy_Fdot])
        ax.axvline(color='k', linewidth=1)
        ax.axhline(color='k', linewidth=1)
        if ii==0:
            ax.set_ylabel('$\\dot{F_y}$ vs $\\dot{F_z}$',rotation=0,labelpad=20)
    for loc,ii in enumerate(range(0,10,2)):
        ax = f.add_subplot(3, 5, loc + 1+10)
        h=ax.pcolormesh(bins_ROTdot[0], bins_ROTdot[1], R_ROTdot[ii], cmap='OrRd', edgecolors='None', vmin=0, vmax=colormax)
        ax.set_xlim(bins_ROTdot[0][edgesx_ROTdot])
        ax.set_ylim(bins_ROTdot[1][edgesy_ROTdot])
        ax.axvline(color='k', linewidth=1)
        ax.axhline(color='k', linewidth=1)
        if ii==0:
            ax.set_ylabel('$\\dot{\\theta}$ vs $\\dot{\\phi}$',rotation=0,labelpad=20)
    plt.suptitle('{}'.format(id))
    plt.colorbar(h)
    plt.pause(0.1)

    if p_save is not None:
        plt.savefig(fig_name,dpi=DPI_RES)
        plt.close('all')
    return(None)