docstr: Refine and recognize the model_bundle in self.streamlines
  This method expects once pruned streamlines as input. It refines the
  first output of recobundle by applying second local slr (optional),
  and second pruning. This method is useful when we are dealing with
  noisy data or when we want to extract small tracks from tractograms.
  This time, search space is created using pruned bundle and not model
  bundle.
  
  Parameters
  ----------
  model_bundle : Streamlines
      model bundle streamlines used as a reference to extract similar
      streamlines from input tractogram
  pruned_streamlines : Streamlines
      Recognized bundle from target tractogram by RecoBundles.
  model_clust_thr : float
      MDF distance threshold for the model bundles
  reduction_thr : float
      Reduce search space by (mm) (default 14)
  reduction_distance : string
      Reduction distance type can be mdf or mam (default mdf)
  slr : bool
      Use Streamline-based Linear Registration (SLR) locally
      (default True)
  slr_metric : BundleMinDistanceMetric
  slr_x0 : array or int or str
      Transformation allowed. translation, rigid, similarity or scaling
      Initial parametrization for the optimization.
  
      If 1D array with:
          a) 6 elements then only rigid registration is performed with
          the 3 first elements for translation and 3 for rotation.
          b) 7 elements also isotropic scaling is performed (similarity).
          c) 12 elements then translation, rotation (in degrees),
          scaling and shearing are performed (affine).
  
          Here is an example of x0 with 12 elements:
          ``x0=np.array([0, 10, 0, 40, 0, 0, 2., 1.5, 1, 0.1, -0.5, 0])``
  
          This has translation (0, 10, 0), rotation (40, 0, 0) in
          degrees, scaling (2., 1.5, 1) and shearing (0.1, -0.5, 0).
  
      If int:
          a) 6
              ``x0 = np.array([0, 0, 0, 0, 0, 0])``
          b) 7
              ``x0 = np.array([0, 0, 0, 0, 0, 0, 1.])``
          c) 12
              ``x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0])``
  
      If str:
          a) "rigid"
              ``x0 = np.array([0, 0, 0, 0, 0, 0])``
          b) "similarity"
              ``x0 = np.array([0, 0, 0, 0, 0, 0, 1.])``
          c) "affine"
              ``x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0])
      (default None)
  slr_bounds : array
      (default None)
  slr_select : tuple
      Select the number of streamlines from model to neirborhood of
      model to perform the local SLR.
  slr_method : string
      Optimization method 'L_BFGS_B' or 'Powell' optimizers can be used.
      (default 'L-BFGS-B')
  pruning_thr : float
      Pruning after reducing the search space (default 6).
  pruning_distance : string
      Pruning distance type can be mdf or mam (default mdf)
  
  Returns
  -------
  recognized_transf : Streamlines
      Recognized bundle in the space of the model tractogram
  recognized_labels : array
      Indices of recognized bundle in the original tractogram
  
  References
  ----------
  .. [Garyfallidis17] Garyfallidis et al. Recognition of white matter
      bundles using local and global streamline-based registration and
      clustering, Neuroimage, 2017.
  
  .. [Chandio2020] Chandio, B.Q., Risacher, S.L., Pestilli, F.,
  Bullock, D., Yeh, FC., Koudoro, S., Rokem, A., Harezlak, J., and
  Garyfallidis, E. Bundle analytics, a computational framework for
  investigating the shapes and profiles of brain pathways across
  populations. Sci Rep 10, 17149 (2020)
