class_docstr: This class is intended for the ResDNN Histology Network model.
class_docstr_tok: ["This","class","is","intended","for","the","ResDNN","Histology","Network","model","."]
docstr: Wrapper function to facilitate prediction of larger dataset.
  The function will mask, normalize, split, predict and 're-assemble'
  the data as a volume.
  
  Parameters
  ----------
  data : np.ndarray
      DWI signal in a 4D array
  gtab : GradientTable class instance
      The acquisition scheme matching the data (must contain at least
      one b0)
  mask : np.ndarray (optional)
      Binary mask of the brain to avoid unnecessary computation and
      unreliable prediction outside the brain.
      Default: Compute prediction only for nonzero voxels (with at least
      one nonzero DWI value).
  
  Returns
  -------
  pred_sh_coef : np.ndarray (x, y, z, M)
      Predicted fODF (as SH). The volume has matching shape to the input
      data, but with ``(sh_order + 1) * (sh_order + 2) / 2`` as a last
      dimension.
docstr_tok: ["Wrapper","function","to","facilitate","prediction","of","larger","dataset",".","The","function","will","mask",",","normalize",",","split",",","predict","and","'re-assemble'","the","data","as","a","volume",".","Parameters","--","--","--","--","--","data",":","np.ndarray","DWI","signal","in","a","4D","array","gtab",":","GradientTable","class","instance","The","acquisition","scheme","matching","the","data","(","must","contain","at","least","one","b0",")","mask",":","np.ndarray","(","optional",")","Binary","mask","of","the","brain","to","avoid","unnecessary","computation","and","unreliable","prediction","outside","the","brain",".","Default",":","Compute","prediction","only","for","nonzero","voxels","(","with","at","least","one","nonzero","DWI","value",")",".","Returns","--","--","--","-","pred_sh_coef",":","np.ndarray","(","x",",","y",",","z",",","M",")","Predicted","fODF","(","as","SH",")",".","The","volume","has","matching","shape","to","the","input","data",",","but","with","``","(","sh_order","+","1",")","*","(","sh_order","+","2",")","\/","2","``","as","a","last","dimension","."]
code: def predict(self, data, gtab, mask=None, chunk_size=1000):
      """Wrapper function to facilitate prediction of larger dataset.
      The function will mask, normalize, split, predict and 're-assemble'
      the data as a volume.
  
      Parameters
      ----------
      data : np.ndarray
          DWI signal in a 4D array
      gtab : GradientTable class instance
          The acquisition scheme matching the data (must contain at least
          one b0)
      mask : np.ndarray (optional)
          Binary mask of the brain to avoid unnecessary computation and
          unreliable prediction outside the brain.
          Default: Compute prediction only for nonzero voxels (with at least
          one nonzero DWI value).
  
      Returns
      -------
      pred_sh_coef : np.ndarray (x, y, z, M)
          Predicted fODF (as SH). The volume has matching shape to the input
          data, but with ``(sh_order + 1) * (sh_order + 2) / 2`` as a last
          dimension.
  
      """
      if mask is None:
          logger.warning(
              "Mask should be provided to accelerate "
              "computation, and because predictions are "
              "not reliable outside of the brain."
          )
          mask = np.sum(data, axis=-1)
      mask = mask.astype(bool)
  
      # Extract B0's and obtain a mean B0
      b0_indices = gtab.b0s_mask
      if not len(b0_indices) > 0:
          raise ValueError("b0 must be present for DWI normalization.")
      logger.info(
          "b0 indices found are: {}".format(
              np.argwhere(b0_indices).ravel()
          )
      )
  
      mean_b0 = np.mean(data[..., b0_indices], axis=-1)
  
      # Detect number of b-values and extract a single shell of DW-MRI Data
      unique_shells = np.sort(unique_bvals_magnitude(gtab.bvals))
      logger.info("Number of b-values: {}".format(unique_shells))
  
      # Extract DWI only
      dw_indices = get_bval_indices(gtab.bvals, unique_shells[1])
      dw_data = data[..., dw_indices]
      dw_bvecs = gtab.bvecs[dw_indices, :]
  
      # Normalize the DW-MRI Data with the mean b0 (voxel-wise)
      norm_dw_data = np.zeros(dw_data.shape)
      for n in range(len(dw_indices)):
          norm_dw_data[..., n] = np.divide(
              dw_data[..., n], mean_b0, where=np.abs(mean_b0) > 0.000001
          )
  
      # Fit SH to the raw DWI signal
      h_sphere = HemiSphere(xyz=dw_bvecs)
      dw_sh_coef = sf_to_sh(
          norm_dw_data,
          h_sphere,
          smooth=0.0006,
          basis_type=self.basis_type,
          sh_order=self.sh_order,
      )
  
      # Flatten and mask the data (N, SH_SIZE) to facilitate chunks
      ori_shape = dw_sh_coef.shape
      flat_dw_sh_coef = dw_sh_coef[mask > 0]
      flat_pred_sh_coef = np.zeros(flat_dw_sh_coef.shape)
  
      count = len(flat_dw_sh_coef) // chunk_size
      for i in range(count + 1):
          if i % 100 == 0 or i == count:
              logger.info("Chunk #{} out of {}".format(i, count))
          tmp_sh = self.__predict(
              flat_dw_sh_coef[i * chunk_size : (i + 1) * chunk_size]
          )
  
          # Removing negative values from the SF
          sphere = get_sphere("repulsion724")
          tmp_sf = sh_to_sf(
              sh=tmp_sh,
              sphere=sphere,
              basis_type=self.basis_type,
              sh_order=self.sh_order,
          )
          tmp_sf[tmp_sf < 0] = 0
          tmp_sh = sf_to_sh(
              tmp_sf,
              sphere,
              smooth=0.0006,
              basis_type=self.basis_type,
              sh_order=self.sh_order,
          )
          flat_pred_sh_coef[
              i * chunk_size : (i + 1) * chunk_size
          ] = tmp_sh
  
      pred_sh_coef = np.zeros(ori_shape)
      pred_sh_coef[mask > 0] = flat_pred_sh_coef
  
      return pred_sh_coef
  
file_path: dipy/dipy/nn/histo_resdnn.py
imports: import logging
  import numpy
  from dipy.core.gradients import unique_bvals_magnitude, get_bval_indices
  from dipy.core.sphere import HemiSphere
  from dipy.data import get_sphere, get_fnames
  from dipy.reconst.shm import sf_to_sh, sh_to_sf, sph_harm_ind_list
  from dipy.testing.decorators import doctest_skip_parser
  from dipy.utils.optpkg import optional_package
  from dipy.nn.utils import set_logger_level
label_desc: Use image node_id 5 for calling the HistoResDNN obj's underlying member method code with example usage: obj.predict(data, gtab, mask, chunk_size) and returns: pred_sh_coef