Esempio n. 1
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)
Esempio n. 2
0
def get_PS_given_R(blk, unit_num=0):
    if True:
        raise Exception('This doesnt work yet')

    CP = neoUtils.get_var(blk, 'CP')
    S = float(blk.annotations['s'][2:-1])
    CP /= S

    FR = neoUtils.get_rate_b(blk, unit_num=unit_num, sigma=2 * pq.ms)[1]
    spiked = np.logical_and(np.all(np.isfinite(CP), axis=1), FR)
    idx = np.all(np.isfinite(CP), axis=1)

    PR_S, edges = np.histogramdd(CP.magnitude[spiked, :], bins=50)
    PS, edges = np.histogramdd(CP.magnitude[idx, :], bins=50)

    return (post)
Esempio n. 3
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))
Esempio n. 4
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)
Esempio n. 5
0
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)
Esempio n. 6
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')
Esempio n. 7
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))
Esempio n. 8
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')
Esempio n. 9
0
def ent_analyses(blk, X_disc=128, Y_disc=64):
    CP = neoUtils.get_var(blk, 'CP')
    S = float(blk.annotations['s'][2:-1])
    CP /= S
    CP = CP.magnitude
    idx = np.all(np.isfinite(CP), axis=1)
    s = np.empty_like(CP)
    s[:] = np.nan
    s[idx, :] = pye.quantise(CP[idx, :], X_disc, uniform='bins')[0]
    FR = neoUtils.get_rate_b(blk, unit_num=unit_num, sigma=2 * pq.ms)[0]
    FR = pye.quantise(FR, Y_disc, uniform='bins')[0]

    idx = np.all(np.isfinite(s), axis=1)
    X = s.astype('int64').T[:, idx]
    Y = FR[np.newaxis, idx]
    DS = pye.DiscreteSystem(X, (X.shape[0], bins), Y, (1, bins))
    DS.calculate_entropies()

    #TODO: I have created a discrete FR and Stimulus, now I need to perform the actual entropy calcs
    if True:
        raise Exception('This is not done')
Esempio n. 10
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))
Esempio n. 11
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')
Esempio n. 12
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
             )
Esempio n. 13
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
             )
Esempio n. 14
0
        'verbosity': 0,
        'threshold': 1e-8,
        'max_iter': 1e2,
        'regularize_weights': {
            'strength': 1e-3,
            'norm': 'L2'
        }
    }
    # comment this line out if not testing
    # the test params make the iteration faster
    #params=test_params ;print('testing params!!')

    for unit_num in range(len(blk.channel_indexes[-1].units)):
        R = {}
        yhat, yhat_sim, model = run_dropout(fname, p_smooth, unit_num, params)
        y = neoUtils.get_rate_b(blk, unit_num)[1]
        X, y, cbool = get_X_y(fname, p_smooth, unit_num, pca_tgl=True,
                              n_pcs=6)  #change to 6postest
        root = neoUtils.get_root(blk, unit_num)
        for key in yhat.iterkeys():
            R[key], kernel_sizes = get_correlations(y, yhat[key],
                                                    yhat_sim[key], cbool)

        root = neoUtils.get_root(blk, unit_num)
        np.savez(os.path.join(p_save, '{}_STM_PCA.npz'.format(root)),
                 X=X,
                 y=y,
                 yhat=yhat,
                 yhat_sim=yhat_sim,
                 cbool=cbool,
                 R=R,
Esempio n. 15
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)