Exemplo n.º 1
0
def Evaluate_Result(homedir='.',
                    tProb_=None,
                    lag_time=1,
                    microstate_mapping_=None,
                    MacroAssignments_=None,
                    name=None):

    MacroMSM = MarkovStateModel(lag_time=lag_time)
    MacroMSM.fit(MacroAssignments_)
    Macro_tProb_ = MacroMSM.tProb_
    Macro_tCount_ = MacroMSM.tCount_
    #plot_matrix(labels=None, tProb_=tProb_, name=name)

    #Calculate metastablilty
    print "Calculating Metastablilty and Modularity..."
    metastability = Macro_tProb_.diagonal().sum()
    metastability /= len(Macro_tProb_)
    print "Metastability:", metastability

    #Calculate modularity
    micro_tProb_ = tProb_
    degree = micro_tProb_.sum(axis=1)  #row sum of tProb_ matrix
    total_degree = degree.sum()

    modularity = 0.0
    len_mapping = len(microstate_mapping_)
    for i in xrange(len_mapping):
        state_i = microstate_mapping_[i]
        for j in xrange(len_mapping):
            state_j = microstate_mapping_[j]
            if state_i == state_j:
                modularity += micro_tProb_[
                    i, j] - degree[i] * degree[j] / total_degree
    modularity /= total_degree
    print "Modularity:", modularity

    Macro_tCountDir = homedir + "/" + name + "Macro_tCounts.mtx"
    Macro_tProbDir = homedir + "/" + name + "Macro_tProb.mtx"
    Metastability_Modularity_Dir = homedir + "/" + name + "Metastability_Modularity.txt"

    np.savetxt(Metastability_Modularity_Dir, [metastability, modularity],
               fmt="%lf")
    scipy.io.mmwrite(Macro_tCountDir,
                     scipy.sparse.csr_matrix(Macro_tCount_),
                     field='integer')
    scipy.io.mmwrite(Macro_tProbDir, scipy.sparse.csr_matrix(Macro_tProb_))
    #Plot tProb matrix
    plot_matrix(tProb_=Macro_tProb_, name=name)
Exemplo n.º 2
0
    def execute_line(self, bql_string, pretty=True, timing=False, plots=None):
        if timing:
            start_time = time.time()

        out  = self.parser.parse_line(bql_string)
        if out is None:
            print "Could not parse command. Try typing 'help' for a list of all commands."
            return
        elif not out:
            return
        method_name, args_dict = out
        result = self.call_bayesdb_engine(method_name, args_dict)
        result = self.callback(method_name, args_dict, result)
        
        if timing:
            end_time = time.time()
            print 'Elapsed time: %.2f seconds.' % (end_time - start_time)

        if plots is None:
            plots = 'DISPLAY' in os.environ.keys()

        if bql_string.lower().strip().startswith('estimate'):
            ## Special logic to display matrices.
            if not (result['filename'] or plots):
                print "No GUI available to display graphics: please enter the filename where the graphics should be saved, with the extension indicating the filetype (e.g. .png or .pdf). Enter a blank filename to instead view the matrix as text."
                filename = raw_input()
                if len(filename) > 0:
                    result['filename'] = filename
                    utils.plot_matrix(result['matrix'], result['column_names'], title=result['title'], filename=result['filename'])
                else:
                    pp = self.pretty_print(result)
                    print pp
                    return pp
            else:
                utils.plot_matrix(result['matrix'], result['column_names'], title=result['message'], filename=result['filename'])
        elif pretty:
            if type(result) == dict and 'message' in result.keys():
                print result['message']
            pp = self.pretty_print(result)
            print pp
            return pp
        else:
            if type(result) == dict and 'message' in result.keys():
                print result['message']
            return result
