Skip to content

3435 update create_file_basename #3436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions monai/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,14 +676,15 @@ def create_file_basename(
folder_path: PathLike,
data_root_dir: PathLike = "",
separate_folder: bool = True,
patch_index: Optional[int] = None,
patch_index=None,
makedirs: bool = True,
) -> str:
"""
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]`
`folder_path/input_file_name (no ext.) /input_file_name (no ext.)[_postfix][_patch_index]`

otherwise the relative path with respect to `data_root_dir` will be inserted, for example:
input_file_name: /foo/bar/test1/image.png,
Expand All @@ -704,6 +705,7 @@ def create_file_basename(
`image.nii`, postfix is `seg` and folder_path is `output`, if `True`, save as:
`output/image/image_seg.nii`, if `False`, save as `output/image_seg.nii`. default to `True`.
patch_index: if not None, append the patch index to filename.
makedirs: whether to create the folder if it does not exist.
"""

# get the filename and directory
Expand All @@ -722,16 +724,18 @@ def create_file_basename(

if separate_folder:
output = os.path.join(output, filename)
# create target folder if no existing
os.makedirs(output, exist_ok=True)

if makedirs:
# create target folder if no existing
os.makedirs(output, exist_ok=True)

# add the sub-folder plus the postfix name to become the file basename in the output path
output = os.path.join(output, (filename + "_" + postfix) if len(postfix) > 0 else filename)

if patch_index is not None:
output += f"_{patch_index}"

return os.path.abspath(output)
return os.path.normpath(output)


def compute_importance_map(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_file_basename.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def test_value(self):
expected = os.path.join(output_tmp, "test", "test_post_8")
self.assertEqual(result, expected)

def test_relative_path(self):
output = create_file_basename("", "test.txt", "output", "", makedirs=False)
expected = os.path.join("output", "test", "test")
self.assertEqual(output, expected)


if __name__ == "__main__":
unittest.main()