def One_Key_Frame_Graphs(
        data_folder,
        sub_dic,
        show_clip=3,
        alinged_sub_folder=r'\Results\Final_Aligned_Frames',
        Stim_Align_sub_folder=r'\Results\Stim_Frame_Align.pkl'):
    result_folder = data_folder + r'\Results'
    graph_save_folder = result_folder + r'\Only_Frame_SubGraphs'
    OS_Tools.mkdir(result_folder)
    OS_Tools.mkdir(graph_save_folder)
    stim_path = data_folder + Stim_Align_sub_folder
    stim_dic = OS_Tools.Load_Variable(stim_path)
    all_tif_name = OS_Tools.Get_File_Name(data_folder + alinged_sub_folder)
    graph_num = len(sub_dic)
    all_sub_graph_names = list(sub_dic.keys())
    for i in range(graph_num):
        current_name = all_sub_graph_names[i]
        current_a = Frame_ID_Extractor(stim_dic, sub_dic[current_name][0])
        current_b = Frame_ID_Extractor(stim_dic, sub_dic[current_name][1])
        current_sub_graph, current_t_graph, current_info_dic = Single_Subgraph_Generator(
            all_tif_name, current_a, current_b)
        current_sub_graph = Graph_Tools.Clip_And_Normalize(
            current_sub_graph, show_clip)
        current_t_graph = Graph_Tools.Clip_And_Normalize(
            current_t_graph, show_clip)
        # Save graphs
        Graph_Tools.Show_Graph(current_sub_graph, current_name + '_Sub_Graph',
                               graph_save_folder)
        Graph_Tools.Show_Graph(current_t_graph, current_name + '_t_Graph',
                               graph_save_folder)
        OS_Tools.Save_Variable(graph_save_folder, current_name + r'_Sub_Info',
                               current_info_dic, '.info')
    return True
def One_Key_Stim_Maps(data_folder,
                      cell_folder,
                      sub_dic,
                      have_blank=None,
                      alinged_sub_folder=r'\Results\Aligned_Frames',
                      Stim_Align_sub_folder=r'\Results\Stim_Frame_Align.pkl'):
    '''
    1 key generate stim map. Befor using this, you need to :
        1.align graphs
        2.give cell file path.
        3.Finish stim frame align.
    '''
    result_folder = data_folder + r'\Results'
    stim_path = data_folder + Stim_Align_sub_folder
    cell_path = OS_Tools.Get_File_Name(cell_folder, '.cell')[0]
    cell_dic = OS_Tools.Load_Variable(cell_path)
    # Then generate spiketrain
    stim_train = OS_Tools.Load_Variable(stim_path)['Original_Stim_Train']
    all_tif_name = OS_Tools.Get_File_Name(data_folder + alinged_sub_folder)
    cell_information = cell_dic['All_Cell_Information']
    if have_blank != None:
        warnings.warn(
            'Have blank is detected automatically, this API is useless now.',
            FutureWarning)
    have_blank = (0 in stim_train)
    if have_blank == True:
        F_train, dF_F_train = Spike_Train_Generator(all_tif_name,
                                                    cell_information,
                                                    Base_F_type='nearest_0',
                                                    stim_train=stim_train)
    else:
        print('No blank, use previous ISI to calculate trains')
        F_train, dF_F_train = Spike_Train_Generator(all_tif_name,
                                                    cell_information,
                                                    Base_F_type='before_ISI',
                                                    stim_train=stim_train)
    # Then save F and dF/F trains
    OS_Tools.Save_Variable(result_folder, 'F_Trains', F_train)
    OS_Tools.Save_Variable(result_folder, 'dF_F_Trains', dF_F_train)
    # At last, calculate Maps.
    Standard_Stim_Processor(data_folder,
                            stim_path,
                            sub_dic,
                            cell_method=cell_path,
                            spike_train_path=result_folder +
                            r'\dF_F_Trains.pkl')
Esempio n. 3
0
def Cell_Find(run_folder):
    output_folder = run_folder+r'\Results'
    aligned_frame_folder = output_folder+r'\Aligned_Frames'
    all_tif_name = OS_Tools.Get_File_Name(aligned_frame_folder)
    Stim_Frame_Dic = OS_Tools.Load_Variable(output_folder,'Stim_Frame_Align.pkl')
    on_off_graph,Finded_Cells = On_Off_Cell_Finder(all_tif_name, Stim_Frame_Dic,shape_boulder=[20,20,20,35],filter_method = 'Gaussian',LP_Para = ((5,5),1.5))
    cell_folder = output_folder+r'\Cells'
    OS_Tools.Save_Variable(cell_folder, 'Finded_Cells', Finded_Cells,'.cell')
    Graph_tools.Show_Graph(on_off_graph, 'on-off_graph', cell_folder)
    all_keys = list(Finded_Cells.keys())
    all_keys.remove('All_Cell_Information')
    for i in range(len(all_keys)):
        Graph_tools.Show_Graph(Finded_Cells[all_keys[i]], all_keys[i], cell_folder)
    return True
Esempio n. 4
0
def Intensity_Selector(data_folder,
                       graph_type='.tif',
                       mode='biggest',
                       propotion=0.05,
                       list_write=True):
    '''
    Select frames have biggest or smallest a.i., and generate average graphs.

    Parameters
    ----------
    data_folder : (str)
        Data folder.
    graph_type : (str), optional
        Data type of . The default is '.tif'.
    mode : ('biggest' or 'smallest'), optional
        Type of frame selection. The default is 'biggest'.
    propotion : (float), optional
        Propotion of graph selection. The default is 0.05.
    list_write : (bool), optional
        Whether we write down graph intensity data. The default is True.

    Returns
    -------
    averaged_graph : (2D Array)
        Averaged graph of selected frames.
    selected_graph_name : (ND List)
        List of selected graph names.

    '''
    all_graph_name = np.array(
        OS_Tools.Get_File_Name(data_folder, file_type=graph_type))
    graph_Num = len(all_graph_name)
    bright_data = np.zeros(graph_Num, dtype='f8')
    for i in range(graph_Num):
        current_graph = cv2.imread(all_graph_name[i], -1)
        bright_data[i] = np.mean(current_graph)
        # write bright data if required.
    if list_write == True:
        OS_Tools.Save_Variable(data_folder, 'brightness_info', bright_data)
    # Then select given mode frames.
    used_graph_num = int(graph_Num * propotion)
    if mode == 'biggest':
        used_graph_id = np.argpartition(bright_data,
                                        -used_graph_num)[-used_graph_num:]
    elif mode == 'smallest':
        used_graph_id = np.argpartition(bright_data,
                                        used_graph_num)[0:used_graph_num]
    selected_graph_name = all_graph_name[used_graph_id]
    averaged_graph = Graph_Tools.Average_From_File(selected_graph_name)
    return averaged_graph, selected_graph_name
