code: def fixed_parcellation(
      mask_image,
      betas,
      nbparcel,
      nn=6,
      method="ward",
      write_dir=None,
      mu=10.0,
      verbose=0,
      fullpath=None,
  ):
      """Fixed parcellation of a given dataset
  
      Parameters
      ----------
      domain/mask_image
      betas: list of paths to activation images from the subject
      nbparcel, int : number of desired parcels
      nn=6: number of nearest neighbors  to define the image topology
            (6, 18 or 26)
      method='ward': clustering method used, to be chosen among
                     'ward', 'gkm', 'ward_and-gkm'
                     'ward': Ward's clustering algorithm
                     'gkm': Geodesic k-means algorithm, random initialization
                     'gkm_and_ward': idem, initialized by Ward's clustering
      write_di: string, topional, write directory.
                      If fullpath is None too, then no file output.
      mu = 10., float: the relative weight of anatomical information
      verbose=0: verbosity mode
      fullpath=None, string,
                     path of the output image
                     If write_dir and fullpath are None then no file output.
                     If only fullpath is None then it is the write dir + a name
                     depending on the method.
  
      Notes
      -----
      Ward's method takes time (about 6 minutes for a 60K voxels dataset)
  
      Geodesic k-means is 'quick and dirty'
  
      Ward's + GKM is expensive but quite good
  
      To reduce CPU time, rather use nn=6 (especially with Ward)
      """
      from nipy.algorithms.graph.field import (
          field_from_coo_matrix_and_data,
      )
  
      if method not in ["ward", "gkm", "ward_and_gkm", "kmeans"]:
          raise ValueError("unknown method")
      if nn not in [6, 18, 26]:
          raise ValueError("nn should be 6,18 or 26")
  
      # step 1: load the data ----------------------------
      # 1.1 the domain
      domain = grid_domain_from_image(mask_image, nn)
  
      if method != "kmeans":
          # 1.2 get the main cc of the graph
          # to remove the small connected components
          pass
  
      coord = domain.coord
  
      # 1.3 read the functional data
      beta = np.array(
          [domain.make_feature_from_image(b) for b in betas]
      )
  
      if len(beta.shape) > 2:
          beta = np.squeeze(beta)
  
      if beta.shape[0] != domain.size:
          beta = beta.T
  
      feature = np.hstack((beta, mu * coord / np.std(coord)))
  
      # step 2: parcellate the data ---------------------------
  
      if method != "kmeans":
          g = field_from_coo_matrix_and_data(domain.topology, feature)
  
      if method == "kmeans":
          _, u, _ = kmeans(feature, nbparcel)
  
      if method == "ward":
          u, _ = g.ward(nbparcel)
  
      if method == "gkm":
          seeds = np.argsort(np.random.rand(g.V))[:nbparcel]
          _, u, _ = g.geodesic_kmeans(seeds)
  
      if method == "ward_and_gkm":
          w, _ = g.ward(nbparcel)
          _, u, _ = g.geodesic_kmeans(label=w)
  
      lpa = SubDomains(domain, u)
  
      if verbose:
          var_beta = np.array(
              [
                  np.var(beta[lpa.label == k], 0).sum()
                  for k in range(lpa.k)
              ]
          )
          var_coord = np.array(
              [
                  np.var(coord[lpa.label == k], 0).sum()
                  for k in range(lpa.k)
              ]
          )
          size = lpa.get_size()
          vf = np.dot(var_beta, size) / size.sum()
          va = np.dot(var_coord, size) / size.sum()
          print(
              nbparcel,
              "functional variance",
              vf,
              "anatomical variance",
              va,
          )
  
      # step3:  write the resulting label image
      if fullpath is not None:
          label_image = fullpath
      elif write_dir is not None:
          label_image = os.path.join(write_dir, f"parcel_{method}.nii")
      else:
          label_image = None
  
      if label_image is not None:
          lpa_img = lpa.to_image(
              fid="id",
              roi=True,
              descrip="Intra-subject parcellation image",
          )
          save(lpa_img, label_image)
          if verbose:
              print(f"Wrote the parcellation images as {label_image}")
  
      return lpa
  
file_path: nipy/nipy/labs/spatial_models/parcel_io.py
imports: import os.path
  from warnings import warn
  import numpy
  from nibabel import Nifti1Image, load, save
  from nipy.algorithms.clustering.utils import kmeans
  from nipy.io.nibcompat import get_affine, get_header
  from ..mask import intersect_masks
  from .discrete_domain import grid_domain_from_image
  from .mroi import SubDomains
label_desc: Use image node_id 5 for calling a global function with example usage: fixed_parcellation(mask_image, betas, nbparcel, nn, method, write_dir, mu, verbose, fullpath) and returns: lpa