def main():

    (raw_data_path, _, _, figure_path, simulated_data_path) = cf.path_config()
    learning_signals_fn = 'learning_signals.csv'
    base_fn = 'sub-*_cond-*_learning_signals.csv'

    (unique_subjects, unique_sessions,
     unique_reward_codes) = md.extract_subjects_sessions(raw_data_path,
                                                         reward_task=1)

    start_time = time.time()

    learning_signals_df = pd.read_csv(
        os.path.join(simulated_data_path, learning_signals_fn))

    n_trials = 400
    n_subjects = learning_signals_df.subj_id.nunique()
    expected_len = n_trials - 2

    unique_reward_codes = np.repeat(
        learning_signals_df.reward_code.sort_values(ascending=True).unique(),
        n_subjects)
    unique_reward_values = np.repeat(
        [6510, 6520, 6530, 7510, 7520, 7530, 8510, 8520, 8530], n_subjects)

    def split(df, group):
        gb = df.groupby(group)
        return [gb.get_group(x) for x in gb.groups]

    def remove_first_last_trials(df_list, expected_len=expected_len):

        df_lens = []

        sliced_dfs = [df.iloc[1:-1] for df in df_list]
        [df_lens.append(len(df))
         for df in sliced_dfs]  # check lengths are expected

        assert np.unique(
            df_lens) == expected_len, 'check length of sliced dfs!'

        return sliced_dfs

    def extract_fns(df_list):

        fns = [('sub-' + str(df.subj_id.unique()[0]) + '_cond-' +
                str(df.reward_code.unique()[0]) + '_learning_signals.csv')
               for df in df_list]

        return fns

    def save_split_dfs(df_list, fn_list, data_path=simulated_data_path):

        [
            df.to_csv(os.path.join(data_path, fn), index=False)
            for df, fn in zip(df_list, fn_list)
        ]

        return print('dfs saved')

    def decode_condition(base_fn=base_fn,
                         data_path=simulated_data_path,
                         reward_values=unique_reward_values,
                         reward_codes=unique_reward_codes):
        def get_cond(elem):
            return elem[-22:-21]  # get the condition code for each file

        learning_signals_fns = glob.glob(os.path.join(data_path, base_fn))
        learning_signals_fns.sort(key=get_cond)

        [
            print(reward_code, reward_val,
                  fn) for fn, reward_val, reward_code in zip(
                      learning_signals_fns, reward_values, unique_reward_codes)
        ]

        learning_signals_fns_decoded = [
            re.sub('cond-' + str(reward_code), 'cond-' + str(reward_val), fn)
            for fn, reward_val, reward_code in zip(
                learning_signals_fns, reward_values, unique_reward_codes)
        ]

        [
            os.rename(original_fn,
                      decoded_fn) for original_fn, decoded_fn in zip(
                          learning_signals_fns, learning_signals_fns_decoded)
        ]

        os.listdir(simulated_data_path)  # check the renaming

        return None

    dfs = split(learning_signals_df, ['subj_id', 'reward_code'])
    sliced_dfs = remove_first_last_trials(dfs)
    parsed_learning_signals_fns = extract_fns(sliced_dfs)

    save_split_dfs(sliced_dfs, parsed_learning_signals_fns)
    decode_condition()

    end_time = time.time()

    time_elapsed = end_time - start_time
    print('time elapsed: ', time_elapsed)