def Cell_Find_And_Plot(
        graph_folder,
        graph_name,
        Cell_Label,
        find_thres = 2.5,
        max_pix = 1000,
        min_pix = 20,
        shape_boulder = [20,20,20,20], 
        sharp_gauss = ([7,7],1.5),
        back_gauss = ([15,15],7),
        size_limit = 20    
        ):
    """
    Cell find from file.

    Parameters
    ----------
    graph_folder : (str)
        Graph folder.
    graph_name : (str)
        Graph name. Extend name shall be contained.
    Cell_Label : (str)
        Save sub Folder. Cell data and cell graphs will be saved in this sub folder.
    find_thres,max_pix,min_pix,shape_boulder,sharp_gauss,back_gauss,size_limit : As Function 1, optional
        As Function 1.

    Returns
    -------
    Cell_Finded : TYPE
        DESCRIPTION.

    """
    Base_Graph = cv2.imread(graph_folder + r'\\' + graph_name,-1)
    graph_save_folder = graph_folder + r'\\' + Cell_Label
    Finded_Cells = Cell_Find_From_Graph(Base_Graph,find_thres,max_pix,min_pix,shape_boulder,sharp_gauss,back_gauss,size_limit)
    OS_Tools.Save_Variable(graph_save_folder,Cell_Label,Finded_Cells,extend_name = '.cell')
    all_keys = list(Finded_Cells.keys())
    all_keys.remove('All_Cell_Information')
    for i in range(len(all_keys)):
        Graph_Tools.Show_Graph(Finded_Cells[all_keys[i]],graph_name = all_keys[i],save_path = graph_save_folder,show_time = 2000,write = True)
    return Finded_Cells
Esempio n. 6
0
def Tremble_Comparision(before_folder,
                        after_folder,
                        boulder_ignore=20,
                        cut_shape=(9, 9),
                        mask_thres=0):
    # Initialization
    save_folder = after_folder + r'\Results'
    OS_Tools.mkdir(save_folder)
    save_folder = save_folder + r'\Tremble_Compare'
    OS_Tools.mkdir(save_folder)
    row_num = cut_shape[0]
    col_num = cut_shape[1]
    frac_num = row_num * col_num
    cov_matrix_dic = {}
    var_matrix_dic = {}
    variation = np.zeros(shape=(row_num, col_num, 2), dtype='f8')
    variation_change = np.zeros(shape=(row_num, col_num), dtype='f8')
    variation_prop = np.zeros(shape=(row_num, col_num), dtype='f8')
    # Calculation Begins
    before_schematic, before_frac_center = Tremble_Evaluator(
        before_folder,
        boulder_ignore=boulder_ignore,
        cut_shape=cut_shape,
        mask_thres=mask_thres)
    after_schematic, after_frac_center = Tremble_Evaluator(
        after_folder,
        boulder_ignore=boulder_ignore,
        cut_shape=cut_shape,
        mask_thres=mask_thres)
    fig, ax = plt.subplots(row_num, col_num,
                           figsize=(30, 28))  # Initialize graphs
    fig.suptitle('Mass Center Distribution', fontsize=54)
    # Cycle all fracture,get scatter map and variance
    for i in range(frac_num):
        # Graph_Plot
        current_row = i % row_num
        current_col = i // row_num
        ax[current_row, current_col].scatter(before_frac_center[i, :, 1],
                                             before_frac_center[i, :, 0],
                                             s=1,
                                             c='r')
        ax[current_row, current_col].scatter(after_frac_center[i, :, 1],
                                             after_frac_center[i, :, 0],
                                             s=1,
                                             c='g')
        # After plot, calculate cov matrix and variance.
        before_cov = np.cov(before_frac_center[i, :, :].T)
        after_cov = np.cov(after_frac_center[i, :, :].T)
        cov_matrix_dic[i] = (before_cov, after_cov)
        before_eig, _ = np.linalg.eig(before_cov)
        after_eig, _ = np.linalg.eig(after_cov)
        before_var = np.round(before_eig.sum(), 4)
        after_var = np.round(after_eig.sum(), 4)
        variation[current_row, current_col, 0] = before_var
        variation[current_row, current_col, 1] = after_var
        variation_change[current_row, current_col] = before_var - after_var
        variation_prop[current_row,
                       current_col] = (before_var - after_var) / before_var
        # Text annotate
        anchored_text = AnchoredText('Before variance:' + str(before_var) +
                                     '\n After variance:' + str(after_var),
                                     loc='lower left')
        ax[current_row, current_col].add_artist(anchored_text)

    # After this, save figures and matrixs.
    var_matrix_dic['Before'] = variation[:, :, 0]
    var_matrix_dic['After'] = variation[:, :, 1]
    Graph_Tools.Show_Graph(before_schematic, 'Before_Schematic', save_folder)
    Graph_Tools.Show_Graph(after_schematic, 'After_Schematic', save_folder)
    fig.savefig(save_folder + '\Scatter Plots.png', dpi=330)
    OS_Tools.Save_Variable(save_folder, 'cov_matrix', cov_matrix_dic)
    OS_Tools.Save_Variable(save_folder, 'variance_matrix', var_matrix_dic)
    # Calculate variance change and plot variance map.
    # Before variance map
    plt.clf()
    fig2 = plt.figure(figsize=(15, 15))
    plt.title('Before Align Variance', fontsize=36)
    fig2 = sns.heatmap(variation[:, :, 0],
                       cmap='bwr',
                       annot=True,
                       annot_kws={"size": 20},
                       square=True,
                       yticklabels=False,
                       xticklabels=False,
                       center=0)
    fig2.figure.savefig(save_folder + '\Before_Variance.png', dpi=330)
    # After variance map
    plt.clf()
    fig2 = plt.figure(figsize=(15, 15))
    plt.title('After Align Variance', fontsize=36)
    fig2 = sns.heatmap(variation[:, :, 1],
                       cmap='bwr',
                       annot=True,
                       annot_kws={"size": 20},
                       square=True,
                       yticklabels=False,
                       xticklabels=False,
                       center=0)
    fig2.figure.savefig(save_folder + '\After_Variance.png', dpi=330)
    # Variance change map
    plt.clf()
    fig2 = plt.figure(figsize=(15, 15))
    plt.title('Variance Change', fontsize=36)
    fig2 = sns.heatmap(variation_change,
                       cmap='bwr',
                       annot=True,
                       annot_kws={"size": 20},
                       square=True,
                       yticklabels=False,
                       xticklabels=False,
                       center=0)
    fig2.figure.savefig(save_folder + '\Variance_Change.png', dpi=330)
    # Variance change propotion map
    plt.clf()
    fig2 = plt.figure(figsize=(15, 15))
    plt.title('Variance Change Propotion', fontsize=36)
    fig2 = sns.heatmap(variation_prop,
                       cmap='bwr',
                       annot=True,
                       annot_kws={"size": 20},
                       square=True,
                       yticklabels=False,
                       xticklabels=False,
                       center=0)
    fig2.figure.savefig(save_folder + '\Variance_Change_Prop.png', dpi=330)
    return cov_matrix_dic, var_matrix_dic
Esempio n. 7
0
base_graph = cv2.imread(r'E:\Test_Data\2P\210101_L76_2P\1-002\Results\Global_Average_After_Align.tif',-1)
Translation_Alignment(after_spons,base_mode = 'input',input_base = base_graph,graph_shape = (376,352))
#%% Then analyze full frame stim maps.
basic_stim_folders = [r'I:\Test_Data\2P\210101_L76_2P\1-014',
                      r'I:\Test_Data\2P\210101_L76_2P\1-016',
                      r'I:\Test_Data\2P\210101_L76_2P\1-017'
                      ]
Translation_Alignment(basic_stim_folders,base_mode = 0)
#Get stim frame aligns
all_stim_folders = [r'I:\Test_Data\2P\210101_L76_2P\210101_L76_2P_stimuli\Run14_2P_OD8_auto',
                    r'I:\Test_Data\2P\210101_L76_2P\210101_L76_2P_stimuli\Run16_2P_G8',
                    r'I:\Test_Data\2P\210101_L76_2P\210101_L76_2P_stimuli\Run17_2P_RGLum4'
                    ]