docstr_tok: ["Refine","and","recognize","the","model_bundle","in","self.streamlines","This","method","expects","once","pruned","streamlines","as","input",".","It","refines","the","first","output","of","recobundle","by","applying","second","local","slr","(","optional",")",",","and","second","pruning",".","This","method","is","useful","when","we","are","dealing","with","noisy","data","or","when","we","want","to","extract","small","tracks","from","tractograms",".","This","time",",","search","space","is","created","using","pruned","bundle","and","not","model","bundle",".","Parameters","--","--","--","--","--","model_bundle",":","Streamlines","model","bundle","streamlines","used","as","a","reference","to","extract","similar","streamlines","from","input","tractogram","pruned_streamlines",":","Streamlines","Recognized","bundle","from","target","tractogram","by","RecoBundles",".","model_clust_thr",":","float","MDF","distance","threshold","for","the","model","bundles","reduction_thr",":","float","Reduce","search","space","by","(","mm",")","(","default","14",")","reduction_distance",":","string","Reduction","distance","type","can","be","mdf","or","mam","(","default","mdf",")","slr",":","bool","Use","Streamline-based","Linear","Registration","(","SLR",")","locally","(","default","True",")","slr_metric",":","BundleMinDistanceMetric","slr_x0",":","array","or","int","or","str","Transformation","allowed",".","translation",",","rigid",",","similarity","or","scaling","Initial","parametrization","for","the","optimization",".","If","1D","array","with",":","a",")","6","elements","then","only","rigid","registration","is","performed","with","the","3","first","elements","for","translation","and","3","for","rotation",".","b",")","7","elements","also","isotropic","scaling","is","performed","(","similarity",")",".","c",")","12","elements","then","translation",",","rotation","(","in","degrees",")",",","scaling","and","shearing","are","performed","(","affine",")",".","Here","is","an","example","of","x0","with","12","elements",":","``","x0=np.array","(","[","0",",","10",",","0",",","40",",","0",",","0",",","2.",",","1.5",",","1",",","0.1",",","-0.5",",","0","]",")","``","This","has","translation","(","0",",","10",",","0",")",",","rotation","(","40",",","0",",","0",")","in","degrees",",","scaling","(","2.",",","1.5",",","1",")","and","shearing","(","0.1",",","-0.5",",","0",")",".","If","int",":","a",")","6","``","x0","=","np.array","(","[","0",",","0",",","0",",","0",",","0",",","0","]",")","``","b",")","7","``","x0","=","np.array","(","[","0",",","0",",","0",",","0",",","0",",","0",",","1",".","]",")","``","c",")","12","``","x0","=","np.array","(","[","0",",","0",",","0",",","0",",","0",",","0",",","1.",",","1.",",","1",",","0",",","0",",","0","]",")","``","If","str",":","a",")","``","rigid","''","``","x0","=","np.array","(","[","0",",","0",",","0",",","0",",","0",",","0","]",")","``","b",")","``","similarity","''","``","x0","=","np.array","(","[","0",",","0",",","0",",","0",",","0",",","0",",","1",".","]",")","``","c",")","``","affine","''","``","x0","=","np.array","(","[","0",",","0",",","0",",","0",",","0",",","0",",","1.",",","1.",",","1",",","0",",","0",",","0","]",")","(","default","None",")","slr_bounds",":","array","(","default","None",")","slr_select",":","tuple","Select","the","number","of","streamlines","from","model","to","neirborhood","of","model","to","perform","the","local","SLR",".","slr_method",":","string","Optimization","method","'L_BFGS_B","'","or","'Powell","'","optimizers","can","be","used",".","(","default","'","L-BFGS-B","'",")","pruning_thr",":","float","Pruning","after","reducing","the","search","space","(","default","6",")",".","pruning_distance",":","string","Pruning","distance","type","can","be","mdf","or","mam","(","default","mdf",")","Returns","--","--","--","-","recognized_transf",":","Streamlines","Recognized","bundle","in","the","space","of","the","model","tractogram","recognized_labels",":","array","Indices","of","recognized","bundle","in","the","original","tractogram","References","--","--","--","--","--","..","[","Garyfallidis17","]","Garyfallidis","et","al",".","Recognition","of","white","matter","bundles","using","local","and","global","streamline-based","registration","and","clustering",",","Neuroimage",",","2017",".","..","[","Chandio2020","]","Chandio",",","B.Q.",",","Risacher",",","S.L.",",","Pestilli",",","F.",",","Bullock",",","D.",",","Yeh",",","FC.",",","Koudoro",",","S.",",","Rokem",",","A.",",","Harezlak",",","J.",",","and","Garyfallidis",",","E.","Bundle","analytics",",","a","computational","framework","for","investigating","the","shapes","and","profiles","of","brain","pathways","across","populations",".","Sci","Rep","10",",","17149","(","2020",")"]
code: def refine(
      self,
      model_bundle,
      pruned_streamlines,
      model_clust_thr,
      reduction_thr=14,
      reduction_distance="mdf",
      slr=True,
      slr_metric=None,
      slr_x0=None,
      slr_bounds=None,
      slr_select=(400, 600),
      slr_method="L-BFGS-B",
      pruning_thr=6,
      pruning_distance="mdf",
  ):
      """Refine and recognize the model_bundle in self.streamlines
      This method expects once pruned streamlines as input. It refines the
      first output of recobundle by applying second local slr (optional),
      and second pruning. This method is useful when we are dealing with
      noisy data or when we want to extract small tracks from tractograms.
      This time, search space is created using pruned bundle and not model
      bundle.
  
      Parameters
      ----------
      model_bundle : Streamlines
          model bundle streamlines used as a reference to extract similar
          streamlines from input tractogram
      pruned_streamlines : Streamlines
          Recognized bundle from target tractogram by RecoBundles.
      model_clust_thr : float
          MDF distance threshold for the model bundles
      reduction_thr : float
          Reduce search space by (mm) (default 14)
      reduction_distance : string
          Reduction distance type can be mdf or mam (default mdf)
      slr : bool
          Use Streamline-based Linear Registration (SLR) locally
          (default True)
      slr_metric : BundleMinDistanceMetric
      slr_x0 : array or int or str
          Transformation allowed. translation, rigid, similarity or scaling
          Initial parametrization for the optimization.
  
          If 1D array with:
              a) 6 elements then only rigid registration is performed with
              the 3 first elements for translation and 3 for rotation.
              b) 7 elements also isotropic scaling is performed (similarity).
              c) 12 elements then translation, rotation (in degrees),
              scaling and shearing are performed (affine).
  
              Here is an example of x0 with 12 elements:
              ``x0=np.array([0, 10, 0, 40, 0, 0, 2., 1.5, 1, 0.1, -0.5, 0])``
  
              This has translation (0, 10, 0), rotation (40, 0, 0) in
              degrees, scaling (2., 1.5, 1) and shearing (0.1, -0.5, 0).
  
          If int:
              a) 6
                  ``x0 = np.array([0, 0, 0, 0, 0, 0])``
              b) 7
                  ``x0 = np.array([0, 0, 0, 0, 0, 0, 1.])``
              c) 12
                  ``x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0])``
  
          If str:
              a) "rigid"
                  ``x0 = np.array([0, 0, 0, 0, 0, 0])``
              b) "similarity"
                  ``x0 = np.array([0, 0, 0, 0, 0, 0, 1.])``
              c) "affine"
                  ``x0 = np.array([0, 0, 0, 0, 0, 0, 1., 1., 1, 0, 0, 0])
          (default None)
      slr_bounds : array
          (default None)
      slr_select : tuple
          Select the number of streamlines from model to neirborhood of
          model to perform the local SLR.
      slr_method : string
          Optimization method 'L_BFGS_B' or 'Powell' optimizers can be used.
          (default 'L-BFGS-B')
      pruning_thr : float
          Pruning after reducing the search space (default 6).
      pruning_distance : string
          Pruning distance type can be mdf or mam (default mdf)
  
      Returns
      -------
      recognized_transf : Streamlines
          Recognized bundle in the space of the model tractogram
      recognized_labels : array
          Indices of recognized bundle in the original tractogram
  
      References
      ----------
      .. [Garyfallidis17] Garyfallidis et al. Recognition of white matter
          bundles using local and global streamline-based registration and
          clustering, Neuroimage, 2017.
  
      .. [Chandio2020] Chandio, B.Q., Risacher, S.L., Pestilli, F.,
      Bullock, D., Yeh, FC., Koudoro, S., Rokem, A., Harezlak, J., and
      Garyfallidis, E. Bundle analytics, a computational framework for
      investigating the shapes and profiles of brain pathways across
      populations. Sci Rep 10, 17149 (2020)
      """
      if self.verbose:
          t = time()
          logger.info("## Refine recognize given bundle ## \n")
  
      model_centroids = self._cluster_model_bundle(
          model_bundle, model_clust_thr=model_clust_thr
      )
  
      pruned_model_centroids = self._cluster_model_bundle(
          pruned_streamlines, model_clust_thr=model_clust_thr
      )
  
      neighb_streamlines, neighb_indices = self._reduce_search_space(
          pruned_model_centroids,
          reduction_thr=reduction_thr,
          reduction_distance=reduction_distance,
      )
  
      if len(neighb_streamlines) == 0:  # if no streamlines recognized
          return Streamlines([]), []
  
      if self.verbose:
          logger.info("2nd local Slr")
  
      if slr:
          transf_streamlines, slr2_bmd = self._register_neighb_to_model(
              model_bundle,
              neighb_streamlines,
              metric=slr_metric,
              x0=slr_x0,
              bounds=slr_bounds,
              select_model=slr_select[0],
              select_target=slr_select[1],
              method=slr_method,
          )
      if self.verbose:
          logger.info("pruning after 2nd local Slr")
  
      pruned_streamlines, labels = self._prune_what_not_in_model(
          model_centroids,
          transf_streamlines,
          neighb_indices,
          pruning_thr=pruning_thr,
          pruning_distance=pruning_distance,
      )
  
      if self.verbose:
          logger.info(
              f"Total duration of recognition time"
              f" is {time()-t:0.3f} s\n"
          )
  
      return pruned_streamlines, self.filtered_indices[labels]
  
imports: from time import time
  from itertools import chain
  import logging
  import numpy
  from dipy.tracking.streamline import set_number_of_points, nbytes, select_random_set_of_streamlines
  from dipy.segment.clustering import qbx_and_merge
  from dipy.tracking.distances import bundles_distances_mdf, bundles_distances_mam
  from dipy.align.streamlinear import StreamlineLinearRegistration, BundleMinDistanceMetric, BundleSumDistanceMatrixMetric, BundleMinDistanceAsymmetricMetric
  from dipy.tracking.streamline import Streamlines, length
  from dipy.utils.deprecator import deprecated_params
  from nibabel.affines import apply_affine
label_desc: Use image node_id 4 for calling the RecoBundles obj's underlying member method code with example usage: obj.refine(model_bundle, pruned_streamlines, model_clust_thr, reduction_thr, reduction_distance, slr, slr_metric, slr_x0, slr_bounds, slr_select, slr_method, pruning_thr, pruning_distance) and returns: pruned_streamlines,  self, Streamlines,  list