Example #1
0
def load_from_filterframework(animal,
                              datatype,
                              filterframework_dir,
                              index_keys=[]):
    if type(index_keys) != list:
        index_keys = [
            index_keys,
        ]
    animal_dict = {}
    animal_dict[animal] = lfdp.Animal(directory=filterframework_dir,
                                      short_name=animal)

    if datatype == 'ntrodeInfo':
        out = lfdp.make_tetrode_dataframe(animal_dict)
        out['subarea'] = out['subarea'].astype(str)

    elif datatype == 'taskInfo':
        out = lfdp.make_epochs_dataframe(animal_dict)

    elif datatype == 'linearcoord' or datatype == 'task_segments':
        out = lfdp.make_epochs_dataframe(animal_dict)
        out = _get_linearcoord_tasksegments(out, animal_dict)

    elif datatype == 'position':
        if len(index_keys[0]) != 3:
            print('epoch_keys requred as list of (animal, day, epoch)')
            return
        position_dict_df = {}
        for (animal, day, epoch) in index_keys:
            epoch_index = (animal, day, epoch)
            position_dict_df[(animal, day,
                              epoch)] = lfdp.get_position_dataframe(
                                  epoch_index, animal_dict)
        out = pd.concat(position_dict_df).reset_index().rename(
            {
                'level_0': 'animal',
                'level_1': 'day',
                'level_2': 'epoch'
            },
            axis=1)
        out['timedelta'] = pd.TimedeltaIndex(out['time'], unit='ns')
        out.set_index(['animal', 'day', 'epoch', 'timedelta'], inplace=True)
        out['is_correct'] = out.is_correct.astype('float')

    elif datatype == 'lfp':
        if len(index_keys[0]) != 4:
            print(
                'ntrode_keys requred as list of (animal, day, epoch, ntrode)')
            return
        out = _load_lfp_from_filterframework(index_keys, animal_dict)

    if 'tetrode_number' in out.index.names:
        print('renaming "tetrode_number" to "ntrode" in multiindex')
        out.index.rename('ntrode', level='tetrode_number', inplace=True)

    return out
Example #2
0
def get_track_segments(epoch_key, animals):
    '''

    Parameters
    ----------
    epoch_key : tuple
    animals : dict of namedtuples

    Returns
    -------
    track_segments : ndarray, shape (n_segments, n_nodes, n_space)
    center_well_position : ndarray, shape (n_space,)

    '''
    environment = np.asarray(
        make_epochs_dataframe(animals).loc[epoch_key].environment)[0]
    ENVIRONMENTS = {'lineartrack': 'linearTrack', 'wtrack': 'wTrack'}

    coordinate_path = os.path.join(
        animals[epoch_key[0]].directory,
        f'{ENVIRONMENTS[environment]}_coordinates.mat')
    linearcoord = loadmat(coordinate_path)['coords'][0]
    track_segments = [
        np.stack(((arm[:-1, :, 0], arm[1:, :, 0])), axis=1)
        for arm in linearcoord
    ]
    track_segments = np.concatenate(track_segments)
    _, unique_ind = np.unique(track_segments, return_index=True, axis=0)
    return track_segments[np.sort(unique_ind)]
