Beispiel #1
0
def main():
    for i, mouse in enumerate(MOUSE):
        correct_decoding_percentage = []
        edge_decoding_percentage = []
        p_val_correct = []
        p_val_edge = []
        decoded_bins_all_sessions = {'envA': [], 'envB': []}
        decoded_env_all_sessions = {'envA': [], 'envB': []}
        number_of_events_per_frame_all_sessions = {'envA': [], 'envB': []}
        all_bins = {'envA': [], 'envB': []}
        all_velocity = {'envA': [], 'envB': []}
        cell_reg_filename = WORK_DIR + '\c%dm%d\cellRegisteredFixed.mat' \
                                       %(CAGE[i], mouse)
        cell_registration = matlab.load_cell_registration(cell_reg_filename)

        for session_ind, day in enumerate(DAYS):
            print CAGE[i], mouse, day
            place_cells = []
            p_neuron_bin = {}
            # Create training data for environment A
            session_dir = WORK_DIR + '\c%dm%d\day%s\%s'\
                                     %(CAGE[i], mouse, day, ENV[0])
            events_tracesA, movement_dataA = load_session_data\
                (session_dir, cell_registration, 2*session_ind)
            linear_trials_indicesA = range(len(events_tracesA))[1:-1]
            bucket_trials_indicesA = [0, len(events_tracesA) - 1]
            [binsA, eventsA] = create_training_data\
                (movement_dataA, events_tracesA, linear_trials_indicesA)

            # use only events of place cells:
            binsA = wide_binning(binsA, NUMBER_OF_BINS, SPACE_BINNING)
            velocityA = concatenate_movment_data\
                (movement_dataA, 'velocity', linear_trials_indicesA)
            all_bins['envA'].append(binsA)
            all_velocity['envA'].append(velocityA)
            velocity_positive = velocityA > VELOCITY_THRESHOLD
            velocity_negative = velocityA < -VELOCITY_THRESHOLD
            place_cells_positive, _, _ = find_place_cells\
                (binsA[velocity_positive], eventsA[:, velocity_positive])

            place_cells_negative, _, _ = find_place_cells\
                (binsA[velocity_negative], eventsA[:, velocity_negative])

            place_cellsA = np.concatenate\
                ([place_cells_positive, place_cells_negative])
            #
            place_cells.append(place_cellsA)

            # Create training data for environment B
            session_dir = WORK_DIR + '\c%dm%d\day%s\%s' % \
                                     (CAGE[i], mouse, day, ENV[1])

            events_tracesB, movement_dataB = load_session_data\
                (session_dir, cell_registration, 2*session_ind+1)

            linear_trials_indicesB = range(len(events_tracesB))[1:-1]
            bucket_trials_indicesB = [0, len(events_tracesB) - 1]
            [binsB, eventsB] = create_training_data\
                (movement_dataB, events_tracesB, linear_trials_indicesB)

            binsB = wide_binning(binsB, NUMBER_OF_BINS, SPACE_BINNING)
            velocityB = concatenate_movment_data\
                (movement_dataB, 'velocity', linear_trials_indicesB)
            all_bins['envB'].append(binsB)
            all_velocity['envB'].append(velocityB)
            velocity_positive = velocityB > VELOCITY_THRESHOLD
            velocity_negative = velocityB < -VELOCITY_THRESHOLD
            place_cells_positive, _, _ = find_place_cells\
                (binsB[velocity_positive], eventsB[:, velocity_positive])

            place_cells_negative, _, _ = find_place_cells\
                (binsB[velocity_negative], eventsB[:, velocity_negative])

            place_cellsB = np.concatenate\
                ([place_cells_positive, place_cells_negative])

            place_cells.append(place_cellsB)

            place_cells = np.concatenate(place_cells)
            place_cells = np.unique(place_cells)

            # dividing into two directions - positive, negative
            p_neuron_binA_positive = maximum_likelihood.calculate_p_r_s_matrix\
                (binsA[velocityA > VELOCITY_THRESHOLD],
                 eventsA[place_cells, :][:, velocityA >VELOCITY_THRESHOLD])
            p_neuron_bin['envA_positive'] = [p_neuron_binA_positive]

            p_neuron_binA_negative = maximum_likelihood.calculate_p_r_s_matrix\
                (binsA[velocityA < -VELOCITY_THRESHOLD],
                 eventsA[place_cells, :][:, velocityA < -VELOCITY_THRESHOLD])
            p_neuron_bin['envA_negative'] = [p_neuron_binA_negative]

            p_neuron_binB_positive = maximum_likelihood.calculate_p_r_s_matrix\
                (binsB[velocityB > VELOCITY_THRESHOLD],
                 eventsB[place_cells, :][:, velocityB > VELOCITY_THRESHOLD])
            p_neuron_bin['envB_positive'] = [p_neuron_binB_positive]

            p_neuron_binB_negative = maximum_likelihood.calculate_p_r_s_matrix\
                (binsB[velocityB < -VELOCITY_THRESHOLD],
                 eventsB[place_cells, :][:, velocityB < -VELOCITY_THRESHOLD])
            p_neuron_bin['envB_negative'] = [p_neuron_binB_negative]

            for trial in range(2):
                trial_events_A = events_tracesA[bucket_trials_indicesA[trial]]\
                [place_cells, :]
                statistics , decoded_bins, decoded_env = \
                    test_bucket_trial(trial_events_A, p_neuron_bin, EDGE_BINS)

                number_of_events_per_frame = np.sum(trial_events_A > 0, axis=0)
                number_of_events_per_frame = \
                    number_of_events_per_frame[number_of_events_per_frame > 0]

                decoded_bins_all_sessions['envA'].append(decoded_bins)
                decoded_env_all_sessions['envA'].append(decoded_env)
                number_of_events_per_frame_all_sessions['envA'].append\
                    (number_of_events_per_frame)

                correct_decoding_percentage.append\
                    (statistics['envA']['overall_decoding_fraction'])
                #
                # edge_decoding_percentage.append\
                #     (statistics['envA']['edge_decoding_fraction'])
                #
                # p_val = calculate_p_val_for_correct_decoding_trial\
                #     (trial_events_A, p_neuron_bin, EDGE_BINS, statistics['envA'],
                #     NUMBER_OF_PERMUTATIONS, 'envA')
                #
                # p_val_correct.append(p_val['overall_decoding_fraction'])
                # p_val_edge.append(p_val['edge_decoding_fraction'])

                trial_events_B = events_tracesB[bucket_trials_indicesB[trial]]\
                    [place_cells, :]

                statistics, decoded_bins, decoded_env  = \
                    test_bucket_trial(trial_events_B, p_neuron_bin, EDGE_BINS)

                number_of_events_per_frame = np.sum(trial_events_B > 0, axis=0)
                number_of_events_per_frame = \
                    number_of_events_per_frame[number_of_events_per_frame > 0]

                decoded_bins_all_sessions['envB'].append(decoded_bins)
                decoded_env_all_sessions['envB'].append(decoded_env)
                number_of_events_per_frame_all_sessions['envB'].append \
                    (number_of_events_per_frame)

                correct_decoding_percentage.append\
                    (statistics['envB']['overall_decoding_fraction'])
                #
                # edge_decoding_percentage.append\
                #     (statistics['envB']['edge_decoding_fraction'])
                #
                # p_val = calculate_p_val_for_correct_decoding_trial\
                #     (trial_events_A, p_neuron_bin, EDGE_BINS, statistics['envB'],
                #      NUMBER_OF_PERMUTATIONS, 'envB')
                #
                # p_val_correct.append(p_val['overall_decoding_fraction'])
                # p_val_edge.append(p_val['edge_decoding_fraction'])

        # np.savez('bucket_decoding_statistics_c%sm%s' %(CAGE[i], mouse),
        #          correct_decoding_percentage = correct_decoding_percentage,
        #          edge_decoding_percentage = edge_decoding_percentage,
        #          p_val_correct = p_val_correct,
        #          p_val_edge = p_val_edge)

        np.savez('bucket_decoding_results_c%sm%s' % (CAGE[i], mouse),
                 correct_decoding_percentage=correct_decoding_percentage,
                 decoded_bins_all_sessions=decoded_bins_all_sessions,
                 decoded_env_all_sessions=decoded_env_all_sessions,
                 number_of_events_per_frame_all_sessions=
                 number_of_events_per_frame_all_sessions)

        np.savez('bins_velocity_c%sm%s' % (CAGE[i], mouse),
                 all_bins=all_bins,
                 all_velocity=all_velocity)
        #
        np.savez('p_neuron_bin_c%sm%s' % (CAGE[i], mouse),
                 p_neuron_bin=p_neuron_bin)

    raw_input('press enter')