for i in range(3):
    _,Stim_Frame_Dic = Stim_Frame_Align(all_stim_folders[i])
    OS_Tools.Save_Variable(all_stim_folders[i], 'Stim_Frame_Align', Stim_Frame_Dic)
#%% Then cell find for all stim maps.
from My_Wheels.Cell_Find_From_Graph import Cell_Find_And_Plot
Cell_Find_And_Plot(r'I:\Test_Data\2P\210101_L76_2P\1-014\Results','Global_Average_After_Align.tif','Stim_Global',find_thres = 1.5) 
cell_folder = r'I:\Test_Data\2P\210101_L76_2P\1-014\Results\Stim_Global'
#%%Then calculate all stim graphs.
from My_Wheels.Standard_Parameters.Sub_Graph_Dics import Sub_Dic_Generator
from My_Wheels.Standard_Stim_Processor import One_Key_Stim_Maps
OD_para = Sub_Dic_Generator('OD_2P')
One_Key_Stim_Maps(r'I:\Test_Data\2P\210101_L76_2P\1-014', cell_folder, OD_para)
# Then G8
G8_para = Sub_Dic_Generator('G8+90')
One_Key_Stim_Maps(r'I:\Test_Data\2P\210101_L76_2P\1-016', cell_folder, G8_para)
# Then RGLum4 
RG_para = Sub_Dic_Generator('RGLum4')
One_Key_Stim_Maps(r'I:\Test_Data\2P\210101_L76_2P\1-017', cell_folder, RG_para)
Esempio n. 8
0
Translation_Alignment(all_folders,base_mode=1,align_range=35,align_boulder=35)
'''Attention here,1-012 and 1-013 have more movement than 20pix, making this hard to use.'''

#%% Align Stim and frame of each stim folder.
from My_Wheels.Stim_Frame_Align import Stim_Frame_Align
all_stim_folders = [
    r'G:\Test_Data\2P\201111_L76_LM\201111_L76_2P_stimuli\Run02_2P_G8',
    r'G:\Test_Data\2P\201111_L76_LM\201111_L76_2P_stimuli\Run03_2P_manual_OD8',
    r'G:\Test_Data\2P\201111_L76_LM\201111_L76_2P_stimuli\Run07_2P_RGLum4',
    r'G:\Test_Data\2P\201111_L76_LM\201111_L76_2P_stimuli\Run08_2P_RGLum4_RG',
    r'G:\Test_Data\2P\201111_L76_LM\201111_L76_2P_stimuli\Run09_2P_RGLum4',
    ]
for i in range(len(all_stim_folders)):
    current_stim_folder = all_stim_folders[i]
    _,current_Stim_Frame_Align = Stim_Frame_Align(current_stim_folder)
    OS_Tools.Save_Variable(current_stim_folder, 'Stim_Frame_Align', current_Stim_Frame_Align)
#%% Cell Find.
from My_Wheels.Cell_Find_From_Graph import On_Off_Cell_Finder
import My_Wheels.Graph_Operation_Kit as Graph_tools
def Cell_Find(run_folder):
    output_folder = run_folder+r'\Results'
    aligned_frame_folder = output_folder+r'\Aligned_Frames'
    all_tif_name = OS_Tools.Get_File_Name(aligned_frame_folder)
    Stim_Frame_Dic = OS_Tools.Load_Variable(output_folder,'Stim_Frame_Align.pkl')
    on_off_graph,Finded_Cells = On_Off_Cell_Finder(all_tif_name, Stim_Frame_Dic,shape_boulder=[20,20,20,35],filter_method = 'Gaussian',LP_Para = ((5,5),1.5))
    cell_folder = output_folder+r'\Cells'
    OS_Tools.Save_Variable(cell_folder, 'Finded_Cells', Finded_Cells,'.cell')
    Graph_tools.Show_Graph(on_off_graph, 'on-off_graph', cell_folder)
    all_keys = list(Finded_Cells.keys())
    all_keys.remove('All_Cell_Information')
    for i in range(len(all_keys)):
Esempio n. 9
0
def Affine_Aligner_Gaussian(data_folder,
                            base_graph,
                            window_size=1,
                            max_point=50000,
                            good_match_prop=0.3,
                            dist_lim=120,
                            match_checker=1,
                            sector_num=4,
                            write_file=False,
                            save_folder='Default'):
    if save_folder == 'Default':
        save_folder = data_folder + r'\Results'
    aligned_tif_folder = save_folder + r'\Affined_Frames'
    OS_Tools.mkdir(save_folder)
    OS_Tools.mkdir(aligned_tif_folder)

    all_tif_name = OS_Tools.Get_File_Name(data_folder)
    graph_num = len(all_tif_name)
    graph_shape = cv2.imread(all_tif_name[0], -1).shape
    height, width = graph_shape
    origin_tif_matrix = np.zeros(shape=graph_shape + (graph_num, ), dtype='u2')
    # Read in all tif name.
    for i in range(graph_num):
        origin_tif_matrix[:, :, i] = cv2.imread(all_tif_name[i], -1)
    # Then get window slipped average graph.
    if window_size == 1:
        slipped_average_matrix = origin_tif_matrix
    else:
        slipped_average_matrix = Filters.Window_Average(
            origin_tif_matrix, window_size=window_size)
    # Use slip average to get deformation parameters.
    aligned_tif_matrix = np.zeros(shape=origin_tif_matrix.shape, dtype='u2')
    h_dic = {}  # Deformation parameters
    for i in range(graph_num):
        target = slipped_average_matrix[:, :, i]
        _, current_h = Affine_Core_Point_Equal(target,
                                               base_graph,
                                               max_point=max_point,
                                               good_match_prop=good_match_prop,
                                               sector_num=sector_num,
                                               dist_lim=dist_lim,
                                               match_checker=match_checker)
        h_dic[i] = current_h
        current_deformed_graph = cv2.warpPerspective(
            origin_tif_matrix[:, :, i], current_h, (width, height))
        Graph_Tools.Show_Graph(current_deformed_graph,
                               all_tif_name[i].split('\\')[-1],
                               aligned_tif_folder,
                               show_time=0,
                               graph_formation='')
        aligned_tif_matrix[:, :, i] = current_deformed_graph
    OS_Tools.Save_Variable(save_folder, 'Deform_H', h_dic)
    if write_file == True:
        OS_Tools.Save_Variable(save_folder, 'Affine_Aligned_Graphs',
                               aligned_tif_matrix)
    # At last, generate average graphs
    graph_before_align = origin_tif_matrix.mean(axis=2).astype('u2')
    graph_after_align = aligned_tif_matrix.mean(axis=2).astype('u2')
    graph_before_align = Graph_Tools.Clip_And_Normalize(graph_before_align,
                                                        clip_std=5)
    graph_after_align = Graph_Tools.Clip_And_Normalize(graph_after_align,
                                                       clip_std=5)
    Graph_Tools.Show_Graph(graph_before_align, 'Graph_Before_Affine',
                           save_folder)
    Graph_Tools.Show_Graph(graph_after_align, 'Graph_After_Affine',
                           save_folder)
    return True
Esempio n. 10
0
                      base_mode='input',
                      input_base=base,
                      graph_shape=(325, 324))
