class_docstr: Executes the complete pipeline of the graph cut algorithm, checking the results.
class_docstr_tok: ["Executes","the","complete","pipeline","of","the","graph","cut","algorithm",",","checking","the","results","."]
docstr: Executes the complete pipeline of the graph cut algorithm.
docstr_tok: ["Executes","the","complete","pipeline","of","the","graph","cut","algorithm","."]
code: def test_region_based(self):
      """Executes the complete pipeline of the graph cut algorithm."""
      # create the graph from the image
      label_image = self.__label_image
      graph = graph_from_labels(
          label_image,
          self.__fg_marker,
          self.__bg_marker,
          boundary_term=self.__boundary_term,
      )
  
      # alter the graph, removing some edges that are undesired
      nweights = graph.get_nweights()
      for edge in self.__get_bad_edges():
          if edge in nweights:
              del nweights[edge]
          else:
              del nweights[(edge[1], edge[0])]
  
      # create new graph from old graph to check the setting methods of the Graph object
      graph_new = Graph()
      graph_new.set_nodes(graph.get_node_count())
      graph_new.set_source_nodes(graph.get_source_nodes())
      graph_new.set_sink_nodes(graph.get_sink_nodes())
      graph_new.set_nweights(nweights)
  
      if graph_new.inconsistent():
          self.fail(
              "The newly generated graph is inconsistent. Reasons: {}".format(
                  "\n".join(graph_new.inconsistent())
              )
          )
  
      # build graph cut graph from graph
      gcgraph = GraphDouble(
          len(graph_new.get_nodes()), len(graph_new.get_nweights())
      )
      gcgraph.add_node(len(graph_new.get_nodes()))
      for node, weight in list(graph_new.get_tweights().items()):
          gcgraph.add_tweights(int(node - 1), weight[0], weight[1])
      for edge, weight in list(graph_new.get_nweights().items()):
          gcgraph.add_edge(
              int(edge[0] - 1), int(edge[1] - 1), weight[0], weight[1]
          )
  
      # execute min-cut / executing BK_MFMC
      try:
          maxflow = gcgraph.maxflow()
      except Exception as e:
          self.fail(
              "An error was thrown during the external executions: {}".format(
                  e.message
              )
          )
  
      # apply results to the label image
      label_image = filter.relabel_map(
          label_image,
          gcgraph.what_segment,
          lambda fun, rid: 0
          if gcgraph.termtype.SINK == fun(int(rid) - 1)
          else 1,
      )
  
      # check results for validity
      self.assertEqual(
          maxflow,
          self.__maxflow,
          "The resulting maxflow {} differs from the expected one {}.".format(
              maxflow, self.__maxflow
          ),
      )
      self.assertSequenceEqual(
          label_image.tolist(),
          self.__result,
          "The resulting cut is wrong. Expected\n {}\n got\n{}".format(
              scipy.asarray(self.__result, dtype=scipy.bool_),
              label_image,
          ),
      )
  
file_path: medpy/tests/graphcut_/cut.py
imports: import unittest
  from medpy.graphcut import graph_from_labels, GraphDouble, Graph, graph_from_voxels
  from medpy.graphcut.energy_voxel import boundary_difference_linear
  from medpy import filter
  import scipy
label_desc: Use image node_id 2 for calling the TestCut obj's underlying member method code with example usage: obj.test_region_based() without return types