Exemplo n.º 1
0
def load_results_train_test_scores(data_path):
    res = defaultdict(list)
    experiment_dirs = sorted(
        [os.path.join(data_path, d) for d in os.listdir(data_path)])

    keys = [
        'DAQ_O_ON_F', 'DAQ_O_OFF_F', 'DAQ_W_ON_F', 'DAQ_O_ON', 'DAQ_O_OFF',
        'DAQ_W_ON', 'TRIAL_FRAMES', 'TRIAL_PERIOD', 'NAME_MOUSE', 'NAME_PLANE',
        'NAME_DATE', 'decode_style', 'neurons', 'shuffle', 'split_style'
    ]
    for exp_dir in experiment_dirs:
        data_dirs = sorted(glob.glob(os.path.join(exp_dir, '*' + '.pkl')))
        config_dirs = sorted(glob.glob(os.path.join(exp_dir, '*.json')))
        for data_dir, config_dir in zip(data_dirs, config_dirs):
            config = tools.file_io.load_json(config_dir)
            cur_res = tools.file_io.load_pickle(data_dir)

            for k in keys:
                cur_res[k] = np.array([config[k]] * len(cur_res['scores']))
            reduce.chain_defaultdicts(res, cur_res)

    add_indices(res)
    add_time(res)

    for i, _ in enumerate(res['scores']):
        res['top_score'].append(np.max(res['scores'][i]))
    res['top_score'] = np.array(res['top_score'])
    return res
Exemplo n.º 2
0
def custom_get_res(start_time, end_time):
    list_of_res = []
    for i, condition in enumerate(conditions):
        if any(s in condition.name for s in ['YFP', 'HALO', 'JAWS']):
            data_path = os.path.join(Config.LOCAL_DATA_PATH,
                                     Config.LOCAL_DATA_BEHAVIOR_FOLDER,
                                     condition.name)
        else:
            data_path = os.path.join(Config.LOCAL_DATA_PATH,
                                     Config.LOCAL_DATA_TIMEPOINT_FOLDER,
                                     condition.name)
        res = custom_analyze_behavior(data_path,
                                      condition,
                                      start_time=start_time,
                                      end_time=end_time)

        if condition.name == 'OFC_LONGTERM':
            res = filter.exclude(res, {'mouse': 3})

        if 'YFP' in condition.name:
            res['condition'] = np.array(['YFP'] * len(res['mouse']))
        elif 'JAWS' in condition.name:
            res['condition'] = np.array(['JAWS'] * len(res['mouse']))
        elif 'HALO' in condition.name:
            res['condition'] = np.array(['HALO'] * len(res['mouse']))
        else:
            res['condition'] = np.array([condition.name] * len(res['mouse']))
        list_of_res.append(res)
    all_res = defaultdict(list)
    for res, condition in zip(list_of_res, conditions):
        reduce.chain_defaultdicts(all_res, res)
    return all_res
Exemplo n.º 3
0
def plot_overlap_water(res, start_days, end_days, figure_path):
    ax_args_copy = overlap_ax_args.copy()
    res = copy.copy(res)
    mice = np.unique(res['mouse'])
    res = filter.filter_days_per_mouse(res, days_per_mouse=end_days)
    add_naive_learned(res, start_days, end_days)
    ax_args_copy.update({'xlim': [-1, 2]})
    y_keys = ['US/CS+', 'CS+/US']
    summary_res = defaultdict(list)
    for arg in y_keys:
        _get_overlap_water(res, arg=arg)
        new_res = reduce.new_filter_reduce(
            res,
            filter_keys=['mouse', 'day', 'odor_valence'],
            reduce_key='Overlap')
        new_res['Type'] = np.array([arg] * len(new_res['training_day']))
        reduce.chain_defaultdicts(summary_res, new_res)

    summary_res.pop('Overlap_sem')
    summary_res.pop('Overlap_std')
    summary_res = filter.filter(summary_res, {'odor_valence': 'CS+'})
    mean_std_res = reduce.new_filter_reduce(summary_res,
                                            filter_keys='Type',
                                            reduce_key='Overlap')
    types = np.unique(summary_res['Type'])
    scatter_args_copy = scatter_args.copy()
    scatter_args_copy.update({'s': 2, 'alpha': .6})
    for i, type in enumerate(types):
        reuse_arg = True
        if i == 0:
            reuse_arg = False
        temp = filter.filter(summary_res, {'Type': type})
        plot.plot_results(temp,
                          x_key='Type',
                          y_key='Overlap',
                          loop_keys='mouse',
                          colors=['Black'] * len(mice),
                          plot_function=plt.scatter,
                          path=figure_path,
                          plot_args=scatter_args_copy,
                          ax_args=ax_args_copy,
                          save=False,
                          reuse=reuse_arg,
                          fig_size=(1.5, 1.5),
                          rect=(.25, .25, .6, .6),
                          legend=False)

    plot.plot_results(mean_std_res,
                      x_key='Type',
                      y_key='Overlap',
                      error_key='Overlap_sem',
                      path=figure_path,
                      plot_function=plt.errorbar,
                      plot_args=error_args,
                      ax_args=ax_args,
                      save=True,
                      reuse=True,
                      fig_size=(1.5, 1.5),
                      legend=False)
    print(mean_std_res['Overlap'])
Exemplo n.º 4
0
def load_pca(save_path):
    res = defaultdict(list)
    experiment_dirs = sorted(
        [os.path.join(save_path, d) for d in os.listdir(save_path)])
    for d in experiment_dirs:
        temp_res = fio.load_pickle(d)
        reduce.chain_defaultdicts(res, temp_res)
    return res
Exemplo n.º 5
0
def easy_shuffle(res, shuffle_keys, n):
    shuffled_res = defaultdict(list)
    for i in range(n):
        temp = shuffle_data(res, shuffle_keys=shuffle_keys)
        reduce.chain_defaultdicts(shuffled_res, temp)
    shuffled_res['shuffle'] = np.ones(len(shuffled_res['odor']), dtype=int)
    reduce.chain_defaultdicts(res, shuffled_res)
    return res
