Ejemplo n.º 1
0
def generate_rdm_all_gradient(nnet,
                              name,
                              blanks,
                              rdm_type=analysis.SPEARMAN,
                              save_files=True,
                              title="RDM training combined",
                              from_file=False,
                              delete_blank_states=True):
    if not from_file:
        if rdm_type != analysis.SPEARMAN:
            raise Exception("not implemented")
        hidden_both, accuracy_totals_both, accuracy_fullseqs_both = test_network_all(
            nnet)
        hidden_ari, accuracy_totals_ari, accuracy_fullseqs_ari = test_network_ari(
            nnet, blanks)
        hidden_bev, accuracy_totals_bev, accuracy_fullseqs_bev = test_network_bev(
            nnet, blanks)
        print("Both: {0}, {1}".format(accuracy_totals_both,
                                      accuracy_fullseqs_both))
        print("Ari: {0}, {1}".format(accuracy_totals_ari,
                                     accuracy_fullseqs_ari))
        print("Bev: {0}, {1}".format(accuracy_totals_bev,
                                     accuracy_fullseqs_bev))

        hidden = utils.flatten_onelevel(hidden_bev) +\
                 utils.flatten_onelevel(hidden_ari) +\
                 utils.flatten_onelevel(hidden_both)

        hidden_left = []
        hidden_right = []
        for vector in hidden:
            hidden_left.append(vector[:len(vector) // 2])
            hidden_right.append(vector[len(vector) // 2:])

        # Now cut the hidden layer in two.
        rdmatrix_left = analysis.rdm_spearman(hidden_left)
        rdmatrix_right = analysis.rdm_spearman(hidden_right)
        # save the massive rdm for debug purposes (so that I don't have to generate it all over again everytime).
        utils.save_object(name + "rdmatright", rdmatrix_right)
        utils.save_object(name + "rdmatleft", rdmatrix_left)
    else:
        rdmatrix_left = utils.load_object(name + "rdmatleft")
        rdmatrix_right = utils.load_object(name + "rdmatright")

    rdmatrix_left, labels = model2.process_matrix(rdmatrix_left,
                                                  delete_blank_states)
    rdmatrix_right, _ = model2.process_matrix(rdmatrix_right,
                                              delete_blank_states)

    return rdmatrix_left, rdmatrix_right, labels
Ejemplo n.º 2
0
def make_rdm_and_mds(name, with_goals=False):
    model = utils.load_object(name, 1)  # eg 'noise_test2'
    if with_goals:
        hidden = pnashierarchy.accuracy_test_with_goals(model)
    else:
        hidden = accuracy_test(model, noise)
    # Turn into a list of simple vectors
    for i, tensor in enumerate(hidden):
        hidden[i] = tensor.numpy().reshape(-1)
    rdmatrix = analysis.rdm_spearman(hidden)
    labels = []
    for i, sequence in enumerate(pnas2018task.seqs):
        for action in sequence[1:]:
            labels.append(str(i) + '_' + action)
    analysis.plot_rdm(rdmatrix, labels, "Spearman rho matrix")

    for i in range(4):
        mdsy = analysis.mds(hidden[6 * i:6 * i + 6])
        analysis.plot_mds_points(mdsy,
                                 range(len(mdsy)),
                                 labels=labels[6 * i:6 * i + 6])

    mdsy = analysis.mds(hidden)
    for i, style in enumerate(['ro-', 'b|--', 'gx-.', 'k_:']):
        analysis.plot_mds_points(mdsy[6 * i:6 * i + 6],
                                 range(6),
                                 labels=labels[6 * i:6 * i + 6],
                                 style=style,
                                 show=(i == 3))
Ejemplo n.º 3
0
def make_rdm_and_mds_reg_hierarchy(name):
    model = utils.load_object(name, 1)  # eg 'noise_test2'
    hidden = accuracy_test_reg_hierarchy(model)
    # Turn a list of tensors into a list of np vectors
    for i, tensor in enumerate(hidden):
        hidden[i] = tensor.numpy().reshape(-1)

    # Now cut that in two
    left_units = [vector[:len(hidden) / 2] for vector in hidden]
    right_units = [vector[len(hidden) / 2:] for vector in hidden]

    # Make the labels for the rdms
    labels = []
    for i, sequence in enumerate(pnas2018task.seqs):
        for action in sequence[1:]:
            labels.append(str(i) + '_' + action)
    for side in [[left_units, "left units"], [right_units, "right_units"]]:
        rdm = analysis.rdm_spearman(side[0])
        analysis.plot_rdm(rdm, labels, "Spearman rho matrix" + side[1])

        #for i in range(4):
        #    mdsy = analysis.mds(side[0][6*i:6*i+6])
        #    analysis.plot_mds_points(mdsy, range(len(mdsy)), labels=labels[6*i:6*i+6])

        mdsy = analysis.mds(side[0])
        for i, style in enumerate(['ro-', 'b|--', 'gx-.', 'k_:']):
            analysis.plot_mds_points(mdsy[6 * i:6 * i + 6],
                                     range(6),
                                     labels=labels[6 * i:6 * i + 6],
                                     style=style)
Ejemplo n.º 4
0
def make_rdm_and_mds(name):
    model = utils.load_object(name)  # eg 'noise_test2'
    hidden = get_model_hidden_activations(model)
    rdmatrix = analysis.rdm_spearman(hidden)
    labels = []
    for goal in tce.action_list:
        for action in tce.action_list[goal]:
            labels.append(goal + '_' + action)
    analysis.plot_rdm(rdmatrix, labels, "Spearman rho matrix")
    mdsy = analysis.mds(hidden)
    analysis.plot_mds_points(mdsy, range(len(mdsy)), labels=labels)
Ejemplo n.º 5
0
def make_rdm_multiple_predictive(name, num_networks,title="-", save_files=True):
    # Make one rdm for each network
    optimal_list = []
    rdmatrices = []
    for i in range(num_networks):
        model = utils.load_object(name, i)
        hidden, optimal = accuracy_test_predictive(model, i)
        optimal_list.append(optimal)
        if optimal:
            # Turn into a list of simple vectors
            for i, tensor in enumerate(hidden):
                hidden[i] = tensor.numpy().reshape(-1)
            rdmatrix = analysis.rdm_spearman(hidden)
            rdmatrices.append(rdmatrix)
    print("{0} networks, of which {1} achieve optimal accuracy".format(num_networks, optimal_list.count(True)))
    # Now average over all matrices
    avg_matrix = None
    for matrix in rdmatrices:
        if avg_matrix is None:
            avg_matrix = matrix
        else:
            avg_matrix += matrix
    avg_matrix = avg_matrix / len(rdmatrices)

    # delete the unwanted rows and columns:
    #avg_matrix = np.delete(avg_matrix, [0, 6, 12], 0)
    #avg_matrix = np.delete(avg_matrix, [0, 6, 12], 1)
    nps = 6  # number of elements per sequence

    if save_files:
        np.savetxt(name+".csv", avg_matrix, delimiter=",")
    labels = []
    for i, sequence in enumerate(pnas2018task.seqs):
        for action in sequence[0:-1]:
            labels.append(str(i)+'_'+action)
    analysis.plot_rdm(avg_matrix, labels, title + " spearman rho matrix")
    if save_files:
        plt.savefig(name+'_rdm')
    plt.clf()

    mdsy = analysis.mds(avg_matrix)
    for i, style in enumerate(['ro-', 'b|--', 'gx-.']):
        analysis.plot_mds_points(mdsy[nps * i:nps * i + nps], range(nps), labels=labels[nps * i:nps * i + nps], style=style)
    plt.title(title)
    if save_files:
        plt.savefig(name + '_mds')
    plt.clf()
    return avg_matrix
Ejemplo n.º 6
0
def generate_rdm_bev(nnet,
                     name,
                     rdm_type=analysis.SPEARMAN,
                     save_files=True,
                     title="RDM training beverage only"):
    if rdm_type != analysis.SPEARMAN:
        raise Exception("not implemented")
    hidden, accuracy_totals, accuracy_fullseqs = test_network_bev(nnet)
    print(accuracy_totals)
    print(accuracy_fullseqs)
    hidden = utils.flatten_onelevel(hidden)
    rdmatrix = analysis.rdm_spearman(hidden)
    labels = utils.flatten_onelevel(task.label_seqs_bev)
    name = name.replace('.', '_') + '_' + rdm_type
    if save_files:
        np.savetxt(name + "_rdm_mat.txt", rdmatrix, delimiter="\t", fmt='%.2e')
    analysis.plot_rdm(rdmatrix,
                      labels,
                      title + " spearman rho matrix",
                      figsize=30,
                      fontsize=0.6)
    if save_files:
        plt.savefig(name + '_rdm.png', dpi=300, bbox_inches='tight')
    plt.clf()
Ejemplo n.º 7
0
def make_rdm_multiple(name, num_networks, with_goals=False, title="-", save_files=True, skips=[],
                      rdm_type=analysis.SPEARMAN, noise=0., save_name=None):
    # Make one rdm for each network
    hidden_activations = []
    rdmatrices = []
    for i in range(num_networks+len(skips)):
        if i in skips:
            continue
        model = utils.load_object(name, i)
        if with_goals:
            hidden = pnashierarchy.accuracy_test_with_goals(model)
        else:
            hidden, _ = accuracy_test(model, name=str(i), noise=noise)
        hidden_activations.append(hidden)
        # Turn into a list of simple vectors
        for k, tensor in enumerate(hidden):
            hidden[k] = tensor.numpy().reshape(-1)

        if rdm_type == analysis.SPEARMAN:
            rdmatrix = analysis.rdm_spearman(hidden)
        elif rdm_type == analysis.MAHALANOBIS:
            rdmatrix = analysis.rdm_mahalanobis(hidden)
            #rdmatrix = analysis.rdm_noisy2_mahalanobis(hidden)
        elif rdm_type == analysis.EUCLIDIAN:
            rdmatrix = analysis.rdm_euclidian(hidden)
        elif rdm_type ==analysis.CRAPPYNOBIS:
            rdmatrix = analysis.rdm_crappynobis(hidden)
        else:
            raise ValueError("Only implemented rdm types are mahalanobis, spearman, euclidian")
        rdmatrices.append(rdmatrix)

    # Now average over all matrices
    avg_matrix = None
    for matrix in rdmatrices:
        if avg_matrix is None:
            avg_matrix = matrix
        else:
            avg_matrix += matrix
    avg_matrix = avg_matrix / num_networks
    name=name.replace('.', '_')+'_'+rdm_type
    if save_files:
        if save_name is None:
            save_name = name
        np.savetxt(save_name+"_rdm.txt", avg_matrix, delimiter="\t", fmt='%.2e')
    labels = []
    for i, sequence in enumerate(rewardtask.seqs):
        for action in sequence[1:]:
            labels.append(str(i)+'_'+action)
    analysis.plot_rdm(avg_matrix, labels, title + " spearman rho matrix")
    if save_files:
        plt.savefig(save_name+'_rdm')
    plt.clf()

    mdsy = analysis.mds(avg_matrix)
    for i, style in enumerate(['ro-', 'b|--', 'gx-.', 'k_:']):
        analysis.plot_mds_points(mdsy[rewardtask.length * i:rewardtask.length * i + rewardtask.length],
                                 range(rewardtask.length),
                                 labels=labels[rewardtask.length * i:rewardtask.length * i + rewardtask.length],
                                 style=style)
    plt.title(title)
    if save_files:
        plt.savefig(save_name + '_mds')
    plt.clf()
    return avg_matrix, hidden_activations
Ejemplo n.º 8
0
def make_rdm_multiple_deepprednet(name,
                                  num_networks,
                                  with_goals=False,
                                  title="-",
                                  save_files=True,
                                  skips=[],
                                  rdm_type=analysis.SPEARMAN):
    # Make one rdm for each network
    hidden_activations1 = []
    hidden_activations2 = []
    rdmatrices1 = []
    rdmatrices2 = []
    for i in range(num_networks + len(skips)):
        if i in skips:
            continue
        model = utils.load_object(name, i)
        if with_goals:
            hidden = pnashierarchy.accuracy_test_with_goals(model)
        else:
            hidden1, hidden2, _ = accuracy_test_deepprednet(model, name=str(i))
        hidden_activations1.append(hidden1)
        hidden_activations2.append(hidden2)
        # Turn into a list of simple vectors
        for j, tensor in enumerate(hidden1):
            hidden1[j] = tensor.numpy().reshape(-1)
        for j, tensor in enumerate(hidden2):
            hidden2[j] = tensor.numpy().reshape(-1)

        if rdm_type == analysis.SPEARMAN:
            rdmatrix1 = analysis.rdm_spearman(hidden1)
            rdmatrix2 = analysis.rdm_spearman(hidden2)
        elif rdm_type == analysis.MAHALANOBIS:
            rdmatrix1 = analysis.rdm_mahalanobis(hidden1)
            rdmatrix2 = analysis.rdm_mahalanobis(hidden2)
        elif rdm_type == analysis.EUCLIDIAN:
            rdmatrix1 = analysis.rdm_euclidian(hidden1)
            rdmatrix2 = analysis.rdm_euclidian(hidden2)
        elif rdm_type == analysis.CRAPPYNOBIS:
            rdmatrix1 = analysis.rdm_crappynobis(hidden1)
            rdmatrix2 = analysis.rdm_crappynobis(hidden2)
        else:
            raise ValueError(
                "Only implemented rdm types are mahalanobis, spearman, euclidian"
            )
        rdmatrices1.append(rdmatrix1)
        rdmatrices2.append(rdmatrix2)

    # Now average over all matrices
    for level, rdmatrices in enumerate([rdmatrices1, rdmatrices2]):
        avg_matrix = None
        for matrix in rdmatrices:
            if avg_matrix is None:
                avg_matrix = matrix
            else:
                avg_matrix += matrix
        avg_matrix = avg_matrix / num_networks
        name_level = name.replace(
            '.', '_') + '_' + rdm_type + "_level_" + str(level)
        if save_files:
            np.savetxt(name_level + "_rdm_mat.txt",
                       avg_matrix,
                       delimiter="\t",
                       fmt='%.2e')
        labels = []
        for i, sequence in enumerate(pnas2018task.seqs):
            for action in sequence[1:]:
                labels.append(str(i) + '_' + action)
        analysis.plot_rdm(avg_matrix, labels, title + " spearman rho matrix")
        if save_files:
            plt.savefig(name_level + '_rdm.jpeg')
        plt.clf()

        mdsy = analysis.mds(avg_matrix)
        for i, style in enumerate(['ro-', 'b|--', 'gx-.', 'k_:']):
            analysis.plot_mds_points(mdsy[6 * i:6 * i + 6],
                                     range(6),
                                     labels=labels[6 * i:6 * i + 6],
                                     style=style)
        plt.title(title)
        if save_files:
            plt.savefig(name_level + '_mds')
        plt.clf()
    return
Ejemplo n.º 9
0
def make_rdm_multiple_hierarchy(name,
                                num_networks,
                                title="-",
                                save_files=True,
                                file_save_name=None,
                                skips=[]):
    if file_save_name == None:
        file_save_name = name
    # Make one rdm for each network
    rdmatrices_left = []
    rdmatrices_right = []
    for i in range(num_networks + len(skips)):
        # Skip number
        if skips is not None and i in skips:
            continue
        model = utils.load_object(name, i)
        hidden, _ = accuracy_test_with_goals(model, i)

        # Turn a list of tensors into a list of np vectors
        for i, tensor in enumerate(hidden):
            hidden[i] = tensor.numpy().reshape(-1)

        # Now cut that in two and make an RDM for each
        cutoff = int(len(hidden[0]) // 2)
        left_units = [vector[:cutoff] for vector in hidden]
        rdm_left = analysis.rdm_spearman(left_units)
        rdmatrices_left.append(rdm_left)

        right_units = [vector[cutoff:] for vector in hidden]
        rdm_right = analysis.rdm_spearman(right_units)
        rdmatrices_right.append(rdm_right)

    matrices = []
    # Do the same processing for each side (low level/left and high_level/right)
    for side in [[rdmatrices_left, "_goals"], [rdmatrices_right, "_actions"]]:
        # Now average over all matrices
        avg_matrix = None
        for matrix in side[0]:
            if avg_matrix is None:
                avg_matrix = matrix
            else:
                avg_matrix += matrix
        avg_matrix = avg_matrix / num_networks

        # delete the unwanted rows and columns:
        avg_matrix = np.delete(avg_matrix, [0, 6, 12], 0)
        avg_matrix = np.delete(avg_matrix, [0, 6, 12], 1)
        nps = 5  # number of elements per sequence

        side_name = file_save_name + side[1]
        np.savetxt(side_name + "_rdm_mat" + utils.datestr() + ".txt",
                   avg_matrix,
                   delimiter="\t",
                   fmt='%.2e')
        labels = []
        for i, sequence in enumerate(seqs):
            for action in sequence[1:-1]:
                labels.append(str(i) + '_' + action)

        analysis.plot_rdm(avg_matrix, labels,
                          title + side_name + " spearman rho matrix")
        if save_files:
            plt.savefig(side_name + '_rdm' + utils.datestr())
        plt.clf()

        #        nps = 5  # number of elements per sequence
        mdsy = analysis.mds(avg_matrix)
        for i, style in enumerate(['ro-', 'b|--', 'gx-.']):
            analysis.plot_mds_points(mdsy[nps * i:nps * i + nps],
                                     range(nps),
                                     labels=labels[nps * i:nps * i + nps],
                                     style=style)
        plt.title(title + side_name)
        if save_files:
            plt.savefig(side_name + '_mds' + utils.datestr())
        plt.clf()
        matrices.append(avg_matrix)
    return matrices
Ejemplo n.º 10
0
def make_rdm_multiple(name,
                      num_networks,
                      type="spearman",
                      with_goals=False,
                      title="-",
                      save_files=True,
                      skips=[]):
    # Make one rdm for each network
    optimal_list = []
    rdmatrices = []
    error_per_step = np.zeros((3, 6, 11), dtype=np.float32)  # number of steps
    hidden_avg = []
    hiddens = np.zeros((3, 6), dtype=np.float32)
    for i in range(num_networks + len(skips)):
        if i in skips:
            continue
        model = utils.load_object(name, i)
        if with_goals:
            hidden, optimal = accuracy_test_with_goals(model, i)
        else:
            hidden, optimal, error_per_step_model = accuracy_test(model, i)
            error_per_step += error_per_step_model
        optimal_list.append(optimal)
        # Turn into a list of simple vectors
        for j, tensor in enumerate(hidden):
            hidden[j] = tensor.numpy().reshape(-1)
            hidden_avg.append(np.average(
                hidden[j]))  # Get the average activation for that time-step
        if type == "spearman":
            rdmatrix = analysis.rdm_spearman(hidden)
        elif type == "euclidian":
            rdmatrix = analysis.rdm_euclidian(hidden)
        else:
            raise Exception("RDM type " + type + " not implemented")
        rdmatrices.append(rdmatrix)

    # Now
    i = j = 0
    for act_avg in hidden_avg:
        hiddens[i, j] += act_avg
        j += 1
        if j > 5:
            j = 0
            i += 1
            if i > 2:
                i = 0
                j = 0
    print(hiddens / num_networks)
    print("{0} networks, of which {1} achieve optimal accuracy".format(
        num_networks, optimal_list.count(True)))

    # Hidden activation per step averages
    #for i, hidden_act in enumerate(hidden):
    #    hidden[i] = np.average(hidden_act)
    #hidden_avg = hidden.reshape(18, 11)
    #print(enumerate(hidden_avg))

    # Error per step averages
    error_avg = error_per_step / 100.
    error_avg = error_avg.reshape(18, 11)
    print(error_avg)

    # Now average over all matrices
    avg_matrix = None
    for matrix in rdmatrices:
        if avg_matrix is None:
            avg_matrix = matrix
        else:
            avg_matrix += matrix
    avg_matrix = avg_matrix / num_networks

    # delete the unwanted rows and columns:
    avg_matrix = np.delete(avg_matrix, [0, 6, 12], 0)
    avg_matrix = np.delete(avg_matrix, [0, 6, 12], 1)
    nps = 5  # number of elements per sequence

    if save_files:
        np.savetxt(name + "_rdm_mat_" + type + utils.datestr() + ".txt",
                   avg_matrix,
                   delimiter="\t",
                   fmt='%.2e')
        np.savetxt(name + "_errors" + utils.datestr() + ".txt",
                   error_avg,
                   delimiter="\t",
                   fmt='%.2e')
    labels = []
    for i, sequence in enumerate(seqs):
        for action in sequence[1:-1]:
            labels.append(str(i) + '_' + action)
    analysis.plot_rdm(avg_matrix, labels, title + " " + type + " matrix")
    if save_files:
        plt.savefig(name + '_rdm_' + type)
    plt.clf()

    mdsy = analysis.mds(avg_matrix)
    for i, style in enumerate(['ro-', 'b|--', 'gx-.']):
        analysis.plot_mds_points(mdsy[nps * i:nps * i + nps],
                                 range(nps),
                                 labels=labels[nps * i:nps * i + nps],
                                 style=style)
    plt.title(title)
    if save_files:
        plt.savefig(name + '_mds')
    plt.clf()
    return avg_matrix
Ejemplo n.º 11
0
def make_rdm_multiple_special(name1,
                              name2,
                              num_networks,
                              file_save_name,
                              title,
                              skips1=[],
                              skips2=[]):
    mats = []
    for name, skips in [(name1, skips1), (name2, skips2)]:
        print(name)
        # Make one rdm for each network
        rdmatrices_left = []
        rdmatrices_right = []
        for i in range(num_networks + len(skips)):
            # Skip number
            if skips is not None and i in skips:
                continue
            model = utils.load_object(name, i)
            if name == name1:  # Have to do the goal ones first.
                hidden = accuracy_test_reg_hierarchy(model, i)
            else:
                hidden = accuracy_test_reg_hierarchy_nogoals(model, i)

            # Turn a list of tensors into a list of np vectors
            for i, tensor in enumerate(hidden):
                hidden[i] = tensor.numpy().reshape(-1)

            # Now cut that in two and make an RDM for each
            cutoff = int(len(hidden[0]) // 2)
            left_units = [vector[:cutoff] for vector in hidden]
            rdm_left = analysis.rdm_spearman(left_units)
            rdmatrices_left.append(rdm_left)

            right_units = [vector[cutoff:] for vector in hidden]
            rdm_right = analysis.rdm_spearman(right_units)
            rdmatrices_right.append(rdm_right)

        # Do the same processing for each side (low level/left and high_level/right)
        for side in [rdmatrices_left, rdmatrices_right]:  # goals, then actions
            # Now average over all matrices
            avg_matrix = None
            for matrix in side:
                if avg_matrix is None:
                    avg_matrix = matrix
                else:
                    avg_matrix += matrix
            avg_matrix = avg_matrix / num_networks
            mats.append(avg_matrix)

    # Now average the average matrices
    rdmatrices_left = (mats[0] + mats[2]) / 2
    rdmatrices_right = (mats[1] + mats[3]) / 2

    # Do the same processing for each side (low level/left and high_level/right)
    for side in [[rdmatrices_left, "_goals"], [rdmatrices_right, "_actions"]]:
        matrix = side[0]
        side_name = file_save_name + side[1]
        np.savetxt(side_name + "_rdm_mat" + utils.datestr() + ".txt",
                   matrix,
                   delimiter="\t",
                   fmt='%.2e')
        labels = []
        for i, sequence in enumerate(pnas2018task.seqs):
            for action in sequence[1:]:
                labels.append(str(i) + '_' + action)
        analysis.plot_rdm(matrix, labels,
                          title + side_name + " spearman rho matrix")
        plt.savefig(side_name + '_rdm' + utils.datestr())
        plt.clf()

        mdsy = analysis.mds(matrix)
        for i, style in enumerate(['ro-', 'b|--', 'gx-.', 'k_:']):
            analysis.plot_mds_points(mdsy[6 * i:6 * i + 6],
                                     range(6),
                                     labels=labels[6 * i:6 * i + 6],
                                     style=style)
        plt.title(title + side_name)
        plt.savefig(side_name + '_mds' + utils.datestr())
        plt.clf()
Ejemplo n.º 12
0
def make_rdm_multiple_hierarchy_nogoals(name,
                                        num_networks,
                                        title="-",
                                        save_files=True,
                                        file_save_name=None,
                                        cutoff=None):
    if file_save_name == None:
        file_save_name = name
    # Make one rdm for each network
    rdmatrices_left = []
    rdmatrices_right = []
    for i in range(num_networks):
        if i == 19:
            continue
        model = utils.load_object(name, i)
        hidden = accuracy_test_reg_hierarchy_nogoals(model, i)

        # Turn a list of tensors into a list of np vectors
        for i, tensor in enumerate(hidden):
            hidden[i] = tensor.numpy().reshape(-1)

        # Now cut that in two and make an RDM for each
        if cutoff is None:
            cutoff = int(len(hidden[0]) // 2)
        left_units = [vector[:cutoff] for vector in hidden]
        rdm_left = analysis.rdm_spearman(left_units)
        rdmatrices_left.append(rdm_left)

        right_units = [vector[cutoff:] for vector in hidden]
        rdm_right = analysis.rdm_spearman(right_units)
        rdmatrices_right.append(rdm_right)

    # Do the same processing for each side (low level/left and high_level/right)
    for side in [[rdmatrices_left, "_goals"], [rdmatrices_right, "_actions"]]:
        # Now average over all matrices
        avg_matrix = None
        for matrix in side[0]:
            if avg_matrix is None:
                avg_matrix = matrix
            else:
                avg_matrix += matrix
        avg_matrix = avg_matrix / num_networks
        side_name = file_save_name + side[1]
        np.savetxt(side_name + "_rdm_mat" + utils.datestr() + ".csv",
                   avg_matrix,
                   delimiter=",")
        labels = []
        for i, sequence in enumerate(pnas2018task.seqs):
            for action in sequence[1:]:
                labels.append(str(i) + '_' + action)
        analysis.plot_rdm(avg_matrix, labels,
                          title + side_name + " spearman rho matrix")
        if save_files:
            plt.savefig(side_name + '_rdm' + utils.datestr())
        plt.clf()

        mdsy = analysis.mds(avg_matrix)
        for i, style in enumerate(['ro-', 'b|--', 'gx-.', 'k_:']):
            analysis.plot_mds_points(mdsy[6 * i:6 * i + 6],
                                     range(6),
                                     labels=labels[6 * i:6 * i + 6],
                                     style=style)
        plt.title(title + side_name)
        if save_files:
            plt.savefig(side_name + '_mds' + utils.datestr())
        plt.clf()