Exemplo n.º 3
0
 def evaluate_performance(self,
                          path_scores_parameters=None,
                          path_scores_cm=None):
     test_set_desc = np.asarray([
         self.desc_relevant_config_videos[i].flatten()
         for i in self.test_video_idx
     ])
     test_set_vas = np.asarray(
         [self.vas_sequences[i] for i in self.test_video_idx])
     num_test_videos = test_set_desc.shape[0]
     sum_error = 0
     confusion_matrix = np.zeros(shape=(11, 11))
     real__vas = []
     predicted__vas = []
     if path_scores_parameters is not None:
         out_df_scores = pd.DataFrame(
             columns=['sequence_num', 'real_vas', 'vas_predicted', 'error'])
     for num_video in np.arange(num_test_videos):
         real_vas = test_set_vas[num_video]
         real__vas.append(real_vas)
         vas_predicted = self.__predict(test_set_desc[num_video].reshape(
             1, -1))
         predicted__vas.append(vas_predicted)
         error = abs(real_vas - vas_predicted)
         sum_error += error
         if path_scores_parameters is not None:
             data = np.hstack((np.array([
                 self.test_video_idx[num_video], real_vas, vas_predicted,
                 error
             ]).reshape(1, -1)))
             out_df_scores = out_df_scores.append(pd.Series(
                 data.reshape(-1), index=out_df_scores.columns),
                                                  ignore_index=True)
         confusion_matrix[real_vas][vas_predicted] += 1
     if path_scores_parameters is not None:
         out_df_scores.to_csv(path_scores_parameters,
                              index=False,
                              header=True)
     if path_scores_cm is not None:
         plot_matrix(cm=confusion_matrix,
                     labels=np.arange(0, 11),
                     normalize=True,
                     fname=path_scores_cm)
     mean_error = round(sum_error / num_test_videos, 3)
     return mean_error, confusion_matrix
Exemplo n.º 4
0
    def _show(self, data, output, train=True):

        self.writer.add_image(
            'a-input',
            make_grid(data[0].cpu(), nrow=data.shape[1], normalize=True))
        self.writer.add_image(
            'b-output_rec',
            make_grid(output["rec"][0].cpu(),
                      nrow=output["rec"].shape[1],
                      normalize=True))
        self.writer.add_image(
            'c-output_pred',
            make_grid(output["pred"][0].cpu(),
                      nrow=output["pred"].shape[1],
                      normalize=True))

        if output["selector"] is not None:
            S_plot = plot_matrix(output["selector"])
            self.writer.add_image('ba-S',
                                  make_grid(S_plot, nrow=1, normalize=False))
        if output["obs_rec_pred"] is not None:
            g_plot = plot_representation(
                output["obs_rec_pred"][:, :output["rec"].shape[1]].cpu())
            g_plot_pred = plot_representation(
                output["obs_rec_pred"][:, output["rec"].shape[1]:].cpu())
            self.writer.add_image(
                'd-g_repr_rec',
                make_grid(to_tensor(g_plot), nrow=1, normalize=False))
            self.writer.add_image(
                'e-g_repr_pred',
                make_grid(to_tensor(g_plot_pred), nrow=1, normalize=False))
        if output["A"] is not None:
            A_plot = plot_matrix(output["A"])
            self.writer.add_image('f-A',
                                  make_grid(A_plot, nrow=1, normalize=False))
        if output["B"] is not None:
            B_plot = plot_matrix(output["B"])
            self.writer.add_image('g-B',
                                  make_grid(B_plot, nrow=1, normalize=False))
        if output["u"] is not None:
            u_plot = plot_representation(
                output["u"][:, :output["u"].shape[1]].cpu())
            self.writer.add_image(
                'e-gu', make_grid(to_tensor(u_plot), nrow=1, normalize=False))