Beispiel #2
0
def main():
    for i, mouse in enumerate(MOUSE):
        cell_reg_filename = WORK_DIR + '\c%dm%d\cellRegisteredFixed.mat' \
                                       %(CAGE[i], mouse)
        cell_registration = matlab.load_cell_registration(cell_reg_filename)

        for session_ind, day in enumerate(DAYS):
            print CAGE[i], mouse, day
            # Create training data for environment A
            session_dir = WORK_DIR + '\c%dm%d\day%s\%s' \
                                     % (CAGE[i], mouse, day, ENV[0])
            events_tracesA, movement_dataA = load_session_data \
                (session_dir, cell_registration, 2*session_ind)
            linear_trials_indicesA = range(len(events_tracesA))[1:-1]
            bucket_trials_indicesA = [0, len(events_tracesA) - 1]

            # Create training data from one linear track trial and one bucket
            # trial - the bucket bin will be bin no. 12
            linear_training_trial = 1
            [train_bins, train_events] = activity_loading.create_training_data \
                (movement_dataA, events_tracesA, [linear_training_trial])
            train_events = train_events > 0
            train_bins = activity_loading.wide_binning(train_bins,
                                                       NUMBER_OF_BINS,
                                                       SPACE_BINNING)

            bucket_training_trial = 0
            bucket_events = events_tracesA[bucket_training_trial] > 0
            bucket_bins = np.ones(bucket_events.shape[1]) * 12

            # use only events of place cells:
            [binsA, eventsA] = activity_loading.create_training_data \
                (movement_dataA, events_tracesA, linear_trials_indicesA)
            eventsA = eventsA > 0
            binsA = activity_loading.wide_binning(binsA, NUMBER_OF_BINS,
                                                  SPACE_BINNING)
            velocityA = activity_loading.concatenate_movment_data\
                (movement_dataA, 'velocity', linear_trials_indicesA)

            velocity_positive = velocityA > VELOCITY_THRESHOLD
            velocity_negative = velocityA < -VELOCITY_THRESHOLD
            place_cells_positive, _, _ = find_place_cells\
                (binsA[velocity_positive], eventsA[:, velocity_positive])

            place_cells_negative, _, _ = find_place_cells\
                (binsA[velocity_negative], eventsA[:, velocity_negative])

            place_cellsA = np.concatenate\
                ([place_cells_positive, place_cells_negative])

            # Concatenate the training data
            training_bins = np.concatenate([train_bins, bucket_bins])
            training_bins = np.array(training_bins, dtype=int)
            training_events = np.hstack([
                train_events[place_cellsA, :], bucket_events[place_cellsA, :]
            ])

            # Calculate the probability matrix
            p_neuron_bin = maximum_likelihood.calculate_p_r_s_matrix(
                training_bins, training_events)

            # Test the second bucket trial
            test_trial = events_tracesA[bucket_trials_indicesA[1]][
                place_cellsA, :]
            decoded_bins = test_bucket_trial(test_trial, p_neuron_bin)

            plt.figure()
            plt.plot(decoded_bins, '*')
            plt.show()

    raw_input('enter')
