code: def build_factor_graph_model(
      labels, feats, factor_types, edge_table, infer_alg=sg.GRAPH_CUT
  ):
      """Build factor graph model
  
      Args:
          labels: matrix of labels [num_train_samples*len_label]
          feats: maxtrix of feats [num_train_samples*len_feat]
          factory_types: vectors of all factor types
          edge_table: matrix of pairwised edges, each row is a pair of node indeces
          infer_alg: inference algorithm (GRAPH_CUT)
  
      Returns:
          labels_fg: matrix of labels in factor graph format
          feats_fg: matrix of features in factor graph format
      """
  
      labels = labels.astype(np.int32)
      num_train_samples = labels.shape[0]
      num_vars = labels.shape[1]
      num_edges = edge_table.shape[0]
      n_stats = 2
  
      feats_fg = sg.FactorGraphFeatures(num_train_samples)
      labels_fg = sg.FactorGraphLabels(num_train_samples)
  
      for i in range(num_train_samples):
          cardinaities = np.array([n_stats] * num_vars, np.int32)
          fg = sg.FactorGraph(cardinaities)
  
          # add unary factors
          for u in range(num_vars):
              data_u = np.array(feats[i, :], np.float64)
              inds_u = np.array([u], np.int32)
              factor_u = sg.Factor(factor_types[u], inds_u, data_u)
              fg.add_factor(factor_u)
  
          # add pairwise factors
          for v in range(num_edges):
              data_p = np.array([1.0])
              inds_p = np.array(edge_table[v, :], np.int32)
              factor_p = sg.Factor(
                  factor_types[v + num_vars], inds_p, data_p
              )
              fg.add_factor(factor_p)
  
          # add factor graph
          feats_fg.add_sample(fg)
  
          # add corresponding label
          loss_weights = np.array([1.0 / num_vars] * num_vars)
          fg_obs = sg.FactorGraphObservation(labels[i, :], loss_weights)
          labels_fg.add_label(fg_obs)
  
      return (labels_fg, feats_fg)
  
file_path: shogun/examples/undocumented/python/structure_graphcuts.py
imports: import numpy
  import itertools
  import shogun
label_desc: Use image node_id 3 for calling a global function with example usage: build_factor_graph_model(labels, feats, factor_types, edge_table, infer_alg) and returns: labels_fg,  feats_fg