Example #1
0
def create_juxta_label(kwik_file,
                       spike_thresholds,
                       num_of_spike_groups=1,
                       adc_channel_used=0,
                       adc_dtype=np.uint16,
                       inter_spike_time_distance=0.002,
                       amp_gain=100,
                       num_of_raw_data_channels=None,
                       spike_channels=None,
                       verbose=True):
    """
    Find the juxta spikes in the extra spike train and label them according to size splitting them into
    num_of_spike_groups groups

    Parameters
    ----------
    kwik_file
    spike_thresholds
    num_of_spike_groups
    adc_channel_used
    adc_dtype
    inter_spike_time_distance
    amp_gain
    num_of_raw_data_channels
    spike_channels
    verbose

    Returns
    -------

    """

    h5file = h5.File(kwik_file, mode='r')
    extra_spike_times = np.array(
        list(h5file['channel_groups/0/spikes/time_samples']))
    h5file.close()

    spikes_used = len(extra_spike_times)
    if verbose:
        print("Total spikes in extra = " + str(len(extra_spike_times)))

    # Get the juxta spikes and generate labels
    # 1) Generate the juxta spike time triggers (and the adc traces in Volts)
    raw_juxta_data_file = r'D:\Data\George\Projects\SpikeSorting\Joana_Paired_128ch\2015-09-03\Data' + \
                          r'\adc2015-09-03T21_18_47.bin'
    raw_data_patch = ephys.load_raw_event_trace(raw_juxta_data_file,
                                                number_of_channels=8,
                                                channel_used=adc_channel_used,
                                                dtype=adc_dtype)
    juxta_spike_triggers, juxta_spike_peaks, juxta_spike_data_in_V = tf.create_spike_triggered_events(
        raw_data_patch.dataMatrix,
        threshold=spike_thresholds,
        inter_spike_time_distance=inter_spike_time_distance,
        amp_gain=amp_gain)
    num_of_spikes = len(juxta_spike_triggers)
    if verbose:
        print('Total spikes in Juxta = ' + str(num_of_spikes))

    # 2) Seperate the juxta spikes into a number of groups according to their size
    juxta_spikes_grouped, juxta_spike_peaks_grouped, juxta_spike_triggers_grouped_withnans,\
            juxta_spike_peaks_grouped_withnans, spike_thresholds_groups = \
        split_juxta_spikes_into_groups_by_size(num_of_spike_groups=num_of_spike_groups,
                                                   juxta_spike_peaks=juxta_spike_peaks,
                                                   juxta_spike_triggers=juxta_spike_triggers)

    # 3) Find the common spikes between the extra apikes and the juxta spikes for all the juxta spikes and for all the
    # sub groups of juxta spikes and group the good spikes
    common_spikes_grouped = {}
    juxta_spikes_not_found_grouped = {}
    indices_of_common_extra_spikes_grouped = {}
    for g in range(1, num_of_spike_groups + 1):
        common_spikes_grouped[g], indices_of_common_extra_spikes_grouped[g], juxta_spikes_not_found_grouped[g] = \
             ut.find_points_in_array_with_jitter(array_of_points_to_be_found=juxta_spikes_grouped[g],
                                                 array_to_search=extra_spike_times[:spikes_used],
                                                 jitter_around_each_point=7)
    if spike_channels is not None and num_of_raw_data_channels is not None:
        common_spikes_grouped[g], indices_of_common_extra_spikes_grouped[g] \
            = select_spikes_in_certain_channels(spike_thresholds, common_spikes_grouped[g],
                                                indices_of_common_extra_spikes_grouped[g],
                                                spike_channels, num_of_raw_data_channels)

    # 5) Get the t-sne indices of the grouped juxta spikes
    indices_of_data_for_tsne = range(spikes_used)
    juxta_cluster_indices_grouped = {}
    for g in range(0, num_of_spike_groups):
        juxta_cluster_indices_temp = np.intersect1d(
            indices_of_data_for_tsne,
            indices_of_common_extra_spikes_grouped[g + 1])
        juxta_cluster_indices_grouped[g] = [
            i for i in np.arange(0, len(indices_of_data_for_tsne)) if len(
                np.where(juxta_cluster_indices_temp ==
                         indices_of_data_for_tsne[i])[0])
        ]
        if verbose and spike_channels is not None:
            print('Labeled after cleaning = ' +
                  str(len(juxta_cluster_indices_grouped[g])))

    return juxta_cluster_indices_grouped, spike_thresholds_groups