Exemplo n.º 6
0
def _filter(res):
    out = defaultdict(list)
    for mouse in np.unique(res['mouse']):
        temp = filter.filter(res, {'mouse': mouse})
        data = temp['ball_data'].flatten()
        max, min = np.max(data), np.min(data)
        if (max - min) > 4:
            reduce.chain_defaultdicts(out, temp)
    return out
Exemplo n.º 7
0
def scalpel(res2, keys, mouse, sessions, valence='CS+'):
    temp = filter.filter(res2, {'mouse': mouse, 'odor_valence': valence})
    print(temp['mouse'].size)
    assert temp['mouse'].size == 1
    ix = np.isin(temp['session'][0], sessions)
    ix = np.invert(ix)
    for k in keys:
        temp[k][0] = temp[k][0][ix]
    _, inverse = np.unique(temp['session'][0], return_inverse=True)
    temp['session'][0] = inverse
    res2 = filter.exclude(res2, {'mouse': mouse, 'odor_valence': valence})
    reduce.chain_defaultdicts(res2, temp)
    return res2
Exemplo n.º 8
0
def plot_bar(res,
             days_per_mouse,
             odor_valence,
             day_pad,
             save,
             reuse,
             figure_path,
             color='black',
             normalize=False):
    res = copy.copy(res)
    res['day_'] = np.zeros_like(res['day'])

    res_ = defaultdict(list)
    for i, days in enumerate(days_per_mouse):
        temp = filter.filter_days_per_mouse(res, days_per_mouse=days)
        temp = filter.filter(temp, {'odor_valence': odor_valence[i]})
        temp['day_'] = np.array([i + day_pad] * len(temp['day_']))
        reduce.chain_defaultdicts(res_, temp)

    _max_dff(res_)
    res_ = reduce.new_filter_reduce(
        res_,
        filter_keys=['odor_valence', 'mouse', 'day_'],
        reduce_key='max_dff')
    res_.pop('max_dff_sem')
    summary = reduce.new_filter_reduce(res_,
                                       filter_keys=['day_', 'odor_valence'],
                                       reduce_key='max_dff')

    if normalize:
        _normalize_across_days(summary)

    # plot.plot_results(summary, x_key='day_', y_key='max_dff', error_key='max_dff_sem',
    #                   path=figure_path,
    #                   colors='black', legend=False, plot_args=error_args, plot_function= plt.errorbar,
    #                   fig_size=(2, 1.5), save=False, reuse=reuse)

    line_args_copy = line_args.copy()
    line_args_copy.update({'alpha': .75, 'linewidth': 1})
    plot.plot_results(summary,
                      x_key='day_',
                      y_key='max_dff',
                      path=figure_path,
                      colors=color,
                      legend=False,
                      plot_args=line_args_copy,
                      fig_size=(2, 1.5),
                      save=save,
                      reuse=reuse,
                      name_str=odor_valence[-1])
