Repository: Project-MONAI/MONAI

Issue:
Unsuitable default value to `bundle_root` in `ckpt_export`
Introduced by https://github.com/Project-MONAI/MONAI/pull/6965 which fixed the `bundle_root` to `os.getcwd()` without updating.
```
[2023-10-12T08:17:01.366Z] Traceback (most recent call last):
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/monai/bundle/config_item.py", line 377, in evaluate
[2023-10-12T08:17:01.366Z]     return eval(value[len(self.prefix) :], globals_, locals)
[2023-10-12T08:17:01.366Z]   File "<string>", line 1, in <module>
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/torch/serialization.py", line 771, in load
[2023-10-12T08:17:01.366Z]     with _open_file_like(f, 'rb') as opened_file:
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/torch/serialization.py", line 270, in _open_file_like
[2023-10-12T08:17:01.366Z]     return _open_file(name_or_buffer, mode)
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/torch/serialization.py", line 251, in __init__
[2023-10-12T08:17:01.366Z]     super(_open_file, self).__init__(open(name, mode))
[2023-10-12T08:17:01.366Z] FileNotFoundError: [Errno 2] No such file or directory: './models/search_code_18590.pt'
```

Unsuitable default value to `bundle_root` in `ckpt_export`
Introduced by https://github.com/Project-MONAI/MONAI/pull/6965 which fixed the `bundle_root` to `os.getcwd()` without updating.
```
[2023-10-12T08:17:01.366Z] Traceback (most recent call last):
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/monai/bundle/config_item.py", line 377, in evaluate
[2023-10-12T08:17:01.366Z]     return eval(value[len(self.prefix) :], globals_, locals)
[2023-10-12T08:17:01.366Z]   File "<string>", line 1, in <module>
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/torch/serialization.py", line 771, in load
[2023-10-12T08:17:01.366Z]     with _open_file_like(f, 'rb') as opened_file:
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/torch/serialization.py", line 270, in _open_file_like
[2023-10-12T08:17:01.366Z]     return _open_file(name_or_buffer, mode)
[2023-10-12T08:17:01.366Z]   File "/root/.local/share/virtualenvs/MONAI-zoo-premerge-CHR6GQKS/lib/python3.8/site-packages/torch/serialization.py", line 251, in __init__
[2023-10-12T08:17:01.366Z]     super(_open_file, self).__init__(open(name, mode))
[2023-10-12T08:17:01.366Z] FileNotFoundError: [Errno 2] No such file or directory: './models/search_code_18590.pt'
```



Discussion:

Resolving patch:
diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py
--- a/monai/bundle/scripts.py
+++ b/monai/bundle/scripts.py
@@ -1273,7 +1273,6 @@ def ckpt_export(
         config_file_,
         filepath_,
         ckpt_file_,
-        bundle_root_,
         net_id_,
         meta_file_,
         key_in_ckpt_,
@@ -1285,7 +1284,6 @@ def ckpt_export(
         "config_file",
         filepath=None,
         ckpt_file=None,
-        bundle_root=os.getcwd(),
         net_id=None,
         meta_file=None,
         key_in_ckpt="",
@@ -1293,18 +1291,23 @@ def ckpt_export(
         input_shape=None,
         converter_kwargs={},
     )
+    bundle_root = _args.get("bundle_root", os.getcwd())
 
     parser = ConfigParser()
-
     parser.read_config(f=config_file_)
-    meta_file_ = os.path.join(bundle_root_, "configs", "metadata.json") if meta_file_ is None else meta_file_
-    filepath_ = os.path.join(bundle_root_, "models", "model.ts") if filepath_ is None else filepath_
-    ckpt_file_ = os.path.join(bundle_root_, "models", "model.pt") if ckpt_file_ is None else ckpt_file_
-    if not os.path.exists(ckpt_file_):
-        raise FileNotFoundError(f'Checkpoint file "{ckpt_file_}" not found, please specify it in argument "ckpt_file".')
+    meta_file_ = os.path.join(bundle_root, "configs", "metadata.json") if meta_file_ is None else meta_file_
     if os.path.exists(meta_file_):
         parser.read_meta(f=meta_file_)
 
+    # the rest key-values in the _args are to override config content
+    for k, v in _args.items():
+        parser[k] = v
+
+    filepath_ = os.path.join(bundle_root, "models", "model.ts") if filepath_ is None else filepath_
+    ckpt_file_ = os.path.join(bundle_root, "models", "model.pt") if ckpt_file_ is None else ckpt_file_
+    if not os.path.exists(ckpt_file_):
+        raise FileNotFoundError(f'Checkpoint file "{ckpt_file_}" not found, please specify it in argument "ckpt_file".')
+
     net_id_ = "network_def" if net_id_ is None else net_id_
     try:
         parser.get_parsed_content(net_id_)
@@ -1313,10 +1316,6 @@ def ckpt_export(
             f'Network definition "{net_id_}" cannot be found in "{config_file_}", specify name with argument "net_id".'
         ) from e
 
-    # the rest key-values in the _args are to override config content
-    for k, v in _args.items():
-        parser[k] = v
-
     # When export through torch.jit.trace without providing input_shape, will try to parse one from the parser.
     if (not input_shape_) and use_trace:
         input_shape_ = _get_fake_input_shape(parser=parser)


Test patch: