예제 #1
0
def init():
    files_para = '../data/ForNorData_MSTd/'
    files_r = '../data/AllData_MSTd/AllData/'
    both_azimuth_files = os.listdir(files_para)
    r_files = os.listdir(files_r)

    VEST = list(range(0, 360 + 45, 45))  # H: vest azimuth
    VIS = list(range(0, 360 + 45, 45))  # H: vis azimuth

    # H_hat: vest azimuth load, vis azimuth load, vest elevation all zero, vis elevation all zero,
    # H: vest azimuth in [0,360,45] range, vis azimuth in [0,360,45] range,
    # vest elevation all zero, vis elevation all zero,
    global vest_a, vis_a, vest_e, vis_e, n1, n2, n3, e, c_vest, c_vis, n0, \
        S_vest_m, S_vis_m,RawR_trial,RawR
    vest_a = []  # vest azimuth in H_hat
    vis_a = []  # vis azimuth in H_hat
    RawR = [
    ]  # firing curve, mean of neuron responses under different stimulus conditions, 就是我们最后要fit的y值(平均数)
    RawR_trial = [
    ]  # neuron responses under different stimulus conditions, 就是我们最后要fit的y值(实际trial的y值)
    RawR_cam = []
    RawR_ves = []
    index = []
    # vest_resp = [] # 不需要用到,只是在算Rmax的时候用了
    # vis_resp = []
    collection = CoherenceSelect.CoherenceSelect()
    for f in both_azimuth_files:
        if f[:-18] in collection:
            temp_file = format_matfile(files_para + f, 'ForNorData')
            vest_a.append(temp_file['vest_Direc'])
            vis_a.append(temp_file['vis_Direc'])
            index.append(files_r + f[:-15])
            temp_file = format_matfile(
                files_r + 'SmoothData_' + f[:-15] + '.mat',
                'CueConflictDataSmooth')
            RawR_trial.append(temp_file['resp_trial_conflict'])
            # RawR.append(temp_file['resp_conflict_200'])
            RawR_cam.append(temp_file['resp_vis'])
            RawR_ves.append(temp_file['resp_ves'])
    SIZE = len(RawR_cam)
    vest_resp = RawR_ves
    vis_resp = RawR_cam
    vest_a = [i + 360 if i < 0 else i for i in np.ravel(vest_a)]
    vis_a = [i + 360 if i < 0 else i for i in np.ravel(vis_a)]

    vest_e = np.zeros(len(vest_a))
    vis_e = np.zeros(len(vis_a))

    vest_stim_ele = 0
    vis_stim_ele = 0

    n1 = n2 = n3 = e = c_vest = c_vis = 1
    n0 = 2

    init_vect = np.array([
        np.random.rand(SIZE) + 9.5,
        np.random.rand(SIZE) + 0.5,
        np.random.rand(SIZE) - 0.53,
        np.random.rand(SIZE) + 0.55,
        np.zeros((SIZE, ))
    ])  # 参数初始值

    global Rmax_m
    Rmax_m = []
    # Rmax_m = np.zeros((200, 9, 9))
    S_vest_m_cam = np.zeros((SIZE, 9, 9))
    S_vis_m_cam = np.zeros((SIZE, 9, 9))
    S_vest_m_ves = np.zeros((SIZE, 9, 9))
    S_vis_m_ves = np.zeros((SIZE, 9, 9))
    c_vest = 0
    c_vis = 1
    for k in range(len(VEST)):
        for j in range(len(VIS)):
            vest_stim_az = VEST[k]
            vis_stim_az = VIS[j]
            # Rmax = np.maximum(np.ravel(np.array(vest_resp)[:, k]), np.ravel(np.array(vis_resp)[:, :, j]))
            # Rmax_m[:, k, j] = Rmax
            S_vest = spherical_sinusoid(vest_stim_az, vest_a, vest_stim_ele,
                                        vest_e, c_vest, n0)
            S_vest_m_cam[:, k, j] = S_vest
            S_vis = spherical_sinusoid(vis_stim_az, vis_a, vis_stim_ele, vis_e,
                                       c_vis, n0)
            S_vis_m_cam[:, k, j] = S_vis
    Rmax1 = []
    Rmax2 = []
    for k in range(SIZE):
        Rmax1.append([])
        for i in range(len(VIS)):
            Max = np.array(vis_resp)[k, 0, i]
            for j in range(len(VEST)):
                Max = max(Max, np.array(vest_resp)[k, j, 0])
            Rmax1[k].append(Max)
    print(np.array(Rmax1).shape)
    for k in range(SIZE):
        Rmax2.append([])
        for i in range(len(VIS)):
            Max = np.array(vest_resp)[k, i, 0]
            for j in range(len(VEST)):
                Max = max(Max, np.array(vis_resp)[k, 0, j])
            Rmax2[k].append(Max)
    print(np.array(Rmax2).shape)
    for k in range(len(Rmax1)):
        Rmax_m.append([])
        Rmax_m[k].append(Rmax1[k])
        Rmax_m[k].append(Rmax2[k])
    print(np.array(Rmax_m).shape)
    c_vest = 1
    c_vis = 0
    for k in range(len(VEST)):
        for j in range(len(VIS)):
            vest_stim_az = VEST[k]
            vis_stim_az = VIS[j]
            #         Rmax = np.maximum(np.ravel(np.array(vest_resp)[:, k]), np.ravel(np.array(vis_resp)[:, :, j]))
            #         Rmax_m[:, k, j] = Rmax
            S_vest = spherical_sinusoid(vest_stim_az, vest_a, vest_stim_ele,
                                        vest_e, c_vest, n0)
            S_vest_m_ves[:, k, j] = S_vest
            S_vis = spherical_sinusoid(vis_stim_az, vis_a, vis_stim_ele, vis_e,
                                       c_vis, n0)
            S_vis_m_ves[:, k, j] = S_vis
    c_vest = 1
    c_vis = 1
    #select_good

    global selected_neurons
    selected_neurons = list(range(SIZE))
    S_vest_m = []
    for i in range(len(selected_neurons)):
        S_vest_m.append([])
    S_vis_m = []
    for i in range(len(selected_neurons)):
        S_vis_m.append([])
    # S_vest_m_cam = S_vest_m_cam[selected_neurons]
    # S_vest_m_cam=S_vest_m_cam.tolist()
    # S_vest_m_ves = S_vest_m_ves[selected_neurons]
    # S_vest_m_ves=S_vest_m_ves.tolist()
    for i in range(len(selected_neurons)):
        S_vest_m[i].append(S_vest_m_cam[i][0])
        u = []
        for j in range(len(S_vest_m_ves[i])):
            u.append(S_vest_m_ves[i][j][0])
        S_vest_m[i].append(u)
    S_vest_m = np.array(S_vest_m)

    # S_vis_m_cam= S_vis_m_cam[selected_neurons]
    # S_vis_m_ves= S_vis_m_ves[selected_neurons]
    # S_vis_m_cam=S_vis_m_cam.tolist()
    # S_vis_m_ves=S_vis_m_ves.tolist()
    for i in range(len(selected_neurons)):
        S_vis_m[i].append(S_vis_m_cam[i][0])
        u = []
        for j in range(len(S_vis_m_ves[i])):
            u.append(S_vis_m_ves[i][j][0])
        S_vis_m[i].append(u)
    S_vis_m = np.array(S_vis_m)

    # init_vect = init_vect[:, selected_neurons]
    # vis_a = np.array(vis_a)[selected_neurons].tolist()
    # vest_a = np.array(vest_a)[selected_neurons].tolist()

    for i in range(len(selected_neurons)):
        RawR.append([])
    # RawR_cam=(np.array(RawR_cam)[selected_neurons]).tolist()
    # RawR_ves=(np.array(RawR_ves)[selected_neurons]).tolist()
    for i in range(len(selected_neurons)):
        RawR[i].append(RawR_cam[i][0])
        u = []
        for j in range(len(RawR_ves[i])):
            u.append(RawR_ves[i][j][0])
        RawR[i].append(u)
    RawR = np.array(RawR)
    print(RawR.shape)
    # RawR_trial = np.array(RawR_trial)[selected_neurons]

    corr_list = [(np.triu(np.corrcoef([i.ravel()
                                       for i in RawR_trial[n]])).sum() -
                  RawR_trial[n].shape[0]) / comb(RawR_trial[n].shape[0], 2)
                 for n in range(SIZE)]
    selected_neurons = [
        i for i in range(SIZE) if corr_list[i] > 0.5
    ]  # select good neurons: correlation between trials over 0.4
    S_vest_m = S_vest_m[selected_neurons]
    S_vis_m = S_vis_m[selected_neurons]
    Rmax_m = np.array(Rmax_m)[selected_neurons]
    Rmax_m = Rmax_m.tolist()
    init_vect = init_vect[:, selected_neurons]
    vis_a = np.array(vis_a)[selected_neurons].tolist()
    vest_a = np.array(vest_a)[selected_neurons].tolist()
    RawR = np.array(RawR)[selected_neurons]
    RawR_trial = np.array(RawR_trial)[selected_neurons]
    S_vest_m = S_vest_m
    S_vis_m = S_vis_m

    # optimize()
    start_time = time.time()
    global errors
    errors = []
    print('init_vect:', np.array(init_vect).shape)
    res = minimize(optimize_f, np.ravel(init_vect), method='SLSQP',
                   bounds=((0, None),) * len(selected_neurons) + ((0, None),) * len(selected_neurons) \
                          + ((None, None),) * len(selected_neurons) + ((None, None),) * len(selected_neurons) + (
                          (None, None),) * len(selected_neurons),
                   options={'maxiter': 10000})
    data_write('../resultConsiderCoherence/参数4_nobound_unimodal.xls',
               (np.array(res.x).reshape(5, len(selected_neurons)).tolist()))
    print('Error:', res.fun)

    # R_square = 1 - res.fun/np.ravel((RawR - np.array(len(selected_neurons)*[np.mean(RawR,axis=0)]))**2).sum()
    R_square = 1 - res.fun * 10000000 / np.ravel(
        (RawR - np.array(len(selected_neurons) * [np.mean(RawR, axis=0)]))**
        2).sum()

    print('R_square:', R_square)
    print("--- %s seconds ---" % (time.time() - start_time))
    rs = calculate_R(res.x)
예제 #2
0
files_para = '../data/ForNorData_MSTd/'
files_r = '../data/AllData_MSTd/AllData/'
both_azimuth_files = os.listdir(files_para)
r_files = os.listdir(files_r)

VEST = list(range(0, 360 + 45, 45))  # H: vest azimuth
VIS = list(range(0, 360 + 45, 45))  # H: vis azimuth

global vest_a, vis_a, vest_e, vis_e, n1, n2, n3, e, c_vest, c_vis, n0, \
    S_vest_m, S_vis_m,RawR_trial,RawR
RawR = [
]  # firing curve, mean of neuron responses under different stimulus conditions, 就是我们最后要fit的y值(平均数)
RawR_trial = [
]  # neuron responses under different stimulus conditions, 就是我们最后要fit的y值(实际trial的y值)
collection = CoherenceSelect.CoherenceSelect()
for f in both_azimuth_files:
    if f[:-18] in collection:
        temp_file = format_matfile(files_r + 'SmoothData_' + f[:-15] + '.mat',
                                   'CueConflictDataSmooth')
        RawR_trial.append(temp_file['resp_trial_conflict'])
        RawR.append(temp_file['resp_conflict'])
SIZE = len(RawR)
corr_list = [(np.triu(np.corrcoef([i.ravel() for i in RawR_trial[n]])).sum() -
              RawR_trial[n].shape[0]) / comb(RawR_trial[n].shape[0], 2)
             for n in range(SIZE)]
