code: def phase_fraction(im, normed=True):
      r
      if im.dtype == bool:
          im = im.astype(int)
      elif im.dtype != int:
          raise Exception('Image must contain integer values for each phase')
      labels = sp.arange(0, sp.amax(im)+1)
      results = sp.zeros_like(labels)
      for i in labels:
          results[i] = sp.sum(im == i)
      if normed:
          results = results/im.size
      return results
docs: r"""
      Calculates the number (or fraction) of each phase in an image
  
      Parameters
      ----------
      im : ND-array
          An ND-array containing integer values
      normed : Boolean
          If ``True`` (default) the returned values are normalized by the total
          number of voxels in image, otherwise the voxel count of each phase is
          returned.
  
      Returns
      -------
      result : 1D-array
          A array of length max(im) with each element containing the number of
          voxels found with the corresponding label.
  
      See Also
      --------
      porosity