Example #3
0
def main():
    # Set the maximum number of threads for openBLAS to use.
    NUM_THREADS = 16
    environ['OPENBLAS_NUM_THREADS'] = str(NUM_THREADS)
    environ['NUMBA_NUM_THREADS'] = str(NUM_THREADS)
    environ['OMP_NUM_THREADS'] = str(NUM_THREADS)
    environ['MKL_NUM_THREADS'] = str(NUM_THREADS)
    LOG_DIRECTORY = join(getcwd(), 'logs')
    makedirs(LOG_DIRECTORY,  exist_ok=True)

    python_function = 'run_by_epoch.py'
    directives = ' '.join(
        ['-l h_rt=36:00:00', f'-pe omp {NUM_THREADS}',
         '-P braincom', '-notify', '-l mem_total=125G',
         '-v OPENBLAS_NUM_THREADS', '-v NUMBA_NUM_THREADS',
         '-v OMP_NUM_THREADS', '-v MKL_NUM_THREADS'])

    args = get_command_line_arguments()
    if args.Animal is None and args.Day is None and args.Epoch is None:
        epoch_info = make_epochs_dataframe(ANIMALS)
        neuron_info = make_neuron_dataframe(ANIMALS)
        n_neurons = (neuron_info
                     .groupby(['animal', 'day', 'epoch'])
                     .neuron_id
                     .agg(len)
                     .rename('n_neurons')
                     .to_frame())

        epoch_info = epoch_info.join(n_neurons)
        is_w_track = (epoch_info.environment
                      .isin(['TrackA', 'TrackB', 'WTrackA', 'WTrackB']))
        epoch_keys = epoch_info[is_w_track & (epoch_info.n_neurons > 20)].index
    else:
        epoch_keys = [(args.Animal, args.Day, args.Epoch)]

    for animal, day, epoch in epoch_keys:
        print(f'Animal: {animal}, Day: {day}, Epoch: {epoch}')

        log_file = f'{animal}_{day:02d}_{epoch:02d}.log'
        function_name = python_function.replace('.py', '')
        job_name = f'{function_name}_{animal}_{day:02d}_{epoch:02d}'
        python_cmd = f'{python_function} {animal} {day} {epoch}'
        queue_job(python_cmd,
                  directives=directives,
                  log_file=join(LOG_DIRECTORY, log_file),
                  job_name=job_name)
Example #4
0
def main():
    # Set the maximum number of threads for openBLAS to use.
    NUM_THREADS = 16
    environ['OPENBLAS_NUM_THREADS'] = str(NUM_THREADS)
    environ['NUMBA_NUM_THREADS'] = str(NUM_THREADS)
    environ['OMP_NUM_THREADS'] = str(NUM_THREADS)
    log_directory = join(getcwd(), 'logs')
    makedirs(log_directory,  exist_ok=True)

    python_function = 'run_by_epoch.py'
    directives = ' '.join(
        ['-l h_rt=1:00:00', '-pe omp {0}'.format(NUM_THREADS),
         '-P braincom', '-notify', '-l mem_total=125G',
         '-v OPENBLAS_NUM_THREADS', '-v NUMBA_NUM_THREADS',
         '-v OMP_NUM_THREADS'])

    args = get_command_line_arguments()
    if args.Animal is None and args.Day is None and args.Epoch is None:
        epoch_info = make_epochs_dataframe(ANIMALS)
        epoch_keys = epoch_info[(epoch_info.type == 'run') & (
            epoch_info.environment != 'lin')].index
    else:
        epoch_keys = [(args.Animal, args.Day, args.Epoch)]

    for (animal, day, epoch_ind) in epoch_keys:
        print('Animal: {0}, Day: {1}, Epoch: {2}'.format(
            animal, day, epoch_ind))
        log_file = '{animal}_{day:02d}_{epoch:02d}.log'.format(
            animal=animal, day=day, epoch=epoch_ind)
        job_name = (
            '{function_name}_{animal}_{day:02d}_{epoch:02d}').format(
            animal=animal, day=day, epoch=epoch_ind,
            function_name=python_function.replace('.py', ''))
        python_cmd = '{python_function} {animal} {day} {epoch}'.format(
            python_function=python_function, animal=animal, day=day,
            epoch=epoch_ind)
        queue_job(python_cmd,
                  directives=directives,
                  log_file=join(log_directory, log_file),
                  job_name=job_name)
Example #5
0
def get_position_occupancy(epoch_key,
                           animals,
                           extent=(0, 300, 0, 300),
                           gridsize=(30, 30)):
    position_info = get_interpolated_position_dataframe(
        epoch_key, animals, max_distance_from_well=5)

    occupancy = plt.hexbin(position_info.x_position,
                           position_info.y_position,
                           extent=extent,
                           gridsize=gridsize)
    occupancy_count = pd.DataFrame({
        'occupancy_count':
        occupancy.get_array(),
        'center_x':
        occupancy.get_offsets()[:, 0],
        'center_y':
        occupancy.get_offsets()[:, 1],
        'environment':
        str(make_epochs_dataframe(animals).xs(epoch_key).environment)
    })
    (occupancy_count['animal'], occupancy_count['day'],
     occupancy_count['epoch']) = epoch_key
    return occupancy_count