global selected_neurons
selected_neurons = [i for i in range(SIZE) if corr_list[i] > 0.4]
RawR = np.array(RawR)[selected_neurons]
RawR_trial = np.array(RawR_trial)[selected_neurons]
print(np.array(RawR).shape)
예제 #3
0
def init():
    files_para = '../data/ForNorData_MSTd/'
    files_r = '../data/AllData_MSTd/AllData/'
    both_azimuth_files = os.listdir(files_para)

    r_files = os.listdir(files_r)

    VEST = list(range(0, 360 + 45, 45))  # H: vest azimuth
    VIS = list(range(0, 360 + 45, 45))  # H: vis azimuth

    # H_hat: vest azimuth load, vis azimuth load, vest elevation all zero, vis elevation all zero,
    # H: vest azimuth in [0,360,45] range, vis azimuth in [0,360,45] range,
    # vest elevation all zero, vis elevation all zero,
    global vest_a, vis_a, vest_e, vis_e, n1, n2, n3, e, c_vest, c_vis, n0, \
        S_vest_m, S_vis_m,RawR_trial,RawR
    vest_a = []  # vest azimuth in H_hat
    vis_a = []  # vis azimuth in H_hat
    RawR = [
    ]  # firing curve, mean of neuron responses under different stimulus conditions, 就是我们最后要fit的y值(平均数)
    RawR_trial = [
    ]  # neuron responses under different stimulus conditions, 就是我们最后要fit的y值(实际trial的y值)
    vest_resp = []  # 不需要用到,只是在算Rmax的时候用了
    vis_resp = []
    collection = CoherenceSelect.CoherenceSelect()
    for f in both_azimuth_files:
        if f[:-18] in collection:
            temp_file = format_matfile(files_para + f, 'ForNorData')
            vest_a.append(temp_file['vest_Direc'])
            vis_a.append(temp_file['vis_Direc'])

            temp_file = format_matfile(
                files_r + 'SmoothData_' + f[:-15] + '.mat',
                'CueConflictDataSmooth')
            RawR_trial.append(temp_file['resp_trial_conflict'])
            RawR.append(temp_file['resp_conflict'])
            vest_resp.append(temp_file['resp_ves'])
            vis_resp.append(temp_file['resp_vis'])
        SIZE = len(vis_resp)
        # temp_file = format_matfile(files_r + f[:-15] + '.mat', 'CueConflictData')
        # RawR_trial.append(temp_file['resp_trial_conflict_200'])
        # RawR.append(temp_file['resp_conflict_200'])
    vest_a = [i + 360 if i < 0 else i for i in np.ravel(vest_a)]
    vis_a = [i + 360 if i < 0 else i for i in np.ravel(vis_a)]

    vest_e = np.zeros(len(vest_a))
    vis_e = np.zeros(len(vis_a))

    vest_stim_ele = 0
    vis_stim_ele = 0

    n1 = n2 = n3 = e = c_vest = c_vis = 1
    n0 = 2

    init_vect = np.array([
        np.random.rand(SIZE) + 9.5,
        np.random.rand(SIZE) + 0.5,
        np.random.rand(SIZE) - 0.53,
        np.random.rand(SIZE) + 0.55,
        np.random.rand(SIZE)
    ])  # 参数初始值
    global Rmax_m
    Rmax_m = np.zeros((SIZE, 9, 9))
    S_vest_m = np.zeros((SIZE, 9, 9))
    S_vis_m = np.zeros((SIZE, 9, 9))
    for k in range(len(VEST)):
        for j in range(len(VIS)):
            vest_stim_az = VEST[k]
            vis_stim_az = VIS[j]
            Rmax = np.maximum(np.ravel(np.array(vest_resp)[:, k]),
                              np.ravel(np.array(vis_resp)[:, :, j]))
            Rmax_m[:, k, j] = Rmax
            S_vest = spherical_sinusoid(vest_stim_az, vest_a, vest_stim_ele,
                                        vest_e, c_vest, n0)
            S_vest_m[:, k, j] = S_vest
            S_vis = spherical_sinusoid(vis_stim_az, vis_a, vis_stim_ele, vis_e,
                                       c_vis, n0)
            S_vis_m[:, k, j] = S_vis

    corr_list = [(np.triu(np.corrcoef([i.ravel()
                                       for i in RawR_trial[n]])).sum() -
                  RawR_trial[n].shape[0]) / comb(RawR_trial[n].shape[0], 2)
                 for n in range(SIZE)]
    global selected_neurons
    selected_neurons = [
        i for i in range(SIZE) if corr_list[i] > 0.5
    ]  # select good neurons: correlation between trials over 0.4
    # global  selected_neurons
    # selected_neurons=list(range(200))
    S_vest_m = S_vest_m[selected_neurons]
    S_vis_m = S_vis_m[selected_neurons]
    Rmax_m = Rmax_m[selected_neurons]
    init_vect = init_vect[:, selected_neurons]
    vis_a = np.array(vis_a)[selected_neurons].tolist()
    vest_a = np.array(vest_a)[selected_neurons].tolist()
    RawR = np.array(RawR)[selected_neurons]
    RawR_trial = np.array(RawR_trial)[selected_neurons]
    S_vest_m = S_vest_m
    S_vis_m = S_vis_m
    current_vect = np.array(read())

    path = '../resultConsiderCoherence/all/'
    for i in os.listdir(path):
        path_file = os.path.join(path, i)
        if os.path.isfile(path_file):
            os.remove(path_file)
    rs = calculate_R(current_vect)
    for i in range(len(selected_neurons)):
        path = '../resultConsiderCoherence/all/' + str(i) + '.xls'
        data_write(path, rs[i])
