예제 #1
0
def combine_files(files, *dates, output='df'):
    dn = {}
    filtered_files = [file for file in files for date in dates if date in file]
    for i, f in enumerate(filtered_files):
        d = h5py.File(f)
        if output == 'df':
            dn['df_{}'.format(i)] = create_df(d)
        else:
            dn['df_{}'.format(i)] = d
    return dn
예제 #2
0
def plot_opto_masking(d, ignoreNoRespAfter=None):
    matplotlib.rcParams['pdf.fonttype'] = 42

    # create dataframe of session
    df = create_df(d)
    mouse = df.mouse
    framerate = df.framerate
    date = get_dates(df.date)

    if ignoreNoRespAfter is not None:
        end = ignore_after(d, ignoreNoRespAfter)[0]
        df = df[:end]

    df['respDir'] = d['trialResponseDir'][:len(df)]

    # separate out the target only, mask only, catch only, and masking trials
    # (we are using the averages of these, not left/right)
    # remove the ignore trials

    # NO OPTO TRIALS

    maskOnly = df[(df['trialType'] == 'maskOnly')
                  & (df['ignoreTrial'] == False)]
    targetOnly = df[(df['trialType'] == 'targetOnly')
                    & (df['ignoreTrial'] == False)]
    masking = df[(df['trialType'] == 'mask') & (df['ignoreTrial'] == False)]
    catch = df[(df['trialType'] == 'catch') & (df['ignoreTrial'] == False)]

    hitsNoOpto = [[] for i in range(4)]  # list for each df above
    respsNoOpto = [[] for i in range(4)]
    totalsNoOpto = [[] for i in range(4)]

    for i, frame in enumerate([maskOnly, targetOnly, masking, catch]):

        hitsNoOpto[i].append(np.sum((frame['resp'] == 1)))
        respsNoOpto[i].append(np.sum((np.isfinite(frame['respDir']))))
        totalsNoOpto[i].append(len(frame))

    totalTrialsNoOpto = np.array(
        [len(frame) for frame in [maskOnly, targetOnly, masking, catch]])

    hitsNoOpto = np.squeeze(np.array(hitsNoOpto))
    respsNoOpto = np.squeeze(np.array(respsNoOpto))
    totalsNoOpto = np.squeeze(np.array(totalsNoOpto))

    # OPTO TRIALS

    maskOnlyOpto = df[(df['trialType'] == 'maskOnlyOpto')
                      & (df['ignoreTrial'] == False)]
    targetOnlyOpto = df[(df['trialType'] == 'targetOnlyOpto')
                        & (df['ignoreTrial'] == False)]
    maskingOpto = df[(df['trialType'] == 'maskOpto')
                     & (df['ignoreTrial'] == False)]
    catchOpto = df[(df['trialType'] == 'catchOpto')
                   & (df['ignoreTrial'] == False)]

    # separate out by onset and count correct by onset
    optoOnset = d['optoOnset'][:]

    hits = [[] for i in range(4)]  # list for each df above
    resps = [[] for i in range(4)]
    totals = [[] for i in range(4)]

    for i, frame in enumerate(
        [maskOnlyOpto, targetOnlyOpto, maskingOpto, catchOpto]):
        for on in optoOnset:

            hits[i].append(
                np.sum((frame['optoOnset'] == on) & (frame['resp'] == 1)))
            resps[i].append(
                np.sum((frame['optoOnset'] == on)
                       & (np.isfinite(frame['respDir']))))
            totals[i].append(np.sum(frame['optoOnset'] == on))

    totalTrialsOpto = np.array([
        len(frame)
        for frame in [maskOnlyOpto, targetOnlyOpto, maskingOpto, catchOpto]
    ])

    hits = np.squeeze(np.array(hits))
    resps = np.squeeze(np.array(resps))
    totals = np.squeeze(np.array(totals))

    # plot the percent correct against the opto onset on the xaxis

    xNoOpto = max(optoOnset) + 1
    yText = [1.2, 1.15, 1.1, 1.05]
    colors = ['k', 'c', 'k', 'm']
    alphas = [.3, .5, .8, 1]

    for num, denom, title in zip(
        [resps, hits], [totals, resps],
        ['Response Rate', 'Fraction Correct Given Response']):

        fig, ax = plt.subplots()
        for i, (al, c, text, lbl, noOptoResp, noOptoCorr) in enumerate(
                zip(alphas, colors, yText,
                    ['Mask only', 'Target only', 'Masked trials', 'Catch'],
                    list(respsNoOpto / totalsNoOpto),
                    list(hitsNoOpto / respsNoOpto))
        ):  # this needs to be resps over totals for no opto

            #for fraction correct there should only be target and masked plotted
            # or the plot needs to be different, like the ones sam made
            if title == 'Response Rate':

                ax.plot(optoOnset,
                        num[i] / denom[i],
                        'o-',
                        color=c,
                        lw=3,
                        alpha=al,
                        label=lbl)
                ax.plot(xNoOpto, noOptoResp, 'o', color=c, alpha=al)

                for x, n in zip(optoOnset, np.transpose(denom[i])):
                    fig.text(x,
                             text,
                             str(n),
                             transform=ax.transData,
                             color=c,
                             alpha=al,
                             fontsize=10,
                             ha='center',
                             va='bottom')

            else:
                if lbl == 'Mask only' or lbl == 'Catch':
                    pass
                else:
                    ax.plot(optoOnset,
                            num[i] / denom[i],
                            'o-',
                            color=c,
                            lw=3,
                            alpha=al,
                            label=lbl)
                    ax.plot(xNoOpto, noOptoCorr, 'o', color=c, alpha=al)

                    for x, n in zip(optoOnset, np.transpose(denom[i])):
                        fig.text(x,
                                 text,
                                 str(n),
                                 transform=ax.transData,
                                 color=c,
                                 alpha=al,
                                 fontsize=10,
                                 ha='center',
                                 va='bottom')