Exemplo n.º 9
0
                                {'odor_valence': ['CS+', 'CS-', 'PT CS+']})
        all_res_lick = reduce.new_filter_reduce(
            all_res,
            filter_keys=['condition', 'odor_valence', 'mouse'],
            reduce_key=lick)

        for i, x in enumerate(all_res_lick[lick]):
            all_res_lick['training_end_licks'].append(np.mean(x[-20:]))
            all_res_lick['start_time'].append(start_time)
            all_res_lick['end_time'].append(end_time)
        for k, v in all_res_lick.items():
            all_res_lick[k] = np.array(v)
        list_of_res.append(all_res_lick)

    for res in list_of_res:
        reduce.chain_defaultdicts(full, res)

    line_args_copy = line_args.copy()
    line_args_copy.update({'marker': '.', 'linewidth': .5, 'markersize': .5})

    valences = np.unique(full['odor_valence'])
    for valence in valences:
        color = [color_dict_valence[valence]]

        mean_std_res = reduce.new_filter_reduce(
            full,
            filter_keys=['odor_valence', 'start_time'],
            reduce_key='training_end_licks')

        plot.plot_results(full,
                          x_key='start_time',
Exemplo n.º 10
0
def plot_summary_odor_pretraining(res,
                                  start_days,
                                  end_days,
                                  arg_naive,
                                  figure_path,
                                  save,
                                  excitatory=True):
    ax_args_copy = ax_args.copy()
    res = copy.copy(res)
    list_of_days = list(zip(start_days, end_days))
    mice = np.unique(res['mouse'])
    start_end_day_res = filter.filter_days_per_mouse(
        res, days_per_mouse=list_of_days)
    get_responsive_cells(start_end_day_res)

    if arg_naive:
        day_start = filter.filter(start_end_day_res,
                                  {'odor_standard': 'PT Naive'})
        day_start['odor_standard'] = np.array(['PT CS+'] *
                                              len(day_start['odor_standard']))
        day_end = filter.filter_days_per_mouse(start_end_day_res,
                                               days_per_mouse=end_days)
        day_end = filter.filter(day_end, {'odor_standard': 'PT CS+'})
        reduce.chain_defaultdicts(day_start, day_end)
        start_end_day_res = day_start
    else:
        start_end_day_res = filter.exclude(start_end_day_res,
                                           {'odor_standard': 'PT Naive'})

    add_naive_learned(start_end_day_res, start_days, end_days, 'a', 'b')
    filter.assign_composite(start_end_day_res,
                            loop_keys=['odor_standard', 'training_day'])

    odor_list = ['PT CS+']
    colors = ['Orange']
    ax_args_copy = ax_args_copy.copy()
    ax_args_copy.update({
        'xlim': [-1, 10],
        'ylim': [0, .4],
        'yticks': [0, .1, .2, .3, .4]
    })
    for i, odor in enumerate(odor_list):
        save_arg = False
        reuse_arg = True
        if i == 0:
            reuse_arg = False

        if save and i == len(odor_list) - 1:
            save_arg = True

        plot.plot_results(start_end_day_res,
                          select_dict={'odor_standard': odor},
                          x_key='odor_standard_training_day',
                          y_key='Fraction Responsive',
                          loop_keys='mouse',
                          colors=[colors[i]] * len(mice),
                          path=figure_path,
                          plot_args=line_args,
                          ax_args=ax_args_copy,
                          save=save_arg,
                          reuse=reuse_arg,
                          fig_size=(2.5, 1.5),
                          legend=False,
                          name_str='_E' if excitatory else '_I')

    before_csm = filter.filter(start_end_day_res,
                               filter_dict={
                                   'training_day': 'a',
                                   'odor_standard': 'PT CS+'
                               })
    after_csm = filter.filter(start_end_day_res,
                              filter_dict={
                                  'training_day': 'b',
                                  'odor_standard': 'PT CS+'
                              })
    from scipy.stats import ranksums, wilcoxon, kruskal

    print('Before PT CS+: {}'.format(np.mean(
        before_csm['Fraction Responsive'])))
    print('After PT CS+: {}'.format(np.mean(after_csm['Fraction Responsive'])))
    from scipy.stats import sem
    print('After PT CS+ STD: {}'.format(sem(after_csm['Fraction Responsive'])))
    print('Wilcoxin:{}'.format(
        wilcoxon(before_csm['Fraction Responsive'],
                 after_csm['Fraction Responsive'])))
Exemplo n.º 11
0
def plot_responsive_difference_odor_and_water(res,
                                              odor_start_days,
                                              water_start_days,
                                              end_days,
                                              use_colors=True,
                                              figure_path=None,
                                              include_water=True,
                                              normalize=False,
                                              pt_start=None,
                                              pt_learned=None,
                                              average=True,
                                              ylim=.22,
                                              reuse_arg=False,
                                              save_arg=True):
    key = 'Change in Fraction Responsive'
    if normalize:
        key = 'Norm. Fraction Responsive'

    def _helper(start_end_day_res):
        combs, list_of_ixs = filter.retrieve_unique_entries(
            start_end_day_res, ['mouse', 'odor_standard'])
        for i, comb in enumerate(combs):
            ixs = list_of_ixs[i]
            assert len(ixs) == 2

            if start_end_day_res['training_day'][0] == 'Naive':
                ref = ixs[0]
                test = ixs[1]
            elif start_end_day_res['training_day'][0] == 'Learned':
                ref = ixs[1]
                test = ixs[0]
            else:
                raise ValueError('cannot find ref day')

            if normalize:
                start_end_day_res[key][test] = start_end_day_res['Fraction Responsive'][test] / \
                                               start_end_day_res['Fraction Responsive'][ref]
                start_end_day_res[key][ref] = 1
            else:

                start_end_day_res[key][test] = start_end_day_res['Fraction Responsive'][test] - \
                                               start_end_day_res['Fraction Responsive'][ref]
                start_end_day_res[key][ref] = 0

    ax_args_copy = ax_args.copy()
    res = copy.copy(res)
    get_responsive_cells(res)
    list_of_days = list(zip(odor_start_days, end_days))
    mice = np.unique(res['mouse'])
    res[key] = np.zeros_like(res['Fraction Responsive'])
    start_end_day_res = filter.filter_days_per_mouse(
        res, days_per_mouse=list_of_days)
    start_end_day_res = filter.filter(
        start_end_day_res, {'odor_valence': ['CS+', 'CS-', 'Naive']})
    add_naive_learned(start_end_day_res, odor_start_days, end_days)

    odors = ['CS+', 'CS-', 'Naive']
    if 'PT CS+' in np.unique(res['odor_valence']):
        odors = ['PT CS+'] + odors
        list_of_days = list(zip(pt_start, pt_learned))
        start_end_day_res_pt = filter.filter_days_per_mouse(
            res, days_per_mouse=list_of_days)
        start_end_day_res_pt = filter.filter(start_end_day_res_pt,
                                             {'odor_valence': 'PT CS+'})
        add_naive_learned(start_end_day_res_pt, pt_start, pt_learned)
        reduce.chain_defaultdicts(start_end_day_res, start_end_day_res_pt)

    if include_water:
        odors += ['US']
        list_of_days = list(zip(water_start_days, end_days))
        start_end_day_res_water = filter.filter_days_per_mouse(
            res, days_per_mouse=list_of_days)
        start_end_day_res_water = filter.filter(start_end_day_res_water,
                                                {'odor_valence': 'US'})
        add_naive_learned(start_end_day_res_water, water_start_days, end_days)
        reduce.chain_defaultdicts(start_end_day_res, start_end_day_res_water)

    filter.assign_composite(start_end_day_res,
                            loop_keys=['odor_standard', 'training_day'])
    if average:
        start_end_day_res = reduce.new_filter_reduce(
            start_end_day_res,
            filter_keys=['odor_valence', 'mouse', 'training_day'],
            reduce_key=key)
        start_end_day_res.pop(key + '_sem')
    _helper(start_end_day_res)
    start_end_day_res = filter.filter(start_end_day_res,
                                      {'training_day': 'Learned'})
    summary_res = reduce.new_filter_reduce(start_end_day_res,
                                           filter_keys='odor_valence',
                                           reduce_key=key)

    dict = {
        'CS+': 'Green',
        'CS-': 'Red',
        'US': 'Turquoise',
        'PT CS+': 'Orange',
        'Naive': 'Gray'
    }
    if use_colors:
        colors = [
            dict[key] for key in np.unique(start_end_day_res['odor_valence'])
        ]
    else:
        colors = ['Black'] * 6

    ax_args_copy = ax_args_copy.copy()
    n_valence = len(np.unique(summary_res['odor_valence']))
    ax_args_copy.update({
        'xlim': [-.5, 3.5],
        'ylim': [-ylim, ylim],
        'yticks': [-.3, -.2, -.1, 0, .1, .2, .3]
    })
    if normalize:
        ax_args_copy.update({
            'xlim': [-.5, 3.5],
            'ylim': [-.1, 1.5],
            'yticks': [0, .5, 1, 1.5]
        })
    error_args_ = {
        'fmt': '.',
        'capsize': 2,
        'elinewidth': 1,
        'markersize': 2,
        'alpha': .75
    }
    scatter_args_copy = scatter_args.copy()
    scatter_args_copy.update({'s': 3})

    for i, odor in enumerate(odors):
        reuse = True
        if i == 0:
            reuse = reuse_arg
        plot.plot_results(start_end_day_res,
                          loop_keys='odor_valence',
                          select_dict={'odor_valence': odor},
                          x_key='odor_valence',
                          y_key=key,
                          colors=[dict[odor]] * len(mice),
                          path=figure_path,
                          plot_args=scatter_args_copy,
                          plot_function=plt.scatter,
                          ax_args=ax_args_copy,
                          save=False,
                          reuse=reuse,
                          fig_size=(2, 1.5),
                          rect=(.25, .2, .6, .6),
                          legend=False,
                          name_str=','.join([str(x) for x in odor_start_days]))

    if not normalize:
        plt.plot(plt.xlim(), [0, 0], '--', color='gray', linewidth=1, alpha=.5)

    plot.plot_results(summary_res,
                      x_key='odor_valence',
                      y_key=key,
                      error_key=key + '_sem',
                      colors='black',
                      path=figure_path,
                      plot_args=error_args_,
                      plot_function=plt.errorbar,
                      ax_args=ax_args_copy,
                      save=save_arg,
                      reuse=True,
                      fig_size=(2, 1.5),
                      legend=False)
Exemplo n.º 12
0
def plot_summary_odor_and_water(res,
                                odor_start_days,
                                water_start_days,
                                end_days,
                                use_colors=True,
                                excitatory=True,
                                arg='odor_valence',
                                figure_path=None):
    include_water = True

    ax_args_copy = ax_args.copy()
    res = copy.copy(res)
    get_responsive_cells(res)
    mice = np.unique(res['mouse'])

    list_of_days = list(zip(odor_start_days, end_days))
    start_end_day_res = filter.filter_days_per_mouse(
        res, days_per_mouse=list_of_days)
    start_end_day_res = filter.exclude(start_end_day_res,
                                       {'odor_valence': 'US'})
    add_naive_learned(start_end_day_res, odor_start_days, end_days, 'a', 'b')

    if include_water:
        list_of_days = list(zip(water_start_days, end_days))
        start_end_day_res_water = filter.filter_days_per_mouse(
            res, days_per_mouse=list_of_days)
        start_end_day_res_water = filter.filter(start_end_day_res_water,
                                                {'odor_valence': 'US'})
        add_naive_learned(start_end_day_res_water, water_start_days, end_days,
                          'a', 'b')
        reduce.chain_defaultdicts(start_end_day_res, start_end_day_res_water)

    ax_args_copy = ax_args_copy.copy()
    if arg == 'odor_valence':
        start_end_day_res = reduce.new_filter_reduce(
            start_end_day_res,
            filter_keys=['training_day', 'mouse', 'odor_valence'],
            reduce_key='Fraction Responsive')
        odor_list = ['CS+', 'CS-']
        ax_args_copy.update({
            'xlim': [-1, 6],
            'ylim': [0, .6],
            'yticks': [0, .1, .2, .3, .4, .5]
        })
        colors = ['Green', 'Red']
    elif arg == 'naive':
        arg = 'odor_valence'
        start_end_day_res = reduce.new_filter_reduce(
            start_end_day_res,
            filter_keys=['training_day', 'mouse', 'odor_valence'],
            reduce_key='Fraction Responsive')
        odor_list = ['CS+']
        ax_args_copy.update({
            'xlim': [-1, 4],
            'ylim': [0, .6],
            'yticks': [0, .1, .2, .3, .4, .5]
        })
        colors = ['GoldenRod']
    else:
        odor_list = ['CS+1', 'CS+2', 'CS-1', 'CS-2']
        colors = ['Green', 'Green', 'Red', 'Red']
        ax_args_copy.update({
            'xlim': [-1, 10],
            'ylim': [0, .6],
            'yticks': [0, .1, .2, .3, .4, .5]
        })

    filter.assign_composite(start_end_day_res, loop_keys=[arg, 'training_day'])
    if not use_colors:
        colors = ['Black'] * 4

    name_str = '_E' if excitatory else '_I'
    for i, odor in enumerate(odor_list):
        reuse_arg = True
        if i == 0:
            reuse_arg = False
        plot.plot_results(start_end_day_res,
                          select_dict={arg: odor},
                          x_key=arg + '_training_day',
                          y_key='Fraction Responsive',
                          loop_keys='mouse',
                          colors=[colors[i]] * len(mice),
                          path=figure_path,
                          plot_args=line_args,
                          ax_args=ax_args_copy,
                          save=False,
                          reuse=reuse_arg,
                          fig_size=(2.5, 1.5),
                          legend=False,
                          name_str=','.join([str(x) for x in odor_start_days]))

    plot.plot_results(start_end_day_res,
                      select_dict={'odor_standard': 'US'},
                      x_key='training_day',
                      y_key='Fraction Responsive',
                      loop_keys='mouse',
                      colors=['Turquoise'] * len(mice),
                      path=figure_path,
                      plot_args=line_args,
                      ax_args=ax_args_copy,
                      fig_size=(1.6, 1.5),
                      legend=False,
                      reuse=True,
                      save=True,
                      name_str=name_str)

    before_odor = filter.filter(start_end_day_res,
                                filter_dict={
                                    'training_day': 'a',
                                    'odor_valence': ['CS+', 'CS-']
                                })
    after_odor = filter.filter(start_end_day_res,
                               filter_dict={
                                   'training_day': 'b',
                                   'odor_valence': ['CS+', 'CS-']
                               })
    before_csp = filter.filter(start_end_day_res,
                               filter_dict={
                                   'training_day': 'a',
                                   'odor_valence': 'CS+'
                               })
    after_csp = filter.filter(start_end_day_res,
                              filter_dict={
                                  'training_day': 'b',
                                  'odor_valence': 'CS+'
                              })
    before_csm = filter.filter(start_end_day_res,
                               filter_dict={
                                   'training_day': 'a',
                                   'odor_valence': 'CS-'
                               })
    after_csm = filter.filter(start_end_day_res,
                              filter_dict={
                                  'training_day': 'b',
                                  'odor_valence': 'CS-'
                              })
    before_water = filter.filter(start_end_day_res,
                                 filter_dict={
                                     'training_day': 'a',
                                     'odor_valence': 'US'
                                 })
    after_water = filter.filter(start_end_day_res,
                                filter_dict={
                                    'training_day': 'b',
                                    'odor_valence': 'US'
                                })

    try:
        from scipy.stats import ranksums, wilcoxon, kruskal

        print('Before Odor: {}'.format(
            np.mean(before_odor['Fraction Responsive'])))
        print('After Odor: {}'.format(
            np.mean(after_odor['Fraction Responsive'])))
        print('Wilcoxin:{}'.format(
            wilcoxon(before_odor['Fraction Responsive'],
                     after_odor['Fraction Responsive'])))

        print('Before CS+: {}'.format(
            np.mean(before_csp['Fraction Responsive'])))
        print('After CS+: {}'.format(np.mean(
            after_csp['Fraction Responsive'])))
        print('Wilcoxin:{}'.format(
            wilcoxon(before_csp['Fraction Responsive'],
                     after_csp['Fraction Responsive'])))

        print('Before CS-: {}'.format(
            np.mean(before_csm['Fraction Responsive'])))
        print('After CS-: {}'.format(np.mean(
            after_csm['Fraction Responsive'])))
        print('Wilcoxin:{}'.format(
            wilcoxon(before_csm['Fraction Responsive'],
                     after_csm['Fraction Responsive'])))

        print('Before US: {}'.format(
            np.mean(before_water['Fraction Responsive'])))
        print('After US: {}'.format(np.mean(
            after_water['Fraction Responsive'])))
        print('Wilcoxin:{}'.format(
            wilcoxon(before_water['Fraction Responsive'],
                     after_water['Fraction Responsive'])))
    except:
        print('stats didnt work')
Exemplo n.º 13
0
from tools import file_io
import filter
import reduce
import plot

data_directory = Config().LOCAL_DATA_PATH
data_directory = os.path.join(data_directory, 'registration','motion_metrics')

conditions = glob.glob(os.path.join(data_directory, '*/'))

res = defaultdict(list)
for c in conditions:
    mouse_pickles = glob.glob(os.path.join(c, '*.pkl'))
    for pickle in mouse_pickles:
        temp = file_io.load_pickle(pickle)
        reduce.chain_defaultdicts(res, temp)

res['within_day_crisp_average'] = []
for v in res['within_day_crisp']:
    res['within_day_crisp_average'].append(np.mean(v))

for k, v in res.items():
    res[k] = np.array(v)


save_path = os.path.join(Config.LOCAL_FIGURE_PATH, 'MISC', 'MOTION_METRICS')

mouse = 'M4_OFC'

vmin, vmax = 0.6, 1
ix = res['mouse'] == mouse
            glob.glob(
                os.path.join(experiment.path, directory, constants.yfp + '*')))
        res2 = analysis.parse(yfp_files,
                              experiment=experiment,
                              condition=constants.yfp,
                              phase=directory)
        res2['experiment'] = np.array([experiment.name] *
                                      len(res2['odor_valence']))

        # if experiment.name == 'MPFC_DT':
        #     res1 = filter.exclude(res1, {'mouse':['H01']})
        #     res2 = filter.exclude(res2, {'mouse':['H01']})
        # if experiment.name == 'MPFC_PT':
        #     res1 = filter.exclude(res1, {'mouse': ['Y01']})
        #     res2 = filter.exclude(res2, {'mouse': ['Y01']})
        reduce.chain_defaultdicts(res, res1)
        reduce.chain_defaultdicts(res, res2)