Beispiel #3
0
def main():
    mice_data = dict.fromkeys(MICE)
    for mouse in MICE:
        print mouse
        number_of_edge_cells_a = []
        number_of_edge_cells_b = []
        unique_edge_cells = []
        edge_cells_high_corr_fraction = []
        for day in DAYS:
            print "day %d" % day
            try:
                # Load session data
                day_path = os.path.join(DATA_PATH, mouse, 'day%d' % day)
                env_a_path = os.path.join(day_path, 'envA')
                env_b_path = os.path.join(day_path, 'envB')
                cell_reg_filename = os.path.join(CELL_REGISTRATION_PATH, mouse,
                                                 "cellRegisteredFixed.mat")
                cell_registration = matlab.load_cell_registration(
                    cell_reg_filename)
                events_a, movement_a = load_two_env_session(
                    env_a_path,
                    session_ind=2 * (day - 1),
                    cell_registration=cell_registration)
                events_b, movement_b = load_two_env_session(
                    env_b_path,
                    session_ind=2 * (day),
                    cell_registration=cell_registration)

                # Find the edge cells of the session
                edge_cells_a = find_env_edge_cells(events_a, movement_a)
                edge_cells_b = find_env_edge_cells(events_b, movement_b)
                all_edge_cells = np.unique(
                    np.concatenate([edge_cells_a, edge_cells_b]))

                # Plot edge cells activity
                flat_events_a, flat_bins_a, _ = flat_session(
                    events_a, movement_a)
                flat_events_b, flat_bins_b, _ = flat_session(
                    events_b, movement_b)
                cells_event_count = []
                cells_corr = []
                for cell in all_edge_cells:
                    title = "Cell no. %d" % cell
                    cell_activity_a = flat_events_a[cell, :]
                    cell_activity_b = flat_events_b[cell, :]
                    # plot_comparison_of_two_sessions(
                    #   cell_activity_a, flat_bins_a,cell_activity_b,
                    #   flat_bins_b, title)
                    n_a, n_b = \
                        plot_hist_comparison(cell_activity_a, flat_bins_a,
                                                     cell_activity_b, flat_bins_b, title)
                    cells_event_count.append([n_a, n_b])
                    cells_corr.append(np.corrcoef(n_a, n_b)[0, 1])

                    # print "Cell no. %d correlation coefficient: %f" % \
                    #       (cell, cells_corr[-1])

                number_of_edge_cells_a.append(len(edge_cells_a))
                number_of_edge_cells_b.append(len(edge_cells_b))
                unique_edge_cells.append(len(all_edge_cells))
                edge_cells_high_corr_fraction.append(
                    np.sum(np.array(cells_corr) > 0.65) /
                    np.float(len(all_edge_cells)))
                print "number of edge cells in environment A: %d" % len(
                    edge_cells_a)
                print "number of edge cells in environment B: %d" % len(
                    edge_cells_b)
                print "number of unique edge cells: %d" % len(all_edge_cells)
                print "Fraction of edge cells above 0.65 correlation: %f" % edge_cells_high_corr_fraction[
                    -1]
                plt.close('all')

            except Exception as e:
                print "Error in day %d" % day
                import pdb
                pdb.set_trace()

        mice_data[mouse] = {
            'Edge cells A': number_of_edge_cells_a,
            'Edge cells B': number_of_edge_cells_b,
            'All edge cells': unique_edge_cells,
            'High correlation edge cells fraction':
            edge_cells_high_corr_fraction
        }

        raw_input('press enter')

    plot_all_mice_summary(mice_data)