"""

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.signal as scis

from loren_frank_data_processing import (get_interpolated_position_dataframe,
                                         get_LFP_dataframe,
                                         get_spike_indicator_dataframe,
                                         make_epochs_dataframe,
                                         make_neuron_dataframe,
                                         make_tetrode_dataframe)
from src.parameters import ANIMALS

epoch_info = make_epochs_dataframe(ANIMALS)

epoch_key = ('HPa', 3, 2)
tetrode_info = make_tetrode_dataframe(ANIMALS).xs(epoch_key, drop_level=False)
tetrode_key = ('HPa', 3, 2, 4)

neuron_info = make_neuron_dataframe(ANIMALS).xs(epoch_key, drop_level=False)
neuron_key = ('HPa', 3, 2, 4, 3)

spike = get_spike_indicator_dataframe(neuron_key, ANIMALS)
position_info = get_interpolated_position_dataframe(epoch_key, ANIMALS)
linear_distance = position_info['linear_distance']
x_pos = position_info['x_position']
y_pos = position_info['y_position']
speed = position_info['speed']
head_direction = position_info['head_direction']
Example #7
0
def load_data(
    epoch_key,
    position_to_linearize=['nose_x', 'nose_y'],
    max_distance_from_well=30,
    min_distance_traveled=50,
):
    logger.info('Loading position info...')
    environment = np.asarray(
        make_epochs_dataframe(ANIMALS).loc[epoch_key].environment)[0]
    if environment == "lineartrack":
        edge_order, edge_spacing = LINEAR_EDGE_ORDER, LINEAR_EDGE_SPACING
    elif environment == "wtrack":
        edge_order, edge_spacing = WTRACK_EDGE_ORDER, WTRACK_EDGE_SPACING
    else:
        edge_order, edge_spacing = None, None
    position_info = get_interpolated_position_info(
        epoch_key,
        position_to_linearize=position_to_linearize,
        max_distance_from_well=max_distance_from_well,
        min_distance_traveled=min_distance_traveled,
        edge_order=edge_order,
        edge_spacing=edge_spacing,
    ).dropna(subset=["linear_position"])
    tetrode_info = make_tetrode_dataframe(ANIMALS, epoch_key=epoch_key)
    tetrode_keys = tetrode_info.loc[tetrode_info.area.isin(['ca1R',
                                                            'ca1L'])].index

    logger.info('Loading multiunit...')

    def _time_function(*args, **kwargs):
        return position_info.index

    adhoc_multiunit = get_adhoc_multiunit(position_info, tetrode_keys,
                                          _time_function,
                                          position_to_linearize)

    logger.info('Loading spikes...')
    time = position_info.index
    try:
        neuron_info = make_neuron_dataframe(
            ANIMALS, exclude_animals=['Monty', 'Peanut']).xs(epoch_key,
                                                             drop_level=False)
        neuron_info = neuron_info.loc[neuron_info.accepted.astype(bool)]
        spikes = get_all_spike_indicators(neuron_info.index, ANIMALS,
                                          _time_function).reindex(time)
    except (ValueError, KeyError):
        neuron_info = None
        spikes = None

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

    track_graph = make_track_graph(epoch_key, ANIMALS)

    dio = get_DIO(epoch_key, ANIMALS)
    dio_indicator = get_DIO_indicator(epoch_key,
                                      ANIMALS,
                                      time_function=_time_function)

    return {
        'position_info': position_info,
        'tetrode_info': tetrode_info,
        'neuron_info': neuron_info,
        'spikes': spikes,
        'dio': dio,
        'dio_indicator': dio_indicator,
        'track_graph': track_graph,
        'edge_order': edge_order,
        'edge_spacing': edge_spacing,
        **adhoc_ripple,
        **adhoc_multiunit,
    }