# analysis
def _get_number_of_licks(mat, start, end):
    on_off = np.diff(mat, n=1)
    n_licks = np.sum(on_off[start:end, :] > 0, axis=1)
    return n_licks


for i in range(len(res['bin_ant_1_raw'])):
    a, b = res['bin_ir_raw'][i], res['bin_samp_raw'][i]
    c, d, e = res['bin_ant_1_raw'][i], res['bin_ant_2_raw'][i], res[
        'bin_ant_3_raw'][i]
    f, g = res['bin_col_1_raw'][i], res['bin_col_2_raw'][i]
Exemplo n.º 15
0
def plot_correlation(res,
                     start_days,
                     end_days,
                     figure_path,
                     odor_end=True,
                     linestyle='-',
                     direction=0,
                     arg=False,
                     save=False,
                     reuse=False,
                     color='black'):
    def _get_ixs(r, arg):
        A = r['Odor_A']
        B = r['Odor_B']
        l = []
        for i, a in enumerate(A):
            b = B[i]
            if arg == 'opposing':
                if a < 2 and b > 1:
                    l.append(i)
            elif arg == 'CS+':
                if a == 0 and b == 1:
                    l.append(i)
            elif arg == 'CS-':
                if a == 2 and b == 3:
                    l.append(i)
        return np.array(l)

    res = filter.filter(res, {'odor_valence': ['CS+', 'CS-']})
    res_before = filter.filter_days_per_mouse(res, start_days)
    corr_before = _correlation(res_before, ['mouse'],
                               shuffle=False,
                               odor_end=odor_end,
                               direction=direction)
    corr_before['day'] = np.array(['A'] * len(corr_before['Odor_A']))
    res_after = filter.filter_days_per_mouse(res, end_days)
    corr_after = _correlation(res_after, ['mouse'],
                              shuffle=False,
                              odor_end=odor_end,
                              direction=direction)
    corr_after['day'] = np.array(['B'] * len(corr_after['Odor_A']))
    corr = defaultdict(list)
    reduce.chain_defaultdicts(corr, corr_before)
    reduce.chain_defaultdicts(corr, corr_after)

    ix_same = np.equal(corr['Odor_A'], corr['Odor_B'])
    ix_different = np.invert(ix_same)
    for k, v in corr.items():
        corr[k] = v[ix_different]

    if arg is not False:
        ixs = _get_ixs(corr, arg)
        for k, v in corr.items():
            corr[k] = v[ixs]

    mouse_corr = reduce.new_filter_reduce(corr,
                                          filter_keys=['mouse', 'day'],
                                          reduce_key='corrcoef')
    mouse_corr.pop('corrcoef_sem')
    mouse_corr.pop('corrcoef_std')
    average_corr = reduce.new_filter_reduce(mouse_corr,
                                            filter_keys=['day'],
                                            reduce_key='corrcoef')

    error_args = {'capsize': 2, 'elinewidth': 1, 'markersize': 2, 'alpha': .5}
    error_args.update({'linestyle': linestyle})
    plot.plot_results(average_corr,
                      x_key='day',
                      y_key='corrcoef',
                      error_key='corrcoef_sem',
                      plot_args=error_args,
                      plot_function=plt.errorbar,
                      colors=color,
                      ax_args={
                          'ylim': [0, 1],
                          'xlim': [-.5, 1.5]
                      },
                      save=save,
                      reuse=reuse,
                      path=figure_path,
                      name_str=str(direction) + '_' + str(arg))

    #stats
    before_odor = filter.filter(corr, filter_dict={'day': 'A'})
    after_odor = filter.filter(corr, filter_dict={'day': 'B'})
    from scipy.stats import ranksums, wilcoxon, kruskal
    print('direction: {}'.format(direction))
    # print('Data Before: {}'.format(before_odor['corrcoef']))
    # print('Data After: {}'.format(after_odor['corrcoef']))
    print('Before: {}'.format(np.mean(before_odor['corrcoef'])))
    print('After: {}'.format(np.mean(after_odor['corrcoef'])))
    print('Wilcoxin:{}'.format(
        wilcoxon(before_odor['corrcoef'], after_odor['corrcoef'])))