def main():
    for i, mouse in enumerate(MOUSE):
        cell_reg_filename = WORK_DIR + '\c%dm%d\cellRegisteredFixed.mat' \
                                       %(CAGE[i], mouse)
        cell_registration = matlab.load_cell_registration(cell_reg_filename)

        for session_ind, day in enumerate(DAYS):
            print 'C%dM%d day %s' % (CAGE[i], mouse, day)
            # Create training data for environment A
            session_dir = WORK_DIR + '\c%dm%d\day%s\%s' \
                                     % (CAGE[i], mouse, day, ENV[0])
            events_tracesA, movement_dataA = load_session_data \
                (session_dir, cell_registration, 2*session_ind)
            linear_trials_indicesA = range(len(events_tracesA))[1:-1]
            bucket_trials_indicesA = [0, len(events_tracesA) - 1]

            # Create training data from one linear track trial
            linear_training_trial = 5
            [train_bins, train_events] = activity_loading.create_training_data \
                (movement_dataA, events_tracesA, [linear_training_trial])
            train_events = train_events > 0
            train_bins = activity_loading.wide_binning(train_bins,
                                                       NUMBER_OF_BINS,
                                                       SPACE_BINNING)
            train_velocity = activity_loading.concatenate_movment_data(
                movement_dataA, 'velocity', [linear_training_trial])

            # Calculate the population vector of the trial:
            positive_velocity_indices = train_velocity > VELOCITY_THRESHOLD
            negative_velocity_indices = train_velocity < VELOCITY_THRESHOLD
            positive_trial_population_vector = np.sum(
                train_events[:, positive_velocity_indices], axis=1) / \
                                       float(len(positive_velocity_indices))
            negative_trial_population_vector = np.sum(
                train_events[:, negative_velocity_indices], axis=1) / \
                                           float(len(negative_velocity_indices))

            # Calculate SCEs in bucket trials
            bucket_trial_index = 1
            bucket_events = events_tracesA[bucket_trial_index] > 0
            sce_chance_level = sce.calculte_SCE_chance_level(
                bucket_events, FRAME_RATE)
            print sce_chance_level
            sce_mask = sce.find_SCE_in_full_epoch(bucket_events,
                                                  sce_chance_level, FRAME_RATE)
            sce_population_vectors, sce_locations = create_sce_population_vector(
                bucket_events, sce_mask, sce.WINDOW, FRAME_RATE)

            # Correlate between the SCE population vectors to the trial's
            # population vector
            number_of_sces = len(sce_population_vectors)
            sce_activity_corr_positive = np.zeros(number_of_sces)
            sce_activity_corr_negative = np.zeros(number_of_sces)
            number_of_active_neurons = np.zeros(number_of_sces)
            for j, sce_pv in enumerate(sce_population_vectors):
                number_of_active_neurons[j] = np.sum(sce_pv > 0)
                sce_activity_corr_positive[j] = np.corrcoef(
                    sce_pv, positive_trial_population_vector)[0, 1]
                sce_activity_corr_negative[j] = np.corrcoef(
                    sce_pv, negative_trial_population_vector)[0, 1]

            # Caclculate the significance of results of positive direction
            correlation_significance_level, unique_number_of_active_neurons = \
                find_sce_correlation_significance_level(
                    bucket_events, number_of_active_neurons,
                    positive_trial_population_vector, NUMBER_OF_PERMUTATIONS,
                    alpha=0.025)

            positive_significant_sce_correlation = np.zeros_like(
                sce_activity_corr_positive, dtype=bool)
            for j, sce_corr in enumerate(sce_activity_corr_positive):
                n = number_of_active_neurons[j]
                significance_level = correlation_significance_level[
                    np.argwhere(unique_number_of_active_neurons == n)[0][0]]
                positive_significant_sce_correlation[
                    j] = sce_corr >= significance_level


            print '%d out of %d significant SCEs (positive direction)' \
                % (np.sum(positive_significant_sce_correlation), number_of_sces)

            # Caclculate the significance of results of negative direction
            correlation_significance_level, unique_number_of_active_neurons = \
                find_sce_correlation_significance_level(
                    bucket_events, number_of_active_neurons,
                    negative_trial_population_vector, NUMBER_OF_PERMUTATIONS,
                    alpha=0.025)

            negative_significant_sce_correlation = np.zeros_like(
                sce_activity_corr_negative, dtype=bool)
            for j, sce_corr in enumerate(sce_activity_corr_negative):
                n = number_of_active_neurons[j]
                significance_level = correlation_significance_level[
                    np.argwhere(unique_number_of_active_neurons == n)[0][0]]
                negative_significant_sce_correlation[
                    j] = sce_corr >= significance_level

            print '%d out of %d significant SCEs (negative direction)' \
                  % (np.sum(negative_significant_sce_correlation), number_of_sces)

            all_significant_sce_correlation = \
                positive_significant_sce_correlation | \
                negative_significant_sce_correlation

            # Plot raster of the SCE activity
            plot_raster_of_the_SCE_activity(bucket_events, sce_locations,
                                            all_significant_sce_correlation)

            # Plot SCE's cell activity on track - each cell separatly
            # significant_sce_locations = sce_locations[all_significant_sce_correlation]
            # for s in significant_sce_locations:
            # sce_activity = bucket_events[:, s:s+int(sce.WINDOW*FRAME_RATE)]
            # active_neurons = np.sum(sce_activity, axis=1) > 0
            # active_neurons_events = train_events[active_neurons, :]
            # plot_cells_activity_on_track(active_neurons_events, train_bins)
            # raw_input('enter')

            # Plot SCE's cell activity on track - together
            significant_sce_locations = sce_locations[
                all_significant_sce_correlation]
            for s in significant_sce_locations:
                sce_activity = bucket_events[:, s:s +
                                             int(sce.WINDOW * FRAME_RATE)]
                active_neurons = np.sum(sce_activity, axis=1) > 0
                active_neurons_events = train_events[active_neurons, :]
                plot_all_cells_activity_on_track(active_neurons_events,
                                                 train_bins)
                raw_input('enter')