예제 #4
0
def init():
    files_para = '../data/ForNorData_MSTd/'
    files_r = '../data/AllData_MSTd/AllData/'
    both_azimuth_files = os.listdir(files_para)
    r_files = os.listdir(files_r)

    VEST = list(range(0, 360 + 45, 45))  # H: vest azimuth
    VIS = list(range(0, 360 + 45, 45))  # H: vis azimuth

    global vest_a, vis_a, vest_e, vis_e, n1, n2, n3, e, c_vest, c_vis, n0, \
        S_vest_m, S_vis_m,RawR_trial,RawR
    vest_a = []  # vest azimuth in H_hat
    vis_a = []  # vis azimuth in H_hat
    RawR = [
    ]  # firing curve, mean of neuron responses under different stimulus conditions, 就是我们最后要fit的y值(平均数)
    RawR_trial = [
    ]  # neuron responses under different stimulus conditions, 就是我们最后要fit的y值(实际trial的y值)
    vest_resp = []  # 不需要用到,只是在算Rmax的时候用了
    vis_resp = []
    collection = CoherenceSelect.CoherenceSelect()
    for f in both_azimuth_files:
        if f[:-18] in collection:
            temp_file = format_matfile(files_para + f, 'ForNorData')
            vest_a.append(temp_file['vest_Direc'])
            vis_a.append(temp_file['vis_Direc'])

            temp_file = format_matfile(
                files_r + 'SmoothData_' + f[:-15] + '.mat',
                'CueConflictDataSmooth')
            RawR_trial.append(temp_file['resp_trial_conflict'])
            vis_resp.append(temp_file['resp_vis'])
            vest_resp.append(temp_file['resp_ves'])
    SIZE = len(vest_a)
    vest_a = [i + 360 if i < 0 else i for i in np.ravel(vest_a)]
    vis_a = [i + 360 if i < 0 else i for i in np.ravel(vis_a)]

    vest_e = np.zeros(len(vest_a))
    vis_e = np.zeros(len(vis_a))

    vest_stim_ele = 0
    vis_stim_ele = 0

    n1 = n2 = n3 = e = c_vis = 1
    c_vest = 0
    n0 = 2

    global Rmax_m
    Rmax_m = []
    S_vest_m = np.zeros((SIZE, 9, 9))
    S_vis_m = np.zeros((SIZE, 9, 9))
    for k in range(len(VEST)):
        for j in range(len(VIS)):
            vest_stim_az = VEST[k]
            vis_stim_az = VIS[j]
            S_vest = spherical_sinusoid(vest_stim_az, vest_a, vest_stim_ele,
                                        vest_e, c_vest, n0)
            # print(np.array(S_vest).shape)
            S_vest_m[:, k, j] = S_vest
            S_vis = spherical_sinusoid(vis_stim_az, vis_a, vis_stim_ele, vis_e,
                                       c_vis, n0)
            S_vis_m[:, k, j] = S_vis

    # S_vis_m_temp=[]
    # S_vest_m_temp=[]
    # for i in range(200):
    #     S_vest_m_temp.append([])
    #     S_vis_m_temp.append([])
    #     S_vis_m_temp[i].append(S_vis_m[i][0])
    #     S_vest_m_temp[i].append(S_vest_m[i][0])
    # print(np.array(S_vis_m_temp).shape)
    # print(np.array(S_vest_m_temp).shape)

    Rmax1 = []
    # Rmax2 = []
    for k in range(SIZE):
        Rmax1.append([])
        for i in range(len(VIS)):
            Max = np.array(vis_resp)[k, 0, i]
            for j in range(len(VEST)):
                Max = max(Max, np.array(vest_resp)[k, j, 0])
            Rmax1[k].append(Max)
    # print(np.array(Rmax1).shape)
    # for k in range(200):
    #     Rmax2.append([])
    #     for i in range(len(VIS)):
    #         Max = np.array(vest_resp)[k, i, 0]
    #         for j in range(len(VEST)):
    #             Max = max(Max, np.array(vis_resp)[k, 0, j])
    #         Rmax2[k].append(Max)
    # print(np.array(Rmax2).shape)
    for k in range(len(Rmax1)):
        Rmax_m.append([])
        Rmax_m[k].append(Rmax1[k])
        # Rmax_m[k].append(Rmax2[k])
    # print(np.array(Rmax_m).shape)
    #select_good
    # corr_list = [(np.triu(np.corrcoef([i.ravel() for i in RawR_trial[n]])).sum() - RawR_trial[n].shape[0]) / comb(
    #     RawR_trial[n].shape[0], 2) for n in range(200)]
    # global selected_neurons
    # selected_neurons = [i for i in range(200) if
    #                     corr_list[i] > 0.4]  # select good neurons: correlation between trials over 0.4
    global selected_neurons
    selected_neurons = list(range(SIZE))
    # S_vest_m=S_vest_m[selected_neurons]
    # S_vis_m = S_vis_m[selected_neurons]
    # # print("S_vest_m:",S_vest_m.shape)
    # # print("S_vis_m:", S_vest_m.shape)
    # # init_vect = init_vect[:, selected_neurons]
    # vis_a = np.array(vis_a)[selected_neurons].tolist()
    # vest_a = np.array(vest_a)[selected_neurons].tolist()
    vest_resp
    S_vest_m_temp = []
    for i in range(len(selected_neurons)):
        S_vest_m_temp.append([])

    corr_list = [(np.triu(np.corrcoef([i.ravel()
                                       for i in RawR_trial[n]])).sum() -
                  RawR_trial[n].shape[0]) / comb(RawR_trial[n].shape[0], 2)
                 for n in range(SIZE)]
    selected_neurons = [
        i for i in range(SIZE) if corr_list[i] > 0.5
    ]  # select good neurons: correlation between trials over 0.4
    S_vest_m = S_vest_m[selected_neurons]
    S_vis_m = S_vis_m[selected_neurons]
    Rmax_m = np.array(Rmax_m)[selected_neurons]
    Rmax_m = Rmax_m.tolist()
    # init_vect = init_vect[:, selected_neurons]
    vis_a = np.array(vis_a)[selected_neurons].tolist()
    vest_a = np.array(vest_a)[selected_neurons].tolist()
    # RawR = np.array(RawR)[selected_neurons]
    RawR_trial = np.array(RawR_trial)[selected_neurons]
    S_vest_m = S_vest_m
    S_vis_m = S_vis_m

    path = '../resultConsiderCoherence/vis/'
    for i in os.listdir(path):
        path_file = os.path.join(path, i)
        if os.path.isfile(path_file):
            os.remove(path_file)

    current_vect = np.array(read())

    #计算c1=c1!=0的rs
    rs = calculate_R(current_vect)
    for i in range(len(selected_neurons)):
        path = '../resultConsiderCoherence/vis/' + str(i) + '.xls'
        data_write(path, rs[i][0])