Exemplo n.º 5
0
def reconstruct_correspondence_matrix(data, cost_func, alphas, betas):
    best_matrix = None
    reconstruction_errors = np.zeros((len(alphas), len(betas)))
    min_reconstruction_error = np.inf
    best_alpha, best_beta = np.nan, np.nan

    for alpha_idx in range(len(alphas)):
        for beta_idx in range(len(betas)):
            reconstructed_correspondence_matrix = (
                cmr.CorrespondenceMatrixReconstructor(
                    C=1.0, max_iters=10000, stopping_eps=10**(-4)).fit(
                        cost_matrix=np.nan_to_num(cost_func(
                            alpha=alphas[alpha_idx],
                            beta=betas[beta_idx],
                            time=data.cost_matrix_time,
                            distance=data.cost_matrix_distance),
                                                  nan=np.inf),
                        living_people=np.nansum(data.correspondence_matrix,
                                                axis=1),
                        working_people=np.nansum(data.correspondence_matrix,
                                                 axis=0)).predict())
            reconstruction_error = np.linalg.norm(
                reconstructed_correspondence_matrix -
                np.nan_to_num(data.correspondence_matrix, nan=0.0))
            reconstruction_errors[alpha_idx, beta_idx] = reconstruction_error
            if reconstruction_error < min_reconstruction_error:
                min_reconstruction_error = reconstruction_error
                best_alpha, best_beta = alphas[alpha_idx], betas[beta_idx]
                best_matrix = reconstructed_correspondence_matrix
            print('alpha =', alphas[alpha_idx], 'beta =', betas[beta_idx],
                  'reconstruction error =', reconstruction_error)

    print('\nOptimization has finished.\n')
    print('reconstruction_errors =\n', reconstruction_errors, '\n')
    print('min_reconstruction_error =', min_reconstruction_error)
    utils.plot_matrix(matrix=reconstruction_errors,
                      plot_name='reconstruction_errors_' + cost_func.__name__,
                      annot=False,
                      linewidths=0,
                      xticklabels=np.round(betas, 2),
                      yticklabels=np.round(alphas, 2),
                      xlabel='beta',
                      ylabel='alpha')
    return best_matrix, best_alpha, best_beta
Exemplo n.º 6
0
def main():
    data = get_traffic_data()
    plot_traffic_data(data)
    print('Data have been found. Running optimization...\n')

    cost_func = cost_function.compute_cost3
    reconstructed_correspondence_matrix, best_alpha, best_beta = (
        reconstruct_correspondence_matrix(data=data,
                                          cost_func=cost_func,
                                          alphas=0.001 * np.arange(1, 501, 1),
                                          betas=0.001 * np.arange(1, 4, 1)))
    utils.plot_matrix(matrix=reconstructed_correspondence_matrix,
                      plot_name='reconstructed_correspondence_matrix_' +
                      cost_func.__name__,
                      annot=True,
                      linewidths=0.5)

    print('best_alpha =', best_alpha, 'best_beta =', best_beta)
    print('\nDone.')
Exemplo n.º 7
0
 def _show(self, data, output, train=True):
     g_plot = plot_representation(output[2][:,:output[0].shape[1]].cpu())
     g_plot_pred = plot_representation(output[2][:,output[0].shape[1]:].cpu())
     A_plot = plot_matrix(output[4])
     if output[5] is not None:
         B_plot = plot_matrix(output[5])
         self.writer.add_image('B', make_grid(B_plot, nrow=1, normalize=False))
     if output[6] is not None:
         u_plot = plot_representation(output[6][:, :output[6].shape[1]].cpu())
         self.writer.add_image('u', make_grid(to_tensor(u_plot), nrow=1, normalize=False))
     # if output[10] is not None: # TODO: Ara el torno a posar
     #     # print(output[10][0].max(), output[-1][0].min())
     #     shape = output[10][0].shape
     #     self.writer.add_image('objects', make_grid(output[10][0].permute(1, 2, 0, 3, 4).reshape(*shape[1:-2], -1, shape[-1]).cpu(), nrow=output[0].shape[1], normalize=True))
     self.writer.add_image('A', make_grid(A_plot, nrow=1, normalize=False))
     self.writer.add_image('g_repr', make_grid(to_tensor(g_plot), nrow=1, normalize=False))
     self.writer.add_image('g_repr_pred', make_grid(to_tensor(g_plot_pred), nrow=1, normalize=False))
     self.writer.add_image('input', make_grid(data[0].cpu(), nrow=data.shape[1], normalize=True))
     self.writer.add_image('output_0rec', make_grid(output[0][0].cpu(), nrow=output[0].shape[1], normalize=True))
     self.writer.add_image('output_1pred', make_grid(output[1][0].cpu(), nrow=output[1].shape[1], normalize=True))