Exemplo n.º 16
0
def plot_max_dff_days(res,
                      days_per_mouse,
                      odor_valence,
                      save,
                      reuse,
                      day_pad,
                      ylim=.115,
                      colors=None,
                      normalize=False,
                      figure_path=None):

    res = copy.copy(res)
    res['day_'] = np.zeros_like(res['day'])

    res_ = defaultdict(list)
    for i, days in enumerate(days_per_mouse):
        temp = filter.filter_days_per_mouse(res, days_per_mouse=days)
        temp = filter.filter(temp, {'odor_valence': odor_valence[i]})
        temp['day_'] = np.array([i + day_pad] * len(temp['day_']))
        reduce.chain_defaultdicts(res_, temp)

    _max_dff(res_)
    res_ = reduce.new_filter_reduce(
        res_,
        filter_keys=['odor_valence', 'mouse', 'day_'],
        reduce_key='max_dff')
    if normalize:
        _normalize_across_days(res_)
        yticks = [0, 1, 2, 3, 4, 5, 6]
    else:
        yticks = np.arange(0, ylim, .05)

    dict = {
        'CS+': 'Green',
        'CS-': 'Red',
        'US': 'Turquoise',
        'PT CS+': 'Orange'
    }
    n_mice = len(np.unique(res['mouse']))
    ax_args_copy = ax_args.copy()
    ax_args_copy.update({
        'ylim': [0, ylim],
        'yticks': yticks,
        'xticks': list(range(20))
    })
    line_args_copy = line_args.copy()
    line_args_copy.update({
        'marker': '.',
        'linestyle': '--',
        'linewidth': .5,
        'alpha': .5,
        'markersize': 2
    })
    if colors is None:
        colors = [dict[x] for x in odor_valence]
    else:
        line_args_copy.update({'marker': None})
    plot.plot_results(res_,
                      loop_keys='mouse',
                      x_key='day_',
                      y_key='max_dff',
                      path=figure_path,
                      colors=colors * n_mice,
                      legend=False,
                      plot_args=line_args_copy,
                      ax_args=ax_args_copy,
                      fig_size=(2, 2),
                      save=save,
                      reuse=reuse)