#                    for r in respsNoOpto[1:3]:
#                        fig.text(xNoOpto, text, str(r), transform=ax.transData, color=c, alpha=al, fontsize=10, ha='center', va='bottom')
#

        if title == 'Response Rate':
            for t, y, cl, alp in zip(totalsNoOpto, yText, colors, alphas):
                fig.text(xNoOpto,
                         y,
                         str(t),
                         transform=ax.transData,
                         color=cl,
                         alpha=alp,
                         fontsize=10,
                         ha='center',
                         va='bottom')

        elif title == 'Fraction Correct Given Response':
            for r, y, cl, alp in zip(respsNoOpto[1:3], yText[1:3], colors[1:3],
                                     alphas[1:3]):
                fig.text(xNoOpto,
                         y,
                         str(r),
                         transform=ax.transData,
                         color=cl,
                         alpha=alp,
                         fontsize=10,
                         ha='center',
                         va='bottom')

    ## format the plots

        formatFigure(fig,
                     ax,
                     xLabel='Opto onset from target onset (ms)',
                     yLabel=title)

        fig.suptitle((mouse + ',  ' + date + '       ' +
                      'Masking with optogenetic silencing of V1'),
                     fontsize=13)

        xticks = list(optoOnset)

        xticklabels = [
            int(np.round((tick / framerate) * 1000)) - 17 for tick in optoOnset
        ]  # remove 17 ms of latency
        x, lbl = ([xNoOpto], ['no\nopto'])
        xticks = np.concatenate((xticks, x))
        xticklabels = xticklabels + lbl

        ax.set_xticks(xticks)
        ax.set_xticklabels(xticklabels)

        ax.set_xlim([min(optoOnset) - 1, max(xticks) + 1])

        ax.set_ylim([0, 1.05])
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.tick_params(direction='out', top=False, right=False)
        plt.subplots_adjust(top=0.815,
                            bottom=0.135,
                            left=0.1,
                            right=0.92,
                            hspace=0.2,
                            wspace=0.2)
        plt.legend(loc='best', fontsize='small', numpoints=1)
예제 #3
0
@author: chelsea.strawder
"""

import fileIO, h5py
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from nogoData import nogo_turn
from dataAnalysis import import_data, create_df

matplotlib.rcParams['pdf.fonttype'] = 42

#def plot_by_param(param):    #turn into callable function - IN dataAnalysis?
df = create_df(import_data())
param = 'targetLength'

nonzeroRxns = df[(df['trialLength'] != df['trialLength'].max())
                 & (df['ignoreTrial'] != True) & (df['resp'] != 0)]
corrNonzero = nonzeroRxns[(nonzeroRxns['resp'] == 1)
                          & (nonzeroRxns['rewDir'] != 0)]
missNonzero = nonzeroRxns[(nonzeroRxns['resp'] == -1)
                          & (nonzeroRxns['rewDir'] != 0)]

miss = missNonzero.pivot_table(values='trialLength',
                               index='{}'.format(param),
                               columns='rewDir',
                               margins=True,
                               dropna=True)
hit = corrNonzero.pivot_table(values='trialLength',
예제 #4
0
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 16:24:15 2020

@author: chelsea.strawder
"""

from dataAnalysis import import_data, create_df
import matplotlib.pyplot as plt
import numpy as np

d = import_data()
df = create_df(d)

trialStart = d['trialStartFrame'][:]
trialEnd = d['trialEndFrame'][:]
wheelPos = d['deltaWheelPos'][:]

bigWheel = [
    i for i, (start, end) in enumerate(zip(trialStart, trialEnd))
    if abs(np.max(wheelPos[start:end])) > .2
]

for i in bigWheel[:10]:
    plt.figure()
    plt.plot(wheelPos[trialStart[i]:trialEnd[i]], label='wheel')
    plt.plot(df.loc[i, 'trialFrameIntervals'], label='fi')
    plt.title(df.mouse + ' ' + str(i))
    for j, x in enumerate(df.loc[i, 'trialFrameIntervals']):
        if x > .016:
            plt.vlines(j, -.5, .5, ls='--')
예제 #5
0
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 15:09:56 2020

@author: chelsea.strawder
"""

from dataAnalysis import import_data, create_df
import scipy.stats as stats

dn = {}
for i, f in enumerate(files[-3:]):              #change index for desired files
    d = h5py.File(f) 
    dn['df_{}'.format(i)] = create_df(d)        #creates keys in dict dn with format df_#

dictget = lambda x, *k: [x[i] for i in k]
df1, df2, df3 = dictget(dn, 'df_0', 'df_1', 'df_2')    # assigns each dataframe to a new variable - essentially unpacks dict dn

dfall = pd.concat([df1,df2,df3], ignore_index=True)



def extract_vars(data):
    return {key: data[str(key)][()] for key in data.keys()}
    
def create_vars(dn):
    for key,val in dn.items():   #work in progress, if even possible
        globals()
        exec (key + '= val')

# IN PLOTTING BY PARAM