types_of_data_to_load = {'t': 'ivm_data_filtered_cell{}.dat',
                         'c': 'ivm_data_filtered_continous_cell{}.dat',
                         'k': 'ivm_data_filtered_klusta_spikes_cell{}.dat',
                         'p': 'patch_data_cell{}.dat',
                         'm': 'ivm_data_raw_cell{}.dat',
                         }


# Generate the spike time triggers (and the adc traces in Volts) for all cells
all_cells_spike_triggers = {}
all_cells_spike_data_in_V = {}
all_cells_spike_triggers_sec = {}
for i in np.arange(0, len(good_cells)):
    raw_data_file_pipette = os.path.join(data_folder, 'adc'+date+'T'+cell_capture_times[good_cells[i]]+'.bin')

    raw_data_patch = ephys.load_raw_event_trace(raw_data_file_pipette, number_of_channels=8,
                                                  channel_used=adc_channel_used, dtype=adc_dtype)
    spike_triggers, spike_data_in_V = tf.find_peaks(raw_data_patch.dataMatrix,
                                                                       threshold=spike_thresholds[good_cells[i]],
                                                                       inter_spike_time_distance=inter_spike_time_distance,
                                                                       amp_gain=amp_gain)
    all_cells_spike_triggers[good_cells[i]] = spike_triggers
    all_cells_spike_data_in_V[good_cells[i]] = spike_data_in_V
    all_cells_spike_triggers_sec[good_cells[i]] = spike_triggers / sampling_freq
    np.save(os.path.join(analysis_folder,'triggers_Cell'+ good_cells[i] + '.npy'), all_cells_spike_triggers[good_cells[i]])
    print(len(spike_triggers))

#Filter for extracellular recording
def highpass(data,BUTTER_ORDER=3, F_HIGH=14250,sampleFreq=30000.0,passFreq=100.0):
    b, a = signal.butter(BUTTER_ORDER,(passFreq/(sampleFreq/2), F_HIGH/(sampleFreq/2)),'pass')
    return signal.filtfilt(b,a,data)
                         'c': 'ivm_data_filtered_continous_cell{}.dat',
                         'k': 'ivm_data_filtered_klusta_spikes_cell{}.dat',
                         'p': 'patch_data_cell{}.dat',
                         'm': 'ivm_data_raw_cell{}.dat',
                         'l': 'ivm_data_filteredlow_cell{}.dat'
                         }


# Generate the spike time triggers (and the adc traces in Volts) for all cells
all_cells_spike_triggers = {}
all_cells_spike_data_in_V = {}
all_cells_spike_triggers_sec = {}
for i in np.arange(0, len(good_cells)):
    raw_data_file_pipette = os.path.join(data_folder, 'adc'+date+'T'+cell_capture_times[good_cells[i]]+'.bin')

    raw_data_patch = ephys.load_raw_event_trace(raw_data_file_pipette, number_of_channels=8,
                                                  channel_used=adc_channel_used, dtype=adc_dtype)
    spike_triggers, spike_data_in_V = tf.find_peaks(raw_data_patch.dataMatrix,
                                                                       threshold=spike_thresholds[good_cells[i]],
                                                                       inter_spike_time_distance=inter_spike_time_distance,
                                                                       amp_gain=amp_gain)
    all_cells_spike_triggers[good_cells[i]] = spike_triggers
    all_cells_spike_data_in_V[good_cells[i]] = spike_data_in_V
    all_cells_spike_triggers_sec[good_cells[i]] = spike_triggers / sampling_freq
    np.save(os.path.join(analysis_folder,'triggers_Cell'+ good_cells[i] + '.npy'), all_cells_spike_triggers[good_cells[i]])
    print(len(spike_triggers))

#Filter for extracellular recording
def highpass(data,BUTTER_ORDER=3, F_HIGH=5000, sampleFreq=30000.0, passFreq=100.0):
    b, a = signal.butter(BUTTER_ORDER,(passFreq/(sampleFreq/2), F_HIGH/(sampleFreq/2)),'pass')
    return signal.filtfilt(b,a,data)
