code: def hroi_agglomeration(input_hroi, criterion="size", smin=0):
      """Performs an agglomeration then a selection of regions
      so that a certain size or volume criterion is satisfied.
  
      Parameters
      ----------
      input_hroi: HierarchicalROI instance
        The input hROI
      criterion: str, optional
        To be chosen among 'size' or 'volume'
      smin: float, optional
        The applied criterion
  
      Returns
      -------
      output_hroi:  HierarchicalROI instance
      """
      if criterion not in ["size", "volume"]:
          return ValueError("unknown criterion")
      output_hroi = input_hroi.copy()
      k = 2 * output_hroi.k
      if criterion == "size":
          value = output_hroi.get_size()
      if criterion == "volume":
          value = output_hroi.get_volume()
  
      # iteratively agglomerate regions that are too small
      while k > output_hroi.k:
          k = output_hroi.k
          # regions agglomeration
          output_hroi.merge_ascending(
              output_hroi.get_id()[value <= smin]
          )
          # suppress parents nodes having only one child
          output_hroi.merge_descending()
          # early stopping 1
          if output_hroi.k == 0:
              break
          # early stopping 2
          if criterion == "size":
              value = output_hroi.get_size()
          if criterion == "volume":
              value = output_hroi.get_volume()
          if value.max() < smin:
              break
  
      # finally remove those regions for which the criterion cannot be matched
      output_hroi.select_roi(output_hroi.get_id()[value > smin])
      return output_hroi
  
imports: import numpy
  from nipy.algorithms.graph.field import field_from_coo_matrix_and_data
  from nipy.algorithms.graph.forest import Forest
  from nipy.algorithms.graph.graph import WeightedGraph
  from .mroi import SubDomains
label_desc: Use image node_id 1 for calling a global function with example usage: hroi_agglomeration(input_hroi, criterion, smin) and returns: output_hroi, ValueError