Exemplo n.º 8
0
def Evaluate_Result(homedir='.', tProb_=None, lag_time=1, microstate_mapping_=None, MacroAssignments_=None, name=None):

    MacroMSM = MarkovStateModel(lag_time=lag_time)
    MacroMSM.fit(MacroAssignments_)
    Macro_tProb_ = MacroMSM.tProb_
    Macro_tCount_ = MacroMSM.tCount_
    #plot_matrix(labels=None, tProb_=tProb_, name=name)

    #Calculate metastablilty
    print "Calculating Metastablilty and Modularity..."
    metastability = Macro_tProb_.diagonal().sum()
    metastability /= len(Macro_tProb_)
    print "Metastability:", metastability

    #Calculate modularity
    micro_tProb_ = tProb_
    degree = micro_tProb_.sum(axis=1) #row sum of tProb_ matrix
    total_degree = degree.sum()

    modularity = 0.0
    len_mapping = len(microstate_mapping_)
    for i in xrange(len_mapping):
        state_i = microstate_mapping_[i]
        for j in xrange(len_mapping):
            state_j = microstate_mapping_[j]
            if state_i == state_j:
                modularity += micro_tProb_[i, j] - degree[i]*degree[j]/total_degree
    modularity /= total_degree
    print "Modularity:", modularity

    Macro_tCountDir       = homedir + "/" + name + "Macro_tCounts.mtx"
    Macro_tProbDir        = homedir + "/" + name + "Macro_tProb.mtx"
    Metastability_Modularity_Dir = homedir + "/" + name + "Metastability_Modularity.txt"

    np.savetxt(Metastability_Modularity_Dir, [metastability, modularity], fmt="%lf")
    scipy.io.mmwrite(Macro_tCountDir, scipy.sparse.csr_matrix(Macro_tCount_), field='integer')
    scipy.io.mmwrite(Macro_tProbDir, scipy.sparse.csr_matrix(Macro_tProb_))
    #Plot tProb matrix
    plot_matrix(tProb_=Macro_tProb_, name=name)
Exemplo n.º 9
0
 def evaluate_performance_on_scaled_pain(self, path_scores_cm=None):
     test_set_desc = np.asarray([
         self.desc_relevant_config_videos[i].flatten()
         for i in self.test_video_idx
     ])
     test_set_vas = np.asarray(
         [self.vas_sequences[i] for i in self.test_video_idx])
     num_test_videos = test_set_desc.shape[0]
     confusion_matrix = np.zeros(shape=(3, 3))
     dict_pain_level = {
         0: 0,
         1: 1,
         2: 1,
         3: 1,
         4: 2,
         5: 2,
         6: 2,
         7: 2,
         8: 2,
         9: 2,
         10: 2
     }
     labels_cm = ["no pain", "weak pain", "severe pain"]
     for num_video in np.arange(num_test_videos):
         real_vas = test_set_vas[num_video]
         real_level_idx = dict_pain_level[real_vas]
         vas_predicted = self.__predict(test_set_desc[num_video].reshape(
             1, -1))
         predicted_level_idx = dict_pain_level[vas_predicted]
         confusion_matrix[real_level_idx][predicted_level_idx] += 1
     if path_scores_cm is not None:
         plot_matrix(cm=confusion_matrix,
                     labels=labels_cm,
                     normalize=True,
                     fname=path_scores_cm)
     return confusion_matrix
