Skip to content

Commit 644c9e5

Browse files
authored
enhances auto3dseg data analyzer info (#6758)
### Description - output file name extension to be consistent with `self.fmt` - improve logging messages when writing files - export file format may be spelt as 'yml' instead of 'yaml' ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] 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. Signed-off-by: Wenqi Li <wenqil@nvidia.com>
1 parent 4addc5d commit 644c9e5

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

monai/apps/auto3dseg/data_analyzer.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DataAnalyzer:
7070
the DataAnalyzer will skip looking for labels and all label-related operations.
7171
hist_bins: bins to compute histogram for each image channel.
7272
hist_range: ranges to compute histogram for each image channel.
73-
fmt: format used to save the analysis results. Defaults to "yaml".
73+
fmt: format used to save the analysis results. Currently support ``"json"`` and ``"yaml"``, defaults to "yaml".
7474
histogram_only: whether to only compute histograms. Defaults to False.
7575
extra_params: other optional arguments. Currently supported arguments are :
7676
'allowed_shape_difference' (default 5) can be used to change the default tolerance of
@@ -164,6 +164,7 @@ def _check_data_uniformity(keys: list[str], result: dict) -> bool:
164164
constant_props = [result[DataStatsKeys.SUMMARY][DataStatsKeys.IMAGE_STATS][key] for key in keys]
165165
for prop in constant_props:
166166
if "stdev" in prop and np.any(prop["stdev"]):
167+
logger.debug(f"summary image_stats {prop} has non-zero stdev {prop['stdev']}.")
167168
return False
168169

169170
return True
@@ -242,15 +243,16 @@ def get_all_case_stats(self, key="training", transform_list=None):
242243
if not self._check_data_uniformity([ImageStatsKeys.SPACING], result):
243244
logger.info("Data spacing is not completely uniform. MONAI transforms may provide unexpected result")
244245
if self.output_path:
246+
logger.info(f"Writing data stats to {self.output_path}.")
245247
ConfigParser.export_config_file(
246248
result, self.output_path, fmt=self.fmt, default_flow_style=None, sort_keys=False
247249
)
250+
by_case_path = self.output_path.replace(f".{self.fmt}", f"_by_case.{self.fmt}")
251+
if by_case_path == self.output_path: # self.output_path not ended with self.fmt?
252+
by_case_path += f".by_case.{self.fmt}"
253+
logger.info(f"Writing by-case data stats to {by_case_path}, this may take a while.")
248254
ConfigParser.export_config_file(
249-
result_bycase,
250-
self.output_path.replace(".yaml", "_by_case.yaml"),
251-
fmt=self.fmt,
252-
default_flow_style=None,
253-
sort_keys=False,
255+
result_bycase, by_case_path, fmt=self.fmt, default_flow_style=None, sort_keys=False
254256
)
255257
# release memory
256258
if self.device.type == "cuda":

monai/bundle/config_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,12 @@ def export_config_file(cls, config: dict, filepath: PathLike, fmt: str = "json",
438438
439439
"""
440440
_filepath: str = str(Path(filepath))
441-
writer = look_up_option(fmt.lower(), {"json", "yaml"})
441+
writer = look_up_option(fmt.lower(), {"json", "yaml", "yml"})
442442
with open(_filepath, "w") as f:
443443
if writer == "json":
444444
json.dump(config, f, **kwargs)
445445
return
446-
if writer == "yaml":
446+
if writer == "yaml" or writer == "yml":
447447
return yaml.safe_dump(config, f, **kwargs)
448448
raise ValueError(f"only support JSON or YAML config file so far, got {writer}.")
449449

tests/test_auto3dseg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def setUp(self):
170170
work_dir = self.test_dir.name
171171
self.dataroot_dir = os.path.join(work_dir, "sim_dataroot")
172172
self.datalist_file = os.path.join(work_dir, "sim_datalist.json")
173-
self.datastat_file = os.path.join(work_dir, "datastats.yaml")
173+
self.datastat_file = os.path.join(work_dir, "datastats.yml")
174174
ConfigParser.export_config_file(sim_datalist, self.datalist_file)
175175

176176
@parameterized.expand(SIM_CPU_TEST_CASES)

0 commit comments

Comments
 (0)