Beispiel #1
0
def update_all_plots_one_cell(filename, save_folder, ext_electrodes = [1, 2, 3, 4, 5, 6, 7], intr_electrode = 1, data_part = 'all', cell = 1):
    """ work on given data file"""
    ext = '.eps'
    #================files which were previously analysed =======================
    names = ['max_2_', 'min_3_', 'all_', 'min_2_', 'max_1_'] # depending on max number of IPSPs used it should be added before
     # name of the file: spws_file, distances, equal_init_spont
    name_used = names[2]
    
    spws_file = name_used + 'SPWs_ipsps_final.npz'
    distances = name_used + 'spw_dist2first.npz'
    type_of_choice = "minimum" #"take_smaller" #set_number" "minimum"
    #equal_init_spont = type_of_choice + name_used + 'induc_spont_equal.npz'
    equal_init_spont = name_used + 'induc_spont_spw.npz'
    
    raw_data = "data_bas.npz"
    intra_data = 'data_baseintra.npz'
    spike_file = 'spikes_largest.npz'
    intra_spike_file = 'intra_spikes.npz'
    all_spikes = 'spikes.npz'
    #==============================================================
    #import pdb; pdb.set_trace()
    plots_folder = 'plots/' + name_used
    print 'working on: ' +  filename
    
    #solutions_folder = 'plots/'
    delete_old = False #!!!! it will delete all the previously saved plots so be careful!
    run_all_functions = True
    
    if delete_old: # !!!!!!
        fold_mng.remove_folder(save_folder + plots_folder)
        
    win = [0, 0]
    
    if intr_electrode == 1:     
        
        plot_name_ipsps_2_dist = 'numIPSP_distance'
        save_plot_in = plots_folder+plot_name_ipsps_2_dist + '/'
        if not run_all_functions:
            """ plots relation between distance from the spike and number of ipsp groups in a SPW """
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_noIpsps2distance(save_folder,save_plot_in , save_plots = plot_name_ipsps_2_dist, spw_file = spws_file, dist_file = distances, ext = ext)
        
        dist_spw2psike = 'dist_spw2spike'
        save_plot_in = plots_folder+ dist_spw2psike + '/'
        if not run_all_functions:
            """ plots relation between distance from the spike and number of ipsp groups in a SPW"""
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_dist_spw2spike(save_folder, save_plot_in, save_plots = dist_spw2psike, dist_file = distances, ext = ext)

        firing_rate = name_used + 'firing_rate'
        save_plot_in = plots_folder+ firing_rate + '/'
        if not run_all_functions:
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_fr_after_spike(save_folder, plot_folder = save_plot_in, 
                                      plot_file = firing_rate, intra_spikes = intra_spike_file,
                                      spike_data = spike_file , ext = ext, win = win)

        firing_rate_and_spws = name_used + 'firing_rate_and_spws'
        save_plot_in = plots_folder+ firing_rate_and_spws + '/'
        if not run_all_functions:
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_fr_after_spike_and_distances_after_spike(save_folder, plot_folder = save_plot_in,  
                                      plot_file = firing_rate_and_spws, intra_spikes = intra_spike_file, dist_file = distances,
                                      spike_data = spike_file , ext = ext)

        separateSPWs = 'separate_SPWs'
        if not run_all_functions:
            analyser.plot_different_SPWs(save_folder, save_plot_in, save_plots = separateSPWs, data_file = raw_data, intra_data_file = intra_data, induc_spont = equal_init_spont, intra_spikes = intra_spike_file, ext = '.png')
        
        alignedSPWs = 'aligned_SPWs'
        highest_peak = 'highest_peak.npz'
        save_plot_in = plots_folder+ alignedSPWs + '/'
        if not run_all_functions:
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_alignedSPW(save_folder, save_file = highest_peak, plot_folder = save_plot_in, save_plots = alignedSPWs, data_file = raw_data, intra_data_file = intra_data, induc_spont = equal_init_spont, intra_spikes = intra_spike_file, ext = '.png')

        
        spikePerElectrode = name_used + 'spike_per_electrode'
        hist_spike_bins = name_used + 'all_dists_hist.npz'
        save_name_max_electr = name_used + 'max_electr_origin.npz'
        save_plot_in = plots_folder+ spikePerElectrode + '/'
        if not run_all_functions: 
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_spike(save_folder, save_plot_in, save_plots = spikePerElectrode, 
                            save_file = hist_spike_bins, save_name_max_electr = save_name_max_electr, 
                            spike_data = spike_file, spw_data = equal_init_spont, 
                            ext = ext, win = win)
            
        #import pdb; pdb.set_trace()
        group_per_isps_all = name_used + 'group_per_isps_all.npz'
        if not run_all_functions: # ok
            analyser.plot_spw_ipsps_no_groups_all(save_folder, save_file = group_per_isps_all, data_file = raw_data, 
                                              spw_data = equal_init_spont, ext = ext)#



        
        groups_w_firing_rate = name_used + 'groups_w_firing_rate'
        save_plot_in = plots_folder+ groups_w_firing_rate + '/'
        if not run_all_functions:
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_groups_w_fr(save_folder, plot_folder = save_plot_in, 
                                      plot_file = groups_w_firing_rate, data_file = raw_data, 
                                      spw_groups = group_per_isps_all, spw_details = equal_init_spont,
                                      spike_data = all_spikes , ext = '.eps', win = win)
                                    # spikes_largest           
        
        cumulative_plot = 'cumulative_plot'
        save_plot_in = plots_folder+ cumulative_plot + '/'
        save_file = name_used + 'cum_change_variance.npz'
        if not run_all_functions:
            fold_mng.create_folder(save_folder + save_plot_in)
            #import pdb; pdb.set_trace()
            analyser.cum_distribution_funct(save_folder, save_file, plot_folder = save_plot_in, plot_file = cumulative_plot, data_file = raw_data, 
                                      spw_details = equal_init_spont,
                                      #spw_details = highest_peak,
                                      ext = '.png', win = win, cell = cell)
        
        #import pdb; pdb.set_trace() 
