code: def spatial_gradient(x: torch.Tensor, dim: int) -> torch.Tensor:
      """
      Calculate gradients on single dimension of a tensor using central finite difference.
      It moves the tensor along the dimension to calculate the approximate gradient
      dx[i] = (x[i+1] - x[i-1]) / 2.
      Adapted from:
          DeepReg (https://github.com/DeepRegNet/DeepReg)
  
      Args:
          x: the shape should be BCH(WD).
          dim: dimension to calculate gradient along.
      Returns:
          gradient_dx: the shape should be BCH(WD)
      """
      slice_1 = slice(1, -1)
      slice_2_s = slice(2, None)
      slice_2_e = slice(None, -2)
      slice_all = slice(None)
      slicing_s, slicing_e = [slice_all, slice_all], [
          slice_all,
          slice_all,
      ]
      while len(slicing_s) < x.ndim:
          slicing_s = slicing_s + [slice_1]
          slicing_e = slicing_e + [slice_1]
      slicing_s[dim] = slice_2_s
      slicing_e[dim] = slice_2_e
      return (x[slicing_s] - x[slicing_e]) / 2.0
  
file_path: MONAI/monai/losses/deform.py
imports: from __future__ import annotations
  import torch
  from torch.nn.modules.loss import _Loss
  from monai.utils import LossReduction
label_desc: Use image node_id 1 for calling a global function with example usage: spatial_gradient(x, dim) and returns: unknown