예제 #5
0
def init():
    files_para = '../data/ForNorData_MSTd/'
    files_r = '../data/AllData_MSTd/AllData/'
    # files_r = 'C:/Users/76774/Desktop/my_RSA/Monkey/data/OriginalData_MST'
    both_azimuth_files = os.listdir(files_para)
    r_files = os.listdir(files_r)

    VEST = list(range(0, 360 + 45, 45))  # H: vest azimuth
    VIS = list(range(0, 360 + 45, 45))  # H: vis azimuth

    # H_hat: vest azimuth load, vis azimuth load, vest elevation all zero, vis elevation all zero,
    # H: vest azimuth in [0,360,45] range, vis azimuth in [0,360,45] range,
    # vest elevation all zero, vis elevation all zero,
    global vest_a, vis_a, vest_e, vis_e, n1, n2, n3, e, c_vest, c_vis, n0, \
        S_vest_m, S_vis_m,RawR_trial,RawR
    vest_a = []  # vest azimuth in H_hat
    vis_a = []  # vis azimuth in H_hat
    RawR = [
    ]  # firing curve, mean of neuron responses under different stimulus conditions, 就是我们最后要fit的y值(平均数)
    RawR_trial = [
    ]  # neuron responses under different stimulus conditions, 就是我们最后要fit的y值(实际trial的y值)
    vest_resp = []  # 不需要用到,只是在算Rmax的时候用了
    vis_resp = []
    index = []
    collection = CoherenceSelect.CoherenceSelect()
    # print(collection)
    Name = []
    for f in both_azimuth_files:
        print(f[:-15])
        if f[:-18] in collection:
            temp_file = format_matfile(files_para + f, 'ForNorData')
            vest_a.append(temp_file['vest_Direc'])
            vis_a.append(temp_file['vis_Direc'])
            index.append(files_r + f[:-15])
            temp_file = format_matfile(
                files_r + 'SmoothData_' + f[:-15] + '.mat',
                'CueConflictDataSmooth')
            # print(files_r +'SmoothData_'+ f[:-15] + '.mat')
            RawR_trial.append(temp_file['resp_trial_conflict'])
            RawR.append(temp_file['resp_conflict'])
            vest_resp.append(temp_file['resp_ves'])
            vis_resp.append(temp_file['resp_vis'])
            Name.append(f[:-18])
    SIZE = len(vis_resp)
    vest_a = [i + 360 if i < 0 else i for i in np.ravel(vest_a)]
    vis_a = [i + 360 if i < 0 else i for i in np.ravel(vis_a)]

    vest_e = np.zeros(len(vest_a))
    vis_e = np.zeros(len(vis_a))

    vest_stim_ele = 0
    vis_stim_ele = 0

    n1 = n2 = n3 = e = c_vest = c_vis = 1
    n0 = 2

    global Rmax_m
    Rmax_m = np.zeros((SIZE, 9, 9))
    S_vest_m = np.zeros((SIZE, 9, 9))
    S_vis_m = np.zeros((SIZE, 9, 9))

    for k in range(len(VEST)):
        for j in range(len(VIS)):
            vest_stim_az = VEST[k]
            vis_stim_az = VIS[j]
            Rmax = np.maximum(np.ravel(np.array(vest_resp)[:, k]),
                              np.ravel(np.array(vis_resp)[:, :, j]))
            Rmax_m[:, k, j] = Rmax
            S_vest = spherical_sinusoid(vest_stim_az, vest_a, vest_stim_ele,
                                        vest_e, c_vest, n0)
            S_vest_m[:, k, j] = S_vest
            S_vis = spherical_sinusoid(vis_stim_az, vis_a, vis_stim_ele, vis_e,
                                       c_vis, n0)
            S_vis_m[:, k, j] = S_vis
    global selected_neurons
    corr_list = [(np.triu(np.corrcoef([i.ravel()
                                       for i in RawR_trial[n]])).sum() -
                  RawR_trial[n].shape[0]) / comb(RawR_trial[n].shape[0], 2)
                 for n in range(SIZE)]
    selected_neurons = [
        i for i in range(SIZE) if corr_list[i] > 0.5
    ]  # select good neurons: correlation between trials over 0.4
    Name = np.array(Name)[selected_neurons].tolist()
    f = open('../resultConsiderCoherence/neuronsNumber.txt', 'w')
    for i, name in enumerate(Name):
        if i != len(Name) - 1:
            line = name + '\t' + str(i) + '\n'
        else:
            line = name + '\t' + str(i)
        print(line)
        f.write(line)
    f.close()