Exemplo n.º 10
0
def plot_traffic_data(data):
    utils.plot_matrix(matrix=data.correspondence_matrix,
                      plot_name='correspondence_matrix',
                      annot=True,
                      linewidths=0.5)
    utils.plot_matrix(matrix=data.cost_matrix_time,
                      plot_name='cost_matrix_time',
                      annot=True,
                      linewidths=0.5)
    utils.plot_matrix(matrix=data.cost_matrix_distance,
                      plot_name='cost_matrix_distance',
                      annot=True,
                      linewidths=0.5)
Exemplo n.º 11
0
    def _do_gen_matrix(self,
                       col_function_name,
                       X_L_list,
                       X_D_list,
                       M_c,
                       T,
                       tablename='',
                       filename=None,
                       col=None,
                       confidence=None,
                       limit=None,
                       submatrix=False):
        if col_function_name == 'mutual information':
            col_function = getattr(self, '_mutual_information')
        elif col_function_name == 'dependence probability':
            col_function = getattr(self, '_dependence_probability')
        elif col_function_name == 'correlation':
            col_function = getattr(self, '_correlation')
        elif col_function_name == 'view_similarity':
            col_function = getattr(self, '_view_similarity')
        else:
            raise Exception('Invalid column function')

        num_cols = len(X_L_list[0]['column_partition']['assignments'])
        column_names = [
            M_c['idx_to_name'][str(idx)] for idx in range(num_cols)
        ]
        column_names = numpy.array(column_names)
        # extract unordered z_matrix
        num_latent_states = len(X_L_list)
        z_matrix = numpy.zeros((num_cols, num_cols))
        for i in range(num_cols):
            for j in range(num_cols):
                z_matrix[i][j] = col_function(i, j, X_L_list, X_D_list, M_c, T)

        if col:
            z_column = list(z_matrix[M_c['name_to_idx'][col]])
            data_tuples = zip(z_column, range(num_cols))
            data_tuples.sort(reverse=True)
            if confidence:
                data_tuples = filter(lambda tup: tup[0] >= float(confidence),
                                     data_tuples)
            if limit and limit != float("inf"):
                data_tuples = data_tuples[:int(limit)]
            data = [tuple([d[0] for d in data_tuples])]
            columns = [d[1] for d in data_tuples]
            column_names = [
                M_c['idx_to_name'][str(idx)] for idx in range(num_cols)
            ]
            column_names = numpy.array(column_names)
            column_names_reordered = column_names[columns]
            if submatrix:
                z_matrix = z_matrix[columns, :][:, columns]
                z_matrix_reordered = z_matrix
            else:
                return {'data': data, 'columns': column_names_reordered}
        else:
            # hierachically cluster z_matrix
            import hcluster
            Y = hcluster.pdist(z_matrix)
            Z = hcluster.linkage(Y)
            pylab.figure()
            hcluster.dendrogram(Z)
            intify = lambda x: int(x.get_text())
            reorder_indices = map(intify, pylab.gca().get_xticklabels())
            pylab.close()
            # REORDER!
            z_matrix_reordered = z_matrix[:,
                                          reorder_indices][reorder_indices, :]
            column_names_reordered = column_names[reorder_indices]

        title = 'Pairwise column %s for %s' % (col_function_name, tablename)
        if filename:
            utils.plot_matrix(z_matrix_reordered, column_names_reordered,
                              title, filename)

        return dict(matrix=z_matrix_reordered,
                    column_names=column_names_reordered,
                    title=title,
                    filename=filename,
                    message="Created " + title)
Exemplo n.º 12
0
X_scaler = StandardScaler()
X_scaled = X_scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.25)

etc_3 = ExtraTreesClassifier(max_features=0.42, n_estimators=ESTIMATORS, n_jobs=-1)

"""Classification Report & Confusion Matrix"""
etc_3.fit(X_train, y_train)
y_pred_etc_3 = etc_3.predict(X_test)