#%% Then calculate stim frame align data.
from My_Wheels.Stim_Frame_Align import Stim_Frame_Align
all_stim_folder = [
    r'I:\Test_Data\2P\201222_L76_2P\201222_L76_2P_stimuli\Run08_2P_OD8_auto',
    r'I:\Test_Data\2P\201222_L76_2P\201222_L76_2P_stimuli\Run10_2P_G8',
    r'I:\Test_Data\2P\201222_L76_2P\201222_L76_2P_stimuli\Run11_2P_RGLum4'
]
for i in range(3):
    current_stim_folder = all_stim_folder[i]
    _, stimdic = Stim_Frame_Align(current_stim_folder,
                                  jmp_step=1500,
                                  head_extend=-1)  # for T pre
    OS_Tools.Save_Variable(current_stim_folder, 'Stim_Frame_Align', stimdic)
#%% Use global morpho as base to find cell.
from My_Wheels.Cell_Find_From_Graph import Cell_Find_And_Plot
global_cell = Cell_Find_And_Plot(
    r'E:\Test_Data\2P\201222_L76_2P\1-008\Results',
    'Global_Average_After_Align.tif', 'Global_Morpho', 1)
cell_folder = r'E:\Test_Data\2P\201222_L76_2P\1-008\Results\Global_Morpho'
#%% For Run08
from My_Wheels.Standard_Stim_Processor import One_Key_Stim_Maps
from My_Wheels.Standard_Parameters.Sub_Graph_Dics import Sub_Dic_Generator
# Calculate Run08 Cells.
OD_Folder = r'E:\Test_Data\2P\201222_L76_2P\1-008'
OD_sub_dic = Sub_Dic_Generator('OD_2P')
One_Key_Stim_Maps(OD_Folder, cell_folder, OD_sub_dic)
#%% Then Run 10 G8
G8_Folder = r'E:\Test_Data\2P\201222_L76_2P\1-010'
Esempio n. 11
0
    ]
all_run_folder = List_Tools.List_Annex(data_folder, run_folder)
Align.Translation_Alignment(all_run_folder,base_mode = 1,align_range=50,align_boulder=50,big_memory_mode=True)
#%% Then find cell from after align spon graph.
from My_Wheels.Cell_Find_From_Graph import Cell_Find_And_Plot
Cell_Find_And_Plot(r'G:\Test_Data\2P\201211_L76_2P\1-001\Results', 'Global_Average_After_Align.tif', 'Global_Morpho',find_thres= 1.5,shape_boulder = [20,20,30,20])
#%% Then calculate the stim train of each stim series.
from My_Wheels.Stim_Frame_Align import Stim_Frame_Align
all_stim_folder = [
    r'G:\Test_Data\2P\201211_L76_2P\201211_L76_2P_stimuli\Run10_2P_G8',
    r'G:\Test_Data\2P\201211_L76_2P\201211_L76_2P_stimuli\Run12_2P_OD8_auto',
    r'G:\Test_Data\2P\201211_L76_2P\201211_L76_2P_stimuli\Run14_2P_RGLum4',
    ]
for i in range(3):
    _,current_stim_dic = Stim_Frame_Align(all_stim_folder[i])
    OS_Tools.Save_Variable(all_stim_folder[i], 'Stim_Frame_Align', current_stim_dic)
#%% Then calculate spike train of different runs.
from My_Wheels.Spike_Train_Generator import Spike_Train_Generator
#Cycle basic stim map. this maps have 
for i,index in enumerate([1,2,4]):
    current_aligned_tif_name  = OS_Tools.Get_File_Name(all_run_folder[index]+r'\Results\Aligned_Frames')
    current_stim = OS_Tools.Load_Variable(all_stim_folder[i],file_name='Stim_Frame_Align.pkl')['Original_Stim_Train']
    current_cell_info = OS_Tools.Load_Variable(all_run_folder[index]+r'\Results\Global_Morpho\Global_Morpho.cell')['All_Cell_Information']
    F_train,dF_F_train = Spike_Train_Generator(current_aligned_tif_name, current_cell_info,Base_F_type= 'nearest_0',stim_train = current_stim)
    OS_Tools.Save_Variable(all_run_folder[index]+r'\Results', 'F_train', F_train)
    OS_Tools.Save_Variable(all_run_folder[index]+r'\Results', 'dF_F_train', dF_F_train)

#%% Then calculate standard stim map.
from My_Wheels.Standard_Stim_Processor import Standard_Stim_Processor
from My_Wheels.Standard_Parameters.Sub_Graph_Dics import Sub_Dic_Generator
Standard_Stim_Processor(r'G:\Test_Data\2P\201211_L76_2P\1-010',
Esempio n. 12
0
# -*- coding: utf-8 -*-
"""
Created on Tue May 18 14:55:56 2021

@author: ZR
"""

from My_Wheels.Cell_Processor import Cell_Processor
from My_Wheels.Standard_Parameters.Stim_Name_Tools import Stim_ID_Combiner
import My_Wheels.OS_Tools_Kit as ot

CP = Cell_Processor(r'K:\Test_Data\2P\210514_L76_2P')
All_Black_Cells = CP.Black_Cell_Identifier(
    ['Run013', 'Run014', 'Run016', 'Run017'])
ot.Save_Variable(r'K:\Test_Data\2P\210514_L76_2P', '_All_Black',
                 All_Black_Cells)
H7O4_Para = Stim_ID_Combiner(
    'HueNOrien4_SC',
    {'Hue': ['Red', 'Yellow', 'Green', 'Gyan', 'Blue', 'Purple', 'White']})
CP.Cell_Response_Maps('Run017', H7O4_Para, subshape=(6, 7), figsize=(25, 25))
G16_Para = Stim_ID_Combiner('G16_Dirs')
CP.Cell_Response_Maps('Run016', G16_Para, subshape=(3, 8))
OD_Para = Stim_ID_Combiner('OD_2P')
CP.Cell_Response_Maps('Run014', OD_Para, subshape=(3, 5))
S3D8_Para = Stim_ID_Combiner('Shape3Dir8_Single')
CP.Cell_Response_Maps('Run013', S3D8_Para, subshape=(4, 8))

#%% Black Cell Location
for i in range(CP.cell_num):
    CP.Single_Cell_Plotter(CP.all_cell_names[i], show_time=0)
black_cell_num = len(All_Black_Cells)
def Cell_Find_From_Mannual(mask_graph_path,average_graph_path = None,boulder = 8,save = True):
    '''
    Find cell from mannual mask.

    Parameters
    ----------
    mask_graph_path : (str)
        Save path of manuual plotted mask.
    average_graph_path : (str,optional)
        If not given, combined graph will not be produced.
    boulder : int, optional
        Boulder of cells. Centroid of cell over this boulde will be ignored.  The default is 20.
    save : bool, optional
        Whether we save cell graphs in specific folder. The default is True.

    Returns
    -------
    Cell_Finded : (Dic)
        Same cell dtype as before.

    '''
    save_path = OS_Tools.CDdotdot(mask_graph_path)
    mask_graph = cv2.imread(mask_graph_path,0)
    height,width = mask_graph.shape
    thres = mask_graph.max()/2
    thres_graph = mask_graph>thres# Get binary cell graph
    washed_thres_graph = skimage.morphology.remove_small_objects(thres_graph,5,connectivity = 1)# remove draw errors.
    cell_label = skimage.measure.label(washed_thres_graph)
    All_Cells = skimage.measure.regionprops(cell_label)
    # Delete cell 
    for i in range(len(All_Cells)-1,-1,-1):
        current_cell = All_Cells[i]
        current_height,current_width = current_cell.centroid
        if current_height<boulder or (height-current_height)<boulder:
            All_Cells.pop(i)
        elif current_width<boulder or (width-current_width)<boulder:
            All_Cells.pop(i)
    # Visualization here.
    visual_cell_graph = Visualize.Cell_Visualize(All_Cells)
    annotated_graph = Visualize.Label_Cell(visual_cell_graph,All_Cells,color = (255,255,0))
    if average_graph_path == None:
        print('Average graph not given, no combined graph.')
        combined_graph = []
        labled_combined_graph = []
    else:
        average_graph = cv2.imread(average_graph_path,1)# Read in 8 bit color map
        # Then annotate cell mask on average graph.
        cell_mask = visual_cell_graph[:,:,0]/2
        combined_graph = average_graph.astype('f8')*0.7
        combined_graph[:,:,1] = np.clip((combined_graph[:,:,1]+cell_mask),0,255)
        combined_graph = combined_graph.astype('u1')
        labled_combined_graph = Visualize.Label_Cell(combined_graph, All_Cells,color = (255,255,0))
    # At last, save all cell information and cell maps,
    Cell_Finded = {}
    Cell_Finded['All_Cell_Information'] = All_Cells
    Cell_Finded['Cell_Graph'] = visual_cell_graph
    Cell_Finded['Annotate_Cell_Graph'] = annotated_graph
    Cell_Finded['Combined_Graph'] = combined_graph
    Cell_Finded['Combined_Graph_With_Number'] = labled_combined_graph
    if save == True:
        OS_Tools.Save_Variable(save_path, 'Manuall_Cells', Cell_Finded,'.cell')
        Graph_Tools.Show_Graph(visual_cell_graph, 'Cell_Graph', save_path)
        Graph_Tools.Show_Graph(annotated_graph, 'Annotate_Cell_Graph', save_path)
        if type(combined_graph) != type([]):
            Graph_Tools.Show_Graph(combined_graph, 'Combined_Graph', save_path)
            Graph_Tools.Show_Graph(labled_combined_graph, 'Combined_Graph_With_Number', save_path)

    return Cell_Finded
Esempio n. 14
0
def Translation_Alignment(all_folders,
                          base_mode='global',
                          input_base=np.array([[0, 0], [0, 0]]),
                          align_range=20,
                          align_boulder=20,
                          before_average=True,
                          average_std=5,
                          big_memory_mode=False,
                          save_aligned_data=False,
                          graph_shape=(512, 512),
                          timer=True):
    '''
    
    This function will align all tif graphs in input folders. Only translation transaction here. Affine transformation need further discussion.
    
    Parameters
    ----------
    all_folders:(list)
        List of all tif folders, elements are strs.
    
    base_mode:('global',int,'input',optional. The default is 'global')
        How to select base frame. 'global': use global average as base. int: use average of specific run as base. 'input':Manually input base graph, need to be a 2D-Ndarray.
        
    input_base:(2D-Ndarray,optional. The default is none.)
        If base_mode = 'input', input_base must be given. This will be the base for alignment.
        
    align_range:(int,optional. The default is 20)
        Max pixel of alignment. 
        
    align_boulder:(int,optional. The default is 20)
        boulder cut for align. For different graph size, this variable shall be change.
        
    before_average:(bool,optional. The default is True)
        Whether before average is done. It can be set False to save time, on this case base graph shall be given.
        
    average_std:(float,optional. The default is 5)
        How much std you want for average graph generation. Different std can effect graph effect.
    
    big_memory_mode:(bool,optional. The default is False)
        If memory is big enough, use this mode is faster.
        
    save_aligned_data:(bool,optional. The default is False)
        Can be true only in big memory mode. This will save all aligned graph in a single 4D-Ndarray file.Save folder is the first folder.
        
    graph_shape:(2-element-turple,optional. The default is (512,512))
        Shape of graphs. All input graph must be in same shape.
        
    timer:(bool,optional. The default is True)
        Show runtime of function and each procedures.
    
        
    Returns
    -------
    bool
        Whether new folder is generated.
    
    '''
    time_tic_start = time.time()
    #%% Step1, generate folders and file cycle.
    all_save_folders = List_Op.List_Annex(all_folders, ['Results'])
    Aligned_frame_folders = List_Op.List_Annex(all_save_folders,
                                               ['Aligned_Frames'])
    for i in range(len(all_save_folders)):
        OS_Tools.mkdir(all_save_folders[i])
        OS_Tools.mkdir(Aligned_frame_folders[i])
    Before_Align_Tif_Name = []
    for i in range(len(all_folders)):
        current_run_tif = OS_Tools.Get_File_Name(all_folders[i])
        Before_Align_Tif_Name.append(current_run_tif)

    #%% Step2, Generate average map before align.
    if before_average == True:
        print('Before run averaging ...')
        Before_Align_Dics = {
        }  # This is the dictionary of all run averages. Keys are run id.
        total_graph_num = 0  # Counter of graph numbers.
        for i in range(len(Before_Align_Tif_Name)):
            current_run_graph_num = len(Before_Align_Tif_Name[i])
            total_graph_num += current_run_graph_num
            current_run_average = Graph_Tools.Average_From_File(
                Before_Align_Tif_Name[i])
            current_run_average = Graph_Tools.Clip_And_Normalize(
                current_run_average, clip_std=average_std)
            Before_Align_Dics[i] = (
                current_run_average, current_run_graph_num
            )  # Attention here, data recorded as turple.
            Graph_Tools.Show_Graph(
                current_run_average, 'Run_Average',
                all_save_folders[i])  # Show and save Run Average.
        # Then Use Weighted average method to generate global tif.
        global_average_graph = np.zeros(shape=np.shape(
            Before_Align_Dics[0][0]),
                                        dtype='f8')  # Base on shape of graph
        for i in range(len(Before_Align_Tif_Name)):
            global_average_graph += Before_Align_Dics[i][0].astype(
                'f8') * Before_Align_Dics[i][1] / total_graph_num
        global_average_graph = Graph_Tools.Clip_And_Normalize(
            global_average_graph, clip_std=average_std)
        # Then save global average in each run folder.
        if len(all_folders) > 1:
            for i in range(len(Before_Align_Tif_Name)):
                Graph_Tools.Show_Graph(global_average_graph,
                                       'Global_Average',
                                       all_save_folders[i],
                                       show_time=0)
        else:
            print('Only One run, no global average.')
    else:
        print('Before average Skipped.')
    time_tic_average0 = time.time()

    #%% Step3, Core Align Function.
    print('Aligning...')
    if base_mode == 'global':
        base = global_average_graph
    elif base_mode == 'input':
        base = input_base
    elif type(base_mode) == int:
        base = Before_Align_Dics[base_mode][0]
    else:
        raise IOError('Invalid base mode.')
    # In big memory mode, save aligned_data in a dictionary file.
    if big_memory_mode == True:
        All_Aligned_Frame = {}
        for i in range(len(Before_Align_Tif_Name)):
            All_Aligned_Frame[i] = np.zeros(
                shape=(graph_shape + (len(Before_Align_Tif_Name[i]), )),
                dtype='u2')  # Generate empty graph matrix.
    for i in range(len(Before_Align_Tif_Name)):  # Cycle all runs
        for j in range(len(
                Before_Align_Tif_Name[i])):  # Cycle all graphs in current run
            current_graph = cv2.imread(Before_Align_Tif_Name[i][j],
                                       -1)  # Read in current graph.
            _, _, current_aligned_graph = Alignment(base,
                                                    current_graph,
                                                    boulder=align_boulder,
                                                    align_range=align_range)
            graph_name = Before_Align_Tif_Name[i][j].split(
                '\\')[-1][:-4]  # Ignore extend name'.tif'.
            Graph_Tools.Show_Graph(current_aligned_graph,
                                   graph_name,
                                   Aligned_frame_folders[i],
                                   show_time=0)
            if big_memory_mode == True:
                All_Aligned_Frame[i][:, :, j] = current_aligned_graph
    print('Align Finished, generating average graphs...')
    time_tic_align_finish = time.time()

    #%% Step4, After Align Average
    After_Align_Graphs = {}
    if big_memory_mode == True:  # Average can be faster.
        temp_global_average_after_align = np.zeros(shape=graph_shape,
                                                   dtype='f8')
        for i in range(len(All_Aligned_Frame)):
            current_run_average = Graph_Tools.Clip_And_Normalize(
                np.mean(All_Aligned_Frame[i], axis=2),
                clip_std=average_std)  # Average run graphs, in type 'u2'
            After_Align_Graphs[i] = (current_run_average,
                                     len(All_Aligned_Frame[i][0, 0, :]))
            temp_global_average_after_align += After_Align_Graphs[i][0].astype(
                'f8') * After_Align_Graphs[i][1] / total_graph_num
        global_average_after_align = Graph_Tools.Clip_And_Normalize(
            temp_global_average_after_align, clip_std=average_std)
    else:  # Traditional ways.
        temp_global_average_after_align = np.zeros(shape=graph_shape,
                                                   dtype='f8')
        for i in range(len(Aligned_frame_folders)):
            current_run_names = OS_Tools.Get_File_Name(
                Aligned_frame_folders[i])
            current_run_average = Graph_Tools.Average_From_File(
                current_run_names)
            current_run_average = Graph_Tools.Clip_And_Normalize(
                current_run_average, clip_std=average_std)
            After_Align_Graphs[i] = (current_run_average,
                                     len(current_run_names))
            temp_global_average_after_align += After_Align_Graphs[i][0].astype(
                'f8') * After_Align_Graphs[i][1] / total_graph_num
        global_average_after_align = Graph_Tools.Clip_And_Normalize(
            temp_global_average_after_align, clip_std=average_std)
    # After average, save aligned graph in each save folder.
    for i in range(len(all_save_folders)):
        current_save_folder = all_save_folders[i]
        Graph_Tools.Show_Graph(After_Align_Graphs[i][0],
                               'Run_Average_After_Align', current_save_folder)
        if i == 0:  # Show global average only once.
            global_show_time = 5000
        else:
            global_show_time = 0
        if len(all_folders) > 1:
            Graph_Tools.Show_Graph(global_average_after_align,
                                   'Global_Average_After_Align',
                                   current_save_folder,
                                   show_time=global_show_time)
    time_tic_average1 = time.time()

    #%% Step5, save and timer
    if save_aligned_data == True:
        OS_Tools.Save_Variable(all_save_folders[0], 'All_Aligned_Frame_Data',
                               All_Aligned_Frame)

    if timer == True:
        whole_time = time_tic_average1 - time_tic_start
        before_average_time = time_tic_average0 - time_tic_start
        align_time = time_tic_align_finish - time_tic_average0
        after_average_time = time_tic_average1 - time_tic_align_finish
        print('Total Time = ' + str(whole_time) + ' s.')
        print('Before Average Time = ' + str(before_average_time) + ' s.')
        print('Align Time = ' + str(align_time) + ' s.')
        print('After Average Time = ' + str(after_average_time) + ' s.')

    return True
Esempio n. 15
0
import My_Wheels.OS_Tools_Kit as ot
import matplotlib.pyplot as plt

day_folder = r'K:\Test_Data\2P\210320_L76_2P'
save_folder = day_folder + r'\_All_Results'
ot.mkdir(save_folder)
#%% Analyze Run05-G16 First.
G16_CP = Cell_Processor(day_folder, 'Run005')
all_cell_name = G16_CP.all_cell_names
Ori_IDs = Stim_ID_Combiner('G16_Oriens')
sub_sf = save_folder + r'\G16_Oriens'
ot.mkdir(sub_sf)
for i in range(len(all_cell_name)):
    _, raw_data, _ = G16_CP.Single_Cell_Response_Data(Ori_IDs,
                                                      all_cell_name[i])
    ot.Save_Variable(sub_sf, all_cell_name[i], raw_data, '.raw')
    test_fig = G16_CP.Average_Response_Map()
    test_fig.savefig(sub_sf + r'\\' + all_cell_name[i] + '_Response.png',
                     dpi=180)
    plt.clf()
#%% Then directions
Dir_IDs = Stim_ID_Combiner('G16_Dirs')
sub_sf = save_folder + r'\G16_Dirs'
ot.mkdir(sub_sf)
for i in range(len(all_cell_name)):
    _, raw_data, _ = G16_CP.Single_Cell_Response_Data(Dir_IDs,
                                                      all_cell_name[i])
    ot.Save_Variable(sub_sf, all_cell_name[i], raw_data, '.raw')
    test_fig = G16_CP.Average_Response_Map()
    test_fig.savefig(sub_sf + r'\\' + all_cell_name[i] + '_Response.png',
                     dpi=180)
def Standard_Cell_Processor(
    animal_name,
    date,
    day_folder,
    cell_file_path,
    #average_graph_path, # not necessary.
    run_id_lists,
    location='A',  # For runs have 
    Stim_Frame_Align_name='_All_Stim_Frame_Infos.sfa',
    #Stim_Frame_Align_subfolder = r'\Results\Stim_Frame_Align.pkl',# API changed.
    align_subfolder=r'\Results\Aligned_Frames',
    response_head_extend=3,
    response_tail_extend=3,
    base_frame=[0, 1, 2],
    filter_para=(0.02, False)):
    # Folder and name initialization
    print('Just make sure average and cell find is already done.')
    cell_dic = ot.Load_Variable(cell_file_path)
    cell_info = cell_dic['All_Cell_Information']
    cell_name_prefix = animal_name + '_' + str(date) + location + '_'
    all_cell_num = len(cell_info)
    all_run_subfolders = lt.List_Annex([day_folder],
                                       lt.Run_Name_Producer_2P(run_id_lists))
    save_folder = day_folder
    all_Stim_Frame_Align = ot.Load_Variable(day_folder + r'\\' +
                                            Stim_Frame_Align_name)
    # Set cell data formats.
    all_cell_list = []
    for i in range(all_cell_num):
        current_cell_name = cell_name_prefix + ot.Bit_Filler(i, 4)
        current_cell_dic = {}
        current_cell_dic['Name'] = current_cell_name
        current_cell_dic['Cell_Info'] = cell_info[i]
        # Cycle all runs for F and dF trains.
        current_cell_dic['dF_F_train'] = {}
        current_cell_dic['F_train'] = {}
        current_cell_dic['Raw_CR_trains'] = {}
        current_cell_dic['CR_trains'] = {}
        all_cell_list.append(current_cell_dic)
    # Then cycle all runs, fill in
    for i in range(len(all_run_subfolders)):
        current_runid = 'Run' + (all_run_subfolders[i][-3:]
                                 )  # Use origin run id to avoid bugs.
        current_all_tif_name = ot.Get_File_Name(
            all_run_subfolders[i] + align_subfolder, '.tif')
        current_Stim_Frame_Align = all_Stim_Frame_Align[current_runid]
        if current_Stim_Frame_Align == None or len(
                current_Stim_Frame_Align
        ) == 302:  # meaning this run is spon or RF25.
            current_run_Fs, current_run_dF_Fs = Spike_Train_Generator(
                current_all_tif_name, cell_info, 'most_unactive', None)
        else:
            current_run_stim_train = current_Stim_Frame_Align[
                'Original_Stim_Train']
            if 0 in current_run_stim_train:  # having 0
                current_run_Fs, current_run_dF_Fs = Spike_Train_Generator(
                    current_all_tif_name,
                    cell_info,
                    Base_F_type='nearest_0',
                    stim_train=current_run_stim_train)
            else:
                current_run_Fs, current_run_dF_Fs = Spike_Train_Generator(
                    current_all_tif_name,
                    cell_info,
                    Base_F_type='before_ISI',
                    stim_train=current_run_stim_train)
        # Then put trains above into each cell files.
        for j in range(all_cell_num):
            all_cell_list[j]['dF_F_train'][current_runid] = current_run_dF_Fs[
                j]
            all_cell_list[j]['F_train'][current_runid] = current_run_Fs[j]
        # Then, we generate Condition Reaction Train for each cell and each condition.
        if current_Stim_Frame_Align == None:
            all_cell_list[j]['CR_trains'][current_runid] = None
            all_cell_list[j]['Raw_CR_trains'][current_runid] = None
        else:
            for j in range(all_cell_num):
                all_cell_list[j]['CR_trains'][current_runid], all_cell_list[j][
                    'Raw_CR_trains'][
                        current_runid] = Single_Condition_Train_Generator(
                            current_run_Fs[j], current_Stim_Frame_Align,
                            response_head_extend, response_tail_extend,
                            base_frame, filter_para)
    # Till now, all cell data of all runs is saved in 'all_cell_list'.
    # Last part, saving files. All cells in one file, dtype = dic.
    all_cell_dic = {}
    for i in range(all_cell_num):
        all_cell_dic[all_cell_list[i]['Name']] = all_cell_list[i]
    ot.Save_Variable(save_folder,
                     '_' + animal_name + '_' + date + location + '_All_Cells',
                     all_cell_dic, '.ac')
    return True
Esempio n. 17
0
from My_Wheels.Cell_Find_From_Graph import Cell_Find_And_Plot
from Stim_Frame_Align import Stim_Frame_Align
all_stim_cell = Cell_Find_And_Plot(r'E:\Test_Data\2P\210112_L76_2P\1-007\Results', 'Global_Average_After_Align.tif','All_Stim',find_thres = 1.5)
all_stim_folders = [
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run07_2P_OD8_auto',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run08_2P_G8',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run09_2P_G8_RF',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run11_2P_G8_RF',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run13_color7_dir8_grating_squarewave_prefsize_BG',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run14_2P_RGLum4_RF',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run15_2P_RGLum4',
    r'E:\Test_Data\2P\210112_L76_2P\210112_L76_stimuli\Run16_shape3_dir8_modified_WJY_201228'
    ]
for i in range(8):
    _,current_stim_dic = Stim_Frame_Align(all_stim_folders[i])
    OS_Tools.Save_Variable(all_stim_folders[i], 'Stim_Frame_Align', current_stim_dic)
#%%Get F and dF trains here.
cell_folder = r'H:\Test_Data\2P\210112_L76_2P\1-007\Results\All_Stim'
from Standard_Parameters.Sub_Graph_Dics import Sub_Dic_Generator
from Standard_Stim_Processor import One_Key_Stim_Maps
# Run07,OD
OD_Para = Sub_Dic_Generator('OD_2P')
One_Key_Stim_Maps(r'E:\Test_Data\2P\210112_L76_2P\1-007', cell_folder, OD_Para)
# Run08_G8
G8_Para = Sub_Dic_Generator('G8+90')
One_Key_Stim_Maps(r'E:\Test_Data\2P\210112_L76_2P\1-008', cell_folder, G8_Para)
One_Key_Stim_Maps(r'E:\Test_Data\2P\210112_L76_2P\1-009', cell_folder, G8_Para)
One_Key_Stim_Maps(r'E:\Test_Data\2P\210112_L76_2P\1-011', cell_folder, G8_Para)
RG_Para = Sub_Dic_Generator('RGLum4')
One_Key_Stim_Maps(r'E:\Test_Data\2P\210112_L76_2P\1-014', cell_folder, RG_Para)
One_Key_Stim_Maps(r'E:\Test_Data\2P\210112_L76_2P\1-015', cell_folder, RG_Para)
def Standard_Stim_Processor(data_folder,
                            stim_folder,
                            sub_dic,
                            alinged_sub_folder=r'\Results\Aligned_Frames',
                            show_clip=3,
                            tuning_graph=False,
                            cell_method='Default',
                            filter_method='Gaussian',
                            LP_Para=((5, 5), 1.5),
                            HP_Para=False,
                            spike_train_path='Default',
                            spike_train_filter_para=(False, False),
                            spike_train_filter_method=False):
    '''
    Generate subtraction graph, cell graph and tuning graphs if requred.

    Parameters
    ----------
    data_folder : (str)
        Run folder.
    stim_folder : (str)
        Stim file folder or Frame_Stim_Align File folder. Pre align is advised.
    sub_dic : (Dic)
        Subtraction dicionary. This can be generated from My_Wheels.Standard_Parameters
    show_clip : (float), optional
        Clip of graph show. The default is 3.
    tuning_graph : (bool), optional
        Whether we generate tuning graph of each cells. The default is False.
    cell_method : (str), optional
        Cell find method. You can input cell file path here. The default is 'Default'.
    filter_method : (str), optional
        False to skip filter. Kernel function of graph filtering. The default is 'Gaussian'.
    LP_Para : (turple), optional
        False to skip. Low pass filter of graph. The default is ((5,5),1.5).
    HP_Para : (turple), optional
        False to skip. High pass filter of graph. Big HP can be very slow!. The default is False.
    spike_train_path : (str), optional
        Path of spike train.'Default' will generate spike train directly. The default is 'Default'.
    spike_train_filter_para : (turple), optional
        Signal filter bandpass propotion of spike train. Please be sure if you need this. The default is (False,False).
    spike_train_filter_method : (str), optional
        False to skip. Method of signal filtering. The default is False.

    Returns
    -------
    None.

    '''
    # Path Cycle.
    from Cell_Find_From_Graph import On_Off_Cell_Finder
    work_folder = data_folder + r'\Results'
    OS_Tools.mkdir(work_folder)
    aligned_frame_folder = data_folder + alinged_sub_folder
    OS_Tools.mkdir(aligned_frame_folder)

    # Step1, align graphs. If already aligned, just read
    if not os.listdir(aligned_frame_folder):  # if this is a new folder
        print('Aligned data not found. Aligning here..')
        Translation_Alignment([data_folder])
    aligned_all_tif_name = np.array(
        OS_Tools.Get_File_Name(aligned_frame_folder)
    )  # Use numpy array, this is easier for slice.

    # Step2, get stim fram align matrix. If already aligned, just read in aligned dictionary.
    file_detector = len(stim_folder.split('.'))
    if file_detector == 1:  # Which means input is a folder
        print('Frame Stim not Aligned, aligning here...')
        from My_Wheels.Stim_Frame_Align import Stim_Frame_Align
        _, Frame_Stim_Dic = Stim_Frame_Align(stim_folder)
    else:  # Input is a file
        Frame_Stim_Dic = OS_Tools.Load_Variable(stim_folder)

    # Step3, get cell information
    if cell_method == 'Default':  # meaning we will use On-Off graph to find cell.
        print('Cell information not found. Finding here..')
        cell_dic = On_Off_Cell_Finder(aligned_all_tif_name,
                                      Frame_Stim_Dic,
                                      filter_method=filter_method,
                                      LP_Para=LP_Para,
                                      HP_Para=HP_Para)
    else:
        cell_dic = OS_Tools.Load_Variable(cell_method)

    # Step4, calculate spike_train.
    if spike_train_path != 'Default':
        dF_F_train = OS_Tools.Load_Variable(spike_train_path)
    else:  # meaning we need to calculate spike train from the very begining.

        _, dF_F_train = Spike_Train_Generator(
            aligned_all_tif_name,
            cell_dic['All_Cell_Information'],
            Base_F_type='nearest_0',
            stim_train=Frame_Stim_Dic['Original_Stim_Train'],
            LP_Para=LP_Para,
            HP_Para=HP_Para,
            filter_method=filter_method)
    #Step5, filt spike trains.
    if spike_train_filter_method != False:  # Meaning we need to do train filter.
        for i in range(len(dF_F_train)):
            dF_F_train[i] = My_Filter.Signal_Filter(dF_F_train,
                                                    spike_train_filter_method,
                                                    spike_train_filter_para)
    # Step6, get each frame graph and cell graph.
    all_graph_keys = list(sub_dic.keys())
    for i in range(len(sub_dic)):
        output_folder = work_folder + r'\Subtraction_Graphs'
        current_key = all_graph_keys[i]
        current_sub_list = sub_dic[current_key]
        A_conds = current_sub_list[0]  # condition of A graph
        B_conds = current_sub_list[1]  # condition of B graph
        A_IDs = []
        B_IDs = []
        for i in range(len(A_conds)):
            A_IDs.extend(Frame_Stim_Dic[A_conds[i]])
        for i in range(len(B_conds)):
            B_IDs.extend(Frame_Stim_Dic[B_conds[i]])
        # Get frame maps.
        current_sub_graph, current_t_graph, current_F_info = Single_Subgraph_Generator(
            aligned_all_tif_name, A_IDs, B_IDs, filter_method, LP_Para,
            HP_Para)

        current_sub_graph = Graph_Tools.Clip_And_Normalize(
            current_sub_graph, show_clip)
        Graph_Tools.Show_Graph(current_sub_graph, current_key + '_SubGraph',
                               output_folder)
        current_t_graph = Graph_Tools.Clip_And_Normalize(
            current_t_graph, show_clip)
        Graph_Tools.Show_Graph(current_t_graph, current_key + '_T_Graph',
                               output_folder)
        OS_Tools.Save_Variable(output_folder,
                               current_key + '_Sub_Info',
                               current_F_info,
                               extend_name='.info')
        # Get cell maps
        cell_info = cell_dic['All_Cell_Information']
        current_cell_sub_graph, current_cell_t_graph, current_cell_info = Single_Cellgraph_Generator(
            dF_F_train, cell_info, show_clip, A_IDs, B_IDs)
        Graph_Tools.Show_Graph(current_cell_sub_graph,
                               current_key + '_Cell_SubGraph', output_folder)
        Graph_Tools.Show_Graph(current_cell_t_graph,
                               current_key + '_Cell_T_Graph', output_folder)
        OS_Tools.Save_Variable(output_folder,
                               current_key + '_Cell_Info',
                               current_cell_info,
                               extend_name='.info')
    #Step7, calculate cell tuning graph.
    if tuning_graph == True:
        print('Not finished yet.')
Esempio n. 19
0
        current_keys = loc_ids+start_id
        current_loc_frame_id = []
        for k in range(len(current_keys)):
            current_loc_frame_id.extend(Frame_Stim_Dic[current_keys[k]])
        current_loc_graph_name = aligned_all_tif_name[current_loc_frame_id]
        current_graph = Graph_Tools.Average_From_File(current_loc_graph_name)
        all_cells = current_graph*cell_mask
        RF_Data[i,j,0] = current_graph.mean()
        RF_Data[i,j,1] = all_cells.mean()
# then sub average value.
frame_average = RF_Data[:,:,0].min()
cell_average = RF_Data[:,:,1].min()
prop_RF_Data = np.zeros(shape = (5,5,2),dtype = 'f8')
prop_RF_Data[:,:,0] = RF_Data[:,:,0]/frame_average -1
prop_RF_Data[:,:,1] = RF_Data[:,:,1]/cell_average -1
OS_Tools.Save_Variable(save_folder, 'Origin_RF_Data', RF_Data)
#%% Then do Spline interpolation here.
import pylab as pl
# Show graph first.
x = np.array([1,2,3,4,5])
y = np.array([1,2,3,4,5])
fval = prop_RF_Data[:,:,0]
cval = prop_RF_Data[:,:,1]
pl.figure(figsize=(10,7))
pl.subplot(1,2,1)
pl.xticks(np.arange(-5,6,1))
pl.yticks(np.arange(-5,6,1))
im1=pl.imshow(fval, extent=[1,5,5,1], cmap='inferno', interpolation='spline16', origin="upper")
from mpl_toolkits.axes_grid1 import make_axes_locatable
ax = pl.gca()
divider = make_axes_locatable(ax)
Esempio n. 20
0
data_folder = [r'G:\Test_Data\2P\201121_L76_LM']
run_name = ['1-015', '1-016']
loc2_folders = List_Tools.List_Annex(data_folder, run_name)
Translation_Alignment(loc2_folders, base_mode='global', align_range=20)
#%%Stim Fram Aligns
from My_Wheels.Stim_Frame_Align import Stim_Frame_Align
import My_Wheels.OS_Tools_Kit as OS_Tools
all_stim_folder = [
    r'G:\Test_Data\2P\201121_L76_LM\201121_L76_stimuli\Run02_2P_G8',
    r'G:\Test_Data\2P\201121_L76_LM\201121_L76_stimuli\Run03_2P_manual_OD8',
    r'G:\Test_Data\2P\201121_L76_LM\201121_L76_stimuli\Run04_2P_RGLum4',
    r'G:\Test_Data\2P\201121_L76_LM\201121_L76_stimuli\Run15_2P_RGLum4'
]
for i in range(4):
    _, Frame_Stim_Dic = Stim_Frame_Align(all_stim_folder[i])
    OS_Tools.Save_Variable(all_stim_folder[i], 'Stim_Fram_Align',
                           Frame_Stim_Dic)
#%%Cell Find from Run01 Morphology graph.
from My_Wheels.Cell_Find_From_Graph import Cell_Find_And_Plot
Cell_Find_And_Plot(r'G:\Test_Data\2P\201121_L76_LM\1-001\Results',
                   'Run_Average_After_Align.tif',
                   'Morpho',
                   find_thres=1.5)
#%% Calculate Spike Train of Run01 Morpho cell into each run.
from My_Wheels.Spike_Train_Generator import Spike_Train_Generator
all_run_folder = [
    r'G:\Test_Data\2P\201121_L76_LM\1-002',
    r'G:\Test_Data\2P\201121_L76_LM\1-003',
    r'G:\Test_Data\2P\201121_L76_LM\1-004'
]
for i in range(3):
    all_tif_name = OS_Tools.Get_File_Name(all_run_folder[i] +