Esempio n. 1
0
def load_data(epoch_key, brain_areas=None):

    if brain_areas is None:
        brain_areas = BRAIN_AREAS

    time = get_trial_time(epoch_key, ANIMALS)
    time = (pd.Series(np.ones_like(time, dtype=np.float),
                      index=time).resample('2ms').mean().index)

    def _time_function(*args, **kwargs):
        return time

    position_info = (get_interpolated_position_dataframe(
        epoch_key, ANIMALS,
        _time_function).dropna(subset=['linear_position', 'speed']))

    time = position_info.index

    tetrode_info = make_tetrode_dataframe(ANIMALS, epoch_key=epoch_key)
    is_brain_areas = (
        tetrode_info.area.astype(str).str.upper().isin(brain_areas))
    tetrode_keys = tetrode_info.loc[is_brain_areas].index
    lfps = get_LFPs(tetrode_keys, ANIMALS)
    lfps = lfps.resample('2ms').mean().fillna(method='pad').reindex(time)

    try:
        neuron_info = make_neuron_dataframe(ANIMALS).xs(epoch_key,
                                                        drop_level=False)
        neuron_info = neuron_info.loc[(neuron_info.numspikes > 100)
                                      & neuron_info.area.isin(brain_areas) &
                                      (neuron_info.type == 'principal')]
        spikes = get_all_spike_indicators(neuron_info.index, ANIMALS,
                                          _time_function).reindex(time)
    except KeyError:
        spikes = None

    tetrode_info = tetrode_info.loc[is_brain_areas]
    multiunit = (get_all_multiunit_indicators(
        tetrode_info.index, ANIMALS,
        _time_function).sel(features=_MARKS).reindex({'time': time}))
    multiunit_spikes = (np.any(~np.isnan(multiunit.values),
                               axis=1)).astype(np.float)
    multiunit_firing_rate = pd.DataFrame(get_multiunit_population_firing_rate(
        multiunit_spikes, SAMPLING_FREQUENCY, smoothing_sigma=0.020),
                                         index=time,
                                         columns=['firing_rate'])

    return {
        'position_info': position_info,
        'spikes': spikes,
        'multiunit': multiunit,
        'lfps': lfps,
        'tetrode_info': tetrode_info,
        'multiunit_firing_rate': multiunit_firing_rate,
        'sampling_frequency': SAMPLING_FREQUENCY,
    }
Esempio n. 2
0
def load_data(epoch_key):
    logger.info('Loading data...')
    time = get_trial_time(epoch_key, ANIMALS)
    time = (pd.Series(np.ones_like(time, dtype=np.float),
                      index=time).resample('2ms').mean().index)

    def _time_function(*args, **kwargs):
        return time

    position_info = (get_interpolated_position_dataframe(
        epoch_key, ANIMALS,
        _time_function).dropna(subset=['linear_position', 'speed']))

    time = position_info.index
    speed = position_info['speed']
    position_boundaries = get_position_boundaries(position_info)

    neuron_info = make_neuron_dataframe(ANIMALS).xs(epoch_key,
                                                    drop_level=False)
    spikes = get_spikes(neuron_info, _time_function)

    tetrode_info = make_tetrode_dataframe(ANIMALS, epoch_key=epoch_key)

    track_graph, _ = make_track_graph(epoch_key, ANIMALS)

    logger.info('Finding multiunit high synchrony events...')
    adhoc_multiunit = get_adhoc_multiunit(speed, tetrode_info, _time_function)

    logger.info('Finding ripple times...')
    adhoc_ripple = get_adhoc_ripple(epoch_key, tetrode_info, time)

    logger.info('Estimating gamma power...')
    gamma_power = estimate_gamma_power(time, tetrode_info)

    logger.info('Estimating theta power...')
    theta_power = estimate_theta_power(time, tetrode_info)

    return {
        'position_info': position_info,
        'tetrode_info': tetrode_info,
        'neuron_info': neuron_info,
        'spikes': spikes,
        'track_graph': track_graph,
        'sampling_frequency': SAMPLING_FREQUENCY,
        **position_boundaries,
        **adhoc_ripple,
        **adhoc_multiunit,
        **gamma_power,
        **theta_power,
    }