def create_juxta_label(kwik_file, spike_thresholds, num_of_spike_groups=1,
                       adc_channel_used=0, adc_dtype=np.uint16, inter_spike_time_distance=0.002,
                       amp_gain=100,
                       num_of_raw_data_channels=None,
                       spike_channels=None,
                       verbose=True):
    """
    Find the juxta spikes in the extra spike train and label them according to size splitting them into
    num_of_spike_groups groups

    Parameters
    ----------
    kwik_file
    spike_thresholds
    num_of_spike_groups
    adc_channel_used
    adc_dtype
    inter_spike_time_distance
    amp_gain
    num_of_raw_data_channels
    spike_channels
    verbose

    Returns
    -------

    """

    h5file = h5.File(kwik_file, mode='r')
    extra_spike_times = np.array(list(h5file['channel_groups/0/spikes/time_samples']))
    h5file.close()

    spikes_used = len(extra_spike_times)
    if verbose:
        print("Total spikes in extra = " + str(len(extra_spike_times)))


    # Get the juxta spikes and generate labels
    # 1) Generate the juxta spike time triggers (and the adc traces in Volts)
    raw_juxta_data_file = r'D:\Data\George\Projects\SpikeSorting\Joana_Paired_128ch\2015-09-03\Data' + \
                          r'\adc2015-09-03T21_18_47.bin'
    raw_data_patch = ephys.load_raw_event_trace(raw_juxta_data_file, number_of_channels=8,
                                                  channel_used=adc_channel_used, dtype=adc_dtype)
    juxta_spike_triggers, juxta_spike_peaks, juxta_spike_data_in_V = tf.create_spike_triggered_events(raw_data_patch.dataMatrix,
                                                                       threshold=spike_thresholds,
                                                                       inter_spike_time_distance=inter_spike_time_distance,
                                                                       amp_gain=amp_gain)
    num_of_spikes = len(juxta_spike_triggers)
    if verbose:
        print('Total spikes in Juxta = ' + str(num_of_spikes))



    # 2) Seperate the juxta spikes into a number of groups according to their size
    juxta_spikes_grouped, juxta_spike_peaks_grouped, juxta_spike_triggers_grouped_withnans,\
            juxta_spike_peaks_grouped_withnans, spike_thresholds_groups = \
        split_juxta_spikes_into_groups_by_size(num_of_spike_groups=num_of_spike_groups,
                                                   juxta_spike_peaks=juxta_spike_peaks,
                                                   juxta_spike_triggers=juxta_spike_triggers)

    # 3) Find the common spikes between the extra apikes and the juxta spikes for all the juxta spikes and for all the
    # sub groups of juxta spikes and group the good spikes
    common_spikes_grouped = {}
    juxta_spikes_not_found_grouped = {}
    indices_of_common_extra_spikes_grouped = {}
    for g in range(1, num_of_spike_groups+1):
        common_spikes_grouped[g], indices_of_common_extra_spikes_grouped[g], juxta_spikes_not_found_grouped[g] = \
             ut.find_points_in_array_with_jitter(array_of_points_to_be_found=juxta_spikes_grouped[g],
                                                 array_to_search=extra_spike_times[:spikes_used],
                                                 jitter_around_each_point=7)
    if spike_channels is not None and num_of_raw_data_channels is not None:
        common_spikes_grouped[g], indices_of_common_extra_spikes_grouped[g] \
            = select_spikes_in_certain_channels(spike_thresholds, common_spikes_grouped[g],
                                                indices_of_common_extra_spikes_grouped[g],
                                                spike_channels, num_of_raw_data_channels)


    # 5) Get the t-sne indices of the grouped juxta spikes
    indices_of_data_for_tsne = range(spikes_used)
    juxta_cluster_indices_grouped = {}
    for g in range(0, num_of_spike_groups):
        juxta_cluster_indices_temp = np.intersect1d(indices_of_data_for_tsne, indices_of_common_extra_spikes_grouped[g+1])
        juxta_cluster_indices_grouped[g] = [i for i in np.arange(0, len(indices_of_data_for_tsne)) if
                                 len(np.where(juxta_cluster_indices_temp == indices_of_data_for_tsne[i])[0])]
        if verbose and spike_channels is not None:
                print('Labeled after cleaning = ' + str(len(juxta_cluster_indices_grouped[g])))

    return juxta_cluster_indices_grouped, spike_thresholds_groups