(self, data, data_hashfile, meta_hash_file_name)
| 1684 | return _item_transformed |
| 1685 | |
| 1686 | def _create_new_cache(self, data, data_hashfile, meta_hash_file_name): |
| 1687 | self._meta_cache[meta_hash_file_name] = copy(data.meta) if isinstance(data, MetaTensor) else {} |
| 1688 | _item_transformed_data = data.array if isinstance(data, MetaTensor) else data |
| 1689 | if isinstance(_item_transformed_data, torch.Tensor): |
| 1690 | _item_transformed_data = _item_transformed_data.numpy() |
| 1691 | self._meta_cache[meta_hash_file_name]["shape"] = _item_transformed_data.shape |
| 1692 | self._meta_cache[meta_hash_file_name]["dtype"] = str(_item_transformed_data.dtype) |
| 1693 | kvikio_numpy.tofile(_item_transformed_data, data_hashfile) |
| 1694 | try: |
| 1695 | # NOTE: Writing to a temporary directory and then using a nearly atomic rename operation |
| 1696 | # to make the cache more robust to manual killing of parent process |
| 1697 | # which may leave partially written cache files in an incomplete state |
| 1698 | with tempfile.TemporaryDirectory() as tmpdirname: |
| 1699 | meta_hash_file = self.cache_dir / meta_hash_file_name |
| 1700 | temp_hash_file = Path(tmpdirname) / meta_hash_file_name |
| 1701 | torch.save( |
| 1702 | obj=convert_to_tensor(self._meta_cache[meta_hash_file_name], convert_numeric=False), |
| 1703 | f=temp_hash_file, |
| 1704 | pickle_module=look_up_option(self.pickle_module, SUPPORTED_PICKLE_MOD), |
| 1705 | pickle_protocol=self.pickle_protocol, |
| 1706 | ) |
| 1707 | if temp_hash_file.is_file() and not meta_hash_file.is_file(): |
| 1708 | # On Unix, if target exists and is a file, it will be replaced silently if the |
| 1709 | # user has permission. |
| 1710 | # for more details: https://docs.python.org/3/library/shutil.html#shutil.move. |
| 1711 | try: |
| 1712 | shutil.move(str(temp_hash_file), meta_hash_file) |
| 1713 | except FileExistsError: |
| 1714 | pass |
| 1715 | except PermissionError: # project-monai/monai issue #3613 |
| 1716 | pass |
| 1717 | |
| 1718 | def _load_meta_cache(self, meta_hash_file_name): |
| 1719 | if meta_hash_file_name in self._meta_cache: |
no test coverage detected