Esempio n. 2
0
def main():

    (raw_data_path, intermediate_data_path, processed_data_path,
     figure_path) = cf.path_config()

    (unique_subjects, unique_sessions,
     unique_reward_codes) = md.extract_subjects_sessions(raw_data_path,
                                                         reward_task=1)

    start_time = time.time()

    for subj_id in unique_subjects:
        for session_n in unique_sessions:

            print('bandpass filtering data for subject {}'.format(subj_id) +
                  ' session {}'.format(session_n))

            _, _, reward_code = ep.find_data_files(subj_id=subj_id,
                                                   session_n=session_n,
                                                   reward_task=1,
                                                   lum_task=0,
                                                   raw_data_path=raw_data_path)

            reward_samples = ep.read_hdf5('samples',
                                          subj_id,
                                          session_n,
                                          processed_data_path,
                                          reward_code=reward_code,
                                          id_str='clean')
            reward_messages = ep.read_hdf5('messages',
                                           subj_id,
                                           session_n,
                                           processed_data_path,
                                           reward_code=reward_code,
                                           id_str='clean')
            reward_events = ep.read_hdf5('events',
                                         subj_id,
                                         session_n,
                                         processed_data_path,
                                         reward_code=reward_code,
                                         id_str='clean')

            reward_samples = bp.high_bandpass_filter(reward_samples)
            reward_samples = bp.low_bandpass_filter(reward_samples)

            lp_fig, lp_figname = vz.visualize(
                reward_samples.trial_sample,
                reward_samples.lowpass_pupil_diameter,
                subj_id,
                session_n,
                reward_code,
                id_str='lowpass')
            vz.save(lp_fig, lp_figname)
            hp_fig, hp_figname = vz.visualize(
                reward_samples.trial_sample,
                reward_samples.highpass_pupil_diameter,
                subj_id,
                session_n,
                reward_code,
                id_str='highpass')
            vz.save(hp_fig, hp_figname)

            hdf = ep.save_hdf5(reward_samples,
                               reward_events,
                               reward_messages,
                               subj_id,
                               session_n,
                               processed_data_path,
                               reward_code=reward_code,
                               id_str='bandpass')

    end_time = time.time()

    time_elapsed = end_time - start_time
    print('time elapsed: ', time_elapsed)
Esempio n. 3
0
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

import os
import pandas as pd


import time

plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['font.family'] = 'DejaVu Sans'

(raw_data_path, intermediate_data_path,
processed_data_path, figure_path, simulated_data_path) = cf.path_config()

cf.plot_config()


