Skip to content

Adding Temporary Directory Routine - #9029

Open
ericspod wants to merge 6 commits into
Project-MONAI:devfrom
ericspod:tmpdir_routine
Open

Adding Temporary Directory Routine#9029
ericspod wants to merge 6 commits into
Project-MONAI:devfrom
ericspod:tmpdir_routine

Conversation

@ericspod

Copy link
Copy Markdown
Member

Description

This adds a temporary directory creation routine to replace the following cell in many tutorial notebooks:

# Set data directory
directory = os.environ.get("MONAI_DATA_DIRECTORY")
if directory is not None:
    os.makedirs(directory, exist_ok=True)
root_dir = tempfile.mkdtemp() if directory is None else directory
print(root_dir)

This should be functionally equivalent, but can also add a finaliser function to delete the directory when the notebook closes. The notebooks can then be updated to reduce the amount of code.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@ericspod
ericspod requested review from KumoLiu and Nic-Ma as code owners July 28, 2026 16:26
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 78097b02-83e7-478a-b5fe-dd165a23d514

📥 Commits

Reviewing files that changed from the base of the PR and between 8592172 and a96b14e.

📒 Files selected for processing (1)
  • monai/apps/utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/apps/utils.py

📝 Walkthrough

Walkthrough

Adds the exported create_temp_dir utility to monai.apps. It selects an explicit directory, the configured MONAI data directory, or a random temporary directory, creates the directory, and optionally registers cleanup at process exit. Tests cover each directory-selection path and simulated finalization cleanup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding a temporary directory routine.
Description check ✅ Passed The description follows the template’s main sections and includes change type checkboxes, tests, and docstrings.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

ericspod added 2 commits July 28, 2026 17:27
…ply.github.com>

I, Eric Kerfoot <17726042+ericspod@users.noreply.github.com>, hereby add my Signed-off-by to this commit: a95e44e
I, Eric Kerfoot <17726042+ericspod@users.noreply.github.com>, hereby add my Signed-off-by to this commit: d9cd2d1
I, Eric Kerfoot <17726042+ericspod@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 5e7e430

Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
monai/apps/utils.py (2)

46-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Sort __all__ to satisfy RUF022.

Ruff reports this export list is unsorted.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@monai/apps/utils.py` around lines 46 - 54, Sort the entries in the __all__
export list alphabetically to satisfy RUF022, preserving all existing exported
symbols and their values.

Source: Linters/SAST tools


436-452: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document filesystem exceptions.

Add a Google-style Raises section for OSError from temporary-directory creation or os.makedirs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@monai/apps/utils.py` around lines 436 - 452, Update the create_temp_dir
docstring to include a Google-style Raises section documenting OSError from
temporary-directory creation and os.makedirs, without changing the function’s
behavior or existing documentation.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@monai/apps/utils.py`:
- Around line 461-462: Update the delete_on_finalise cleanup registration in
create_temp_dir to make shutil.rmtree ignore missing directories, ensuring
repeated callbacks are idempotent and exit cleanup does not raise
FileNotFoundError.

---

Nitpick comments:
In `@monai/apps/utils.py`:
- Around line 46-54: Sort the entries in the __all__ export list alphabetically
to satisfy RUF022, preserving all existing exported symbols and their values.
- Around line 436-452: Update the create_temp_dir docstring to include a
Google-style Raises section documenting OSError from temporary-directory
creation and os.makedirs, without changing the function’s behavior or existing
documentation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f394f467-bd08-4818-9576-ff8eeb031547

📥 Commits

Reviewing files that changed from the base of the PR and between 3ee058b and 5e7e430.

📒 Files selected for processing (3)
  • monai/apps/__init__.py
  • monai/apps/utils.py
  • tests/apps/test_create_temp_dir.py

Comment thread monai/apps/utils.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
tests/apps/test_create_temp_dir.py (1)

60-80: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use a descriptive file-handle name.

Rename o to file_handle or similar.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/apps/test_create_temp_dir.py` around lines 60 - 80, In
test_finalisation, rename the file handle variable o to a descriptive name such
as file_handle, and update its write call accordingly.

Source: Path instructions

monai/apps/utils.py (2)

46-54: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Sort __all__ to satisfy Ruff.

create_temp_dir should be placed alphabetically with the other exports.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@monai/apps/utils.py` around lines 46 - 54, Sort the export names in __all__
alphabetically, moving create_temp_dir into its correct position while
preserving all existing exports.

Source: Linters/SAST tools


437-452: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document propagated filesystem errors.

Add a Google-style Raises section for OSError from directory creation.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@monai/apps/utils.py` around lines 437 - 452, Update the docstring for the
temporary-directory function to add a Google-style Raises section documenting
that directory creation can raise OSError. Keep the existing arguments and
return documentation unchanged.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@monai/apps/utils.py`:
- Around line 46-54: Sort the export names in __all__ alphabetically, moving
create_temp_dir into its correct position while preserving all existing exports.
- Around line 437-452: Update the docstring for the temporary-directory function
to add a Google-style Raises section documenting that directory creation can
raise OSError. Keep the existing arguments and return documentation unchanged.

In `@tests/apps/test_create_temp_dir.py`:
- Around line 60-80: In test_finalisation, rename the file handle variable o to
a descriptive name such as file_handle, and update its write call accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d3aa5766-d56d-47e5-b8be-dea4293006fa

📥 Commits

Reviewing files that changed from the base of the PR and between 3ee058b and 8592172.

📒 Files selected for processing (3)
  • monai/apps/__init__.py
  • monai/apps/utils.py
  • tests/apps/test_create_temp_dir.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/apps/init.py

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant