Пример #1
0
def example_date_function(date, df):
    """
    All date_functions must take date and df.

    Parameters
    ----------
    date : Date instance
    df : DataFrame

    Returns
    -------
    Updated DataFrame

    """

    df['cossim_plus'] = nannone(
        calc.distance.cosine_similarity_stimuli(date, 'plus', 'ensure'))
    df['cossim_plus_dropzero'] = nannone(
        calc.distance.cosine_similarity_stimuli(date,
                                                'plus',
                                                'ensure',
                                                drop_glm_zeros=True,
                                                rectify=True))
    df['cossim_plus_trials_max'] = nannone(
        calc.distance.cosine_similarity_stimuli(date,
                                                'plus',
                                                'ensure',
                                                compare_to_trials=True,
                                                max_across_trial=True,
                                                rectify=True))
    df['cossim_plus_trials'] = nannone(
        calc.distance.cosine_similarity_stimuli(date,
                                                'plus',
                                                'ensure',
                                                compare_to_trials=True,
                                                rectify=True))

    df['fcreward'] = calc_legacy.glm_groups.fraction(date,
                                                     'ensure-vdrive-plus')
    df['reward'] = calc_legacy.glm_groups.fraction(date, 'ensure')

    return df
Пример #2
0
def dataframe(sorter, date_function):
    """
    Iterates over sorter, running date_function and
    adding those parameters to default mouse, date, and dprime.

    Parameters
    ----------
    sorter : Sorter
        To iterate over
    date_function : function with date and dataframe as arguments
        Append to and return a dataframe from a date
        that will be concatenated.

    Returns
    -------
    dataframe

    """

    df = None
    for date in sorter:
        data = {
            'mouse': [date.mouse],
            'date': [date.date],
            'reversed': [flow.metadata.reversal(date.mouse) < date.date],
            'dprime_legacy': [calc_legacy.performance.dprime(date)],
            'dprime_pool': [calc.performance.dprime(date)],
            'react_plus':
            [nannone(calc_legacy.reactivation_rate.freq(date, 'plus'))],
            'react_neutral':
            [nannone(calc_legacy.reactivation_rate.freq(date, 'neutral'))],
            'react_minus':
            [nannone(calc_legacy.reactivation_rate.freq(date, 'minus'))],
            'react_hungry_plus': [
                nannone(
                    calc_legacy.reactivation_rate.freq(date, 'plus',
                                                       '-hungry'))
            ],
            'react_hungry_neutral': [
                nannone(
                    calc_legacy.reactivation_rate.freq(date, 'neutral',
                                                       '-hungry'))
            ],
            'react_hungry_minus': [
                nannone(
                    calc_legacy.reactivation_rate.freq(date, 'minus',
                                                       '-hungry'))
            ],
        }

        default = pd.DataFrame(data)
        date_df = date_function(date, default)

        if df is None:
            df = date_df
        else:
            df = pd.concat([df, date_df], ignore_index=True, sort=True)

    return df
Пример #3
0
def dataframe_date(run):
    """

    Parameters
    ----------
    run

    Returns
    -------

    """

    df = pd.DataFrame([run.mouse], columns=['mouse'])
    df['mouse'] = run.mouse
    df['date'] = run.date
    df['run'] = run.run
    df['dprime'] = nannone(calc.performance.dprime_run(run, across_run=False))
    df['reversed'] = run.date < flow.metadata.reversal(run.mouse)

    df['cossim_plus'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'plus',
                                                    'ensure',
                                                    rectify=False))
    df['cossim_neutral'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'neutral',
                                                    'ensure',
                                                    rectify=False))
    df['cossim_minus'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'minus',
                                                    'ensure',
                                                    rectify=False))

    df['cossim_plus_wide'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'plus',
                                                    'ensure',
                                                    rectify=False,
                                                    trange_glm=(-1, 2)))
    df['cossim_neutral_wide'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'neutral',
                                                    'ensure',
                                                    rectify=False,
                                                    trange_glm=(-1, 2)))
    df['cossim_minus_wide'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'minus',
                                                    'ensure',
                                                    rectify=False,
                                                    trange_glm=(-1, 2)))

    df['cossim_plus_0.5'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'plus',
                                                    'ensure',
                                                    rectify=False,
                                                    end_s=0.5))
    df['cossim_neutral_0.5'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'neutral',
                                                    'ensure',
                                                    rectify=False,
                                                    end_s=0.5))
    df['cossim_minus_0.5'] = nannone(
        calc.distance.cosine_similarity_stimuli_run(run,
                                                    'minus',
                                                    'ensure',
                                                    rectify=False,
                                                    end_s=0.5))

    return df