Esempio n. 3
0
def get_adhoc_ripple(epoch_key, tetrode_info, position_time,
                     position_to_linearize):
    LFP_SAMPLING_FREQUENCY = 1500

    # Get speed in terms of the LFP time
    time = get_trial_time(epoch_key, ANIMALS)
    position_df = get_position_info(epoch_key, skip_linearization=True)
    new_index = pd.Index(np.unique(np.concatenate((position_df.index, time))),
                         name='time')
    position_df = (position_df.reindex(index=new_index).interpolate(
        method='linear').reindex(index=time))
    speed_feature = position_to_linearize[0].split('_')[0]
    speed = position_df[f'{speed_feature}_vel']

    # Load LFPs
    tetrode_keys = tetrode_info.loc[tetrode_info.area.isin(['ca1R',
                                                            'ca1L'])].index
    ripple_lfps = get_LFPs(tetrode_keys, ANIMALS)

    # Get ripple filtered LFPs
    ripple_filtered_lfps = pd.DataFrame(filter_ripple_band(
        np.asarray(ripple_lfps)),
                                        index=ripple_lfps.index)

    # Get Ripple Times
    ripple_times = Kay_ripple_detector(
        time=ripple_filtered_lfps.index,
        filtered_lfps=ripple_filtered_lfps.values,
        speed=speed.values,
        sampling_frequency=LFP_SAMPLING_FREQUENCY,
        zscore_threshold=2.0,
        close_ripple_threshold=np.timedelta64(0, 'ms'),
        minimum_duration=np.timedelta64(15, 'ms'))

    ripple_times.index = ripple_times.index.rename('replay_number')
    ripple_labels = get_labels(ripple_times, position_time)
    is_ripple = ripple_labels > 0
    ripple_times = ripple_times.assign(
        duration=lambda df: (df.end_time - df.start_time).dt.total_seconds())

    # Estmate ripple band power and change
    ripple_consensus_trace = pd.DataFrame(get_ripple_consensus_trace(
        ripple_filtered_lfps, LFP_SAMPLING_FREQUENCY),
                                          index=ripple_filtered_lfps.index,
                                          columns=['ripple_consensus_trace'])
    ripple_consensus_trace_zscore = pd.DataFrame(
        zscore(ripple_consensus_trace, nan_policy='omit'),
        index=ripple_filtered_lfps.index,
        columns=['ripple_consensus_trace_zscore'])

    instantaneous_ripple_power = np.full_like(ripple_filtered_lfps, np.nan)
    not_null = np.all(pd.notnull(ripple_filtered_lfps), axis=1)
    instantaneous_ripple_power[not_null] = get_envelope(
        np.asarray(ripple_filtered_lfps)[not_null])**2
    instantaneous_ripple_power_change = np.nanmedian(
        instantaneous_ripple_power /
        np.nanmean(instantaneous_ripple_power, axis=0),
        axis=1)
    instantaneous_ripple_power_change = pd.DataFrame(
        instantaneous_ripple_power_change,
        index=ripple_filtered_lfps.index,
        columns=['instantaneous_ripple_power_change'])

    return dict(
        ripple_times=ripple_times,
        ripple_labels=ripple_labels,
        ripple_filtered_lfps=ripple_filtered_lfps,
        ripple_consensus_trace=ripple_consensus_trace,
        ripple_lfps=ripple_lfps,
        ripple_consensus_trace_zscore=ripple_consensus_trace_zscore,
        instantaneous_ripple_power_change=instantaneous_ripple_power_change,
        is_ripple=is_ripple)
Esempio n. 4
0
def load_data(epoch_key, brain_areas=['CA1', 'CA2', 'CA3']):

    time = get_trial_time(epoch_key, ANIMALS)
    time = (pd.Series(np.ones_like(time, dtype=np.float), index=time)
            .resample('2ms').mean()
            .index)

    def _time_function(*args, **kwargs):
        return time

    logger.info('Loading position info...')
    position_info = (
        get_interpolated_position_dataframe(
            epoch_key, ANIMALS, _time_function)
        .dropna(subset=['linear_position', 'speed']))

    time = position_info.index

    tetrode_info = make_tetrode_dataframe(ANIMALS, epoch_key=epoch_key)
    is_brain_areas = (
        tetrode_info.area.astype(str).str.upper().isin(brain_areas))
    tetrode_keys = tetrode_info.loc[is_brain_areas].index
    lfps = get_LFPs(tetrode_keys, ANIMALS)
    lfps = lfps.resample('2ms').mean().fillna(method='pad').reindex(time)

    logger.info('Loading spikes...')
    try:
        neuron_info = make_neuron_dataframe(ANIMALS).xs(
            epoch_key, drop_level=False)
        neuron_info = neuron_info.loc[
            (neuron_info.numspikes > 100) &
            neuron_info.area.isin(brain_areas) &
            (neuron_info.type == 'principal')]
        spikes = get_all_spike_indicators(
            neuron_info.index, ANIMALS, _time_function).reindex(time)
    except KeyError:
        spikes = None

    logger.info('Loading multiunit...')
    tetrode_info = tetrode_info.loc[is_brain_areas]
    multiunit = (get_all_multiunit_indicators(
        tetrode_info.index, ANIMALS, _time_function)
        .reindex({'time': time}))

    multiunit = multiunit.sel(features=MARKS)
    multiunit_spikes = (np.any(~np.isnan(multiunit.values), axis=1)
                        ).astype(np.float)
    multiunit_firing_rate = pd.DataFrame(
        get_multiunit_population_firing_rate(
            multiunit_spikes, SAMPLING_FREQUENCY), index=time,
        columns=['firing_rate'])

    logger.info('Finding ripple times...')
    (ripple_times, ripple_filtered_lfps, ripple_lfps,
     ripple_consensus_trace_zscore) = get_ripple_times(epoch_key)

    ripple_times = ripple_times.assign(
        duration=lambda df: (df.end_time - df.start_time).dt.total_seconds())

    return {
        'position_info': position_info,
        'ripple_times': ripple_times,
        'spikes': spikes,
        'multiunit': multiunit,
        'lfps': lfps,
        'tetrode_info': tetrode_info,
        'ripple_filtered_lfps': ripple_filtered_lfps,
        'ripple_lfps': ripple_lfps,
        'ripple_consensus_trace_zscore': ripple_consensus_trace_zscore,
        'multiunit_firing_rate': multiunit_firing_rate,
        'sampling_frequency': SAMPLING_FREQUENCY,
    }