Skip to content

enhances auto3dseg data analyzer info #6758

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
Jul 22, 2023
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: 8 additions & 6 deletions monai/apps/auto3dseg/data_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class DataAnalyzer:
the DataAnalyzer will skip looking for labels and all label-related operations.
hist_bins: bins to compute histogram for each image channel.
hist_range: ranges to compute histogram for each image channel.
fmt: format used to save the analysis results. Defaults to "yaml".
fmt: format used to save the analysis results. Currently support ``"json"`` and ``"yaml"``, defaults to "yaml".
histogram_only: whether to only compute histograms. Defaults to False.
extra_params: other optional arguments. Currently supported arguments are :
'allowed_shape_difference' (default 5) can be used to change the default tolerance of
Expand Down Expand Up @@ -164,6 +164,7 @@ def _check_data_uniformity(keys: list[str], result: dict) -> bool:
constant_props = [result[DataStatsKeys.SUMMARY][DataStatsKeys.IMAGE_STATS][key] for key in keys]
for prop in constant_props:
if "stdev" in prop and np.any(prop["stdev"]):
logger.debug(f"summary image_stats {prop} has non-zero stdev {prop['stdev']}.")
return False

return True
Expand Down Expand Up @@ -242,15 +243,16 @@ def get_all_case_stats(self, key="training", transform_list=None):
if not self._check_data_uniformity([ImageStatsKeys.SPACING], result):
logger.info("Data spacing is not completely uniform. MONAI transforms may provide unexpected result")
if self.output_path:
logger.info(f"Writing data stats to {self.output_path}.")
ConfigParser.export_config_file(
result, self.output_path, fmt=self.fmt, default_flow_style=None, sort_keys=False
)
by_case_path = self.output_path.replace(f".{self.fmt}", f"_by_case.{self.fmt}")
if by_case_path == self.output_path: # self.output_path not ended with self.fmt?
by_case_path += f".by_case.{self.fmt}"
logger.info(f"Writing by-case data stats to {by_case_path}, this may take a while.")
ConfigParser.export_config_file(
result_bycase,
self.output_path.replace(".yaml", "_by_case.yaml"),
fmt=self.fmt,
default_flow_style=None,
sort_keys=False,
result_bycase, by_case_path, fmt=self.fmt, default_flow_style=None, sort_keys=False
)
# release memory
if self.device.type == "cuda":
Expand Down
4 changes: 2 additions & 2 deletions monai/bundle/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ def export_config_file(cls, config: dict, filepath: PathLike, fmt: str = "json",

"""
_filepath: str = str(Path(filepath))
writer = look_up_option(fmt.lower(), {"json", "yaml"})
writer = look_up_option(fmt.lower(), {"json", "yaml", "yml"})
with open(_filepath, "w") as f:
if writer == "json":
json.dump(config, f, **kwargs)
return
if writer == "yaml":
if writer == "yaml" or writer == "yml":
return yaml.safe_dump(config, f, **kwargs)
raise ValueError(f"only support JSON or YAML config file so far, got {writer}.")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_auto3dseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def setUp(self):
work_dir = self.test_dir.name
self.dataroot_dir = os.path.join(work_dir, "sim_dataroot")
self.datalist_file = os.path.join(work_dir, "sim_datalist.json")
self.datastat_file = os.path.join(work_dir, "datastats.yaml")
self.datastat_file = os.path.join(work_dir, "datastats.yml")
ConfigParser.export_config_file(sim_datalist, self.datalist_file)

@parameterized.expand(SIM_CPU_TEST_CASES)
Expand Down