Пример #4
0
def dataframe(sorter,
              run_function,
              event_threshold=0.1,
              legacy=True,
              per_cell=False,
              deconvolved_threshold=0.2,
              trange=(-2, 3)):
    """
    Iterates over sorter, running date_function and
    adding those parameters to default mouse, date, and dprime.

    Parameters
    ----------
    sorter : Sorter
        To iterate over
    date_function : function with date and dataframe as arguments
        Append to and return a dataframe from a date
        that will be concatenated.

    Returns
    -------
    dataframe

    """

    df = None
    for run in sorter:
        data = {
            'mouse': [run.mouse],
            'date': [run.date],
            'run': [run.run],
            'reversed': [flow.metadata.reversal(run.mouse) < run.date],
            'dprime_pool': [calc.performance.dprime(run.parent)],
        }

        if legacy == True:
            data['dprime_legacy'] = [
                calc_legacy.performance.dprime(run.parent)
            ]

            for cs in config.stimuli():
                data['react_%s' % cs] = [
                    nannone(calc_legacy.reactivation_rate.freq(run.parent, cs))
                ]

        for cs in config.stimuli():
            if legacy:
                evs = calc_legacy.reactivation_rate.events(run, run.run, cs)
            else:
                evs = calc.reactivation_rate.events(run, cs, event_threshold)

            if evs is not None and len(evs) > 0:
                csdata = deepcopy(data)
                for key in csdata:
                    csdata[key] = csdata[key] * len(evs)
                csdata['frame'] = evs
                csdata['stimulus'] = [cs] * len(evs)

                csdata['reward_rich'] = np.zeros(len(evs)) > 1
                csdata['reward_poor'] = np.zeros(len(evs)) > 1
                csdata['reward_mixed'] = np.zeros(len(evs)) > 1

                if cs == 'plus':
                    if legacy:
                        rewnon = calc_legacy.reactivation_rate.event_rich_poor(
                            run, run.run)
                    else:
                        rewnon = calc.reactivation_rate.event_rich_poor(
                            run, cs, event_threshold)
                    rewnon = np.array(
                        rewnon) if rewnon is not None else [np.nan] * len(evs)

                    csdata['rewnon'] = rewnon
                    csdata['reward_rich'][rewnon >= 0.8] = True
                    csdata['reward_poor'][rewnon <= 0.2] = True
                    csdata['reward_mixed'][(0.2 < rewnon)
                                           & (rewnon < 0.8)] = True

                default = pd.DataFrame(csdata)
                date_df = run_function(run, default)

                if per_cell:
                    old_date_df = date_df
                    date_df = None

                    t2p = run.trace2p()
                    trs = t2p.trace('deconvolved')

                    ncells = np.arange(np.shape(trs)[0])
                    celldata = {}
                    for ev in evs:
                        if -1 * trange[0] < ev < np.shape(trs)[1] - trange[1]:
                            act = np.nanmax(trs[:,
                                                ev + trange[0]:ev + trange[1]],
                                            axis=1)
                            act = act > deconvolved_threshold
                            celldata['cell_id'] = ncells[act]

                            if len(ncells[act]) > 0:
                                cell_df = pd.DataFrame(celldata)
                                ind = old_date_df.index[old_date_df.frame ==
                                                        ev][0]
                                for key in old_date_df.keys():
                                    cell_df[key] = old_date_df.at[ind, key]

                                if date_df is None:
                                    date_df = cell_df
                                else:
                                    date_df = pd.concat([date_df, cell_df],
                                                        ignore_index=True,
                                                        sort=True)

                if date_df is not None:
                    if df is None:
                        df = date_df
                    else:
                        df = pd.concat([df, date_df],
                                       ignore_index=True,
                                       sort=True)

    return df
