Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add code for generating a mapping json file.

Signed-off-by: staydelight <[email protected]>
  • Loading branch information
staydelight committed Jun 13, 2024
1 parent d4fb0b7 commit bfb6d58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions monai/transforms/io/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def __init__(
output_name_formatter: Callable[[dict, Transform], dict] | None = None,
folder_layout: FolderLayoutBase | None = None,
savepath_in_metadict: bool = False,
mapping_log_path: Union[Path, str, None] = None
mapping_json_path: Union[Path, str, None] = None
) -> None:
self.folder_layout: FolderLayoutBase
if folder_layout is None:
Expand Down Expand Up @@ -440,11 +440,11 @@ def __init__(
self.write_kwargs = {"verbose": print_log}
self._data_index = 0
self.savepath_in_metadict = savepath_in_metadict
if mapping_log_path:
self.mapping_log_path = Path(mapping_log_path)
if mapping_json_path:
self.mapping_json_path = Path(mapping_json_path)
self.savepath_in_metadict = True
else:
self.mapping_log_path = None
self.mapping_json_path = None

def set_options(self, init_kwargs=None, data_kwargs=None, meta_kwargs=None, write_kwargs=None):
"""
Expand Down Expand Up @@ -513,20 +513,20 @@ def __call__(
self._data_index += 1
if self.savepath_in_metadict and meta_data is not None:
meta_data["saved_to"] = filename
if self.mapping_log_path and meta_data is not None:
if self.mapping_json_path and meta_data is not None:
log_data = []
log_data.append({
"input": meta_data.get("filename_or_obj", ()),
"output": meta_data.get("saved_to", ())
})

try:
with open(self.mapping_log_path, 'r') as f:
with open(self.mapping_json_path, 'r') as f:
existing_log_data = json.load(f)
except FileNotFoundError:
existing_log_data = []

with open(self.mapping_log_path, 'w') as f:
with open(self.mapping_json_path, 'w') as f:
existing_log_data.extend(log_data)
json.dump(existing_log_data, f, indent=4)
return img
Expand Down
4 changes: 2 additions & 2 deletions monai/transforms/io/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def __init__(
output_name_formatter: Callable[[dict, Transform], dict] | None = None,
folder_layout: monai.data.FolderLayoutBase | None = None,
savepath_in_metadict: bool = False,
mapping_log_path: Union[Path, str, None] = None
mapping_json_path: Union[Path, str, None] = None
) -> None:
super().__init__(keys, allow_missing_keys)
self.meta_keys = ensure_tuple_rep(meta_keys, len(self.keys))
Expand All @@ -305,7 +305,7 @@ def __init__(
output_name_formatter=output_name_formatter,
folder_layout=folder_layout,
savepath_in_metadict=savepath_in_metadict,
mapping_log_path= mapping_log_path,
mapping_json_path= mapping_json_path,
)

def set_options(self, init_kwargs=None, data_kwargs=None, meta_kwargs=None, write_kwargs=None):
Expand Down

0 comments on commit bfb6d58

Please sign in to comment.