original_docstring: 
      copy sitk metadata tags from one image to another
      :param new_image:
      :param reference_sitk_img:
      :return:
      
docstring: copy sitk metadata tags from one image to another
docstring_tokens: copy sitk metadata tags from one image to another
code: def copy_meta(new_image, reference_sitk_img):
      """
      copy sitk metadata tags from one image to another
      :param new_image:
      :param reference_sitk_img:
      :return:
      """
      t1 = time()
      if isinstance(new_image, np.ndarray):
          new_image = sitk.GetImageFromArray(new_image)
  
      if reference_sitk_img is not None:
          assert (isinstance(reference_sitk_img, sitk.Image)), 'no reference image given'
          assert (isinstance(new_image, sitk.Image)), 'only np.ndarrays and sitk images could be stored'
  
          # copy metadata
          for key in reference_sitk_img.GetMetaDataKeys():
              new_image.SetMetaData(key, get_metadata_maybe(reference_sitk_img, key))
          # logging.debug('Metadata_copied: {:0.3f}s'.format(time() - t1))
  
          # copy all structural information to image with same dimension and size
          if (reference_sitk_img.GetDimension() == new_image.GetDimension()) and (
                  reference_sitk_img.GetSize() == new_image.GetSize()):
              new_image.CopyInformation(reference_sitk_img)
  
          # same dimension (e.g. 4) but different size per dimension
          elif (reference_sitk_img.GetDimension() == new_image.GetDimension()):
  
              # copy spacing and origin but keep size as it is
              new_image.SetOrigin(reference_sitk_img.GetOrigin())
              new_image.SetSpacing(reference_sitk_img.GetSpacing())
              new_image.SetDirection(reference_sitk_img.GetDirection())
  
          # copy structural informations to smaller images e.g. 4D to 3D
          elif reference_sitk_img.GetDimension() > new_image.GetDimension():
              shape_ = len(new_image.GetSize())
              reference_shape = len(reference_sitk_img.GetSize())
              new_image.SetOrigin(reference_sitk_img.GetOrigin()[:shape_])
              new_image.SetSpacing(reference_sitk_img.GetSpacing()[:shape_])
              # copy direction to smaller images
              # 1. extract the direction, 2. create a matrix, 3. slice by the new shape, 4. flatten
              if new_image.GetDimension() > 2:  # only for volumes
                  direction = np.array(reference_sitk_img.GetDirection())
                  dir_ = direction.reshape(reference_shape, reference_shape)
                  direction = dir_[:shape_, :shape_].flatten()
                  new_image.SetDirection(direction)
  
          # copy structural informations to bigger images e.g. 3D to 4D, fill with 1.0
          else:
              ones = [1.0] * (new_image.GetDimension() - reference_sitk_img.GetDimension())
              new_image.SetOrigin((*reference_sitk_img.GetOrigin(), *ones))
              new_image.SetSpacing((*reference_sitk_img.GetSpacing(), *ones))
  
          new_image.SetDirection(reference_sitk_img.GetDirection())
          # logging.debug('spatial data_copied: {:0.3f}s'.format(time() - t1))
  
          return new_image
code_tokens: def copy_meta ( new_image , reference_sitk_img ) : t1 = time ( ) if isinstance ( new_image , np . ndarray ) : new_image = sitk . GetImageFromArray ( new_image ) if reference_sitk_img is not None : assert ( isinstance ( reference_sitk_img , sitk . Image ) ) , 'no reference image given' assert ( isinstance ( new_image , sitk . Image ) ) , 'only np.ndarrays and sitk images could be stored' for key in reference_sitk_img . GetMetaDataKeys ( ) : new_image . SetMetaData ( key , get_metadata_maybe ( reference_sitk_img , key ) ) if ( reference_sitk_img . GetDimension ( ) == new_image . GetDimension ( ) ) and ( reference_sitk_img . GetSize ( ) == new_image . GetSize ( ) ) : new_image . CopyInformation ( reference_sitk_img ) elif ( reference_sitk_img . GetDimension ( ) == new_image . GetDimension ( ) ) : new_image . SetOrigin ( reference_sitk_img . GetOrigin ( ) ) new_image . SetSpacing ( reference_sitk_img . GetSpacing ( ) ) new_image . SetDirection ( reference_sitk_img . GetDirection ( ) ) elif reference_sitk_img . GetDimension ( ) > new_image . GetDimension ( ) : shape_ = len ( new_image . GetSize ( ) ) reference_shape = len ( reference_sitk_img . GetSize ( ) ) new_image . SetOrigin ( reference_sitk_img . GetOrigin ( ) [ : shape_ ] ) new_image . SetSpacing ( reference_sitk_img . GetSpacing ( ) [ : shape_ ] ) if new_image . GetDimension ( ) > 2 : direction = np . array ( reference_sitk_img . GetDirection ( ) ) dir_ = direction . reshape ( reference_shape , reference_shape ) direction = dir_ [ : shape_ , : shape_ ] . flatten ( ) new_image . SetDirection ( direction ) else : ones = [ 1.0 ] * ( new_image . GetDimension ( ) - reference_sitk_img . GetDimension ( ) ) new_image . SetOrigin ( ( * reference_sitk_img . GetOrigin ( ) , * ones ) ) new_image . SetSpacing ( ( * reference_sitk_img . GetSpacing ( ) , * ones ) ) new_image . SetDirection ( reference_sitk_img . GetDirection ( ) ) return new_image
short_docstring: copy sitk metadata tags from one image to another
short_docstring_tokens: copy sitk metadata tags from one image to another
comment: """
      copy sitk metadata tags from one image to another
      :param new_image:
      :param reference_sitk_img:
      :return:
      """$SEP$# copy metadata$SEP$# logging.debug('Metadata_copied: {:0.3f}s'.format(time() - t1))$SEP$# copy all structural information to image with same dimension and size$SEP$# same dimension (e.g. 4) but different size per dimension$SEP$# copy spacing and origin but keep size as it is$SEP$# copy structural informations to smaller images e.g. 4D to 3D$SEP$# copy direction to smaller images$SEP$# 1. extract the direction, 2. create a matrix, 3. slice by the new shape, 4. flatten$SEP$# only for volumes$SEP$# copy structural informations to bigger images e.g. 3D to 4D, fill with 1.0$SEP$# logging.debug('spatial data_copied: {:0.3f}s'.format(time() - t1))