Repository: Project-MONAI/MONAI
File: monai/inferers/merger.py (Python)

Code before:
    def __init__(
        self,
        merged_shape: Sequence[int],
        cropped_shape: Sequence[int] | None = None,
        dtype: np.dtype | str = "float32",
        value_dtype: np.dtype | str = "float32",
        count_dtype: np.dtype | str = "uint8",
        store: zarr.storage.Store | str = "merged.zarr",
        value_store: zarr.storage.Store | str | None = None,
        count_store: zarr.storage.Store | str | None = None,
        compressor: str | None = None,
        value_compressor: str | None = None,
        count_compressor: str | None = None,
        chunks: Sequence[int] | bool = True,
        thread_locking: bool = True,
    ) -> None:
        super().__init__(merged_shape=merged_shape, cropped_shape=cropped_shape)
        if not self.merged_shape:
            raise ValueError(f"`merged_shape` must be provided for `ZarrAvgMerger`. {self.merged_shape} is give.")
        self.output_dtype = dtype
        self.value_dtype = value_dtype
        self.count_dtype = count_dtype
        self.store = store
        if version_geq(get_package_version("zarr"), "3.0.0"):
            if value_store is None:
                with TemporaryDirectory() as tmpdir:
                    self.value_store = zarr.storage.LocalStore(tmpdir)
            else:
                self.value_store = value_store
            if count_store is None:
                with TemporaryDirectory() as tmpdir:
                    self.count_store = zarr.storage.LocalStore(tmpdir)
            else:
                self.count_store = count_store
        else:
            self.value_store = zarr.storage.TempStore() if value_store is None else value_store
            self.count_store = zarr.storage.TempStore() if count_store is None else count_store
        self.chunks = chunks
        self.compressor = compressor
        self.value_compressor = value_compressor
        self.count_compressor = count_compressor
        self.output = zarr.empty(
            shape=self.merged_shape,
            chunks=self.chunks,
            dtype=self.output_dtype,
            compressor=self.compressor,
            store=self.store,
            overwrite=True,
        )
        self.values = zarr.zeros(
            shape=self.merged_shape,

Review comment (suggestion):
`TemporaryDirectory` should delete the directory once the context block if left, is this a problem here? It might not be immediate but we can't rely on it existing outside the block I don't think. If you store an instance of `TemporaryDirectory` as an attribute it will get cleaned up when the object is collected.

Code after:
    def __init__(
        self,
        merged_shape: Sequence[int],
        cropped_shape: Sequence[int] | None = None,
        dtype: np.dtype | str = "float32",
        value_dtype: np.dtype | str = "float32",
        count_dtype: np.dtype | str = "uint8",
        store: zarr.storage.Store | str = "merged.zarr",
        value_store: zarr.storage.Store | str | None = None,
        count_store: zarr.storage.Store | str | None = None,
        compressor: str | None = None,
        value_compressor: str | None = None,
        count_compressor: str | None = None,
        chunks: Sequence[int] | bool = True,
        thread_locking: bool = True,
    ) -> None:
        super().__init__(merged_shape=merged_shape, cropped_shape=cropped_shape)
        if not self.merged_shape:
            raise ValueError(f"`merged_shape` must be provided for `ZarrAvgMerger`. {self.merged_shape} is give.")
        self.output_dtype = dtype
        self.value_dtype = value_dtype
        self.count_dtype = count_dtype
        self.store = store
        self.tmpdir: TemporaryDirectory | None
        if version_geq(get_package_version("zarr"), "3.0.0"):
            if value_store is None:
                self.tmpdir = TemporaryDirectory()
                self.value_store = zarr.storage.LocalStore(self.tmpdir.name)
            else:
                self.value_store = value_store
            if count_store is None:
                self.tmpdir = TemporaryDirectory()
                self.count_store = zarr.storage.LocalStore(self.tmpdir.name)
            else:
                self.count_store = count_store
        else:
            self.tmpdir = None
            self.value_store = zarr.storage.TempStore() if value_store is None else value_store
            self.count_store = zarr.storage.TempStore() if count_store is None else count_store
        self.chunks = chunks
        self.compressor = compressor
        self.value_compressor = value_compressor
        self.count_compressor = count_compressor
        self.output = zarr.empty(
            shape=self.merged_shape,
            chunks=self.chunks,
            dtype=self.output_dtype,
            compressor=self.compressor,
            store=self.store,
            overwrite=True,
        )