code: def path_under_rev_dataset(ds, path):
      ds_path = ds.pathobj
      try:
          rpath = str(ut.Path(path).relative_to(ds_path))
          if not rpath.startswith(op.pardir):
              # path is already underneath the dataset
              return path
      except Exception:
          # whatever went wrong, we gotta play save
          pass
  
      root = get_dataset_root(str(path))
      while root is not None and not ds_path.samefile(root):
          # path and therefore root could be relative paths,
          # hence in the next round we cannot use dirname()
          # to jump in the the next directory up, but we have
          # to use ./.. and get_dataset_root() will handle
          # the rest just fine
          root = get_dataset_root(op.join(root, op.pardir))
      if root is None:
          return None
      return ds_path / op.relpath(str(path), root)
  
imports: import inspect
  import logging
  from functools import wraps
  from os.path import curdir, exists, join, normpath, pardir
  from weakref import WeakValueDictionary
  from datalad import cfg
  from datalad.config import ConfigManager
  from datalad.core.local.repo import repo_from_path
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.constraints import Constraint
  from datalad.support.due import due
  from datalad.support.due_utils import duecredit_dataset
  from datalad.support.exceptions import NoDatasetFound
  from datalad.dataset.repo import path_based_str_repr, PathBasedFlyweight
  from datalad.support.gitrepo import GitRepo
  from datalad.support import path
  import datalad.utils
  from datalad.utils import getpwd, optional_args, get_dataset_root, get_sig_param_names, get_dataset_root, Path, PurePath, ensure_list
label_desc: Use image node_id 4 for calling a global function with example usage: path_under_rev_dataset(ds, path) and returns: unknown, None, path