#
        
        plot_ampl_synch = 'ampl_synchrony'
        save_file = name_used + 'ampl_sync_dat'
        save_plot_in = plots_folder+ plot_ampl_synch + '/'
        if run_all_functions: # ok
            fold_mng.create_folder(save_folder + save_plot_in)
            analyser.plot_amplitude_vs_synchrony(save_folder, save_file, 
                                                 plot_folder = save_plot_in, 
                                                 plot_file = plot_ampl_synch, 
                                                 data_file = raw_data,
                                                 spw_details = equal_init_spont, ext = ext) 
Beispiel #2
0
def work_on_all(filename, save_folder, ext_electrodes = [1, 2, 3, 4, 5, 6, 7], intr_electrode = 1, data_part = 'all'):
    """ work on given data file - proceed all the variables and save them """
    ext = '.png'
    
    print 'working on: ' +  filename
    reanalize = True # set to true if to analyze the data no matter if it was already analysed or not
    if intr_electrode == 1:   
        delete_old = False #!!!!
        
        run_all_functions = True
        
        if delete_old: # !!!!!!
            fold_mng.remove_folder(save_folder)
        win = [0, 0]
        
        
        raw_data        = 'data.npz'
        if not run_all_functions:
            # reads the raw data from the file
            updater.up_datafile(filename, save_folder = save_folder, save_file = raw_data, ext_electrodes = ext_electrodes, intr_electrode = 1, reanalize = reanalize)
    

        plot_folder = 'full_data/'
        save_plots = 'data'
        if not run_all_functions:
            # saves part of the data 
            analyser.display_data(save_folder, plot_folder, save_plots, raw_data, trace = 0, part = [0, 10000], ext = ext)
        
        raw_baselined   = "data_bas.npz"
        if not run_all_functions:
            # removes the baseline
            updater.up_databas(save_folder, save_file = raw_baselined, load_file = raw_data, reanalize = reanalize)
        
        spikes_raw      = 'spikes.npz'
        spikes_filter = 'fast_data_'
        filter_folder = 'filtered/'
        if not run_all_functions:
            # finds all the extracellular spikes
            updater.up_extraspikes(save_folder, filter_folder, save_file = spikes_raw, load_file = raw_baselined, spikes_filter =  spikes_filter, reanalize = reanalize)
        
        # this funtion might be used for future spike clustering, but shall not be used for other reasons so far
        #spikes_params   = 'spikes_params.npz'
        #if run_all_functions:
        #    updater.up_expikes_params(save_folder, save_file = spikes_params, load_datafile = raw_baselined, load_spikefile = spikes_raw, reanalize = reanalize)
        
        spikes_largest = 'spikes_largest.npz'
        if not run_all_functions:
            # checks which spikes have the highest amplitude (if the same spike is detected in multiple electrodes)
            #     only the spike of the highest amplitude will be kept - structrue stays the same
            updater.up_spikes_ampl(save_folder, save_file =spikes_largest, load_spike_file = spikes_raw, reanalize = reanalize)
         
        SPWs_potential  = 'potential_SPWs.npz'
        if not run_all_functions:
            updater.up_highWaves(save_folder, filter_folder, save_file = SPWs_potential, load_datafile = raw_baselined,reanalize = reanalize)
     
        SPWs_potential_numb  = 'potential_SPWs_numbered.npz'
        if not run_all_functions:
            # it numbers which wave is the same SPWs and assigns number to them
            updater.up_highWaves_numb(save_folder, save_file = SPWs_potential_numb, load_spwsfile = SPWs_potential, reanalize = reanalize)
        
        
        SPWs_ipsps      = 'spws_params.npz' #'spws_ipsps.npz'
        if not run_all_functions:
            # it finds the preliminary IPSPs for each of the detected waves
            updater.up_SPW_ipsp(save_folder, filter_folder, save_file = SPWs_ipsps, load_datafile = raw_baselined, load_waves = SPWs_potential_numb, load_spikes = spikes_largest, reanalize = reanalize)
        
        ipsps_corrected = 'ipsps_corrected.npz'
        if not run_all_functions:
            # correct the IPSPs (no matter if used for SPW start or for later
            updater.up_correct_ipsps(save_folder, save_fig = 'spw_ipsp', save_file = ipsps_corrected, load_datafile = raw_baselined, load_spwsipsp = SPWs_ipsps, load_spwsspike = spikes_largest, reanalize = reanalize, ext = ext)
        
        #spws_large_enough = 'spw_large_enough.npz'
        #min_amplitude_of_spw = 40 #microV SPW in any point, in any electrode has to be at least this amplitude
        #if run_all_functions:
        #    updater.up_remove_too_small_spws(save_folder, save_file = spws_large_enough, load_datafile = raw_baselined, load_spwsipsp = ipsps_corrected, min_ampl = min_amplitude_of_spw, reanalize = reanalize, ext = ext)
        
        SPWs_ipsps_beg  = 'SPWs_ipsps_beg.npz'
        min_ipsp_ampl = 20
        if not run_all_functions:
            # finding properly each of the IPSP
            # it combines information on Waves/Ipsps and spikes to find the beginning of the SPW 
            updater.up_spws_beg(save_folder, save_fig = 'spw_ipsp', save_file = SPWs_ipsps_beg, load_datafile = raw_baselined, load_spwsipsp = ipsps_corrected, load_spwsspike = spikes_largest, reanalize = reanalize, ext = ext, expected_min_ipsp_ampl = min_ipsp_ampl)
    
        ipsps_groups = 'ipsps_grouped.npz'
        if not run_all_functions:
            # put IPSPs to groups
            updater.up_group_ipsps(save_folder, ipsps_groups, SPWs_ipsps_beg, raw_baselined, save_file = ipsps_groups, reanalize = reanalize)    
    
        SPWs_ipsps_corrected = 'SPWs_ipsps_corrected.npz'
        if not run_all_functions:
            updater.up_fill_gap_between_ipsp_groups(save_folder, SPWs_ipsps_corrected, ipsps_groups, data_file = raw_baselined, reanalize = reanalize)    
        
        SPWs_all_IPSPs = 'SPWs_all_ipsps.npz'
        if not run_all_functions:
            
            updater.up_spws_ipsp_beg(save_folder, filter_folder, save_fig = 'spw_ipsp', save_file = SPWs_all_IPSPs, load_datafile = raw_baselined, load_spwsipsp = SPWs_ipsps_corrected, load_spwsspike = spikes_largest, reanalize = reanalize, ext = ext, expected_min_ipsp_ampl = min_ipsp_ampl)
    
    
    #    # ----> check if the group does not exist on other electrodes
        SPWs_missing_link = 'SPWs_missing_link.npz'
        if not run_all_functions:
            updater.up_add_missing_electrodes_SPW(save_folder, SPWs_missing_link, SPWs_all_IPSPs, data_file = raw_baselined, reanalize = reanalize)


        SPWs_ipsps_corrected2 = 'SPWs_ipsps_corrected.npz'
        if not run_all_functions:
            updater.up_fill_gap_between_ipsp_groups(save_folder, SPWs_ipsps_corrected2, SPWs_missing_link, data_file = raw_baselined, reanalize = reanalize)
        
        SPWs_merged = 'SPWs_merged.npz'
        if not run_all_functions:
            updater.up_merge_close_groups(save_folder, SPWs_merged, SPWs_ipsps_corrected2, data_file = raw_baselined, reanalize = reanalize)
        
        min_amplitude_of_spw = 20
        too_small_removed = 'too_small_removed.npz'
        if not run_all_functions:
            updater.up_remove_too_small_spws(save_folder, save_file = too_small_removed, load_datafile = raw_baselined, load_spwsipsp = SPWs_merged, min_ampl = min_amplitude_of_spw, reanalize = reanalize, ext = ext)
        
        if not run_all_functions:
            my_name = 'cell7'
            #save_fig_name = '/home/maja/PhDProject/SPWs/SPWs/saved_data/solutions/all_/' + my_name + '.pdf'
            save_fig_name = '/home/maja/phdProject/analysis/swp/solutions/all_/' + my_name
            for numb in [19]: #kom no 7 (11)
                updater.up_create_sup_fig(save_fig_name, save_folder, data_file = raw_data, filter_folder = filter_folder, 
                                          spike_file = spikes_raw, spikes_raw = spikes_raw, spikes_largest = spikes_largest,
                                          final_Ipsp_spw = too_small_removed, ext = '.eps', start_no = numb)      
            #plt.show()      

        # use different number of ipsps, 
        # [-1, 2] - all to two IPSPS
        # 3 IPSPS to any number
        # any number of IPSPS 
        min_no_ipsps_used = [[-1, 2], [3, -1], [-1, -1], [2, -1], [-1, 1]]
        #min_no_ipsps_used = [[3, -1]]
        names = ['max_2_', 'min_3_', 'all_', 'min_2_', 'max_1_']
        #names = ['min_3_']
        
        if run_all_functions:
            updater.up_display_SPWs(save_folder, data_file = raw_baselined, spw_file = too_small_removed, reanalize = False) 
            
        for idx, min_no_ipsps in enumerate(min_no_ipsps_used): 

            SPWs_ipsps_final = names[idx] + 'SPWs_ipsps_final.npz'
            #min_no_ipsps = 3
            if run_all_functions:
                updater.up_remove_with_to_few_ipsps(save_folder, SPWs_ipsps_final, too_small_removed, to_remove = min_no_ipsps, reanalize = reanalize)
        
            #print intr_electrode
            data_intra = 'data_intra.npz'
            if not run_all_functions:
                updater.up_intrafile(filename, save_folder, save_file = data_intra, int_electrodes = [0], reanalize = reanalize)
            
            data_intra_base = 'data_baseintra.npz'
            if not run_all_functions:
                # removes the baseline
                updater.up_databas(save_folder, save_file = data_intra_base, load_file = data_intra, reanalize = reanalize)
                
            plot_folder = 'full_data/'
            save_plots = 'data_intra'
            if not run_all_functions:
                # plots part of the data given (in part)
                analyser.display_data(save_folder, plot_folder, save_plots, data_intra_base, trace = 0, part = [0, 100000], ext = ext)
            
            intra_spikes = 'intra_spikes.npz'
            if not run_all_functions:
                # detects intracellular spikes
                updater.up_intraSpikes(save_folder, save_file = intra_spikes, load_file = data_intra_base, reanalize = reanalize)
            
            ##SPWs_ipsps_corrected2 = SPWs_ipsps_final 
            if not run_all_functions:
                # it makes the plot to exactly analyse each SPW
                analyser.plot_data_interactive(save_folder, load_datafile = raw_baselined, load_spw_ipsps = SPWs_ipsps_final, 
                                               load_spikefile = spikes_largest, load_spikesall = spikes_raw, 
                                               load_ipspsOld =  SPWs_ipsps_beg, spw_base = SPWs_potential_numb,
                                               load_dataintrafile = data_intra_base, load_intraSpikes = intra_spikes)
    #    
            dist_spw_inspikes = names[idx] + 'spw_dist2first.npz'
            if not run_all_functions:
                # finds the closest distance spw to the proceeding intracellular spike
                updater.up_dist_SpwfromSpike(save_folder, save_file = dist_spw_inspikes, load_intrafile = intra_spikes, load_spwfile = SPWs_ipsps_final, spikes = 'all', reanalize = reanalize)
            
            induc_spont_spw = names[idx] + 'induc_spont_spw.npz'
            max_dist = [0.0, 5] # ms
            if not run_all_functions:
                # checks which SPWs are induced and which are spontaneous (if it's further than max_dist[1] it is spontaneous)
                # if any error is being allowed it should be given in max_idst[0], e.g. -0.5 (half milisecond before intra spike
                updater.up_induc_spont_spw(save_folder, save_file = induc_spont_spw, load_distances = dist_spw_inspikes, load_spwfile = SPWs_ipsps_final, max_init_dist = max_dist, reanalize = reanalize, ext = ext)
    #    
            induc_spont_equal = names[idx] + 'induc_spont_equal.npz'
            if not run_all_functions:
                # counts spontaneous and initiated SPWs and it randomly choses set of SPWs from the bigger set so that there is equal number in both sets
                updater.equalize_number_spws(save_folder, save_file = induc_spont_equal, induc_spont = induc_spont_spw, load_distances = dist_spw_inspikes, reanalize = reanalize)