print "[Classification Report]"
print classification_report(y_test, y_pred_etc_3)
print "[Train dataset] Score: %.5f" % etc_3.score(X_train, y_train)
print "[Test dataset] Score: %.5f" % etc_3.score(X_test, y_test)
plot_matrix(etc_3, X_test, y_test, filename)

"""Plot ROC Curve"""
y_score = etc_3.predict_proba(X_test)
y_test_bin = np.array(label_binarize(y_test, classes=np.unique(y)))
n_classes = y_test_bin.shape[1]
plot_roc_curve(n_classes, y_test_bin, y_score, filename)

"""Plot Decision Area"""
clf = ExtraTreesClassifier(n_estimators=ESTIMATORS, max_features=0.42, n_jobs=-1)
plot_decision_area(clf, X_scaled[:, 2:4], y, title="Extra Trees Classifier", filename=filename)

"""Plot Learning Curve"""
X_lc = X_scaled[:10000]
y_lc = y[:10000]
plot_learning_curve(clf, "Extra Trees Classifier", X_lc, y_lc, filename=filename)
Exemplo n.º 13
0
"""Testing A Simple Prediction"""
#print("Feature vector: %s" % X_test[:1])
print("Label: %s" % str(y_test[0]))
print("Predicted: %s" % str(net0.predict(X_test[:1])))


"""Metrics"""

# layer_info = PrintLayerInfo()
# net0.verbose = 3
# net0.initialize()
#print layer_info(net0)

print "[Classification Report]: "
print classification_report(y_test, predicted)
print "[Train dataset] Score: ", net0.score(X_train, y_train)
print "[Test dataset] Score: ", net0.score(X_test, y_test)
plot_matrix(net0, X_test, y_test, filename)

valid_accuracies = np.array([i["valid_accuracy"] for i in net0.train_history_])
plot_accuracy(valid_accuracies, filename)

train_loss = [row['train_loss'] for row in net0.train_history_]
valid_loss = [row['valid_loss'] for row in net0.train_history_]
plot_loss(valid_loss, train_loss, filename)

y_score = net0.predict_proba(X_test) #[:, 1]
y_test_bin = np.array(label_binarize(y_test, classes=np.unique(y)))
n_classes = y_test_bin.shape[1]
plot_roc_curve(n_classes, y_test_bin, y_score, filename=filename)
Exemplo n.º 14
0
 def visualise_feature_matrix(self):
     utils.plot_matrix(self.feature_matrix)
Exemplo n.º 15
0
net0 = NeuralNetConstructor(93)
net0.fit(X_train, y_train)
predicted = net0.predict(X_test)
"""Testing A Simple Prediction"""
#print("Feature vector: %s" % X_test[:1])
print("Label: %s" % str(y_test[0]))
print("Predicted: %s" % str(net0.predict(X_test[:1])))
"""Metrics"""

# layer_info = PrintLayerInfo()
# net0.verbose = 3
# net0.initialize()
#print layer_info(net0)

print "[Classification Report]: "
print classification_report(y_test, predicted)
print "[Train dataset] Score: ", net0.score(X_train, y_train)
print "[Test dataset] Score: ", net0.score(X_test, y_test)
plot_matrix(net0, X_test, y_test, filename)

valid_accuracies = np.array([i["valid_accuracy"] for i in net0.train_history_])
plot_accuracy(valid_accuracies, filename)

train_loss = [row['train_loss'] for row in net0.train_history_]
valid_loss = [row['valid_loss'] for row in net0.train_history_]
plot_loss(valid_loss, train_loss, filename)

y_score = net0.predict_proba(X_test)  #[:, 1]
y_test_bin = np.array(label_binarize(y_test, classes=np.unique(y)))
n_classes = y_test_bin.shape[1]
plot_roc_curve(n_classes, y_test_bin, y_score, filename=filename)
Exemplo n.º 16
0
X_scaled = X_scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=.25)

etc_3 = ExtraTreesClassifier(max_features=0.42,
                             n_estimators=ESTIMATORS,
                             n_jobs=-1)