Пример #5
0
def dataframe_date(date):
    """

    Parameters
    ----------
    date

    Returns
    -------

    """

    df = pd.DataFrame([date.mouse], columns=['mouse'])
    # df['mouse'] = date.mouse
    df['date'] = date.date
    df['date_obj'] = df['date'].apply(
        lambda x: datetime.datetime.strptime(str(x), '%y%m%d'))
    df['dprime'] = calc_legacy.performance.dprime(date)
    df['dprime_new'] = calc.performance.dprime(date)
    df['dprime_new_alltrials'] = calc.performance.dprime(date,
                                                         hmm_engaged=False)
    df['dprime_run'] = calc.performance.dprime(date, across_run=False)
    df['dprime_day_start'] = calc.psytrack.dprime_day_start(date)
    df['dprime_day_end'] = calc.psytrack.dprime_day_end(date)
    df['d_dprime_day'] = calc.psytrack.d_dprime_day(date)
    df['reversed'] = flow.metadata.reversal(date.mouse) < date.date

    # df['react_plus'] = nannone(calc_legacy.reactivation_rate.freq(date, 'plus'))
    # df['react_neutral'] = nannone(calc_legacy.reactivation_rate.freq(date, 'neutral'))
    # df['react_minus'] = nannone(calc_legacy.reactivation_rate.freq(date, 'minus'))

    df['react_new_plus'] = nannone(calc.reactivation_rate.freq(date, 'plus'))
    df['react_new_neutral'] = nannone(
        calc.reactivation_rate.freq(date, 'neutral'))
    df['react_new_minus'] = nannone(calc.reactivation_rate.freq(date, 'minus'))
    df['react_new_comb'] = \
        df['react_new_plus'] + df['react_new_neutral'] + df['react_new_minus']
    df['react_all_plus'] = nannone(
        calc.reactivation_rate.freq(date, 'plus', state='all'))
    df['react_all_neutral'] = nannone(
        calc.reactivation_rate.freq(date, 'neutral', state='all'))
    df['react_all_minus'] = nannone(
        calc.reactivation_rate.freq(date, 'minus', state='all'))

    df['count_plus'] = nannone(calc.reactivation_rate.count(date, 'plus'))
    df['count_neutral'] = nannone(calc.reactivation_rate.count(
        date, 'neutral'))
    df['count_minus'] = nannone(calc.reactivation_rate.count(date, 'minus'))
    df['freq_denom'] = nannone(calc.reactivation_rate.freq_denominator(date))

    # df['cosdist_plus'] = nannone(calc_legacy.cosine_distance.stimulus(date, 'plus'))
    # df['cosdist_neutral'] = nannone(calc_legacy.cosine_distance.stimulus(date, 'neutral'))
    # df['cosdist_minus'] = nannone(calc_legacy.cosine_distance.stimulus(date, 'minus'))

    # cosine_similarity_stimuli(
    #     date, cs, group, trace_type='deconvolved', start_s=0, end_s=1, trange_glm=(0, 1), rectify=False,
    #     exclude_outliers=False, remove_group=None, offset_glm_positive=False, remove_baseline_stimuli=False,
    #     drop_glm_zeros=False, max_across_trial=False, compare_to_trials=False, error_trials=0, binarize=None,
    #     correlation=False)

    return df