Exemplo n.º 17
0
def plot_correlation_across_days(res,
                                 days,
                                 loop_keys,
                                 shuffle,
                                 figure_path,
                                 reuse,
                                 save,
                                 analyze,
                                 plot_bool,
                                 odor_end=True):
    if analyze:
        res_ = defaultdict(list)
        for day_list in days:
            d = list(zip(day_list[0], day_list[1]))
            res_temp = filter.filter_days_per_mouse(res, d)
            corr_res = _correlation(res_temp,
                                    loop_keys,
                                    shuffle,
                                    odor_end=odor_end)
            reduce.chain_defaultdicts(res_, corr_res)

        res_ = filter.filter(res_, {'Odor_A': 0, 'Odor_B': 1})
        res_ = reduce.new_filter_reduce(res_,
                                        filter_keys=['mouse', 'odor_standard'],
                                        reduce_key='corrcoef')
        res_.pop('corrcoef_sem')
        return res_

    if plot_bool:
        res_ = res
        if shuffle:
            s = '_shuffled'
        else:
            s = ''

        ax_args_copy = ax_args.copy()
        ax_args_copy.update({
            'xlim': [-.5, 2.5],
            'ylim': [0, 1.05],
            'yticks': np.arange(0, 1.1, .2)
        })

        swarm_args_copy = swarm_args.copy()
        swarm_args_copy.update({'palette': ['green', 'red', 'gray']})

        plot.plot_results(res_,
                          x_key='odor_valence',
                          y_key='corrcoef',
                          path=figure_path,
                          plot_args=swarm_args_copy,
                          plot_function=sns.stripplot,
                          ax_args=ax_args_copy,
                          reuse=reuse,
                          save=False,
                          sort=True,
                          name_str=s)

        summary = reduce.new_filter_reduce(res_,
                                           filter_keys=['odor_valence'],
                                           reduce_key='corrcoef')
        plot.plot_results(summary,
                          x_key='odor_valence',
                          y_key='corrcoef',
                          error_key='corrcoef_sem',
                          colors='black',
                          path=figure_path,
                          plot_args=error_args,
                          plot_function=plt.errorbar,
                          save=save,
                          reuse=True,
                          legend=False,
                          name_str=s)

        from scipy.stats import ranksums
        print(summary['corrcoef'])
