code: def threshold_connect_components(map, threshold, copy=True):
      """Given a map with some coefficients set to zero, segment the
          connect components with number of voxels smaller than the
          threshold and set them to 0.
  
      Parameters
      ----------
      map: ndarray,
           The spatial map to segment
      threshold: scalar,
                 The minimum number of voxels to keep a cluster.
      copy: bool, optional
            If copy is false, the input array is modified inplace
  
      Returns
      -------
      map: ndarray,
           the map with connected components removed
      """
      labels, _ = ndimage.label(map)
      weights = np.bincount(labels.ravel())
      if copy:
          map = map.copy()
      for label, weight in enumerate(weights):
          if label == 0:
              continue
          if weight < threshold:
              map[labels == label] = 0
      return map
  
file_path: nipy/nipy/labs/mask.py
imports: import math
  import numpy
  from nibabel import load, nifti1, save
  from scipy import ndimage
  from ..io.nibcompat import get_affine, get_header, get_unscaled_data
label_desc: Use image node_id 2 for calling a global function with example usage: threshold_connect_components(map, threshold, copy) and returns: map