def calc_peaks(samples, width=100):

    """ Find the peak of the pupillary response within the trial. """
    peak_idx, _ = find_peaks(samples.z_pupil_diameter, width=width)
    samples['peak_samples'] = np.nan

    if peak_idx.any():
        peaks = samples.z_pupil_diameter.iloc[peak_idx]
        max_peak_idx = peaks.idxmax()
        samples['peak_samples'][max_peak_idx] = samples.z_pupil_diameter[max_peak_idx]
        print('peak values ', samples.z_pupil_diameter[max_peak_idx],
Esempio n. 4
0
from pupil_parse.preprocess_utils import deblink_delimit as dd

import time

# from pupil_parse.preprocess_utils import bandpass_filter as bp

from scipy.signal import find_peaks
import numpy as np

import seaborn as sns
import matplotlib.pyplot as plt

subj_id, session_n = 789, 3

(raw_data_path, intermediate_data_path, processed_data_path,
 figure_path) = cf.path_config()

subj_data_file, subj_data_file_raw, reward_code = ep.find_data_files(
    subj_id=subj_id,
    session_n=session_n,
    reward_task=1,
    lum_task=0,
    raw_data_path=raw_data_path)

reward_samples = ep.read_hdf5('samples',
                              subj_id,
                              session_n,
                              intermediate_data_path,
                              reward_code=reward_code)
print('samples')
reward_messages = ep.read_hdf5('messages',
def main():

    (raw_data_path, intermediate_data_path, processed_data_path,
     figure_path) = cf.path_config()

    (unique_subjects, unique_sessions,
     unique_reward_codes) = md.extract_subjects_sessions(raw_data_path,
                                                         reward_task=1)

    start_time = time.time()

    lp_min = []
    lp_max = []

    hp_min = []
    hp_max = []

    for subj_id in unique_subjects:
        for session_n in unique_sessions:

            print('bandpass filtering data for subject {}'.format(subj_id) +
                  ' session {}'.format(session_n))

            _, _, reward_code = ep.find_data_files(subj_id=subj_id,
                                                   session_n=session_n,
                                                   reward_task=1,
                                                   lum_task=0,
                                                   raw_data_path=raw_data_path)

            reward_samples = ep.read_hdf5('samples',
                                          subj_id,
                                          session_n,
                                          processed_data_path,
                                          reward_code=reward_code,
                                          id_str='corr')
            reward_messages = ep.read_hdf5('messages',
                                           subj_id,
                                           session_n,
                                           processed_data_path,
                                           reward_code=reward_code,
                                           id_str='corr')
            reward_events = ep.read_hdf5('events',
                                         subj_id,
                                         session_n,
                                         processed_data_path,
                                         reward_code=reward_code,
                                         id_str='corr')

            reward_samples = bp.high_bandpass_filter(reward_samples)
            reward_samples = bp.low_bandpass_filter(reward_samples)

            lp_min.append(np.nanmin(reward_samples.lowpass_pupil_diameter))
            lp_max.append(np.nanmax(reward_samples.lowpass_pupil_diameter))

            hp_min.append(np.nanmin(reward_samples.highpass_pupil_diameter))
            hp_max.append(np.nanmax(reward_samples.highpass_pupil_diameter))

    end_time = time.time()

    time_elapsed = end_time - start_time
    print('time elapsed: ', time_elapsed)

    print('min lowpass values: ', np.nanmin(lp_min))
    print('max lowpass values: ', np.nanmax(lp_max))

    print('min highpass values: ', np.nanmin(hp_min))
    print('max highpass values: ', np.nanmax(hp_max))
import os
import re
from glob import glob
import numpy as np
from collections import Counter

from pupil_parse.preprocess_utils import config as cf

(raw_data_path, intermediate_data_path,
_, _, _) = cf.path_config()

def extract_subjects_sessions(raw_data_path, reward_task=0, lum_task=0,
session_max=9, session_min=1, n_subjects=4, n_runs=None):
    """Find the metadata for each session."""

    if reward_task:
        task_str = '[0-9]'*4
    if lum_task:
        task_str = 'lum'
        session_max = session_max + 1
        session_min = session_min - 1
    elif not reward_task or lum_task:
        raise ValueError('The task must be specified as either\
        the reinforcement learning task or the luminance range task.\
        See the arguments for this function.')

    subj_data_file_list = glob(raw_data_path + 'sub-*' +
     '/' + 'ses-*' + '/' + 'pupil' + '/' +
    'sub-*' + '_' + 'ses-*' +
    '_task-' + task_str + '*.EDF')
Esempio n. 7
0
def main():

    (raw_data_path, intermediate_data_path, processed_data_path,
     figure_path) = cf.path_config()

    (unique_subjects, unique_sessions,
     unique_reward_codes) = md.extract_subjects_sessions(raw_data_path,
                                                         reward_task=1)

    start_time = time.time()

    for subj_id in unique_subjects:
        for session_n in unique_sessions:

            print('processing subject {}'.format(subj_id) +
                  ' session {}'.format(session_n))

            time.sleep(1)

            _, _, reward_code = ep.find_data_files(subj_id=subj_id,
                                                   session_n=session_n,
                                                   reward_task=1,
                                                   lum_task=0,
                                                   raw_data_path=raw_data_path)

            reward_samples = ep.read_hdf5('samples',
                                          subj_id,
                                          session_n,
                                          intermediate_data_path,
                                          reward_code=reward_code,
                                          id_str='seg')
            reward_messages = ep.read_hdf5('messages',
                                           subj_id,
                                           session_n,
                                           intermediate_data_path,
                                           reward_code=reward_code,
                                           id_str='seg')
            reward_events = ep.read_hdf5('events',
                                         subj_id,
                                         session_n,
                                         intermediate_data_path,
                                         reward_code=reward_code,
                                         id_str='seg')

            deblinked_reward_samples = dd.deblink(reward_samples,
                                                  reward_events)
            print('deblinking complete')
            outlier_removed_reward_samples = dd.outlier_removal(
                deblinked_reward_samples)
            print('outliers removed')
            interpolated_reward_samples = dd.interpolate(
                outlier_removed_reward_samples)
            print('data interpolated')

            hdf = ep.save_hdf5(interpolated_reward_samples,
                               reward_events,
                               reward_messages,
                               subj_id,
                               session_n,
                               processed_data_path,
                               reward_code=reward_code,
                               id_str='clean')
            print('clean data saved')

    end_time = time.time()

    time_elapsed = end_time - start_time
    print('time elapsed: ', time_elapsed)