Beispiel #5
0
def main():

    for i, mouse in enumerate(MOUSE):
        # Creating a list of all mouse events and behavioral data
        mouse_events = []
        mouse_movement = []
        mouse_place_cells = []

        cell_reg_filename = WORK_DIR[i] + '\c%dm%d\cellRegistered_%s.mat' % (
            CAGE[i], mouse, ENV[i][1:])
        cell_registration = matlab.load_cell_registration(cell_reg_filename)
        mouse_dir = WORK_DIR[i] + '\c%dm%d' % (CAGE[i], mouse)
        days_list = [x[1] for x in os.walk(mouse_dir)][0]

        for day in days_list:
            print 'loading data set for', CAGE[i], mouse, day

            # load the session data
            session_dir = mouse_dir + '\%s%s' % (day, ENV[i])
            print session_dir
            session_ind = int(day[-1]) - 1
            events_traces, movement_data = \
                load_session_data(session_dir, cell_registration, session_ind)

            mouse_events.append(events_traces)
            mouse_movement.append(movement_data)

            # find the place cells of current session (differencing directions):
            linear_trials_indices = range(len(events_traces))[1:-1]
            [bins, events] = create_training_data\
                (movement_data, events_traces, linear_trials_indices)

            bins = wide_binning(bins, NUMBER_OF_BINS, SPACE_BINNING)
            velocity = concatenate_movment_data\
                (movement_data, 'velocity', linear_trials_indices)

            velocity_positive = velocity > VELOCITY_THRESHOLD
            velocity_negative = velocity < -VELOCITY_THRESHOLD
            place_cells_positive, _, _ = find_place_cells\
                (bins[velocity_positive], events[:, velocity_positive],
                 min_number_of_events=MIN_NUMBER_OF_EVENTS)

            place_cells_negative, _, _ = find_place_cells\
                (bins[velocity_negative], events[:, velocity_negative],
                 min_number_of_events=MIN_NUMBER_OF_EVENTS)

            place_cells = np.concatenate\
                ([place_cells_positive, place_cells_negative])

            place_cells = np.unique(place_cells)

            mouse_place_cells.append(place_cells)

        # Loop on all sessions, for train and test inside session and between
        # sessions

        mean_error_all_sessions = [[] for j, _ in enumerate(days_list)]
        pval_for_mean_error = [[] for j, _ in enumerate(days_list)]
        mean_error_permutaion_all_sessions = [[]
                                              for j, _ in enumerate(days_list)]
        for train_session_ind, day in enumerate(days_list):
            print 'training on data set for', CAGE[i], mouse, day
            # Create p_neuron_bin with all session trials for testing with other
            # sessions - Notice! the probability is for all neurons! there is a
            # need to separate for the common place cells in different sessions
            train_movement_data = mouse_movement[train_session_ind]
            train_events_traces = mouse_events[train_session_ind]
            train_place_cells = mouse_place_cells[train_session_ind]
            linear_trials_indices = range(len(train_events_traces))[1:-1]

            p_neuron_bin = create_p_neuron_bin(train_movement_data,
                                               train_events_traces,
                                               linear_trials_indices)

            number_of_days = len(days_list)
            for test_session_ind in np.arange(train_session_ind,
                                              number_of_days):
                print 'testing on data set for', CAGE[i], mouse, days_list[
                    test_session_ind]
                # The case below is a special case where the session that is
                # tested is the session that the decoder trained on. and so,
                # for each trial there is a need to leave the tested trial out
                # of the training trials
                if test_session_ind == train_session_ind:
                    test_trials_indices = linear_trials_indices
                    for test_trial in test_trials_indices:
                        print test_trial
                        train_trials_indices = range(
                            len(train_events_traces))[1:-1]
                        train_trials_indices.remove(test_trial)
                        current_p_neuron_bin = create_p_neuron_bin(
                            train_movement_data, train_events_traces,
                            train_trials_indices)

                        current_p_neuron_bin = [
                            x[train_place_cells, :]
                            for x in current_p_neuron_bin
                        ]
                        [test_bins, test_events
                         ] = create_training_data(train_movement_data,
                                                  train_events_traces,
                                                  [test_trial])
                        test_bins = wide_binning(test_bins, NUMBER_OF_BINS,
                                                 SPACE_BINNING)
                        estimated_bins, _ = decode_trial \
                            (test_events[train_place_cells, :],
                             current_p_neuron_bin, FRAMES_TO_LOOK_BACK)

                        active_frames = np.sum(
                            test_events[train_place_cells, :], axis=0) > 0
                        # calculating the mean_error for the frames that had activity in,
                        # since the frames that didn't have activity, get the
                        # estimation of the previous frame
                        mean_error_bins = np.mean(
                            np.abs((test_bins[active_frames] -
                                    estimated_bins[active_frames])))
                        # pval, mae_permutation = calculate_p_val_for_mae(test_events[train_place_cells, :],
                        #                         test_bins,
                        #                         current_p_neuron_bin,
                        #                         mean_error_bins,
                        #                         NUMBER_OF_PERMUTATIONS)

                        session_details = 'C%sM%s - train session: %d, ' \
                                          'test session+trial: %d %d\n mean error: %d'\
                                          %(CAGE[i], mouse, train_session_ind,
                                            test_session_ind, test_trial, float(mean_error_bins))
                        plot_decoded_bins(estimated_bins[active_frames],
                                          test_bins[active_frames],
                                          session_details)

                        # plot_decoded_bins(estimated_bins, test_bins,
                        #                   session_details)

                        # mean_error_all_sessions \
                        #     [np.abs(train_session_ind - test_session_ind)]. \
                        #     append(mean_error_bins)
                        # pval_for_mean_error \
                        #     [np.abs(train_session_ind - test_session_ind)]. \
                        #     append(pval)
                        # mean_error_permutaion_all_sessions \
                        #     [np.abs(train_session_ind - test_session_ind)]. \
                        #     append(mae_permutation)

                # The case below is for different sessions for training and testing
                # else:
                #     test_movement_data = mouse_movement[test_session_ind]
                #     test_events_traces = mouse_events[test_session_ind]
                #     test_place_cells = mouse_place_cells[test_session_ind]
                #     shared_place_cells = np.intersect1d(train_place_cells,
                #                                    test_place_cells)
                #     test_trials_indices = range(len(test_events_traces))[1:-1]
                #     current_p_neuron_bin = [x[shared_place_cells, :] for x in p_neuron_bin]
                #
                #     for test_trial in test_trials_indices:
                #         [test_bins, test_events] = create_training_data(
                #             test_movement_data, test_events_traces, [test_trial])
                #         test_bins = wide_binning(test_bins, NUMBER_OF_BINS,
                #                                  SPACE_BINNING)
                #         estimated_bins, _ = decode_trial \
                #             (test_events[shared_place_cells, :],
                #              current_p_neuron_bin, FRAMES_TO_LOOK_BACK)
                #
                #         active_frames = np.sum(test_events, axis=0) > 0
                #         mean_error_bins = np.mean(np.abs((test_bins[active_frames] -
                #                            estimated_bins[active_frames])))
                #         pval, mae_permutation = calculate_p_val_for_mae(
                #             test_events[shared_place_cells, :],
                #             test_bins,
                #             current_p_neuron_bin,
                #             mean_error_bins,
                #             NUMBER_OF_PERMUTATIONS)
                #
                #         session_details = 'C%sM%s - train session: %d, ' \
                #                           'test session+trial: %d %d\n mean error: %d' \
                #                           % (CAGE[i], mouse, train_session_ind,
                #                              test_session_ind, test_trial,
                #                              float(mean_error_bins))

                # plot_decoded_bins(estimated_bins, test_bins,
                #                   session_details)
                #
                # mean_error_all_sessions \
                #     [np.abs(train_session_ind - test_session_ind)]. \
                #     append(mean_error_bins)
                # pval_for_mean_error \
                #     [np.abs(train_session_ind - test_session_ind)]. \
                #     append(pval)
                # mean_error_permutaion_all_sessions\
                # [np.abs(train_session_ind - test_session_ind)].\
                #     append(mae_permutation)

        # print 'saving: linear_track_decoding_results_c%sm%s' % (CAGE[i], mouse)
        # np.savez('linear_track_decoding_results_c%sm%s' % (CAGE[i], mouse),
        #          mean_error_all_sessions=mean_error_all_sessions,
        #          mean_error_permutaion_all_sessions =
        #          mean_error_permutaion_all_sessions,
        #          pval_for_mean_error=pval_for_mean_error)

        # print 'saving: Lshape_track_decoding_results_c%sm%s' % (CAGE[i], mouse)
        # np.savez('Lshape_track_decoding_results_c%sm%s' % (CAGE[i], mouse),
        #          mean_error_all_sessions=mean_error_all_sessions,
        #          mean_error_permutaion_all_sessions=
        #          mean_error_permutaion_all_sessions,
        #          pval_for_mean_error=pval_for_mean_error)
        raw_input('press enter')
        close("all")