"""Classification Report & Confusion Matrix"""
etc_3.fit(X_train, y_train)
y_pred_etc_3 = etc_3.predict(X_test)

print "[Classification Report]"
print classification_report(y_test, y_pred_etc_3)
print "[Train dataset] Score: %.5f" % etc_3.score(X_train, y_train)
print "[Test dataset] Score: %.5f" % etc_3.score(X_test, y_test)
plot_matrix(etc_3, X_test, y_test, filename)
"""Plot ROC Curve"""
y_score = etc_3.predict_proba(X_test)
y_test_bin = np.array(label_binarize(y_test, classes=np.unique(y)))
n_classes = y_test_bin.shape[1]
plot_roc_curve(n_classes, y_test_bin, y_score, filename)
"""Plot Decision Area"""
clf = ExtraTreesClassifier(n_estimators=ESTIMATORS,
                           max_features=0.42,
                           n_jobs=-1)
plot_decision_area(clf,
                   X_scaled[:, 2:4],
                   y,
                   title="Extra Trees Classifier",
                   filename=filename)
"""Plot Learning Curve"""
Exemplo n.º 17
0
    def _show(self, data, output, train=True):

        rec = output["rec_ori"]

        if output["pred_roll"] is not None:
            data_plot = data[0, -output["pred_roll"].shape[1]:]
            rec_plot = output["rec_ori"][0, -output["pred_roll"].shape[1]:]
            pred_plot = output["pred_roll"][0]
            vid_plot = torch.cat([data_plot, rec_plot, pred_plot], dim=0)
            nrow = output["pred_roll"].shape[1]
        else:
            vid_plot = torch.cat([data[0, -output["rec_ori"].shape[1]:], output["rec_ori"][0, :]], dim=0)
            nrow = output["rec_ori"].shape[1]
        self.writer.add_image('a-Videos--Input-Rec-Pred', make_grid(vid_plot.cpu(), nrow=nrow, normalize=True))

        # self.writer.add_image('a-input', make_grid(data[0].cpu(), nrow=data.shape[1], normalize=True))
        # self.writer.add_image('b-output_rec', make_grid(output["rec"][0, -output["pred"].shape[1]:].cpu(), nrow=output["pred"].shape[1], normalize=True))
        # # self.writer.add_image('b-output_rec_ori', make_grid(output["rec_ori"][0, -output["pred"].shape[1]:].cpu(), nrow=output["pred"].shape[1], normalize=True))
        # self.writer.add_image('c-output_pred', make_grid(output["pred"][0].cpu(), nrow=output["pred"].shape[1], normalize=True))

        # if output["selector"] is not None:
        #     S_plot = plot_matrix(output["selector"])
        #     self.writer.add_image('ba-S', make_grid(S_plot, nrow=1, normalize=False))
        if output["obs_rec_pred"] is not None:
            if output["pred"] is not None:
                g_plot = plot_representation        (output["obs_rec_pred"][:,rec.shape[1]-output["pred"].shape[1]:rec.shape[1]].cpu())
                g_plot_pred = plot_representation   (output["obs_rec_pred"][:,rec.shape[1]:].cpu())
                self.writer.add_image('e-g_repr_pred', make_grid(to_tensor(g_plot_pred), nrow=1, normalize=False))
            else: g_plot = plot_representation        (output["obs_rec_pred"].cpu())
            self.writer.add_image('d-g_repr_rec', make_grid(to_tensor(g_plot), nrow=1, normalize=False))

        if output["obs_rec_pred_rev"] is not None:
            g_plot = plot_representation        (output["obs_rec_pred_rev"].cpu())
            self.writer.add_image('ea-g_repr_pred_rev', make_grid(to_tensor(g_plot), nrow=1, normalize=False))
        if output["dyn_features"] is not None:
            # g_plot = plot_representation        (output["dyn_features"]["rec_ori"].cpu())
            # self.writer.add_image('states_rec_ori', make_grid(to_tensor(g_plot), nrow=1, normalize=False))
            g_plot = plot_representation        (output["dyn_features"]["rec_ori"].cpu()) #[:,-output["pred"].shape[1]:].cpu())
            self.writer.add_image('states_rec', make_grid(to_tensor(g_plot), nrow=1, normalize=False))

            if output["pred"] is not None:
                g_plot = plot_representation        (output["dyn_features"]["pred"].cpu())
                self.writer.add_image('states_pred', make_grid(to_tensor(g_plot), nrow=1, normalize=False))
            if output["pred_roll"] is not None:
                g_plot = plot_representation        (output["dyn_features"]["pred_roll"].cpu())
                self.writer.add_image('states_pred_roll', make_grid(to_tensor(g_plot), nrow=1, normalize=False))

        if output["A"] is not None:
            A_plot = plot_matrix(output["A"])
            self.writer.add_image('f-A', make_grid(A_plot, nrow=1, normalize=False))
        # if output["Ainv"] is not None:
        #     A_plot = plot_matrix(output["Ainv"])
        #     self.writer.add_image('fa-A-inv', make_grid(A_plot, nrow=1, normalize=False))
        #     AA_plot = plot_matrix(torch.mm(output["A"],output["Ainv"]))
        #     self.writer.add_image('fb-AA', make_grid(AA_plot, nrow=1, normalize=False))
        # else:
        #     AA_plot = plot_matrix(torch.mm(output["A"],torch.pinverse(output["A"])))
        #     self.writer.add_image('fb-AA', make_grid(AA_plot, nrow=1, normalize=False))
        # if output["selectors_rec"] is not None:
        #     sels_rec = output["selectors_rec"]
        #     if output["selectors_pred"] is not None:
        #         sels_pred = output["selectors_pred"]
        #         sel_rec_plot = plot_representation(sels_rec[:,-sels_pred.shape[1]:].cpu())
        #     else:
        #         sel_rec_plot = plot_representation(sels_rec.cpu())
        #     self.writer.add_image('b-sel_repr_rec', make_grid(to_tensor(sel_rec_plot), nrow=1, normalize=False))
        # if output["selectors_pred"] is not None:
        #     sels_pred = output["selectors_pred"]
        #     sel_pred_plot = plot_representation(sels_pred.cpu())
        #     self.writer.add_image('b-sel_repr_pred', make_grid(to_tensor(sel_pred_plot), nrow=1, normalize=False))
        if output["B"] is not None:
            B_plot = plot_matrix(output["B"])
            self.writer.add_image('g-B', make_grid(B_plot, nrow=1, normalize=False))
        if output["u"] is not None:
            u_plot = plot_representation(output["u"][:, :output["u"].shape[1]].cpu())
            self.writer.add_image('eb-gu', make_grid(to_tensor(u_plot), nrow=1, normalize=False))

        # if output[10] is not None: # TODO: Ara el torno a posar
        #     # print(output[10][0].max(), output[-1][0].min())
        #     shape = output[10][0].shape
        #     self.writer.add_image('objects', make_grid(output[10][0].permute(1, 2, 0, 3, 4).reshape(*shape[1:-2], -1, shape[-1]).cpu(), nrow=output[0].shape[1], normalize=True))

        # output["rec"] = torch.clamp(torch.sum(out_rec.reshape(bs, self.n_objects, -1, *out_rec.size()[1:]), dim=1), min=0, max=1)
        # output["pred"] = torch.clamp(torch.sum(out_pred.reshape(bs, self.n_objects, -1, *out_rec.size()[1:]), dim=1), min=0, max=1)
        # output["obs_rec_pred"] = returned_g.reshape(-1, returned_g.size()[-1:])
        # output["gauss_post"] = returned_post
        # output["A"] = A
        # output["B"] = B
        # output["u"] = u.reshape(bs * self.n_objects, -1, u.shape[-1])
        # output["bern_logit"] = u_logit.reshape(bs, self.n_objects, -1, u_logit.shape[-1]) # Input distribution