Exemplo n.º 18
0
                day_dt_learned = filter.filter(day_dt_learned,
                                               {'odor_valence': 'CS+'})
                day_dt_learned['title'] = np.array(
                    ['DT_L'] * len(day_dt_learned['mouse']))

                day_dt_last = filter.filter_days_per_mouse(
                    summary_res, days_per_mouse=dt_last_day)
                day_dt_last = filter.filter(day_dt_last,
                                            {'odor_valence': 'CS+'})
                day_dt_last['title'] = np.array(['DT_E'] *
                                                len(day_dt_last['mouse']))

                from collections import defaultdict
                plot_res = defaultdict(list)
                plot_res = reduce.chain_defaultdicts(plot_res,
                                                     day_pt_start,
                                                     copy_dict=True)
                plot_res = reduce.chain_defaultdicts(plot_res,
                                                     day_pt_learned,
                                                     copy_dict=True)
                plot_res = reduce.chain_defaultdicts(plot_res,
                                                     day_dt_naive,
                                                     copy_dict=True)
                plot_res = reduce.chain_defaultdicts(plot_res,
                                                     day_dt_start,
                                                     copy_dict=True)
                plot_res = reduce.chain_defaultdicts(plot_res,
                                                     day_dt_learned,
                                                     copy_dict=True)
                plot_res = reduce.chain_defaultdicts(plot_res,
                                                     day_dt_last,
    res = analyze_behavior(data_path, condition)

    if 'YFP' in condition.name:
        res['condition'] = np.array(['YFP'] * len(res['mouse']))
    elif 'CHANNEL' in condition.name:
        res['condition'] = np.array(['CHANNEL'] * len(res['mouse']))
    else:
        res['condition'] = np.array([condition.name] * len(res['mouse']))

    list_of_res.append(res)
    names.append(condition.name)
directory_name = ','.join(names)
save_path_all = os.path.join(Config.LOCAL_FIGURE_PATH, 'BEHAVIOR', directory_name)
all_res = defaultdict(list)
for res, condition in zip(list_of_res, conditions):
    reduce.chain_defaultdicts(all_res, res)

ax_args_pt = {'yticks': [0, 5, 10], 'ylim': [-1, 12], 'xticks': [0, 100, 200, 300], 'xlim': [0, 300]}
bool_ax_args_pt = {'yticks': [0, 50, 100], 'ylim': [-5, 105], 'xticks': [0, 100, 200, 300], 'xlim': [0, 300]}
bar_args = {'alpha': .6, 'fill': False}
scatter_args = {'marker': 'o', 's': 10, 'alpha': .6}


lick = 'lick'
lick_smoothed = 'lick_smoothed'
boolean_smoothed = 'boolean_smoothed'
boolean_sem = 'boolean_smoothed_sem'
lick_sem = 'lick_smoothed_sem'

if 'trials_to_criterion' in experiments:
    reduce_key = 'criterion'
Exemplo n.º 20
0
            res1 = filter.exclude(res1, {'mouse': ['Y01']})
            res2 = filter.exclude(res2, {'mouse': ['Y01']})
        # if experiment.name == 'OFC_DT': # the unusual mouse
        #     res1 = filter.exclude(res1, {'mouse': ['H03']})
        #     res2 = filter.exclude(res2, {'mouse': ['H03']})

        keys = analysis.Indices().__dict__.keys()
        if experiment.name == 'OFC_PT' and directory == constants.pretraining_directory:
            mice = ['Y11','Y12','Y13']
            session_list = [[2,3,4,5,6], [5,7,9,11],[4,5,6]]

            for mouse, sessions in zip(mice, session_list):
                res2 = scalpel(res2, keys, mouse, sessions)
                print(np.unique(res2['session'][-1]))

        reduce.chain_defaultdicts(res, res1)
        reduce.chain_defaultdicts(res, res2)

print(np.unique(res['mouse']))

if collapse_arg:
    res1 = filter.filter(res, filter_dict={'experiment': collapse_arg, 'condition':'H'})
    res2 = filter.filter(res, filter_dict={'condition': 'Y'})
    res = reduce.chain_defaultdicts(res1, res2, copy_dict=True)

analysis.analyze(res)
analysis.shift_discrimination_index(res)
filter.assign_composite(res, ['phase', 'odor_valence'])
res = filter.filter(res, {'phase_odor_valence': ['Pretraining_CS+', 'Discrimination_CS+', 'Discrimination_CS-']})

if 'release_of_inhibition' in plotting:
Exemplo n.º 21
0
def plot_stability_across_days(res, start_days_per_mouse, learned_days_per_mouse, figure_path):
    strengthen_starting_threshold = .1
    strengthen_absolute_threshold = .2
    strengthen_relative_threshold = 2
    weaken_starting_threshold = .3
    weaken_absolute_threshold = .15
    weaken_relative_threshold = .5

    res = copy.copy(res)
    res = filter.filter(res, {'odor_valence':['CS+', 'CS-']})
    list_odor_on = res['DAQ_O_ON_F']
    list_water_on = res['DAQ_W_ON_F']
    for i in range(len(list_odor_on)):
        dff_list = res['dff'][i]
        dff_max = np.max(dff_list[:,list_odor_on[i]: list_water_on[i]],axis=1)
        res['dff_max'].append(dff_max)
    res['dff_max'] = np.array(res['dff_max'])

    out = defaultdict(list)
    combinations, ixs = filter.retrieve_unique_entries(res, ['mouse','odor'])
    for combination, ix in zip(combinations, ixs):
        odor_valence = np.unique(res['odor_valence'][ix])
        mouse = np.unique(res['mouse'][ix])
        days = res['day'][ix]
        assert len(days) == len(np.unique(days))
        assert len(mouse) == 1
        sort_ix = np.argsort(days)
        list_of_dff_max = res['dff_max'][ix][sort_ix]
        list_of_ssig = res['sig'][ix][sort_ix]
        data = [x * y for x, y in zip(list_of_ssig, list_of_dff_max)]
        data = np.array(data)

        strengthen_mask = data[0,:] > strengthen_starting_threshold
        strengthen_overall_threshold = np.max((data[0,:] * strengthen_relative_threshold, data[0,:] + strengthen_absolute_threshold), axis=0)
        strengthen_passed_mask = strengthen_overall_threshold < data
        strengthen_any = np.any(strengthen_passed_mask[1:,:], axis=0)
        n_strengthen_passed = np.sum(strengthen_any * strengthen_mask)
        n_strengthen_denom = np.sum(strengthen_mask)

        new_mask = np.invert(strengthen_mask)
        n_new_passed = np.sum(strengthen_any * new_mask)
        n_new_denom = np.sum(new_mask)

        weaken_mask = data[0,:] > weaken_starting_threshold
        weaken_overall_threshold = np.min((data[0,:] * weaken_relative_threshold, data[0,:] - weaken_absolute_threshold), axis=0)
        weaken_passed_mask = weaken_overall_threshold > data
        weaken_any = np.any(weaken_passed_mask[1:,:], axis=0)
        n_weaken_passed = np.sum(weaken_any * weaken_mask)
        n_weaken_denom = np.sum(weaken_mask)

        strs = ['strengthen_denom', 'strengthen_pass', 'new_denom', 'new_pass','weaken_denom','weaken_pass']
        vals = [n_strengthen_denom, n_strengthen_passed, n_new_denom, n_new_passed, n_weaken_denom, n_weaken_passed]

        for k, v in zip(strs,vals):
            out['Type'].append(k)
            out['Count'].append(v)
            out['mouse'].append(mouse[0])
            out['odor_valence'].append(odor_valence[0])
    for k, v in out.items():
        out[k] = np.array(v)

    def _helper(res):
        out = defaultdict(list)
        strs = [['strengthen_denom', 'strengthen_pass'], ['new_denom', 'new_pass'], ['weaken_denom', 'weaken_pass']]
        out_strs = ['up', 'new', 'down']
        for i, keys in enumerate(strs):
            ix_denom = res['Type'] == keys[0]
            ix_numer = res['Type'] == keys[1]
            numer = np.sum(res['Count'][ix_numer])
            denom = np.sum(res['Count'][ix_denom])
            fraction =  numer/ denom
            out['Type'].append(out_strs[i])
            out['Fraction'].append(fraction)
            out['numer'].append(numer)
            out['denom'].append(denom)
        for k, v in out.items():
            out[k] = np.array(v)
        return out

    ax_args_copy = ax_args.copy()
    ax_args_copy.update({'ylim':[0, 0.5], 'yticks':[0, .1, .2, .3, .4, .5]})
    overall = copy.copy(out)
    csm_res = filter.filter(overall, {'odor_valence':'CS-'})
    csp_res = filter.filter(overall, {'odor_valence':'CS+'})

    csp_stats = _helper(csp_res)
    csp_stats['Condition'] = np.array(['CS+'] * len(csp_stats['Type']))
    csm_stats = _helper(csm_res)
    csm_stats['Condition'] = np.array(['CS-'] * len(csm_stats['Type']))
    overall_stats = reduce.chain_defaultdicts(csp_stats, csm_stats, copy_dict=True)
    filter.assign_composite(overall_stats, ['Type', 'Condition'])

    bar_args = {'alpha': 1, 'edgecolor':'black','linewidth':1}
    colors = ['Green','Red']
    save_path, name = plot.plot_results(overall_stats, x_key='Type_Condition', y_key='Fraction', sort=True,
                                        colors=colors,
                                        path=figure_path,
                                        plot_function=plt.bar, plot_args=bar_args, ax_args=ax_args_copy,
                                        save=False, reuse=False)

    strs = ['down','new','up']
    fontsize = 4
    for i in range(3):
        ix = csp_stats['Type'] == strs[i]
        numer = csp_stats['numer'][ix][0]
        denom = csp_stats['denom'][ix][0]

        x = i * 2
        y = numer/denom
        y_offset = .025
        plt.text(x, y + y_offset, str(numer) + '/' + str(denom), horizontalalignment = 'center', fontsize= fontsize)

    strs = ['down','new','up']
    for i in range(3):
        ix = csm_stats['Type'] == strs[i]
        numer = csm_stats['numer'][ix][0]
        denom = csm_stats['denom'][ix][0]

        x = i * 2 + 1
        y = numer/denom
        y_offset = .025
        plt.text(x, y + y_offset, str(numer) + '/' + str(denom), horizontalalignment = 'center', fontsize= fontsize)
    plot._easy_save(save_path, name, pdf=True)

    overall_stats_no_distinction = _helper(overall)
    save_path, name = plot.plot_results(overall_stats_no_distinction, x_key='Type', y_key='Fraction', sort=True,
                      colors=['Grey']*10,
                      path=figure_path,
                      plot_function=plt.bar, plot_args=bar_args, ax_args=ax_args_copy,
                      save=False, reuse=False)
    strs = ['down','new','up']
    for i in range(3):
        ix = overall_stats_no_distinction['Type'] == strs[i]
        numer = overall_stats_no_distinction['numer'][ix][0]
        denom = overall_stats_no_distinction['denom'][ix][0]

        x = i
        y = numer/denom
        y_offset = .025
        plt.text(x, y + y_offset, str(numer) + '/' + str(denom), horizontalalignment = 'center', fontsize= fontsize)
    plot._easy_save(save_path, name, pdf=True)