MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / create_file_basename

Function create_file_basename

monai/data/utils.py:984–1056  ·  view source on GitHub ↗

Utility function to create the path to the output file based on the input filename (file name extension is not added by this function). When ``data_root_dir`` is not specified, the output file name is: `folder_path/input_file_name (no ext.) /input_file_name (no ext.)[_postfix][

(
    postfix: str,
    input_file_name: PathLike,
    folder_path: PathLike,
    data_root_dir: PathLike = "",
    separate_folder: bool = True,
    patch_index=None,
    makedirs: bool = True,
)

Source from the content-addressed store, hash-verified

982
983
984def create_file_basename(
985 postfix: str,
986 input_file_name: PathLike,
987 folder_path: PathLike,
988 data_root_dir: PathLike = "",
989 separate_folder: bool = True,
990 patch_index=None,
991 makedirs: bool = True,
992) -> str:
993 """
994 Utility function to create the path to the output file based on the input
995 filename (file name extension is not added by this function).
996 When ``data_root_dir`` is not specified, the output file name is:
997
998 `folder_path/input_file_name (no ext.) /input_file_name (no ext.)[_postfix][_patch_index]`
999
1000 otherwise the relative path with respect to ``data_root_dir`` will be inserted, for example:
1001
1002 .. code-block:: python
1003
1004 from monai.data import create_file_basename
1005 create_file_basename(
1006 postfix="seg",
1007 input_file_name="/foo/bar/test1/image.png",
1008 folder_path="/output",
1009 data_root_dir="/foo/bar",
1010 separate_folder=True,
1011 makedirs=False)
1012 # output: /output/test1/image/image_seg
1013
1014 Args:
1015 postfix: output name's postfix
1016 input_file_name: path to the input image file.
1017 folder_path: path for the output file
1018 data_root_dir: if not empty, it specifies the beginning parts of the input file's
1019 absolute path. This is used to compute `input_file_rel_path`, the relative path to the file from
1020 `data_root_dir` to preserve folder structure when saving in case there are files in different
1021 folders with the same file names.
1022 separate_folder: whether to save every file in a separate folder, for example: if input filename is
1023 `image.nii`, postfix is `seg` and folder_path is `output`, if `True`, save as:
1024 `output/image/image_seg.nii`, if `False`, save as `output/image_seg.nii`. default to `True`.
1025 patch_index: if not None, append the patch index to filename.
1026 makedirs: whether to create the folder if it does not exist.
1027 """
1028
1029 # get the filename and directory
1030 filedir, filename = os.path.split(input_file_name)
1031 # remove extension
1032 filename, ext = os.path.splitext(filename)
1033 if ext == ".gz":
1034 filename, ext = os.path.splitext(filename)
1035 # use data_root_dir to find relative path to file
1036 filedir_rel_path = ""
1037 if data_root_dir and filedir:
1038 filedir_rel_path = os.path.relpath(filedir, data_root_dir)
1039
1040 # output folder path will be original name without the extension
1041 output = os.path.join(folder_path, filedir_rel_path)

Callers 3

filenameMethod · 0.90
test_valueMethod · 0.90
test_relative_pathMethod · 0.90

Calls 1

splitMethod · 0.80

Tested by 2

test_valueMethod · 0.72
test_relative_pathMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…