parameters: (self, orientation)
return_statement: return reoriented
docstring: Realigns image data and world matrix to conform to a specific slice orientation.
  
          TODO ensure header if coorectly updated for mutlires data
docstring_summary: Realigns image data and world matrix to conform to a specific slice orientation.
docstring_tokens: Realigns image data and world matrix to conform to a specific slice orientation .
function: def reorient(self, orientation):
          """
          Realigns image data and world matrix to conform to a specific slice orientation.
  
          TODO ensure header if coorectly updated for mutlires data
          """
          trg_orientation = orientation.upper()
          src_orientation = otn.orientation_from_matrix(self.affine)
          if trg_orientation == src_orientation.upper():
              return self
  
          # extract world axes
          get_world_axes = lambda aff: np.argmax(np.absolute(np.linalg.inv(aff)), axis=0)
          trg_matrix = otn.matrix_from_orientation(trg_orientation)
          src_matrix = otn.matrix_from_orientation(src_orientation)
          world_axes_trg = get_world_axes(trg_matrix[:self.basedims, :self.basedims])
          world_axes_src = get_world_axes(src_matrix[:self.basedims, :self.basedims])
  
          voxsize = np.asarray(self.voxsize)
          voxsize = voxsize[world_axes_src][world_axes_trg]
  
          # init
          data = self.data.copy()
          affine = self.affine.copy()
  
          # align axes
          affine[:, world_axes_trg] = affine[:, world_axes_src]
          for i in range(self.basedims):
              if world_axes_src[i] != world_axes_trg[i]:
                  data = np.swapaxes(data, world_axes_src[i], world_axes_trg[i])
                  swapped_axis_idx = np.where(world_axes_src == world_axes_trg[i])
                  world_axes_src[swapped_axis_idx], world_axes_src[i] = world_axes_src[i], world_axes_src[swapped_axis_idx]
  
          # align directions
          dot_products = np.sum(affine[:3, :3] * trg_matrix[:3, :3], axis=0)
          for i in range(self.basedims):
              if dot_products[i] < 0:
                  data = np.flip(data, axis=i)
                  affine[:, i] = - affine[:, i]
                  affine[:3, 3] = affine[:3, 3] - affine[:3, i] * (data.shape[i] - 1)
  
          reoriented = Volume(data, affine, voxsize=voxsize)
          reoriented.copy_metadata(self)
          return reoriented
function_tokens: def reorient ( self , orientation ) : trg_orientation = orientation . upper ( ) src_orientation = otn . orientation_from_matrix ( self . affine ) if trg_orientation == src_orientation . upper ( ) : return self # extract world axes get_world_axes = lambda aff : np . argmax ( np . absolute ( np . linalg . inv ( aff ) ) , axis = 0 ) trg_matrix = otn . matrix_from_orientation ( trg_orientation ) src_matrix = otn . matrix_from_orientation ( src_orientation ) world_axes_trg = get_world_axes ( trg_matrix [ : self . basedims , : self . basedims ] ) world_axes_src = get_world_axes ( src_matrix [ : self . basedims , : self . basedims ] ) voxsize = np . asarray ( self . voxsize ) voxsize = voxsize [ world_axes_src ] [ world_axes_trg ] # init data = self . data . copy ( ) affine = self . affine . copy ( ) # align axes affine [ : , world_axes_trg ] = affine [ : , world_axes_src ] for i in range ( self . basedims ) : if world_axes_src [ i ] != world_axes_trg [ i ] : data = np . swapaxes ( data , world_axes_src [ i ] , world_axes_trg [ i ] ) swapped_axis_idx = np . where ( world_axes_src == world_axes_trg [ i ] ) world_axes_src [ swapped_axis_idx ] , world_axes_src [ i ] = world_axes_src [ i ] , world_axes_src [ swapped_axis_idx ] # align directions dot_products = np . sum ( affine [ : 3 , : 3 ] * trg_matrix [ : 3 , : 3 ] , axis = 0 ) for i in range ( self . basedims ) : if dot_products [ i ] < 0 : data = np . flip ( data , axis = i ) affine [ : , i ] = - affine [ : , i ] affine [ : 3 , 3 ] = affine [ : 3 , 3 ] - affine [ : 3 , i ] * ( data . shape [ i ] - 1 ) reoriented = Volume ( data , affine , voxsize = voxsize ) reoriented . copy_metadata ( self ) return reoriented